FileDocCategorySizeDatePackage
ContainerEncryptionParamsTest.javaAPI DocAndroid 5.1 API18299Thu Mar 12 22:22:12 GMT 2015android.content.pm

ContainerEncryptionParamsTest

public class ContainerEncryptionParamsTest extends android.test.AndroidTestCase

Fields Summary
private static final String
ENC_ALGORITHM
private static final byte[]
IV_BYTES
private static final IvParameterSpec
ENC_PARAMS
private static final byte[]
ENC_KEY_BYTES
private static final SecretKey
ENC_KEY
private static final String
MAC_ALGORITHM
private static final byte[]
MAC_KEY_BYTES
private static final SecretKey
MAC_KEY
private static final byte[]
MAC_TAG
private static final int
AUTHENTICATED_START
private static final int
ENCRYPTED_START
private static final int
DATA_END
Constructors Summary
Methods Summary
public voidtestEquals_AuthenticatedStart_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START - 1,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_DataEnd_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END + 1);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_EncAlgo_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(new String(
                "AES-256/CBC/PKCS7Padding"), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_EncKey_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec("BLAHBLAH".getBytes(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_EncParams_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec("BLAHBLAH".getBytes()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_EncryptedStart_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START - 1, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_MacAlgo_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), "BLAHBLAH", null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_MacKey_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec("FAKE_MAC_KEY".getBytes(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_MacTag_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), "broken".getBytes(),
                AUTHENTICATED_START, ENCRYPTED_START, DATA_END);

        assertFalse(params1.equals(params2));
    
public voidtestEquals_Success()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertEquals(params1, params2);
    
public voidtestHashCode_AuthenticatedStart_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START - 1,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_DataEnd_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END + 1);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_EncAlgo_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(new String(
                "AES-256/CBC/PKCS7Padding"), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_EncKey_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec("BLAHBLAH".getBytes(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_EncParams_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec("BLAHBLAH".getBytes()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_EncryptedStart_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START - 1, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_MacAlgo_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), "BLAHBLAH", null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_MacKey_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec("FAKE_MAC_KEY".getBytes(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_MacTag_Failure()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), "broken".getBytes(),
                AUTHENTICATED_START, ENCRYPTED_START, DATA_END);

        assertFalse(params1.hashCode() == params2.hashCode());
    
public voidtestHashCode_Success()

        ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        ContainerEncryptionParams params2 = new ContainerEncryptionParams(
                new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()),
                new SecretKeySpec(ENC_KEY_BYTES.clone(), "RAW"), new String(MAC_ALGORITHM), null,
                new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        assertEquals(params1.hashCode(), params2.hashCode());
    
public voidtestParcel()


         
        ContainerEncryptionParams expected = new ContainerEncryptionParams(ENC_ALGORITHM,
                ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START,
                ENCRYPTED_START, DATA_END);

        Parcel parcel = Parcel.obtain();
        expected.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);

        ContainerEncryptionParams actual = ContainerEncryptionParams.CREATOR
                .createFromParcel(parcel);

        assertEquals(ENC_ALGORITHM, actual.getEncryptionAlgorithm());

        if (!(actual.getEncryptionSpec() instanceof IvParameterSpec)) {
            fail("encryption parameters should be IvParameterSpec");
        } else {
            IvParameterSpec actualParams = (IvParameterSpec) actual.getEncryptionSpec();
            assertTrue(Arrays.equals(IV_BYTES, actualParams.getIV()));
        }

        assertEquals(ENC_KEY, actual.getEncryptionKey());

        assertEquals(MAC_ALGORITHM, actual.getMacAlgorithm());

        assertNull(actual.getMacSpec());

        assertEquals(MAC_KEY, actual.getMacKey());

        assertTrue(Arrays.equals(MAC_TAG, actual.getMacTag()));

        assertEquals(AUTHENTICATED_START, actual.getAuthenticatedDataStart());

        assertEquals(ENCRYPTED_START, actual.getEncryptedDataStart());

        assertEquals(DATA_END, actual.getDataEnd());