• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.tests.sdksandbox;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.Manifest;
24 import android.app.sdksandbox.SandboxedSdk;
25 import android.app.sdksandbox.SdkSandboxManager;
26 import android.app.sdksandbox.testutils.FakeLoadSdkCallback;
27 import android.content.Context;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.provider.DeviceConfig;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 import androidx.test.ext.junit.rules.ActivityScenarioRule;
34 import androidx.test.platform.app.InstrumentationRegistry;
35 
36 import com.android.tests.sdkprovider.restrictions.contentproviders.IContentProvidersSdkApi;
37 
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.junit.runners.JUnit4;
43 
44 @RunWith(JUnit4.class)
45 public class ContentProviderRestrictionsTestApp {
46     private SdkSandboxManager mSdkSandboxManager;
47     private static final String ENFORCE_CONTENT_PROVIDER_RESTRICTIONS =
48             "enforce_content_provider_restrictions";
49 
50     private static final String SDK_PACKAGE =
51             "com.android.tests.sdkprovider.restrictions.contentproviders";
52 
53     /** This rule is defined to start an activity in the foreground to call the sandbox APIs */
54     @Rule
55     public final ActivityScenarioRule mRule =
56             new ActivityScenarioRule<>(SdkSandboxEmptyActivity.class);
57 
58     @Before
setup()59     public void setup() {
60         Context context = ApplicationProvider.getApplicationContext();
61         mSdkSandboxManager = context.getSystemService(SdkSandboxManager.class);
62         assertThat(mSdkSandboxManager).isNotNull();
63         InstrumentationRegistry.getInstrumentation()
64                 .getUiAutomation()
65                 .adoptShellPermissionIdentity(Manifest.permission.WRITE_DEVICE_CONFIG);
66     }
67 
68     @Test
testGetContentProvider_restrictionsApplied()69     public void testGetContentProvider_restrictionsApplied() throws Exception {
70         mRule.getScenario();
71 
72         DeviceConfig.setProperty(
73                 DeviceConfig.NAMESPACE_ADSERVICES,
74                 ENFORCE_CONTENT_PROVIDER_RESTRICTIONS,
75                 "true",
76                 false);
77 
78         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
79         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
80         callback.assertLoadSdkIsSuccessful();
81         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
82 
83         IBinder binder = sandboxedSdk.getInterface();
84         IContentProvidersSdkApi contentProvidersSdkApi =
85                 IContentProvidersSdkApi.Stub.asInterface(binder);
86 
87         assertThrows(SecurityException.class, () -> contentProvidersSdkApi.getContentProvider());
88     }
89 
90     @Test
testRegisterContentObserver_restrictionsApplied()91     public void testRegisterContentObserver_restrictionsApplied() throws Exception {
92         mRule.getScenario();
93 
94         DeviceConfig.setProperty(
95                 DeviceConfig.NAMESPACE_ADSERVICES,
96                 ENFORCE_CONTENT_PROVIDER_RESTRICTIONS,
97                 "true",
98                 false);
99 
100         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
101         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
102         callback.assertLoadSdkIsSuccessful();
103         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
104 
105         IBinder binder = sandboxedSdk.getInterface();
106         IContentProvidersSdkApi contentProvidersSdkApi =
107                 IContentProvidersSdkApi.Stub.asInterface(binder);
108 
109         assertThrows(
110                 SecurityException.class, () -> contentProvidersSdkApi.registerContentObserver());
111     }
112 
113     @Test(expected = Test.None.class /* no exception expected */)
testGetContentProvider_restrictionsNotApplied()114     public void testGetContentProvider_restrictionsNotApplied() throws Exception {
115         mRule.getScenario();
116 
117         DeviceConfig.setProperty(
118                 DeviceConfig.NAMESPACE_ADSERVICES,
119                 ENFORCE_CONTENT_PROVIDER_RESTRICTIONS,
120                 "false",
121                 false);
122 
123         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
124         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
125         callback.assertLoadSdkIsSuccessful();
126         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
127 
128         IBinder binder = sandboxedSdk.getInterface();
129         IContentProvidersSdkApi contentProvidersSdkApi =
130                 IContentProvidersSdkApi.Stub.asInterface(binder);
131 
132         contentProvidersSdkApi.getContentProvider();
133     }
134 
135     @Test(expected = Test.None.class /* no exception expected */)
testRegisterContentObserver_restrictionsNotApplied()136     public void testRegisterContentObserver_restrictionsNotApplied() throws Exception {
137         mRule.getScenario();
138 
139         DeviceConfig.setProperty(
140                 DeviceConfig.NAMESPACE_ADSERVICES,
141                 ENFORCE_CONTENT_PROVIDER_RESTRICTIONS,
142                 "false",
143                 false);
144 
145         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
146         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
147         callback.assertLoadSdkIsSuccessful();
148         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
149 
150         IBinder binder = sandboxedSdk.getInterface();
151         IContentProvidersSdkApi contentProvidersSdkApi =
152                 IContentProvidersSdkApi.Stub.asInterface(binder);
153 
154         contentProvidersSdkApi.registerContentObserver();
155     }
156 
157     @Test(expected = Test.None.class /* no exception expected */)
testGetContentProvider_defaultValueRestrictionsNotApplied()158     public void testGetContentProvider_defaultValueRestrictionsNotApplied() throws Exception {
159         /** Ensuring that the property is not present in DeviceConfig */
160         DeviceConfig.deleteProperty(
161                 DeviceConfig.NAMESPACE_ADSERVICES, ENFORCE_CONTENT_PROVIDER_RESTRICTIONS);
162 
163         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
164         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
165         callback.assertLoadSdkIsSuccessful();
166         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
167 
168         IBinder binder = sandboxedSdk.getInterface();
169         IContentProvidersSdkApi contentProvidersSdkApi =
170                 IContentProvidersSdkApi.Stub.asInterface(binder);
171 
172         contentProvidersSdkApi.getContentProvider();
173     }
174 
175     @Test(expected = Test.None.class /* no exception expected */)
testRegisterContentObserver_defaultValueRestrictionsNotApplied()176     public void testRegisterContentObserver_defaultValueRestrictionsNotApplied() throws Exception {
177         /** Ensuring that the property is not present in DeviceConfig */
178         DeviceConfig.deleteProperty(
179                 DeviceConfig.NAMESPACE_ADSERVICES, ENFORCE_CONTENT_PROVIDER_RESTRICTIONS);
180 
181         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
182         mSdkSandboxManager.loadSdk(SDK_PACKAGE, new Bundle(), Runnable::run, callback);
183         callback.assertLoadSdkIsSuccessful();
184         SandboxedSdk sandboxedSdk = callback.getSandboxedSdk();
185 
186         IBinder binder = sandboxedSdk.getInterface();
187         IContentProvidersSdkApi contentProvidersSdkApi =
188                 IContentProvidersSdkApi.Stub.asInterface(binder);
189 
190         contentProvidersSdkApi.registerContentObserver();
191     }
192 }
193