summaryrefslogtreecommitdiff
path: root/adservices/tests/cts/src/android/adservices/debuggablects/MeasurementCtsDebuggableTest.java
blob: 5f893c176996b0cf832181a129d6a8a8781e20b1 (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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
 * Copyright (C) 2023 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.adservices.debuggablects;

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

import android.adservices.common.AdServicesOutcomeReceiver;
import android.adservices.measurement.DeletionRequest;
import android.adservices.measurement.MeasurementManager;
import android.adservices.measurement.WebSourceParams;
import android.adservices.measurement.WebSourceRegistrationRequest;
import android.adservices.measurement.WebTriggerParams;
import android.adservices.measurement.WebTriggerRegistrationRequest;
import android.adservices.utils.MockWebServerRule;
import android.content.Context;
import android.net.Uri;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;

import com.android.adservices.common.AdServicesDeviceSupportedRule;
import com.android.adservices.common.AdServicesFlagsSetterRule;
import com.android.adservices.common.AdservicesTestHelper;

import com.google.mockwebserver.MockResponse;
import com.google.mockwebserver.MockWebServer;
import com.google.mockwebserver.RecordedRequest;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/** CTS debuggable test for Measurement API. */
@RunWith(JUnit4.class)
public class MeasurementCtsDebuggableTest {
    protected static final Context sContext = ApplicationProvider.getApplicationContext();
    private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
    private static UiDevice sDevice;

    private static final String SERVER_BASE_URI = replaceTestDomain("https://localhost");
    private static final String WEB_ORIGIN = replaceTestDomain("https://rb-example-origin.test");
    private static final String WEB_DESTINATION =
            replaceTestDomain("https://rb-example-destination.test");

    private static final String PACKAGE_NAME =
            ApplicationProvider.getApplicationContext().getPackageName();

    private static final int DEFAULT_PORT = 38383;
    private static final int KEYS_PORT = 38384;

    private static final long TIMEOUT_IN_MS = 5_000;

    private static final int EVENT_REPORTING_JOB_ID = 3;
    private static final int ATTRIBUTION_REPORTING_JOB_ID = 5;
    private static final int ASYNC_REGISTRATION_QUEUE_JOB_ID = 20;
    private static final int AGGREGATE_REPORTING_JOB_ID = 7;

    private static final String AGGREGATE_ENCRYPTION_KEY_COORDINATOR_ORIGIN =
            SERVER_BASE_URI + ":" + KEYS_PORT;
    private static final String AGGREGATE_ENCRYPTION_KEY_COORDINATOR_PATH = "keys";
    private static final String REGISTRATION_RESPONSE_SOURCE_HEADER =
            "Attribution-Reporting-Register-Source";
    private static final String REGISTRATION_RESPONSE_TRIGGER_HEADER =
            "Attribution-Reporting-Register-Trigger";
    private static final String SOURCE_PATH = "/source";
    private static final String TRIGGER_PATH = "/trigger";
    private static final String AGGREGATE_ATTRIBUTION_REPORT_URI_PATH =
            "/.well-known/attribution-reporting/report-aggregate-attribution";
    private static final String EVENT_ATTRIBUTION_REPORT_URI_PATH =
            "/.well-known/attribution-reporting/report-event-attribution";

    private MeasurementManager mMeasurementManager;

    @Rule(order = 0)
    public final AdServicesDeviceSupportedRule adServicesDeviceSupportedRule =
            new AdServicesDeviceSupportedRule();

    @Rule(order = 1)
    public final AdServicesFlagsSetterRule flags =
            AdServicesFlagsSetterRule.forGlobalKillSwitchDisabledTests()
                    .setCompatModeFlags()
                    .setMeasurementTags();

    @BeforeClass
    public static void setupDevicePropertiesAndInitializeClient() throws Exception {
        setFlagsForMeasurement();
    }

    @AfterClass
    public static void resetDeviceProperties() throws Exception {
        resetFlagsForMeasurement();
    }

    @Before
    public void setup() throws Exception {
        mMeasurementManager = MeasurementManager.get(sContext);
        Objects.requireNonNull(mMeasurementManager);
    }

    @Test
    public void registerSourceAndTriggerAndRunAttributionAndEventReporting() throws Exception {
        executeRegisterSource();
        executeRegisterTrigger();
        executeAttribution();
        executeEventReporting();
        executeDeleteRegistrations();
    }

    @Test
    public void registerSourceAndTriggerAndRunAttributionAndAggregateReporting() throws Exception {
        executeRegisterSource();
        executeRegisterTrigger();
        executeAttribution();
        executeAggregateReporting();
        executeDeleteRegistrations();
    }

    @Test
    public void registerWebSourceAndWebTriggerAndRunAttributionAndEventReporting()
            throws Exception {
        executeRegisterWebSource();
        executeRegisterWebTrigger();
        executeAttribution();
        executeEventReporting();
        executeDeleteRegistrations();
    }

    @Test
    public void registerWebSourceAndWebTriggerAndRunAttributionAndAggregateReporting()
            throws Exception {
        executeRegisterWebSource();
        executeRegisterWebTrigger();
        executeAttribution();
        executeAggregateReporting();
        executeDeleteRegistrations();
    }

    private static UiDevice getUiDevice() {
        if (sDevice == null) {
            sDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        }
        return sDevice;
    }

    private static String replaceTestDomain(String value) {
        return value.replaceAll("test", "com");
    }

    private MockWebServerRule createForHttps(int port) {
        MockWebServerRule mockWebServerRule =
                MockWebServerRule.forHttps(
                        sContext, "adservices_untrusted_test_server.p12", "adservices_test");
        try {
            mockWebServerRule.reserveServerListeningPort(port);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        return mockWebServerRule;
    }

    private MockWebServer startServer(int port, MockResponse... mockResponses) {
        try {
            final MockWebServerRule serverRule = createForHttps(port);
            return serverRule.startMockWebServer(List.of(mockResponses));
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    private void shutdownServer(MockWebServer mockWebServer) {
        try {
            if (mockWebServer != null) mockWebServer.shutdown();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            sleep();
        }
    }

    private static void sleep() {
        sleep(1L);
    }

    private static void sleep(long seconds) {
        try {
            TimeUnit.MILLISECONDS.sleep(seconds * 1000L);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }

    private MockResponse createRegisterSourceResponse() {
        final MockResponse mockRegisterSourceResponse = new MockResponse();
        final String payload =
                "{"
                        + "\"destination\": \"android-app://"
                        + PACKAGE_NAME
                        + "\","
                        + "\"priority\": \"10\","
                        + "\"expiry\": \"1728000\","
                        + "\"source_event_id\": \"11111111111\","
                        + "\"aggregation_keys\": "
                        + "              {"
                        + "                \"campaignCounts\": \"0x159\","
                        + "                \"geoValue\": \"0x5\""
                        + "              }"
                        + "}";

        mockRegisterSourceResponse.setHeader(REGISTRATION_RESPONSE_SOURCE_HEADER, payload);
        mockRegisterSourceResponse.setResponseCode(200);
        return mockRegisterSourceResponse;
    }

    private MockResponse createRegisterTriggerResponse() {
        final MockResponse mockRegisterTriggerResponse = new MockResponse();
        final String payload =
                "{\"event_trigger_data\":"
                        + "[{"
                        + "  \"trigger_data\": \"1\","
                        + "  \"priority\": \"1\","
                        + "  \"deduplication_key\": \"111\""
                        + "}],"
                        + "\"aggregatable_trigger_data\": ["
                        + "              {"
                        + "                \"key_piece\": \"0x200\","
                        + "                \"source_keys\": ["
                        + "                  \"campaignCounts\","
                        + "                  \"geoValue\""
                        + "                ]"
                        + "              }"
                        + "            ],"
                        + "            \"aggregatable_values\": {"
                        + "              \"campaignCounts\": 32768,"
                        + "              \"geoValue\": 1664"
                        + "            }"
                        + "}";

        mockRegisterTriggerResponse.setHeader(REGISTRATION_RESPONSE_TRIGGER_HEADER, payload);
        mockRegisterTriggerResponse.setResponseCode(200);
        return mockRegisterTriggerResponse;
    }

    private MockResponse createRegisterWebSourceResponse() {
        final MockResponse mockRegisterWebSourceResponse = new MockResponse();
        final String payload =
                "{"
                        + "\"web_destination\": \""
                        + WEB_DESTINATION
                        + "\","
                        + "\"priority\": \"10\","
                        + "\"expiry\": \"1728000\","
                        + "\"source_event_id\": \"99999999999\","
                        + "\"aggregation_keys\": "
                        + "              {"
                        + "                \"campaignCounts\": \"0x159\","
                        + "                \"geoValue\": \"0x5\""
                        + "              }"
                        + "}";

        mockRegisterWebSourceResponse.setHeader(REGISTRATION_RESPONSE_SOURCE_HEADER, payload);
        mockRegisterWebSourceResponse.setResponseCode(200);
        return mockRegisterWebSourceResponse;
    }

    private MockResponse createRegisterWebTriggerResponse() {
        final MockResponse mockRegisterWebTriggerResponse = new MockResponse();
        final String payload =
                "{\"event_trigger_data\":"
                        + "[{"
                        + "  \"trigger_data\": \"9\","
                        + "  \"priority\": \"9\","
                        + "  \"deduplication_key\": \"999\""
                        + "}],"
                        + "\"aggregatable_trigger_data\": ["
                        + "              {"
                        + "                \"key_piece\": \"0x200\","
                        + "                \"source_keys\": ["
                        + "                  \"campaignCounts\","
                        + "                  \"geoValue\""
                        + "                ]"
                        + "              }"
                        + "            ],"
                        + "            \"aggregatable_values\": {"
                        + "              \"campaignCounts\": 32768,"
                        + "              \"geoValue\": 1664"
                        + "            }"
                        + "}]}";

        mockRegisterWebTriggerResponse.setHeader(REGISTRATION_RESPONSE_TRIGGER_HEADER, payload);
        mockRegisterWebTriggerResponse.setResponseCode(200);
        return mockRegisterWebTriggerResponse;
    }

    private MockResponse createEventReportUploadResponse() {
        MockResponse reportResponse = new MockResponse();
        reportResponse.setResponseCode(200);
        return reportResponse;
    }

    private MockResponse createAggregateReportUploadResponse() {
        MockResponse reportResponse = new MockResponse();
        reportResponse.setResponseCode(200);
        return reportResponse;
    }

    private MockResponse createGetAggregationKeyResponse() {
        MockResponse mockGetAggregationKeyResponse = new MockResponse();
        final String body =
                "{\"keys\":[{"
                        + "\"id\":\"0fa73e34-c6f3-4839-a4ed-d1681f185a76\","
                        + "\"key\":\"bcy3EsCsm/7rhO1VSl9W+h4MM0dv20xjcFbbLPE16Vg\\u003d\"}]}";

        mockGetAggregationKeyResponse.setBody(body);
        mockGetAggregationKeyResponse.setHeader("age", "14774");
        mockGetAggregationKeyResponse.setHeader("cache-control", "max-age=72795");
        mockGetAggregationKeyResponse.setResponseCode(200);

        return mockGetAggregationKeyResponse;
    }

    private void executeAsyncRegistrationJob() {
        executeJob(ASYNC_REGISTRATION_QUEUE_JOB_ID);
    }

    private void executeJob(int jobId) {
        final String packageName = AdservicesTestHelper.getAdServicesPackageName(sContext);
        final String cmd = "cmd jobscheduler run -f " + packageName + " " + jobId;
        try {
            getUiDevice().executeShellCommand(cmd);
        } catch (IOException e) {
            throw new IllegalStateException(
                    String.format("Error while executing job %d", jobId), e);
        }
    }

    private void executeAttributionJob() {
        executeJob(ATTRIBUTION_REPORTING_JOB_ID);
    }

    private void executeEventReportingJob() {
        executeJob(EVENT_REPORTING_JOB_ID);
    }

    private void executeAggregateReportingJob() {
        executeJob(AGGREGATE_REPORTING_JOB_ID);
    }

    private void executeRegisterSource() {
        final MockResponse mockResponse = createRegisterSourceResponse();
        final MockWebServer mockWebServer = startServer(DEFAULT_PORT, mockResponse);

        try {
            final String path = SERVER_BASE_URI + ":" + mockWebServer.getPort() + SOURCE_PATH;

            final CountDownLatch countDownLatch = new CountDownLatch(1);
            mMeasurementManager.registerSource(
                    Uri.parse(path),
                    /* inputEvent= */ null,
                    CALLBACK_EXECUTOR,
                    (AdServicesOutcomeReceiver<Object, Exception>)
                            result -> countDownLatch.countDown());
            assertThat(countDownLatch.await(TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)).isTrue();

            sleep();
            executeAsyncRegistrationJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(mockWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(SOURCE_PATH);
            assertThat(mockWebServer.getRequestCount()).isEqualTo(1);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error while registering source", e);
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeRegisterTrigger() {
        final MockResponse mockResponse = createRegisterTriggerResponse();
        final MockWebServer mockWebServer = startServer(DEFAULT_PORT, mockResponse);

        try {
            final String path = SERVER_BASE_URI + ":" + mockWebServer.getPort() + TRIGGER_PATH;

            final CountDownLatch countDownLatch = new CountDownLatch(1);
            mMeasurementManager.registerTrigger(
                    Uri.parse(path),
                    CALLBACK_EXECUTOR,
                    (AdServicesOutcomeReceiver<Object, Exception>)
                            result -> countDownLatch.countDown());
            assertThat(countDownLatch.await(TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)).isTrue();

            sleep();
            executeAsyncRegistrationJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(mockWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(TRIGGER_PATH);
            assertThat(mockWebServer.getRequestCount()).isEqualTo(1);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error while registering trigger", e);
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeRegisterWebSource() {
        final MockResponse mockResponse = createRegisterWebSourceResponse();
        final MockWebServer mockWebServer = startServer(DEFAULT_PORT, mockResponse);

        try {
            final String path = SERVER_BASE_URI + ":" + mockWebServer.getPort() + SOURCE_PATH;
            final WebSourceParams params = new WebSourceParams.Builder(Uri.parse(path)).build();
            final WebSourceRegistrationRequest request =
                    new WebSourceRegistrationRequest.Builder(
                                    Collections.singletonList(params), Uri.parse(WEB_ORIGIN))
                            .setWebDestination(Uri.parse(WEB_DESTINATION))
                            .build();

            final CountDownLatch countDownLatch = new CountDownLatch(1);
            mMeasurementManager.registerWebSource(
                    request,
                    CALLBACK_EXECUTOR,
                    (AdServicesOutcomeReceiver<Object, Exception>)
                            result -> countDownLatch.countDown());
            assertThat(countDownLatch.await(TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)).isTrue();

            sleep();
            executeAsyncRegistrationJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(mockWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(SOURCE_PATH);
            assertThat(mockWebServer.getRequestCount()).isEqualTo(1);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error while registering web source", e);
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeRegisterWebTrigger() {
        final MockResponse mockResponse = createRegisterWebTriggerResponse();
        final MockWebServer mockWebServer = startServer(DEFAULT_PORT, mockResponse);

        try {
            final String path = SERVER_BASE_URI + ":" + mockWebServer.getPort() + TRIGGER_PATH;
            final WebTriggerParams params = new WebTriggerParams.Builder(Uri.parse(path)).build();
            final WebTriggerRegistrationRequest request =
                    new WebTriggerRegistrationRequest.Builder(
                                    Collections.singletonList(params), Uri.parse(WEB_DESTINATION))
                            .build();

            final CountDownLatch countDownLatch = new CountDownLatch(1);
            mMeasurementManager.registerWebTrigger(
                    request,
                    CALLBACK_EXECUTOR,
                    (AdServicesOutcomeReceiver<Object, Exception>)
                            result -> countDownLatch.countDown());
            assertThat(countDownLatch.await(TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)).isTrue();

            sleep();
            executeAsyncRegistrationJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(mockWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(TRIGGER_PATH);
            assertThat(mockWebServer.getRequestCount()).isEqualTo(1);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error while registering web trigger", e);
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeAttribution() {
        final MockResponse mockResponse = createGetAggregationKeyResponse();
        final MockWebServer mockWebServer = startServer(KEYS_PORT, mockResponse);

        try {
            sleep();
            executeAttributionJob();
            sleep();
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeEventReporting() {
        final MockResponse mockResponse = createEventReportUploadResponse();
        final MockWebServer mockWebServer = startServer(DEFAULT_PORT, mockResponse, mockResponse);
        try {
            sleep();
            executeEventReportingJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(mockWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(EVENT_ATTRIBUTION_REPORT_URI_PATH);
            assertThat(mockWebServer.getRequestCount()).isEqualTo(1);
        } finally {
            shutdownServer(mockWebServer);
        }
    }

    private void executeAggregateReporting() {
        final MockResponse aggregateReportMockResponse = createAggregateReportUploadResponse();
        final MockWebServer aggregateReportWebServer =
                startServer(DEFAULT_PORT, aggregateReportMockResponse, aggregateReportMockResponse);

        final MockResponse keysMockResponse = createGetAggregationKeyResponse();
        final MockWebServer keysReportWebServer =
                startServer(KEYS_PORT, keysMockResponse, keysMockResponse);

        try {
            sleep();
            executeAggregateReportingJob();
            sleep();

            RecordedRequest recordedRequest = takeRequestTimeoutWrapper(aggregateReportWebServer);
            assertThat(recordedRequest.getPath()).isEqualTo(AGGREGATE_ATTRIBUTION_REPORT_URI_PATH);
            assertThat(aggregateReportWebServer.getRequestCount()).isEqualTo(1);
        } finally {
            shutdownServer(aggregateReportWebServer);
            shutdownServer(keysReportWebServer);
        }
    }

    private void executeDeleteRegistrations() {
        try {
            DeletionRequest deletionRequest =
                    new DeletionRequest.Builder()
                            // Preserve none since empty origin and site lists are provided.
                            .setMatchBehavior(DeletionRequest.MATCH_BEHAVIOR_PRESERVE)
                            .build();
            final CountDownLatch countDownLatch = new CountDownLatch(1);
            mMeasurementManager.deleteRegistrations(
                    deletionRequest,
                    CALLBACK_EXECUTOR,
                    (AdServicesOutcomeReceiver<Object, Exception>)
                            result -> countDownLatch.countDown());
            assertThat(countDownLatch.await(TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)).isTrue();
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error while deleting registrations", e);
        }
    }

    private RecordedRequest takeRequestTimeoutWrapper(MockWebServer mockWebServer) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<RecordedRequest> future = executor.submit(mockWebServer::takeRequest);
        try {
            return future.get(60, TimeUnit.SECONDS);
        } catch (TimeoutException | InterruptedException | ExecutionException e) {
            throw new IllegalStateException("Error while running mockWebServer.takeRequest()", e);
        } finally {
            future.cancel(true);
        }
    }

    private static void setFlagsForMeasurement() throws Exception {
        // Disable device config sync
        getUiDevice().executeShellCommand(
                "device_config set_sync_disabled_for_tests persistent");

        // Override consent notified behavior to give user consent.
        getUiDevice()
                .executeShellCommand("setprop debug.adservices.consent_notified_debug_mode true");

        // Override consent manager behavior to give user consent.
        getUiDevice().executeShellCommand(
                "setprop debug.adservices.consent_manager_debug_mode true");

        // Override the flag to allow current package to call APIs.
        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices msmt_api_app_allow_list " + PACKAGE_NAME);

        // Override the flag to allow current package to call web API.
        getUiDevice().executeShellCommand(
                "device_config put adservices web_context_client_allow_list " + PACKAGE_NAME);

        // Override global kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices global_kill_switch false");

        // Override Ad ID kill switch.
        getUiDevice().executeShellCommand(
                "setprop debug.adservices.adid_kill_switch false");

        // Override measurement kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices measurement_kill_switch false");

        // Override measurement registration job kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices measurement_job_registration_job_queue_kill_switch "
                + "false");

        // Disable foreground checks.
        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enforce_foreground_status_register_source false");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enforce_foreground_status_register_trigger false");

        // Set aggregate key URL.
        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices "
                                + "measurement_aggregation_coordinator_origin_list "
                                + AGGREGATE_ENCRYPTION_KEY_COORDINATOR_ORIGIN);

        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices "
                                + "measurement_default_aggregation_coordinator_origin "
                                + AGGREGATE_ENCRYPTION_KEY_COORDINATOR_ORIGIN);

        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices "
                                + "measurement_aggregation_coordinator_path "
                                + AGGREGATE_ENCRYPTION_KEY_COORDINATOR_PATH);

        // Set reporting windows
        // Assume trigger registration can happen within 8 seconds of source registration.
        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enable_configurable_event_reporting_windows true");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_event_reports_vtc_early_reporting_windows 8,15");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_min_event_report_delay_millis 0");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enable_configurable_aggregate_report_delay true");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_aggregate_report_delay_config 0,0");
        sleep();
    }

    private static void resetFlagsForMeasurement() throws Exception {
        // Reset device config sync
        getUiDevice().executeShellCommand(
                "device_config set_sync_disabled_for_tests null");

        // Reset allowed packages.
        getUiDevice()
                .executeShellCommand("device_config put adservices msmt_api_app_allow_list null");

        // Reset the flag to allow current package to call web API.
        getUiDevice().executeShellCommand(
                "device_config put adservices web_context_client_allow_list null");

        // Reset global kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices global_kill_switch null");

        // Reset Ad ID kill switch.
        getUiDevice().executeShellCommand(
                "setprop debug.adservices.adid_kill_switch null");

        // Reset measurement kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices measurement_kill_switch false");

        // Reset measurement registration job kill switch.
        getUiDevice().executeShellCommand(
                "device_config put adservices measurement_job_registration_job_queue_kill_switch "
                + "null");

        // Reset foreground checks.
        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enforce_foreground_status_register_source null");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enforce_foreground_status_register_trigger null");

        // Reset aggregate key URL.
        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices "
                                + "measurement_default_aggregation_coordinator_origin null");
        getUiDevice()
                .executeShellCommand(
                        "device_config put adservices "
                                + "measurement_aggregation_coordinator_path null");

        // Reset reporting windows
        // Assume trigger registration can happen within 8 seconds of source registration.
        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enable_configurable_event_reporting_windows null");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_event_reports_vtc_early_reporting_windows null");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_min_event_report_delay_millis null");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_enable_configurable_aggregate_report_delay null");

        getUiDevice().executeShellCommand(
                "device_config put adservices "
                + "measurement_aggregate_report_delay_config null");
    }
}