Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f8ff834 authored by beatricemarch's avatar beatricemarch Committed by Beatrice Marchegiani
Browse files

Add a 2.5 MB size limit to the text file storing BMM Events.

Test: atest CtsBackupHostTestCases, GtsBackupHostTestCases
atest BackupManagerMonitorDumpsysUtilsTest, UserBackupManagerServiceTest
Bug: 297163567

Change-Id: Ia1fdc0b465b960337f5e5c626460e305d96f4eb2
parent 501eeee2
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -486,6 +486,13 @@ public class UserBackupManagerService {
            File baseStateDir,
            File dataDir,
            TransportManager transportManager) {
        // check if we are past the retention period for BMM Events,
        // if so delete expired events and do not print them to dumpsys
        BackupManagerMonitorDumpsysUtils backupManagerMonitorDumpsysUtils =
                new BackupManagerMonitorDumpsysUtils();
        if (backupManagerMonitorDumpsysUtils.deleteExpiredBMMEvents() && DEBUG){
            Slog.d(TAG, "BMM Events recorded for dumpsys have expired");
        }
        return new UserBackupManagerService(
                userId,
                context,
@@ -4197,6 +4204,9 @@ public class UserBackupManagerService {
            // We have not recorded BMMEvents yet.
            pw.println("NO BACKUP MANAGER MONITOR EVENTS");
            return;
        } else if (bm.isFileLargerThanSizeLimit(events)){
            pw.println("BACKUP MANAGER MONITOR EVENTS FILE OVER SIZE LIMIT - "
                    + "future events will not be recorded");
        }
        pw.println("START OF BACKUP MANAGER MONITOR EVENTS");
        try (BufferedReader reader = new BufferedReader(new FileReader(events))) {
+24 −1
Original line number Diff line number Diff line
@@ -57,11 +57,17 @@ public class BackupManagerMonitorDumpsysUtils {
    // Retention period of 60 days (in millisec) for the BMM Events.
    // After tha time has passed the text file containing the BMM events will be emptied
    private static final long LOGS_RETENTION_PERIOD_MILLISEC = 60 * TimeUnit.DAYS.toMillis(1);
    // Size limit for the text file containing the BMM events
    private static final long BMM_FILE_SIZE_LIMIT_BYTES = 25 * 1024 * 1000; // 2.5 MB

    // We cache the value of IsAfterRetentionPeriod() to avoid unnecessary disk I/O
    // mIsAfterRetentionPeriodCached tracks if we have cached the value of IsAfterRetentionPeriod()
    private boolean mIsAfterRetentionPeriodCached = false;
    // The cahched value of IsAfterRetentionPeriod()
    // The cached value of IsAfterRetentionPeriod()
    private boolean mIsAfterRetentionPeriod;
    // If isFileLargerThanSizeLimit(bmmEvents)  returns true we cache the value to avoid
    // unnecessary disk I/O
   private boolean mIsFileLargerThanSizeLimit = false;

    /**
     * Parses the BackupManagerMonitor bundle for a RESTORE event in a series of strings that
@@ -116,6 +122,11 @@ public class BackupManagerMonitorDumpsysUtils {
            recordSetUpTimestamp();
        }

        if(isFileLargerThanSizeLimit(bmmEvents)){
            // Do not write more events if the file is over size limit
            return;
        }

        try (FileOutputStream out = new FileOutputStream(bmmEvents, /*append*/ true);
             PrintWriter pw = new FastPrintWriter(out);) {

@@ -192,6 +203,13 @@ public class BackupManagerMonitorDumpsysUtils {
        return fname;
    }

    public boolean isFileLargerThanSizeLimit(File events){
        if (!mIsFileLargerThanSizeLimit) {
            mIsFileLargerThanSizeLimit = events.length() > getBMMEventsFileSizeLimit();
        }
        return mIsFileLargerThanSizeLimit;
    }

    private String timestamp() {
        long currentTime = System.currentTimeMillis();
        Date date = new Date(currentTime);
@@ -402,6 +420,11 @@ public class BackupManagerMonitorDumpsysUtils {
        return LOGS_RETENTION_PERIOD_MILLISEC;
    }

    @VisibleForTesting
    long getBMMEventsFileSizeLimit(){
        return BMM_FILE_SIZE_LIMIT_BYTES;
    }

    /**
     * Delete the BMM Events file after the retention period has passed.
     *
+42 −0
Original line number Diff line number Diff line
@@ -26,11 +26,14 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileWriter;

public class BackupManagerMonitorDumpsysUtilsTest {
    private long mRetentionPeriod;
    private File mTempBMMEventsFile;
    private File mTempSetUpDateFile;

    private long mSizeLimit;
    private TestBackupManagerMonitorDumpsysUtils mBackupManagerMonitorDumpsysUtils;
    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();
@@ -38,6 +41,7 @@ public class BackupManagerMonitorDumpsysUtilsTest {
    @Before
    public void setUp() throws Exception {
        mRetentionPeriod = 30 * 60 * 1000;
        mSizeLimit = 25 * 1024 * 1000;
        mTempBMMEventsFile = tmp.newFile("testbmmevents.txt");
        mTempSetUpDateFile = tmp.newFile("testSetUpDate.txt");
        mBackupManagerMonitorDumpsysUtils = new TestBackupManagerMonitorDumpsysUtils();
@@ -110,6 +114,33 @@ public class BackupManagerMonitorDumpsysUtilsTest {
    }


    @Test
    public void parseBackupManagerMonitorEventForDumpsys_fileOverSizeLimit_doNotRecordEvents()
            throws Exception {
        assertTrue(mTempBMMEventsFile.length() == 0);
        Bundle event = createRestoreBMMEvent();
        mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
        long fileSizeBefore = mTempBMMEventsFile.length();

        mBackupManagerMonitorDumpsysUtils.setTestSizeLimit(0);
        mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
        long fileSizeAfter = mTempBMMEventsFile.length();
        assertTrue(mBackupManagerMonitorDumpsysUtils.isFileLargerThanSizeLimit(mTempBMMEventsFile));
        assertTrue(fileSizeBefore == fileSizeAfter);
    }

    @Test
    public void parseBackupManagerMonitorEventForDumpsys_fileUnderSizeLimit_recordEvents()
            throws Exception {
        assertTrue(mTempBMMEventsFile.length() == 0);
        Bundle event = createRestoreBMMEvent();

        mBackupManagerMonitorDumpsysUtils.setTestSizeLimit(25 * 1024 * 1000);
        mBackupManagerMonitorDumpsysUtils.parseBackupManagerMonitorRestoreEventForDumpsys(event);
        assertFalse(mBackupManagerMonitorDumpsysUtils.isFileLargerThanSizeLimit(mTempBMMEventsFile));
        assertTrue(mTempBMMEventsFile.length() != 0);
    }

    @Test
    public void deleteExpiredBackupManagerMonitorEvent_eventsAreExpired_deleteEventsAndReturnTrue()
            throws Exception {
@@ -238,15 +269,20 @@ public class BackupManagerMonitorDumpsysUtilsTest {
            extends BackupManagerMonitorDumpsysUtils {

        private long testRetentionPeriod;
        private long testSizeLimit;

        TestBackupManagerMonitorDumpsysUtils() {
            super();
            this.testRetentionPeriod = mRetentionPeriod;
            this.testSizeLimit = mSizeLimit;
        }

        public void setTestRetentionPeriod(long testRetentionPeriod) {
            this.testRetentionPeriod = testRetentionPeriod;
        }
        public void setTestSizeLimit(long testSizeLimit) {
            this.testSizeLimit = testSizeLimit;
        }

        @Override
        public File getBMMEventsFile() {
@@ -263,5 +299,11 @@ public class BackupManagerMonitorDumpsysUtilsTest {
            return testRetentionPeriod;
        }

        @Override
        long getBMMEventsFileSizeLimit(){
            return testSizeLimit;
        }


    }
}