Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 78056c77 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Convert LockSettingsService tests to JUnit4"

parents 0899f50b d01bb41b
Loading
Loading
Loading
Loading
+23 −19
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@

package com.android.server.locksettings;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -40,7 +42,9 @@ import android.os.UserManager;
import android.os.storage.IStorageManager;
import android.os.storage.StorageManager;
import android.security.KeyStore;
import android.test.AndroidTestCase;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.widget.ILockSettings;
import com.android.internal.widget.LockPatternUtils;
@@ -49,6 +53,9 @@ import com.android.server.LocalServices;
import com.android.server.locksettings.recoverablekeystore.RecoverableKeyStoreManager;
import com.android.server.wm.WindowManagerInternal;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

@@ -56,8 +63,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;


public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public abstract class BaseLockSettingsServiceTests {
    protected static final int PRIMARY_USER_ID = 0;
    protected static final int MANAGED_PROFILE_USER_ID = 12;
    protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
@@ -93,10 +100,8 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
    RecoverableKeyStoreManager mRecoverableKeyStoreManager;
    protected boolean mHasSecureLockScreen;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

    @Before
    public void setUp_baseServices() throws Exception {
        mGateKeeperService = new FakeGateKeeperService();
        mNotificationManager = mock(NotificationManager.class);
        mUserManager = mock(UserManager.class);
@@ -115,11 +120,11 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
        LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
        LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);

        mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
                mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class),
                mock(KeyguardManager.class));
        mContext = new MockLockSettingsContext(InstrumentationRegistry.getContext(), mUserManager,
                mNotificationManager, mDevicePolicyManager, mock(StorageManager.class),
                mock(TrustManager.class), mock(KeyguardManager.class));
        mStorage = new LockSettingsStorageTestable(mContext,
                new File(getContext().getFilesDir(), "locksettings"));
                new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
        File storageDir = mStorage.mStorageDir;
        if (storageDir.exists()) {
            FileUtils.deleteContents(storageDir);
@@ -222,11 +227,10 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
        return sm;
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    @After
    public void tearDown_baseServices() throws Exception {
        mStorage.closeDatabase();
        File db = getContext().getDatabasePath("locksettings.db");
        File db = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
        assertTrue(!db.exists() || db.delete());

        File storageDir = mStorage.mStorageDir;
+12 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED

import static com.android.server.testutils.TestUtils.assertExpectException;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
@@ -29,10 +30,14 @@ import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;

import java.util.ArrayList;
@@ -45,11 +50,11 @@ import java.util.ArrayList;
 */
@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    @Before
    public void enableSpCache() throws Exception {
        enableSpCaching(true);
    }

@@ -58,6 +63,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
                .canUserHaveUntrustedCredentialReset(anyInt())).thenReturn(enable);
    }

    @Test
    public void testSyntheticPasswordClearCredentialUntrusted() throws RemoteException {
        final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
        final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes();
@@ -78,6 +84,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
        assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
    }

    @Test
    public void testSyntheticPasswordChangeCredentialUntrusted() throws RemoteException {
        final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
        final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes();
@@ -95,6 +102,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
                LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID).getResponseCode());
    }

    @Test
    public void testUntrustedCredentialChangeMaintainsAuthSecret() throws RemoteException {
        final byte[] password =
                "testUntrustedCredentialChangeMaintainsAuthSecret-password".getBytes();
@@ -117,6 +125,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
        assertEquals(1, secret.getAllValues().stream().distinct().count());
    }

    @Test
    public void testUntrustedCredentialChangeBlockedIfSpNotCached() throws RemoteException {
        final byte[] password =
                "testUntrustedCredentialChangeBlockedIfSpNotCached-password".getBytes();
+34 −14
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@ import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -37,59 +43,60 @@ import android.platform.test.annotations.Presubmit;
import android.service.gatekeeper.GateKeeperResponse;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse;
import com.android.server.locksettings.FakeGateKeeperService.VerifyHandle;
import com.android.server.locksettings.LockSettingsStorage.CredentialHash;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsServiceTests
 * atest FrameworksServicesTests:LockSettingsServiceTests
 */
@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testCreatePasswordPrimaryUser() throws RemoteException {
        testCreateCredential(PRIMARY_USER_ID, "password", CREDENTIAL_TYPE_PASSWORD,
                PASSWORD_QUALITY_ALPHABETIC);
    }

    @Test
    public void testCreatePasswordFailsWithoutLockScreen() throws RemoteException {
        testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "password",
                CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC);
    }

    @Test
    public void testCreatePatternPrimaryUser() throws RemoteException {
        testCreateCredential(PRIMARY_USER_ID, "123456789", CREDENTIAL_TYPE_PATTERN,
                PASSWORD_QUALITY_SOMETHING);
    }

    @Test
    public void testCreatePatternFailsWithoutLockScreen() throws RemoteException {
        testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "123456789",
                CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING);
    }

    @Test
    public void testChangePasswordPrimaryUser() throws RemoteException {
        testChangeCredentials(PRIMARY_USER_ID, "78963214", CREDENTIAL_TYPE_PATTERN,
                "asdfghjk", CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC);
    }

    @Test
    public void testChangePatternPrimaryUser() throws RemoteException {
        testChangeCredentials(PRIMARY_USER_ID, "!£$%^&*(())", CREDENTIAL_TYPE_PASSWORD,
                "1596321", CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING);
    }

    @Test
    public void testChangePasswordFailPrimaryUser() throws RemoteException {
        final long sid = 1234;
        final String FAILED_MESSAGE = "Failed to enroll password";
@@ -105,6 +112,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
        assertVerifyCredentials(PRIMARY_USER_ID, "password", CREDENTIAL_TYPE_PASSWORD, sid);
    }

    @Test
    public void testClearPasswordPrimaryUser() throws RemoteException {
        final String PASSWORD = "password";
        initializeStorageWithCredential(PRIMARY_USER_ID, PASSWORD, CREDENTIAL_TYPE_PASSWORD, 1234);
@@ -115,6 +123,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
        assertEquals(0, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
    }

    @Test
    public void testManagedProfileUnifiedChallenge() throws RemoteException {
        final String firstUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-1";
        final String secondUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-2";
@@ -170,6 +179,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
        assertEquals(0, mGateKeeperService.getSecureUserId(TURNED_OFF_PROFILE_USER_ID));
    }

    @Test
    public void testManagedProfileSeparateChallenge() throws RemoteException {
        final String primaryPassword = "testManagedProfileSeparateChallenge-primary";
        final String profilePassword = "testManagedProfileSeparateChallenge-profile";
@@ -218,6 +228,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
        assertEquals(profileSid, mGateKeeperService.getSecureUserId(MANAGED_PROFILE_USER_ID));
    }

    @Test
    public void testSetLockCredential_forPrimaryUser_sendsCredentials() throws Exception {
        final byte[] password = "password".getBytes();

@@ -233,6 +244,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                .lockScreenSecretChanged(CREDENTIAL_TYPE_PASSWORD, password, PRIMARY_USER_ID);
    }

    @Test
    public void testSetLockCredential_forProfileWithSeparateChallenge_sendsCredentials()
            throws Exception {
        final byte[] pattern = "12345".getBytes();
@@ -249,6 +261,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                .lockScreenSecretChanged(CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID);
    }

    @Test
    public void testSetLockCredential_forProfileWithSeparateChallenge_updatesCredentials()
            throws Exception {
        final String oldCredential = "12345";
@@ -272,6 +285,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID);
    }

    @Test
    public void testSetLockCredential_forProfileWithUnifiedChallenge_doesNotSendRandomCredential()
            throws Exception {
        mService.setSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID, false, null);
@@ -289,6 +303,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        eq(CREDENTIAL_TYPE_PASSWORD), any(), eq(MANAGED_PROFILE_USER_ID));
    }

    @Test
    public void
            testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_updatesBothCredentials()
                    throws Exception {
@@ -313,6 +328,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID);
    }

    @Test
    public void
            testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_removesBothCredentials()
                    throws Exception {
@@ -335,6 +351,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                .lockScreenSecretChanged(CREDENTIAL_TYPE_NONE, null, MANAGED_PROFILE_USER_ID);
    }

    @Test
    public void testSetLockCredential_forUnifiedToSeparateChallengeProfile_sendsNewCredentials()
            throws Exception {
        final String parentPassword = "parentPassword";
@@ -356,6 +373,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        CREDENTIAL_TYPE_PASSWORD, profilePassword, MANAGED_PROFILE_USER_ID);
    }

    @Test
    public void
            testSetLockCredential_forSeparateToUnifiedChallengeProfile_doesNotSendRandomCredential()
                    throws Exception {
@@ -379,6 +397,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                .lockScreenSecretChanged(anyInt(), any(), eq(MANAGED_PROFILE_USER_ID));
    }

    @Test
    public void testVerifyCredential_forPrimaryUser_sendsCredentials() throws Exception {
        final String password = "password";
        initializeStorageWithCredential(PRIMARY_USER_ID, password, CREDENTIAL_TYPE_PASSWORD, 1234);
@@ -392,6 +411,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        CREDENTIAL_TYPE_PASSWORD, password.getBytes(), PRIMARY_USER_ID);
    }

    @Test
    public void testVerifyCredential_forProfileWithSeparateChallenge_sendsCredentials()
            throws Exception {
        final byte[] pattern = "12345".getBytes();
@@ -411,8 +431,8 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
                        CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID);
    }

    public void
            testVerifyCredential_forPrimaryUserWithUnifiedChallengeProfile_sendsCredentialsForBoth()
    @Test
    public void verifyCredential_forPrimaryUserWithUnifiedChallengeProfile_sendsCredentialsForBoth()
                    throws Exception {
        final String pattern = "12345";
        initializeStorageWithCredential(PRIMARY_USER_ID, pattern, CREDENTIAL_TYPE_PATTERN, 1234);
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import static com.android.internal.widget.LockPatternUtils.stringToPattern;

import static junit.framework.Assert.assertEquals;

import static org.mockito.Matchers.anyInt;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
+60 −18

File changed.

Preview size limit exceeded, changes collapsed.

Loading