FileDocCategorySizeDatePackage
MacAuthenticatedInputStreamTest.javaAPI DocAndroid 5.1 API4073Thu Mar 12 22:22:12 GMT 2015android.content.pm

MacAuthenticatedInputStreamTest

public class MacAuthenticatedInputStreamTest extends android.test.AndroidTestCase

Fields Summary
private static final SecretKey
HMAC_KEY_1
private static final byte[]
TEST_STRING_1
private static final byte[]
TEST_STRING_1_MAC
Generated with: echo -n 'Hello, World!' | openssl dgst -hmac 'test_key_1' -binary -sha1 | recode ..//x1 | sed 's/0x/(byte) 0x/g'
private static final byte[]
TEST_STRING_1_MAC_BROKEN
Same as TEST_STRING_1_MAC but with the first byte as 0x28 instead of 0x29.
private ByteArrayInputStream
mTestStream1
Constructors Summary
Methods Summary
protected voidsetUp()


    
         
        super.setUp();

        mTestStream1 = new ByteArrayInputStream(TEST_STRING_1);
    
public voidtestString1Authenticate_NullTag_Failure()

        Mac mac = Mac.getInstance("HMAC-SHA1");
        mac.init(HMAC_KEY_1);

        MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);

        assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));

        assertFalse(is.isTagEqual(null));
    
public voidtestString1Authenticate_ReadSingleByte_Success()

        Mac mac = Mac.getInstance("HMAC-SHA1");
        mac.init(HMAC_KEY_1);

        MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);

        int numRead = 0;
        while (is.read() != -1) {
            numRead++;

            if (numRead > TEST_STRING_1.length) {
                fail("read too many bytes");
            }
        }
        assertEquals(TEST_STRING_1.length, numRead);

        assertTrue(is.isTagEqual(TEST_STRING_1_MAC));
    
public voidtestString1Authenticate_Success()

        Mac mac = Mac.getInstance("HMAC-SHA1");
        mac.init(HMAC_KEY_1);

        MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);

        assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));

        assertTrue(is.isTagEqual(TEST_STRING_1_MAC));
    
public voidtestString1Authenticate_WrongTag_Failure()

        Mac mac = Mac.getInstance("HMAC-SHA1");
        mac.init(HMAC_KEY_1);

        MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);

        assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));

        assertFalse(is.isTagEqual(TEST_STRING_1_MAC_BROKEN));