1 /*
2  * Copyright (C) 2021 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.bedstead.nene.devicepolicy;
18 
19 import static android.os.Build.VERSION_CODES.TIRAMISU;
20 
21 import static com.android.bedstead.enterprise.EnterpriseDeviceStateExtensionsKt.profileOwner;
22 import static com.android.bedstead.enterprise.EnterpriseDeviceStateExtensionsKt.workProfile;
23 import static com.android.bedstead.harrier.UserType.SECONDARY_USER;
24 import static com.android.bedstead.multiuser.MultiUserDeviceStateExtensionsKt.secondaryUser;
25 import static com.android.bedstead.testapps.TestAppsDeviceStateExtensionsKt.testApps;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import android.content.ComponentName;
30 
31 import com.android.bedstead.harrier.BedsteadJUnit4;
32 import com.android.bedstead.harrier.DeviceState;
33 import com.android.bedstead.multiuser.annotations.EnsureHasSecondaryUser;
34 import com.android.bedstead.multiuser.annotations.RequireRunNotOnSecondaryUser;
35 import com.android.bedstead.harrier.annotations.RequireRunOnInitialUser;
36 import com.android.bedstead.enterprise.annotations.RequireRunOnWorkProfile;
37 import com.android.bedstead.harrier.annotations.RequireSdkVersion;
38 import com.android.bedstead.enterprise.annotations.EnsureHasNoDpc;
39 import com.android.bedstead.enterprise.annotations.EnsureHasProfileOwner;
40 import com.android.bedstead.nene.TestApis;
41 import com.android.bedstead.nene.users.UserReference;
42 import com.android.bedstead.remotedpc.RemoteDpc;
43 import com.android.bedstead.testapp.TestApp;
44 import com.android.bedstead.testapp.TestAppInstance;
45 
46 import org.junit.Before;
47 import org.junit.ClassRule;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 
52 @RunWith(BedsteadJUnit4.class)
53 public class ProfileOwnerTest {
54 
55     @ClassRule
56     @Rule
57     public static final DeviceState sDeviceState = new DeviceState();
58 
59     private static final ComponentName DPC_COMPONENT_NAME = new ComponentName(
60             RemoteDpc.REMOTE_DPC_APP_PACKAGE_NAME_OR_PREFIX,
61             "com.android.bedstead.testapp.BaseTestAppDeviceAdminReceiver"
62     );
63     private static final TestApp sNonTestOnlyDpc = testApps(sDeviceState).query()
64             .whereIsDeviceAdmin().isTrue()
65             .whereTestOnly().isFalse()
66             .get();
67     private static final ComponentName NON_TEST_ONLY_DPC_COMPONENT_NAME = new ComponentName(
68             sNonTestOnlyDpc.packageName(),
69             "com.android.bedstead.testapp.DeviceAdminTestApp.DeviceAdminReceiver"
70     );
71 
72     private static UserReference sProfile;
73 
74     @Before
setUp()75     public void setUp() {
76         sProfile = TestApis.users().instrumented();
77     }
78 
79     @Test
80     @EnsureHasProfileOwner
user_returnsUser()81     public void user_returnsUser() {
82         assertThat(profileOwner(sDeviceState).devicePolicyController().user()).isEqualTo(sProfile);
83     }
84 
85     @Test
86     @EnsureHasProfileOwner
pkg_returnsPackage()87     public void pkg_returnsPackage() {
88         assertThat(profileOwner(sDeviceState).devicePolicyController().pkg()).isNotNull();
89     }
90 
91     @Test
92     @EnsureHasProfileOwner
componentName_returnsComponentName()93     public void componentName_returnsComponentName() {
94         assertThat(profileOwner(sDeviceState).devicePolicyController().componentName())
95                 .isEqualTo(DPC_COMPONENT_NAME);
96     }
97 
98     @Test
99     @EnsureHasProfileOwner
remove_removesProfileOwner()100     public void remove_removesProfileOwner() {
101         profileOwner(sDeviceState).devicePolicyController().remove();
102         try {
103             assertThat(TestApis.devicePolicy().getProfileOwner(sProfile)).isNull();
104         } finally {
105             TestApis.devicePolicy().setProfileOwner(sProfile, DPC_COMPONENT_NAME);
106         }
107     }
108 
109     @Test
110     @EnsureHasNoDpc
remove_nonTestOnlyDpc_removesProfileOwner()111     public void remove_nonTestOnlyDpc_removesProfileOwner() {
112         try (TestAppInstance dpc = sNonTestOnlyDpc.install()) {
113             ProfileOwner profileOwner = TestApis.devicePolicy().setProfileOwner(
114                     TestApis.users().instrumented(), NON_TEST_ONLY_DPC_COMPONENT_NAME);
115 
116             profileOwner.remove();
117 
118             assertThat(TestApis.devicePolicy().getProfileOwner()).isNull();
119         }
120     }
121 
122     @Test
123     @EnsureHasNoDpc
124     @RequireRunOnInitialUser
setAndRemoveProfileOwnerRepeatedly_doesNotThrowError()125     public void setAndRemoveProfileOwnerRepeatedly_doesNotThrowError() {
126         try (UserReference profile = TestApis.users().createUser().createAndStart()) {
127             try (TestAppInstance dpc = sNonTestOnlyDpc.install()) {
128                 for (int i = 0; i < 100; i++) {
129                     ProfileOwner profileOwner = TestApis.devicePolicy().setProfileOwner(
130                             TestApis.users().instrumented(), NON_TEST_ONLY_DPC_COMPONENT_NAME);
131                     profileOwner.remove();
132                 }
133             }
134         }
135     }
136 
137     @Test
138     @EnsureHasSecondaryUser
139     @RequireRunNotOnSecondaryUser
140     @EnsureHasProfileOwner(onUser = SECONDARY_USER)
remove_onOtherUser_removesProfileOwner()141     public void remove_onOtherUser_removesProfileOwner() {
142             TestApis.devicePolicy().getProfileOwner(secondaryUser(sDeviceState)).remove();
143 
144             assertThat(TestApis.devicePolicy().getProfileOwner(secondaryUser(sDeviceState)))
145                     .isNull();
146     }
147 
148     @Test
149     @RequireRunOnWorkProfile
remove_onWorkProfile_testDpc_removesProfileOwner()150     public void remove_onWorkProfile_testDpc_removesProfileOwner() {
151         TestApis.devicePolicy().getProfileOwner().remove();
152 
153         assertThat(TestApis.devicePolicy().getProfileOwner()).isNull();
154     }
155 
156     @Test
157     @RequireSdkVersion(min = TIRAMISU)
158     @RequireRunOnWorkProfile
setIsOrganizationOwned_becomesOrganizationOwned()159     public void setIsOrganizationOwned_becomesOrganizationOwned() {
160         ProfileOwner profileOwner = (ProfileOwner) profileOwner(sDeviceState,
161                 workProfile(sDeviceState)).devicePolicyController();
162 
163         profileOwner.setIsOrganizationOwned(true);
164 
165         assertThat(profileOwner.isOrganizationOwned()).isTrue();
166     }
167 
168     @Test
169     @RequireSdkVersion(min = TIRAMISU)
170     @RequireRunOnWorkProfile
unsetIsOrganizationOwned_becomesNotOrganizationOwned()171     public void unsetIsOrganizationOwned_becomesNotOrganizationOwned() {
172         ProfileOwner profileOwner = (ProfileOwner) profileOwner(sDeviceState,
173                 workProfile(sDeviceState)).devicePolicyController();
174         profileOwner.setIsOrganizationOwned(true);
175 
176         profileOwner.setIsOrganizationOwned(false);
177 
178         assertThat(profileOwner.isOrganizationOwned()).isFalse();
179     }
180 }
181