Convert LockSettingsService tests to JUnit4
In preparation for adding more tests to LockSettingsService, convert the
existing JUnit3 tests to JUnit4 like the tests in the
recoverablekeystore subdirectory.
(cherry-picked from commit e7178a2358a457922ecb35c5f418903a3de363ab)
Test: atest FrameworksServicesTests:CachedSyntheticPasswordTests \
FrameworksServicesTests:LockSettingsServiceTests \
FrameworksServicesTests:LockSettingsShellCommandTest \
FrameworksServicesTests:LockSettingsStorageTests \
FrameworksServicesTests:LockscreenFrpTest \
FrameworksServicesTests:PasswordSlotManagerTests \
FrameworksServicesTests:SP800DeriveTests
Test: atest FrameworksServicesTests:SyntheticPasswordTests \
FrameworksServicesTests:WeaverBasedSyntheticPasswordTests
Bug: 145144304
Merged-In: Iaf280718d4c28966b60c76dda90a4674a24f26c6
Change-Id: Iaf280718d4c28966b60c76dda90a4674a24f26c6
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/BaseLockSettingsServiceTests.java b/services/tests/servicestests/src/com/android/server/locksettings/BaseLockSettingsServiceTests.java
index 1f5ebe4..842cdd5 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/BaseLockSettingsServiceTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/BaseLockSettingsServiceTests.java
@@ -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.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.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.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 @@
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 @@
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 @@
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;
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/CachedSyntheticPasswordTests.java b/services/tests/servicestests/src/com/android/server/locksettings/CachedSyntheticPasswordTests.java
index d2a9145..6616c96 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/CachedSyntheticPasswordTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/CachedSyntheticPasswordTests.java
@@ -20,6 +20,7 @@
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.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 @@
*/
@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 @@
.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 @@
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 @@
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 @@
assertEquals(1, secret.getAllValues().stream().distinct().count());
}
+ @Test
public void testUntrustedCredentialChangeBlockedIfSpNotCached() throws RemoteException {
final byte[] password =
"testUntrustedCredentialChangeBlockedIfSpNotCached-password".getBytes();
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTests.java b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTests.java
index 67d6eda..c0f27ff 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTests.java
@@ -25,6 +25,12 @@
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.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 @@
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 @@
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 @@
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 @@
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 @@
.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 @@
.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 @@
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 @@
eq(CREDENTIAL_TYPE_PASSWORD), any(), eq(MANAGED_PROFILE_USER_ID));
}
+ @Test
public void
testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_updatesBothCredentials()
throws Exception {
@@ -313,6 +328,7 @@
CREDENTIAL_TYPE_PASSWORD, newCredential, MANAGED_PROFILE_USER_ID);
}
+ @Test
public void
testSetLockCredential_forPrimaryUserWithUnifiedChallengeProfile_removesBothCredentials()
throws Exception {
@@ -335,6 +351,7 @@
.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 @@
CREDENTIAL_TYPE_PASSWORD, profilePassword, MANAGED_PROFILE_USER_ID);
}
+ @Test
public void
testSetLockCredential_forSeparateToUnifiedChallengeProfile_doesNotSendRandomCredential()
throws Exception {
@@ -379,6 +397,7 @@
.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 @@
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 @@
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);
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsShellCommandTest.java b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsShellCommandTest.java
index c00d33b..b959126 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsShellCommandTest.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsShellCommandTest.java
@@ -23,7 +23,7 @@
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;
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsStorageTests.java b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsStorageTests.java
index 18453aa..a1f423e 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsStorageTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsStorageTests.java
@@ -16,7 +16,13 @@
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.when;
@@ -31,18 +37,24 @@
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.platform.test.annotations.Presubmit;
-import android.test.AndroidTestCase;
import android.util.Log;
import android.util.Log.TerribleFailure;
import android.util.Log.TerribleFailureHandler;
+import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
import com.android.internal.widget.LockPatternUtils;
import com.android.server.PersistentDataBlockManagerInternal;
import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
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.util.ArrayList;
import java.util.Arrays;
@@ -50,11 +62,12 @@
import java.util.concurrent.CountDownLatch;
/**
- * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests
+ * atest FrameworksServicesTests:LockSettingsStorageTests
*/
@SmallTest
@Presubmit
-public class LockSettingsStorageTests extends AndroidTestCase {
+@RunWith(AndroidJUnit4.class)
+public class LockSettingsStorageTests {
private static final int SOME_USER_ID = 1034;
private final byte[] PASSWORD_0 = "thepassword0".getBytes();
private final byte[] PASSWORD_1 = "password1".getBytes();
@@ -68,11 +81,10 @@
private File mDb;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- mStorageDir = new File(getContext().getFilesDir(), "locksettings");
- mDb = getContext().getDatabasePath("locksettings.db");
+ @Before
+ public void setUp() throws Exception {
+ mStorageDir = new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings");
+ mDb = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
assertTrue(FileUtils.deleteContents(mStorageDir));
@@ -84,11 +96,12 @@
// User 3 is a profile of user 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(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class));
mStorage = new LockSettingsStorageTestable(context,
- new File(getContext().getFilesDir(), "locksettings"));
+ new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
@Override
public void initialize(SQLiteDatabase db) {
@@ -97,18 +110,19 @@
});
}
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
mStorage.closeDatabase();
}
+ @Test
public void testKeyValue_InitializeWorked() {
assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
mStorage.clearCache();
assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
}
+ @Test
public void testKeyValue_WriteThenRead() {
mStorage.writeKeyValue("key", "value", 0);
assertEquals("value", mStorage.readKeyValue("key", "default", 0));
@@ -116,11 +130,13 @@
assertEquals("value", mStorage.readKeyValue("key", "default", 0));
}
+ @Test
public void testKeyValue_DefaultValue() {
assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
}
+ @Test
public void testKeyValue_Concurrency() {
final Object monitor = new Object();
List<Thread> threads = new ArrayList<>();
@@ -160,6 +176,7 @@
assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
}
+ @Test
public void testKeyValue_CacheStarvedWriter() {
final CountDownLatch latch = new CountDownLatch(1);
List<Thread> threads = new ArrayList<>();
@@ -195,6 +212,7 @@
assertEquals("Cached value didn't match stored value", storage, cached);
}
+ @Test
public void testRemoveUser() {
mStorage.writeKeyValue("key", "value", 0);
writePasswordBytes(PASSWORD_0, 0);
@@ -212,10 +230,12 @@
assertPatternBytes(PATTERN_1, 1);
}
+ @Test
public void testCredential_Default() {
assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
}
+ @Test
public void testPassword_Write() {
writePasswordBytes(PASSWORD_0, 0);
@@ -224,6 +244,7 @@
assertPasswordBytes(PASSWORD_0, 0);
}
+ @Test
public void testPassword_WriteProfileWritesParent() {
writePasswordBytes(PASSWORD_0, 1);
writePasswordBytes(PASSWORD_1, 2);
@@ -235,6 +256,7 @@
assertPasswordBytes(PASSWORD_1, 2);
}
+ @Test
public void testLockType_WriteProfileWritesParent() {
writePasswordBytes(PASSWORD_0, 10);
writePatternBytes(PATTERN_0, 20);
@@ -250,6 +272,7 @@
mStorage.readCredentialHash(20).type);
}
+ @Test
public void testPassword_WriteParentWritesProfile() {
writePasswordBytes(PASSWORD_0, 2);
writePasswordBytes(PASSWORD_1, 1);
@@ -261,6 +284,7 @@
assertPasswordBytes(PASSWORD_0, 2);
}
+ @Test
public void testProfileLock_ReadWriteChildProfileLock() {
assertFalse(mStorage.hasChildProfileLock(20));
mStorage.writeChildProfileLock(20, PASSWORD_0);
@@ -271,6 +295,7 @@
assertTrue(mStorage.hasChildProfileLock(20));
}
+ @Test
public void testPattern_Write() {
writePatternBytes(PATTERN_0, 0);
@@ -279,6 +304,7 @@
assertPatternBytes(PATTERN_0, 0);
}
+ @Test
public void testPattern_WriteProfileWritesParent() {
writePatternBytes(PATTERN_0, 1);
writePatternBytes(PATTERN_1, 2);
@@ -290,6 +316,7 @@
assertPatternBytes(PATTERN_1, 2);
}
+ @Test
public void testPattern_WriteParentWritesProfile() {
writePatternBytes(PATTERN_1, 2);
writePatternBytes(PATTERN_0, 1);
@@ -301,6 +328,7 @@
assertPatternBytes(PATTERN_1, 2);
}
+ @Test
public void testPrefetch() {
mStorage.writeKeyValue("key", "toBeFetched", 0);
writePatternBytes(PATTERN_0, 0);
@@ -312,8 +340,9 @@
assertPatternBytes(PATTERN_0, 0);
}
+ @Test
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/password.key", storage.getLegacyLockPasswordFilename(0));
@@ -321,27 +350,31 @@
assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
}
+ @Test
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.password.key", storage.getLockPasswordFilename(1));
}
+ @Test
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.password.key", storage.getLockPasswordFilename(2));
}
+ @Test
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.password.key", storage.getLockPasswordFilename(3));
}
+ @Test
public void testSyntheticPasswordState() {
final byte[] data = {1,2,3,4};
mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
@@ -352,18 +385,21 @@
assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
}
+ @Test
public void testPersistentDataBlock_unavailable() {
mStorage.mPersistentDataBlock = null;
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
}
+ @Test
public void testPersistentDataBlock_empty() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
}
+ @Test
public void testPersistentDataBlock_withData() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
@@ -378,6 +414,7 @@
assertArrayEquals(PAYLOAD, data.payload);
}
+ @Test
public void testPersistentDataBlock_exception() {
mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
@@ -385,6 +422,7 @@
assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
}
+ @Test
public void testPersistentData_serializeUnserialize() {
byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
@@ -395,16 +433,19 @@
assertArrayEquals(PAYLOAD, deserialized.payload);
}
+ @Test
public void testPersistentData_unserializeNull() {
PersistentData deserialized = PersistentData.fromBytes(null);
assertSame(PersistentData.NONE, deserialized);
}
+ @Test
public void testPersistentData_unserializeEmptyArray() {
PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
assertSame(PersistentData.NONE, deserialized);
}
+ @Test
public void testPersistentData_unserializeInvalid() {
assertNotNull(suppressAndReturnWtf(() -> {
PersistentData deserialized = PersistentData.fromBytes(new byte[]{5});
@@ -412,6 +453,7 @@
}));
}
+ @Test
public void testPersistentData_unserialize_version1() {
// This test ensures that we can read serialized VERSION_1 PersistentData even if we change
// the wire format in the future.
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/PasswordSlotManagerTests.java b/services/tests/servicestests/src/com/android/server/locksettings/PasswordSlotManagerTests.java
index 31526b5..0f24fb2 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/PasswordSlotManagerTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/PasswordSlotManagerTests.java
@@ -20,6 +20,12 @@
import android.test.AndroidTestCase;
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.ByteArrayOutputStream;
@@ -30,24 +36,22 @@
@SmallTest
@Presubmit
+@RunWith(AndroidJUnit4.class)
public class PasswordSlotManagerTests extends AndroidTestCase {
PasswordSlotManagerTestable mManager;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
+ @Before
+ public void setUp() throws Exception {
mManager = new PasswordSlotManagerTestable();
}
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
-
+ @After
+ public void tearDown() throws Exception {
mManager.cleanup();
}
+ @Test
public void testBasicSlotUse() throws Exception {
mManager.markSlotInUse(0);
mManager.markSlotInUse(1);
@@ -64,6 +68,7 @@
assertEquals(expected, mManager.getUsedSlots());
}
+ @Test
public void testMergeSlots() throws Exception {
// Add some slots from a different OS image.
mManager.setGsiImageNumber(1);
@@ -90,6 +95,7 @@
assertEquals(expected, mManager.getUsedSlots());
}
+ @Test
public void testSerialization() throws Exception {
mManager.markSlotInUse(0);
mManager.markSlotInUse(1);
@@ -109,6 +115,7 @@
assertEquals(expected, map);
}
+ @Test
public void testSaving() throws Exception {
mManager.markSlotInUse(0);
mManager.markSlotInUse(1);
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/SP800DeriveTests.java b/services/tests/servicestests/src/com/android/server/locksettings/SP800DeriveTests.java
index 29d0fc1..89b7ec8 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/SP800DeriveTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/SP800DeriveTests.java
@@ -16,16 +16,23 @@
package com.android.server.locksettings;
+import static org.junit.Assert.assertEquals;
+
import android.platform.test.annotations.Presubmit;
-import android.test.AndroidTestCase;
import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.HexDump;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
@SmallTest
@Presubmit
-public class SP800DeriveTests extends AndroidTestCase {
+@RunWith(AndroidJUnit4.class)
+public class SP800DeriveTests {
+ @Test
public void testFixedInput() throws Exception {
// CAVP: https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/key-derivation
byte[] keyBytes = HexDump.hexStringToByteArray(
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java b/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java
index 1cd590c..d9b1320 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java
@@ -24,6 +24,12 @@
import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_ENABLED_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.atLeastOnce;
import static org.mockito.Mockito.never;
@@ -36,6 +42,7 @@
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;
@@ -43,31 +50,25 @@
import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationToken;
import com.android.server.locksettings.SyntheticPasswordManager.PasswordData;
+import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
/**
- * runtest frameworks-services -c com.android.server.locksettings.SyntheticPasswordTests
+ * atest FrameworksServicesTests:SyntheticPasswordTests
*/
@SmallTest
@Presubmit
+@RunWith(AndroidJUnit4.class)
public class SyntheticPasswordTests extends BaseLockSettingsServiceTests {
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};
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
+ @Test
public void testPasswordBasedSyntheticPassword() throws RemoteException {
final int USER_ID = 10;
final byte[] password = "user-password".getBytes();
@@ -102,6 +103,7 @@
return mService.getLong(SYNTHETIC_PASSWORD_HANDLE_KEY, 0, userId) != 0;
}
+ @Test
public void testPasswordMigration() throws RemoteException {
final byte[] password = "testPasswordMigration-password".getBytes();
@@ -135,6 +137,7 @@
mService.setLockCredential(password, type, null, quality, userId, false);
}
+ @Test
public void testSyntheticPasswordChangeCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordChangeCredential-password".getBytes();
final byte[] newPassword = "testSyntheticPasswordChangeCredential-newpassword".getBytes();
@@ -149,6 +152,7 @@
assertEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
}
+ @Test
public void testSyntheticPasswordVerifyCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordVerifyCredential-password".getBytes();
final byte[] badPassword = "testSyntheticPasswordVerifyCredential-badpassword".getBytes();
@@ -163,6 +167,7 @@
.getResponseCode());
}
+ @Test
public void testSyntheticPasswordClearCredential() throws RemoteException {
final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes();
final byte[] badPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes();
@@ -183,6 +188,7 @@
assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID));
}
+ @Test
public void testSyntheticPasswordChangeCredentialKeepsAuthSecret() throws RemoteException {
final byte[] password =
"testSyntheticPasswordChangeCredentialKeepsAuthSecret-password".getBytes();
@@ -202,6 +208,7 @@
assertEquals(1, secret.getAllValues().stream().distinct().count());
}
+ @Test
public void testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret() throws RemoteException {
final byte[] password =
"testSyntheticPasswordVerifyPassesPrimaryUserAuthSecret-password".getBytes();
@@ -216,6 +223,7 @@
verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class));
}
+ @Test
public void testSecondaryUserDoesNotPassAuthSecret() throws RemoteException {
final byte[] password = "testSecondaryUserDoesNotPassAuthSecret-password".getBytes();
@@ -226,6 +234,7 @@
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
}
+ @Test
public void testNoSyntheticPasswordOrCredentialDoesNotPassAuthSecret() throws RemoteException {
// Setting null doesn't create a synthetic password
initializeCredentialUnderSP(null, PRIMARY_USER_ID);
@@ -236,6 +245,7 @@
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
}
+ @Test
public void testSyntheticPasswordAndCredentialDoesNotPassAuthSecret() throws RemoteException {
final byte[] password = "passwordForASyntheticPassword".getBytes();
initializeCredentialUnderSP(password, PRIMARY_USER_ID);
@@ -246,6 +256,7 @@
verify(mAuthSecretService, never()).primaryUserCredential(any(ArrayList.class));
}
+ @Test
public void testSyntheticPasswordButNoCredentialPassesAuthSecret() throws RemoteException {
final byte[] password = "getASyntheticPassword".getBytes();
initializeCredentialUnderSP(password, PRIMARY_USER_ID);
@@ -258,6 +269,7 @@
verify(mAuthSecretService).primaryUserCredential(any(ArrayList.class));
}
+ @Test
public void testManagedProfileUnifiedChallengeMigration() throws RemoteException {
final byte[] UnifiedPassword = "testManagedProfileUnifiedChallengeMigration-pwd".getBytes();
disableSyntheticPassword();
@@ -292,6 +304,7 @@
assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID));
}
+ @Test
public void testManagedProfileSeparateChallengeMigration() throws RemoteException {
final byte[] primaryPassword =
"testManagedProfileSeparateChallengeMigration-primary".getBytes();
@@ -336,6 +349,7 @@
assertTrue(hasSyntheticPassword(MANAGED_PROFILE_USER_ID));
}
+ @Test
public void testTokenBasedResetPassword() throws RemoteException {
final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes();
@@ -368,6 +382,7 @@
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
}
+ @Test
public void testTokenBasedClearPassword() throws RemoteException {
final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes();
@@ -394,6 +409,7 @@
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
}
+ @Test
public void testTokenBasedResetPasswordAfterCredentialChanges() throws RemoteException {
final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes();
@@ -422,6 +438,7 @@
assertArrayEquals(storageKey, mStorageManager.getUserUnlockToken(PRIMARY_USER_ID));
}
+ @Test
public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNeedsMigration()
throws RemoteException {
final String token = "some-high-entropy-secure-token";
@@ -432,6 +449,7 @@
assertTrue(hasSyntheticPassword(PRIMARY_USER_ID));
}
+ @Test
public void testEscrowTokenActivatedImmediatelyIfNoUserPasswordNoMigration()
throws RemoteException {
final String token = "some-high-entropy-secure-token";
@@ -442,6 +460,7 @@
assertTrue(hasSyntheticPassword(PRIMARY_USER_ID));
}
+ @Test
public void testEscrowTokenActivatedLaterWithUserPasswordNeedsMigration()
throws RemoteException {
final byte[] token = "some-high-entropy-secure-token".getBytes();
@@ -463,6 +482,7 @@
assertTrue(mLocalService.isEscrowTokenActive(handle, PRIMARY_USER_ID));
}
+ @Test
public void testSetLockCredentialWithTokenFailsWithoutLockScreen() throws Exception {
final byte[] password = "password".getBytes();
final byte[] pattern = "123654".getBytes();
@@ -494,6 +514,7 @@
assertFalse(mService.havePattern(PRIMARY_USER_ID));
}
+ @Test
public void testgetHashFactorPrimaryUser() throws RemoteException {
final byte[] password = "password".getBytes();
mService.setLockCredential(password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null,
@@ -509,6 +530,7 @@
assertArrayEquals(hashFactor, newHashFactor);
}
+ @Test
public void testgetHashFactorManagedProfileUnifiedChallenge() throws RemoteException {
final byte[] pattern = "1236".getBytes();
mService.setLockCredential(pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
@@ -517,6 +539,7 @@
assertNotNull(mService.getHashFactor(null, MANAGED_PROFILE_USER_ID));
}
+ @Test
public void testgetHashFactorManagedProfileSeparateChallenge() throws RemoteException {
final byte[] primaryPassword = "primary".getBytes();
final byte[] profilePassword = "profile".getBytes();
@@ -527,6 +550,7 @@
assertNotNull(mService.getHashFactor(profilePassword, MANAGED_PROFILE_USER_ID));
}
+ @Test
public void testPasswordData_serializeDeserialize() {
PasswordData data = new PasswordData();
data.scryptN = 11;
@@ -546,6 +570,7 @@
assertArrayEquals(PAYLOAD2, deserialized.passwordHandle);
}
+ @Test
public void testPasswordData_deserialize() {
// Test that we can deserialize existing PasswordData and don't inadvertently change the
// wire format.
@@ -569,6 +594,7 @@
assertArrayEquals(PAYLOAD2, deserialized.passwordHandle);
}
+ @Test
public void testGsiDisablesAuthSecret() throws RemoteException {
mGsiService.setIsGsiRunning(true);
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/WeaverBasedSyntheticPasswordTests.java b/services/tests/servicestests/src/com/android/server/locksettings/WeaverBasedSyntheticPasswordTests.java
index abbf016..a3ac515 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/WeaverBasedSyntheticPasswordTests.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/WeaverBasedSyntheticPasswordTests.java
@@ -3,15 +3,18 @@
import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
@SmallTest
@Presubmit
+@RunWith(AndroidJUnit4.class)
public class WeaverBasedSyntheticPasswordTests extends SyntheticPasswordTests {
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void enableWeaver() throws Exception {
mSpManager.enableWeaver();
}
-
}