Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2010 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.security.tests;
     18 
     19 import android.app.Activity;
     20 import android.security.SystemKeyStore;
     21 import android.test.ActivityUnitTestCase;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 
     24 /**
     25  * Junit / Instrumentation test case for KeyStore class
     26  *
     27  * Running the test suite:
     28  *
     29  *  adb shell am instrument -w android.security.tests/.KeyStoreTestRunner
     30  */
     31 @MediumTest
     32 public class SystemKeyStoreTest extends ActivityUnitTestCase<Activity> {
     33 
     34     private static final String keyName = "TestKey";
     35     private static final String keyName2 = "TestKey2";
     36     private SystemKeyStore mSysKeyStore = null;
     37 
     38     public SystemKeyStoreTest() {
     39         super(Activity.class);
     40     }
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         mSysKeyStore = SystemKeyStore.getInstance();
     45         try {
     46             mSysKeyStore.deleteKey(keyName);
     47             mSysKeyStore.deleteKey(keyName2);
     48         } catch (Exception e) { }
     49         super.setUp();
     50     }
     51 
     52     @Override
     53     protected void tearDown() throws Exception {
     54         try {
     55             mSysKeyStore.deleteKey(keyName);
     56             mSysKeyStore.deleteKey(keyName2);
     57         } catch (Exception e) { }
     58         super.tearDown();
     59     }
     60 
     61     public void testBasicAccess() throws Exception {
     62         try {
     63             byte[] newKey = mSysKeyStore.generateNewKey(128, "AES", keyName);
     64             assertNotNull(newKey);
     65             byte[] recKey = mSysKeyStore.retrieveKey(keyName);
     66             assertEquals(newKey.length, recKey.length);
     67             for (int i = 0; i < newKey.length; i++) {
     68                 assertEquals(newKey[i], recKey[i]);
     69             }
     70             mSysKeyStore.deleteKey(keyName);
     71             byte[] nullKey = mSysKeyStore.retrieveKey(keyName);
     72             assertNull(nullKey);
     73 
     74             String newKeyStr = mSysKeyStore.generateNewKeyHexString(128, "AES", keyName2);
     75             assertNotNull(newKeyStr);
     76             String recKeyStr = mSysKeyStore.retrieveKeyHexString(keyName2);
     77             assertEquals(newKeyStr, recKeyStr);
     78 
     79             mSysKeyStore.deleteKey(keyName2);
     80             String nullKey2 = mSysKeyStore.retrieveKeyHexString(keyName2);
     81             assertNull(nullKey2);
     82         } catch (Exception e) {
     83             fail();
     84         }
     85     }
     86 }
     87