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/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.