Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.backup; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
| 22 | |
Artem Iglikov | 026e933 | 2017-06-01 13:06:29 +0100 | [diff] [blame] | 23 | import android.app.backup.BackupManager; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Intent; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 26 | import android.content.pm.ApplicationInfo; |
| 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.content.pm.ServiceInfo; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 30 | import android.os.IBinder; |
| 31 | import android.platform.test.annotations.Presubmit; |
| 32 | |
| 33 | import com.android.server.backup.testing.BackupTransportStub; |
| 34 | import com.android.server.backup.testing.DefaultPackageManagerWithQueryIntentServicesAsUser; |
| 35 | import com.android.server.backup.testing.ShadowBackupTransportStub; |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 36 | import com.android.server.backup.testing.ShadowContextImplForBackup; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 37 | import com.android.server.backup.testing.TransportBoundListenerStub; |
Artem Iglikov | 026e933 | 2017-06-01 13:06:29 +0100 | [diff] [blame] | 38 | import com.android.server.backup.testing.TransportReadyCallbackStub; |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 39 | import com.android.server.backup.transport.TransportClient; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 40 | |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 41 | import org.junit.After; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 45 | import org.mockito.MockitoAnnotations; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 46 | import org.robolectric.RobolectricTestRunner; |
| 47 | import org.robolectric.RuntimeEnvironment; |
| 48 | import org.robolectric.annotation.Config; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 49 | import org.robolectric.res.builder.RobolectricPackageManager; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 50 | import org.robolectric.shadows.ShadowLog; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 51 | import org.robolectric.shadows.ShadowLooper; |
| 52 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 53 | import java.util.ArrayList; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 54 | import java.util.Arrays; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 55 | import java.util.Collections; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 56 | import java.util.HashSet; |
| 57 | import java.util.List; |
| 58 | |
| 59 | @RunWith(RobolectricTestRunner.class) |
| 60 | @Config( |
| 61 | manifest = Config.NONE, |
| 62 | sdk = 23, |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 63 | shadows = { |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 64 | ShadowContextImplForBackup.class, |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 65 | ShadowBackupTransportStub.class |
| 66 | } |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 67 | ) |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 68 | @Presubmit |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 69 | public class TransportManagerTest { |
| 70 | private static final String PACKAGE_NAME = "some.package.name"; |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 71 | private static final String ANOTHER_PACKAGE_NAME = "another.package.name"; |
| 72 | |
| 73 | private TransportInfo mTransport1; |
| 74 | private TransportInfo mTransport2; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 75 | |
| 76 | private RobolectricPackageManager mPackageManager; |
| 77 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 78 | private final TransportBoundListenerStub mTransportBoundListenerStub = |
| 79 | new TransportBoundListenerStub(true); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 80 | |
Artem Iglikov | 026e933 | 2017-06-01 13:06:29 +0100 | [diff] [blame] | 81 | private final TransportReadyCallbackStub mTransportReadyCallbackStub = |
| 82 | new TransportReadyCallbackStub(); |
| 83 | |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 84 | @Before |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 85 | public void setUp() throws Exception { |
| 86 | MockitoAnnotations.initMocks(this); |
| 87 | |
| 88 | ShadowLog.stream = System.out; |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 89 | mPackageManager = new DefaultPackageManagerWithQueryIntentServicesAsUser( |
| 90 | RuntimeEnvironment.getAppResourceLoader()); |
| 91 | RuntimeEnvironment.setRobolectricPackageManager(mPackageManager); |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 92 | |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 93 | 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 Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 110 | public void onPackageAdded_bindsToAllTransports() throws Exception { |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 111 | setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 112 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 113 | |
| 114 | TransportManager transportManager = new TransportManager( |
| 115 | RuntimeEnvironment.application.getApplicationContext(), |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 116 | new HashSet<>(Arrays.asList( |
| 117 | mTransport1.componentName, mTransport2.componentName)), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 118 | null /* defaultTransport */, |
| 119 | mTransportBoundListenerStub, |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 120 | ShadowLooper.getMainLooper()); |
| 121 | transportManager.onPackageAdded(PACKAGE_NAME); |
| 122 | |
| 123 | assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn( |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 124 | Arrays.asList(mTransport1.componentName, mTransport2.componentName)); |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 125 | assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn( |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 126 | 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 Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 155 | @Test |
| 156 | public void onPackageAdded_whitelistIsNull_doesNotBindToTransports() throws Exception { |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 157 | setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 158 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 159 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 160 | 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 Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 167 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 168 | assertThat(transportManager.getAllTransportComponents()).isEmpty(); |
| 169 | assertThat(transportManager.getBoundTransportNames()).isEmpty(); |
| 170 | assertThat(mTransportBoundListenerStub.isCalled()).isFalse(); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 173 | @Test |
| 174 | public void onPackageAdded_onlyOneTransportWhitelisted_onlyConnectsToWhitelistedTransport() |
| 175 | throws Exception { |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 176 | setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 177 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 178 | |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 179 | TransportManager transportManager = new TransportManager( |
| 180 | RuntimeEnvironment.application.getApplicationContext(), |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 181 | new HashSet<>(Collections.singleton(mTransport2.componentName)), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 182 | null /* defaultTransport */, |
| 183 | mTransportBoundListenerStub, |
| 184 | ShadowLooper.getMainLooper()); |
| 185 | transportManager.onPackageAdded(PACKAGE_NAME); |
| 186 | |
| 187 | assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn( |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 188 | Collections.singleton(mTransport2.componentName)); |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 189 | assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn( |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 190 | Collections.singleton(mTransport2.name)); |
| 191 | assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport1.stub)).isFalse(); |
| 192 | assertThat(mTransportBoundListenerStub.isCalledForTransport(mTransport2.stub)).isTrue(); |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 193 | } |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 194 | |
| 195 | @Test |
| 196 | public void onPackageAdded_appIsNotPrivileged_doesNotBindToTransports() throws Exception { |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 197 | setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), 0); |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 198 | |
| 199 | TransportManager transportManager = new TransportManager( |
| 200 | RuntimeEnvironment.application.getApplicationContext(), |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 201 | new HashSet<>(Arrays.asList( |
| 202 | mTransport1.componentName, mTransport2.componentName)), |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 203 | 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 Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 213 | @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 Iglikov | 026e933 | 2017-06-01 13:06:29 +0100 | [diff] [blame] | 426 | @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 Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 479 | @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 | |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 535 | private void setUpPackageWithTransports(String packageName, List<TransportInfo> transports, |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 536 | int flags) throws Exception { |
| 537 | PackageInfo packageInfo = new PackageInfo(); |
| 538 | packageInfo.packageName = packageName; |
| 539 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 540 | packageInfo.applicationInfo.privateFlags = flags; |
| 541 | |
| 542 | mPackageManager.addPackage(packageInfo); |
| 543 | |
| 544 | List<ResolveInfo> transportsInfo = new ArrayList<>(); |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 545 | for (TransportInfo transport : transports) { |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 546 | ResolveInfo info = new ResolveInfo(); |
| 547 | info.serviceInfo = new ServiceInfo(); |
| 548 | info.serviceInfo.packageName = packageName; |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 549 | info.serviceInfo.name = transport.name; |
Artem Iglikov | bd476ea | 2017-09-01 10:57:26 +0100 | [diff] [blame] | 550 | transportsInfo.add(info); |
| 551 | } |
| 552 | |
| 553 | Intent intent = new Intent(TransportManager.SERVICE_ACTION_TRANSPORT_HOST); |
| 554 | intent.setPackage(packageName); |
| 555 | |
| 556 | mPackageManager.addResolveInfoForIntent(intent, transportsInfo); |
| 557 | } |
| 558 | |
Artem Iglikov | 19e1227 | 2017-05-31 17:12:28 +0100 | [diff] [blame] | 559 | private TransportManager createTransportManagerAndSetUpTransports( |
| 560 | List<TransportInfo> availableTransports, String defaultTransportName) throws Exception { |
| 561 | return createTransportManagerAndSetUpTransports(availableTransports, |
| 562 | Collections.<TransportInfo>emptyList(), defaultTransportName); |
| 563 | } |
| 564 | |
| 565 | private TransportManager createTransportManagerAndSetUpTransports( |
| 566 | List<TransportInfo> availableTransports, List<TransportInfo> unavailableTransports, |
| 567 | String defaultTransportName) |
| 568 | throws Exception { |
| 569 | List<String> availableTransportsNames = new ArrayList<>(); |
| 570 | List<ComponentName> availableTransportsComponentNames = new ArrayList<>(); |
| 571 | for (TransportInfo transport : availableTransports) { |
| 572 | availableTransportsNames.add(transport.name); |
| 573 | availableTransportsComponentNames.add(transport.componentName); |
| 574 | } |
| 575 | |
| 576 | List<ComponentName> allTransportsComponentNames = new ArrayList<>(); |
| 577 | allTransportsComponentNames.addAll(availableTransportsComponentNames); |
| 578 | for (TransportInfo transport : unavailableTransports) { |
| 579 | allTransportsComponentNames.add(transport.componentName); |
| 580 | } |
| 581 | |
| 582 | for (TransportInfo transport : unavailableTransports) { |
| 583 | ShadowContextImplForBackup.sUnbindableComponents.add(transport.componentName); |
| 584 | } |
| 585 | |
| 586 | setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), |
| 587 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); |
| 588 | |
| 589 | TransportManager transportManager = new TransportManager( |
| 590 | RuntimeEnvironment.application.getApplicationContext(), |
| 591 | new HashSet<>(allTransportsComponentNames), |
| 592 | defaultTransportName, |
| 593 | mTransportBoundListenerStub, |
| 594 | ShadowLooper.getMainLooper()); |
| 595 | transportManager.onPackageAdded(PACKAGE_NAME); |
| 596 | |
| 597 | assertThat(transportManager.getAllTransportComponents()).asList().containsExactlyElementsIn( |
| 598 | availableTransportsComponentNames); |
| 599 | assertThat(transportManager.getBoundTransportNames()).asList().containsExactlyElementsIn( |
| 600 | availableTransportsNames); |
| 601 | for (TransportInfo transport : availableTransports) { |
| 602 | assertThat(mTransportBoundListenerStub.isCalledForTransport(transport.stub)).isTrue(); |
| 603 | } |
| 604 | for (TransportInfo transport : unavailableTransports) { |
| 605 | assertThat(mTransportBoundListenerStub.isCalledForTransport(transport.stub)).isFalse(); |
| 606 | } |
| 607 | |
| 608 | mTransportBoundListenerStub.resetState(); |
| 609 | |
| 610 | return transportManager; |
| 611 | } |
| 612 | |
| 613 | private static class TransportInfo { |
| 614 | public final String packageName; |
| 615 | public final String name; |
| 616 | public final ComponentName componentName; |
| 617 | public final BackupTransportStub stub; |
| 618 | public final IBinder binder; |
| 619 | |
| 620 | TransportInfo(String packageName, String name) { |
| 621 | this.packageName = packageName; |
| 622 | this.name = name; |
| 623 | this.componentName = new ComponentName(packageName, name); |
| 624 | this.stub = new BackupTransportStub(name); |
| 625 | this.binder = mock(IBinder.class); |
| 626 | } |
| 627 | } |
Artem Iglikov | 5ed1dab | 2017-05-25 15:56:05 +0100 | [diff] [blame] | 628 | } |