1 /*
2  * Copyright (C) 2020 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.power;
18 
19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.pm.PackageInstaller;
27 import android.content.pm.PackageInstaller.SessionInfo;
28 import android.content.pm.PackageManager;
29 import android.provider.Settings;
30 import android.test.mock.MockContentResolver;
31 
32 import androidx.test.filters.SmallTest;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import com.android.internal.util.test.FakeSettingsProvider;
36 
37 import com.google.common.io.Files;
38 
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.junit.MockitoJUnit;
47 import org.mockito.junit.MockitoRule;
48 
49 import java.io.File;
50 import java.util.List;
51 
52 /**
53  * Tests for {@link PreRebootLogger}
54  */
55 @SmallTest
56 @RunWith(AndroidJUnit4.class)
57 public class PreRebootLoggerTest {
58     @Rule public final MockitoRule mocks = MockitoJUnit.rule();
59     @Mock Context mContext;
60     @Mock PackageManager mPackageManager;
61     @Mock PackageInstaller mPackageInstaller;
62     @Mock List<SessionInfo> mSessions;
63     private MockContentResolver mContentResolver;
64     private File mDumpDir;
65 
66     @BeforeClass
setupOnce()67     public static void setupOnce() {
68         FakeSettingsProvider.clearSettingsProvider();
69     }
70 
71     @AfterClass
tearDownOnce()72     public static void tearDownOnce() {
73         FakeSettingsProvider.clearSettingsProvider();
74     }
75 
76     @Before
enableAdbConfig()77     public void enableAdbConfig() {
78         mContentResolver = new MockContentResolver(getInstrumentation().getTargetContext());
79         when(mContext.getContentResolver()).thenReturn(mContentResolver);
80         mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
81         Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, 1);
82     }
83 
84     @Before
prepareActiveStagedSessions()85     public void prepareActiveStagedSessions() {
86         when(mContext.getPackageManager()).thenReturn(mPackageManager);
87         when(mPackageManager.getPackageInstaller()).thenReturn(mPackageInstaller);
88         when(mPackageInstaller.getActiveStagedSessions()).thenReturn(mSessions);
89         when(mSessions.isEmpty()).thenReturn(false);
90     }
91 
92     @Before
setupDumpDir()93     public void setupDumpDir() {
94         mDumpDir = Files.createTempDir();
95         mDumpDir.deleteOnExit();
96     }
97 
98     @Test
log_dumpsInformationProperly()99     public void log_dumpsInformationProperly() {
100         PreRebootLogger.log(mContext, mDumpDir);
101 
102         assertThat(mDumpDir.list()).asList().containsExactly("system", "package", "rollback");
103     }
104 
105     @Test
dump_exceedTimeout_wontBlockCurrentThread()106     public void dump_exceedTimeout_wontBlockCurrentThread() {
107         PreRebootLogger.dump(mDumpDir, 1 /* maxWaitTime */);
108 
109         assertThat(mDumpDir.listFiles()).asList().containsNoneOf("system", "package", "rollback");
110     }
111 
112     @Test
log_noActiveStagedSession_wipesDumpedInformation()113     public void log_noActiveStagedSession_wipesDumpedInformation() {
114         PreRebootLogger.log(mContext, mDumpDir);
115         when(mSessions.isEmpty()).thenReturn(true);
116 
117         PreRebootLogger.log(mContext, mDumpDir);
118 
119         assertThat(mDumpDir.listFiles()).isEmpty();
120     }
121 
122     @Test
log_adbDisabled_wipesDumpedInformation()123     public void log_adbDisabled_wipesDumpedInformation() {
124         PreRebootLogger.log(mContext, mDumpDir);
125         Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, 0);
126 
127         PreRebootLogger.log(mContext, mDumpDir);
128 
129         assertThat(mDumpDir.listFiles()).isEmpty();
130     }
131 }
132