Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.content.pm;
     18 
     19 import android.test.AndroidTestCase;
     20 
     21 import java.io.ByteArrayInputStream;
     22 import java.util.Arrays;
     23 
     24 import javax.crypto.Mac;
     25 import javax.crypto.SecretKey;
     26 import javax.crypto.spec.SecretKeySpec;
     27 
     28 import libcore.io.Streams;
     29 
     30 public class MacAuthenticatedInputStreamTest extends AndroidTestCase {
     31 
     32     private static final SecretKey HMAC_KEY_1 = new SecretKeySpec("test_key_1".getBytes(), "HMAC");
     33 
     34     private static final byte[] TEST_STRING_1 = "Hello, World!".getBytes();
     35 
     36     /**
     37      * Generated with:
     38      *
     39      * echo -n 'Hello, World!' | openssl dgst -hmac 'test_key_1' -binary -sha1 | recode ..//x1 |
     40      *   sed 's/0x/(byte) 0x/g'
     41      */
     42     private static final byte[] TEST_STRING_1_MAC = {
     43             (byte) 0x29, (byte) 0xB1, (byte) 0x87, (byte) 0x6B, (byte) 0xFE, (byte) 0x83,
     44             (byte) 0x96, (byte) 0x51, (byte) 0x61, (byte) 0x02, (byte) 0xAF, (byte) 0x7B,
     45             (byte) 0xBA, (byte) 0x05, (byte) 0xE6, (byte) 0xA4, (byte) 0xAB, (byte) 0x36,
     46             (byte) 0x18, (byte) 0x02
     47     };
     48 
     49     /**
     50      * Same as TEST_STRING_1_MAC but with the first byte as 0x28 instead of
     51      * 0x29.
     52      */
     53     private static final byte[] TEST_STRING_1_MAC_BROKEN = {
     54             (byte) 0x28, (byte) 0xB1, (byte) 0x87, (byte) 0x6B, (byte) 0xFE, (byte) 0x83,
     55             (byte) 0x96, (byte) 0x51, (byte) 0x61, (byte) 0x02, (byte) 0xAF, (byte) 0x7B,
     56             (byte) 0xBA, (byte) 0x05, (byte) 0xE6, (byte) 0xA4, (byte) 0xAB, (byte) 0x36,
     57             (byte) 0x18, (byte) 0x02
     58     };
     59 
     60     private ByteArrayInputStream mTestStream1;
     61 
     62     @Override
     63     protected void setUp() throws Exception {
     64         super.setUp();
     65 
     66         mTestStream1 = new ByteArrayInputStream(TEST_STRING_1);
     67     }
     68 
     69     public void testString1Authenticate_Success() throws Exception {
     70         Mac mac = Mac.getInstance("HMAC-SHA1");
     71         mac.init(HMAC_KEY_1);
     72 
     73         MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);
     74 
     75         assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));
     76 
     77         assertTrue(is.isTagEqual(TEST_STRING_1_MAC));
     78     }
     79 
     80     public void testString1Authenticate_WrongTag_Failure() throws Exception {
     81         Mac mac = Mac.getInstance("HMAC-SHA1");
     82         mac.init(HMAC_KEY_1);
     83 
     84         MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);
     85 
     86         assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));
     87 
     88         assertFalse(is.isTagEqual(TEST_STRING_1_MAC_BROKEN));
     89     }
     90 
     91     public void testString1Authenticate_NullTag_Failure() throws Exception {
     92         Mac mac = Mac.getInstance("HMAC-SHA1");
     93         mac.init(HMAC_KEY_1);
     94 
     95         MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);
     96 
     97         assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));
     98 
     99         assertFalse(is.isTagEqual(null));
    100     }
    101 
    102     public void testString1Authenticate_ReadSingleByte_Success() throws Exception {
    103         Mac mac = Mac.getInstance("HMAC-SHA1");
    104         mac.init(HMAC_KEY_1);
    105 
    106         MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);
    107 
    108         int numRead = 0;
    109         while (is.read() != -1) {
    110             numRead++;
    111 
    112             if (numRead > TEST_STRING_1.length) {
    113                 fail("read too many bytes");
    114             }
    115         }
    116         assertEquals(TEST_STRING_1.length, numRead);
    117 
    118         assertTrue(is.isTagEqual(TEST_STRING_1_MAC));
    119     }
    120 }
    121