blob: eb5c9538c5c3799dd736320cc55d83154c5bbb76 [file] [log] [blame]
Artem Iglikov5ed1dab2017-05-25 15:56:05 +01001/*
2 * Copyright (C) 2017 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
17package com.android.server.backup;
18
19import static com.google.common.truth.Truth.assertThat;
20
Artem Iglikov19e12272017-05-31 17:12:28 +010021import static org.mockito.Mockito.mock;
22
Artem Iglikov026e9332017-06-01 13:06:29 +010023import android.app.backup.BackupManager;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010024import android.content.ComponentName;
25import android.content.Intent;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010026import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageInfo;
28import android.content.pm.ResolveInfo;
29import android.content.pm.ServiceInfo;
Artem Iglikovbd476ea2017-09-01 10:57:26 +010030import android.os.IBinder;
31import android.platform.test.annotations.Presubmit;
32
33import com.android.server.backup.testing.BackupTransportStub;
34import com.android.server.backup.testing.DefaultPackageManagerWithQueryIntentServicesAsUser;
35import com.android.server.backup.testing.ShadowBackupTransportStub;
Artem Iglikov19e12272017-05-31 17:12:28 +010036import com.android.server.backup.testing.ShadowContextImplForBackup;
Artem Iglikovbd476ea2017-09-01 10:57:26 +010037import com.android.server.backup.testing.TransportBoundListenerStub;
Artem Iglikov026e9332017-06-01 13:06:29 +010038import com.android.server.backup.testing.TransportReadyCallbackStub;
Bernardo Rufinoab953332017-11-22 22:10:32 +000039import com.android.server.backup.transport.TransportClient;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010040
Artem Iglikov19e12272017-05-31 17:12:28 +010041import org.junit.After;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010042import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
Artem Iglikovbd476ea2017-09-01 10:57:26 +010045import org.mockito.MockitoAnnotations;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010046import org.robolectric.RobolectricTestRunner;
47import org.robolectric.RuntimeEnvironment;
48import org.robolectric.annotation.Config;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010049import org.robolectric.res.builder.RobolectricPackageManager;
Artem Iglikovbd476ea2017-09-01 10:57:26 +010050import org.robolectric.shadows.ShadowLog;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010051import org.robolectric.shadows.ShadowLooper;
52
Artem Iglikovbd476ea2017-09-01 10:57:26 +010053import java.util.ArrayList;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010054import java.util.Arrays;
Artem Iglikovbd476ea2017-09-01 10:57:26 +010055import java.util.Collections;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010056import java.util.HashSet;
57import java.util.List;
58
59@RunWith(RobolectricTestRunner.class)
60@Config(
61 manifest = Config.NONE,
62 sdk = 23,
Artem Iglikovbd476ea2017-09-01 10:57:26 +010063 shadows = {
Artem Iglikov19e12272017-05-31 17:12:28 +010064 ShadowContextImplForBackup.class,
Artem Iglikovbd476ea2017-09-01 10:57:26 +010065 ShadowBackupTransportStub.class
66 }
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010067)
Artem Iglikovbd476ea2017-09-01 10:57:26 +010068@Presubmit
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010069public class TransportManagerTest {
70 private static final String PACKAGE_NAME = "some.package.name";
Artem Iglikov19e12272017-05-31 17:12:28 +010071 private static final String ANOTHER_PACKAGE_NAME = "another.package.name";
72
73 private TransportInfo mTransport1;
74 private TransportInfo mTransport2;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010075
76 private RobolectricPackageManager mPackageManager;
77
Artem Iglikovbd476ea2017-09-01 10:57:26 +010078 private final TransportBoundListenerStub mTransportBoundListenerStub =
79 new TransportBoundListenerStub(true);
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010080
Artem Iglikov026e9332017-06-01 13:06:29 +010081 private final TransportReadyCallbackStub mTransportReadyCallbackStub =
82 new TransportReadyCallbackStub();
83
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010084 @Before
Artem Iglikovbd476ea2017-09-01 10:57:26 +010085 public void setUp() throws Exception {
86 MockitoAnnotations.initMocks(this);
87
88 ShadowLog.stream = System.out;
Artem Iglikov5ed1dab2017-05-25 15:56:05 +010089 mPackageManager = new DefaultPackageManagerWithQueryIntentServicesAsUser(
90 RuntimeEnvironment.getAppResourceLoader());
91 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager);
Artem Iglikovbd476ea2017-09-01 10:57:26 +010092
Artem Iglikov19e12272017-05-31 17:12:28 +010093 mTransport1 = new TransportInfo(PACKAGE_NAME, "transport1.name");
94 mTransport2 = new TransportInfo(PACKAGE_NAME, "transport2.name");
95
96 ShadowContextImplForBackup.sComponentBinderMap.put(mTransport1.componentName,
97 mTransport1.binder);
98 ShadowContextImplForBackup.sComponentBinderMap.put(mTransport2.componentName,
99 mTransport2.binder);
100 ShadowBackupTransportStub.sBinderTransportMap.put(mTransport1.binder, mTransport1.stub);
101 ShadowBackupTransportStub.sBinderTransportMap.put(mTransport2.binder, mTransport2.stub);
102 }
103
104 @After
105 public void tearDown() throws Exception {
106 ShadowContextImplForBackup.resetBackupShadowState();
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100107 }
108
109 @Test
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100110 public void onPackageAdded_bindsToAllTransports() throws Exception {
Artem Iglikov19e12272017-05-31 17:12:28 +0100111 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100112 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100113
114 TransportManager transportManager = new TransportManager(
115 RuntimeEnvironment.application.getApplicationContext(),
Artem Iglikov19e12272017-05-31 17:12:28 +0100116 new HashSet<>(Arrays.asList(
117 mTransport1.componentName, mTransport2.componentName)),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100118 null /* defaultTransport */,
119 mTransportBoundListenerStub,
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100120 ShadowLooper.getMainLooper());
121 transportManager.onPackageAdded(PACKAGE_NAME);
122
123 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
Artem Iglikov19e12272017-05-31 17:12:28 +0100124 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100125 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
Artem Iglikov19e12272017-05-31 17:12:28 +0100126 Arrays.asList(mTransport1.name, mTransport2.name));
127 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isTrue();
128 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue();
129 }
130
131 @Test
132 public void onPackageAdded_oneTransportUnavailable_bindsToOnlyOneTransport() throws Exception {
133 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
134 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
135
136 ShadowContextImplForBackup.sUnbindableComponents.add(mTransport1.componentName);
137
138 TransportManager transportManager = new TransportManager(
139 RuntimeEnvironment.application.getApplicationContext(),
140 new HashSet<>(Arrays.asList(
141 mTransport1.componentName, mTransport2.componentName)),
142 null /* defaultTransport */,
143 mTransportBoundListenerStub,
144 ShadowLooper.getMainLooper());
145 transportManager.onPackageAdded(PACKAGE_NAME);
146
147 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
148 Collections.singleton(mTransport2.componentName));
149 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
150 Collections.singleton(mTransport2.name));
151 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
152 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue();
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100153 }
154
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100155 @Test
156 public void onPackageAdded_whitelistIsNull_doesNotBindToTransports() throws Exception {
Artem Iglikov19e12272017-05-31 17:12:28 +0100157 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100158 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100159
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100160 TransportManager transportManager = new TransportManager(
161 RuntimeEnvironment.application.getApplicationContext(),
162 null /* whitelist */,
163 null /* defaultTransport */,
164 mTransportBoundListenerStub,
165 ShadowLooper.getMainLooper());
166 transportManager.onPackageAdded(PACKAGE_NAME);
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100167
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100168 assertThat(transportManager.getAllTransportComponents()).isEmpty();
169 assertThat(transportManager.getBoundTransportNames()).isEmpty();
170 assertThat(mTransportBoundListenerStub.isCalled()).isFalse();
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100171 }
172
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100173 @Test
174 public void onPackageAdded_onlyOneTransportWhitelisted_onlyConnectsToWhitelistedTransport()
175 throws Exception {
Artem Iglikov19e12272017-05-31 17:12:28 +0100176 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100177 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100178
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100179 TransportManager transportManager = new TransportManager(
180 RuntimeEnvironment.application.getApplicationContext(),
Artem Iglikov19e12272017-05-31 17:12:28 +0100181 new HashSet<>(Collections.singleton(mTransport2.componentName)),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100182 null /* defaultTransport */,
183 mTransportBoundListenerStub,
184 ShadowLooper.getMainLooper());
185 transportManager.onPackageAdded(PACKAGE_NAME);
186
187 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
Artem Iglikov19e12272017-05-31 17:12:28 +0100188 Collections.singleton(mTransport2.componentName));
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100189 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
Artem Iglikov19e12272017-05-31 17:12:28 +0100190 Collections.singleton(mTransport2.name));
191 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
192 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue();
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100193 }
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100194
195 @Test
196 public void onPackageAdded_appIsNotPrivileged_doesNotBindToTransports() throws Exception {
Artem Iglikov19e12272017-05-31 17:12:28 +0100197 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), 0);
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100198
199 TransportManager transportManager = new TransportManager(
200 RuntimeEnvironment.application.getApplicationContext(),
Artem Iglikov19e12272017-05-31 17:12:28 +0100201 new HashSet<>(Arrays.asList(
202 mTransport1.componentName, mTransport2.componentName)),
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100203 null /* defaultTransport */,
204 mTransportBoundListenerStub,
205 ShadowLooper.getMainLooper());
206 transportManager.onPackageAdded(PACKAGE_NAME);
207
208 assertThat(transportManager.getAllTransportComponents()).isEmpty();
209 assertThat(transportManager.getBoundTransportNames()).isEmpty();
210 assertThat(mTransportBoundListenerStub.isCalled()).isFalse();
211 }
212
Artem Iglikov19e12272017-05-31 17:12:28 +0100213 @Test
214 public void onPackageRemoved_transportsUnbound() throws Exception {
215 TransportManager transportManager = createTransportManagerAndSetUpTransports(
216 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
217
218 transportManager.onPackageRemoved(PACKAGE_NAME);
219
220 assertThat(transportManager.getAllTransportComponents()).isEmpty();
221 assertThat(transportManager.getBoundTransportNames()).isEmpty();
222 }
223
224 @Test
225 public void onPackageRemoved_incorrectPackageName_nothingHappens() throws Exception {
226 TransportManager transportManager = createTransportManagerAndSetUpTransports(
227 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
228
229 transportManager.onPackageRemoved(ANOTHER_PACKAGE_NAME);
230
231 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
232 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
233 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
234 Arrays.asList(mTransport1.name, mTransport2.name));
235 }
236
237 @Test
238 public void onPackageChanged_oneComponentChanged_onlyOneTransportRebound() throws Exception {
239 TransportManager transportManager = createTransportManagerAndSetUpTransports(
240 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
241
242 transportManager.onPackageChanged(PACKAGE_NAME, new String[]{mTransport2.name});
243
244 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
245 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
246 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
247 Arrays.asList(mTransport1.name, mTransport2.name));
248 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
249 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue();
250 }
251
252 @Test
253 public void onPackageChanged_nothingChanged_noTransportsRebound() throws Exception {
254 TransportManager transportManager = createTransportManagerAndSetUpTransports(
255 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
256
257 transportManager.onPackageChanged(PACKAGE_NAME, new String[0]);
258
259 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
260 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
261 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
262 Arrays.asList(mTransport1.name, mTransport2.name));
263 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
264 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isFalse();
265 }
266
267 @Test
268 public void onPackageChanged_unexpectedComponentChanged_noTransportsRebound() throws Exception {
269 TransportManager transportManager = createTransportManagerAndSetUpTransports(
270 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
271
272 transportManager.onPackageChanged(PACKAGE_NAME, new String[]{"unexpected.component"});
273
274 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
275 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
276 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
277 Arrays.asList(mTransport1.name, mTransport2.name));
278 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
279 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isFalse();
280 }
281
282 @Test
283 public void onPackageChanged_transportsRebound() throws Exception {
284 TransportManager transportManager = createTransportManagerAndSetUpTransports(
285 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
286
287 transportManager.onPackageChanged(PACKAGE_NAME, new String[]{mTransport2.name});
288
289 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
290 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
291 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
292 Arrays.asList(mTransport1.name, mTransport2.name));
293 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse();
294 assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue();
295 }
296
297 @Test
298 public void getTransportBinder_returnsCorrectBinder() throws Exception {
299 TransportManager transportManager = createTransportManagerAndSetUpTransports(
300 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
301
302 assertThat(transportManager.getTransportBinder(mTransport1.name)).isEqualTo(
303 mTransport1.stub);
304 assertThat(transportManager.getTransportBinder(mTransport2.name)).isEqualTo(
305 mTransport2.stub);
306 }
307
308 @Test
309 public void getTransportBinder_incorrectTransportName_returnsNull() throws Exception {
310 TransportManager transportManager = createTransportManagerAndSetUpTransports(
311 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
312
313 assertThat(transportManager.getTransportBinder("incorrect.transport")).isNull();
314 }
315
316 @Test
317 public void getTransportBinder_oneTransportUnavailable_returnsCorrectBinder() throws Exception {
318 TransportManager transportManager =
319 createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2),
320 Collections.singletonList(mTransport1), mTransport1.name);
321
322 assertThat(transportManager.getTransportBinder(mTransport1.name)).isNull();
323 assertThat(transportManager.getTransportBinder(mTransport2.name)).isEqualTo(
324 mTransport2.stub);
325 }
326
327 @Test
328 public void getCurrentTransport_selectTransportNotCalled_returnsDefaultTransport()
329 throws Exception {
330 TransportManager transportManager = createTransportManagerAndSetUpTransports(
331 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
332
333 assertThat(transportManager.getCurrentTransportName()).isEqualTo(mTransport1.name);
334 }
335
336 @Test
337 public void getCurrentTransport_selectTransportCalled_returnsCorrectTransport()
338 throws Exception {
339 TransportManager transportManager = createTransportManagerAndSetUpTransports(
340 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
341
342 assertThat(transportManager.getCurrentTransportName()).isEqualTo(mTransport1.name);
343
344 transportManager.selectTransport(mTransport2.name);
345
346 assertThat(transportManager.getCurrentTransportName()).isEqualTo(mTransport2.name);
347 }
348
349 @Test
350 public void getCurrentTransportBinder_returnsCorrectBinder() throws Exception {
351 TransportManager transportManager = createTransportManagerAndSetUpTransports(
352 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
353
354 assertThat(transportManager.getCurrentTransportBinder()).isEqualTo(mTransport1.stub);
355 }
356
357 @Test
358 public void getCurrentTransportBinder_transportNotBound_returnsNull() throws Exception {
359 TransportManager transportManager =
360 createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2),
361 Collections.singletonList(mTransport1), mTransport2.name);
362
363 transportManager.selectTransport(mTransport1.name);
364
365 assertThat(transportManager.getCurrentTransportBinder()).isNull();
366 }
367
368 @Test
369 public void getTransportName_returnsCorrectTransportName() throws Exception {
370 TransportManager transportManager = createTransportManagerAndSetUpTransports(
371 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
372
373 assertThat(transportManager.getTransportName(mTransport1.stub)).isEqualTo(mTransport1.name);
374 assertThat(transportManager.getTransportName(mTransport2.stub)).isEqualTo(mTransport2.name);
375 }
376
377 @Test
378 public void getTransportName_transportNotBound_returnsNull() throws Exception {
379 TransportManager transportManager =
380 createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2),
381 Collections.singletonList(mTransport1), mTransport1.name);
382
383 assertThat(transportManager.getTransportName(mTransport1.stub)).isNull();
384 assertThat(transportManager.getTransportName(mTransport2.stub)).isEqualTo(mTransport2.name);
385 }
386
387 @Test
388 public void getTransportWhitelist_returnsCorrectWhiteList() throws Exception {
389 TransportManager transportManager = new TransportManager(
390 RuntimeEnvironment.application.getApplicationContext(),
391 new HashSet<>(Arrays.asList(mTransport1.componentName, mTransport2.componentName)),
392 mTransport1.name,
393 mTransportBoundListenerStub,
394 ShadowLooper.getMainLooper());
395
396 assertThat(transportManager.getTransportWhitelist()).containsExactlyElementsIn(
397 Arrays.asList(mTransport1.componentName, mTransport2.componentName));
398 }
399
400 @Test
401 public void getTransportWhitelist_whiteListIsNull_returnsEmptyArray() throws Exception {
402 TransportManager transportManager = new TransportManager(
403 RuntimeEnvironment.application.getApplicationContext(),
404 null /* whitelist */,
405 mTransport1.name,
406 mTransportBoundListenerStub,
407 ShadowLooper.getMainLooper());
408
409 assertThat(transportManager.getTransportWhitelist()).isEmpty();
410 }
411
412 @Test
413 public void selectTransport_setsTransportCorrectlyAndReturnsPreviousTransport()
414 throws Exception {
415 TransportManager transportManager = new TransportManager(
416 RuntimeEnvironment.application.getApplicationContext(),
417 null /* whitelist */,
418 mTransport1.name,
419 mTransportBoundListenerStub,
420 ShadowLooper.getMainLooper());
421
422 assertThat(transportManager.selectTransport(mTransport2.name)).isEqualTo(mTransport1.name);
423 assertThat(transportManager.selectTransport(mTransport1.name)).isEqualTo(mTransport2.name);
424 }
425
Artem Iglikov026e9332017-06-01 13:06:29 +0100426 @Test
427 public void ensureTransportReady_transportNotYetBound_callsListenerOnFailure()
428 throws Exception {
429 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
430 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
431
432 TransportManager transportManager = new TransportManager(
433 RuntimeEnvironment.application.getApplicationContext(),
434 new HashSet<>(Arrays.asList(mTransport1.componentName, mTransport2.componentName)),
435 mTransport1.name,
436 mTransportBoundListenerStub,
437 ShadowLooper.getMainLooper());
438
439 transportManager.ensureTransportReady(mTransport1.componentName,
440 mTransportReadyCallbackStub);
441
442 assertThat(mTransportReadyCallbackStub.getSuccessCalls()).isEmpty();
443 assertThat(mTransportReadyCallbackStub.getFailureCalls()).containsExactlyElementsIn(
444 Collections.singleton(
445 BackupManager.ERROR_TRANSPORT_UNAVAILABLE));
446 }
447
448 @Test
449 public void ensureTransportReady_transportCannotBeBound_callsListenerOnFailure()
450 throws Exception {
451 TransportManager transportManager =
452 createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2),
453 Collections.singletonList(mTransport1), mTransport1.name);
454
455 transportManager.ensureTransportReady(mTransport1.componentName,
456 mTransportReadyCallbackStub);
457
458 assertThat(mTransportReadyCallbackStub.getSuccessCalls()).isEmpty();
459 assertThat(mTransportReadyCallbackStub.getFailureCalls()).containsExactlyElementsIn(
460 Collections.singleton(
461 BackupManager.ERROR_TRANSPORT_UNAVAILABLE));
462 }
463
464 @Test
465 public void ensureTransportReady_transportsAlreadyBound_callsListenerOnSuccess()
466 throws Exception {
467 TransportManager transportManager =
468 createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2),
469 Collections.singletonList(mTransport1), mTransport1.name);
470
471 transportManager.ensureTransportReady(mTransport2.componentName,
472 mTransportReadyCallbackStub);
473
474 assertThat(mTransportReadyCallbackStub.getSuccessCalls()).containsExactlyElementsIn(
475 Collections.singleton(mTransport2.name));
476 assertThat(mTransportReadyCallbackStub.getFailureCalls()).isEmpty();
477 }
478
Bernardo Rufinoab953332017-11-22 22:10:32 +0000479 @Test
480 public void getTransportClient_forRegisteredTransport_returnCorrectly() throws Exception {
481 TransportManager transportManager =
482 createTransportManagerAndSetUpTransports(
483 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
484
485 TransportClient transportClient =
486 transportManager.getTransportClient(mTransport1.name, "caller");
487
488 assertThat(transportClient.getTransportComponent()).isEqualTo(mTransport1.componentName);
489 }
490
491 @Test
492 public void getTransportClient_forOldNameOfTransportThatChangedName_returnsNull()
493 throws Exception {
494 TransportManager transportManager =
495 createTransportManagerAndSetUpTransports(
496 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
497 transportManager.describeTransport(
498 mTransport1.componentName, "newName", null, "destinationString", null, null);
499
500 TransportClient transportClient =
501 transportManager.getTransportClient(mTransport1.name, "caller");
502
503 assertThat(transportClient).isNull();
504 }
505
506 @Test
507 public void getTransportClient_forNewNameOfTransportThatChangedName_returnsCorrectly()
508 throws Exception {
509 TransportManager transportManager =
510 createTransportManagerAndSetUpTransports(
511 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
512 transportManager.describeTransport(
513 mTransport1.componentName, "newName", null, "destinationString", null, null);
514
515 TransportClient transportClient =
516 transportManager.getTransportClient("newName", "caller");
517
518 assertThat(transportClient.getTransportComponent()).isEqualTo(mTransport1.componentName);
519 }
520
521 @Test
522 public void getTransportName_forTransportThatChangedName_returnsNewName()
523 throws Exception {
524 TransportManager transportManager =
525 createTransportManagerAndSetUpTransports(
526 Arrays.asList(mTransport1, mTransport2), mTransport1.name);
527 transportManager.describeTransport(
528 mTransport1.componentName, "newName", null, "destinationString", null, null);
529
530 String transportName = transportManager.getTransportName(mTransport1.componentName);
531
532 assertThat(transportName).isEqualTo("newName");
533 }
534
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000535 @Test
536 public void isTransportRegistered_returnsCorrectly() throws Exception {
537 TransportManager transportManager =
538 createTransportManagerAndSetUpTransports(
539 Collections.singletonList(mTransport1),
540 Collections.singletonList(mTransport2),
541 mTransport1.name);
542
543 assertThat(transportManager.isTransportRegistered(mTransport1.name)).isTrue();
544 assertThat(transportManager.isTransportRegistered(mTransport2.name)).isFalse();
545 }
546
Artem Iglikov19e12272017-05-31 17:12:28 +0100547 private void setUpPackageWithTransports(String packageName, List<TransportInfo> transports,
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100548 int flags) throws Exception {
549 PackageInfo packageInfo = new PackageInfo();
550 packageInfo.packageName = packageName;
551 packageInfo.applicationInfo = new ApplicationInfo();
552 packageInfo.applicationInfo.privateFlags = flags;
553
554 mPackageManager.addPackage(packageInfo);
555
556 List<ResolveInfo> transportsInfo = new ArrayList<>();
Artem Iglikov19e12272017-05-31 17:12:28 +0100557 for (TransportInfo transport : transports) {
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100558 ResolveInfo info = new ResolveInfo();
559 info.serviceInfo = new ServiceInfo();
560 info.serviceInfo.packageName = packageName;
Artem Iglikov19e12272017-05-31 17:12:28 +0100561 info.serviceInfo.name = transport.name;
Artem Iglikovbd476ea2017-09-01 10:57:26 +0100562 transportsInfo.add(info);
563 }
564
565 Intent intent = new Intent(TransportManager.SERVICE_ACTION_TRANSPORT_HOST);
566 intent.setPackage(packageName);
567
568 mPackageManager.addResolveInfoForIntent(intent, transportsInfo);
569 }
570
Artem Iglikov19e12272017-05-31 17:12:28 +0100571 private TransportManager createTransportManagerAndSetUpTransports(
572 List<TransportInfo> availableTransports, String defaultTransportName) throws Exception {
573 return createTransportManagerAndSetUpTransports(availableTransports,
574 Collections.<TransportInfo>emptyList(), defaultTransportName);
575 }
576
577 private TransportManager createTransportManagerAndSetUpTransports(
578 List<TransportInfo> availableTransports, List<TransportInfo> unavailableTransports,
579 String defaultTransportName)
580 throws Exception {
581 List<String> availableTransportsNames = new ArrayList<>();
582 List<ComponentName> availableTransportsComponentNames = new ArrayList<>();
583 for (TransportInfo transport : availableTransports) {
584 availableTransportsNames.add(transport.name);
585 availableTransportsComponentNames.add(transport.componentName);
586 }
587
588 List<ComponentName> allTransportsComponentNames = new ArrayList<>();
589 allTransportsComponentNames.addAll(availableTransportsComponentNames);
590 for (TransportInfo transport : unavailableTransports) {
591 allTransportsComponentNames.add(transport.componentName);
592 }
593
594 for (TransportInfo transport : unavailableTransports) {
595 ShadowContextImplForBackup.sUnbindableComponents.add(transport.componentName);
596 }
597
598 setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2),
599 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
600
601 TransportManager transportManager = new TransportManager(
602 RuntimeEnvironment.application.getApplicationContext(),
603 new HashSet<>(allTransportsComponentNames),
604 defaultTransportName,
605 mTransportBoundListenerStub,
606 ShadowLooper.getMainLooper());
607 transportManager.onPackageAdded(PACKAGE_NAME);
608
609 assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn(
610 availableTransportsComponentNames);
611 assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn(
612 availableTransportsNames);
613 for (TransportInfo transport : availableTransports) {
614 assertThat(mTransportBoundListenerStub.isCalledForTransport(transport.stub)).isTrue();
615 }
616 for (TransportInfo transport : unavailableTransports) {
617 assertThat(mTransportBoundListenerStub.isCalledForTransport(transport.stub)).isFalse();
618 }
619
620 mTransportBoundListenerStub.resetState();
621
622 return transportManager;
623 }
624
625 private static class TransportInfo {
626 public final String packageName;
627 public final String name;
628 public final ComponentName componentName;
629 public final BackupTransportStub stub;
630 public final IBinder binder;
631
632 TransportInfo(String packageName, String name) {
633 this.packageName = packageName;
634 this.name = name;
635 this.componentName = new ComponentName(packageName, name);
636 this.stub = new BackupTransportStub(name);
637 this.binder = mock(IBinder.class);
638 }
639 }
Artem Iglikov5ed1dab2017-05-25 15:56:05 +0100640}