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

Commit 7d44e2f1 authored by Eric Biggers's avatar Eric Biggers
Browse files

Convert LockscreenCredentialTest to JUnit4

Convert LockscreenCredentialTest from the JUnit3-based AndroidTestCase,
which is deprecated, to JUnit4.

Bug: 219511761
Bug: 232900169
Bug: 243881358
Test: atest LockscreenCredentialTest
Change-Id: Id8da1606b1f758ff91f67c25d37a36f36a72c2bf
Merged-In: Id8da1606b1f758ff91f67c25d37a36f36a72c2bf
(cherry picked from commit b63430f2)
parent 397f5416
Loading
Loading
Loading
Loading
+30 −7
Original line number Original line Diff line number Diff line
@@ -16,16 +16,27 @@


package com.android.internal.widget;
package com.android.internal.widget;



import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;


import android.test.AndroidTestCase;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


import java.util.Arrays;
import androidx.test.ext.junit.runners.AndroidJUnit4;


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

import java.util.Arrays;


public class LockscreenCredentialTest extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class LockscreenCredentialTest {


    @Test
    public void testEmptyCredential() {
    public void testEmptyCredential() {
        LockscreenCredential empty = LockscreenCredential.createNone();
        LockscreenCredential empty = LockscreenCredential.createNone();


@@ -38,30 +49,33 @@ public class LockscreenCredentialTest extends AndroidTestCase {
        assertFalse(empty.isPattern());
        assertFalse(empty.isPattern());
    }
    }


    @Test
    public void testPinCredential() {
    public void testPinCredential() {
        LockscreenCredential pin = LockscreenCredential.createPin("3456");
        LockscreenCredential pin = LockscreenCredential.createPin("3456");


        assertTrue(pin.isPin());
        assertTrue(pin.isPin());
        assertEquals(4, pin.size());
        assertEquals(4, pin.size());
        assertTrue(Arrays.equals("3456".getBytes(), pin.getCredential()));
        assertArrayEquals("3456".getBytes(), pin.getCredential());


        assertFalse(pin.isNone());
        assertFalse(pin.isNone());
        assertFalse(pin.isPassword());
        assertFalse(pin.isPassword());
        assertFalse(pin.isPattern());
        assertFalse(pin.isPattern());
    }
    }


    @Test
    public void testPasswordCredential() {
    public void testPasswordCredential() {
        LockscreenCredential password = LockscreenCredential.createPassword("password");
        LockscreenCredential password = LockscreenCredential.createPassword("password");


        assertTrue(password.isPassword());
        assertTrue(password.isPassword());
        assertEquals(8, password.size());
        assertEquals(8, password.size());
        assertTrue(Arrays.equals("password".getBytes(), password.getCredential()));
        assertArrayEquals("password".getBytes(), password.getCredential());


        assertFalse(password.isNone());
        assertFalse(password.isNone());
        assertFalse(password.isPin());
        assertFalse(password.isPin());
        assertFalse(password.isPattern());
        assertFalse(password.isPattern());
    }
    }


    @Test
    public void testPatternCredential() {
    public void testPatternCredential() {
        LockscreenCredential pattern = LockscreenCredential.createPattern(Arrays.asList(
        LockscreenCredential pattern = LockscreenCredential.createPattern(Arrays.asList(
                LockPatternView.Cell.of(0, 0),
                LockPatternView.Cell.of(0, 0),
@@ -73,13 +87,14 @@ public class LockscreenCredentialTest extends AndroidTestCase {


        assertTrue(pattern.isPattern());
        assertTrue(pattern.isPattern());
        assertEquals(5, pattern.size());
        assertEquals(5, pattern.size());
        assertTrue(Arrays.equals("12369".getBytes(), pattern.getCredential()));
        assertArrayEquals("12369".getBytes(), pattern.getCredential());


        assertFalse(pattern.isNone());
        assertFalse(pattern.isNone());
        assertFalse(pattern.isPin());
        assertFalse(pattern.isPin());
        assertFalse(pattern.isPassword());
        assertFalse(pattern.isPassword());
    }
    }


    @Test
    public void testPasswordOrNoneCredential() {
    public void testPasswordOrNoneCredential() {
        assertEquals(LockscreenCredential.createNone(),
        assertEquals(LockscreenCredential.createNone(),
                LockscreenCredential.createPasswordOrNone(null));
                LockscreenCredential.createPasswordOrNone(null));
@@ -89,6 +104,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                LockscreenCredential.createPasswordOrNone("abcd"));
                LockscreenCredential.createPasswordOrNone("abcd"));
    }
    }


    @Test
    public void testPinOrNoneCredential() {
    public void testPinOrNoneCredential() {
        assertEquals(LockscreenCredential.createNone(),
        assertEquals(LockscreenCredential.createNone(),
                LockscreenCredential.createPinOrNone(null));
                LockscreenCredential.createPinOrNone(null));
@@ -98,6 +114,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                LockscreenCredential.createPinOrNone("1357"));
                LockscreenCredential.createPinOrNone("1357"));
    }
    }


    @Test
    public void testSanitize() {
    public void testSanitize() {
        LockscreenCredential password = LockscreenCredential.createPassword("password");
        LockscreenCredential password = LockscreenCredential.createPassword("password");
        password.zeroize();
        password.zeroize();
@@ -128,6 +145,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
        } catch (IllegalStateException expected) { }
        } catch (IllegalStateException expected) { }
    }
    }


    @Test
    public void testEquals() {
    public void testEquals() {
        assertEquals(LockscreenCredential.createNone(), LockscreenCredential.createNone());
        assertEquals(LockscreenCredential.createNone(), LockscreenCredential.createNone());
        assertEquals(LockscreenCredential.createPassword("1234"),
        assertEquals(LockscreenCredential.createPassword("1234"),
@@ -164,6 +182,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                LockscreenCredential.createPin("5678"));
                LockscreenCredential.createPin("5678"));
    }
    }


    @Test
    public void testDuplicate() {
    public void testDuplicate() {
        LockscreenCredential credential;
        LockscreenCredential credential;


@@ -177,6 +196,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
        assertEquals(credential, credential.duplicate());
        assertEquals(credential, credential.duplicate());
    }
    }


    @Test
    public void testPasswordToHistoryHash() {
    public void testPasswordToHistoryHash() {
        String password = "1234";
        String password = "1234";
        LockscreenCredential credential = LockscreenCredential.createPassword(password);
        LockscreenCredential credential = LockscreenCredential.createPassword(password);
@@ -193,6 +213,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                .isEqualTo(expectedHash);
                .isEqualTo(expectedHash);
    }
    }


    @Test
    public void testPasswordToHistoryHashInvalidInput() {
    public void testPasswordToHistoryHashInvalidInput() {
        String password = "1234";
        String password = "1234";
        LockscreenCredential credential = LockscreenCredential.createPassword(password);
        LockscreenCredential credential = LockscreenCredential.createPassword(password);
@@ -221,6 +242,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                .isNull();
                .isNull();
    }
    }


    @Test
    public void testLegacyPasswordToHash() {
    public void testLegacyPasswordToHash() {
        String password = "1234";
        String password = "1234";
        String salt = "6d5331dd120077a0";
        String salt = "6d5331dd120077a0";
@@ -233,6 +255,7 @@ public class LockscreenCredentialTest extends AndroidTestCase {
                .isEqualTo(expectedHash);
                .isEqualTo(expectedHash);
    }
    }


    @Test
    public void testLegacyPasswordToHashInvalidInput() {
    public void testLegacyPasswordToHashInvalidInput() {
        String password = "1234";
        String password = "1234";
        String salt = "6d5331dd120077a0";
        String salt = "6d5331dd120077a0";