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
|
/*
* 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 android.app.sdksandbox;
import static com.google.common.truth.Truth.assertThat;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.test.mock.MockContext;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.test.InstrumentationRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Tests {@link SandboxedSdkContext} APIs.
*/
@RunWith(JUnit4.class)
public class SandboxedSdkContextUnitTest {
private SandboxedSdkContext mSandboxedSdkContext;
private static final String CLIENT_PACKAGE_NAME = "com.android.client";
private static final String SDK_NAME = "com.android.codeproviderresources";
private static final String RESOURCES_PACKAGE = "com.android.codeproviderresources_1";
private static final String TEST_INTEGER_KEY = "test_integer";
private static final int TEST_INTEGER_VALUE = 1234;
private static final String TEST_STRING_KEY = "test_string";
private static final String TEST_STRING_VALUE = "Test String";
private static final String TEST_ASSET_FILE = "test-asset.txt";
private static final String TEST_ASSET_VALUE = "This is a test asset";
private static final String SDK_CE_DATA_DIR = "/data/misc_ce/0/sdksandbox/com.foo/sdk@123";
private static final String SDK_DE_DATA_DIR = "/data/misc_de/0/sdksandbox/com.foo/sdk@123";
@Before
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getContext();
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
RESOURCES_PACKAGE,
PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES);
mSandboxedSdkContext =
new SandboxedSdkContext(
InstrumentationRegistry.getContext(),
getClass().getClassLoader(),
CLIENT_PACKAGE_NAME,
info,
SDK_NAME,
SDK_CE_DATA_DIR,
SDK_DE_DATA_DIR);
}
@Test
public void testResources() {
Resources resources = mSandboxedSdkContext.getResources();
assertThat(resources).isNotNull();
int integerId = resources.getIdentifier(TEST_INTEGER_KEY, "integer", RESOURCES_PACKAGE);
assertThat(resources.getInteger(integerId)).isEqualTo(TEST_INTEGER_VALUE);
int stringId = resources.getIdentifier(TEST_STRING_KEY, "string", RESOURCES_PACKAGE);
assertThat(resources.getString(stringId)).isEqualTo(TEST_STRING_VALUE);
}
@Test
public void testSdkName() {
assertThat(mSandboxedSdkContext.getSdkName()).isEqualTo(SDK_NAME);
}
@Test
public void testSdkPackageName() {
assertThat(mSandboxedSdkContext.getSdkPackageName()).isEqualTo(RESOURCES_PACKAGE);
}
@Test
public void testClientPackageName() {
assertThat(mSandboxedSdkContext.getClientPackageName()).isEqualTo(CLIENT_PACKAGE_NAME);
}
@Test
public void testAssets() throws Exception {
AssetManager assets = mSandboxedSdkContext.getAssets();
assertThat(assets).isNotNull();
BufferedReader reader = new BufferedReader(new InputStreamReader(
assets.open(TEST_ASSET_FILE)));
String readAsset = reader.readLine();
assertThat(readAsset).isEqualTo(TEST_ASSET_VALUE);
}
@Test
public void testExposureThroughSandboxedSdkProvider() throws Exception {
TestSdkProvider sdkProvider = new TestSdkProvider();
sdkProvider.attachContext(mSandboxedSdkContext);
assertThat(sdkProvider.getContext()).isSameInstanceAs(mSandboxedSdkContext);
}
@Test
public void testClassLoader() {
assertThat(mSandboxedSdkContext.getClassLoader()).isEqualTo(getClass().getClassLoader());
}
@Test
public void testGetDataDir_CredentialEncrypted() throws Exception {
assertThat(mSandboxedSdkContext.getDataDir().toString()).isEqualTo(SDK_CE_DATA_DIR);
Context superClass = mSandboxedSdkContext;
assertThat(superClass.getDataDir().toString()).isEqualTo(SDK_CE_DATA_DIR);
Context ceContext = mSandboxedSdkContext.createCredentialProtectedStorageContext();
assertThat(ceContext.isCredentialProtectedStorage()).isTrue();
assertThat(ceContext.getDataDir().toString()).isEqualTo(SDK_CE_DATA_DIR);
}
@Test
public void testGetDataDir_DeviceEncrypted() throws Exception {
Context deContext = mSandboxedSdkContext.createDeviceProtectedStorageContext();
assertThat(deContext.isDeviceProtectedStorage()).isTrue();
assertThat(deContext.getDataDir().toString()).isEqualTo(SDK_DE_DATA_DIR);
Context superClass = deContext;
assertThat(superClass.getDataDir().toString()).isEqualTo(SDK_DE_DATA_DIR);
}
@Test
public void testGetSystemService_notRegistered_delegatesToBaseContext() throws Exception {
TestService testService = new TestService(InstrumentationRegistry.getContext());
TestContext testContext = new TestContext(testService);
ApplicationInfo info =
InstrumentationRegistry.getContext()
.getPackageManager()
.getApplicationInfo(
RESOURCES_PACKAGE,
PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES);
SdkSandboxSystemServiceRegistry registry = new SdkSandboxSystemServiceRegistry();
SandboxedSdkContext sandboxedSdkContext =
new SandboxedSdkContext(
testContext,
getClass().getClassLoader(),
CLIENT_PACKAGE_NAME,
info,
SDK_NAME,
SDK_CE_DATA_DIR,
SDK_DE_DATA_DIR,
registry);
assertThat(sandboxedSdkContext.getSystemService("ignored")).isSameInstanceAs(testService);
}
@Test
public void testGetSystemService_registered_mutatesService() throws Exception {
TestService testService = new TestService(InstrumentationRegistry.getContext());
TestContext testContext = new TestContext(testService);
ApplicationInfo info =
InstrumentationRegistry.getContext()
.getPackageManager()
.getApplicationInfo(
RESOURCES_PACKAGE,
PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES);
SdkSandboxSystemServiceRegistry registry = new SdkSandboxSystemServiceRegistry();
registry.registerServiceMutator(
"service", (service, ctx) -> (((TestService) service)).initialize(ctx));
SandboxedSdkContext sandboxedSdkContext =
new SandboxedSdkContext(
testContext,
getClass().getClassLoader(),
CLIENT_PACKAGE_NAME,
info,
SDK_NAME,
SDK_CE_DATA_DIR,
SDK_DE_DATA_DIR,
registry);
TestService service = (TestService) sandboxedSdkContext.getSystemService("service");
assertThat(service.mInitialized).isTrue();
assertThat(service.mCtx).isSameInstanceAs(sandboxedSdkContext);
}
@Test
public void testCachingPerContextIntance() throws Exception {
// TODO(b/242889021): simplify this test after refactoring the c-tor of SandboxedSdkContext
ApplicationInfo info =
InstrumentationRegistry.getContext()
.getPackageManager()
.getApplicationInfo(
RESOURCES_PACKAGE,
PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES);
SandboxedSdkContext ctx1 =
new SandboxedSdkContext(
InstrumentationRegistry.getContext()
.createCredentialProtectedStorageContext(),
getClass().getClassLoader(),
CLIENT_PACKAGE_NAME,
info,
SDK_NAME,
SDK_CE_DATA_DIR,
SDK_DE_DATA_DIR);
SandboxedSdkContext ctx2 =
new SandboxedSdkContext(
InstrumentationRegistry.getContext()
.createCredentialProtectedStorageContext(),
getClass().getClassLoader(),
CLIENT_PACKAGE_NAME,
info,
SDK_NAME,
SDK_CE_DATA_DIR,
SDK_DE_DATA_DIR);
ActivityManager am1 = ctx1.getSystemService(ActivityManager.class);
ActivityManager am2 = ctx2.getSystemService(ActivityManager.class);
assertThat(am1).isNotSameInstanceAs(am2);
}
private static class TestContext extends MockContext {
private final TestService mMockService;
private TestContext(TestService mockService) {
mMockService = mockService;
}
@Override
public Object getSystemService(String serviceName) {
return mMockService;
}
}
private static class TestService {
private boolean mInitialized = false;
private Context mCtx;
private TestService(Context ctx) {
mCtx = ctx;
}
public TestService initialize(Context ctx) {
mInitialized = true;
mCtx = ctx;
return this;
}
}
private static class TestSdkProvider extends SandboxedSdkProvider {
@NonNull
@Override
public SandboxedSdk onLoadSdk(@NonNull Bundle params) throws LoadSdkException {
return null;
}
@NonNull
@Override
public View getView(
@NonNull Context windowContext, @NonNull Bundle params, int width, int height) {
return null;
}
}
}
|