1 /* 2 * Copyright (C) 2009 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.KeyStore; 21 import android.test.ActivityUnitTestCase; 22 import android.test.suitebuilder.annotation.MediumTest; 23 import java.nio.charset.Charsets; 24 import java.util.Arrays; 25 import java.util.HashSet; 26 27 /** 28 * Junit / Instrumentation test case for KeyStore class 29 * 30 * Running the test suite: 31 * 32 * adb shell am instrument -w android.security.tests/.KeyStoreTestRunner 33 */ 34 @MediumTest 35 public class KeyStoreTest extends ActivityUnitTestCase<Activity> { 36 private static final String TEST_PASSWD = "12345678"; 37 private static final String TEST_PASSWD2 = "87654321"; 38 private static final String TEST_KEYNAME = "testkey"; 39 private static final String TEST_KEYNAME1 = "testkey1"; 40 private static final String TEST_KEYNAME2 = "testkey2"; 41 private static final byte[] TEST_KEYVALUE = "test value".getBytes(Charsets.UTF_8); 42 43 // "Hello, World" in Chinese 44 private static final String TEST_I18N_KEY = "\u4F60\u597D, \u4E16\u754C"; 45 private static final byte[] TEST_I18N_VALUE = TEST_I18N_KEY.getBytes(Charsets.UTF_8); 46 47 private KeyStore mKeyStore = null; 48 KeyStoreTest()49 public KeyStoreTest() { 50 super(Activity.class); 51 } 52 53 @Override setUp()54 protected void setUp() throws Exception { 55 mKeyStore = KeyStore.getInstance(); 56 if (mKeyStore.state() != KeyStore.State.UNINITIALIZED) { 57 mKeyStore.reset(); 58 } 59 assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); 60 super.setUp(); 61 } 62 63 @Override tearDown()64 protected void tearDown() throws Exception { 65 mKeyStore.reset(); 66 super.tearDown(); 67 } 68 teststate()69 public void teststate() throws Exception { 70 assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); 71 } 72 testPassword()73 public void testPassword() throws Exception { 74 assertTrue(mKeyStore.password(TEST_PASSWD)); 75 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 76 } 77 testGet()78 public void testGet() throws Exception { 79 assertNull(mKeyStore.get(TEST_KEYNAME)); 80 mKeyStore.password(TEST_PASSWD); 81 assertNull(mKeyStore.get(TEST_KEYNAME)); 82 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 83 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); 84 } 85 testPut()86 public void testPut() throws Exception { 87 assertNull(mKeyStore.get(TEST_KEYNAME)); 88 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 89 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 90 mKeyStore.password(TEST_PASSWD); 91 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 92 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); 93 } 94 testI18n()95 public void testI18n() throws Exception { 96 assertFalse(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); 97 assertFalse(mKeyStore.contains(TEST_I18N_KEY)); 98 mKeyStore.password(TEST_I18N_KEY); 99 assertTrue(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); 100 assertTrue(mKeyStore.contains(TEST_I18N_KEY)); 101 } 102 testDelete()103 public void testDelete() throws Exception { 104 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 105 mKeyStore.password(TEST_PASSWD); 106 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 107 108 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 109 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); 110 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 111 assertNull(mKeyStore.get(TEST_KEYNAME)); 112 } 113 testContains()114 public void testContains() throws Exception { 115 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 116 117 mKeyStore.password(TEST_PASSWD); 118 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 119 120 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 121 assertTrue(mKeyStore.contains(TEST_KEYNAME)); 122 } 123 testSaw()124 public void testSaw() throws Exception { 125 String[] emptyResult = mKeyStore.saw(TEST_KEYNAME); 126 assertNotNull(emptyResult); 127 assertEquals(0, emptyResult.length); 128 129 mKeyStore.password(TEST_PASSWD); 130 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); 131 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); 132 133 String[] results = mKeyStore.saw(TEST_KEYNAME); 134 assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()), 135 TEST_KEYNAME2.substring(TEST_KEYNAME.length()))), 136 new HashSet(Arrays.asList(results))); 137 } 138 testLock()139 public void testLock() throws Exception { 140 assertFalse(mKeyStore.lock()); 141 142 mKeyStore.password(TEST_PASSWD); 143 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 144 145 assertTrue(mKeyStore.lock()); 146 assertEquals(KeyStore.State.LOCKED, mKeyStore.state()); 147 } 148 testUnlock()149 public void testUnlock() throws Exception { 150 mKeyStore.password(TEST_PASSWD); 151 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 152 mKeyStore.lock(); 153 154 assertFalse(mKeyStore.unlock(TEST_PASSWD2)); 155 assertTrue(mKeyStore.unlock(TEST_PASSWD)); 156 } 157 testIsEmpty()158 public void testIsEmpty() throws Exception { 159 assertTrue(mKeyStore.isEmpty()); 160 mKeyStore.password(TEST_PASSWD); 161 assertTrue(mKeyStore.isEmpty()); 162 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 163 assertFalse(mKeyStore.isEmpty()); 164 mKeyStore.reset(); 165 assertTrue(mKeyStore.isEmpty()); 166 } 167 } 168