Home | History | Annotate | Download | only in enterprise
      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.testutils.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
     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
    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
    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
    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
    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
    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
    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
    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
    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
    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
    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
    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
    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
    233     public void testGetMaximumFailedPasswordsForWipeInCurrentUser() {
    234         when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(null);
    235         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(null);
    236         when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, MY_USER_ID))
    237                 .thenReturn(10);
    238         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(0);
    239 
    240         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(OWNER);
    241         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10);
    242 
    243         when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(OWNER);
    244         when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(null);
    245         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10);
    246     }
    247 
    248     @Test
    249     public void testGetMaximumFailedPasswordsForWipeInManagedProfile() {
    250         when(mDevicePolicyManager.getProfileOwnerAsUser(MANAGED_PROFILE_USER_ID)).thenReturn(OWNER);
    251         when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, MANAGED_PROFILE_USER_ID))
    252                 .thenReturn(10);
    253         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(0);
    254 
    255         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
    256         assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(10);
    257     }
    258 
    259     @Test
    260     public void testGetImeLabelIfOwnerSet() throws Exception {
    261         final ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
    262         when(applicationInfo.loadLabel(mPackageManager)).thenReturn(IME_PACKAGE_LABEL);
    263 
    264         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID);
    265         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
    266                 .thenReturn(applicationInfo);
    267 
    268         // IME not set by Device Owner.
    269         when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(false);
    270         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
    271 
    272         // Device Owner set IME to empty string.
    273         when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(true);
    274         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, null);
    275         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
    276 
    277         // Device Owner set IME to nonexistent package.
    278         Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID);
    279         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
    280                 .thenThrow(new PackageManager.NameNotFoundException());
    281         assertThat(mProvider.getImeLabelIfOwnerSet()).isNull();
    282 
    283         // Device Owner set IME to existent package.
    284         resetAndInitializePackageManagerWrapper();
    285         when(mPackageManagerWrapper.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, MY_USER_ID))
    286                 .thenReturn(applicationInfo);
    287         assertThat(mProvider.getImeLabelIfOwnerSet()).isEqualTo(IME_PACKAGE_LABEL);
    288     }
    289 
    290     @Test
    291     public void testGetNumberOfOwnerInstalledCaCertsForCurrent() {
    292         final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM);
    293         final UserHandle managedProfileUserHandle = new UserHandle(MANAGED_PROFILE_USER_ID);
    294         final UserInfo managedProfile =
    295                 new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE);
    296 
    297         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
    298                 .thenReturn(Arrays.asList(new String[] {"ca1", "ca2"}));
    299 
    300         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
    301                 .thenReturn(null);
    302         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
    303                 .isEqualTo(0);
    304         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
    305                 .thenReturn(new ArrayList<>());
    306         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
    307                 .isEqualTo(0);
    308         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
    309                 .thenReturn(Arrays.asList(new String[] {"ca1", "ca2"}));
    310         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser())
    311                 .isEqualTo(2);
    312     }
    313 
    314     @Test
    315     public void testGetNumberOfOwnerInstalledCaCertsForManagedProfile() {
    316         final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM);
    317         final UserHandle managedProfileUserHandle = new UserHandle(MANAGED_PROFILE_USER_ID);
    318         final UserInfo managedProfile =
    319                 new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE);
    320 
    321         // Without a profile
    322         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
    323                 .thenReturn(Arrays.asList(new String[] {"ca1", "ca2"}));
    324         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
    325                 .isEqualTo(0);
    326 
    327         // With a profile
    328         mProfiles.add(managedProfile);
    329         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
    330                 .thenReturn(null);
    331         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
    332                 .isEqualTo(0);
    333         when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle))
    334                 .thenReturn(new ArrayList<>());
    335         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
    336                 .isEqualTo(0);
    337         when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle))
    338                 .thenReturn(Arrays.asList(new String[] {"ca1", "ca2"}));
    339         assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile())
    340                 .isEqualTo(2);
    341     }
    342 
    343     @Test
    344     public void testGetNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() {
    345         when(mDevicePolicyManager.getActiveAdminsAsUser(MY_USER_ID))
    346                 .thenReturn(Arrays.asList(new ComponentName[] {ADMIN_1, ADMIN_2}));
    347         when(mDevicePolicyManager.getActiveAdminsAsUser(MANAGED_PROFILE_USER_ID))
    348                 .thenReturn(Arrays.asList(new ComponentName[] {ADMIN_1}));
    349 
    350         assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile())
    351                 .isEqualTo(2);
    352 
    353         mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
    354         assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile())
    355                 .isEqualTo(3);
    356     }
    357 
    358     private void resetAndInitializePackageManagerWrapper() {
    359         reset(mPackageManagerWrapper);
    360         when(mPackageManagerWrapper.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN))
    361                 .thenReturn(true);
    362         when(mPackageManagerWrapper.getPackageManager()).thenReturn(mPackageManager);
    363     }
    364 }
    365