• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.reset;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.admin.DevicePolicyManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.PackageManager;
29 import android.content.pm.UserInfo;
30 import android.content.res.Resources;
31 import android.net.ConnectivityManager;
32 import android.net.ProxyInfo;
33 import android.os.UserHandle;
34 import android.os.UserManager;
35 import android.provider.Settings;
36 import android.text.SpannableStringBuilder;
37 
38 import com.android.settings.R;
39 import com.android.settings.testutils.SettingsRobolectricTestRunner;
40 import com.android.settingslib.wrapper.PackageManagerWrapper;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RuntimeEnvironment;
48 
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Date;
52 import java.util.List;
53 
54 @RunWith(SettingsRobolectricTestRunner.class)
55 public class EnterprisePrivacyFeatureProviderImplTest {
56 
57     private final ComponentName OWNER = new ComponentName("dummy", "component");
58     private final ComponentName ADMIN_1 = new ComponentName("dummy", "admin1");
59     private final ComponentName ADMIN_2 = new ComponentName("dummy", "admin2");
60     private final String OWNER_ORGANIZATION = new String("ACME");
61     private final Date TIMESTAMP = new Date(2011, 11, 11);
62     private final int MY_USER_ID = UserHandle.myUserId();
63     private final int MANAGED_PROFILE_USER_ID = MY_USER_ID + 1;
64     private final String VPN_PACKAGE_ID = "com.example.vpn";
65     private final String IME_PACKAGE_ID = "com.example.ime";
66     private final String IME_PACKAGE_LABEL = "Test IME";
67 
68     private List<UserInfo> mProfiles = new ArrayList<>();
69 
70     private @Mock Context mContext;
71     private @Mock DevicePolicyManager mDevicePolicyManager;
72     private @Mock PackageManagerWrapper mPackageManagerWrapper;
73     private @Mock PackageManager mPackageManager;
74     private @Mock UserManager mUserManager;
75     private @Mock ConnectivityManager mConnectivityManger;
76     private Resources mResources;
77 
78     private EnterprisePrivacyFeatureProvider mProvider;
79 
80     @Before
setUp()81     public void setUp() {
82         MockitoAnnotations.initMocks(this);
83 
84         when(mContext.getApplicationContext()).thenReturn(mContext);
85         resetAndInitializePackageManagerWrapper();
86         when(mUserManager.getProfiles(MY_USER_ID)).thenReturn(mProfiles);
87         mProfiles.add(new UserInfo(MY_USER_ID, "", "", 0 /* flags */));
88         mResources = RuntimeEnvironment.application.getResources();
89 
90         mProvider = new EnterprisePrivacyFeatureProviderImpl(mContext, mDevicePolicyManager,
91                 mPackageManagerWrapper, mUserManager, mConnectivityManger, mResources);
92     }
93 
94     @Test
testHasDeviceOwner()95     public void testHasDeviceOwner() {
96         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null);
97         assertThat(mProvider.hasDeviceOwner()).isFalse();
98 
99         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
100         assertThat(mProvider.hasDeviceOwner()).isTrue();
101     }
102 
103     @Test
testIsInCompMode()104     public void testIsInCompMode() {
105         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
106         assertThat(mProvider.isInCompMode()).isFalse();
107 
108         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
109         assertThat(mProvider.isInCompMode()).isTrue();
110     }
111 
112     @Test
testGetDeviceOwnerOrganizationName()113     public void testGetDeviceOwnerOrganizationName() {
114         when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
115         assertThat(mProvider.getDeviceOwnerOrganizationName()).isNull();
116 
117         when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION);
118         assertThat(mProvider.getDeviceOwnerOrganizationName()).isEqualTo(OWNER_ORGANIZATION);
119     }
120 
121     @Test
testGetDeviceOwnerDisclosure()122     public void testGetDeviceOwnerDisclosure() {
123         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null);
124         assertThat(mProvider.getDeviceOwnerDisclosure()).isNull();
125 
126         SpannableStringBuilder disclosure = new SpannableStringBuilder();
127         disclosure.append(mResources.getString(R.string.do_disclosure_generic));
128         disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator));
129         disclosure.append(mResources.getString(R.string.learn_more),
130                 new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(mContext), 0);
131         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
132         when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
133         assertThat(mProvider.getDeviceOwnerDisclosure()).isEqualTo(disclosure);
134 
135         disclosure = new SpannableStringBuilder();
136         disclosure.append(mResources.getString(R.string.do_disclosure_with_name,
137                 OWNER_ORGANIZATION));
138         disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator));
139         disclosure.append(mResources.getString(R.string.learn_more),
140                 new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(mContext), 0);
141         when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION);
142         assertThat(mProvider.getDeviceOwnerDisclosure()).isEqualTo(disclosure);
143     }
144 
145     @Test
testGetLastSecurityLogRetrievalTime()146     public void testGetLastSecurityLogRetrievalTime() {
147         when(mDevicePolicyManager.getLastSecurityLogRetrievalTime()).thenReturn(-1L);
148         assertThat(mProvider.getLastSecurityLogRetrievalTime()).isNull();
149 
150         when(mDevicePolicyManager.getLastSecurityLogRetrievalTime())
151                 .thenReturn(TIMESTAMP.getTime());
152         assertThat(mProvider.getLastSecurityLogRetrievalTime()).isEqualTo(TIMESTAMP);
153     }
154 
155     @Test
testGetLastBugReportRequestTime()156     public void testGetLastBugReportRequestTime() {
157         when(mDevicePolicyManager.getLastBugReportRequestTime()).thenReturn(-1L);
158         assertThat(mProvider.getLastBugReportRequestTime()).isNull();
159 
160         when(mDevicePolicyManager.getLastBugReportRequestTime()).thenReturn(TIMESTAMP.getTime());
161         assertThat(mProvider.getLastBugReportRequestTime()).isEqualTo(TIMESTAMP);
162     }
163 
164     @Test
testGetLastNetworkLogRetrievalTime()165     public void testGetLastNetworkLogRetrievalTime() {
166         when(mDevicePolicyManager.getLastNetworkLogRetrievalTime()).thenReturn(-1L);
167         assertThat(mProvider.getLastNetworkLogRetrievalTime()).isNull();
168 
169         when(mDevicePolicyManager.getLastNetworkLogRetrievalTime()).thenReturn(TIMESTAMP.getTime());
170         assertThat(mProvider.getLastNetworkLogRetrievalTime()).isEqualTo(TIMESTAMP);
171     }
172 
173     @Test
testIsSecurityLoggingEnabled()174     public void testIsSecurityLoggingEnabled() {
175         when(mDevicePolicyManager.isSecurityLoggingEnabled(null)).thenReturn(false);
176         assertThat(mProvider.isSecurityLoggingEnabled()).isFalse();
177 
178         when(mDevicePolicyManager.isSecurityLoggingEnabled(null)).thenReturn(true);
179         assertThat(mProvider.isSecurityLoggingEnabled()).isTrue();
180     }
181 
182     @Test
testIsNetworkLoggingEnabled()183     public void testIsNetworkLoggingEnabled() {
184         when(mDevicePolicyManager.isNetworkLoggingEnabled(null)).thenReturn(false);
185         assertThat(mProvider.isNetworkLoggingEnabled()).isFalse();
186 
187         when(mDevicePolicyManager.isNetworkLoggingEnabled(null)).thenReturn(true);
188         assertThat(mProvider.isNetworkLoggingEnabled()).isTrue();
189     }
190 
191     @Test
testIsAlwaysOnVpnSetInCurrentUser()192     public void testIsAlwaysOnVpnSetInCurrentUser() {
193         when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MY_USER_ID)).thenReturn(null);
194         assertThat(mProvider.isAlwaysOnVpnSetInCurrentUser()).isFalse();
195 
196         when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MY_USER_ID))
197                 .thenReturn(VPN_PACKAGE_ID);
198         assertThat(mProvider.isAlwaysOnVpnSetInCurrentUser()).isTrue();
199     }
200 
201     @Test
testIsAlwaysOnVpnSetInManagedProfileProfile()202     public void testIsAlwaysOnVpnSetInManagedProfileProfile() {
203         assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse();
204 
205         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
206 
207         when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MANAGED_PROFILE_USER_ID))
208                 .thenReturn(null);
209         assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse();
210 
211         when(mConnectivityManger.getAlwaysOnVpnPackageForUser(MANAGED_PROFILE_USER_ID))
212                 .thenReturn(VPN_PACKAGE_ID);
213         assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isTrue();
214     }
215 
216     @Test
testIsGlobalHttpProxySet()217     public void testIsGlobalHttpProxySet() {
218         when(mConnectivityManger.getGlobalProxy()).thenReturn(null);
219         assertThat(mProvider.isGlobalHttpProxySet()).isFalse();
220 
221         when(mConnectivityManger.getGlobalProxy())
222             .thenReturn(ProxyInfo.buildDirectProxy("localhost", 123));
223         assertThat(mProvider.isGlobalHttpProxySet()).isTrue();
224     }
225 
226     @Test
testGetMaximumFailedPasswordsForWipeInCurrentUser()227     public void testGetMaximumFailedPasswordsForWipeInCurrentUser() {
228         when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(null);
229         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(null);
230         when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, MY_USER_ID))
231                 .thenReturn(10);
232         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(0);
233 
234         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(OWNER);
235         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10);
236 
237         when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(OWNER);
238         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(null);
239         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10);
240     }
241 
242     @Test
testGetMaximumFailedPasswordsForWipeInManagedProfile()243     public void testGetMaximumFailedPasswordsForWipeInManagedProfile() {
244         when(mDevicePolicyManager.getProfileOwnerAsUser(MANAGED_PROFILE_USER_ID)).thenReturn(OWNER);
245         when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, MANAGED_PROFILE_USER_ID))
246                 .thenReturn(10);
247         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(0);
248 
249         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
250         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(10);
251     }
252 
253     @Test
testGetImeLabelIfOwnerSet()254     public void testGetImeLabelIfOwnerSet() throws Exception {
255         final ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
256         when(applicationInfo.loadLabel(mPackageManager)).thenReturn(IME_PACKAGE_LABEL);
257 
258         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID);
259         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
260                 .thenReturn(applicationInfo);
261 
262         // IME not set by Device Owner.
263         when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(false);
264         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
265 
266         // Device Owner set IME to empty string.
267         when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(true);
268         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, null);
269         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
270 
271         // Device Owner set IME to nonexistent package.
272         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID);
273         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
274                 .thenThrow(new PackageManager.NameNotFoundException());
275         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
276 
277         // Device Owner set IME to existent package.
278         resetAndInitializePackageManagerWrapper();
279         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
280                 .thenReturn(applicationInfo);
281         assertThat(mProvider.getImeLabelIfOwnerSet()).isEqualTo(IME_PACKAGE_LABEL);
282     }
283 
284     @Test
testGetNumberOfOwnerInstalledCaCertsForCurrent()285     public void testGetNumberOfOwnerInstalledCaCertsForCurrent() {
286         final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM);
287         final UserHandle managedProfileUserHandle = new UserHandle(MANAGED_PROFILE_USER_ID);
288 
289         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
290                 .thenReturn(Arrays.asList("ca1", "ca2"));
291 
292         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
293                 .thenReturn(null);
294         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
295                 .isEqualTo(0);
296         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
297                 .thenReturn(new ArrayList<>());
298         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
299                 .isEqualTo(0);
300         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
301                 .thenReturn(Arrays.asList("ca1", "ca2"));
302         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
303                 .isEqualTo(2);
304     }
305 
306     @Test
testGetNumberOfOwnerInstalledCaCertsForManagedProfile()307     public void testGetNumberOfOwnerInstalledCaCertsForManagedProfile() {
308         final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM);
309         final UserHandle managedProfileUserHandle = new UserHandle(MANAGED_PROFILE_USER_ID);
310         final UserInfo managedProfile =
311                 new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE);
312 
313         // Without a profile
314         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
315                 .thenReturn(Arrays.asList("ca1", "ca2"));
316         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
317                 .isEqualTo(0);
318 
319         // With a profile
320         mProfiles.add(managedProfile);
321         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
322                 .thenReturn(null);
323         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
324                 .isEqualTo(0);
325         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
326                 .thenReturn(new ArrayList<>());
327         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
328                 .isEqualTo(0);
329         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
330                 .thenReturn(Arrays.asList("ca1", "ca2"));
331         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
332                 .isEqualTo(2);
333     }
334 
335     @Test
testGetNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()336     public void testGetNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() {
337         when(mDevicePolicyManager.getActiveAdminsAsUser(MY_USER_ID))
338                 .thenReturn(Arrays.asList(ADMIN_1, ADMIN_2));
339         when(mDevicePolicyManager.getActiveAdminsAsUser(MANAGED_PROFILE_USER_ID))
340                 .thenReturn(Arrays.asList(ADMIN_1));
341 
342         assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile())
343                 .isEqualTo(2);
344 
345         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
346         assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile())
347                 .isEqualTo(3);
348     }
349 
350     @Test
testAreBackupsMandatory()351     public void testAreBackupsMandatory() {
352         assertThat(mProvider.areBackupsMandatory()).isFalse();
353         ComponentName transportComponent = new ComponentName("test", "test");
354         when(mDevicePolicyManager.getMandatoryBackupTransport())
355                 .thenReturn(transportComponent);
356         assertThat(mProvider.areBackupsMandatory()).isTrue();
357     }
358 
resetAndInitializePackageManagerWrapper()359     private void resetAndInitializePackageManagerWrapper() {
360         reset(mPackageManagerWrapper);
361         when(mPackageManagerWrapper.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN))
362                 .thenReturn(true);
363         when(mPackageManagerWrapper.getPackageManager()).thenReturn(mPackageManager);
364     }
365 }
366