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;
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  *  runtest keystore-unit
30  *
31  * Or this individual test case:
32  *
33  *  runtest --path frameworks/base/keystore/tests/src/android/security/SystemKeyStoreTest.java
34  */
35 @MediumTest
36 public class SystemKeyStoreTest extends ActivityUnitTestCase<Activity> {
37 
38     private static final String keyName = "TestKey";
39     private static final String keyName2 = "TestKey2";
40     private SystemKeyStore mSysKeyStore = null;
41 
SystemKeyStoreTest()42     public SystemKeyStoreTest() {
43         super(Activity.class);
44     }
45 
46     @Override
setUp()47     protected void setUp() throws Exception {
48         mSysKeyStore = SystemKeyStore.getInstance();
49         try {
50             mSysKeyStore.deleteKey(keyName);
51             mSysKeyStore.deleteKey(keyName2);
52         } catch (Exception e) { }
53         super.setUp();
54     }
55 
56     @Override
tearDown()57     protected void tearDown() throws Exception {
58         try {
59             mSysKeyStore.deleteKey(keyName);
60             mSysKeyStore.deleteKey(keyName2);
61         } catch (Exception e) { }
62         super.tearDown();
63     }
64 
testBasicAccess()65     public void testBasicAccess() throws Exception {
66         try {
67             byte[] newKey = mSysKeyStore.generateNewKey(128, "AES", keyName);
68             assertNotNull(newKey);
69             byte[] recKey = mSysKeyStore.retrieveKey(keyName);
70             assertEquals(newKey.length, recKey.length);
71             for (int i = 0; i < newKey.length; i++) {
72                 assertEquals(newKey[i], recKey[i]);
73             }
74             mSysKeyStore.deleteKey(keyName);
75             byte[] nullKey = mSysKeyStore.retrieveKey(keyName);
76             assertNull(nullKey);
77 
78             String newKeyStr = mSysKeyStore.generateNewKeyHexString(128, "AES", keyName2);
79             assertNotNull(newKeyStr);
80             String recKeyStr = mSysKeyStore.retrieveKeyHexString(keyName2);
81             assertEquals(newKeyStr, recKeyStr);
82 
83             mSysKeyStore.deleteKey(keyName2);
84             String nullKey2 = mSysKeyStore.retrieveKeyHexString(keyName2);
85             assertNull(nullKey2);
86         } catch (Exception e) {
87             fail();
88         }
89     }
90 }
91