Merge "Convert LockSettingsService tests to JUnit4"

This commit is contained in:
Treehugger Robot
2019-12-02 19:18:23 +00:00
committed by Gerrit Code Review
9 changed files with 198 additions and 80 deletions

View File

@@ -16,10 +16,12 @@
package com.android.server.locksettings; package com.android.server.locksettings;
import static org.mockito.Matchers.any; import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.anyBoolean; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyInt; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Matchers.eq; 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.doAnswer;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -40,7 +42,9 @@ import android.os.UserManager;
import android.os.storage.IStorageManager; import android.os.storage.IStorageManager;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.security.KeyStore; 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.ILockSettings;
import com.android.internal.widget.LockPatternUtils; 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.locksettings.recoverablekeystore.RecoverableKeyStoreManager;
import com.android.server.wm.WindowManagerInternal; 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.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
@@ -56,8 +63,8 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@RunWith(AndroidJUnit4.class)
public abstract class BaseLockSettingsServiceTests extends AndroidTestCase { public abstract class BaseLockSettingsServiceTests {
protected static final int PRIMARY_USER_ID = 0; protected static final int PRIMARY_USER_ID = 0;
protected static final int MANAGED_PROFILE_USER_ID = 12; protected static final int MANAGED_PROFILE_USER_ID = 12;
protected static final int TURNED_OFF_PROFILE_USER_ID = 17; protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
@@ -93,10 +100,8 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
RecoverableKeyStoreManager mRecoverableKeyStoreManager; RecoverableKeyStoreManager mRecoverableKeyStoreManager;
protected boolean mHasSecureLockScreen; protected boolean mHasSecureLockScreen;
@Override @Before
protected void setUp() throws Exception { public void setUp_baseServices() throws Exception {
super.setUp();
mGateKeeperService = new FakeGateKeeperService(); mGateKeeperService = new FakeGateKeeperService();
mNotificationManager = mock(NotificationManager.class); mNotificationManager = mock(NotificationManager.class);
mUserManager = mock(UserManager.class); mUserManager = mock(UserManager.class);
@@ -115,11 +120,11 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal); LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
LocalServices.addService(WindowManagerInternal.class, mMockWindowManager); LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);
mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager, mContext = new MockLockSettingsContext(InstrumentationRegistry.getContext(), mUserManager,
mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class), mNotificationManager, mDevicePolicyManager, mock(StorageManager.class),
mock(KeyguardManager.class)); mock(TrustManager.class), mock(KeyguardManager.class));
mStorage = new LockSettingsStorageTestable(mContext, mStorage = new LockSettingsStorageTestable(mContext,
new File(getContext().getFilesDir(), "locksettings")); new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
File storageDir = mStorage.mStorageDir; File storageDir = mStorage.mStorageDir;
if (storageDir.exists()) { if (storageDir.exists()) {
FileUtils.deleteContents(storageDir); FileUtils.deleteContents(storageDir);
@@ -222,11 +227,10 @@ public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
return sm; return sm;
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown_baseServices() throws Exception {
super.tearDown();
mStorage.closeDatabase(); mStorage.closeDatabase();
File db = getContext().getDatabasePath("locksettings.db"); File db = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
assertTrue(!db.exists() || db.delete()); assertTrue(!db.exists() || db.delete());
File storageDir = mStorage.mStorageDir; File storageDir = mStorage.mStorageDir;

View File

@@ -20,6 +20,7 @@ import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED
import static com.android.server.testutils.TestUtils.assertExpectException; 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.anyInt;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -29,10 +30,14 @@ import android.os.RemoteException;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse; import com.android.internal.widget.VerifyCredentialResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import java.util.ArrayList; import java.util.ArrayList;
@@ -45,11 +50,11 @@ import java.util.ArrayList;
*/ */
@SmallTest @SmallTest
@Presubmit @Presubmit
@RunWith(AndroidJUnit4.class)
public class CachedSyntheticPasswordTests extends SyntheticPasswordTests { public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
@Override @Before
protected void setUp() throws Exception { public void enableSpCache() throws Exception {
super.setUp();
enableSpCaching(true); enableSpCaching(true);
} }
@@ -58,6 +63,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
.canUserHaveUntrustedCredentialReset(anyInt())).thenReturn(enable); .canUserHaveUntrustedCredentialReset(anyInt())).thenReturn(enable);
} }
@Test
public void testSyntheticPasswordClearCredentialUntrusted() throws RemoteException { public void testSyntheticPasswordClearCredentialUntrusted() throws RemoteException {
final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes(); final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes(); final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes();
@@ -78,6 +84,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
} }
@Test
public void testSyntheticPasswordChangeCredentialUntrusted() throws RemoteException { public void testSyntheticPasswordChangeCredentialUntrusted() throws RemoteException {
final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes(); final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".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()); LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID).getResponseCode());
} }
@Test
public void testUntrustedCredentialChangeMaintainsAuthSecret() throws RemoteException { public void testUntrustedCredentialChangeMaintainsAuthSecret() throws RemoteException {
final byte[] password = final byte[] password =
"testUntrustedCredentialChangeMaintainsAuthSecret-password".getBytes(); "testUntrustedCredentialChangeMaintainsAuthSecret-password".getBytes();
@@ -117,6 +125,7 @@ public class CachedSyntheticPasswordTests extends SyntheticPasswordTests {
assertEquals(1, secret.getAllValues().stream().distinct().count()); assertEquals(1, secret.getAllValues().stream().distinct().count());
} }
@Test
public void testUntrustedCredentialChangeBlockedIfSpNotCached() throws RemoteException { public void testUntrustedCredentialChangeBlockedIfSpNotCached() throws RemoteException {
final byte[] password = final byte[] password =
"testUntrustedCredentialChangeBlockedIfSpNotCached-password".getBytes(); "testUntrustedCredentialChangeBlockedIfSpNotCached-password".getBytes();

View File

@@ -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_PASSWORD;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN; 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.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@@ -37,59 +43,60 @@ import android.platform.test.annotations.Presubmit;
import android.service.gatekeeper.GateKeeperResponse; import android.service.gatekeeper.GateKeeperResponse;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse; import com.android.internal.widget.VerifyCredentialResponse;
import com.android.server.locksettings.FakeGateKeeperService.VerifyHandle; import com.android.server.locksettings.FakeGateKeeperService.VerifyHandle;
import com.android.server.locksettings.LockSettingsStorage.CredentialHash; 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 @SmallTest
@Presubmit @Presubmit
@RunWith(AndroidJUnit4.class)
public class LockSettingsServiceTests extends BaseLockSettingsServiceTests { public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
@Test
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCreatePasswordPrimaryUser() throws RemoteException { public void testCreatePasswordPrimaryUser() throws RemoteException {
testCreateCredential(PRIMARY_USER_ID, "password", CREDENTIAL_TYPE_PASSWORD, testCreateCredential(PRIMARY_USER_ID, "password", CREDENTIAL_TYPE_PASSWORD,
PASSWORD_QUALITY_ALPHABETIC); PASSWORD_QUALITY_ALPHABETIC);
} }
@Test
public void testCreatePasswordFailsWithoutLockScreen() throws RemoteException { public void testCreatePasswordFailsWithoutLockScreen() throws RemoteException {
testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "password", testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "password",
CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC); CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC);
} }
@Test
public void testCreatePatternPrimaryUser() throws RemoteException { public void testCreatePatternPrimaryUser() throws RemoteException {
testCreateCredential(PRIMARY_USER_ID, "123456789", CREDENTIAL_TYPE_PATTERN, testCreateCredential(PRIMARY_USER_ID, "123456789", CREDENTIAL_TYPE_PATTERN,
PASSWORD_QUALITY_SOMETHING); PASSWORD_QUALITY_SOMETHING);
} }
@Test
public void testCreatePatternFailsWithoutLockScreen() throws RemoteException { public void testCreatePatternFailsWithoutLockScreen() throws RemoteException {
testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "123456789", testCreateCredentialFailsWithoutLockScreen(PRIMARY_USER_ID, "123456789",
CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING); CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING);
} }
@Test
public void testChangePasswordPrimaryUser() throws RemoteException { public void testChangePasswordPrimaryUser() throws RemoteException {
testChangeCredentials(PRIMARY_USER_ID, "78963214", CREDENTIAL_TYPE_PATTERN, testChangeCredentials(PRIMARY_USER_ID, "78963214", CREDENTIAL_TYPE_PATTERN,
"asdfghjk", CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC); "asdfghjk", CREDENTIAL_TYPE_PASSWORD, PASSWORD_QUALITY_ALPHABETIC);
} }
@Test
public void testChangePatternPrimaryUser() throws RemoteException { public void testChangePatternPrimaryUser() throws RemoteException {
testChangeCredentials(PRIMARY_USER_ID, "!£$%^&*(())", CREDENTIAL_TYPE_PASSWORD, testChangeCredentials(PRIMARY_USER_ID, "!£$%^&*(())", CREDENTIAL_TYPE_PASSWORD,
"1596321", CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING); "1596321", CREDENTIAL_TYPE_PATTERN, PASSWORD_QUALITY_SOMETHING);
} }
@Test
public void testChangePasswordFailPrimaryUser() throws RemoteException { public void testChangePasswordFailPrimaryUser() throws RemoteException {
final long sid = 1234; final long sid = 1234;
final String FAILED_MESSAGE = "Failed to enroll password"; 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); assertVerifyCredentials(PRIMARY_USER_ID, "password", CREDENTIAL_TYPE_PASSWORD, sid);
} }
@Test
public void testClearPasswordPrimaryUser() throws RemoteException { public void testClearPasswordPrimaryUser() throws RemoteException {
final String PASSWORD = "password"; final String PASSWORD = "password";
initializeStorageWithCredential(PRIMARY_USER_ID, PASSWORD, CREDENTIAL_TYPE_PASSWORD, 1234); 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)); assertEquals(0, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
} }
@Test
public void testManagedProfileUnifiedChallenge() throws RemoteException { public void testManagedProfileUnifiedChallenge() throws RemoteException {
final String firstUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-1"; final String firstUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-1";
final String secondUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-2"; final String secondUnifiedPassword = "testManagedProfileUnifiedChallenge-pwd-2";
@@ -170,6 +179,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
assertEquals(0, mGateKeeperService.getSecureUserId(TURNED_OFF_PROFILE_USER_ID)); assertEquals(0, mGateKeeperService.getSecureUserId(TURNED_OFF_PROFILE_USER_ID));
} }
@Test
public void testManagedProfileSeparateChallenge() throws RemoteException { public void testManagedProfileSeparateChallenge() throws RemoteException {
final String primaryPassword = "testManagedProfileSeparateChallenge-primary"; final String primaryPassword = "testManagedProfileSeparateChallenge-primary";
final String profilePassword = "testManagedProfileSeparateChallenge-profile"; final String profilePassword = "testManagedProfileSeparateChallenge-profile";
@@ -218,6 +228,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
assertEquals(profileSid, mGateKeeperService.getSecureUserId(MANAGED_PROFILE_USER_ID)); assertEquals(profileSid, mGateKeeperService.getSecureUserId(MANAGED_PROFILE_USER_ID));
} }
@Test
public void testSetLockCredential_forPrimaryUser_sendsCredentials() throws Exception { public void testSetLockCredential_forPrimaryUser_sendsCredentials() throws Exception {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
@@ -233,6 +244,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
.lockScreenSecretChanged(CREDENTIAL_TYPE_PASSWORD, password, PRIMARY_USER_ID); .lockScreenSecretChanged(CREDENTIAL_TYPE_PASSWORD, password, PRIMARY_USER_ID);
} }
@Test
public void testSetLockCredential_forProfileWithSeparateChallenge_sendsCredentials() public void testSetLockCredential_forProfileWithSeparateChallenge_sendsCredentials()
throws Exception { throws Exception {
final byte[] pattern = "12345".getBytes(); final byte[] pattern = "12345".getBytes();
@@ -249,6 +261,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
.lockScreenSecretChanged(CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID); .lockScreenSecretChanged(CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID);
} }
@Test
public void testSetLockCredential_forProfileWithSeparateChallenge_updatesCredentials() public void testSetLockCredential_forProfileWithSeparateChallenge_updatesCredentials()
throws Exception { throws Exception {
final String oldCredential = "12345"; final String oldCredential = "12345";
@@ -272,6 +285,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID); CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID);
} }
@Test
public void testSetLockCredential_forProfileWithUnifiedChallenge_doesNotSendRandomCredential() public void testSetLockCredential_forProfileWithUnifiedChallenge_doesNotSendRandomCredential()
throws Exception { throws Exception {
mService.setSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID, false, null); 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)); eq(CREDENTIAL_TYPE_PASSWORD), any(), eq(MANAGED_PROFILE_USER_ID));
} }
@Test
public void public void
testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_updatesBothCredentials() testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_updatesBothCredentials()
throws Exception { throws Exception {
@@ -313,6 +328,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID); CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID);
} }
@Test
public void public void
testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_removesBothCredentials() testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_removesBothCredentials()
throws Exception { throws Exception {
@@ -335,6 +351,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
.lockScreenSecretChanged(CREDENTIAL_TYPE_NONE, null, MANAGED_PROFILE_USER_ID); .lockScreenSecretChanged(CREDENTIAL_TYPE_NONE, null, MANAGED_PROFILE_USER_ID);
} }
@Test
public void testSetLockCredential_forUnifiedToSeparateChallengeProfile_sendsNewCredentials() public void testSetLockCredential_forUnifiedToSeparateChallengeProfile_sendsNewCredentials()
throws Exception { throws Exception {
final String parentPassword = "parentPassword"; final String parentPassword = "parentPassword";
@@ -356,6 +373,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
CREDENTIAL_TYPE_PASSWORD, profilePassword, MANAGED_PROFILE_USER_ID); CREDENTIAL_TYPE_PASSWORD, profilePassword, MANAGED_PROFILE_USER_ID);
} }
@Test
public void public void
testSetLockCredential_forSeparateToUnifiedChallengeProfile_doesNotSendRandomCredential() testSetLockCredential_forSeparateToUnifiedChallengeProfile_doesNotSendRandomCredential()
throws Exception { throws Exception {
@@ -379,6 +397,7 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
.lockScreenSecretChanged(anyInt(), any(), eq(MANAGED_PROFILE_USER_ID)); .lockScreenSecretChanged(anyInt(), any(), eq(MANAGED_PROFILE_USER_ID));
} }
@Test
public void testVerifyCredential_forPrimaryUser_sendsCredentials() throws Exception { public void testVerifyCredential_forPrimaryUser_sendsCredentials() throws Exception {
final String password = "password"; final String password = "password";
initializeStorageWithCredential(PRIMARY_USER_ID, password, CREDENTIAL_TYPE_PASSWORD, 1234); 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); CREDENTIAL_TYPE_PASSWORD, password.getBytes(), PRIMARY_USER_ID);
} }
@Test
public void testVerifyCredential_forProfileWithSeparateChallenge_sendsCredentials() public void testVerifyCredential_forProfileWithSeparateChallenge_sendsCredentials()
throws Exception { throws Exception {
final byte[] pattern = "12345".getBytes(); final byte[] pattern = "12345".getBytes();
@@ -411,8 +431,8 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID); CREDENTIAL_TYPE_PATTERN, pattern, MANAGED_PROFILE_USER_ID);
} }
public void @Test
testVerifyCredential_forPrimaryUserWithUnifiedChallengeProfile_sendsCredentialsForBoth() public void verifyCredential_forPrimaryUserWithUnifiedChallengeProfile_sendsCredentialsForBoth()
throws Exception { throws Exception {
final String pattern = "12345"; final String pattern = "12345";
initializeStorageWithCredential(PRIMARY_USER_ID, pattern, CREDENTIAL_TYPE_PATTERN, 1234); initializeStorageWithCredential(PRIMARY_USER_ID, pattern, CREDENTIAL_TYPE_PATTERN, 1234);

View File

@@ -23,7 +23,7 @@ import static com.android.internal.widget.LockPatternUtils.stringToPattern;
import static junit.framework.Assert.assertEquals; 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.any;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;

View File

@@ -16,7 +16,13 @@
package com.android.server.locksettings; package com.android.server.locksettings;
import static org.mockito.Matchers.eq; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -31,18 +37,24 @@ import android.os.SystemClock;
import android.os.UserManager; import android.os.UserManager;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import android.test.AndroidTestCase;
import android.util.Log; import android.util.Log;
import android.util.Log.TerribleFailure; import android.util.Log.TerribleFailure;
import android.util.Log.TerribleFailureHandler; import android.util.Log.TerribleFailureHandler;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.server.PersistentDataBlockManagerInternal; import com.android.server.PersistentDataBlockManagerInternal;
import com.android.server.locksettings.LockSettingsStorage.CredentialHash; import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
import com.android.server.locksettings.LockSettingsStorage.PersistentData; import com.android.server.locksettings.LockSettingsStorage.PersistentData;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -50,11 +62,12 @@ import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
/** /**
* runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests * atest FrameworksServicesTests:LockSettingsStorageTests
*/ */
@SmallTest @SmallTest
@Presubmit @Presubmit
public class LockSettingsStorageTests extends AndroidTestCase { @RunWith(AndroidJUnit4.class)
public class LockSettingsStorageTests {
private static final int SOME_USER_ID = 1034; private static final int SOME_USER_ID = 1034;
private final byte[] PASSWORD_0 = "thepassword0".getBytes(); private final byte[] PASSWORD_0 = "thepassword0".getBytes();
private final byte[] PASSWORD_1 = "password1".getBytes(); private final byte[] PASSWORD_1 = "password1".getBytes();
@@ -68,11 +81,10 @@ public class LockSettingsStorageTests extends AndroidTestCase {
private File mDb; private File mDb;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); mStorageDir = new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings");
mStorageDir = new File(getContext().getFilesDir(), "locksettings"); mDb = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
mDb = getContext().getDatabasePath("locksettings.db");
assertTrue(mStorageDir.exists() || mStorageDir.mkdirs()); assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
assertTrue(FileUtils.deleteContents(mStorageDir)); assertTrue(FileUtils.deleteContents(mStorageDir));
@@ -84,11 +96,12 @@ public class LockSettingsStorageTests extends AndroidTestCase {
// User 3 is a profile of user 0. // User 3 is a profile of user 0.
when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0)); when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager, MockLockSettingsContext context = new MockLockSettingsContext(
InstrumentationRegistry.getContext(), mockUserManager,
mock(NotificationManager.class), mock(DevicePolicyManager.class), mock(NotificationManager.class), mock(DevicePolicyManager.class),
mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class)); mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class));
mStorage = new LockSettingsStorageTestable(context, mStorage = new LockSettingsStorageTestable(context,
new File(getContext().getFilesDir(), "locksettings")); new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() { mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
@Override @Override
public void initialize(SQLiteDatabase db) { public void initialize(SQLiteDatabase db) {
@@ -97,18 +110,19 @@ public class LockSettingsStorageTests extends AndroidTestCase {
}); });
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
mStorage.closeDatabase(); mStorage.closeDatabase();
} }
@Test
public void testKeyValue_InitializeWorked() { public void testKeyValue_InitializeWorked() {
assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0)); assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
mStorage.clearCache(); mStorage.clearCache();
assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0)); assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
} }
@Test
public void testKeyValue_WriteThenRead() { public void testKeyValue_WriteThenRead() {
mStorage.writeKeyValue("key", "value", 0); mStorage.writeKeyValue("key", "value", 0);
assertEquals("value", mStorage.readKeyValue("key", "default", 0)); assertEquals("value", mStorage.readKeyValue("key", "default", 0));
@@ -116,11 +130,13 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertEquals("value", mStorage.readKeyValue("key", "default", 0)); assertEquals("value", mStorage.readKeyValue("key", "default", 0));
} }
@Test
public void testKeyValue_DefaultValue() { public void testKeyValue_DefaultValue() {
assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0)); assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0)); assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
} }
@Test
public void testKeyValue_Concurrency() { public void testKeyValue_Concurrency() {
final Object monitor = new Object(); final Object monitor = new Object();
List<Thread> threads = new ArrayList<>(); List<Thread> threads = new ArrayList<>();
@@ -160,6 +176,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0)); assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
} }
@Test
public void testKeyValue_CacheStarvedWriter() { public void testKeyValue_CacheStarvedWriter() {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
List<Thread> threads = new ArrayList<>(); List<Thread> threads = new ArrayList<>();
@@ -195,6 +212,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertEquals("Cached value didn't match stored value", storage, cached); assertEquals("Cached value didn't match stored value", storage, cached);
} }
@Test
public void testRemoveUser() { public void testRemoveUser() {
mStorage.writeKeyValue("key", "value", 0); mStorage.writeKeyValue("key", "value", 0);
writePasswordBytes(PASSWORD_0, 0); writePasswordBytes(PASSWORD_0, 0);
@@ -212,10 +230,12 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPatternBytes(PATTERN_1, 1); assertPatternBytes(PATTERN_1, 1);
} }
@Test
public void testCredential_Default() { public void testCredential_Default() {
assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE); assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
} }
@Test
public void testPassword_Write() { public void testPassword_Write() {
writePasswordBytes(PASSWORD_0, 0); writePasswordBytes(PASSWORD_0, 0);
@@ -224,6 +244,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPasswordBytes(PASSWORD_0, 0); assertPasswordBytes(PASSWORD_0, 0);
} }
@Test
public void testPassword_WriteProfileWritesParent() { public void testPassword_WriteProfileWritesParent() {
writePasswordBytes(PASSWORD_0, 1); writePasswordBytes(PASSWORD_0, 1);
writePasswordBytes(PASSWORD_1, 2); writePasswordBytes(PASSWORD_1, 2);
@@ -235,6 +256,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPasswordBytes(PASSWORD_1, 2); assertPasswordBytes(PASSWORD_1, 2);
} }
@Test
public void testLockType_WriteProfileWritesParent() { public void testLockType_WriteProfileWritesParent() {
writePasswordBytes(PASSWORD_0, 10); writePasswordBytes(PASSWORD_0, 10);
writePatternBytes(PATTERN_0, 20); writePatternBytes(PATTERN_0, 20);
@@ -250,6 +272,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
mStorage.readCredentialHash(20).type); mStorage.readCredentialHash(20).type);
} }
@Test
public void testPassword_WriteParentWritesProfile() { public void testPassword_WriteParentWritesProfile() {
writePasswordBytes(PASSWORD_0, 2); writePasswordBytes(PASSWORD_0, 2);
writePasswordBytes(PASSWORD_1, 1); writePasswordBytes(PASSWORD_1, 1);
@@ -261,6 +284,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPasswordBytes(PASSWORD_0, 2); assertPasswordBytes(PASSWORD_0, 2);
} }
@Test
public void testProfileLock_ReadWriteChildProfileLock() { public void testProfileLock_ReadWriteChildProfileLock() {
assertFalse(mStorage.hasChildProfileLock(20)); assertFalse(mStorage.hasChildProfileLock(20));
mStorage.writeChildProfileLock(20, PASSWORD_0); mStorage.writeChildProfileLock(20, PASSWORD_0);
@@ -271,6 +295,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertTrue(mStorage.hasChildProfileLock(20)); assertTrue(mStorage.hasChildProfileLock(20));
} }
@Test
public void testPattern_Write() { public void testPattern_Write() {
writePatternBytes(PATTERN_0, 0); writePatternBytes(PATTERN_0, 0);
@@ -279,6 +304,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPatternBytes(PATTERN_0, 0); assertPatternBytes(PATTERN_0, 0);
} }
@Test
public void testPattern_WriteProfileWritesParent() { public void testPattern_WriteProfileWritesParent() {
writePatternBytes(PATTERN_0, 1); writePatternBytes(PATTERN_0, 1);
writePatternBytes(PATTERN_1, 2); writePatternBytes(PATTERN_1, 2);
@@ -290,6 +316,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPatternBytes(PATTERN_1, 2); assertPatternBytes(PATTERN_1, 2);
} }
@Test
public void testPattern_WriteParentWritesProfile() { public void testPattern_WriteParentWritesProfile() {
writePatternBytes(PATTERN_1, 2); writePatternBytes(PATTERN_1, 2);
writePatternBytes(PATTERN_0, 1); writePatternBytes(PATTERN_0, 1);
@@ -301,6 +328,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPatternBytes(PATTERN_1, 2); assertPatternBytes(PATTERN_1, 2);
} }
@Test
public void testPrefetch() { public void testPrefetch() {
mStorage.writeKeyValue("key", "toBeFetched", 0); mStorage.writeKeyValue("key", "toBeFetched", 0);
writePatternBytes(PATTERN_0, 0); writePatternBytes(PATTERN_0, 0);
@@ -312,8 +340,9 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertPatternBytes(PATTERN_0, 0); assertPatternBytes(PATTERN_0, 0);
} }
@Test
public void testFileLocation_Owner() { public void testFileLocation_Owner() {
LockSettingsStorage storage = new LockSettingsStorage(getContext()); LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0)); assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0)); assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
@@ -321,27 +350,31 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0)); assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
} }
@Test
public void testFileLocation_SecondaryUser() { public void testFileLocation_SecondaryUser() {
LockSettingsStorage storage = new LockSettingsStorage(getContext()); LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1)); assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1)); assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
} }
@Test
public void testFileLocation_ProfileToSecondary() { public void testFileLocation_ProfileToSecondary() {
LockSettingsStorage storage = new LockSettingsStorage(getContext()); LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2)); assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2)); assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
} }
@Test
public void testFileLocation_ProfileToOwner() { public void testFileLocation_ProfileToOwner() {
LockSettingsStorage storage = new LockSettingsStorage(getContext()); LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3)); assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3)); assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
} }
@Test
public void testSyntheticPasswordState() { public void testSyntheticPasswordState() {
final byte[] data = {1,2,3,4}; final byte[] data = {1,2,3,4};
mStorage.writeSyntheticPasswordState(10, 1234L, "state", data); mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
@@ -352,18 +385,21 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state")); assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
} }
@Test
public void testPersistentDataBlock_unavailable() { public void testPersistentDataBlock_unavailable() {
mStorage.mPersistentDataBlock = null; mStorage.mPersistentDataBlock = null;
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
} }
@Test
public void testPersistentDataBlock_empty() { public void testPersistentDataBlock_empty() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
} }
@Test
public void testPersistentDataBlock_withData() { public void testPersistentDataBlock_withData() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
when(mStorage.mPersistentDataBlock.getFrpCredentialHandle()) when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
@@ -378,6 +414,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertArrayEquals(PAYLOAD, data.payload); assertArrayEquals(PAYLOAD, data.payload);
} }
@Test
public void testPersistentDataBlock_exception() { public void testPersistentDataBlock_exception() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
when(mStorage.mPersistentDataBlock.getFrpCredentialHandle()) when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
@@ -385,6 +422,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
} }
@Test
public void testPersistentData_serializeUnserialize() { public void testPersistentData_serializeUnserialize() {
byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID, byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD); DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
@@ -395,16 +433,19 @@ public class LockSettingsStorageTests extends AndroidTestCase {
assertArrayEquals(PAYLOAD, deserialized.payload); assertArrayEquals(PAYLOAD, deserialized.payload);
} }
@Test
public void testPersistentData_unserializeNull() { public void testPersistentData_unserializeNull() {
PersistentData deserialized = PersistentData.fromBytes(null); PersistentData deserialized = PersistentData.fromBytes(null);
assertSame(PersistentData.NONE, deserialized); assertSame(PersistentData.NONE, deserialized);
} }
@Test
public void testPersistentData_unserializeEmptyArray() { public void testPersistentData_unserializeEmptyArray() {
PersistentData deserialized = PersistentData.fromBytes(new byte[0]); PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
assertSame(PersistentData.NONE, deserialized); assertSame(PersistentData.NONE, deserialized);
} }
@Test
public void testPersistentData_unserializeInvalid() { public void testPersistentData_unserializeInvalid() {
assertNotNull(suppressAndReturnWtf(() -> { assertNotNull(suppressAndReturnWtf(() -> {
PersistentData deserialized = PersistentData.fromBytes(new byte[]{5}); PersistentData deserialized = PersistentData.fromBytes(new byte[]{5});
@@ -412,6 +453,7 @@ public class LockSettingsStorageTests extends AndroidTestCase {
})); }));
} }
@Test
public void testPersistentData_unserialize_version1() { public void testPersistentData_unserialize_version1() {
// This test ensures that we can read serialized VERSION_1 PersistentData even if we change // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
// the wire format in the future. // the wire format in the future.

View File

@@ -20,6 +20,12 @@ import android.platform.test.annotations.Presubmit;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -30,24 +36,22 @@ import java.util.Set;
@SmallTest @SmallTest
@Presubmit @Presubmit
@RunWith(AndroidJUnit4.class)
public class PasswordSlotManagerTests extends AndroidTestCase { public class PasswordSlotManagerTests extends AndroidTestCase {
PasswordSlotManagerTestable mManager; PasswordSlotManagerTestable mManager;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
mManager = new PasswordSlotManagerTestable(); mManager = new PasswordSlotManagerTestable();
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
mManager.cleanup(); mManager.cleanup();
} }
@Test
public void testBasicSlotUse() throws Exception { public void testBasicSlotUse() throws Exception {
mManager.markSlotInUse(0); mManager.markSlotInUse(0);
mManager.markSlotInUse(1); mManager.markSlotInUse(1);
@@ -64,6 +68,7 @@ public class PasswordSlotManagerTests extends AndroidTestCase {
assertEquals(expected, mManager.getUsedSlots()); assertEquals(expected, mManager.getUsedSlots());
} }
@Test
public void testMergeSlots() throws Exception { public void testMergeSlots() throws Exception {
// Add some slots from a different OS image. // Add some slots from a different OS image.
mManager.setGsiImageNumber(1); mManager.setGsiImageNumber(1);
@@ -90,6 +95,7 @@ public class PasswordSlotManagerTests extends AndroidTestCase {
assertEquals(expected, mManager.getUsedSlots()); assertEquals(expected, mManager.getUsedSlots());
} }
@Test
public void testSerialization() throws Exception { public void testSerialization() throws Exception {
mManager.markSlotInUse(0); mManager.markSlotInUse(0);
mManager.markSlotInUse(1); mManager.markSlotInUse(1);
@@ -109,6 +115,7 @@ public class PasswordSlotManagerTests extends AndroidTestCase {
assertEquals(expected, map); assertEquals(expected, map);
} }
@Test
public void testSaving() throws Exception { public void testSaving() throws Exception {
mManager.markSlotInUse(0); mManager.markSlotInUse(0);
mManager.markSlotInUse(1); mManager.markSlotInUse(1);

View File

@@ -16,16 +16,23 @@
package com.android.server.locksettings; package com.android.server.locksettings;
import static org.junit.Assert.assertEquals;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import android.test.AndroidTestCase;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.HexDump; import com.android.internal.util.HexDump;
import org.junit.Test;
import org.junit.runner.RunWith;
@SmallTest @SmallTest
@Presubmit @Presubmit
public class SP800DeriveTests extends AndroidTestCase { @RunWith(AndroidJUnit4.class)
public class SP800DeriveTests {
@Test
public void testFixedInput() throws Exception { public void testFixedInput() throws Exception {
// CAVP: https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/key-derivation // CAVP: https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/key-derivation
byte[] keyBytes = HexDump.hexStringToByteArray( byte[] keyBytes = HexDump.hexStringToByteArray(

View File

@@ -24,6 +24,12 @@ import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSW
import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_ENABLED_KEY; import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_ENABLED_KEY;
import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_HANDLE_KEY; import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_HANDLE_KEY;
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.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@@ -36,6 +42,7 @@ import android.os.UserHandle;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.VerifyCredentialResponse; import com.android.internal.widget.VerifyCredentialResponse;
@@ -43,31 +50,25 @@ import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationRe
import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationToken; import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationToken;
import com.android.server.locksettings.SyntheticPasswordManager.PasswordData; import com.android.server.locksettings.SyntheticPasswordManager.PasswordData;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* runtest frameworks-services -c com.android.server.locksettings.SyntheticPasswordTests * atest FrameworksServicesTests:SyntheticPasswordTests
*/ */
@SmallTest @SmallTest
@Presubmit @Presubmit
@RunWith(AndroidJUnit4.class)
public class SyntheticPasswordTests extends BaseLockSettingsServiceTests { public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 55}; public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 55};
public static final byte[] PAYLOAD2 = new byte[] {2, 3, -2, -3, 44, 1}; public static final byte[] PAYLOAD2 = new byte[] {2, 3, -2, -3, 44, 1};
@Override @Test
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPasswordBasedSyntheticPassword() throws RemoteException { public void testPasswordBasedSyntheticPassword() throws RemoteException {
final int USER_ID = 10; final int USER_ID = 10;
final byte[] password = "user-password".getBytes(); final byte[] password = "user-password".getBytes();
@@ -102,6 +103,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
return mService.getLong(SYNTHETIC_PASSWORD_HANDLE_KEY, 0, userId) != 0; return mService.getLong(SYNTHETIC_PASSWORD_HANDLE_KEY, 0, userId) != 0;
} }
@Test
public void testPasswordMigration() throws RemoteException { public void testPasswordMigration() throws RemoteException {
final byte[] password = "testPasswordMigration-password".getBytes(); final byte[] password = "testPasswordMigration-password".getBytes();
@@ -135,6 +137,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
mService.setLockCredential(password, type, null, quality, userId, false); mService.setLockCredential(password, type, null, quality, userId, false);
} }
@Test
public void testSyntheticPasswordChangeCredential() throws RemoteException { public void testSyntheticPasswordChangeCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordChangeCredential-password".getBytes(); final byte[] password = "testSyntheticPasswordChangeCredential-password".getBytes();
final byte[] newPassword = "testSyntheticPasswordChangeCredential-newpassword".getBytes(); final byte[] newPassword = "testSyntheticPasswordChangeCredential-newpassword".getBytes();
@@ -149,6 +152,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); assertEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
} }
@Test
public void testSyntheticPasswordVerifyCredential() throws RemoteException { public void testSyntheticPasswordVerifyCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordVerifyCredential-password".getBytes(); final byte[] password = "testSyntheticPasswordVerifyCredential-password".getBytes();
final byte[] badPassword = "testSyntheticPasswordVerifyCredential-badpassword".getBytes(); final byte[] badPassword = "testSyntheticPasswordVerifyCredential-badpassword".getBytes();
@@ -163,6 +167,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
.getResponseCode()); .getResponseCode());
} }
@Test
public void testSyntheticPasswordClearCredential() throws RemoteException { public void testSyntheticPasswordClearCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes(); final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
final byte[] badPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes(); final byte[] badPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes();
@@ -183,6 +188,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
} }
@Test
public void testSyntheticPasswordChangeCredentialKeepsAuthSecret() throws RemoteException { public void testSyntheticPasswordChangeCredentialKeepsAuthSecret() throws RemoteException {
final byte[] password = final byte[] password =
"testSyntheticPasswordChangeCredentialKeepsAuthSecret-password".getBytes(); "testSyntheticPasswordChangeCredentialKeepsAuthSecret-password".getBytes();
@@ -202,6 +208,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertEquals(1, secret.getAllValues().stream().distinct().count()); assertEquals(1, secret.getAllValues().stream().distinct().count());
} }
@Test
public void testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret() throws RemoteException { public void testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret() throws RemoteException {
final byte[] password = final byte[] password =
"testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret-password".getBytes(); "testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret-password".getBytes();
@@ -216,6 +223,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class)); verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class));
} }
@Test
public void testSecondaryUserDoesNotPassAuthSecret() throws RemoteException { public void testSecondaryUserDoesNotPassAuthSecret() throws RemoteException {
final byte[] password = "testSecondaryUserDoesNotPassAuthSecret-password".getBytes(); final byte[] password = "testSecondaryUserDoesNotPassAuthSecret-password".getBytes();
@@ -226,6 +234,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class)); verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
} }
@Test
public void testNoSyntheticPasswordOrCredentialDoesNotPassAuthSecret() throws RemoteException { public void testNoSyntheticPasswordOrCredentialDoesNotPassAuthSecret() throws RemoteException {
// Setting null doesn't create a synthetic password // Setting null doesn't create a synthetic password
initializeCredentialUnderSP(null, PRIMARY_USER_ID); initializeCredentialUnderSP(null, PRIMARY_USER_ID);
@@ -236,6 +245,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class)); verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
} }
@Test
public void testSyntheticPasswordAndCredentialDoesNotPassAuthSecret() throws RemoteException { public void testSyntheticPasswordAndCredentialDoesNotPassAuthSecret() throws RemoteException {
final byte[] password = "passwordForASyntheticPassword".getBytes(); final byte[] password = "passwordForASyntheticPassword".getBytes();
initializeCredentialUnderSP(password, PRIMARY_USER_ID); initializeCredentialUnderSP(password, PRIMARY_USER_ID);
@@ -246,6 +256,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class)); verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
} }
@Test
public void testSyntheticPasswordButNoCredentialPassesAuthSecret() throws RemoteException { public void testSyntheticPasswordButNoCredentialPassesAuthSecret() throws RemoteException {
final byte[] password = "getASyntheticPassword".getBytes(); final byte[] password = "getASyntheticPassword".getBytes();
initializeCredentialUnderSP(password, PRIMARY_USER_ID); initializeCredentialUnderSP(password, PRIMARY_USER_ID);
@@ -258,6 +269,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class)); verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class));
} }
@Test
public void testManagedProfileUnifiedChallengeMigration() throws RemoteException { public void testManagedProfileUnifiedChallengeMigration() throws RemoteException {
final byte[] UnifiedPassword = "testManagedProfileUnifiedChallengeMigration-pwd".getBytes(); final byte[] UnifiedPassword = "testManagedProfileUnifiedChallengeMigration-pwd".getBytes();
disableSyntheticPassword(); disableSyntheticPassword();
@@ -292,6 +304,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID)); assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID));
} }
@Test
public void testManagedProfileSeparateChallengeMigration() throws RemoteException { public void testManagedProfileSeparateChallengeMigration() throws RemoteException {
final byte[] primaryPassword = final byte[] primaryPassword =
"testManagedProfileSeparateChallengeMigration-primary".getBytes(); "testManagedProfileSeparateChallengeMigration-primary".getBytes();
@@ -336,6 +349,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID)); assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID));
} }
@Test
public void testTokenBasedResetPassword() throws RemoteException { public void testTokenBasedResetPassword() throws RemoteException {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes(); final byte[] pattern = "123654".getBytes();
@@ -368,6 +382,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID)); assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
} }
@Test
public void testTokenBasedClearPassword() throws RemoteException { public void testTokenBasedClearPassword() throws RemoteException {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes(); final byte[] pattern = "123654".getBytes();
@@ -394,6 +409,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID)); assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
} }
@Test
public void testTokenBasedResetPasswordAfterCredentialChanges() throws RemoteException { public void testTokenBasedResetPasswordAfterCredentialChanges() throws RemoteException {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes(); final byte[] pattern = "123654".getBytes();
@@ -422,6 +438,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID)); assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
} }
@Test
public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNeedsMigration() public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNeedsMigration()
throws RemoteException { throws RemoteException {
final String token = "some-high-entropy-secure-token"; final String token = "some-high-entropy-secure-token";
@@ -432,6 +449,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertTrue(hasSyntheticPassword(PRIMARY_USER_ID)); assertTrue(hasSyntheticPassword(PRIMARY_USER_ID));
} }
@Test
public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNoMigration() public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNoMigration()
throws RemoteException { throws RemoteException {
final String token = "some-high-entropy-secure-token"; final String token = "some-high-entropy-secure-token";
@@ -442,6 +460,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertTrue(hasSyntheticPassword(PRIMARY_USER_ID)); assertTrue(hasSyntheticPassword(PRIMARY_USER_ID));
} }
@Test
public void testEscrowTokenActivatedLaterWithUserPasswordNeedsMigration() public void testEscrowTokenActivatedLaterWithUserPasswordNeedsMigration()
throws RemoteException { throws RemoteException {
final byte[] token = "some-high-entropy-secure-token".getBytes(); final byte[] token = "some-high-entropy-secure-token".getBytes();
@@ -463,6 +482,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertTrue(mLocalService.isEscrowTokenActive(handle, PRIMARY_USER_ID)); assertTrue(mLocalService.isEscrowTokenActive(handle, PRIMARY_USER_ID));
} }
@Test
public void testSetLockCredentialWithTokenFailsWithoutLockScreen() throws Exception { public void testSetLockCredentialWithTokenFailsWithoutLockScreen() throws Exception {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes(); final byte[] pattern = "123654".getBytes();
@@ -494,6 +514,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertFalse(mService.havePattern(PRIMARY_USER_ID)); assertFalse(mService.havePattern(PRIMARY_USER_ID));
} }
@Test
public void testgetHashFactorPrimaryUser() throws RemoteException { public void testgetHashFactorPrimaryUser() throws RemoteException {
final byte[] password = "password".getBytes(); final byte[] password = "password".getBytes();
mService.setLockCredential(password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null, mService.setLockCredential(password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null,
@@ -509,6 +530,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(hashFactor, newHashFactor); assertArrayEquals(hashFactor, newHashFactor);
} }
@Test
public void testgetHashFactorManagedProfileUnifiedChallenge() throws RemoteException { public void testgetHashFactorManagedProfileUnifiedChallenge() throws RemoteException {
final byte[] pattern = "1236".getBytes(); final byte[] pattern = "1236".getBytes();
mService.setLockCredential(pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN, mService.setLockCredential(pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
@@ -517,6 +539,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertNotNull(mService.getHashFactor(null, MANAGED_PROFILE_USER_ID)); assertNotNull(mService.getHashFactor(null, MANAGED_PROFILE_USER_ID));
} }
@Test
public void testgetHashFactorManagedProfileSeparateChallenge() throws RemoteException { public void testgetHashFactorManagedProfileSeparateChallenge() throws RemoteException {
final byte[] primaryPassword = "primary".getBytes(); final byte[] primaryPassword = "primary".getBytes();
final byte[] profilePassword = "profile".getBytes(); final byte[] profilePassword = "profile".getBytes();
@@ -527,6 +550,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertNotNull(mService.getHashFactor(profilePassword, MANAGED_PROFILE_USER_ID)); assertNotNull(mService.getHashFactor(profilePassword, MANAGED_PROFILE_USER_ID));
} }
@Test
public void testPasswordData_serializeDeserialize() { public void testPasswordData_serializeDeserialize() {
PasswordData data = new PasswordData(); PasswordData data = new PasswordData();
data.scryptN = 11; data.scryptN = 11;
@@ -546,6 +570,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(PAYLOAD2, deserialized.passwordHandle); assertArrayEquals(PAYLOAD2, deserialized.passwordHandle);
} }
@Test
public void testPasswordData_deserialize() { public void testPasswordData_deserialize() {
// Test that we can deserialize existing PasswordData and don't inadvertently change the // Test that we can deserialize existing PasswordData and don't inadvertently change the
// wire format. // wire format.
@@ -569,6 +594,7 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
assertArrayEquals(PAYLOAD2, deserialized.passwordHandle); assertArrayEquals(PAYLOAD2, deserialized.passwordHandle);
} }
@Test
public void testGsiDisablesAuthSecret() throws RemoteException { public void testGsiDisablesAuthSecret() throws RemoteException {
mGsiService.setIsGsiRunning(true); mGsiService.setIsGsiRunning(true);

View File

@@ -3,15 +3,18 @@ package com.android.server.locksettings;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.runner.RunWith;
@SmallTest @SmallTest
@Presubmit @Presubmit
@RunWith(AndroidJUnit4.class)
public class WeaverBasedSyntheticPasswordTests extends SyntheticPasswordTests { public class WeaverBasedSyntheticPasswordTests extends SyntheticPasswordTests {
@Override @Before
protected void setUp() throws Exception { public void enableWeaver() throws Exception {
super.setUp();
mSpManager.enableWeaver(); mSpManager.enableWeaver();
} }
} }