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