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.om;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.om.OverlayIdentifier;
22 import android.content.om.OverlayInfo;
23 import android.content.pm.UserPackage;
24 
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.google.common.truth.Expect;
28 
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.Set;
34 import java.util.function.Consumer;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class OverlayManagerServiceImplRebootTests extends OverlayManagerServiceImplTestsBase {
38 
39     private static final String OVERLAY = "com.test.overlay";
40     private static final OverlayIdentifier IDENTIFIER = new OverlayIdentifier(OVERLAY);
41     private static final String TARGET = "com.test.target";
42     private static final int USER = 0;
43 
44     private static final String OVERLAY2 = OVERLAY + "2";
45     private static final OverlayIdentifier IDENTIFIER2 = new OverlayIdentifier(OVERLAY2);
46 
47     @Rule
48     public final Expect expect = Expect.create();
49 
50     @Test
alwaysInitializeAllPackages()51     public void alwaysInitializeAllPackages() {
52         final OverlayManagerServiceImpl impl = getImpl();
53         final String otherTarget = "some.other.target";
54         addPackage(target(TARGET), USER);
55         addPackage(target(otherTarget), USER);
56         addPackage(overlay(OVERLAY, TARGET), USER);
57 
58         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
59 
60         // The result should be the same for every time
61         assertThat(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
62         assertThat(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
63     }
64 
65     @Test
testImmutableEnabledChange()66     public void testImmutableEnabledChange() {
67         final OverlayManagerServiceImpl impl = getImpl();
68         addPackage(target(TARGET), USER);
69         addPackage(overlay(OVERLAY, TARGET), USER);
70 
71         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
72 
73         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
74         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
75         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
76         expect.that(o1).isNotNull();
77         assertThat(expect.hasFailures()).isFalse();
78         expect.that(o1.isEnabled()).isFalse();
79         expect.that(o1.isMutable).isFalse();
80 
81         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
82         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
83         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
84         expect.that(o2).isNotNull();
85         assertThat(expect.hasFailures()).isFalse();
86         expect.that(o2.isEnabled()).isTrue();
87         expect.that(o2.isMutable).isFalse();
88 
89         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
90         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
91         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
92         expect.that(o3).isNotNull();
93         assertThat(expect.hasFailures()).isFalse();
94         expect.that(o3.isEnabled()).isFalse();
95         expect.that(o3.isMutable).isFalse();
96     }
97 
98     @Test
testMutableEnabledChangeHasNoEffect()99     public void testMutableEnabledChangeHasNoEffect() {
100         final OverlayManagerServiceImpl impl = getImpl();
101         addPackage(target(TARGET), USER);
102         addPackage(overlay(OVERLAY, TARGET), USER);
103         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
104 
105         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
106 
107         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
108         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
109         expect.that(o1).isNotNull();
110         assertThat(expect.hasFailures()).isFalse();
111         expect.that(o1.isEnabled()).isFalse();
112         expect.that(o1.isMutable).isTrue();
113 
114         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_ENABLED, 0 /* priority */);
115         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
116         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
117         expect.that(o2).isNotNull();
118         assertThat(expect.hasFailures()).isFalse();
119         expect.that(o2.isEnabled()).isFalse();
120         expect.that(o2.isMutable).isTrue();
121 
122         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
123         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
124         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
125         expect.that(o3).isNotNull();
126         assertThat(expect.hasFailures()).isFalse();
127         expect.that(o3.isEnabled()).isFalse();
128         expect.that(o3.isMutable).isTrue();
129     }
130 
131     @Test
testMutableEnabledToImmutableEnabled()132     public void testMutableEnabledToImmutableEnabled() {
133         final OverlayManagerServiceImpl impl = getImpl();
134         addPackage(target(TARGET), USER);
135         addPackage(overlay(OVERLAY, TARGET), USER);
136 
137         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
138 
139         final Consumer<ConfigState> setOverlay = (state -> {
140             configureSystemOverlay(OVERLAY, state, 0 /* priority */);
141             expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
142             final OverlayInfo o = impl.getOverlayInfo(IDENTIFIER, USER);
143             expect.that(o).isNotNull();
144             assertThat(expect.hasFailures()).isFalse();
145             expect.that(o.isEnabled()).isEqualTo(state == ConfigState.IMMUTABLE_ENABLED
146                     || state == ConfigState.MUTABLE_ENABLED);
147             expect.that(o.isMutable).isEqualTo(state == ConfigState.MUTABLE_DISABLED
148                     || state == ConfigState.MUTABLE_ENABLED);
149         });
150 
151         // Immutable/enabled -> mutable/enabled
152         setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);
153         setOverlay.accept(ConfigState.MUTABLE_ENABLED);
154 
155         // Mutable/enabled -> immutable/enabled
156         setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);
157 
158         // Immutable/enabled -> mutable/disabled
159         setOverlay.accept(ConfigState.MUTABLE_DISABLED);
160 
161         // Mutable/disabled -> immutable/enabled
162         setOverlay.accept(ConfigState.IMMUTABLE_ENABLED);
163 
164         // Immutable/enabled -> immutable/disabled
165         setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);
166 
167         // Immutable/disabled -> mutable/enabled
168         setOverlay.accept(ConfigState.MUTABLE_ENABLED);
169 
170         // Mutable/enabled -> immutable/disabled
171         setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);
172 
173         // Immutable/disabled -> mutable/disabled
174         setOverlay.accept(ConfigState.MUTABLE_DISABLED);
175 
176         // Mutable/disabled -> immutable/disabled
177         setOverlay.accept(ConfigState.IMMUTABLE_DISABLED);
178     }
179 
180     @Test
testMutablePriorityChange()181     public void testMutablePriorityChange() throws Exception {
182         final OverlayManagerServiceImpl impl = getImpl();
183         addPackage(target(TARGET), USER);
184         addPackage(overlay(OVERLAY, TARGET), USER);
185         addPackage(overlay(OVERLAY2, TARGET), USER);
186         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
187         configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 1 /* priority */);
188 
189         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
190 
191         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
192         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
193         expect.that(o1).isNotNull();
194         assertThat(expect.hasFailures()).isFalse();
195         expect.that(o1.priority).isEqualTo(0);
196         expect.that(o1.isEnabled()).isFalse();
197 
198         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER2, USER);
199         expect.that(o2).isNotNull();
200         assertThat(expect.hasFailures()).isFalse();
201         expect.that(o2.priority).isEqualTo(1);
202         expect.that(o2.isEnabled()).isFalse();
203 
204         // Overlay priority changing between reboots should not affect enable state of mutable
205         // overlays.
206         impl.setEnabled(IDENTIFIER, true, USER);
207 
208         // Reorder the overlays
209         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 1 /* priority */);
210         configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
211         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
212         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
213         expect.that(o3).isNotNull();
214         assertThat(expect.hasFailures()).isFalse();
215         expect.that(o3.priority).isEqualTo(1);
216         expect.that(o3.isEnabled()).isTrue();
217 
218         final OverlayInfo o4 = impl.getOverlayInfo(IDENTIFIER2, USER);
219         expect.that(o4).isNotNull();
220         assertThat(expect.hasFailures()).isFalse();
221         expect.that(o4.priority).isEqualTo(0);
222         expect.that(o4.isEnabled()).isFalse();
223     }
224 
225     @Test
testImmutablePriorityChange()226     public void testImmutablePriorityChange() throws Exception {
227         final OverlayManagerServiceImpl impl = getImpl();
228         addPackage(target(TARGET), USER);
229         addPackage(overlay(OVERLAY, TARGET), USER);
230         addPackage(overlay(OVERLAY2, TARGET), USER);
231         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
232         configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);
233 
234         final Set<UserPackage> allPackages = Set.of(UserPackage.of(USER, TARGET));
235 
236         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
237         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
238         expect.that(o1).isNotNull();
239         assertThat(expect.hasFailures()).isFalse();
240         expect.that(o1.priority).isEqualTo(0);
241         expect.that(o1.isEnabled()).isTrue();
242 
243         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER2, USER);
244         expect.that(o2).isNotNull();
245         assertThat(expect.hasFailures()).isFalse();
246         expect.that(o2.priority).isEqualTo(1);
247         expect.that(o2.isEnabled()).isTrue();
248 
249         // Reorder the overlays
250         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);
251         configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
252         expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
253         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
254         expect.that(o3).isNotNull();
255         assertThat(expect.hasFailures()).isFalse();
256         expect.that(o3.priority).isEqualTo(1);
257         expect.that(o3.isEnabled()).isTrue();
258 
259         final OverlayInfo o4 = impl.getOverlayInfo(IDENTIFIER2, USER);
260         expect.that(o4).isNotNull();
261         assertThat(expect.hasFailures()).isFalse();
262         expect.that(o4.priority).isEqualTo(0);
263         expect.that(o4.isEnabled()).isTrue();
264     }
265 }
266