Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +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.utils; |
| 18 | |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 19 | import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_ID; |
| 20 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_APK_NOT_INSTALLED; |
| 21 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK; |
| 22 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE; |
| 23 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH; |
| 24 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_RESTORE_ANY_VERSION; |
| 25 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_SYSTEM_APP_NO_AGENT; |
| 26 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSIONS_MATCH; |
| 27 | import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER; |
| 28 | |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 29 | import static com.google.common.truth.Truth.assertThat; |
| 30 | |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 33 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 34 | import static org.mockito.internal.verification.VerificationModeFactory.times; |
| 35 | |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 36 | import android.app.backup.IBackupManagerMonitor; |
| 37 | import android.content.Context; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 38 | import android.content.pm.ApplicationInfo; |
| 39 | import android.content.pm.PackageInfo; |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 40 | import android.content.pm.Signature; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 41 | import android.os.Bundle; |
| 42 | import android.os.Process; |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 43 | import android.platform.test.annotations.Presubmit; |
| 44 | import android.support.test.InstrumentationRegistry; |
| 45 | import android.support.test.filters.SmallTest; |
| 46 | import android.support.test.runner.AndroidJUnit4; |
| 47 | |
| 48 | import com.android.frameworks.servicestests.R; |
| 49 | import com.android.server.backup.FileMetadata; |
| 50 | import com.android.server.backup.restore.PerformAdbRestoreTask; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 51 | import com.android.server.backup.restore.RestorePolicy; |
| 52 | import com.android.server.backup.testutils.PackageManagerStub; |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 53 | |
| 54 | import com.google.common.hash.Hashing; |
| 55 | |
| 56 | import org.junit.Before; |
| 57 | import org.junit.Test; |
| 58 | import org.junit.runner.RunWith; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 59 | import org.mockito.ArgumentCaptor; |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 60 | import org.mockito.Mock; |
| 61 | import org.mockito.MockitoAnnotations; |
| 62 | |
| 63 | import java.io.InputStream; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 64 | import java.util.List; |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 65 | |
| 66 | @SmallTest |
| 67 | @Presubmit |
| 68 | @RunWith(AndroidJUnit4.class) |
| 69 | public class TarBackupReaderTest { |
| 70 | private static final String TELEPHONY_PACKAGE_NAME = "com.android.providers.telephony"; |
| 71 | private static final String TELEPHONY_PACKAGE_SIGNATURE_SHA256 = |
| 72 | "301aa3cb081134501c45f1422abc66c24224fd5ded5fdc8f17e697176fd866aa"; |
| 73 | private static final int TELEPHONY_PACKAGE_VERSION = 25; |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 74 | private static final Signature FAKE_SIGNATURE_1 = new Signature("1234"); |
| 75 | private static final Signature FAKE_SIGNATURE_2 = new Signature("5678"); |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 76 | |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 77 | @Mock private BytesReadListener mBytesReadListenerMock; |
| 78 | @Mock private IBackupManagerMonitor mBackupManagerMonitorMock; |
| 79 | |
| 80 | private final PackageManagerStub mPackageManagerStub = new PackageManagerStub(); |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 81 | private Context mContext; |
| 82 | |
| 83 | @Before |
| 84 | public void setUp() throws Exception { |
| 85 | MockitoAnnotations.initMocks(this); |
| 86 | |
| 87 | mContext = InstrumentationRegistry.getContext(); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void readTarHeaders_backupEncrypted_correctlyParsesFileMetadata() throws Exception { |
| 92 | InputStream inputStream = mContext.getResources().openRawResource( |
| 93 | R.raw.backup_telephony_with_password); |
| 94 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 95 | inputStream, "123"); |
| 96 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 97 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 98 | FileMetadata fileMetadata = tarBackupReader.readTarHeaders(); |
| 99 | |
| 100 | assertThat(fileMetadata.packageName).isEqualTo(TELEPHONY_PACKAGE_NAME); |
| 101 | assertThat(fileMetadata.mode).isEqualTo(0600); |
| 102 | assertThat(fileMetadata.size).isEqualTo(2438); |
| 103 | assertThat(fileMetadata.domain).isEqualTo(null); |
| 104 | assertThat(fileMetadata.path).isEqualTo("_manifest"); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void readTarHeaders_backupNotEncrypted_correctlyParsesFileMetadata() throws Exception { |
| 109 | InputStream inputStream = mContext.getResources().openRawResource( |
| 110 | R.raw.backup_telephony_no_password); |
| 111 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 112 | inputStream, null); |
| 113 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 114 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 115 | FileMetadata fileMetadata = tarBackupReader.readTarHeaders(); |
| 116 | |
| 117 | assertThat(fileMetadata.packageName).isEqualTo(TELEPHONY_PACKAGE_NAME); |
| 118 | assertThat(fileMetadata.mode).isEqualTo(0600); |
| 119 | assertThat(fileMetadata.size).isEqualTo(2438); |
| 120 | assertThat(fileMetadata.domain).isEqualTo(null); |
| 121 | assertThat(fileMetadata.path).isEqualTo("_manifest"); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void readAppManifest_backupEncrypted_correctlyParsesAppManifest() throws Exception { |
| 126 | InputStream inputStream = mContext.getResources().openRawResource( |
| 127 | R.raw.backup_telephony_with_password); |
| 128 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 129 | inputStream, "123"); |
| 130 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 131 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 132 | FileMetadata fileMetadata = tarBackupReader.readTarHeaders(); |
| 133 | |
| 134 | Signature[] signatures = tarBackupReader.readAppManifestAndReturnSignatures(fileMetadata); |
| 135 | |
| 136 | assertThat(fileMetadata.version).isEqualTo(TELEPHONY_PACKAGE_VERSION); |
| 137 | assertThat(fileMetadata.hasApk).isFalse(); |
| 138 | assertThat(signatures).isNotNull(); |
| 139 | assertThat(signatures).hasLength(1); |
| 140 | |
| 141 | String signatureSha256 = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString(); |
| 142 | assertThat(signatureSha256).isEqualTo(TELEPHONY_PACKAGE_SIGNATURE_SHA256); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void readAppManifest_backupNotEncrypted_correctlyParsesAppManifest() throws Exception { |
| 147 | InputStream inputStream = mContext.getResources().openRawResource( |
| 148 | R.raw.backup_telephony_no_password); |
| 149 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 150 | inputStream, null); |
| 151 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 152 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 153 | FileMetadata fileMetadata = tarBackupReader.readTarHeaders(); |
| 154 | |
| 155 | Signature[] signatures = tarBackupReader.readAppManifestAndReturnSignatures(fileMetadata); |
| 156 | |
| 157 | assertThat(fileMetadata.version).isEqualTo(TELEPHONY_PACKAGE_VERSION); |
| 158 | assertThat(fileMetadata.hasApk).isFalse(); |
| 159 | assertThat(signatures).isNotNull(); |
| 160 | assertThat(signatures).hasLength(1); |
| 161 | |
| 162 | String signatureSha256 = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString(); |
| 163 | assertThat(signatureSha256).isEqualTo(TELEPHONY_PACKAGE_SIGNATURE_SHA256); |
| 164 | } |
Artem Iglikov | a08220b | 2017-05-09 14:28:29 +0100 | [diff] [blame^] | 165 | |
| 166 | @Test |
| 167 | public void chooseRestorePolicy_signaturesIsNull_returnsIgnore() throws Exception { |
| 168 | InputStream inputStream = mContext.getResources().openRawResource( |
| 169 | R.raw.backup_telephony_no_password); |
| 170 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 171 | inputStream, null); |
| 172 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 173 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 174 | |
| 175 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 176 | true /* allowApks */, new FileMetadata(), null /* signatures */); |
| 177 | |
| 178 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 179 | verifyZeroInteractions(mBackupManagerMonitorMock); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void chooseRestorePolicy_packageDoesNotExistAndAllowApksAndHasApk_returnsAcceptIfApk() |
| 184 | throws Exception { |
| 185 | InputStream inputStream = mContext.getResources().openRawResource( |
| 186 | R.raw.backup_telephony_no_password); |
| 187 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 188 | inputStream, null); |
| 189 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 190 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 191 | FileMetadata info = new FileMetadata(); |
| 192 | info.hasApk = true; |
| 193 | PackageManagerStub.sPackageInfo = null; |
| 194 | |
| 195 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 196 | true /* allowApks */, info, new Signature[0] /* signatures */); |
| 197 | |
| 198 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT_IF_APK); |
| 199 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 200 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 201 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 202 | LOG_EVENT_ID_APK_NOT_INSTALLED); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void |
| 207 | chooseRestorePolicy_packageDoesNotExistAndAllowApksAndDoesNotHaveApk_returnsAcceptIfApkLogsCannotRestore() |
| 208 | throws Exception { |
| 209 | InputStream inputStream = mContext.getResources().openRawResource( |
| 210 | R.raw.backup_telephony_no_password); |
| 211 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 212 | inputStream, null); |
| 213 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 214 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 215 | FileMetadata info = new FileMetadata(); |
| 216 | info.hasApk = false; |
| 217 | PackageManagerStub.sPackageInfo = null; |
| 218 | |
| 219 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 220 | true /* allowApks */, info, new Signature[0] /* signatures */); |
| 221 | |
| 222 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT_IF_APK); |
| 223 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 224 | verify(mBackupManagerMonitorMock, times(2)).onEvent(bundleCaptor.capture()); |
| 225 | List<Bundle> eventBundles = bundleCaptor.getAllValues(); |
| 226 | assertThat(eventBundles).hasSize(2); |
| 227 | assertThat(eventBundles.get(0).get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 228 | LOG_EVENT_ID_APK_NOT_INSTALLED); |
| 229 | assertThat(eventBundles.get(1).get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 230 | LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK); |
| 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void chooseRestorePolicy_packageDoesNotExistAndDoesNotAllowApks_returnsIgnore() |
| 235 | throws Exception { |
| 236 | InputStream inputStream = mContext.getResources().openRawResource( |
| 237 | R.raw.backup_telephony_no_password); |
| 238 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 239 | inputStream, null); |
| 240 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 241 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 242 | PackageManagerStub.sPackageInfo = null; |
| 243 | |
| 244 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 245 | false /* allowApks */, new FileMetadata(), new Signature[0] /* signatures */); |
| 246 | |
| 247 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 248 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 249 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 250 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 251 | LOG_EVENT_ID_APK_NOT_INSTALLED); |
| 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void chooseRestorePolicy_doesNotAllowsBackup_returnsIgnore() throws Exception { |
| 256 | InputStream inputStream = mContext.getResources().openRawResource( |
| 257 | R.raw.backup_telephony_no_password); |
| 258 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 259 | inputStream, null); |
| 260 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 261 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 262 | |
| 263 | PackageInfo packageInfo = new PackageInfo(); |
| 264 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 265 | packageInfo.applicationInfo.flags = ~ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 266 | PackageManagerStub.sPackageInfo = packageInfo; |
| 267 | |
| 268 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 269 | false /* allowApks */, new FileMetadata(), new Signature[0] /* signatures */); |
| 270 | |
| 271 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 272 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 273 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 274 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 275 | LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE); |
| 276 | } |
| 277 | |
| 278 | @Test |
| 279 | public void chooseRestorePolicy_systemAppWithNoAgent_returnsIgnore() throws Exception { |
| 280 | InputStream inputStream = mContext.getResources().openRawResource( |
| 281 | R.raw.backup_telephony_no_password); |
| 282 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 283 | inputStream, null); |
| 284 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 285 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 286 | |
| 287 | PackageInfo packageInfo = new PackageInfo(); |
| 288 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 289 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 290 | packageInfo.applicationInfo.uid = Process.SYSTEM_UID; |
| 291 | packageInfo.applicationInfo.backupAgentName = null; |
| 292 | PackageManagerStub.sPackageInfo = packageInfo; |
| 293 | |
| 294 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 295 | false /* allowApks */, new FileMetadata(), new Signature[0] /* signatures */); |
| 296 | |
| 297 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 298 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 299 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 300 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 301 | LOG_EVENT_ID_SYSTEM_APP_NO_AGENT); |
| 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void chooseRestorePolicy_nonSystemAppSignaturesDoNotMatch_returnsIgnore() |
| 306 | throws Exception { |
| 307 | InputStream inputStream = mContext.getResources().openRawResource( |
| 308 | R.raw.backup_telephony_no_password); |
| 309 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 310 | inputStream, null); |
| 311 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 312 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 313 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 314 | |
| 315 | PackageInfo packageInfo = new PackageInfo(); |
| 316 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 317 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 318 | packageInfo.applicationInfo.uid = Process.FIRST_APPLICATION_UID; |
| 319 | packageInfo.applicationInfo.backupAgentName = null; |
| 320 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_2}; |
| 321 | PackageManagerStub.sPackageInfo = packageInfo; |
| 322 | |
| 323 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 324 | false /* allowApks */, new FileMetadata(), signatures); |
| 325 | |
| 326 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 327 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 328 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 329 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 330 | LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH); |
| 331 | } |
| 332 | |
| 333 | @Test |
| 334 | public void chooseRestorePolicy_systemAppWithBackupAgentAndRestoreAnyVersion_returnsAccept() throws Exception { |
| 335 | InputStream inputStream = mContext.getResources().openRawResource( |
| 336 | R.raw.backup_telephony_no_password); |
| 337 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 338 | inputStream, null); |
| 339 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 340 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 341 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 342 | |
| 343 | PackageInfo packageInfo = new PackageInfo(); |
| 344 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 345 | packageInfo.applicationInfo.flags |= |
| 346 | ApplicationInfo.FLAG_ALLOW_BACKUP | ApplicationInfo.FLAG_RESTORE_ANY_VERSION; |
| 347 | packageInfo.applicationInfo.uid = Process.SYSTEM_UID; |
| 348 | packageInfo.applicationInfo.backupAgentName = "backup.agent"; |
| 349 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 350 | PackageManagerStub.sPackageInfo = packageInfo; |
| 351 | |
| 352 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 353 | false /* allowApks */, new FileMetadata(), signatures); |
| 354 | |
| 355 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT); |
| 356 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 357 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 358 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 359 | LOG_EVENT_ID_RESTORE_ANY_VERSION); |
| 360 | } |
| 361 | |
| 362 | @Test |
| 363 | public void chooseRestorePolicy_restoreAnyVersion_returnsAccept() throws Exception { |
| 364 | InputStream inputStream = mContext.getResources().openRawResource( |
| 365 | R.raw.backup_telephony_no_password); |
| 366 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 367 | inputStream, null); |
| 368 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 369 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 370 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 371 | |
| 372 | PackageInfo packageInfo = new PackageInfo(); |
| 373 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 374 | packageInfo.applicationInfo.flags |= |
| 375 | ApplicationInfo.FLAG_ALLOW_BACKUP | ApplicationInfo.FLAG_RESTORE_ANY_VERSION; |
| 376 | packageInfo.applicationInfo.uid = Process.FIRST_APPLICATION_UID; |
| 377 | packageInfo.applicationInfo.backupAgentName = null; |
| 378 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 379 | PackageManagerStub.sPackageInfo = packageInfo; |
| 380 | |
| 381 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 382 | false /* allowApks */, new FileMetadata(), signatures); |
| 383 | |
| 384 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT); |
| 385 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 386 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 387 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 388 | LOG_EVENT_ID_RESTORE_ANY_VERSION); |
| 389 | } |
| 390 | |
| 391 | @Test |
| 392 | public void chooseRestorePolicy_notRestoreAnyVersionButVersionMatch_returnsAccept() |
| 393 | throws Exception { |
| 394 | InputStream inputStream = mContext.getResources().openRawResource( |
| 395 | R.raw.backup_telephony_no_password); |
| 396 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 397 | inputStream, null); |
| 398 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 399 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 400 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 401 | FileMetadata info = new FileMetadata(); |
| 402 | info.version = 1; |
| 403 | |
| 404 | PackageInfo packageInfo = new PackageInfo(); |
| 405 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 406 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 407 | packageInfo.applicationInfo.flags &= ~ApplicationInfo.FLAG_RESTORE_ANY_VERSION; |
| 408 | packageInfo.applicationInfo.uid = Process.FIRST_APPLICATION_UID; |
| 409 | packageInfo.applicationInfo.backupAgentName = null; |
| 410 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 411 | packageInfo.versionCode = 2; |
| 412 | PackageManagerStub.sPackageInfo = packageInfo; |
| 413 | |
| 414 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 415 | false /* allowApks */, info, signatures); |
| 416 | |
| 417 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT); |
| 418 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 419 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 420 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 421 | LOG_EVENT_ID_VERSIONS_MATCH); |
| 422 | } |
| 423 | |
| 424 | @Test |
| 425 | public void |
| 426 | chooseRestorePolicy_notRestoreAnyVersionAndVersionMismatchButAllowApksAndHasApk_returnsAcceptIfApk() |
| 427 | throws Exception { |
| 428 | InputStream inputStream = mContext.getResources().openRawResource( |
| 429 | R.raw.backup_telephony_no_password); |
| 430 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 431 | inputStream, null); |
| 432 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 433 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 434 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 435 | FileMetadata info = new FileMetadata(); |
| 436 | info.version = 2; |
| 437 | info.hasApk = true; |
| 438 | |
| 439 | PackageInfo packageInfo = new PackageInfo(); |
| 440 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 441 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 442 | packageInfo.applicationInfo.flags &= ~ApplicationInfo.FLAG_RESTORE_ANY_VERSION; |
| 443 | packageInfo.applicationInfo.uid = Process.FIRST_APPLICATION_UID; |
| 444 | packageInfo.applicationInfo.backupAgentName = null; |
| 445 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 446 | packageInfo.versionCode = 1; |
| 447 | PackageManagerStub.sPackageInfo = packageInfo; |
| 448 | |
| 449 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 450 | true /* allowApks */, info, signatures); |
| 451 | |
| 452 | assertThat(policy).isEqualTo(RestorePolicy.ACCEPT_IF_APK); |
| 453 | verifyNoMoreInteractions(mBackupManagerMonitorMock); |
| 454 | } |
| 455 | |
| 456 | @Test |
| 457 | public void |
| 458 | chooseRestorePolicy_notRestoreAnyVersionAndVersionMismatchAndDoesNotAllowApks_returnsIgnore() |
| 459 | throws Exception { |
| 460 | InputStream inputStream = mContext.getResources().openRawResource( |
| 461 | R.raw.backup_telephony_no_password); |
| 462 | InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream( |
| 463 | inputStream, null); |
| 464 | TarBackupReader tarBackupReader = new TarBackupReader(tarInputStream, |
| 465 | mBytesReadListenerMock, mBackupManagerMonitorMock); |
| 466 | Signature[] signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 467 | FileMetadata info = new FileMetadata(); |
| 468 | info.version = 2; |
| 469 | |
| 470 | PackageInfo packageInfo = new PackageInfo(); |
| 471 | packageInfo.applicationInfo = new ApplicationInfo(); |
| 472 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 473 | packageInfo.applicationInfo.flags &= ~ApplicationInfo.FLAG_RESTORE_ANY_VERSION; |
| 474 | packageInfo.applicationInfo.uid = Process.FIRST_APPLICATION_UID; |
| 475 | packageInfo.applicationInfo.backupAgentName = null; |
| 476 | packageInfo.signatures = new Signature[]{FAKE_SIGNATURE_1}; |
| 477 | packageInfo.versionCode = 1; |
| 478 | PackageManagerStub.sPackageInfo = packageInfo; |
| 479 | |
| 480 | RestorePolicy policy = tarBackupReader.chooseRestorePolicy(mPackageManagerStub, |
| 481 | false /* allowApks */, info, signatures); |
| 482 | |
| 483 | assertThat(policy).isEqualTo(RestorePolicy.IGNORE); |
| 484 | ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 485 | verify(mBackupManagerMonitorMock).onEvent(bundleCaptor.capture()); |
| 486 | assertThat(bundleCaptor.getValue().get(EXTRA_LOG_EVENT_ID)).isEqualTo( |
| 487 | LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER); |
| 488 | } |
| 489 | |
Artem Iglikov | 13fffd2 | 2017-05-08 17:17:13 +0100 | [diff] [blame] | 490 | } |