Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 23 | import java.nio.charset.Charsets; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.HashSet; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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"; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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"; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 41 | private static final byte[] TEST_KEYVALUE = "test value".getBytes(Charsets.UTF_8); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 42 | |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 43 | // "Hello, World" in Chinese |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 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); |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 46 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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(); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 56 | if (mKeyStore.state() != KeyStore.State.UNINITIALIZED) { |
| 57 | mKeyStore.reset(); |
| 58 | } |
| 59 | assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 60 | super.setUp(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void tearDown() throws Exception { |
| 65 | mKeyStore.reset(); |
| 66 | super.tearDown(); |
| 67 | } |
| 68 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 69 | public void teststate() throws Exception { |
| 70 | assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void testPassword() throws Exception { |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 74 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 75 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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 | |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 85 | public void testI18n() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 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)); |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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 { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 113 | String[] emptyResult = mKeyStore.saw(TEST_KEYNAME); |
| 114 | assertNotNull(emptyResult); |
| 115 | assertEquals(0, emptyResult.length); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 116 | |
| 117 | mKeyStore.password(TEST_PASSWD); |
| 118 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); |
| 119 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); |
| 120 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 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))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void testLock() throws Exception { |
| 128 | assertFalse(mKeyStore.lock()); |
| 129 | |
| 130 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 131 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 132 | |
| 133 | assertTrue(mKeyStore.lock()); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 134 | assertEquals(KeyStore.State.LOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void testUnlock() throws Exception { |
| 138 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 139 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 140 | mKeyStore.lock(); |
| 141 | |
| 142 | assertFalse(mKeyStore.unlock(TEST_PASSWD2)); |
| 143 | assertTrue(mKeyStore.unlock(TEST_PASSWD)); |
| 144 | } |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame^] | 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 | } |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 155 | } |