blob: 7a142ccf7f8880229afe6eef71a1a498a0a4704b [file] [log] [blame]
Chung-yih Wangf35e9662009-09-29 11:20:28 +08001/*
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 Root96ad6cb2012-08-10 12:39:15 -070017package android.security;
Chung-yih Wangf35e9662009-09-29 11:20:28 +080018
19import android.app.Activity;
Kenny Root78ad8492013-02-13 17:02:57 -080020import android.os.Process;
Chung-yih Wangf35e9662009-09-29 11:20:28 +080021import android.security.KeyStore;
22import android.test.ActivityUnitTestCase;
Kenny Root473c7122012-08-17 21:13:48 -070023import android.test.AssertionFailedError;
Chung-yih Wangf35e9662009-09-29 11:20:28 +080024import android.test.suitebuilder.annotation.MediumTest;
Kenny Rootf64386f2013-08-16 14:03:29 -070025import com.android.org.conscrypt.NativeCrypto;
Elliott Hughesd396a442013-06-28 16:24:48 -070026import java.nio.charset.StandardCharsets;
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070027import java.util.Arrays;
Kenny Root473c7122012-08-17 21:13:48 -070028import java.util.Date;
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070029import java.util.HashSet;
Chung-yih Wangf35e9662009-09-29 11:20:28 +080030
31/**
32 * Junit / Instrumentation test case for KeyStore class
33 *
34 * Running the test suite:
35 *
Kenny Root96ad6cb2012-08-10 12:39:15 -070036 * runtest keystore-unit
37 *
38 * Or this individual test case:
39 *
40 * runtest --path frameworks/base/keystore/tests/src/android/security/KeyStoreTest.java
Chung-yih Wangf35e9662009-09-29 11:20:28 +080041 */
42@MediumTest
43public class KeyStoreTest extends ActivityUnitTestCase<Activity> {
44 private static final String TEST_PASSWD = "12345678";
Chung-yih Wangf35e9662009-09-29 11:20:28 +080045 private static final String TEST_PASSWD2 = "87654321";
Brian Carlstrom5ea68db2012-07-17 23:40:49 -070046 private static final String TEST_KEYNAME = "test-key";
47 private static final String TEST_KEYNAME1 = "test-key.1";
Brian Carlstrom2a5b1472012-07-30 18:44:29 -070048 private static final String TEST_KEYNAME2 = "test-key\02";
Elliott Hughesd396a442013-06-28 16:24:48 -070049 private static final byte[] TEST_KEYVALUE = "test value".getBytes(StandardCharsets.UTF_8);
Chung-yih Wangf35e9662009-09-29 11:20:28 +080050
Nick Kralevich34c47c82010-03-09 13:28:14 -080051 // "Hello, World" in Chinese
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070052 private static final String TEST_I18N_KEY = "\u4F60\u597D, \u4E16\u754C";
Elliott Hughesd396a442013-06-28 16:24:48 -070053 private static final byte[] TEST_I18N_VALUE = TEST_I18N_KEY.getBytes(StandardCharsets.UTF_8);
Nick Kralevich34c47c82010-03-09 13:28:14 -080054
Kenny Root5423e682011-11-14 08:43:13 -080055 // Test vector data for signatures
Brian Carlstrom5ea68db2012-07-17 23:40:49 -070056 private static final byte[] TEST_DATA = new byte[256];
57 static {
58 for (int i = 0; i < TEST_DATA.length; i++) {
59 TEST_DATA[i] = (byte) i;
60 }
61 }
Kenny Root5423e682011-11-14 08:43:13 -080062
Chung-yih Wangf35e9662009-09-29 11:20:28 +080063 private KeyStore mKeyStore = null;
64
65 public KeyStoreTest() {
66 super(Activity.class);
67 }
68
Kenny Root5423e682011-11-14 08:43:13 -080069 private static final byte[] PRIVKEY_BYTES = hexToBytes(
70 "308204BE020100300D06092A864886F70D0101010500048204A8308204A4020100028201" +
71 "0100E0473E8AB8F2284FEB9E742FF9748FA118ED98633C92F52AEB7A2EBE0D3BE60329BE" +
72 "766AD10EB6A515D0D2CFD9BEA7930F0C306537899F7958CD3E85B01F8818524D312584A9" +
73 "4B251E3625B54141EDBFEE198808E1BB97FC7CB49B9EAAAF68E9C98D7D0EDC53BBC0FA00" +
74 "34356D6305FBBCC3C7001405386ABBC873CB0F3EF7425F3D33DF7B315AE036D2A0B66AFD" +
75 "47503B169BF36E3B5162515B715FDA83DEAF2C58AEB9ABFB3097C3CC9DD9DBE5EF296C17" +
76 "6139028E8A671E63056D45F40188D2C4133490845DE52C2534E9C6B2478C07BDAE928823" +
77 "B62D066C7770F9F63F3DBA247F530844747BE7AAA85D853B8BD244ACEC3DE3C89AB46453" +
78 "AB4D24C3AC6902030100010282010037784776A5F17698F5AC960DFB83A1B67564E648BD" +
79 "0597CF8AB8087186F2669C27A9ECBDD480F0197A80D07309E6C6A96F925331E57F8B4AC6" +
80 "F4D45EDA45A23269C09FC428C07A4E6EDF738A15DEC97FABD2F2BB47A14F20EA72FCFE4C" +
81 "36E01ADA77BD137CD8D4DA10BB162E94A4662971F175F985FA188F056CB97EE2816F43AB" +
82 "9D3747612486CDA8C16196C30818A995EC85D38467791267B3BF21F273710A6925862576" +
83 "841C5B6712C12D4BD20A2F3299ADB7C135DA5E9515ABDA76E7CAF2A3BE80551D073B78BF" +
84 "1162C48AD2B7F4743A0238EE4D252F7D5E7E6533CCAE64CCB39360075A2FD1E034EC3AE5" +
85 "CE9C408CCBF0E25E4114021687B3DD4754AE8102818100F541884BC3737B2922D4119EF4" +
86 "5E2DEE2CD4CBB75F45505A157AA5009F99C73A2DF0724AC46024306332EA898177634546" +
87 "5DC6DF1E0A6F140AFF3B7396E6A8994AC5DAA96873472FE37749D14EB3E075E629DBEB35" +
88 "83338A6F3649D0A2654A7A42FD9AB6BFA4AC4D481D390BB229B064BDC311CC1BE1B63189" +
89 "DA7C40CDECF2B102818100EA1A742DDB881CEDB7288C87E38D868DD7A409D15A43F445D5" +
90 "377A0B5731DDBFCA2DAF28A8E13CD5C0AFCEC3347D74A39E235A3CD9633F274DE2B94F92" +
91 "DF43833911D9E9F1CF58F27DE2E08FF45964C720D3EC2139DC7CAFC912953CDECB2F355A" +
92 "2E2C35A50FAD754CB3B23166424BA3B6E3112A2B898C38C5C15EDB238693390281805182" +
93 "8F1EC6FD996029901BAF1D7E337BA5F0AF27E984EAD895ACE62BD7DF4EE45A224089F2CC" +
94 "151AF3CD173FCE0474BCB04F386A2CDCC0E0036BA2419F54579262D47100BE931984A3EF" +
95 "A05BECF141574DC079B3A95C4A83E6C43F3214D6DF32D512DE198085E531E616B83FD7DD" +
96 "9D1F4E2607C3333D07C55D107D1D3893587102818100DB4FB50F50DE8EDB53FF34C80931" +
97 "88A0512867DA2CCA04897759E587C244010DAF8664D59E8083D16C164789301F67A9F078" +
98 "060D834A2ADBD367575B68A8A842C2B02A89B3F31FCCEC8A22FE395795C5C6C7422B4E5D" +
99 "74A1E9A8F30E7759B9FC2D639C1F15673E84E93A5EF1506F4315383C38D45CBD1B14048F" +
100 "4721DC82326102818100D8114593AF415FB612DBF1923710D54D07486205A76A3B431949" +
101 "68C0DFF1F11EF0F61A4A337D5FD3741BBC9640E447B8B6B6C47C3AC1204357D3B0C55BA9" +
102 "286BDA73F629296F5FA9146D8976357D3C751E75148696A40B74685C82CE30902D639D72" +
103 "4FF24D5E2E9407EE34EDED2E3B4DF65AA9BCFEB6DF28D07BA6903F165768");
104
105
106 private static byte[] hexToBytes(String s) {
107 int len = s.length();
108 byte[] data = new byte[len / 2];
109 for (int i = 0; i < len; i += 2) {
110 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(
111 s.charAt(i + 1), 16));
112 }
113 return data;
114 }
115
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800116 @Override
117 protected void setUp() throws Exception {
118 mKeyStore = KeyStore.getInstance();
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700119 if (mKeyStore.state() != KeyStore.State.UNINITIALIZED) {
120 mKeyStore.reset();
121 }
Kenny Root5423e682011-11-14 08:43:13 -0800122 assertEquals("KeyStore should be in an uninitialized state",
123 KeyStore.State.UNINITIALIZED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800124 super.setUp();
125 }
126
127 @Override
128 protected void tearDown() throws Exception {
129 mKeyStore.reset();
130 super.tearDown();
131 }
132
Kenny Root78ad8492013-02-13 17:02:57 -0800133 public void testState() throws Exception {
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700134 assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800135 }
136
137 public void testPassword() throws Exception {
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800138 assertTrue(mKeyStore.password(TEST_PASSWD));
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700139 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800140 }
141
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700142 public void testGet() throws Exception {
143 assertNull(mKeyStore.get(TEST_KEYNAME));
144 mKeyStore.password(TEST_PASSWD);
145 assertNull(mKeyStore.get(TEST_KEYNAME));
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700146 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF,
147 KeyStore.FLAG_ENCRYPTED));
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700148 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
149 }
150
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800151 public void testPut() throws Exception {
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700152 assertNull(mKeyStore.get(TEST_KEYNAME));
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700153 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF,
154 KeyStore.FLAG_ENCRYPTED));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800155 assertFalse(mKeyStore.contains(TEST_KEYNAME));
156 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700157 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF,
158 KeyStore.FLAG_ENCRYPTED));
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700159 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800160 }
161
Kenny Root78ad8492013-02-13 17:02:57 -0800162 public void testPut_grantedUid_Wifi() throws Exception {
163 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700164 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID,
165 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800166 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
167 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700168 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID,
169 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800170 assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
171 }
172
173 public void testPut_ungrantedUid_Bluetooth() throws Exception {
174 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700175 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID,
176 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800177 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
178 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700179 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID,
180 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800181 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
182 }
183
Nick Kralevich34c47c82010-03-09 13:28:14 -0800184 public void testI18n() throws Exception {
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700185 assertFalse(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE, KeyStore.UID_SELF,
186 KeyStore.FLAG_ENCRYPTED));
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700187 assertFalse(mKeyStore.contains(TEST_I18N_KEY));
188 mKeyStore.password(TEST_I18N_KEY);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700189 assertTrue(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE, KeyStore.UID_SELF,
190 KeyStore.FLAG_ENCRYPTED));
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700191 assertTrue(mKeyStore.contains(TEST_I18N_KEY));
Nick Kralevich34c47c82010-03-09 13:28:14 -0800192 }
193
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800194 public void testDelete() throws Exception {
Brian Carlstrom5ea68db2012-07-17 23:40:49 -0700195 assertFalse(mKeyStore.delete(TEST_KEYNAME));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800196 mKeyStore.password(TEST_PASSWD);
Brian Carlstrom5ea68db2012-07-17 23:40:49 -0700197 assertFalse(mKeyStore.delete(TEST_KEYNAME));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800198
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700199 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF,
200 KeyStore.FLAG_ENCRYPTED));
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700201 assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800202 assertTrue(mKeyStore.delete(TEST_KEYNAME));
Brian Carlstrombef5e5a2011-06-27 17:22:02 -0700203 assertNull(mKeyStore.get(TEST_KEYNAME));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800204 }
205
Kenny Root78ad8492013-02-13 17:02:57 -0800206 public void testDelete_grantedUid_Wifi() throws Exception {
207 assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
208 mKeyStore.password(TEST_PASSWD);
209 assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
210
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700211 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID,
212 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800213 assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
214 assertTrue(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
215 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
216 }
217
218 public void testDelete_ungrantedUid_Bluetooth() throws Exception {
219 assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
220 mKeyStore.password(TEST_PASSWD);
221 assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
222
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700223 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID,
224 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800225 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
226 assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
227 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
228 }
229
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800230 public void testContains() throws Exception {
231 assertFalse(mKeyStore.contains(TEST_KEYNAME));
232
Kenny Root78ad8492013-02-13 17:02:57 -0800233 assertTrue(mKeyStore.password(TEST_PASSWD));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800234 assertFalse(mKeyStore.contains(TEST_KEYNAME));
235
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700236 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF,
237 KeyStore.FLAG_ENCRYPTED));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800238 assertTrue(mKeyStore.contains(TEST_KEYNAME));
239 }
240
Kenny Root78ad8492013-02-13 17:02:57 -0800241 public void testContains_grantedUid_Wifi() throws Exception {
242 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
243
244 assertTrue(mKeyStore.password(TEST_PASSWD));
245 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
246
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700247 assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID,
248 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800249 assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
250 }
251
252 public void testContains_grantedUid_Bluetooth() throws Exception {
253 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
254
255 assertTrue(mKeyStore.password(TEST_PASSWD));
256 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
257
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700258 assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID,
259 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800260 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
261 }
262
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800263 public void testSaw() throws Exception {
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700264 String[] emptyResult = mKeyStore.saw(TEST_KEYNAME);
265 assertNotNull(emptyResult);
266 assertEquals(0, emptyResult.length);
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800267
268 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700269 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
270 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800271
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700272 String[] results = mKeyStore.saw(TEST_KEYNAME);
273 assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
274 TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
275 new HashSet(Arrays.asList(results)));
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800276 }
277
Kenny Root78ad8492013-02-13 17:02:57 -0800278 public void testSaw_ungrantedUid_Bluetooth() throws Exception {
279 String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID);
280 assertNull(results1);
281
282 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700283 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
284 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
Kenny Root78ad8492013-02-13 17:02:57 -0800285
286 String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID);
287 assertNull(results2);
288 }
289
290 public void testSaw_grantedUid_Wifi() throws Exception {
291 String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID);
292 assertNotNull(results1);
293 assertEquals(0, results1.length);
294
295 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700296 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.WIFI_UID, KeyStore.FLAG_ENCRYPTED);
297 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.WIFI_UID, KeyStore.FLAG_ENCRYPTED);
Kenny Root78ad8492013-02-13 17:02:57 -0800298
299 String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID);
300 assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
301 TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
302 new HashSet(Arrays.asList(results2)));
303 }
304
305 public void testSaw_grantedUid_Vpn() throws Exception {
306 String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID);
307 assertNotNull(results1);
308 assertEquals(0, results1.length);
309
310 mKeyStore.password(TEST_PASSWD);
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700311 mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.VPN_UID, KeyStore.FLAG_ENCRYPTED);
312 mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.VPN_UID, KeyStore.FLAG_ENCRYPTED);
Kenny Root78ad8492013-02-13 17:02:57 -0800313
314 String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID);
315 assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
316 TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
317 new HashSet(Arrays.asList(results2)));
318 }
319
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800320 public void testLock() throws Exception {
321 assertFalse(mKeyStore.lock());
322
323 mKeyStore.password(TEST_PASSWD);
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700324 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800325
326 assertTrue(mKeyStore.lock());
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700327 assertEquals(KeyStore.State.LOCKED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800328 }
329
330 public void testUnlock() throws Exception {
331 mKeyStore.password(TEST_PASSWD);
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700332 assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800333 mKeyStore.lock();
334
335 assertFalse(mKeyStore.unlock(TEST_PASSWD2));
336 assertTrue(mKeyStore.unlock(TEST_PASSWD));
337 }
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700338
339 public void testIsEmpty() throws Exception {
340 assertTrue(mKeyStore.isEmpty());
341 mKeyStore.password(TEST_PASSWD);
342 assertTrue(mKeyStore.isEmpty());
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700343 mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700344 assertFalse(mKeyStore.isEmpty());
345 mKeyStore.reset();
346 assertTrue(mKeyStore.isEmpty());
347 }
Kenny Root5423e682011-11-14 08:43:13 -0800348
349 public void testGenerate_NotInitialized_Fail() throws Exception {
350 assertFalse("Should fail when keystore is not initialized",
Kenny Rootf64386f2013-08-16 14:03:29 -0700351 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
352 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800353 }
354
355 public void testGenerate_Locked_Fail() throws Exception {
356 mKeyStore.password(TEST_PASSWD);
357 mKeyStore.lock();
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700358 assertFalse("Should fail when keystore is locked",
Kenny Rootf64386f2013-08-16 14:03:29 -0700359 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
360 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800361 }
362
363 public void testGenerate_Success() throws Exception {
Kenny Root78ad8492013-02-13 17:02:57 -0800364 assertTrue(mKeyStore.password(TEST_PASSWD));
Kenny Root5423e682011-11-14 08:43:13 -0800365
366 assertTrue("Should be able to generate key when unlocked",
Kenny Rootf64386f2013-08-16 14:03:29 -0700367 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
368 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root78ad8492013-02-13 17:02:57 -0800369 assertTrue(mKeyStore.contains(TEST_KEYNAME));
370 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
371 }
372
373 public void testGenerate_grantedUid_Wifi_Success() throws Exception {
374 assertTrue(mKeyStore.password(TEST_PASSWD));
375
376 assertTrue("Should be able to generate key when unlocked",
Kenny Rootf64386f2013-08-16 14:03:29 -0700377 mKeyStore.generate(TEST_KEYNAME, Process.WIFI_UID, NativeCrypto.EVP_PKEY_RSA,
378 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root78ad8492013-02-13 17:02:57 -0800379 assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
380 assertFalse(mKeyStore.contains(TEST_KEYNAME));
381 }
382
383 public void testGenerate_ungrantedUid_Bluetooth_Failure() throws Exception {
384 assertTrue(mKeyStore.password(TEST_PASSWD));
385
Kenny Rootf64386f2013-08-16 14:03:29 -0700386 assertFalse(mKeyStore.generate(TEST_KEYNAME, Process.BLUETOOTH_UID,
387 NativeCrypto.EVP_PKEY_RSA, 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root78ad8492013-02-13 17:02:57 -0800388 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
389 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
390 assertFalse(mKeyStore.contains(TEST_KEYNAME));
Kenny Root5423e682011-11-14 08:43:13 -0800391 }
392
393 public void testImport_Success() throws Exception {
Kenny Root78ad8492013-02-13 17:02:57 -0800394 assertTrue(mKeyStore.password(TEST_PASSWD));
Kenny Root5423e682011-11-14 08:43:13 -0800395
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700396 assertTrue("Should be able to import key when unlocked", mKeyStore.importKey(TEST_KEYNAME,
397 PRIVKEY_BYTES, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800398 assertTrue(mKeyStore.contains(TEST_KEYNAME));
399 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
400 }
401
402 public void testImport_grantedUid_Wifi_Success() throws Exception {
403 assertTrue(mKeyStore.password(TEST_PASSWD));
404
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700405 assertTrue("Should be able to import key when unlocked", mKeyStore.importKey(TEST_KEYNAME,
406 PRIVKEY_BYTES, Process.WIFI_UID, KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800407 assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
408 assertFalse(mKeyStore.contains(TEST_KEYNAME));
409 }
410
411 public void testImport_ungrantedUid_Bluetooth_Failure() throws Exception {
412 assertTrue(mKeyStore.password(TEST_PASSWD));
413
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700414 assertFalse(mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES, Process.BLUETOOTH_UID,
415 KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800416 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
417 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
418 assertFalse(mKeyStore.contains(TEST_KEYNAME));
Kenny Root5423e682011-11-14 08:43:13 -0800419 }
420
421 public void testImport_Failure_BadEncoding() throws Exception {
422 mKeyStore.password(TEST_PASSWD);
423
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700424 assertFalse("Invalid DER-encoded key should not be imported", mKeyStore.importKey(
425 TEST_KEYNAME, TEST_DATA, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root78ad8492013-02-13 17:02:57 -0800426 assertFalse(mKeyStore.contains(TEST_KEYNAME));
427 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
Kenny Root5423e682011-11-14 08:43:13 -0800428 }
429
430 public void testSign_Success() throws Exception {
431 mKeyStore.password(TEST_PASSWD);
432
Kenny Rootf64386f2013-08-16 14:03:29 -0700433 assertTrue(mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
434 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root78ad8492013-02-13 17:02:57 -0800435 assertTrue(mKeyStore.contains(TEST_KEYNAME));
Kenny Root5423e682011-11-14 08:43:13 -0800436 final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA);
437
438 assertNotNull("Signature should not be null", signature);
439 }
440
441 public void testVerify_Success() throws Exception {
442 mKeyStore.password(TEST_PASSWD);
443
Kenny Rootf64386f2013-08-16 14:03:29 -0700444 assertTrue(mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
445 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root78ad8492013-02-13 17:02:57 -0800446 assertTrue(mKeyStore.contains(TEST_KEYNAME));
Kenny Root5423e682011-11-14 08:43:13 -0800447 final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA);
448
449 assertNotNull("Signature should not be null", signature);
450
451 assertTrue("Signature should verify with same data",
452 mKeyStore.verify(TEST_KEYNAME, TEST_DATA, signature));
453 }
454
455 public void testSign_NotInitialized_Failure() throws Exception {
456 assertNull("Should not be able to sign without first initializing the keystore",
457 mKeyStore.sign(TEST_KEYNAME, TEST_DATA));
458 }
459
460 public void testSign_NotGenerated_Failure() throws Exception {
461 mKeyStore.password(TEST_PASSWD);
462
463 assertNull("Should not be able to sign without first generating keys",
464 mKeyStore.sign(TEST_KEYNAME, TEST_DATA));
465 }
466
467 public void testGrant_Generated_Success() throws Exception {
468 assertTrue("Password should work for keystore",
469 mKeyStore.password(TEST_PASSWD));
470
471 assertTrue("Should be able to generate key for testcase",
Kenny Rootf64386f2013-08-16 14:03:29 -0700472 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
473 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800474
475 assertTrue("Should be able to grant key to other user",
476 mKeyStore.grant(TEST_KEYNAME, 0));
477 }
478
479 public void testGrant_Imported_Success() throws Exception {
480 assertTrue("Password should work for keystore", mKeyStore.password(TEST_PASSWD));
481
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700482 assertTrue("Should be able to import key for testcase", mKeyStore.importKey(TEST_KEYNAME,
483 PRIVKEY_BYTES, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root5423e682011-11-14 08:43:13 -0800484
485 assertTrue("Should be able to grant key to other user", mKeyStore.grant(TEST_KEYNAME, 0));
486 }
487
488 public void testGrant_NoKey_Failure() throws Exception {
489 assertTrue("Should be able to unlock keystore for test",
490 mKeyStore.password(TEST_PASSWD));
491
492 assertFalse("Should not be able to grant without first initializing the keystore",
493 mKeyStore.grant(TEST_KEYNAME, 0));
494 }
495
496 public void testGrant_NotInitialized_Failure() throws Exception {
497 assertFalse("Should not be able to grant without first initializing the keystore",
498 mKeyStore.grant(TEST_KEYNAME, 0));
499 }
500
501 public void testUngrant_Generated_Success() throws Exception {
502 assertTrue("Password should work for keystore",
503 mKeyStore.password(TEST_PASSWD));
504
505 assertTrue("Should be able to generate key for testcase",
Kenny Rootf64386f2013-08-16 14:03:29 -0700506 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
507 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800508
509 assertTrue("Should be able to grant key to other user",
510 mKeyStore.grant(TEST_KEYNAME, 0));
511
512 assertTrue("Should be able to ungrant key to other user",
513 mKeyStore.ungrant(TEST_KEYNAME, 0));
514 }
515
516 public void testUngrant_Imported_Success() throws Exception {
517 assertTrue("Password should work for keystore",
518 mKeyStore.password(TEST_PASSWD));
519
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700520 assertTrue("Should be able to import key for testcase", mKeyStore.importKey(TEST_KEYNAME,
521 PRIVKEY_BYTES, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root5423e682011-11-14 08:43:13 -0800522
523 assertTrue("Should be able to grant key to other user",
524 mKeyStore.grant(TEST_KEYNAME, 0));
525
526 assertTrue("Should be able to ungrant key to other user",
527 mKeyStore.ungrant(TEST_KEYNAME, 0));
528 }
529
530 public void testUngrant_NotInitialized_Failure() throws Exception {
531 assertFalse("Should fail to ungrant key when keystore not initialized",
532 mKeyStore.ungrant(TEST_KEYNAME, 0));
533 }
534
535 public void testUngrant_NoGrant_Failure() throws Exception {
536 assertTrue("Password should work for keystore",
537 mKeyStore.password(TEST_PASSWD));
538
539 assertTrue("Should be able to generate key for testcase",
Kenny Rootf64386f2013-08-16 14:03:29 -0700540 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
541 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800542
543 assertFalse("Should not be able to revoke not existent grant",
544 mKeyStore.ungrant(TEST_KEYNAME, 0));
545 }
546
547 public void testUngrant_DoubleUngrant_Failure() throws Exception {
548 assertTrue("Password should work for keystore",
549 mKeyStore.password(TEST_PASSWD));
550
551 assertTrue("Should be able to generate key for testcase",
Kenny Rootf64386f2013-08-16 14:03:29 -0700552 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
553 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800554
555 assertTrue("Should be able to grant key to other user",
556 mKeyStore.grant(TEST_KEYNAME, 0));
557
558 assertTrue("Should be able to ungrant key to other user",
559 mKeyStore.ungrant(TEST_KEYNAME, 0));
560
561 assertFalse("Should fail to ungrant key to other user second time",
562 mKeyStore.ungrant(TEST_KEYNAME, 0));
563 }
564
565 public void testUngrant_DoubleGrantUngrant_Failure() throws Exception {
566 assertTrue("Password should work for keystore",
567 mKeyStore.password(TEST_PASSWD));
568
569 assertTrue("Should be able to generate key for testcase",
Kenny Rootf64386f2013-08-16 14:03:29 -0700570 mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
571 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Root5423e682011-11-14 08:43:13 -0800572
573 assertTrue("Should be able to grant key to other user",
574 mKeyStore.grant(TEST_KEYNAME, 0));
575
576 assertTrue("Should be able to grant key to other user a second time",
577 mKeyStore.grant(TEST_KEYNAME, 0));
578
579 assertTrue("Should be able to ungrant key to other user",
580 mKeyStore.ungrant(TEST_KEYNAME, 0));
581
582 assertFalse("Should fail to ungrant key to other user second time",
583 mKeyStore.ungrant(TEST_KEYNAME, 0));
584 }
Kenny Root473c7122012-08-17 21:13:48 -0700585
Kenny Root5f1d9652013-03-21 14:21:50 -0700586 public void testDuplicate_grantedUid_Wifi_Success() throws Exception {
Kenny Rootbd794192013-03-20 11:36:50 -0700587 assertTrue(mKeyStore.password(TEST_PASSWD));
588
589 assertFalse(mKeyStore.contains(TEST_KEYNAME));
590
Kenny Rootf64386f2013-08-16 14:03:29 -0700591 assertTrue(mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
592 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Rootbd794192013-03-20 11:36:50 -0700593
594 assertTrue(mKeyStore.contains(TEST_KEYNAME));
595 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
596
Kenny Root5f1d9652013-03-21 14:21:50 -0700597 // source doesn't exist
598 assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, -1, TEST_KEYNAME1, Process.WIFI_UID));
599 assertFalse(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
Kenny Rootbd794192013-03-20 11:36:50 -0700600
Kenny Root5f1d9652013-03-21 14:21:50 -0700601 // Copy from current UID to granted UID
602 assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID));
603 assertTrue(mKeyStore.contains(TEST_KEYNAME));
604 assertFalse(mKeyStore.contains(TEST_KEYNAME1));
605 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
606 assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
607 assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID));
608
609 // Copy from granted UID to same granted UID
610 assertTrue(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2,
611 Process.WIFI_UID));
612 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
613 assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
614 assertTrue(mKeyStore.contains(TEST_KEYNAME2, Process.WIFI_UID));
615 assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2,
616 Process.WIFI_UID));
617
618 assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1));
619 assertTrue(mKeyStore.contains(TEST_KEYNAME));
620 assertFalse(mKeyStore.contains(TEST_KEYNAME1));
621 assertTrue(mKeyStore.contains(TEST_KEYNAME2));
622 assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1));
Kenny Rootbd794192013-03-20 11:36:50 -0700623 }
624
Kenny Root5f1d9652013-03-21 14:21:50 -0700625 public void testDuplicate_ungrantedUid_Bluetooth_Failure() throws Exception {
Kenny Rootbd794192013-03-20 11:36:50 -0700626 assertTrue(mKeyStore.password(TEST_PASSWD));
627
628 assertFalse(mKeyStore.contains(TEST_KEYNAME));
629
Kenny Rootf64386f2013-08-16 14:03:29 -0700630 assertTrue(mKeyStore.generate(TEST_KEYNAME, KeyStore.UID_SELF, NativeCrypto.EVP_PKEY_RSA,
631 1024, KeyStore.FLAG_ENCRYPTED, null));
Kenny Rootbd794192013-03-20 11:36:50 -0700632
633 assertTrue(mKeyStore.contains(TEST_KEYNAME));
634 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
635
Kenny Root5f1d9652013-03-21 14:21:50 -0700636 assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, Process.BLUETOOTH_UID));
637 assertFalse(mKeyStore.duplicate(TEST_KEYNAME, Process.BLUETOOTH_UID, TEST_KEYNAME2,
638 Process.BLUETOOTH_UID));
Kenny Rootbd794192013-03-20 11:36:50 -0700639
640 assertTrue(mKeyStore.contains(TEST_KEYNAME));
641 assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
642 }
643
Kenny Root473c7122012-08-17 21:13:48 -0700644 /**
645 * The amount of time to allow before and after expected time for variance
646 * in timing tests.
647 */
648 private static final long SLOP_TIME_MILLIS = 15000L;
649
650 public void testGetmtime_Success() throws Exception {
651 assertTrue("Password should work for keystore",
652 mKeyStore.password(TEST_PASSWD));
653
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700654 assertTrue("Should be able to import key when unlocked", mKeyStore.importKey(TEST_KEYNAME,
655 PRIVKEY_BYTES, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root473c7122012-08-17 21:13:48 -0700656
657 long now = System.currentTimeMillis();
658 long actual = mKeyStore.getmtime(TEST_KEYNAME);
659
660 long expectedAfter = now - SLOP_TIME_MILLIS;
661 long expectedBefore = now + SLOP_TIME_MILLIS;
662
663 assertLessThan("Time should be close to current time", expectedBefore, actual);
664 assertGreaterThan("Time should be close to current time", expectedAfter, actual);
665 }
666
667 private static void assertLessThan(String explanation, long expectedBefore, long actual) {
668 if (actual >= expectedBefore) {
669 throw new AssertionFailedError(explanation + ": actual=" + actual
670 + ", expected before: " + expectedBefore);
671 }
672 }
673
674 private static void assertGreaterThan(String explanation, long expectedAfter, long actual) {
675 if (actual <= expectedAfter) {
676 throw new AssertionFailedError(explanation + ": actual=" + actual
677 + ", expected after: " + expectedAfter);
678 }
679 }
680
681 public void testGetmtime_NonExist_Failure() throws Exception {
682 assertTrue("Password should work for keystore",
683 mKeyStore.password(TEST_PASSWD));
684
Kenny Rootb2c0ff62013-04-12 17:36:25 -0700685 assertTrue("Should be able to import key when unlocked", mKeyStore.importKey(TEST_KEYNAME,
686 PRIVKEY_BYTES, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
Kenny Root473c7122012-08-17 21:13:48 -0700687
688 assertEquals("-1 should be returned for non-existent key",
689 -1L, mKeyStore.getmtime(TEST_KEYNAME2));
690 }
Chung-yih Wangf35e9662009-09-29 11:20:28 +0800691}