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 | |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame^] | 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 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 86 | public void testPut() throws Exception { |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame^] | 87 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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)); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame^] | 92 | assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 95 | public void testI18n() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 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)); |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 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); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame^] | 109 | assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 110 | assertTrue(mKeyStore.delete(TEST_KEYNAME)); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame^] | 111 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 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 | |
| 124 | public void testSaw() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 125 | String[] emptyResult = mKeyStore.saw(TEST_KEYNAME); |
| 126 | assertNotNull(emptyResult); |
| 127 | assertEquals(0, emptyResult.length); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 128 | |
| 129 | mKeyStore.password(TEST_PASSWD); |
| 130 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); |
| 131 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); |
| 132 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 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))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public void testLock() throws Exception { |
| 140 | assertFalse(mKeyStore.lock()); |
| 141 | |
| 142 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 143 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 144 | |
| 145 | assertTrue(mKeyStore.lock()); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 146 | assertEquals(KeyStore.State.LOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | public void testUnlock() throws Exception { |
| 150 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 151 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 152 | mKeyStore.lock(); |
| 153 | |
| 154 | assertFalse(mKeyStore.unlock(TEST_PASSWD2)); |
| 155 | assertTrue(mKeyStore.unlock(TEST_PASSWD)); |
| 156 | } |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 157 | |
| 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 | } |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 167 | } |