blob: 3ac42de911a99dbcd291c2fb50df2ece1b4a560d [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;
26import static org.mockito.Mockito.when;
phweisse375fc42017-04-19 20:15:06 +020027import static org.mockito.Mockito.doThrow;
28import static org.mockito.Mockito.doNothing;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090029import static org.mockito.Mockito.times;
30import static org.mockito.Mockito.verify;
Jason Monk3cfedd72016-12-09 09:31:37 -050031
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010032import android.app.admin.DevicePolicyManager;
phweisse375fc42017-04-19 20:15:06 +020033import android.content.ComponentName;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010034import android.content.Context;
phweisse375fc42017-04-19 20:15:06 +020035import android.content.Intent;
36import android.content.pm.StringParceledListSlice;
phweiss0dbf9592017-05-11 15:31:27 +020037import android.content.pm.UserInfo;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010038import android.net.ConnectivityManager;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090039import android.net.ConnectivityManager.NetworkCallback;
40import android.net.NetworkCapabilities;
41import android.net.NetworkRequest;
Jason Monk61936ee2018-12-21 12:41:34 -050042import android.os.Handler;
43import android.os.Looper;
phweiss0dbf9592017-05-11 15:31:27 +020044import android.os.UserManager;
phweisse375fc42017-04-19 20:15:06 +020045import android.security.IKeyChainService;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010046import android.support.test.runner.AndroidJUnit4;
47import android.test.suitebuilder.annotation.SmallTest;
48
phweisse375fc42017-04-19 20:15:06 +020049import com.android.systemui.statusbar.policy.SecurityController.SecurityControllerCallback;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010050import com.android.systemui.SysuiTestCase;
Jason Monk3cfedd72016-12-09 09:31:37 -050051
phweisse375fc42017-04-19 20:15:06 +020052import java.util.ArrayList;
53import java.util.Arrays;
54import java.util.concurrent.CountDownLatch;
55import java.util.concurrent.TimeUnit;
56import java.util.List;
57
Justin Klaassen6b476432017-05-08 07:11:46 -070058import org.junit.After;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010059import org.junit.Before;
Jason Monk7903c202017-05-08 13:35:23 -040060import org.junit.Ignore;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010061import org.junit.Test;
62import org.junit.runner.RunWith;
63
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010064
65@SmallTest
66@RunWith(AndroidJUnit4.class)
phweisse375fc42017-04-19 20:15:06 +020067public class SecurityControllerTest extends SysuiTestCase implements SecurityControllerCallback {
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010068 private final DevicePolicyManager mDevicePolicyManager = mock(DevicePolicyManager.class);
phweisse375fc42017-04-19 20:15:06 +020069 private final IKeyChainService.Stub mKeyChainService = mock(IKeyChainService.Stub.class);
phweiss0dbf9592017-05-11 15:31:27 +020070 private final UserManager mUserManager = mock(UserManager.class);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010071 private SecurityControllerImpl mSecurityController;
Justin Klaassen6b476432017-05-08 07:11:46 -070072 private CountDownLatch mStateChangedLatch;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090073 private ConnectivityManager mConnectivityManager = mock(ConnectivityManager.class);
phweisse375fc42017-04-19 20:15:06 +020074
75 // implementing SecurityControllerCallback
76 @Override
77 public void onStateChanged() {
Justin Klaassen6b476432017-05-08 07:11:46 -070078 mStateChangedLatch.countDown();
phweisse375fc42017-04-19 20:15:06 +020079 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010080
81 @Before
82 public void setUp() throws Exception {
Jason Monk3cfedd72016-12-09 09:31:37 -050083 mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
phweiss0dbf9592017-05-11 15:31:27 +020084 mContext.addMockSystemService(Context.USER_SERVICE, mUserManager);
Chalard Jean5b0c7c62018-03-09 20:52:15 +090085 mContext.addMockSystemService(Context.CONNECTIVITY_SERVICE, mConnectivityManager);
phweisse375fc42017-04-19 20:15:06 +020086
87 Intent intent = new Intent(IKeyChainService.class.getName());
88 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
89 mContext.addMockService(comp, mKeyChainService);
90
phweiss0dbf9592017-05-11 15:31:27 +020091 when(mUserManager.getUserInfo(anyInt())).thenReturn(new UserInfo());
92
phweisse375fc42017-04-19 20:15:06 +020093 when(mKeyChainService.getUserCaAliases())
94 .thenReturn(new StringParceledListSlice(new ArrayList<String>()));
95 // Without this line, mKeyChainService gets wrapped in a proxy when Stub.asInterface() is
96 // used on it, and the mocking above does not work.
97 when(mKeyChainService.queryLocalInterface("android.security.IKeyChainService"))
98 .thenReturn(mKeyChainService);
99
phweiss0dbf9592017-05-11 15:31:27 +0200100 // Wait for callbacks from 1) the CACertLoader and 2) the onUserSwitched() function in the
101 // constructor of mSecurityController
102 mStateChangedLatch = new CountDownLatch(2);
Jason Monk61936ee2018-12-21 12:41:34 -0500103 // TODO: Migrate this test to TestableLooper and use a handler attached
104 // to that.
105 mSecurityController = new SecurityControllerImpl(mContext,
106 new Handler(Looper.getMainLooper()), this);
Justin Klaassen6b476432017-05-08 07:11:46 -0700107 }
108
109 @After
110 public void tearDown() {
111 mSecurityController.removeCallback(this);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100112 }
113
114 @Test
115 public void testIsDeviceManaged() {
116 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
117 assertTrue(mSecurityController.isDeviceManaged());
118
119 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
120 assertFalse(mSecurityController.isDeviceManaged());
121 }
122
123 @Test
124 public void testGetDeviceOwnerOrganizationName() {
125 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn("organization");
126 assertEquals("organization", mSecurityController.getDeviceOwnerOrganizationName());
127 }
phweisse375fc42017-04-19 20:15:06 +0200128
129 @Test
phweiss0dbf9592017-05-11 15:31:27 +0200130 public void testWorkAccount() throws Exception {
131 // Wait for the callbacks from setUp()
132 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
133 assertFalse(mSecurityController.hasCACertInCurrentUser());
134
135 final int PRIMARY_USER_ID = 0;
136 final int MANAGED_USER_ID = 1;
137 List<UserInfo> profiles = Arrays.asList(new UserInfo(PRIMARY_USER_ID, "Primary",
138 UserInfo.FLAG_PRIMARY),
139 new UserInfo(MANAGED_USER_ID, "Working",
140 UserInfo.FLAG_MANAGED_PROFILE));
141 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
142 assertTrue(mSecurityController.hasWorkProfile());
143 assertFalse(mSecurityController.hasCACertInWorkProfile());
144
145 mStateChangedLatch = new CountDownLatch(1);
146
147 when(mKeyChainService.getUserCaAliases())
148 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
149
150 mSecurityController.new CACertLoader()
151 .execute(MANAGED_USER_ID);
152
Justin Klaassen6b476432017-05-08 07:11:46 -0700153 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweiss0dbf9592017-05-11 15:31:27 +0200154 assertTrue(mSecurityController.hasCACertInWorkProfile());
155 }
156
157 @Test
158 public void testCaCertLoader() throws Exception {
159 // Wait for the callbacks from setUp()
160 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200161 assertFalse(mSecurityController.hasCACertInCurrentUser());
162
163 // With a CA cert
Justin Klaassen6b476432017-05-08 07:11:46 -0700164 mStateChangedLatch = new CountDownLatch(1);
phweisse375fc42017-04-19 20:15:06 +0200165
166 when(mKeyChainService.getUserCaAliases())
167 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
168
169 mSecurityController.new CACertLoader()
170 .execute(0);
171
Justin Klaassen6b476432017-05-08 07:11:46 -0700172 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200173 assertTrue(mSecurityController.hasCACertInCurrentUser());
174
175 // Exception
176
Justin Klaassen6b476432017-05-08 07:11:46 -0700177 mStateChangedLatch = new CountDownLatch(1);
phweisse375fc42017-04-19 20:15:06 +0200178
179 when(mKeyChainService.getUserCaAliases())
180 .thenThrow(new AssertionError("Test AssertionError"))
181 .thenReturn(new StringParceledListSlice(new ArrayList<String>()));
182
183 mSecurityController.new CACertLoader()
184 .execute(0);
185
phweiss0dbf9592017-05-11 15:31:27 +0200186 assertFalse(mStateChangedLatch.await(1, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200187 assertTrue(mSecurityController.hasCACertInCurrentUser());
188 // The retry takes 30s
Justin Klaassen6b476432017-05-08 07:11:46 -0700189 //assertTrue(mStateChangedLatch.await(31, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200190 //assertFalse(mSecurityController.hasCACertInCurrentUser());
phweisse375fc42017-04-19 20:15:06 +0200191 }
Chalard Jean5b0c7c62018-03-09 20:52:15 +0900192
193 @Test
194 public void testNetworkRequest() {
195 verify(mConnectivityManager, times(1)).registerNetworkCallback(argThat(
196 (NetworkRequest request) -> request.networkCapabilities.getUids() == null
197 && request.networkCapabilities.getCapabilities().length == 0
198 ), any(NetworkCallback.class));
199 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100200}