blob: 0c9130d08d6dabf43f2a90d3bd5a77d63d45f69d [file] [log] [blame]
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +01001/*
2 * Copyright (C) 2016 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
17package com.android.systemui.statusbar.policy;
18
Jason Monk3cfedd72016-12-09 09:31:37 -050019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090022import static org.mockito.Matchers.any;
phweiss0dbf9592017-05-11 15:31:27 +020023import static org.mockito.Matchers.anyInt;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090024import static org.mockito.Matchers.argThat;
Jason Monk3cfedd72016-12-09 09:31:37 -050025import static org.mockito.Mockito.mock;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090026import static org.mockito.Mockito.times;
27import static org.mockito.Mockito.verify;
Brett Chabot84151d92019-02-27 15:37:59 -080028import static org.mockito.Mockito.when;
Jason Monk3cfedd72016-12-09 09:31:37 -050029
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010030import android.app.admin.DevicePolicyManager;
phweisse375fc42017-04-19 20:15:06 +020031import android.content.ComponentName;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010032import android.content.Context;
phweisse375fc42017-04-19 20:15:06 +020033import android.content.Intent;
34import android.content.pm.StringParceledListSlice;
phweiss0dbf9592017-05-11 15:31:27 +020035import android.content.pm.UserInfo;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010036import android.net.ConnectivityManager;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090037import android.net.ConnectivityManager.NetworkCallback;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090038import android.net.NetworkRequest;
Jason Monk61936ee2018-12-21 12:41:34 -050039import android.os.Handler;
40import android.os.Looper;
phweiss0dbf9592017-05-11 15:31:27 +020041import android.os.UserManager;
phweisse375fc42017-04-19 20:15:06 +020042import android.security.IKeyChainService;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010043import android.test.suitebuilder.annotation.SmallTest;
44
Brett Chabot84151d92019-02-27 15:37:59 -080045import androidx.test.runner.AndroidJUnit4;
Jason Monk3cfedd72016-12-09 09:31:37 -050046
Brett Chabot84151d92019-02-27 15:37:59 -080047import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000048import com.android.systemui.broadcast.BroadcastDispatcher;
Brett Chabot84151d92019-02-27 15:37:59 -080049import com.android.systemui.statusbar.policy.SecurityController.SecurityControllerCallback;
phweisse375fc42017-04-19 20:15:06 +020050
Justin Klaassen6b476432017-05-08 07:11:46 -070051import org.junit.After;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010052import org.junit.Before;
53import org.junit.Test;
54import org.junit.runner.RunWith;
55
Brett Chabot84151d92019-02-27 15:37:59 -080056import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.List;
59import java.util.concurrent.CountDownLatch;
60import java.util.concurrent.TimeUnit;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010061
62@SmallTest
63@RunWith(AndroidJUnit4.class)
phweisse375fc42017-04-19 20:15:06 +020064public class SecurityControllerTest extends SysuiTestCase implements SecurityControllerCallback {
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010065 private final DevicePolicyManager mDevicePolicyManager = mock(DevicePolicyManager.class);
phweisse375fc42017-04-19 20:15:06 +020066 private final IKeyChainService.Stub mKeyChainService = mock(IKeyChainService.Stub.class);
phweiss0dbf9592017-05-11 15:31:27 +020067 private final UserManager mUserManager = mock(UserManager.class);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010068 private SecurityControllerImpl mSecurityController;
Justin Klaassen6b476432017-05-08 07:11:46 -070069 private CountDownLatch mStateChangedLatch;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090070 private ConnectivityManager mConnectivityManager = mock(ConnectivityManager.class);
phweisse375fc42017-04-19 20:15:06 +020071
72 // implementing SecurityControllerCallback
73 @Override
74 public void onStateChanged() {
Justin Klaassen6b476432017-05-08 07:11:46 -070075 mStateChangedLatch.countDown();
phweisse375fc42017-04-19 20:15:06 +020076 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010077
78 @Before
79 public void setUp() throws Exception {
Jason Monk3cfedd72016-12-09 09:31:37 -050080 mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
phweiss0dbf9592017-05-11 15:31:27 +020081 mContext.addMockSystemService(Context.USER_SERVICE, mUserManager);
Chalard Jean5b0c7c62018-03-09 20:52:15 +090082 mContext.addMockSystemService(Context.CONNECTIVITY_SERVICE, mConnectivityManager);
phweisse375fc42017-04-19 20:15:06 +020083
84 Intent intent = new Intent(IKeyChainService.class.getName());
85 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
86 mContext.addMockService(comp, mKeyChainService);
87
phweiss0dbf9592017-05-11 15:31:27 +020088 when(mUserManager.getUserInfo(anyInt())).thenReturn(new UserInfo());
89
phweisse375fc42017-04-19 20:15:06 +020090 when(mKeyChainService.getUserCaAliases())
91 .thenReturn(new StringParceledListSlice(new ArrayList<String>()));
92 // Without this line, mKeyChainService gets wrapped in a proxy when Stub.asInterface() is
93 // used on it, and the mocking above does not work.
94 when(mKeyChainService.queryLocalInterface("android.security.IKeyChainService"))
95 .thenReturn(mKeyChainService);
96
phweiss1a50c522019-10-21 19:41:09 +020097 // Wait for callbacks from the onUserSwitched() function in the
phweiss0dbf9592017-05-11 15:31:27 +020098 // constructor of mSecurityController
phweiss1a50c522019-10-21 19:41:09 +020099 mStateChangedLatch = new CountDownLatch(1);
Jason Monk61936ee2018-12-21 12:41:34 -0500100 // TODO: Migrate this test to TestableLooper and use a handler attached
101 // to that.
102 mSecurityController = new SecurityControllerImpl(mContext,
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000103 new Handler(Looper.getMainLooper()), mock(BroadcastDispatcher.class), this);
Justin Klaassen6b476432017-05-08 07:11:46 -0700104 }
105
106 @After
107 public void tearDown() {
108 mSecurityController.removeCallback(this);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100109 }
110
111 @Test
112 public void testIsDeviceManaged() {
113 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
114 assertTrue(mSecurityController.isDeviceManaged());
115
116 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
117 assertFalse(mSecurityController.isDeviceManaged());
118 }
119
120 @Test
121 public void testGetDeviceOwnerOrganizationName() {
122 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn("organization");
123 assertEquals("organization", mSecurityController.getDeviceOwnerOrganizationName());
124 }
phweisse375fc42017-04-19 20:15:06 +0200125
126 @Test
phweiss0dbf9592017-05-11 15:31:27 +0200127 public void testWorkAccount() throws Exception {
128 // Wait for the callbacks from setUp()
129 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
130 assertFalse(mSecurityController.hasCACertInCurrentUser());
131
132 final int PRIMARY_USER_ID = 0;
133 final int MANAGED_USER_ID = 1;
134 List<UserInfo> profiles = Arrays.asList(new UserInfo(PRIMARY_USER_ID, "Primary",
135 UserInfo.FLAG_PRIMARY),
136 new UserInfo(MANAGED_USER_ID, "Working",
137 UserInfo.FLAG_MANAGED_PROFILE));
138 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
139 assertTrue(mSecurityController.hasWorkProfile());
140 assertFalse(mSecurityController.hasCACertInWorkProfile());
141
142 mStateChangedLatch = new CountDownLatch(1);
143
144 when(mKeyChainService.getUserCaAliases())
145 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
146
147 mSecurityController.new CACertLoader()
148 .execute(MANAGED_USER_ID);
149
Justin Klaassen6b476432017-05-08 07:11:46 -0700150 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweiss0dbf9592017-05-11 15:31:27 +0200151 assertTrue(mSecurityController.hasCACertInWorkProfile());
152 }
153
154 @Test
155 public void testCaCertLoader() throws Exception {
156 // Wait for the callbacks from setUp()
157 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200158 assertFalse(mSecurityController.hasCACertInCurrentUser());
159
160 // With a CA cert
Justin Klaassen6b476432017-05-08 07:11:46 -0700161 mStateChangedLatch = new CountDownLatch(1);
phweisse375fc42017-04-19 20:15:06 +0200162
163 when(mKeyChainService.getUserCaAliases())
164 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
165
166 mSecurityController.new CACertLoader()
167 .execute(0);
168
Justin Klaassen6b476432017-05-08 07:11:46 -0700169 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200170 assertTrue(mSecurityController.hasCACertInCurrentUser());
171
172 // Exception
Justin Klaassen6b476432017-05-08 07:11:46 -0700173 mStateChangedLatch = new CountDownLatch(1);
phweisse375fc42017-04-19 20:15:06 +0200174
175 when(mKeyChainService.getUserCaAliases())
176 .thenThrow(new AssertionError("Test AssertionError"))
177 .thenReturn(new StringParceledListSlice(new ArrayList<String>()));
178
179 mSecurityController.new CACertLoader()
180 .execute(0);
181
phweiss0dbf9592017-05-11 15:31:27 +0200182 assertFalse(mStateChangedLatch.await(1, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200183 assertTrue(mSecurityController.hasCACertInCurrentUser());
phweiss1a50c522019-10-21 19:41:09 +0200184
185 mSecurityController.new CACertLoader()
186 .execute(0);
187
188 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
189 assertFalse(mSecurityController.hasCACertInCurrentUser());
phweisse375fc42017-04-19 20:15:06 +0200190 }
Chalard Jean5b0c7c62018-03-09 20:52:15 +0900191
192 @Test
193 public void testNetworkRequest() {
194 verify(mConnectivityManager, times(1)).registerNetworkCallback(argThat(
195 (NetworkRequest request) -> request.networkCapabilities.getUids() == null
196 && request.networkCapabilities.getCapabilities().length == 0
197 ), any(NetworkCallback.class));
198 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100199}