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