blob: cf4f9753ad9fbd8f6d8d687a0bca0a68ab5168b4 [file] [log] [blame]
Robert Berry6d2fbdf2017-07-17 09:56:44 +01001/*
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
17package com.android.server.backup;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.platform.test.annotations.Presubmit;
Brett Chabota26eda92018-07-23 13:08:30 -070022
23import androidx.test.filters.SmallTest;
24import androidx.test.runner.AndroidJUnit4;
Robert Berry6d2fbdf2017-07-17 09:56:44 +010025
26import com.google.android.collect.Sets;
27
28import org.junit.Before;
29import org.junit.Rule;
30import org.junit.Test;
31import org.junit.rules.TemporaryFolder;
32import org.junit.runner.RunWith;
33import org.mockito.MockitoAnnotations;
34
35import java.io.DataInputStream;
36import java.io.DataOutputStream;
37import java.io.File;
38import java.io.FileInputStream;
39import java.io.FileNotFoundException;
40import java.io.FileOutputStream;
41import java.util.HashSet;
42import java.util.Set;
43
44@SmallTest
45@Presubmit
46@RunWith(AndroidJUnit4.class)
Robert Berrya1109ef2017-07-17 09:56:44 +010047public class ProcessedPackagesJournalTest {
Robert Berry6d2fbdf2017-07-17 09:56:44 +010048 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 Berrya1109ef2017-07-17 09:56:44 +010057 private ProcessedPackagesJournal mProcessedPackagesJournal;
Robert Berry6d2fbdf2017-07-17 09:56:44 +010058
59 @Before
60 public void setUp() throws Exception {
61 MockitoAnnotations.initMocks(this);
62 mStateDirectory = mTemporaryFolder.newFolder();
Robert Berrya1109ef2017-07-17 09:56:44 +010063 mProcessedPackagesJournal = new ProcessedPackagesJournal(mStateDirectory);
64 mProcessedPackagesJournal.init();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010065 }
66
67 @Test
68 public void constructor_loadsAnyPreviousJournalFromDisk() throws Exception {
69 writePermanentJournalPackages(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL));
70
Robert Berrya1109ef2017-07-17 09:56:44 +010071 ProcessedPackagesJournal journalFromDisk =
72 new ProcessedPackagesJournal(mStateDirectory);
73 journalFromDisk.init();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010074
75 assertThat(journalFromDisk.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue();
76 assertThat(journalFromDisk.hasBeenProcessed(GMAIL)).isTrue();
77 }
78
79 @Test
80 public void hasBeenProcessed_isFalseForAnyPackageFromBlankInit() {
Robert Berrya1109ef2017-07-17 09:56:44 +010081 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse();
82 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
83 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010084 }
85
86 @Test
87 public void addPackage_addsPackageToObjectState() {
Robert Berrya1109ef2017-07-17 09:56:44 +010088 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
Robert Berry6d2fbdf2017-07-17 09:56:44 +010089
Robert Berrya1109ef2017-07-17 09:56:44 +010090 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010091 }
92
93 @Test
94 public void addPackage_addsPackageToFileSystem() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +010095 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
Robert Berry6d2fbdf2017-07-17 09:56:44 +010096
97 assertThat(readJournalPackages()).contains(GOOGLE_PHOTOS);
98 }
99
100 @Test
101 public void getPackagesCopy_returnsTheCurrentState() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100102 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
103 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100104
Robert Berrya1109ef2017-07-17 09:56:44 +0100105 assertThat(mProcessedPackagesJournal.getPackagesCopy())
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100106 .isEqualTo(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL));
107 }
108
109 @Test
110 public void getPackagesCopy_returnsACopy() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100111 mProcessedPackagesJournal.getPackagesCopy().add(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100112
Robert Berrya1109ef2017-07-17 09:56:44 +0100113 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100114 }
115
116 @Test
117 public void reset_removesAllPackagesFromObjectState() {
Robert Berrya1109ef2017-07-17 09:56:44 +0100118 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
119 mProcessedPackagesJournal.addPackage(GOOGLE_PLUS);
120 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100121
Robert Berrya1109ef2017-07-17 09:56:44 +0100122 mProcessedPackagesJournal.reset();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100123
Robert Berrya1109ef2017-07-17 09:56:44 +0100124 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse();
125 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
126 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100127 }
128
129 @Test
130 public void reset_removesAllPackagesFromFileSystem() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100131 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
132 mProcessedPackagesJournal.addPackage(GOOGLE_PLUS);
133 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100134
Robert Berrya1109ef2017-07-17 09:56:44 +0100135 mProcessedPackagesJournal.reset();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100136
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}