1 /*
2  * Copyright (C) 2023 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 
19 import static org.junit.Assert.assertTrue;
20 import static org.testng.AssertJUnit.assertFalse;
21 import android.app.backup.BackupAnnotations;
22 import android.app.backup.BackupManagerMonitor;
23 import android.os.Bundle;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TemporaryFolder;
28 import java.io.File;
29 import java.io.FileWriter;
30 
31 public class BackupManagerMonitorDumpsysUtilsTest {
32     private long mRetentionPeriod;
33     private File mTempBMMEventsFile;
34     private File mTempSetUpDateFile;
35 
36     private long mSizeLimit;
37     private TestBackupManagerMonitorDumpsysUtils mBackupManagerMonitorDumpsysUtils;
38     @Rule
39     public TemporaryFolder tmp = new TemporaryFolder();
40 
41     @Before
setUp()42     public void setUp() throws Exception {
43         mRetentionPeriod = 30 * 60 * 1000;
44         mSizeLimit = 25 * 1024 * 1000;
45         mTempBMMEventsFile = tmp.newFile("testbmmevents.txt");
46         mTempSetUpDateFile = tmp.newFile("testSetUpDate.txt");
47         mBackupManagerMonitorDumpsysUtils = new TestBackupManagerMonitorDumpsysUtils();
48     }
49 
50 
51     @Test
parseBackupManagerMonitorEventForDumpsys_bundleIsNull_noLogsWrittenToFile()52     public void parseBackupManagerMonitorEventForDumpsys_bundleIsNull_noLogsWrittenToFile()
53             throws Exception {
54         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(null);
55 
56         assertTrue(mTempBMMEventsFile.length() == 0);
57 
58     }
59 
60     @Test
parseBackupManagerMonitorEventForDumpsys_missingID_noLogsWrittenToFile()61     public void parseBackupManagerMonitorEventForDumpsys_missingID_noLogsWrittenToFile()
62             throws Exception {
63         Bundle event = new Bundle();
64         event.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_CATEGORY, 1);
65         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
66 
67         assertTrue(mTempBMMEventsFile.length() == 0);
68     }
69 
70     @Test
parseBackupManagerMonitorEventForDumpsys_missingCategory_noLogsWrittenToFile()71     public void parseBackupManagerMonitorEventForDumpsys_missingCategory_noLogsWrittenToFile()
72             throws Exception {
73         Bundle event = new Bundle();
74         event.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_ID, 1);
75         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
76 
77         assertTrue(mTempBMMEventsFile.length() == 0);
78     }
79 
80     @Test
parseBackupManagerMonitorEventForDumpsys_eventWithCategoryAndId_eventIsWrittenToFile()81     public void parseBackupManagerMonitorEventForDumpsys_eventWithCategoryAndId_eventIsWrittenToFile()
82             throws Exception {
83         Bundle event = createRestoreBMMEvent();
84         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
85 
86         assertTrue(mTempBMMEventsFile.length() != 0);
87     }
88 
89     @Test
parseBackupManagerMonitorEventForDumpsys_firstEvent_recordSetUpTimestamp()90     public void parseBackupManagerMonitorEventForDumpsys_firstEvent_recordSetUpTimestamp()
91             throws Exception {
92         assertTrue(mTempBMMEventsFile.length()==0);
93         assertTrue(mTempSetUpDateFile.length()==0);
94 
95         Bundle event = createRestoreBMMEvent();
96         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
97 
98         assertTrue(mTempBMMEventsFile.length() != 0);
99         assertTrue(mTempSetUpDateFile.length()!=0);
100     }
101 
102     @Test
parseBackupManagerMonitorEventForDumpsys_notFirstEvent_doNotChangeSetUpTimestamp()103     public void parseBackupManagerMonitorEventForDumpsys_notFirstEvent_doNotChangeSetUpTimestamp()
104             throws Exception {
105         Bundle event1 = createRestoreBMMEvent();
106         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event1);
107         String setUpTimestampBefore = mBackupManagerMonitorDumpsysUtils.getSetUpDate();
108 
109         Bundle event2 = createRestoreBMMEvent();
110         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event2);
111         String setUpTimestampAfter = mBackupManagerMonitorDumpsysUtils.getSetUpDate();
112 
113         assertTrue(setUpTimestampBefore.equals(setUpTimestampAfter));
114     }
115 
116 
117     @Test
parseBackupManagerMonitorEventForDumpsys_fileOverSizeLimit_doNotRecordEvents()118     public void parseBackupManagerMonitorEventForDumpsys_fileOverSizeLimit_doNotRecordEvents()
119             throws Exception {
120         assertTrue(mTempBMMEventsFile.length() == 0);
121         Bundle event = createRestoreBMMEvent();
122         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
123         long fileSizeBefore = mTempBMMEventsFile.length();
124 
125         mBackupManagerMonitorDumpsysUtils.setTestSizeLimit(0);
126         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
127         long fileSizeAfter = mTempBMMEventsFile.length();
128         assertTrue(mBackupManagerMonitorDumpsysUtils.isFileLargerThanSizeLimit(mTempBMMEventsFile));
129         assertTrue(fileSizeBefore == fileSizeAfter);
130     }
131 
132     @Test
parseBackupManagerMonitorEventForDumpsys_fileUnderSizeLimit_recordEvents()133     public void parseBackupManagerMonitorEventForDumpsys_fileUnderSizeLimit_recordEvents()
134             throws Exception {
135         assertTrue(mTempBMMEventsFile.length() == 0);
136         Bundle event = createRestoreBMMEvent();
137 
138         mBackupManagerMonitorDumpsysUtils.setTestSizeLimit(25 * 1024 * 1000);
139         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
140         assertFalse(mBackupManagerMonitorDumpsysUtils.isFileLargerThanSizeLimit(mTempBMMEventsFile));
141         assertTrue(mTempBMMEventsFile.length() != 0);
142     }
143 
144     @Test
deleteExpiredBackupManagerMonitorEvent_eventsAreExpired_deleteEventsAndReturnTrue()145     public void deleteExpiredBackupManagerMonitorEvent_eventsAreExpired_deleteEventsAndReturnTrue()
146             throws Exception {
147         Bundle event = createRestoreBMMEvent();
148         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
149         assertTrue(mTempBMMEventsFile.length() != 0);
150         // Re-initialise the test BackupManagerMonitorDumpsysUtils to
151         // clear the cached value of isAfterRetentionPeriod
152         mBackupManagerMonitorDumpsysUtils = new TestBackupManagerMonitorDumpsysUtils();
153 
154         // set a retention period of 0 second
155         mBackupManagerMonitorDumpsysUtils.setTestRetentionPeriod(0);
156 
157         assertTrue(mBackupManagerMonitorDumpsysUtils.deleteExpiredBMMEvents());
158         assertFalse(mTempBMMEventsFile.exists());
159     }
160 
161     @Test
deleteExpiredBackupManagerMonitorEvent_eventsAreNotExpired_returnFalse()162     public void deleteExpiredBackupManagerMonitorEvent_eventsAreNotExpired_returnFalse() throws
163             Exception {
164         Bundle event = createRestoreBMMEvent();
165         mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
166         assertTrue(mTempBMMEventsFile.length() != 0);
167 
168         // set a retention period of 30 minutes
169         mBackupManagerMonitorDumpsysUtils.setTestRetentionPeriod(30 * 60 * 1000);
170 
171         assertFalse(mBackupManagerMonitorDumpsysUtils.deleteExpiredBMMEvents());
172         assertTrue(mTempBMMEventsFile.length() != 0);
173     }
174 
175     @Test
isAfterRetentionPeriod_afterRetentionPeriod_returnTrue()176     public void isAfterRetentionPeriod_afterRetentionPeriod_returnTrue() throws
177             Exception {
178         mBackupManagerMonitorDumpsysUtils.recordSetUpTimestamp();
179 
180         // set a retention period of 0 second
181         mBackupManagerMonitorDumpsysUtils.setTestRetentionPeriod(0);
182 
183         assertTrue(mBackupManagerMonitorDumpsysUtils.isAfterRetentionPeriod());
184     }
185 
186     @Test
isAfterRetentionPeriod_beforeRetentionPeriod_returnFalse()187     public void isAfterRetentionPeriod_beforeRetentionPeriod_returnFalse() throws
188             Exception {
189         mBackupManagerMonitorDumpsysUtils.recordSetUpTimestamp();
190 
191         // set a retention period of 30 minutes
192         mBackupManagerMonitorDumpsysUtils.setTestRetentionPeriod(30 * 60 * 1000);
193 
194         assertFalse(mBackupManagerMonitorDumpsysUtils.isAfterRetentionPeriod());
195     }
196 
197     @Test
isAfterRetentionPeriod_noSetupDate_returnFalse()198     public void isAfterRetentionPeriod_noSetupDate_returnFalse() throws
199             Exception {
200         assertTrue(mTempSetUpDateFile.length() == 0);
201 
202         assertFalse(mBackupManagerMonitorDumpsysUtils.isAfterRetentionPeriod());
203     }
204 
205     @Test
isDateAfterNMillisec_date1IsAfterThanDate2_returnTrue()206     public void isDateAfterNMillisec_date1IsAfterThanDate2_returnTrue() throws
207             Exception {
208         long timestamp1 = System.currentTimeMillis();
209         long timestamp2 = timestamp1 - 1;
210 
211         assertTrue(mBackupManagerMonitorDumpsysUtils.isDateAfterNMillisec(timestamp1, timestamp2,
212                 0));
213     }
214 
215     @Test
isDateAfterNMillisec_date1IsAfterNMillisecFromDate2_returnTrue()216     public void isDateAfterNMillisec_date1IsAfterNMillisecFromDate2_returnTrue() throws
217             Exception {
218         long timestamp1 = System.currentTimeMillis();
219         long timestamp2 = timestamp1 + 10;
220 
221         assertTrue(mBackupManagerMonitorDumpsysUtils.isDateAfterNMillisec(timestamp1, timestamp2,
222                 10));
223     }
224 
225     @Test
isDateAfterNMillisec_date1IsLessThanNMillisecFromDate2_returnFalse()226     public void isDateAfterNMillisec_date1IsLessThanNMillisecFromDate2_returnFalse() throws
227             Exception {
228         long timestamp1 = System.currentTimeMillis();
229         long timestamp2 = timestamp1 + 10;
230 
231         assertFalse(mBackupManagerMonitorDumpsysUtils.isDateAfterNMillisec(timestamp1, timestamp2,
232                 11));
233     }
234 
235     @Test
recordSetUpTimestamp_timestampNotSetBefore_setTimestamp()236     public void recordSetUpTimestamp_timestampNotSetBefore_setTimestamp() throws
237             Exception {
238         assertTrue(mTempSetUpDateFile.length() == 0);
239 
240         mBackupManagerMonitorDumpsysUtils.recordSetUpTimestamp();
241 
242         assertTrue(mTempSetUpDateFile.length() != 0);
243     }
244 
245     @Test
recordSetUpTimestamp_timestampSetBefore_doNothing()246     public void recordSetUpTimestamp_timestampSetBefore_doNothing() throws
247             Exception {
248         mBackupManagerMonitorDumpsysUtils.recordSetUpTimestamp();
249         assertTrue(mTempSetUpDateFile.length() != 0);
250         String timestampBefore = mBackupManagerMonitorDumpsysUtils.getSetUpDate();
251 
252         mBackupManagerMonitorDumpsysUtils.recordSetUpTimestamp();
253 
254         assertTrue(mTempSetUpDateFile.length() != 0);
255         String timestampAfter = mBackupManagerMonitorDumpsysUtils.getSetUpDate();
256         assertTrue(timestampAfter.equals(timestampBefore));
257     }
258 
createRestoreBMMEvent()259     private Bundle createRestoreBMMEvent() {
260         Bundle event = new Bundle();
261         event.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_ID, 1);
262         event.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_CATEGORY, 1);
263         event.putInt(BackupManagerMonitor.EXTRA_LOG_OPERATION_TYPE,
264                 BackupAnnotations.OperationType.RESTORE);
265         return event;
266     }
267 
268     private class TestBackupManagerMonitorDumpsysUtils
269             extends BackupManagerMonitorDumpsysUtils {
270 
271         private long testRetentionPeriod;
272         private long testSizeLimit;
273 
TestBackupManagerMonitorDumpsysUtils()274         TestBackupManagerMonitorDumpsysUtils() {
275             super();
276             this.testRetentionPeriod = mRetentionPeriod;
277             this.testSizeLimit = mSizeLimit;
278         }
279 
setTestRetentionPeriod(long testRetentionPeriod)280         public void setTestRetentionPeriod(long testRetentionPeriod) {
281             this.testRetentionPeriod = testRetentionPeriod;
282         }
setTestSizeLimit(long testSizeLimit)283         public void setTestSizeLimit(long testSizeLimit) {
284             this.testSizeLimit = testSizeLimit;
285         }
286 
287         @Override
getBMMEventsFile()288         public File getBMMEventsFile() {
289             return mTempBMMEventsFile;
290         }
291 
292         @Override
getSetUpDateFile()293         File getSetUpDateFile() {
294             return mTempSetUpDateFile;
295         }
296 
297         @Override
getRetentionPeriodInMillisec()298         long getRetentionPeriodInMillisec() {
299             return testRetentionPeriod;
300         }
301 
302         @Override
getBMMEventsFileSizeLimit()303         long getBMMEventsFileSizeLimit(){
304             return testSizeLimit;
305         }
306 
307 
308     }
309 }
310