1 /*
2  * Copyright (C) 2014 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.devicepolicy;
18 
19 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
20 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
21 import static android.app.admin.SystemUpdatePolicy.TYPE_INSTALL_WINDOWED;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 import static com.google.common.truth.Truth.assertWithMessage;
25 
26 import android.content.ComponentName;
27 import android.os.IpcDataCache;
28 import android.test.suitebuilder.annotation.SmallTest;
29 
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  * Tests for the DeviceOwner object that saves & loads device and policy owner information.
38  *
39  * <p>Run this test with:
40  *
41  * {@code atest FrameworksServicesTests:com.android.server.devicepolicy.OwnersTest}
42  */
43 @SmallTest
44 @RunWith(AndroidJUnit4.class)
45 public class OwnersTest extends DpmTestBase {
46 
47     private static final int TEST_PO_USER = 10;
48     private static final String TESTDPC_PACKAGE = "com.afwsamples.testdpc";
49     private final DeviceStateCacheImpl mDeviceStateCache = new DeviceStateCacheImpl();
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         // Disable caches in this test process. This must happen early, since some of the
54         // following initialization steps invalidate caches.
55         IpcDataCache.disableForTestMode();
56     }
57 
58     @Test
loadProfileOwner()59     public void loadProfileOwner() throws Exception {
60         getServices().addUsers(TEST_PO_USER);
61 
62         final Owners owners = makeOwners();
63 
64         DpmTestUtils.writeToFile(owners.getProfileOwnerFile(TEST_PO_USER),
65                 DpmTestUtils.readAsset(mRealTestContext, "OwnersTest/profile_owner_1.xml"));
66 
67         owners.load();
68 
69         assertThat(owners.hasDeviceOwner()).isFalse();
70         assertThat(owners.getSystemUpdatePolicy()).isNull();
71 
72         assertThat(owners.getProfileOwnerKeys()).hasSize(1);
73         assertThat(owners.getProfileOwnerComponent(10))
74                 .isEqualTo(new ComponentName(TESTDPC_PACKAGE,
75                         "com.afwsamples.testdpc.DeviceAdminReceiver"));
76 
77         assertWithMessage("Profile owner data in DeviceStateCache wasn't populated")
78                 .that(mDeviceStateCache.isUserOrganizationManaged(TEST_PO_USER)).isTrue();
79     }
80 
81     @Test
loadDeviceOwner()82     public void loadDeviceOwner() throws Exception {
83         final Owners owners = makeOwners();
84 
85         DpmTestUtils.writeToFile(owners.getDeviceOwnerFile(),
86                 DpmTestUtils.readAsset(mRealTestContext, "OwnersTest/device_owner_1.xml"));
87 
88         owners.load();
89 
90         assertThat(owners.hasDeviceOwner()).isTrue();
91 
92         assertThat(owners.getProfileOwnerKeys()).hasSize(0);
93         assertThat(owners.getDeviceOwnerComponent())
94                 .isEqualTo(new ComponentName(TESTDPC_PACKAGE,
95                         "com.afwsamples.testdpc.DeviceAdminReceiver"));
96 
97         assertThat(owners.getSystemUpdatePolicy().getPolicyType()).isEqualTo(TYPE_INSTALL_WINDOWED);
98 
99         assertWithMessage("Device owner data in DeviceStateCache wasn't populated")
100                 .that(mDeviceStateCache.isUserOrganizationManaged(owners.getDeviceOwnerUserId()))
101                 .isTrue();
102     }
103 
104     @Test
testDeviceOwnerType()105     public void testDeviceOwnerType() throws Exception {
106         final Owners owners = makeOwners();
107 
108         DpmTestUtils.writeToFile(owners.getDeviceOwnerFile(),
109                 DpmTestUtils.readAsset(mRealTestContext, "OwnersTest/device_owner_1.xml"));
110 
111         owners.load();
112 
113         assertThat(owners.getDeviceOwnerType(TESTDPC_PACKAGE))
114                 .isEqualTo(DEVICE_OWNER_TYPE_DEFAULT);
115 
116         // Should be able to set DO type to "financed".
117         owners.setDeviceOwnerType(
118                 TESTDPC_PACKAGE, DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false);
119         assertThat(owners.getDeviceOwnerType(TESTDPC_PACKAGE))
120                 .isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
121 
122         // Once set, DO type cannot be changed.
123         owners.setDeviceOwnerType(
124                 TESTDPC_PACKAGE, DEVICE_OWNER_TYPE_DEFAULT, /* isAdminTestOnly= */ false);
125         assertThat(owners.getDeviceOwnerType(TESTDPC_PACKAGE))
126                 .isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
127     }
128 
makeOwners()129     private Owners makeOwners() {
130         final MockSystemServices services = getServices();
131         return new Owners(services.userManager, services.userManagerInternal,
132                 services.packageManagerInternal, services.activityTaskManagerInternal,
133                 services.activityManagerInternal, mDeviceStateCache, services.pathProvider);
134     }
135 }
136