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; 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 32public class KeyStoreTest extends ActivityUnitTestCase<Activity> { 33 private static final String TEST_PASSWD = "12345678"; 34 private static final String TEST_EMPTY_PASSWD = ""; 35 private static final String TEST_SHORT_PASSWD = "short"; 36 private static final String TEST_PASSWD2 = "87654321"; 37 private static final String TEST_KEYNAME = "testkey"; 38 private static final String TEST_KEYNAME1 = "testkey1"; 39 private static final String TEST_KEYNAME2 = "testkey2"; 40 private static final String TEST_KEYVALUE = "test value"; 41 42 // "Hello, World" in Chinese 43 private static final String TEST_I18N = "\u4F60\u597D, \u4E16\u754C"; 44 45 private KeyStore mKeyStore = null; 46 47 public KeyStoreTest() { 48 super(Activity.class); 49 } 50 51 @Override 52 protected void setUp() throws Exception { 53 mKeyStore = KeyStore.getInstance(); 54 if (mKeyStore.test() != KeyStore.UNINITIALIZED) mKeyStore.reset(); 55 assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test()); 56 super.setUp(); 57 } 58 59 @Override 60 protected void tearDown() throws Exception { 61 mKeyStore.reset(); 62 super.tearDown(); 63 } 64 65 public void testTest() throws Exception { 66 assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test()); 67 } 68 69 public void testPassword() throws Exception { 70 //assertFalse(mKeyStore.password(TEST_EMPTY_PASSWD)); 71 //assertFalse(mKeyStore.password(TEST_SHORT_PASSWD)); 72 73 assertTrue(mKeyStore.password(TEST_PASSWD)); 74 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 75 76 assertFalse(mKeyStore.password(TEST_PASSWD2, TEST_PASSWD2)); 77 //assertFalse(mKeyStore.password(TEST_PASSWD, TEST_SHORT_PASSWD)); 78 79 assertTrue(mKeyStore.password(TEST_PASSWD, TEST_PASSWD2)); 80 } 81 82 public void testPut() throws Exception { 83 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 84 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 85 mKeyStore.password(TEST_PASSWD); 86 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 87 } 88 89 public void testI18n() throws Exception { 90 assertFalse(mKeyStore.put(TEST_I18N, TEST_I18N)); 91 assertFalse(mKeyStore.contains(TEST_I18N)); 92 mKeyStore.password(TEST_I18N); 93 assertTrue(mKeyStore.put(TEST_I18N, TEST_I18N)); 94 assertTrue(mKeyStore.contains(TEST_I18N)); 95 } 96 97 public void testDelete() throws Exception { 98 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 99 mKeyStore.password(TEST_PASSWD); 100 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 101 102 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 103 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 104 } 105 106 public void testContains() throws Exception { 107 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 108 109 mKeyStore.password(TEST_PASSWD); 110 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 111 112 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 113 assertTrue(mKeyStore.contains(TEST_KEYNAME)); 114 } 115 116 public void testSaw() throws Exception { 117 String[] results = mKeyStore.saw(TEST_KEYNAME); 118 assertEquals(0, results.length); 119 120 mKeyStore.password(TEST_PASSWD); 121 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); 122 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); 123 124 results = mKeyStore.saw(TEST_KEYNAME); 125 assertEquals(2, results.length); 126 } 127 128 public void testLock() throws Exception { 129 assertFalse(mKeyStore.lock()); 130 131 mKeyStore.password(TEST_PASSWD); 132 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 133 134 assertTrue(mKeyStore.lock()); 135 assertEquals(KeyStore.LOCKED, mKeyStore.test()); 136 } 137 138 public void testUnlock() throws Exception { 139 mKeyStore.password(TEST_PASSWD); 140 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 141 mKeyStore.lock(); 142 143 assertFalse(mKeyStore.unlock(TEST_PASSWD2)); 144 assertTrue(mKeyStore.unlock(TEST_PASSWD)); 145 } 146} 147