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 package com.android.server.devicepolicy;
17 
18 import static android.os.UserHandle.USER_NULL;
19 
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import static org.testng.Assert.expectThrows;
23 
24 import android.content.ComponentName;
25 
26 import org.junit.Test;
27 
28 /**
29  * Run it as {@code atest FrameworksMockingServicesTests:OwnerShellDataTest}
30  */
31 public final class OwnerShellDataTest {
32 
33     private static final int USER_ID = 007;
34     private static final int PARENT_USER_ID = 'M' + 'I' + 6;
35     private static final ComponentName ADMIN = new ComponentName("Bond", "James");
36 
37     @Test
testForDeviceOwner_noAdmin()38     public void testForDeviceOwner_noAdmin() {
39         expectThrows(NullPointerException.class,
40                 () -> OwnerShellData.forDeviceOwner(USER_ID, /* admin= */ null));
41     }
42 
43     @Test
testForDeviceOwner_invalidUser()44     public void testForDeviceOwner_invalidUser() {
45         expectThrows(IllegalArgumentException.class,
46                 () -> OwnerShellData.forDeviceOwner(USER_NULL, ADMIN));
47     }
48 
49     @Test
testForDeviceOwner()50     public void testForDeviceOwner() {
51         OwnerShellData dto = OwnerShellData.forDeviceOwner(USER_ID, ADMIN);
52 
53         assertWithMessage("dto(%s).userId", dto).that(dto.userId).isEqualTo(USER_ID);
54         assertWithMessage("dto(%s).parentUserId", dto).that(dto.parentUserId)
55                 .isEqualTo(USER_NULL);
56         assertWithMessage("dto(%s).admin", dto).that(dto.admin).isSameInstanceAs(ADMIN);
57         assertWithMessage("dto(%s).isDeviceOwner", dto).that(dto.isDeviceOwner).isTrue();
58         assertWithMessage("dto(%s).isProfileOwner", dto).that(dto.isProfileOwner).isFalse();
59         assertWithMessage("dto(%s).isManagedProfileOwner", dto).that(dto.isManagedProfileOwner)
60                 .isFalse();
61         assertWithMessage("dto(%s).isAffiliated", dto).that(dto.isAffiliated).isFalse();
62     }
63 
64     @Test
testForUserProfileOwner_noAdmin()65     public void testForUserProfileOwner_noAdmin() {
66         expectThrows(NullPointerException.class,
67                 () -> OwnerShellData.forUserProfileOwner(USER_ID, /* admin= */ null));
68     }
69 
70     @Test
testForUserProfileOwner_invalidUser()71     public void testForUserProfileOwner_invalidUser() {
72         expectThrows(IllegalArgumentException.class,
73                 () -> OwnerShellData.forUserProfileOwner(USER_NULL, ADMIN));
74     }
75 
76     @Test
testForUserProfileOwner()77     public void testForUserProfileOwner() {
78         OwnerShellData dto = OwnerShellData.forUserProfileOwner(USER_ID, ADMIN);
79 
80         assertWithMessage("dto(%s).userId", dto).that(dto.userId).isEqualTo(USER_ID);
81         assertWithMessage("dto(%s).parentUserId", dto).that(dto.parentUserId)
82                 .isEqualTo(USER_NULL);
83         assertWithMessage("dto(%s).admin", dto).that(dto.admin).isSameInstanceAs(ADMIN);
84         assertWithMessage("dto(%s).isDeviceOwner", dto).that(dto.isDeviceOwner).isFalse();
85         assertWithMessage("dto(%s).isProfileOwner", dto).that(dto.isProfileOwner).isTrue();
86         assertWithMessage("dto(%s).isManagedProfileOwner", dto).that(dto.isManagedProfileOwner)
87                 .isFalse();
88         assertWithMessage("dto(%s).isAffiliated", dto).that(dto.isAffiliated).isFalse();
89     }
90 
91     @Test
testForManagedProfileOwner_noAdmin()92     public void testForManagedProfileOwner_noAdmin() {
93         expectThrows(NullPointerException.class,
94                 () -> OwnerShellData.forManagedProfileOwner(USER_ID, PARENT_USER_ID, null));
95     }
96 
97     @Test
testForManagedProfileOwner_invalidUser()98     public void testForManagedProfileOwner_invalidUser() {
99         expectThrows(IllegalArgumentException.class,
100                 () -> OwnerShellData.forManagedProfileOwner(USER_NULL, PARENT_USER_ID, ADMIN));
101     }
102 
103     @Test
testForManagedProfileOwner_invalidParent()104     public void testForManagedProfileOwner_invalidParent() {
105         expectThrows(IllegalArgumentException.class,
106                 () -> OwnerShellData.forManagedProfileOwner(USER_ID, USER_NULL, ADMIN));
107     }
108 
109     @Test
testForManagedProfileOwner_parentOfItself()110     public void testForManagedProfileOwner_parentOfItself() {
111         expectThrows(IllegalArgumentException.class,
112                 () -> OwnerShellData.forManagedProfileOwner(USER_ID, USER_ID, ADMIN));
113     }
114 
115     @Test
testForManagedProfileOwner()116     public void testForManagedProfileOwner() {
117         OwnerShellData dto = OwnerShellData.forManagedProfileOwner(USER_ID, PARENT_USER_ID, ADMIN);
118 
119         assertWithMessage("dto(%s).userId", dto).that(dto.userId).isEqualTo(USER_ID);
120         assertWithMessage("dto(%s).parentUserId", dto).that(dto.parentUserId)
121                 .isEqualTo(PARENT_USER_ID);
122         assertWithMessage("dto(%s).admin", dto).that(dto.admin).isSameInstanceAs(ADMIN);
123         assertWithMessage("dto(%s).isDeviceOwner", dto).that(dto.isDeviceOwner).isFalse();
124         assertWithMessage("dto(%s).isProfileOwner", dto).that(dto.isProfileOwner).isFalse();
125         assertWithMessage("dto(%s).isManagedProfileOwner", dto).that(dto.isManagedProfileOwner)
126                 .isTrue();
127         assertWithMessage("dto(%s).isAffiliated", dto).that(dto.isAffiliated).isFalse();
128     }
129 }
130