summaryrefslogtreecommitdiff
path: root/adservices/tests/cts/endtoends/permissions/valid/src/com/android/adservices/tests/permissions/PermissionsValidTest.java
blob: 4d8eda8c3cc8094dd5b52973ac6e5660250bd9bb (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
/*
 * Copyright (C) 2022 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 com.android.adservices.tests.permissions;

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

import static org.junit.Assert.assertThrows;

import android.Manifest;
import android.adservices.adselection.AdSelectionConfig;
import android.adservices.adselection.AdSelectionConfigFixture;
import android.adservices.adselection.ReportImpressionRequest;
import android.adservices.clients.adselection.AdSelectionClient;
import android.adservices.clients.customaudience.AdvertisingCustomAudienceClient;
import android.adservices.clients.topics.AdvertisingTopicsClient;
import android.adservices.common.AdTechIdentifier;
import android.adservices.customaudience.CustomAudience;
import android.adservices.topics.GetTopicsResponse;
import android.content.Context;
import android.net.Uri;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.adservices.service.PhFlagsFixture;
import com.android.compatibility.common.util.ShellUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@RunWith(AndroidJUnit4.class)
// TODO: Add tests for measurement (b/238194122).
public class PermissionsValidTest {
    private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
    private static final Context sContext = ApplicationProvider.getApplicationContext();
    private static final String PERMISSION_NOT_REQUESTED =
            "Caller is not authorized to call this API. Permission was not requested.";

    @Before
    public void setup() {
        overrideConsentManagerDebugMode(true);
        overridingAdservicesLoggingLevel("VERBOSE");
        InstrumentationRegistry.getInstrumentation()
                .getUiAutomation()
                .adoptShellPermissionIdentity(Manifest.permission.WRITE_DEVICE_CONFIG);
    }

    @After
    public void teardown() {
        overrideConsentManagerDebugMode(false);
    }

    @Test
    public void testValidPermissions_topics() throws Exception {
        overrideDisableTopicsEnrollmentCheck("1");

        AdvertisingTopicsClient advertisingTopicsClient1 =
                new AdvertisingTopicsClient.Builder()
                        .setContext(sContext)
                        .setSdkName("sdk1")
                        .setExecutor(CALLBACK_EXECUTOR)
                        .build();

        GetTopicsResponse sdk1Result = advertisingTopicsClient1.getTopics().get();
        // Not getting an error here indicates that permissions are valid. The valid case is also
        // tested in TopicsManagerTest.
        assertThat(sdk1Result.getTopics()).isEmpty();
        overrideDisableTopicsEnrollmentCheck("0");
    }

    @Test
    public void testValidPermissions_fledgeJoinCustomAudience()
            throws ExecutionException, InterruptedException {
        PhFlagsFixture.overrideFledgeEnrollmentCheck(false);

        try {
            AdvertisingCustomAudienceClient customAudienceClient =
                    new AdvertisingCustomAudienceClient.Builder()
                            .setContext(sContext)
                            .setExecutor(CALLBACK_EXECUTOR)
                            .build();

            CustomAudience customAudience =
                    new CustomAudience.Builder()
                            .setBuyer(AdTechIdentifier.fromString("test.com"))
                            .setName("exampleCustomAudience")
                            .setDailyUpdateUri(Uri.parse("https://test.com/daily-update"))
                            .setBiddingLogicUri(Uri.parse("https://test.com/bidding-logic"))
                            .build();

            customAudienceClient.joinCustomAudience(customAudience).get();
        } finally {
            PhFlagsFixture.overrideFledgeEnrollmentCheck(true);
        }
    }

    @Test
    public void testValidPermissions_selectAds() {
        PhFlagsFixture.overrideFledgeEnrollmentCheck(false);

        AdSelectionConfig adSelectionConfig = AdSelectionConfigFixture.anAdSelectionConfig();

        try {
            AdSelectionClient mAdSelectionClient =
                    new AdSelectionClient.Builder()
                            .setContext(sContext)
                            .setExecutor(CALLBACK_EXECUTOR)
                            .build();

            ExecutionException exception =
                    assertThrows(
                            ExecutionException.class,
                            () -> mAdSelectionClient.selectAds(adSelectionConfig).get());
            // We only need to get past the permissions check for this test to be valid
            assertThat(exception.getMessage()).isNotEqualTo(PERMISSION_NOT_REQUESTED);
        } finally {
            PhFlagsFixture.overrideFledgeEnrollmentCheck(true);
        }
    }

    @Test
    public void testValidPermissions_reportImpression() {
        PhFlagsFixture.overrideFledgeEnrollmentCheck(false);

        AdSelectionConfig adSelectionConfig = AdSelectionConfigFixture.anAdSelectionConfig();

        long adSelectionId = 1;

        try {
            AdSelectionClient mAdSelectionClient =
                    new AdSelectionClient.Builder()
                            .setContext(sContext)
                            .setExecutor(CALLBACK_EXECUTOR)
                            .build();

            ReportImpressionRequest request =
                    new ReportImpressionRequest(adSelectionId, adSelectionConfig);

            ExecutionException exception =
                    assertThrows(
                            ExecutionException.class,
                            () -> mAdSelectionClient.reportImpression(request).get());
            // We only need to get past the permissions check for this test to be valid
            assertThat(exception.getMessage()).isNotEqualTo(PERMISSION_NOT_REQUESTED);
        } finally {
            PhFlagsFixture.overrideFledgeEnrollmentCheck(true);
        }
    }

    @Test
    public void testValidPermissions_fledgeLeaveCustomAudience()
            throws ExecutionException, InterruptedException {
        PhFlagsFixture.overrideFledgeEnrollmentCheck(false);

        try {
            AdvertisingCustomAudienceClient customAudienceClient =
                    new AdvertisingCustomAudienceClient.Builder()
                            .setContext(sContext)
                            .setExecutor(CALLBACK_EXECUTOR)
                            .build();

            customAudienceClient
                    .leaveCustomAudience(
                            AdTechIdentifier.fromString("test.com"),
                            "exampleCustomAudience")
                    .get();
        } finally {
            PhFlagsFixture.overrideFledgeEnrollmentCheck(true);
        }
    }

    // Override the flag to disable Topics enrollment check.
    private void overrideDisableTopicsEnrollmentCheck(String val) {
        // Setting it to 1 here disables the Topics enrollment check.
        ShellUtils.runShellCommand(
                "setprop debug.adservices.disable_topics_enrollment_check " + val);
    }

    private void overridingAdservicesLoggingLevel(String loggingLevel) {
        ShellUtils.runShellCommand("setprop log.tag.adservices %s", loggingLevel);
    }

    // Override the Consent Manager behaviour - Consent Given
    private void overrideConsentManagerDebugMode(boolean isGiven) {
        ShellUtils.runShellCommand(
                "setprop debug.adservices.consent_manager_debug_mode " + isGiven);
    }
}