1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.tests.permissions; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.adservices.clients.topics.AdvertisingTopicsClient; 24 import android.content.Context; 25 26 import androidx.test.core.app.ApplicationProvider; 27 28 import com.android.adservices.common.CompatAdServicesTestUtils; 29 import com.android.compatibility.common.util.ShellUtils; 30 import com.android.modules.utils.build.SdkLevel; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.junit.runners.JUnit4; 37 38 import java.util.concurrent.ExecutionException; 39 import java.util.concurrent.Executor; 40 import java.util.concurrent.Executors; 41 42 @RunWith(JUnit4.class) 43 // TODO: Add tests for measurement (b/238194122). 44 public class NotInAllowListTest { 45 private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool(); 46 private static final Context sContext = ApplicationProvider.getApplicationContext(); 47 private static final String CALLER_NOT_ALLOWED = 48 "java.lang.SecurityException: Caller is not authorized to call this API. " 49 + "Caller is not allowed."; 50 51 private String mPreviousSignatureAllowList; 52 53 @Before setup()54 public void setup() { 55 if (!SdkLevel.isAtLeastT()) { 56 CompatAdServicesTestUtils.setFlags(); 57 } 58 overrideSignatureAllowListToEmpty(); 59 } 60 61 @After teardown()62 public void teardown() { 63 if (!SdkLevel.isAtLeastT()) { 64 CompatAdServicesTestUtils.resetFlagsToDefault(); 65 } 66 overrideSignatureAllowList(); 67 } 68 69 @Test testNotInAllowList()70 public void testNotInAllowList() { 71 AdvertisingTopicsClient advertisingTopicsClient1 = 72 new AdvertisingTopicsClient.Builder() 73 .setContext(sContext) 74 .setSdkName("sdk1") 75 .setExecutor(CALLBACK_EXECUTOR) 76 .build(); 77 78 ExecutionException exception = 79 assertThrows( 80 ExecutionException.class, () -> advertisingTopicsClient1.getTopics().get()); 81 assertThat(exception.getMessage()).isEqualTo(CALLER_NOT_ALLOWED); 82 } 83 84 // Override Signature Allow List to original overrideSignatureAllowList()85 public void overrideSignatureAllowList() { 86 ShellUtils.runShellCommand( 87 "device_config put adservices ppapi_app_signature_allow_list %s", 88 mPreviousSignatureAllowList); 89 } 90 91 // Override Signature Allow List to deny the signature of this test overrideSignatureAllowListToEmpty()92 public void overrideSignatureAllowListToEmpty() { 93 mPreviousSignatureAllowList = 94 ShellUtils.runShellCommand( 95 "device_config get adservices ppapi_app_signature_allow_list"); 96 ShellUtils.runShellCommand( 97 "device_config put adservices ppapi_app_signature_allow_list %s", "empty"); 98 } 99 } 100