Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.rollback; |
| 18 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.junit.Assert.assertTrue; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.eq; |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 24 | import static org.mockito.Matchers.anyInt; |
| 25 | import static org.mockito.Matchers.anyString; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 26 | import static org.mockito.Mockito.doReturn; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.spy; |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 29 | import static org.mockito.Mockito.when; |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 30 | import static org.mockito.MockitoAnnotations.initMocks; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 31 | |
| 32 | import android.content.pm.VersionedPackage; |
| 33 | import android.content.rollback.PackageRollbackInfo; |
| 34 | import android.content.rollback.PackageRollbackInfo.RestoreInfo; |
| 35 | import android.util.IntArray; |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 36 | import android.util.SparseLongArray; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 37 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 38 | import com.android.server.pm.ApexManager; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 39 | import com.android.server.pm.Installer; |
| 40 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 41 | import org.junit.Before; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.junit.runners.JUnit4; |
| 45 | import org.mockito.InOrder; |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 46 | import org.mockito.Mock; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 47 | import org.mockito.Mockito; |
| 48 | |
| 49 | import java.io.File; |
| 50 | import java.util.ArrayList; |
| 51 | |
| 52 | @RunWith(JUnit4.class) |
| 53 | public class AppDataRollbackHelperTest { |
| 54 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 55 | @Mock private ApexManager mApexManager; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | initMocks(this); |
| 60 | } |
| 61 | |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 62 | @Test |
| 63 | public void testSnapshotAppData() throws Exception { |
| 64 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 65 | AppDataRollbackHelper helper = spy(new AppDataRollbackHelper(installer, mApexManager)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 66 | |
| 67 | // All users are unlocked so we should snapshot data for them. |
| 68 | doReturn(true).when(helper).isUserCredentialLocked(eq(10)); |
| 69 | doReturn(true).when(helper).isUserCredentialLocked(eq(11)); |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 70 | PackageRollbackInfo info = createPackageRollbackInfo("com.foo.bar"); |
| 71 | helper.snapshotAppData(5, info, new int[]{10, 11}); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 72 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 73 | assertEquals(2, info.getPendingBackups().size()); |
| 74 | assertEquals(10, info.getPendingBackups().get(0)); |
| 75 | assertEquals(11, info.getPendingBackups().get(1)); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 76 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 77 | assertEquals(0, info.getCeSnapshotInodes().size()); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 78 | |
| 79 | InOrder inOrder = Mockito.inOrder(installer); |
| 80 | inOrder.verify(installer).snapshotAppData( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 81 | eq("com.foo.bar"), eq(10), eq(5), eq(Installer.FLAG_STORAGE_DE)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 82 | inOrder.verify(installer).snapshotAppData( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 83 | eq("com.foo.bar"), eq(11), eq(5), eq(Installer.FLAG_STORAGE_DE)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 84 | inOrder.verifyNoMoreInteractions(); |
| 85 | |
| 86 | // One of the users is unlocked but the other isn't |
| 87 | doReturn(false).when(helper).isUserCredentialLocked(eq(10)); |
| 88 | doReturn(true).when(helper).isUserCredentialLocked(eq(11)); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 89 | when(installer.snapshotAppData(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(239L); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 90 | |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 91 | PackageRollbackInfo info2 = createPackageRollbackInfo("com.foo.bar"); |
| 92 | helper.snapshotAppData(7, info2, new int[]{10, 11}); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 93 | assertEquals(1, info2.getPendingBackups().size()); |
| 94 | assertEquals(11, info2.getPendingBackups().get(0)); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 95 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 96 | assertEquals(1, info2.getCeSnapshotInodes().size()); |
| 97 | assertEquals(239L, info2.getCeSnapshotInodes().get(10)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 98 | |
| 99 | inOrder = Mockito.inOrder(installer); |
| 100 | inOrder.verify(installer).snapshotAppData( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 101 | eq("com.foo.bar"), eq(10), eq(7), |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 102 | eq(Installer.FLAG_STORAGE_CE | Installer.FLAG_STORAGE_DE)); |
| 103 | inOrder.verify(installer).snapshotAppData( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 104 | eq("com.foo.bar"), eq(11), eq(7), eq(Installer.FLAG_STORAGE_DE)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 105 | inOrder.verifyNoMoreInteractions(); |
| 106 | } |
| 107 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 108 | private static PackageRollbackInfo createPackageRollbackInfo(String packageName, |
| 109 | final int[] installedUsers) { |
| 110 | return new PackageRollbackInfo( |
| 111 | new VersionedPackage(packageName, 2), new VersionedPackage(packageName, 1), |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 112 | new IntArray(), new ArrayList<>(), false, false, IntArray.wrap(installedUsers), |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 113 | new SparseLongArray()); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 116 | private static PackageRollbackInfo createPackageRollbackInfo(String packageName) { |
| 117 | return createPackageRollbackInfo(packageName, new int[] {}); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 120 | private static Rollback createRollbackForId(int rollbackId) { |
| 121 | return new Rollback(rollbackId, new File("/does/not/exist"), -1, |
| 122 | 0, "com.xyz"); |
| 123 | } |
| 124 | |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 125 | @Test |
| 126 | public void testRestoreAppDataSnapshot_pendingBackupForUser() throws Exception { |
| 127 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 128 | AppDataRollbackHelper helper = spy(new AppDataRollbackHelper(installer, mApexManager)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 129 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 130 | PackageRollbackInfo info = createPackageRollbackInfo("com.foo"); |
| 131 | IntArray pendingBackups = info.getPendingBackups(); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 132 | pendingBackups.add(10); |
| 133 | pendingBackups.add(11); |
| 134 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 135 | assertTrue(helper.restoreAppData(13 /* rollbackId */, info, 10 /* userId */, 1 /* appId */, |
| 136 | "seinfo")); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 137 | |
| 138 | // Should only require FLAG_STORAGE_DE here because we have a pending backup that we |
| 139 | // didn't manage to execute. |
| 140 | InOrder inOrder = Mockito.inOrder(installer); |
| 141 | inOrder.verify(installer).restoreAppDataSnapshot( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 142 | eq("com.foo"), eq(1) /* appId */, eq("seinfo"), eq(10) /* userId */, |
| 143 | eq(13) /* rollbackId */, eq(Installer.FLAG_STORAGE_DE)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 144 | inOrder.verifyNoMoreInteractions(); |
| 145 | |
| 146 | assertEquals(1, pendingBackups.size()); |
| 147 | assertEquals(11, pendingBackups.get(0)); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testRestoreAppDataSnapshot_availableBackupForLockedUser() throws Exception { |
| 152 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 153 | AppDataRollbackHelper helper = spy(new AppDataRollbackHelper(installer, mApexManager)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 154 | doReturn(true).when(helper).isUserCredentialLocked(eq(10)); |
| 155 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 156 | PackageRollbackInfo info = createPackageRollbackInfo("com.foo"); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 157 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 158 | assertTrue(helper.restoreAppData(73 /* rollbackId */, info, 10 /* userId */, 1 /* appId */, |
| 159 | "seinfo")); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 160 | |
| 161 | InOrder inOrder = Mockito.inOrder(installer); |
| 162 | inOrder.verify(installer).restoreAppDataSnapshot( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 163 | eq("com.foo"), eq(1) /* appId */, eq("seinfo"), eq(10) /* userId */, |
| 164 | eq(73) /* rollbackId */, eq(Installer.FLAG_STORAGE_DE)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 165 | inOrder.verifyNoMoreInteractions(); |
| 166 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 167 | ArrayList<RestoreInfo> pendingRestores = info.getPendingRestores(); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 168 | assertEquals(1, pendingRestores.size()); |
| 169 | assertEquals(10, pendingRestores.get(0).userId); |
| 170 | assertEquals(1, pendingRestores.get(0).appId); |
| 171 | assertEquals("seinfo", pendingRestores.get(0).seInfo); |
| 172 | } |
| 173 | |
| 174 | @Test |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 175 | public void testRestoreAppDataSnapshot_availableBackupForUnlockedUser() throws Exception { |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 176 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 177 | AppDataRollbackHelper helper = spy(new AppDataRollbackHelper(installer, mApexManager)); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 178 | doReturn(false).when(helper).isUserCredentialLocked(eq(10)); |
| 179 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 180 | PackageRollbackInfo info = createPackageRollbackInfo("com.foo"); |
| 181 | assertFalse(helper.restoreAppData(101 /* rollbackId */, info, 10 /* userId */, |
| 182 | 1 /* appId */, "seinfo")); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 183 | |
| 184 | InOrder inOrder = Mockito.inOrder(installer); |
| 185 | inOrder.verify(installer).restoreAppDataSnapshot( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 186 | eq("com.foo"), eq(1) /* appId */, eq("seinfo"), eq(10) /* userId */, |
| 187 | eq(101) /* rollbackId */, |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 188 | eq(Installer.FLAG_STORAGE_DE | Installer.FLAG_STORAGE_CE)); |
| 189 | inOrder.verifyNoMoreInteractions(); |
| 190 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 191 | ArrayList<RestoreInfo> pendingRestores = info.getPendingRestores(); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 192 | assertEquals(0, pendingRestores.size()); |
| 193 | } |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 194 | |
| 195 | @Test |
| 196 | public void destroyAppData() throws Exception { |
| 197 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 198 | AppDataRollbackHelper helper = new AppDataRollbackHelper(installer, mApexManager); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 199 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 200 | PackageRollbackInfo info = createPackageRollbackInfo("com.foo.bar"); |
| 201 | info.putCeSnapshotInode(11, 239L); |
| 202 | helper.destroyAppDataSnapshot(5 /* rollbackId */, info, 10 /* userId */); |
| 203 | helper.destroyAppDataSnapshot(5 /* rollbackId */, info, 11 /* userId */); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 204 | |
| 205 | InOrder inOrder = Mockito.inOrder(installer); |
| 206 | inOrder.verify(installer).destroyAppDataSnapshot( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 207 | eq("com.foo.bar"), eq(10) /* userId */, eq(0L) /* ceSnapshotInode */, |
| 208 | eq(5) /* rollbackId */, eq(Installer.FLAG_STORAGE_DE)); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 209 | inOrder.verify(installer).destroyAppDataSnapshot( |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 210 | eq("com.foo.bar"), eq(11) /* userId */, eq(239L) /* ceSnapshotInode */, |
| 211 | eq(5) /* rollbackId */, eq(Installer.FLAG_STORAGE_DE | Installer.FLAG_STORAGE_CE)); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 212 | inOrder.verifyNoMoreInteractions(); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 213 | |
| 214 | assertEquals(0, info.getCeSnapshotInodes().size()); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 218 | public void commitPendingBackupAndRestoreForUser() throws Exception { |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 219 | Installer installer = mock(Installer.class); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 220 | AppDataRollbackHelper helper = new AppDataRollbackHelper(installer, mApexManager); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 221 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 222 | when(installer.snapshotAppData(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(53L); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 223 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 224 | // This one should be backed up. |
| 225 | PackageRollbackInfo pendingBackup = createPackageRollbackInfo("com.foo", new int[]{37, 73}); |
| 226 | pendingBackup.addPendingBackup(37); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 227 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 228 | // Nothing should be done for this one. |
| 229 | PackageRollbackInfo wasRecentlyRestored = createPackageRollbackInfo("com.bar", |
| 230 | new int[]{37, 73}); |
| 231 | wasRecentlyRestored.addPendingBackup(37); |
| 232 | wasRecentlyRestored.getPendingRestores().add( |
| 233 | new RestoreInfo(37 /* userId */, 239 /* appId*/, "seInfo")); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 234 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 235 | // This one should be restored |
| 236 | PackageRollbackInfo pendingRestore = createPackageRollbackInfo("com.abc", |
| 237 | new int[]{37, 73}); |
| 238 | pendingRestore.putCeSnapshotInode(37, 1543L); |
| 239 | pendingRestore.getPendingRestores().add( |
| 240 | new RestoreInfo(37 /* userId */, 57 /* appId*/, "seInfo")); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 241 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 242 | // This one shouldn't be processed, because it hasn't pending backups/restores for userId |
| 243 | // 37. |
| 244 | PackageRollbackInfo ignoredInfo = createPackageRollbackInfo("com.bar", |
| 245 | new int[]{3, 73}); |
| 246 | wasRecentlyRestored.addPendingBackup(3); |
| 247 | wasRecentlyRestored.addPendingBackup(73); |
| 248 | wasRecentlyRestored.getPendingRestores().add( |
| 249 | new RestoreInfo(73 /* userId */, 239 /* appId*/, "seInfo")); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 250 | |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 251 | Rollback dataWithPendingBackup = createRollbackForId(101); |
Richard Uhler | cca637a | 2019-02-27 11:50:48 +0000 | [diff] [blame] | 252 | dataWithPendingBackup.info.getPackages().add(pendingBackup); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 253 | |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 254 | Rollback dataWithRecentRestore = createRollbackForId(17239); |
Richard Uhler | cca637a | 2019-02-27 11:50:48 +0000 | [diff] [blame] | 255 | dataWithRecentRestore.info.getPackages().add(wasRecentlyRestored); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 256 | |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 257 | Rollback dataForDifferentUser = createRollbackForId(17239); |
Richard Uhler | cca637a | 2019-02-27 11:50:48 +0000 | [diff] [blame] | 258 | dataForDifferentUser.info.getPackages().add(ignoredInfo); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 259 | |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 260 | Rollback dataForRestore = createRollbackForId(17239); |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 261 | dataForRestore.info.getPackages().add(pendingRestore); |
| 262 | dataForRestore.info.getPackages().add(wasRecentlyRestored); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 263 | |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 264 | InOrder inOrder = Mockito.inOrder(installer); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 265 | |
| 266 | // Check that pending backup and restore for the same package mutually destroyed each other. |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 267 | assertTrue(helper.commitPendingBackupAndRestoreForUser(37, dataWithRecentRestore)); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 268 | assertEquals(-1, wasRecentlyRestored.getPendingBackups().indexOf(37)); |
| 269 | assertNull(wasRecentlyRestored.getRestoreInfo(37)); |
| 270 | |
| 271 | // Check that backup was performed. |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 272 | assertTrue(helper.commitPendingBackupAndRestoreForUser(37, dataWithPendingBackup)); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 273 | inOrder.verify(installer).snapshotAppData(eq("com.foo"), eq(37), eq(101), |
| 274 | eq(Installer.FLAG_STORAGE_CE)); |
| 275 | assertEquals(-1, pendingBackup.getPendingBackups().indexOf(37)); |
| 276 | assertEquals(53, pendingBackup.getCeSnapshotInodes().get(37)); |
| 277 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 278 | // Check that restore was performed. |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 279 | assertTrue(helper.commitPendingBackupAndRestoreForUser(37, dataForRestore)); |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 280 | inOrder.verify(installer).restoreAppDataSnapshot( |
| 281 | eq("com.abc"), eq(57) /* appId */, eq("seInfo"), eq(37) /* userId */, |
| 282 | eq(17239) /* rollbackId */, eq(Installer.FLAG_STORAGE_CE)); |
| 283 | assertNull(pendingRestore.getRestoreInfo(37)); |
| 284 | |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 285 | inOrder.verifyNoMoreInteractions(); |
| 286 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 287 | } |