Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +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 | |
| 21 | import android.platform.test.annotations.Presubmit; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 22 | |
| 23 | import androidx.test.filters.SmallTest; |
| 24 | import androidx.test.runner.AndroidJUnit4; |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 25 | |
| 26 | import com.google.android.collect.Sets; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Rule; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.rules.TemporaryFolder; |
| 32 | import org.junit.runner.RunWith; |
| 33 | import org.mockito.MockitoAnnotations; |
| 34 | |
| 35 | import java.io.DataInputStream; |
| 36 | import java.io.DataOutputStream; |
| 37 | import java.io.File; |
| 38 | import java.io.FileInputStream; |
| 39 | import java.io.FileNotFoundException; |
| 40 | import java.io.FileOutputStream; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | @SmallTest |
| 45 | @Presubmit |
| 46 | @RunWith(AndroidJUnit4.class) |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 47 | public class ProcessedPackagesJournalTest { |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 48 | private static final String JOURNAL_FILE_NAME = "processed"; |
| 49 | |
| 50 | private static final String GOOGLE_PHOTOS = "com.google.photos"; |
| 51 | private static final String GMAIL = "com.google.gmail"; |
| 52 | private static final String GOOGLE_PLUS = "com.google.plus"; |
| 53 | |
| 54 | @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); |
| 55 | |
| 56 | private File mStateDirectory; |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 57 | private ProcessedPackagesJournal mProcessedPackagesJournal; |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 58 | |
| 59 | @Before |
| 60 | public void setUp() throws Exception { |
| 61 | MockitoAnnotations.initMocks(this); |
| 62 | mStateDirectory = mTemporaryFolder.newFolder(); |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 63 | mProcessedPackagesJournal = new ProcessedPackagesJournal(mStateDirectory); |
| 64 | mProcessedPackagesJournal.init(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void constructor_loadsAnyPreviousJournalFromDisk() throws Exception { |
| 69 | writePermanentJournalPackages(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL)); |
| 70 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 71 | ProcessedPackagesJournal journalFromDisk = |
| 72 | new ProcessedPackagesJournal(mStateDirectory); |
| 73 | journalFromDisk.init(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 74 | |
| 75 | assertThat(journalFromDisk.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue(); |
| 76 | assertThat(journalFromDisk.hasBeenProcessed(GMAIL)).isTrue(); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void hasBeenProcessed_isFalseForAnyPackageFromBlankInit() { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 81 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse(); |
| 82 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse(); |
| 83 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void addPackage_addsPackageToObjectState() { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 88 | mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 89 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 90 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void addPackage_addsPackageToFileSystem() throws Exception { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 95 | mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 96 | |
| 97 | assertThat(readJournalPackages()).contains(GOOGLE_PHOTOS); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void getPackagesCopy_returnsTheCurrentState() throws Exception { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 102 | mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS); |
| 103 | mProcessedPackagesJournal.addPackage(GMAIL); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 104 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 105 | assertThat(mProcessedPackagesJournal.getPackagesCopy()) |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 106 | .isEqualTo(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL)); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void getPackagesCopy_returnsACopy() throws Exception { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 111 | mProcessedPackagesJournal.getPackagesCopy().add(GMAIL); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 112 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 113 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void reset_removesAllPackagesFromObjectState() { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 118 | mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS); |
| 119 | mProcessedPackagesJournal.addPackage(GOOGLE_PLUS); |
| 120 | mProcessedPackagesJournal.addPackage(GMAIL); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 121 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 122 | mProcessedPackagesJournal.reset(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 123 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 124 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse(); |
| 125 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse(); |
| 126 | assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void reset_removesAllPackagesFromFileSystem() throws Exception { |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 131 | mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS); |
| 132 | mProcessedPackagesJournal.addPackage(GOOGLE_PLUS); |
| 133 | mProcessedPackagesJournal.addPackage(GMAIL); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 134 | |
Robert Berry | a1109ef | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 135 | mProcessedPackagesJournal.reset(); |
Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame] | 136 | |
| 137 | assertThat(readJournalPackages()).isEmpty(); |
| 138 | } |
| 139 | |
| 140 | private HashSet<String> readJournalPackages() throws Exception { |
| 141 | File journal = new File(mStateDirectory, JOURNAL_FILE_NAME); |
| 142 | HashSet<String> packages = new HashSet<>(); |
| 143 | |
| 144 | try (FileInputStream fis = new FileInputStream(journal); |
| 145 | DataInputStream dis = new DataInputStream(fis)) { |
| 146 | while (dis.available() > 0) { |
| 147 | packages.add(dis.readUTF()); |
| 148 | } |
| 149 | } catch (FileNotFoundException e) { |
| 150 | return new HashSet<>(); |
| 151 | } |
| 152 | |
| 153 | return packages; |
| 154 | } |
| 155 | |
| 156 | private void writePermanentJournalPackages(Set<String> packages) throws Exception { |
| 157 | File journal = new File(mStateDirectory, JOURNAL_FILE_NAME); |
| 158 | |
| 159 | try (FileOutputStream fos = new FileOutputStream(journal); |
| 160 | DataOutputStream dos = new DataOutputStream(fos)) { |
| 161 | for (String packageName : packages) { |
| 162 | dos.writeUTF(packageName); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |