KeyStoreTest.java revision f35e9663d7bdae523953185b4ad6b6f9e8e7d6ca
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 private KeyStore mKeyStore = null; 43 44 public KeyStoreTest() { 45 super(Activity.class); 46 } 47 48 @Override 49 protected void setUp() throws Exception { 50 mKeyStore = KeyStore.getInstance(); 51 if (mKeyStore.test() != KeyStore.UNINITIALIZED) mKeyStore.reset(); 52 assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test()); 53 super.setUp(); 54 } 55 56 @Override 57 protected void tearDown() throws Exception { 58 mKeyStore.reset(); 59 super.tearDown(); 60 } 61 62 public void testTest() throws Exception { 63 assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test()); 64 } 65 66 public void testPassword() throws Exception { 67 //assertFalse(mKeyStore.password(TEST_EMPTY_PASSWD)); 68 //assertFalse(mKeyStore.password(TEST_SHORT_PASSWD)); 69 70 assertTrue(mKeyStore.password(TEST_PASSWD)); 71 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 72 73 assertFalse(mKeyStore.password(TEST_PASSWD2, TEST_PASSWD2)); 74 //assertFalse(mKeyStore.password(TEST_PASSWD, TEST_SHORT_PASSWD)); 75 76 assertTrue(mKeyStore.password(TEST_PASSWD, TEST_PASSWD2)); 77 } 78 79 public void testPut() throws Exception { 80 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 81 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 82 mKeyStore.password(TEST_PASSWD); 83 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); 84 } 85 86 public void testDelete() throws Exception { 87 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 88 mKeyStore.password(TEST_PASSWD); 89 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 90 91 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 92 assertTrue(mKeyStore.delete(TEST_KEYNAME)); 93 } 94 95 public void testContains() throws Exception { 96 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 97 98 mKeyStore.password(TEST_PASSWD); 99 assertFalse(mKeyStore.contains(TEST_KEYNAME)); 100 101 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); 102 assertTrue(mKeyStore.contains(TEST_KEYNAME)); 103 } 104 105 public void testSaw() throws Exception { 106 String[] results = mKeyStore.saw(TEST_KEYNAME); 107 assertEquals(0, results.length); 108 109 mKeyStore.password(TEST_PASSWD); 110 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); 111 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); 112 113 results = mKeyStore.saw(TEST_KEYNAME); 114 assertEquals(2, results.length); 115 } 116 117 public void testLock() throws Exception { 118 assertFalse(mKeyStore.lock()); 119 120 mKeyStore.password(TEST_PASSWD); 121 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 122 123 assertTrue(mKeyStore.lock()); 124 assertEquals(KeyStore.LOCKED, mKeyStore.test()); 125 } 126 127 public void testUnlock() throws Exception { 128 mKeyStore.password(TEST_PASSWD); 129 assertEquals(KeyStore.NO_ERROR, mKeyStore.test()); 130 mKeyStore.lock(); 131 132 assertFalse(mKeyStore.unlock(TEST_PASSWD2)); 133 assertTrue(mKeyStore.unlock(TEST_PASSWD)); 134 } 135} 136