summaryrefslogtreecommitdiff
path: root/tests/tests/carrierapi/src/android/carrierapi/cts/BugreportManagerTest.java
blob: d6e845d452deee5c9ba61ea9ee3357903eb7b47d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.carrierapi.cts;

import static com.android.compatibility.common.util.SystemUtil.runShellCommand;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.fail;

import android.content.pm.PackageManager;
import android.os.BugreportManager;
import android.os.BugreportManager.BugreportCallback;
import android.os.BugreportParams;
import android.os.FileUtils;
import android.os.ParcelFileDescriptor;
import android.platform.test.annotations.SystemUserOnly;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.Direction;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import com.android.compatibility.common.util.CddTest;
import com.android.compatibility.common.util.PollingCheck;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * Unit tests for {@link BugreportManager}'s carrier functionality, specifically "connectivity"
 * bugreports.
 *
 * <p>Structure is largely adapted from
 * frameworks/base/core/tests/bugreports/.../BugreportManagerTest.java.
 *
 * <p>Test using `atest CtsCarrierApiTestCases:BugreportManagerTest` or `make cts -j64 &&
 * cts-tradefed run cts -m CtsCarrierApiTestCases --test
 * android.carrierapi.cts.BugreportManagerTest`
 *
 * <p>TODO(b/211774553) consider enforcing BR content. Will likely have to be a host-side test for
 * performance reasons.
 */
@SystemUserOnly(reason = "BugreportManager requires calls to originate from the primary user")
@RunWith(AndroidJUnit4.class)
public class BugreportManagerTest extends BaseCarrierApiTest {
    private static final String TAG = "BugreportManagerTest";

    // See BugreportManagerServiceImpl#BUGREPORT_SERVICE.
    private static final String BUGREPORT_SERVICE = "bugreportd";

    private static final long BUGREPORT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(10);
    private static final long UIAUTOMATOR_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(10);
    private static final long ONEWAY_CALLBACK_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(5);
    // This value is defined in dumpstate.cpp:TELEPHONY_REPORT_USER_CONSENT_TIMEOUT_MS. Because the
    // consent dialog is so large and important, the user *must* be given at least 2 minutes to read
    // it before it times out.
    private static final long MINIMUM_CONSENT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(2);

    private static final BySelector CONSENT_DIALOG_TITLE_SELECTOR = By.res("android", "alertTitle");

    @Rule public TestName name = new TestName();

    private BugreportManager mBugreportManager;
    private File mBugreportFile;
    private ParcelFileDescriptor mBugreportFd;
    private File mScreenshotFile;
    private ParcelFileDescriptor mScreenshotFd;

    @Before
    public void setUp() throws Exception {
        mBugreportManager = getContext().getSystemService(BugreportManager.class);

        killCurrentBugreportIfRunning();
        mBugreportFile = createTempFile("bugreport_" + name.getMethodName(), ".zip");
        mBugreportFd = parcelFd(mBugreportFile);
        // Should never be written for anything a carrier app can trigger; several tests assert that
        // this file has no content.
        mScreenshotFile = createTempFile("screenshot_" + name.getMethodName(), ".png");
        mScreenshotFd = parcelFd(mScreenshotFile);
    }

    @After
    public void tearDown() throws Exception {
        if (!werePreconditionsSatisfied()) return;

        FileUtils.closeQuietly(mBugreportFd);
        FileUtils.closeQuietly(mScreenshotFd);
        killCurrentBugreportIfRunning();
    }

    @Test
    @CddTest(requirement = "9.8.10/C-1-1")
    public void startConnectivityBugreport() throws Exception {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();

        assertThat(callback.hasEarlyReportFinished()).isFalse();
        mBugreportManager.startConnectivityBugreport(mBugreportFd, Runnable::run, callback);
        setConsentDialogReply(ConsentReply.ALLOW);
        waitUntilDoneOrTimeout(callback);

        assertThat(callback.isSuccess()).isTrue();
        assertThat(callback.hasEarlyReportFinished()).isTrue();
        assertThat(callback.hasReceivedProgress()).isTrue();
        assertThat(mBugreportFile.length()).isGreaterThan(0L);
        assertFdIsClosed(mBugreportFd);
    }

    @Test
    @CddTest(requirement = "9.8.10/C-1-3")
    public void startConnectivityBugreport_consentDenied() throws Exception {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();

        mBugreportManager.startConnectivityBugreport(mBugreportFd, Runnable::run, callback);
        setConsentDialogReply(ConsentReply.DENY);
        waitUntilDoneOrTimeout(callback);

        assertThat(callback.getErrorCode())
                .isEqualTo(BugreportCallback.BUGREPORT_ERROR_USER_DENIED_CONSENT);
        assertThat(callback.hasReceivedProgress()).isTrue();
        assertThat(mBugreportFile.length()).isEqualTo(0L);
        assertFdIsClosed(mBugreportFd);
    }

    @Test
    @CddTest(requirement = "9.8.10/C-1-3")
    public void startConnectivityBugreport_consentTimeout() throws Exception {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();
        long startTimeMillis = System.currentTimeMillis();

        mBugreportManager.startConnectivityBugreport(mBugreportFd, Runnable::run, callback);
        setConsentDialogReply(ConsentReply.NONE_TIMEOUT);
        waitUntilDoneOrTimeout(callback);

        assertThat(callback.getErrorCode())
                .isEqualTo(BugreportCallback.BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT);
        assertThat(callback.hasReceivedProgress()).isTrue();
        assertThat(mBugreportFile.length()).isEqualTo(0L);
        assertFdIsClosed(mBugreportFd);
        // Ensure the dialog was displaying long enough.
        assertThat(System.currentTimeMillis() - startTimeMillis)
                .isAtLeast(MINIMUM_CONSENT_TIMEOUT_MILLIS);
        // The dialog may still be displaying, dismiss it if so.
        dismissConsentDialogIfPresent();
    }

    @Test
    public void simultaneousBugreportsNotAllowed() throws Exception {
        BugreportCallbackImpl callback1 = new BugreportCallbackImpl();
        BugreportCallbackImpl callback2 = new BugreportCallbackImpl();
        File bugreportFile2 = createTempFile("bugreport_2_" + name.getMethodName(), ".zip");
        ParcelFileDescriptor bugreportFd2 = parcelFd(bugreportFile2);

        assertThat(callback1.hasEarlyReportFinished()).isFalse();
        // Start the first report, but don't accept the consent dialog or wait for the callback to
        // complete yet.
        mBugreportManager.startConnectivityBugreport(mBugreportFd, Runnable::run, callback1);

        // Attempting to start a second report immediately gets us a concurrency error.
        mBugreportManager.startConnectivityBugreport(bugreportFd2, Runnable::run, callback2);
        // Since IDumpstateListener#onError is oneway, it's not guaranteed that binder has delivered
        // the callback to us yet, even though BugreportManagerServiceImpl sends it before returning
        // from #startBugreport.
        PollingCheck.check(
                "No terminal callback received for the second bugreport",
                ONEWAY_CALLBACK_TIMEOUT_MILLIS,
                callback2::isDone);
        assertThat(callback2.getErrorCode())
                .isEqualTo(BugreportCallback.BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);

        // Now wait for the first report to complete normally.
        setConsentDialogReply(ConsentReply.ALLOW);
        waitUntilDoneOrTimeout(callback1);

        assertThat(callback1.isSuccess()).isTrue();
        assertThat(callback1.hasEarlyReportFinished()).isTrue();
        assertThat(callback1.hasReceivedProgress()).isTrue();
        assertThat(mBugreportFile.length()).isGreaterThan(0L);
        assertFdIsClosed(mBugreportFd);
        // The second report never got any details filled in.
        assertThat(callback2.hasReceivedProgress()).isFalse();
        assertThat(bugreportFile2.length()).isEqualTo(0L);
        assertFdIsClosed(bugreportFd2);
    }

    @Test
    @CddTest(requirement = "9.8.10/C-1-3")
    public void cancelBugreport() throws Exception {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();

        // Start the report, but don't accept the consent dialog or wait for the callback to
        // complete yet.
        mBugreportManager.startConnectivityBugreport(mBugreportFd, Runnable::run, callback);

        assertThat(callback.isDone()).isFalse();

        // Cancel and wait for the final result.
        mBugreportManager.cancelBugreport();
        waitUntilDoneOrTimeout(callback);

        assertThat(callback.getErrorCode()).isEqualTo(BugreportCallback.BUGREPORT_ERROR_RUNTIME);
        assertThat(mBugreportFile.length()).isEqualTo(0L);
        assertFdIsClosed(mBugreportFd);
    }

    @Test
    @CddTest(requirement = "9.8.10/C-1-1")
    public void startBugreport_connectivityBugreport() throws Exception {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();

        assertThat(callback.hasEarlyReportFinished()).isFalse();
        // Carrier apps that compile with the system SDK have visibility to use this API, so we need
        // to enforce that the additional parameters can't be abused to e.g. surreptitiously capture
        // screenshots.
        mBugreportManager.startBugreport(
                mBugreportFd,
                mScreenshotFd,
                new BugreportParams(BugreportParams.BUGREPORT_MODE_TELEPHONY),
                Runnable::run,
                callback);
        setConsentDialogReply(ConsentReply.ALLOW);
        waitUntilDoneOrTimeout(callback);

        assertThat(callback.isSuccess()).isTrue();
        assertThat(callback.hasEarlyReportFinished()).isTrue();
        assertThat(callback.hasReceivedProgress()).isTrue();
        assertThat(mBugreportFile.length()).isGreaterThan(0L);
        assertFdIsClosed(mBugreportFd);
        // Screenshots are never captured for connectivity bugreports, even if an FD is passed in.
        assertThat(mScreenshotFile.length()).isEqualTo(0L);
        assertFdIsClosed(mScreenshotFd);
    }

    @Test
    public void startBugreport_fullBugreport() throws Exception {
        assertSecurityExceptionThrownForMode(BugreportParams.BUGREPORT_MODE_FULL);
    }

    @Test
    public void startBugreport_interactiveBugreport() throws Exception {
        assertSecurityExceptionThrownForMode(BugreportParams.BUGREPORT_MODE_INTERACTIVE);
    }

    @Test
    public void startBugreport_remoteBugreport() throws Exception {
        assertSecurityExceptionThrownForMode(BugreportParams.BUGREPORT_MODE_REMOTE);
    }

    @Test
    public void startBugreport_wearBugreport() throws Exception {
        assertSecurityExceptionThrownForMode(BugreportParams.BUGREPORT_MODE_WEAR);
    }

    @Test
    public void startBugreport_wifiBugreport() throws Exception {
        assertSecurityExceptionThrownForMode(BugreportParams.BUGREPORT_MODE_WIFI);
    }

    @Test
    public void startBugreport_defaultBugreport() throws Exception {
        // BUGREPORT_MODE_DEFAULT (6) is defined by the AIDL, but isn't accepted by
        // BugreportManagerServiceImpl or exposed in BugreportParams.
        assertExceptionThrownForMode(6, IllegalArgumentException.class);
    }

    @Test
    public void startBugreport_negativeMode() throws Exception {
        assertExceptionThrownForMode(-1, IllegalArgumentException.class);
    }

    @Test
    public void startBugreport_invalidMode() throws Exception {
        // Current max is BUGREPORT_MODE_DEFAULT (6) as defined by the AIDL.
        assertExceptionThrownForMode(7, IllegalArgumentException.class);
    }

    /* Implementatiion of {@link BugreportCallback} that offers wrappers around execution result */
    private static final class BugreportCallbackImpl extends BugreportCallback {
        private int mErrorCode = -1;
        private boolean mSuccess = false;
        private boolean mReceivedProgress = false;
        private boolean mEarlyReportFinished = false;
        private final Object mLock = new Object();

        @Override
        public synchronized void onProgress(float progress) {
            mReceivedProgress = true;
        }

        @Override
        public synchronized void onError(int errorCode) {
            Log.d(TAG, "Bugreport errored");
            mErrorCode = errorCode;
        }

        @Override
        public synchronized void onFinished() {
            Log.d(TAG, "Bugreport finished");
            mSuccess = true;
        }

        @Override
        public synchronized void onEarlyReportFinished() {
            mEarlyReportFinished = true;
        }

        /* Indicates completion; and ended up with a success or error. */
        public synchronized boolean isDone() {
            return (mErrorCode != -1) || mSuccess;
        }

        public synchronized int getErrorCode() {
            return mErrorCode;
        }

        public synchronized boolean isSuccess() {
            return mSuccess;
        }

        public synchronized boolean hasReceivedProgress() {
            return mReceivedProgress;
        }

        public synchronized boolean hasEarlyReportFinished() {
            return mEarlyReportFinished;
        }
    }

    /**
     * Kills the current bugreport if one is in progress to prevent failing test cases from
     * cascading into other cases and causing flakes.
     */
    private static void killCurrentBugreportIfRunning() throws Exception {
        runShellCommand("setprop ctl.stop " + BUGREPORT_SERVICE);
    }

    /** Allow/deny the consent dialog to sharing bugreport data, or just check existence. */
    private enum ConsentReply {
        // Touch the positive button.
        ALLOW,
        // Touch the negative button.
        DENY,
        // Just verify that the dialog has appeared, but make no touches.
        NONE_TIMEOUT,
    }

    private boolean isWear() {
        return getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
    }

    private BySelector getSelectorForConsentDialog() {
        if (isWear()) {
            return By.pkg(getContext().getPackageManager().getPermissionControllerPackageName());
        }
        return CONSENT_DIALOG_TITLE_SELECTOR;
    }

    private void setConsentDialogReply(ConsentReply consentReply) throws Exception {
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // No need to wake + dismiss keyguard here; CTS respects our DISABLE_KEYGUARD permission.
        if (!device.wait(
                Until.hasObject(getSelectorForConsentDialog()), UIAUTOMATOR_TIMEOUT_MILLIS)) {
            fail("The consent dialog can't be found");
        }

        final BySelector replySelector;
        switch (consentReply) {
            case ALLOW:
                Log.d(TAG, "Allow the consent dialog");
                replySelector = By.res("android", "button1");
                break;
            case DENY:
                Log.d(TAG, "Deny the consent dialog");
                replySelector = By.res("android", "button2");
                break;
            case NONE_TIMEOUT:
            default:
                // Not making a choice, just leave the dialog up now that we know it exists. It will
                // eventually time out, but we don't wait for that here.
                return;
        }

        UiObject2 replyButton;
        UiObject2 scrollable =
                device.findObject(By.res("android:id/scrollView").scrollable(true));
        while ((replyButton = device.findObject(replySelector)) == null) {
            // Need to scroll the screen to get to the buttons on some form factors
            // (e.g. on a watch).
            scrollable.scroll(Direction.DOWN, 100);
        }

        assertWithMessage("The button of consent dialog is not found")
                .that(replyButton)
                .isNotNull();
        replyButton.click();

        assertThat(
                        device.wait(
                                Until.gone(getSelectorForConsentDialog()),
                                UIAUTOMATOR_TIMEOUT_MILLIS))
                .isTrue();
    }

    private void dismissConsentDialogIfPresent() throws Exception {
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        if (!device.hasObject(getSelectorForConsentDialog())) {
            return;
        }

        Log.d(
                TAG,
                "Consent dialog still present on the screen even though report finished,"
                        + " dismissing it");
        device.pressBack();
        assertThat(
                        device.wait(
                                Until.gone(getSelectorForConsentDialog()),
                                UIAUTOMATOR_TIMEOUT_MILLIS))
                .isTrue();
    }

    private static void waitUntilDoneOrTimeout(BugreportCallbackImpl callback) throws Exception {
        long startTimeMillis = System.currentTimeMillis();
        while (!callback.isDone()) {
            Thread.sleep(1000);
            if (System.currentTimeMillis() - startTimeMillis >= BUGREPORT_TIMEOUT_MILLIS) {
                Log.w(TAG, "Timed out waiting for bugreport completion");
                break;
            }
            Log.d(TAG, "Waited " + (System.currentTimeMillis() - startTimeMillis + "ms"));
        }
    }

    private void assertSecurityExceptionThrownForMode(int mode) {
        assertExceptionThrownForMode(mode, SecurityException.class);
    }

    private <T extends Throwable> void assertExceptionThrownForMode(
            int mode, Class<T> exceptionType) {
        BugreportCallbackImpl callback = new BugreportCallbackImpl();
        try {
            mBugreportManager.startBugreport(
                    mBugreportFd,
                    mScreenshotFd,
                    new BugreportParams(mode),
                    Runnable::run,
                    callback);
            fail("BugreportMode " + mode + " should cause " + exceptionType.getSimpleName());
        } catch (Throwable thrown) {
            if (!exceptionType.isInstance(thrown)) {
                throw thrown;
            }
        }

        assertThat(callback.isDone()).isFalse();
        assertThat(callback.hasReceivedProgress()).isFalse();
        assertThat(mBugreportFile.length()).isEqualTo(0L);
        assertFdIsClosed(mBugreportFd);
        assertThat(mScreenshotFile.length()).isEqualTo(0L);
        assertFdIsClosed(mScreenshotFd);
    }

    private static File createTempFile(String prefix, String extension) throws Exception {
        File f = File.createTempFile(prefix, extension);
        f.setReadable(true, true);
        f.setWritable(true, true);
        f.deleteOnExit();
        return f;
    }

    private static ParcelFileDescriptor parcelFd(File file) throws Exception {
        return ParcelFileDescriptor.open(
                file, ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_APPEND);
    }

    private static void assertFdIsClosed(ParcelFileDescriptor pfd) {
        try {
            int fd = pfd.getFd();
            fail("Expected ParcelFileDescriptor argument to be closed, but got: " + fd);
        } catch (IllegalStateException expected) {
        }
    }
}