blob: b4a1f1857d77532d098ce3e64ec59b0d7646303f [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;
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
24
25import com.google.android.collect.Sets;
26
27import org.junit.Before;
28import org.junit.Rule;
29import org.junit.Test;
30import org.junit.rules.TemporaryFolder;
31import org.junit.runner.RunWith;
32import org.mockito.MockitoAnnotations;
33
34import java.io.DataInputStream;
35import java.io.DataOutputStream;
36import java.io.File;
37import java.io.FileInputStream;
38import java.io.FileNotFoundException;
39import java.io.FileOutputStream;
40import java.util.HashSet;
41import java.util.Set;
42
43@SmallTest
44@Presubmit
45@RunWith(AndroidJUnit4.class)
Robert Berrya1109ef2017-07-17 09:56:44 +010046public class ProcessedPackagesJournalTest {
Robert Berry6d2fbdf2017-07-17 09:56:44 +010047 private static final String JOURNAL_FILE_NAME = "processed";
48
49 private static final String GOOGLE_PHOTOS = "com.google.photos";
50 private static final String GMAIL = "com.google.gmail";
51 private static final String GOOGLE_PLUS = "com.google.plus";
52
53 @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
54
55 private File mStateDirectory;
Robert Berrya1109ef2017-07-17 09:56:44 +010056 private ProcessedPackagesJournal mProcessedPackagesJournal;
Robert Berry6d2fbdf2017-07-17 09:56:44 +010057
58 @Before
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
61 mStateDirectory = mTemporaryFolder.newFolder();
Robert Berrya1109ef2017-07-17 09:56:44 +010062 mProcessedPackagesJournal = new ProcessedPackagesJournal(mStateDirectory);
63 mProcessedPackagesJournal.init();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010064 }
65
66 @Test
67 public void constructor_loadsAnyPreviousJournalFromDisk() throws Exception {
68 writePermanentJournalPackages(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL));
69
Robert Berrya1109ef2017-07-17 09:56:44 +010070 ProcessedPackagesJournal journalFromDisk =
71 new ProcessedPackagesJournal(mStateDirectory);
72 journalFromDisk.init();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010073
74 assertThat(journalFromDisk.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue();
75 assertThat(journalFromDisk.hasBeenProcessed(GMAIL)).isTrue();
76 }
77
78 @Test
79 public void hasBeenProcessed_isFalseForAnyPackageFromBlankInit() {
Robert Berrya1109ef2017-07-17 09:56:44 +010080 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse();
81 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
82 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010083 }
84
85 @Test
86 public void addPackage_addsPackageToObjectState() {
Robert Berrya1109ef2017-07-17 09:56:44 +010087 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
Robert Berry6d2fbdf2017-07-17 09:56:44 +010088
Robert Berrya1109ef2017-07-17 09:56:44 +010089 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue();
Robert Berry6d2fbdf2017-07-17 09:56:44 +010090 }
91
92 @Test
93 public void addPackage_addsPackageToFileSystem() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +010094 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
Robert Berry6d2fbdf2017-07-17 09:56:44 +010095
96 assertThat(readJournalPackages()).contains(GOOGLE_PHOTOS);
97 }
98
99 @Test
100 public void getPackagesCopy_returnsTheCurrentState() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100101 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
102 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100103
Robert Berrya1109ef2017-07-17 09:56:44 +0100104 assertThat(mProcessedPackagesJournal.getPackagesCopy())
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100105 .isEqualTo(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL));
106 }
107
108 @Test
109 public void getPackagesCopy_returnsACopy() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100110 mProcessedPackagesJournal.getPackagesCopy().add(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100111
Robert Berrya1109ef2017-07-17 09:56:44 +0100112 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100113 }
114
115 @Test
116 public void reset_removesAllPackagesFromObjectState() {
Robert Berrya1109ef2017-07-17 09:56:44 +0100117 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
118 mProcessedPackagesJournal.addPackage(GOOGLE_PLUS);
119 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100120
Robert Berrya1109ef2017-07-17 09:56:44 +0100121 mProcessedPackagesJournal.reset();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100122
Robert Berrya1109ef2017-07-17 09:56:44 +0100123 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse();
124 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GMAIL)).isFalse();
125 assertThat(mProcessedPackagesJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100126 }
127
128 @Test
129 public void reset_removesAllPackagesFromFileSystem() throws Exception {
Robert Berrya1109ef2017-07-17 09:56:44 +0100130 mProcessedPackagesJournal.addPackage(GOOGLE_PHOTOS);
131 mProcessedPackagesJournal.addPackage(GOOGLE_PLUS);
132 mProcessedPackagesJournal.addPackage(GMAIL);
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100133
Robert Berrya1109ef2017-07-17 09:56:44 +0100134 mProcessedPackagesJournal.reset();
Robert Berry6d2fbdf2017-07-17 09:56:44 +0100135
136 assertThat(readJournalPackages()).isEmpty();
137 }
138
139 private HashSet<String> readJournalPackages() throws Exception {
140 File journal = new File(mStateDirectory, JOURNAL_FILE_NAME);
141 HashSet<String> packages = new HashSet<>();
142
143 try (FileInputStream fis = new FileInputStream(journal);
144 DataInputStream dis = new DataInputStream(fis)) {
145 while (dis.available() > 0) {
146 packages.add(dis.readUTF());
147 }
148 } catch (FileNotFoundException e) {
149 return new HashSet<>();
150 }
151
152 return packages;
153 }
154
155 private void writePermanentJournalPackages(Set<String> packages) throws Exception {
156 File journal = new File(mStateDirectory, JOURNAL_FILE_NAME);
157
158 try (FileOutputStream fos = new FileOutputStream(journal);
159 DataOutputStream dos = new DataOutputStream(fos)) {
160 for (String packageName : packages) {
161 dos.writeUTF(packageName);
162 }
163 }
164 }
165}