blob: 854cc2fad8e5d4f83f0df995ca75fbd953aae2e9 [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;
48import com.android.systemui.statusbar.policy.SecurityController.SecurityControllerCallback;
phweisse375fc42017-04-19 20:15:06 +020049
Justin Klaassen6b476432017-05-08 07:11:46 -070050import org.junit.After;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010051import org.junit.Before;
52import org.junit.Test;
53import org.junit.runner.RunWith;
54
Brett Chabot84151d92019-02-27 15:37:59 -080055import java.util.ArrayList;
56import java.util.Arrays;
57import java.util.List;
58import java.util.concurrent.CountDownLatch;
59import java.util.concurrent.TimeUnit;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010060
61@SmallTest
62@RunWith(AndroidJUnit4.class)
phweisse375fc42017-04-19 20:15:06 +020063public class SecurityControllerTest extends SysuiTestCase implements SecurityControllerCallback {
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010064 private final DevicePolicyManager mDevicePolicyManager = mock(DevicePolicyManager.class);
phweisse375fc42017-04-19 20:15:06 +020065 private final IKeyChainService.Stub mKeyChainService = mock(IKeyChainService.Stub.class);
phweiss0dbf9592017-05-11 15:31:27 +020066 private final UserManager mUserManager = mock(UserManager.class);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010067 private SecurityControllerImpl mSecurityController;
Justin Klaassen6b476432017-05-08 07:11:46 -070068 private CountDownLatch mStateChangedLatch;
Chalard Jean5b0c7c62018-03-09 20:52:15 +090069 private ConnectivityManager mConnectivityManager = mock(ConnectivityManager.class);
phweisse375fc42017-04-19 20:15:06 +020070
71 // implementing SecurityControllerCallback
72 @Override
73 public void onStateChanged() {
Justin Klaassen6b476432017-05-08 07:11:46 -070074 mStateChangedLatch.countDown();
phweisse375fc42017-04-19 20:15:06 +020075 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010076
77 @Before
78 public void setUp() throws Exception {
Jason Monk3cfedd72016-12-09 09:31:37 -050079 mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
phweiss0dbf9592017-05-11 15:31:27 +020080 mContext.addMockSystemService(Context.USER_SERVICE, mUserManager);
Chalard Jean5b0c7c62018-03-09 20:52:15 +090081 mContext.addMockSystemService(Context.CONNECTIVITY_SERVICE, mConnectivityManager);
phweisse375fc42017-04-19 20:15:06 +020082
83 Intent intent = new Intent(IKeyChainService.class.getName());
84 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
85 mContext.addMockService(comp, mKeyChainService);
86
phweiss0dbf9592017-05-11 15:31:27 +020087 when(mUserManager.getUserInfo(anyInt())).thenReturn(new UserInfo());
88
phweisse375fc42017-04-19 20:15:06 +020089 when(mKeyChainService.getUserCaAliases())
90 .thenReturn(new StringParceledListSlice(new ArrayList<String>()));
91 // Without this line, mKeyChainService gets wrapped in a proxy when Stub.asInterface() is
92 // used on it, and the mocking above does not work.
93 when(mKeyChainService.queryLocalInterface("android.security.IKeyChainService"))
94 .thenReturn(mKeyChainService);
95
phweiss0dbf9592017-05-11 15:31:27 +020096 // Wait for callbacks from 1) the CACertLoader and 2) the onUserSwitched() function in the
97 // constructor of mSecurityController
98 mStateChangedLatch = new CountDownLatch(2);
Jason Monk61936ee2018-12-21 12:41:34 -050099 // TODO: Migrate this test to TestableLooper and use a handler attached
100 // to that.
101 mSecurityController = new SecurityControllerImpl(mContext,
102 new Handler(Looper.getMainLooper()), this);
Justin Klaassen6b476432017-05-08 07:11:46 -0700103 }
104
105 @After
106 public void tearDown() {
107 mSecurityController.removeCallback(this);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100108 }
109
110 @Test
111 public void testIsDeviceManaged() {
112 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
113 assertTrue(mSecurityController.isDeviceManaged());
114
115 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
116 assertFalse(mSecurityController.isDeviceManaged());
117 }
118
119 @Test
120 public void testGetDeviceOwnerOrganizationName() {
121 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn("organization");
122 assertEquals("organization", mSecurityController.getDeviceOwnerOrganizationName());
123 }
phweisse375fc42017-04-19 20:15:06 +0200124
125 @Test
phweiss0dbf9592017-05-11 15:31:27 +0200126 public void testWorkAccount() throws Exception {
127 // Wait for the callbacks from setUp()
128 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
129 assertFalse(mSecurityController.hasCACertInCurrentUser());
130
131 final int PRIMARY_USER_ID = 0;
132 final int MANAGED_USER_ID = 1;
133 List<UserInfo> profiles = Arrays.asList(new UserInfo(PRIMARY_USER_ID, "Primary",
134 UserInfo.FLAG_PRIMARY),
135 new UserInfo(MANAGED_USER_ID, "Working",
136 UserInfo.FLAG_MANAGED_PROFILE));
137 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
138 assertTrue(mSecurityController.hasWorkProfile());
139 assertFalse(mSecurityController.hasCACertInWorkProfile());
140
141 mStateChangedLatch = new CountDownLatch(1);
142
143 when(mKeyChainService.getUserCaAliases())
144 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
145
146 mSecurityController.new CACertLoader()
147 .execute(MANAGED_USER_ID);
148
Justin Klaassen6b476432017-05-08 07:11:46 -0700149 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweiss0dbf9592017-05-11 15:31:27 +0200150 assertTrue(mSecurityController.hasCACertInWorkProfile());
151 }
152
153 @Test
154 public void testCaCertLoader() throws Exception {
155 // Wait for the callbacks from setUp()
156 assertTrue(mStateChangedLatch.await(1, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200157 assertFalse(mSecurityController.hasCACertInCurrentUser());
158
159 // With a CA cert
Justin Klaassen6b476432017-05-08 07:11:46 -0700160 mStateChangedLatch = new CountDownLatch(1);
phweisse375fc42017-04-19 20:15:06 +0200161
162 when(mKeyChainService.getUserCaAliases())
163 .thenReturn(new StringParceledListSlice(Arrays.asList("One CA Alias")));
164
165 mSecurityController.new CACertLoader()
166 .execute(0);
167
Justin Klaassen6b476432017-05-08 07:11:46 -0700168 assertTrue(mStateChangedLatch.await(3, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200169 assertTrue(mSecurityController.hasCACertInCurrentUser());
170
171 // Exception
172
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());
184 // The retry takes 30s
Justin Klaassen6b476432017-05-08 07:11:46 -0700185 //assertTrue(mStateChangedLatch.await(31, TimeUnit.SECONDS));
phweisse375fc42017-04-19 20:15:06 +0200186 //assertFalse(mSecurityController.hasCACertInCurrentUser());
phweisse375fc42017-04-19 20:15:06 +0200187 }
Chalard Jean5b0c7c62018-03-09 20:52:15 +0900188
189 @Test
190 public void testNetworkRequest() {
191 verify(mConnectivityManager, times(1)).registerNetworkCallback(argThat(
192 (NetworkRequest request) -> request.networkCapabilities.getUids() == null
193 && request.networkCapabilities.getCapabilities().length == 0
194 ), any(NetworkCallback.class));
195 }
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100196}