KeyStoreTest.java revision 5cfee3fabb3482c6a6df1c8b6f21e843cf214527
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 17package android.security.tests; 18 19import android.app.Activity; 20import android.security.KeyStore; 21import android.test.ActivityUnitTestCase; 22import android.test.suitebuilder.annotation.MediumTest; 23import java.nio.charset.Charsets; 24import java.util.Arrays; 25import 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 35public 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 49 public KeyStoreTest() { 50 super(Activity.class); 51 } 52 53 @Override 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 64 protected void tearDown() throws Exception { 65 mKeyStore.reset(); 66 super.tearDown(); 67 } 68 69 public void teststate() throws Exception { 70 assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); 71 } 72 73 public void testPassword() throws Exception { 74 assertTrue(mKeyStore.password(TEST_PASSWD)); 75 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 76 } 77 78 public void testPut() throws Exception { 79 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 80 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 81 mKeyStore.password(TEST_PASSWD); 82 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 83 } 84 85 public void testI18n() throws Exception { 86 assertFalse(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); 87 assertFalse(mKeyStore.contains(TEST_I18N_KEY)); 88 mKeyStore.password(TEST_I18N_KEY); 89 assertTrue(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); 90 assertTrue(mKeyStore.contains(TEST_I18N_KEY)); 91 } 92 93 public void testDelete() throws Exception { 94 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 95 mKeyStore.password(TEST_PASSWD); 96 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 97 98 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 99 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 100 } 101 102 public void testContains() throws Exception { 103 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 104 105 mKeyStore.password(TEST_PASSWD); 106 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 107 108 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 109 assertTrue(mKeyStore.contains(TEST_KEYNAME)); 110 } 111 112 public void testSaw() throws Exception { 113 String[] emptyResult = mKeyStore.saw(TEST_KEYNAME); 114 assertNotNull(emptyResult); 115 assertEquals(0, emptyResult.length); 116 117 mKeyStore.password(TEST_PASSWD); 118 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); 119 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); 120 121 String[] results = mKeyStore.saw(TEST_KEYNAME); 122 assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()), 123 TEST_KEYNAME2.substring(TEST_KEYNAME.length()))), 124 new HashSet(Arrays.asList(results))); 125 } 126 127 public void testLock() throws Exception { 128 assertFalse(mKeyStore.lock()); 129 130 mKeyStore.password(TEST_PASSWD); 131 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 132 133 assertTrue(mKeyStore.lock()); 134 assertEquals(KeyStore.State.LOCKED, mKeyStore.state()); 135 } 136 137 public void testUnlock() throws Exception { 138 mKeyStore.password(TEST_PASSWD); 139 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); 140 mKeyStore.lock(); 141 142 assertFalse(mKeyStore.unlock(TEST_PASSWD2)); 143 assertTrue(mKeyStore.unlock(TEST_PASSWD)); 144 } 145 146 public void testIsEmpty() throws Exception { 147 assertTrue(mKeyStore.isEmpty()); 148 mKeyStore.password(TEST_PASSWD); 149 assertTrue(mKeyStore.isEmpty()); 150 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 151 assertFalse(mKeyStore.isEmpty()); 152 mKeyStore.reset(); 153 assertTrue(mKeyStore.isEmpty()); 154 } 155} 156