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 | |
Kenny Root | 96ad6cb | 2012-08-10 12:39:15 -0700 | [diff] [blame] | 17 | package android.security; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 20 | import android.os.Process; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 21 | import android.security.KeyStore; |
| 22 | import android.test.ActivityUnitTestCase; |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 23 | import android.test.AssertionFailedError; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.MediumTest; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 25 | import java.nio.charset.Charsets; |
| 26 | import java.util.Arrays; |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 27 | import java.util.Date; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 28 | import java.util.HashSet; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Junit / Instrumentation test case for KeyStore class |
| 32 | * |
| 33 | * Running the test suite: |
| 34 | * |
Kenny Root | 96ad6cb | 2012-08-10 12:39:15 -0700 | [diff] [blame] | 35 | * runtest keystore-unit |
| 36 | * |
| 37 | * Or this individual test case: |
| 38 | * |
| 39 | * runtest --path frameworks/base/keystore/tests/src/android/security/KeyStoreTest.java |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 40 | */ |
| 41 | @MediumTest |
| 42 | public class KeyStoreTest extends ActivityUnitTestCase<Activity> { |
| 43 | private static final String TEST_PASSWD = "12345678"; |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 44 | private static final String TEST_PASSWD2 = "87654321"; |
Brian Carlstrom | 5ea68db | 2012-07-17 23:40:49 -0700 | [diff] [blame] | 45 | private static final String TEST_KEYNAME = "test-key"; |
| 46 | private static final String TEST_KEYNAME1 = "test-key.1"; |
Brian Carlstrom | 2a5b147 | 2012-07-30 18:44:29 -0700 | [diff] [blame] | 47 | private static final String TEST_KEYNAME2 = "test-key\02"; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 48 | 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] | 49 | |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 50 | // "Hello, World" in Chinese |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 51 | private static final String TEST_I18N_KEY = "\u4F60\u597D, \u4E16\u754C"; |
| 52 | 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] | 53 | |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 54 | // Test vector data for signatures |
Brian Carlstrom | 5ea68db | 2012-07-17 23:40:49 -0700 | [diff] [blame] | 55 | private static final byte[] TEST_DATA = new byte[256]; |
| 56 | static { |
| 57 | for (int i = 0; i < TEST_DATA.length; i++) { |
| 58 | TEST_DATA[i] = (byte) i; |
| 59 | } |
| 60 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 61 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 62 | private KeyStore mKeyStore = null; |
| 63 | |
| 64 | public KeyStoreTest() { |
| 65 | super(Activity.class); |
| 66 | } |
| 67 | |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 68 | private static final byte[] PRIVKEY_BYTES = hexToBytes( |
| 69 | "308204BE020100300D06092A864886F70D0101010500048204A8308204A4020100028201" + |
| 70 | "0100E0473E8AB8F2284FEB9E742FF9748FA118ED98633C92F52AEB7A2EBE0D3BE60329BE" + |
| 71 | "766AD10EB6A515D0D2CFD9BEA7930F0C306537899F7958CD3E85B01F8818524D312584A9" + |
| 72 | "4B251E3625B54141EDBFEE198808E1BB97FC7CB49B9EAAAF68E9C98D7D0EDC53BBC0FA00" + |
| 73 | "34356D6305FBBCC3C7001405386ABBC873CB0F3EF7425F3D33DF7B315AE036D2A0B66AFD" + |
| 74 | "47503B169BF36E3B5162515B715FDA83DEAF2C58AEB9ABFB3097C3CC9DD9DBE5EF296C17" + |
| 75 | "6139028E8A671E63056D45F40188D2C4133490845DE52C2534E9C6B2478C07BDAE928823" + |
| 76 | "B62D066C7770F9F63F3DBA247F530844747BE7AAA85D853B8BD244ACEC3DE3C89AB46453" + |
| 77 | "AB4D24C3AC6902030100010282010037784776A5F17698F5AC960DFB83A1B67564E648BD" + |
| 78 | "0597CF8AB8087186F2669C27A9ECBDD480F0197A80D07309E6C6A96F925331E57F8B4AC6" + |
| 79 | "F4D45EDA45A23269C09FC428C07A4E6EDF738A15DEC97FABD2F2BB47A14F20EA72FCFE4C" + |
| 80 | "36E01ADA77BD137CD8D4DA10BB162E94A4662971F175F985FA188F056CB97EE2816F43AB" + |
| 81 | "9D3747612486CDA8C16196C30818A995EC85D38467791267B3BF21F273710A6925862576" + |
| 82 | "841C5B6712C12D4BD20A2F3299ADB7C135DA5E9515ABDA76E7CAF2A3BE80551D073B78BF" + |
| 83 | "1162C48AD2B7F4743A0238EE4D252F7D5E7E6533CCAE64CCB39360075A2FD1E034EC3AE5" + |
| 84 | "CE9C408CCBF0E25E4114021687B3DD4754AE8102818100F541884BC3737B2922D4119EF4" + |
| 85 | "5E2DEE2CD4CBB75F45505A157AA5009F99C73A2DF0724AC46024306332EA898177634546" + |
| 86 | "5DC6DF1E0A6F140AFF3B7396E6A8994AC5DAA96873472FE37749D14EB3E075E629DBEB35" + |
| 87 | "83338A6F3649D0A2654A7A42FD9AB6BFA4AC4D481D390BB229B064BDC311CC1BE1B63189" + |
| 88 | "DA7C40CDECF2B102818100EA1A742DDB881CEDB7288C87E38D868DD7A409D15A43F445D5" + |
| 89 | "377A0B5731DDBFCA2DAF28A8E13CD5C0AFCEC3347D74A39E235A3CD9633F274DE2B94F92" + |
| 90 | "DF43833911D9E9F1CF58F27DE2E08FF45964C720D3EC2139DC7CAFC912953CDECB2F355A" + |
| 91 | "2E2C35A50FAD754CB3B23166424BA3B6E3112A2B898C38C5C15EDB238693390281805182" + |
| 92 | "8F1EC6FD996029901BAF1D7E337BA5F0AF27E984EAD895ACE62BD7DF4EE45A224089F2CC" + |
| 93 | "151AF3CD173FCE0474BCB04F386A2CDCC0E0036BA2419F54579262D47100BE931984A3EF" + |
| 94 | "A05BECF141574DC079B3A95C4A83E6C43F3214D6DF32D512DE198085E531E616B83FD7DD" + |
| 95 | "9D1F4E2607C3333D07C55D107D1D3893587102818100DB4FB50F50DE8EDB53FF34C80931" + |
| 96 | "88A0512867DA2CCA04897759E587C244010DAF8664D59E8083D16C164789301F67A9F078" + |
| 97 | "060D834A2ADBD367575B68A8A842C2B02A89B3F31FCCEC8A22FE395795C5C6C7422B4E5D" + |
| 98 | "74A1E9A8F30E7759B9FC2D639C1F15673E84E93A5EF1506F4315383C38D45CBD1B14048F" + |
| 99 | "4721DC82326102818100D8114593AF415FB612DBF1923710D54D07486205A76A3B431949" + |
| 100 | "68C0DFF1F11EF0F61A4A337D5FD3741BBC9640E447B8B6B6C47C3AC1204357D3B0C55BA9" + |
| 101 | "286BDA73F629296F5FA9146D8976357D3C751E75148696A40B74685C82CE30902D639D72" + |
| 102 | "4FF24D5E2E9407EE34EDED2E3B4DF65AA9BCFEB6DF28D07BA6903F165768"); |
| 103 | |
| 104 | |
| 105 | private static byte[] hexToBytes(String s) { |
| 106 | int len = s.length(); |
| 107 | byte[] data = new byte[len / 2]; |
| 108 | for (int i = 0; i < len; i += 2) { |
| 109 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit( |
| 110 | s.charAt(i + 1), 16)); |
| 111 | } |
| 112 | return data; |
| 113 | } |
| 114 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 115 | @Override |
| 116 | protected void setUp() throws Exception { |
| 117 | mKeyStore = KeyStore.getInstance(); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 118 | if (mKeyStore.state() != KeyStore.State.UNINITIALIZED) { |
| 119 | mKeyStore.reset(); |
| 120 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 121 | assertEquals("KeyStore should be in an uninitialized state", |
| 122 | KeyStore.State.UNINITIALIZED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 123 | super.setUp(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void tearDown() throws Exception { |
| 128 | mKeyStore.reset(); |
| 129 | super.tearDown(); |
| 130 | } |
| 131 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 132 | public void testState() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 133 | assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public void testPassword() throws Exception { |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 137 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 138 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame] | 141 | public void testGet() throws Exception { |
| 142 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
| 143 | mKeyStore.password(TEST_PASSWD); |
| 144 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
| 145 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); |
| 146 | assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); |
| 147 | } |
| 148 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 149 | public void testPut() throws Exception { |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame] | 150 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 151 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); |
| 152 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 153 | mKeyStore.password(TEST_PASSWD); |
| 154 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame] | 155 | assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 158 | public void testPut_grantedUid_Wifi() throws Exception { |
| 159 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 160 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID)); |
| 161 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 162 | mKeyStore.password(TEST_PASSWD); |
| 163 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID)); |
| 164 | assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 165 | } |
| 166 | |
| 167 | public void testPut_ungrantedUid_Bluetooth() throws Exception { |
| 168 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 169 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID)); |
| 170 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 171 | mKeyStore.password(TEST_PASSWD); |
| 172 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID)); |
| 173 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 174 | } |
| 175 | |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 176 | public void testI18n() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 177 | assertFalse(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); |
| 178 | assertFalse(mKeyStore.contains(TEST_I18N_KEY)); |
| 179 | mKeyStore.password(TEST_I18N_KEY); |
| 180 | assertTrue(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE)); |
| 181 | assertTrue(mKeyStore.contains(TEST_I18N_KEY)); |
Nick Kralevich | 34c47c8 | 2010-03-09 13:28:14 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 184 | public void testDelete() throws Exception { |
Brian Carlstrom | 5ea68db | 2012-07-17 23:40:49 -0700 | [diff] [blame] | 185 | assertFalse(mKeyStore.delete(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 186 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5ea68db | 2012-07-17 23:40:49 -0700 | [diff] [blame] | 187 | assertFalse(mKeyStore.delete(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 188 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 189 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame] | 190 | assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 191 | assertTrue(mKeyStore.delete(TEST_KEYNAME)); |
Brian Carlstrom | bef5e5a | 2011-06-27 17:22:02 -0700 | [diff] [blame] | 192 | assertNull(mKeyStore.get(TEST_KEYNAME)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 195 | public void testDelete_grantedUid_Wifi() throws Exception { |
| 196 | assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID)); |
| 197 | mKeyStore.password(TEST_PASSWD); |
| 198 | assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID)); |
| 199 | |
| 200 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID)); |
| 201 | assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 202 | assertTrue(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID)); |
| 203 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 204 | } |
| 205 | |
| 206 | public void testDelete_ungrantedUid_Bluetooth() throws Exception { |
| 207 | assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 208 | mKeyStore.password(TEST_PASSWD); |
| 209 | assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 210 | |
| 211 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID)); |
| 212 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 213 | assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 214 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 215 | } |
| 216 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 217 | public void testContains() throws Exception { |
| 218 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 219 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 220 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 221 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 222 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 223 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 224 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 225 | } |
| 226 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 227 | public void testContains_grantedUid_Wifi() throws Exception { |
| 228 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 229 | |
| 230 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 231 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 232 | |
| 233 | assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID)); |
| 234 | assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 235 | } |
| 236 | |
| 237 | public void testContains_grantedUid_Bluetooth() throws Exception { |
| 238 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 239 | |
| 240 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 241 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 242 | |
| 243 | assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID)); |
| 244 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 245 | } |
| 246 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 247 | public void testSaw() throws Exception { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 248 | String[] emptyResult = mKeyStore.saw(TEST_KEYNAME); |
| 249 | assertNotNull(emptyResult); |
| 250 | assertEquals(0, emptyResult.length); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 251 | |
| 252 | mKeyStore.password(TEST_PASSWD); |
| 253 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); |
| 254 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); |
| 255 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 256 | String[] results = mKeyStore.saw(TEST_KEYNAME); |
| 257 | assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()), |
| 258 | TEST_KEYNAME2.substring(TEST_KEYNAME.length()))), |
| 259 | new HashSet(Arrays.asList(results))); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 260 | } |
| 261 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 262 | public void testSaw_ungrantedUid_Bluetooth() throws Exception { |
| 263 | String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID); |
| 264 | assertNull(results1); |
| 265 | |
| 266 | mKeyStore.password(TEST_PASSWD); |
| 267 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE); |
| 268 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE); |
| 269 | |
| 270 | String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID); |
| 271 | assertNull(results2); |
| 272 | } |
| 273 | |
| 274 | public void testSaw_grantedUid_Wifi() throws Exception { |
| 275 | String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID); |
| 276 | assertNotNull(results1); |
| 277 | assertEquals(0, results1.length); |
| 278 | |
| 279 | mKeyStore.password(TEST_PASSWD); |
| 280 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.WIFI_UID); |
| 281 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.WIFI_UID); |
| 282 | |
| 283 | String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID); |
| 284 | assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()), |
| 285 | TEST_KEYNAME2.substring(TEST_KEYNAME.length()))), |
| 286 | new HashSet(Arrays.asList(results2))); |
| 287 | } |
| 288 | |
| 289 | public void testSaw_grantedUid_Vpn() throws Exception { |
| 290 | String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID); |
| 291 | assertNotNull(results1); |
| 292 | assertEquals(0, results1.length); |
| 293 | |
| 294 | mKeyStore.password(TEST_PASSWD); |
| 295 | mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.VPN_UID); |
| 296 | mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.VPN_UID); |
| 297 | |
| 298 | String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID); |
| 299 | assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()), |
| 300 | TEST_KEYNAME2.substring(TEST_KEYNAME.length()))), |
| 301 | new HashSet(Arrays.asList(results2))); |
| 302 | } |
| 303 | |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 304 | public void testLock() throws Exception { |
| 305 | assertFalse(mKeyStore.lock()); |
| 306 | |
| 307 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 308 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 309 | |
| 310 | assertTrue(mKeyStore.lock()); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 311 | assertEquals(KeyStore.State.LOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | public void testUnlock() throws Exception { |
| 315 | mKeyStore.password(TEST_PASSWD); |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 316 | assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state()); |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 317 | mKeyStore.lock(); |
| 318 | |
| 319 | assertFalse(mKeyStore.unlock(TEST_PASSWD2)); |
| 320 | assertTrue(mKeyStore.unlock(TEST_PASSWD)); |
| 321 | } |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 322 | |
| 323 | public void testIsEmpty() throws Exception { |
| 324 | assertTrue(mKeyStore.isEmpty()); |
| 325 | mKeyStore.password(TEST_PASSWD); |
| 326 | assertTrue(mKeyStore.isEmpty()); |
| 327 | mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE); |
| 328 | assertFalse(mKeyStore.isEmpty()); |
| 329 | mKeyStore.reset(); |
| 330 | assertTrue(mKeyStore.isEmpty()); |
| 331 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 332 | |
| 333 | public void testGenerate_NotInitialized_Fail() throws Exception { |
| 334 | assertFalse("Should fail when keystore is not initialized", |
| 335 | mKeyStore.generate(TEST_KEYNAME)); |
| 336 | } |
| 337 | |
| 338 | public void testGenerate_Locked_Fail() throws Exception { |
| 339 | mKeyStore.password(TEST_PASSWD); |
| 340 | mKeyStore.lock(); |
| 341 | assertFalse("Should fail when keystore is locked", mKeyStore.generate(TEST_KEYNAME)); |
| 342 | } |
| 343 | |
| 344 | public void testGenerate_Success() throws Exception { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 345 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 346 | |
| 347 | assertTrue("Should be able to generate key when unlocked", |
| 348 | mKeyStore.generate(TEST_KEYNAME)); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 349 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 350 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 351 | } |
| 352 | |
| 353 | public void testGenerate_grantedUid_Wifi_Success() throws Exception { |
| 354 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 355 | |
| 356 | assertTrue("Should be able to generate key when unlocked", |
| 357 | mKeyStore.generate(TEST_KEYNAME, Process.WIFI_UID)); |
| 358 | assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 359 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 360 | } |
| 361 | |
| 362 | public void testGenerate_ungrantedUid_Bluetooth_Failure() throws Exception { |
| 363 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 364 | |
| 365 | assertFalse(mKeyStore.generate(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 366 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 367 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 368 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | public void testImport_Success() throws Exception { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 372 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 373 | |
| 374 | assertTrue("Should be able to import key when unlocked", |
| 375 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 376 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 377 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 378 | } |
| 379 | |
| 380 | public void testImport_grantedUid_Wifi_Success() throws Exception { |
| 381 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 382 | |
| 383 | assertTrue("Should be able to import key when unlocked", |
| 384 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES, Process.WIFI_UID)); |
| 385 | assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 386 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 387 | } |
| 388 | |
| 389 | public void testImport_ungrantedUid_Bluetooth_Failure() throws Exception { |
| 390 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 391 | |
| 392 | assertFalse(mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES, Process.BLUETOOTH_UID)); |
| 393 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 394 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 395 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | public void testImport_Failure_BadEncoding() throws Exception { |
| 399 | mKeyStore.password(TEST_PASSWD); |
| 400 | |
| 401 | assertFalse("Invalid DER-encoded key should not be imported", |
| 402 | mKeyStore.importKey(TEST_KEYNAME, TEST_DATA)); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 403 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 404 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | public void testSign_Success() throws Exception { |
| 408 | mKeyStore.password(TEST_PASSWD); |
| 409 | |
| 410 | assertTrue(mKeyStore.generate(TEST_KEYNAME)); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 411 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 412 | final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA); |
| 413 | |
| 414 | assertNotNull("Signature should not be null", signature); |
| 415 | } |
| 416 | |
| 417 | public void testVerify_Success() throws Exception { |
| 418 | mKeyStore.password(TEST_PASSWD); |
| 419 | |
| 420 | assertTrue(mKeyStore.generate(TEST_KEYNAME)); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 421 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 422 | final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA); |
| 423 | |
| 424 | assertNotNull("Signature should not be null", signature); |
| 425 | |
| 426 | assertTrue("Signature should verify with same data", |
| 427 | mKeyStore.verify(TEST_KEYNAME, TEST_DATA, signature)); |
| 428 | } |
| 429 | |
| 430 | public void testSign_NotInitialized_Failure() throws Exception { |
| 431 | assertNull("Should not be able to sign without first initializing the keystore", |
| 432 | mKeyStore.sign(TEST_KEYNAME, TEST_DATA)); |
| 433 | } |
| 434 | |
| 435 | public void testSign_NotGenerated_Failure() throws Exception { |
| 436 | mKeyStore.password(TEST_PASSWD); |
| 437 | |
| 438 | assertNull("Should not be able to sign without first generating keys", |
| 439 | mKeyStore.sign(TEST_KEYNAME, TEST_DATA)); |
| 440 | } |
| 441 | |
| 442 | public void testGrant_Generated_Success() throws Exception { |
| 443 | assertTrue("Password should work for keystore", |
| 444 | mKeyStore.password(TEST_PASSWD)); |
| 445 | |
| 446 | assertTrue("Should be able to generate key for testcase", |
| 447 | mKeyStore.generate(TEST_KEYNAME)); |
| 448 | |
| 449 | assertTrue("Should be able to grant key to other user", |
| 450 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 451 | } |
| 452 | |
| 453 | public void testGrant_Imported_Success() throws Exception { |
| 454 | assertTrue("Password should work for keystore", mKeyStore.password(TEST_PASSWD)); |
| 455 | |
| 456 | assertTrue("Should be able to import key for testcase", |
| 457 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); |
| 458 | |
| 459 | assertTrue("Should be able to grant key to other user", mKeyStore.grant(TEST_KEYNAME, 0)); |
| 460 | } |
| 461 | |
| 462 | public void testGrant_NoKey_Failure() throws Exception { |
| 463 | assertTrue("Should be able to unlock keystore for test", |
| 464 | mKeyStore.password(TEST_PASSWD)); |
| 465 | |
| 466 | assertFalse("Should not be able to grant without first initializing the keystore", |
| 467 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 468 | } |
| 469 | |
| 470 | public void testGrant_NotInitialized_Failure() throws Exception { |
| 471 | assertFalse("Should not be able to grant without first initializing the keystore", |
| 472 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 473 | } |
| 474 | |
| 475 | public void testUngrant_Generated_Success() throws Exception { |
| 476 | assertTrue("Password should work for keystore", |
| 477 | mKeyStore.password(TEST_PASSWD)); |
| 478 | |
| 479 | assertTrue("Should be able to generate key for testcase", |
| 480 | mKeyStore.generate(TEST_KEYNAME)); |
| 481 | |
| 482 | assertTrue("Should be able to grant key to other user", |
| 483 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 484 | |
| 485 | assertTrue("Should be able to ungrant key to other user", |
| 486 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 487 | } |
| 488 | |
| 489 | public void testUngrant_Imported_Success() throws Exception { |
| 490 | assertTrue("Password should work for keystore", |
| 491 | mKeyStore.password(TEST_PASSWD)); |
| 492 | |
| 493 | assertTrue("Should be able to import key for testcase", |
| 494 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); |
| 495 | |
| 496 | assertTrue("Should be able to grant key to other user", |
| 497 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 498 | |
| 499 | assertTrue("Should be able to ungrant key to other user", |
| 500 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 501 | } |
| 502 | |
| 503 | public void testUngrant_NotInitialized_Failure() throws Exception { |
| 504 | assertFalse("Should fail to ungrant key when keystore not initialized", |
| 505 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 506 | } |
| 507 | |
| 508 | public void testUngrant_NoGrant_Failure() throws Exception { |
| 509 | assertTrue("Password should work for keystore", |
| 510 | mKeyStore.password(TEST_PASSWD)); |
| 511 | |
| 512 | assertTrue("Should be able to generate key for testcase", |
| 513 | mKeyStore.generate(TEST_KEYNAME)); |
| 514 | |
| 515 | assertFalse("Should not be able to revoke not existent grant", |
| 516 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 517 | } |
| 518 | |
| 519 | public void testUngrant_DoubleUngrant_Failure() throws Exception { |
| 520 | assertTrue("Password should work for keystore", |
| 521 | mKeyStore.password(TEST_PASSWD)); |
| 522 | |
| 523 | assertTrue("Should be able to generate key for testcase", |
| 524 | mKeyStore.generate(TEST_KEYNAME)); |
| 525 | |
| 526 | assertTrue("Should be able to grant key to other user", |
| 527 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 528 | |
| 529 | assertTrue("Should be able to ungrant key to other user", |
| 530 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 531 | |
| 532 | assertFalse("Should fail to ungrant key to other user second time", |
| 533 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 534 | } |
| 535 | |
| 536 | public void testUngrant_DoubleGrantUngrant_Failure() throws Exception { |
| 537 | assertTrue("Password should work for keystore", |
| 538 | mKeyStore.password(TEST_PASSWD)); |
| 539 | |
| 540 | assertTrue("Should be able to generate key for testcase", |
| 541 | mKeyStore.generate(TEST_KEYNAME)); |
| 542 | |
| 543 | assertTrue("Should be able to grant key to other user", |
| 544 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 545 | |
| 546 | assertTrue("Should be able to grant key to other user a second time", |
| 547 | mKeyStore.grant(TEST_KEYNAME, 0)); |
| 548 | |
| 549 | assertTrue("Should be able to ungrant key to other user", |
| 550 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 551 | |
| 552 | assertFalse("Should fail to ungrant key to other user second time", |
| 553 | mKeyStore.ungrant(TEST_KEYNAME, 0)); |
| 554 | } |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 555 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame^] | 556 | public void testDuplicate_grantedUid_Wifi_Success() throws Exception { |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 557 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 558 | |
| 559 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 560 | |
| 561 | assertTrue(mKeyStore.generate(TEST_KEYNAME)); |
| 562 | |
| 563 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 564 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 565 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame^] | 566 | // source doesn't exist |
| 567 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, -1, TEST_KEYNAME1, Process.WIFI_UID)); |
| 568 | assertFalse(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID)); |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 569 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame^] | 570 | // Copy from current UID to granted UID |
| 571 | assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID)); |
| 572 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 573 | assertFalse(mKeyStore.contains(TEST_KEYNAME1)); |
| 574 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 575 | assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID)); |
| 576 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID)); |
| 577 | |
| 578 | // Copy from granted UID to same granted UID |
| 579 | assertTrue(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2, |
| 580 | Process.WIFI_UID)); |
| 581 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID)); |
| 582 | assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID)); |
| 583 | assertTrue(mKeyStore.contains(TEST_KEYNAME2, Process.WIFI_UID)); |
| 584 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2, |
| 585 | Process.WIFI_UID)); |
| 586 | |
| 587 | assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1)); |
| 588 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 589 | assertFalse(mKeyStore.contains(TEST_KEYNAME1)); |
| 590 | assertTrue(mKeyStore.contains(TEST_KEYNAME2)); |
| 591 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1)); |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame^] | 594 | public void testDuplicate_ungrantedUid_Bluetooth_Failure() throws Exception { |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 595 | assertTrue(mKeyStore.password(TEST_PASSWD)); |
| 596 | |
| 597 | assertFalse(mKeyStore.contains(TEST_KEYNAME)); |
| 598 | |
| 599 | assertTrue(mKeyStore.generate(TEST_KEYNAME)); |
| 600 | |
| 601 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 602 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 603 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame^] | 604 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, Process.BLUETOOTH_UID)); |
| 605 | assertFalse(mKeyStore.duplicate(TEST_KEYNAME, Process.BLUETOOTH_UID, TEST_KEYNAME2, |
| 606 | Process.BLUETOOTH_UID)); |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 607 | |
| 608 | assertTrue(mKeyStore.contains(TEST_KEYNAME)); |
| 609 | assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID)); |
| 610 | } |
| 611 | |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 612 | /** |
| 613 | * The amount of time to allow before and after expected time for variance |
| 614 | * in timing tests. |
| 615 | */ |
| 616 | private static final long SLOP_TIME_MILLIS = 15000L; |
| 617 | |
| 618 | public void testGetmtime_Success() throws Exception { |
| 619 | assertTrue("Password should work for keystore", |
| 620 | mKeyStore.password(TEST_PASSWD)); |
| 621 | |
| 622 | assertTrue("Should be able to import key when unlocked", |
| 623 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); |
| 624 | |
| 625 | long now = System.currentTimeMillis(); |
| 626 | long actual = mKeyStore.getmtime(TEST_KEYNAME); |
| 627 | |
| 628 | long expectedAfter = now - SLOP_TIME_MILLIS; |
| 629 | long expectedBefore = now + SLOP_TIME_MILLIS; |
| 630 | |
| 631 | assertLessThan("Time should be close to current time", expectedBefore, actual); |
| 632 | assertGreaterThan("Time should be close to current time", expectedAfter, actual); |
| 633 | } |
| 634 | |
| 635 | private static void assertLessThan(String explanation, long expectedBefore, long actual) { |
| 636 | if (actual >= expectedBefore) { |
| 637 | throw new AssertionFailedError(explanation + ": actual=" + actual |
| 638 | + ", expected before: " + expectedBefore); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | private static void assertGreaterThan(String explanation, long expectedAfter, long actual) { |
| 643 | if (actual <= expectedAfter) { |
| 644 | throw new AssertionFailedError(explanation + ": actual=" + actual |
| 645 | + ", expected after: " + expectedAfter); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | public void testGetmtime_NonExist_Failure() throws Exception { |
| 650 | assertTrue("Password should work for keystore", |
| 651 | mKeyStore.password(TEST_PASSWD)); |
| 652 | |
| 653 | assertTrue("Should be able to import key when unlocked", |
| 654 | mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); |
| 655 | |
| 656 | assertEquals("-1 should be returned for non-existent key", |
| 657 | -1L, mKeyStore.getmtime(TEST_KEYNAME2)); |
| 658 | } |
Chung-yih Wang | f35e966 | 2009-09-29 11:20:28 +0800 | [diff] [blame] | 659 | } |