KeyStoreTest.java revision 5f1d965f7d7e1df50981ffed8faa11fbcc17ca22
1f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang/*
2f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * Copyright (C) 2009 The Android Open Source Project
3f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *
4f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * Licensed under the Apache License, Version 2.0 (the "License");
5f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * you may not use this file except in compliance with the License.
6f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * You may obtain a copy of the License at
7f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *
8f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *      http://www.apache.org/licenses/LICENSE-2.0
9f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *
10f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * Unless required by applicable law or agreed to in writing, software
11f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * distributed under the License is distributed on an "AS IS" BASIS,
12f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * See the License for the specific language governing permissions and
14f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * limitations under the License.
15f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang */
16f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
1796ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Rootpackage android.security;
18f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
19f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wangimport android.app.Activity;
2078ad849163a7b01073b46fbd7d818392720005d1Kenny Rootimport android.os.Process;
21f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wangimport android.security.KeyStore;
22f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wangimport android.test.ActivityUnitTestCase;
23473c712b19bad992ab4eafcd43175fdce77b913dKenny Rootimport android.test.AssertionFailedError;
24f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wangimport android.test.suitebuilder.annotation.MediumTest;
255cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstromimport java.nio.charset.Charsets;
265cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstromimport java.util.Arrays;
27473c712b19bad992ab4eafcd43175fdce77b913dKenny Rootimport java.util.Date;
285cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstromimport java.util.HashSet;
29f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
30f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang/**
31f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * Junit / Instrumentation test case for KeyStore class
32f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *
33f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang * Running the test suite:
34f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang *
3596ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Root *  runtest keystore-unit
3696ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Root *
3796ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Root * Or this individual test case:
3896ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Root *
3996ad6cb080d0721a433d2bcb201f4a4582bf1cafKenny Root *  runtest --path frameworks/base/keystore/tests/src/android/security/KeyStoreTest.java
40f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang */
41f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang@MediumTest
42f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wangpublic class KeyStoreTest extends ActivityUnitTestCase<Activity> {
43f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    private static final String TEST_PASSWD = "12345678";
44f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    private static final String TEST_PASSWD2 = "87654321";
455ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom    private static final String TEST_KEYNAME = "test-key";
465ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom    private static final String TEST_KEYNAME1 = "test-key.1";
472a5b147ec8fc1235af928042bdfb78170b18067bBrian Carlstrom    private static final String TEST_KEYNAME2 = "test-key\02";
485cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom    private static final byte[] TEST_KEYVALUE = "test value".getBytes(Charsets.UTF_8);
49f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
5034c47c855815d731e6deb55748ff690b0ec7b53fNick Kralevich    // "Hello, World" in Chinese
515cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom    private static final String TEST_I18N_KEY = "\u4F60\u597D, \u4E16\u754C";
525cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom    private static final byte[] TEST_I18N_VALUE = TEST_I18N_KEY.getBytes(Charsets.UTF_8);
5334c47c855815d731e6deb55748ff690b0ec7b53fNick Kralevich
545423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    // Test vector data for signatures
555ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom    private static final byte[] TEST_DATA =  new byte[256];
565ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom    static {
575ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom        for (int i = 0; i < TEST_DATA.length; i++) {
585ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom            TEST_DATA[i] = (byte) i;
595ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom        }
605ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom    }
615423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
62f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    private KeyStore mKeyStore = null;
63f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
64f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public KeyStoreTest() {
65f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        super(Activity.class);
66f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
67f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
685423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    private static final byte[] PRIVKEY_BYTES = hexToBytes(
695423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "308204BE020100300D06092A864886F70D0101010500048204A8308204A4020100028201" +
705423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "0100E0473E8AB8F2284FEB9E742FF9748FA118ED98633C92F52AEB7A2EBE0D3BE60329BE" +
715423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "766AD10EB6A515D0D2CFD9BEA7930F0C306537899F7958CD3E85B01F8818524D312584A9" +
725423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "4B251E3625B54141EDBFEE198808E1BB97FC7CB49B9EAAAF68E9C98D7D0EDC53BBC0FA00" +
735423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "34356D6305FBBCC3C7001405386ABBC873CB0F3EF7425F3D33DF7B315AE036D2A0B66AFD" +
745423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "47503B169BF36E3B5162515B715FDA83DEAF2C58AEB9ABFB3097C3CC9DD9DBE5EF296C17" +
755423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "6139028E8A671E63056D45F40188D2C4133490845DE52C2534E9C6B2478C07BDAE928823" +
765423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "B62D066C7770F9F63F3DBA247F530844747BE7AAA85D853B8BD244ACEC3DE3C89AB46453" +
775423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "AB4D24C3AC6902030100010282010037784776A5F17698F5AC960DFB83A1B67564E648BD" +
785423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "0597CF8AB8087186F2669C27A9ECBDD480F0197A80D07309E6C6A96F925331E57F8B4AC6" +
795423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "F4D45EDA45A23269C09FC428C07A4E6EDF738A15DEC97FABD2F2BB47A14F20EA72FCFE4C" +
805423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "36E01ADA77BD137CD8D4DA10BB162E94A4662971F175F985FA188F056CB97EE2816F43AB" +
815423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "9D3747612486CDA8C16196C30818A995EC85D38467791267B3BF21F273710A6925862576" +
825423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "841C5B6712C12D4BD20A2F3299ADB7C135DA5E9515ABDA76E7CAF2A3BE80551D073B78BF" +
835423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "1162C48AD2B7F4743A0238EE4D252F7D5E7E6533CCAE64CCB39360075A2FD1E034EC3AE5" +
845423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "CE9C408CCBF0E25E4114021687B3DD4754AE8102818100F541884BC3737B2922D4119EF4" +
855423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "5E2DEE2CD4CBB75F45505A157AA5009F99C73A2DF0724AC46024306332EA898177634546" +
865423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "5DC6DF1E0A6F140AFF3B7396E6A8994AC5DAA96873472FE37749D14EB3E075E629DBEB35" +
875423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "83338A6F3649D0A2654A7A42FD9AB6BFA4AC4D481D390BB229B064BDC311CC1BE1B63189" +
885423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "DA7C40CDECF2B102818100EA1A742DDB881CEDB7288C87E38D868DD7A409D15A43F445D5" +
895423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "377A0B5731DDBFCA2DAF28A8E13CD5C0AFCEC3347D74A39E235A3CD9633F274DE2B94F92" +
905423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "DF43833911D9E9F1CF58F27DE2E08FF45964C720D3EC2139DC7CAFC912953CDECB2F355A" +
915423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "2E2C35A50FAD754CB3B23166424BA3B6E3112A2B898C38C5C15EDB238693390281805182" +
925423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "8F1EC6FD996029901BAF1D7E337BA5F0AF27E984EAD895ACE62BD7DF4EE45A224089F2CC" +
935423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "151AF3CD173FCE0474BCB04F386A2CDCC0E0036BA2419F54579262D47100BE931984A3EF" +
945423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "A05BECF141574DC079B3A95C4A83E6C43F3214D6DF32D512DE198085E531E616B83FD7DD" +
955423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "9D1F4E2607C3333D07C55D107D1D3893587102818100DB4FB50F50DE8EDB53FF34C80931" +
965423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "88A0512867DA2CCA04897759E587C244010DAF8664D59E8083D16C164789301F67A9F078" +
975423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "060D834A2ADBD367575B68A8A842C2B02A89B3F31FCCEC8A22FE395795C5C6C7422B4E5D" +
985423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "74A1E9A8F30E7759B9FC2D639C1F15673E84E93A5EF1506F4315383C38D45CBD1B14048F" +
995423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "4721DC82326102818100D8114593AF415FB612DBF1923710D54D07486205A76A3B431949" +
1005423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "68C0DFF1F11EF0F61A4A337D5FD3741BBC9640E447B8B6B6C47C3AC1204357D3B0C55BA9" +
1015423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "286BDA73F629296F5FA9146D8976357D3C751E75148696A40B74685C82CE30902D639D72" +
1025423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            "4FF24D5E2E9407EE34EDED2E3B4DF65AA9BCFEB6DF28D07BA6903F165768");
1035423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
1045423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
1055423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    private static byte[] hexToBytes(String s) {
1065423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        int len = s.length();
1075423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        byte[] data = new byte[len / 2];
1085423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        for (int i = 0; i < len; i += 2) {
1095423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(
1105423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                    s.charAt(i + 1), 16));
1115423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        }
1125423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        return data;
1135423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
1145423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
115f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    @Override
116f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    protected void setUp() throws Exception {
117f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore = KeyStore.getInstance();
1185cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        if (mKeyStore.state() != KeyStore.State.UNINITIALIZED) {
1195cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom            mKeyStore.reset();
1205cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        }
1215423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertEquals("KeyStore should be in an uninitialized state",
1225423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                KeyStore.State.UNINITIALIZED, mKeyStore.state());
123f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        super.setUp();
124f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
125f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
126f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    @Override
127f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    protected void tearDown() throws Exception {
128f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.reset();
129f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        super.tearDown();
130f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
131f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
13278ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testState() throws Exception {
1335cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(KeyStore.State.UNINITIALIZED, mKeyStore.state());
134f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
135f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
136f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testPassword() throws Exception {
137f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.password(TEST_PASSWD));
1385cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
139f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
140f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
141bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom    public void testGet() throws Exception {
142bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertNull(mKeyStore.get(TEST_KEYNAME));
143bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        mKeyStore.password(TEST_PASSWD);
144bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertNull(mKeyStore.get(TEST_KEYNAME));
145bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
146bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
147bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom    }
148bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom
149f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testPut() throws Exception {
150bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertNull(mKeyStore.get(TEST_KEYNAME));
151f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
152f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.contains(TEST_KEYNAME));
153f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.password(TEST_PASSWD);
154f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
155bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
156f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
157f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
15878ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testPut_grantedUid_Wifi() throws Exception {
15978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
16078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID));
16178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
16278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
16378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID));
16478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
16578ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
16678ad849163a7b01073b46fbd7d818392720005d1Kenny Root
16778ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testPut_ungrantedUid_Bluetooth() throws Exception {
16878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
16978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID));
17078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
17178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
17278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID));
17378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
17478ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
17578ad849163a7b01073b46fbd7d818392720005d1Kenny Root
17634c47c855815d731e6deb55748ff690b0ec7b53fNick Kralevich    public void testI18n() throws Exception {
1775cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertFalse(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE));
1785cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertFalse(mKeyStore.contains(TEST_I18N_KEY));
1795cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        mKeyStore.password(TEST_I18N_KEY);
1805cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertTrue(mKeyStore.put(TEST_I18N_KEY, TEST_I18N_VALUE));
1815cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertTrue(mKeyStore.contains(TEST_I18N_KEY));
18234c47c855815d731e6deb55748ff690b0ec7b53fNick Kralevich    }
18334c47c855815d731e6deb55748ff690b0ec7b53fNick Kralevich
184f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testDelete() throws Exception {
1855ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom        assertFalse(mKeyStore.delete(TEST_KEYNAME));
186f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.password(TEST_PASSWD);
1875ea68db37fd5ad4e0ddc0745b4347e86f17f78dbBrian Carlstrom        assertFalse(mKeyStore.delete(TEST_KEYNAME));
188f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
18978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
190bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertTrue(Arrays.equals(TEST_KEYVALUE, mKeyStore.get(TEST_KEYNAME)));
191f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.delete(TEST_KEYNAME));
192bef5e5aabcb6ab440829f4418d1cbc268564eee0Brian Carlstrom        assertNull(mKeyStore.get(TEST_KEYNAME));
193f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
194f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
19578ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testDelete_grantedUid_Wifi() throws Exception {
19678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
19778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
19878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
19978ad849163a7b01073b46fbd7d818392720005d1Kenny Root
20078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID));
20178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
20278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.delete(TEST_KEYNAME, Process.WIFI_UID));
20378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
20478ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
20578ad849163a7b01073b46fbd7d818392720005d1Kenny Root
20678ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testDelete_ungrantedUid_Bluetooth() throws Exception {
20778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
20878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
20978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
21078ad849163a7b01073b46fbd7d818392720005d1Kenny Root
21178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID));
21278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
21378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.delete(TEST_KEYNAME, Process.BLUETOOTH_UID));
21478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
21578ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
21678ad849163a7b01073b46fbd7d818392720005d1Kenny Root
217f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testContains() throws Exception {
218f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.contains(TEST_KEYNAME));
219f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
22078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
221f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.contains(TEST_KEYNAME));
222f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
22378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
224f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.contains(TEST_KEYNAME));
225f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
226f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
22778ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testContains_grantedUid_Wifi() throws Exception {
22878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
22978ad849163a7b01073b46fbd7d818392720005d1Kenny Root
23078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
23178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
23278ad849163a7b01073b46fbd7d818392720005d1Kenny Root
23378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.WIFI_UID));
23478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
23578ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
23678ad849163a7b01073b46fbd7d818392720005d1Kenny Root
23778ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testContains_grantedUid_Bluetooth() throws Exception {
23878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
23978ad849163a7b01073b46fbd7d818392720005d1Kenny Root
24078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
24178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
24278ad849163a7b01073b46fbd7d818392720005d1Kenny Root
24378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE, Process.BLUETOOTH_UID));
24478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
24578ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
24678ad849163a7b01073b46fbd7d818392720005d1Kenny Root
247f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testSaw() throws Exception {
2485cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        String[] emptyResult = mKeyStore.saw(TEST_KEYNAME);
2495cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertNotNull(emptyResult);
2505cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(0, emptyResult.length);
251f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
252f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.password(TEST_PASSWD);
253f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE);
254f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE);
255f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
2565cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        String[] results = mKeyStore.saw(TEST_KEYNAME);
2575cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
2585cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom                                               TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
2595cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom                     new HashSet(Arrays.asList(results)));
260f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
261f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
26278ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testSaw_ungrantedUid_Bluetooth() throws Exception {
26378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID);
26478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertNull(results1);
26578ad849163a7b01073b46fbd7d818392720005d1Kenny Root
26678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
26778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE);
26878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE);
26978ad849163a7b01073b46fbd7d818392720005d1Kenny Root
27078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.BLUETOOTH_UID);
27178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertNull(results2);
27278ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
27378ad849163a7b01073b46fbd7d818392720005d1Kenny Root
27478ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testSaw_grantedUid_Wifi() throws Exception {
27578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID);
27678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertNotNull(results1);
27778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertEquals(0, results1.length);
27878ad849163a7b01073b46fbd7d818392720005d1Kenny Root
27978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
28078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.WIFI_UID);
28178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.WIFI_UID);
28278ad849163a7b01073b46fbd7d818392720005d1Kenny Root
28378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.WIFI_UID);
28478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
28578ad849163a7b01073b46fbd7d818392720005d1Kenny Root                                               TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
28678ad849163a7b01073b46fbd7d818392720005d1Kenny Root                     new HashSet(Arrays.asList(results2)));
28778ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
28878ad849163a7b01073b46fbd7d818392720005d1Kenny Root
28978ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testSaw_grantedUid_Vpn() throws Exception {
29078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results1 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID);
29178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertNotNull(results1);
29278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertEquals(0, results1.length);
29378ad849163a7b01073b46fbd7d818392720005d1Kenny Root
29478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.password(TEST_PASSWD);
29578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE, Process.VPN_UID);
29678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE, Process.VPN_UID);
29778ad849163a7b01073b46fbd7d818392720005d1Kenny Root
29878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        String[] results2 = mKeyStore.saw(TEST_KEYNAME, Process.VPN_UID);
29978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertEquals(new HashSet(Arrays.asList(TEST_KEYNAME1.substring(TEST_KEYNAME.length()),
30078ad849163a7b01073b46fbd7d818392720005d1Kenny Root                                               TEST_KEYNAME2.substring(TEST_KEYNAME.length()))),
30178ad849163a7b01073b46fbd7d818392720005d1Kenny Root                     new HashSet(Arrays.asList(results2)));
30278ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
30378ad849163a7b01073b46fbd7d818392720005d1Kenny Root
304f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testLock() throws Exception {
305f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.lock());
306f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
307f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.password(TEST_PASSWD);
3085cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
309f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
310f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.lock());
3115cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(KeyStore.State.LOCKED, mKeyStore.state());
312f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
313f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
314f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    public void testUnlock() throws Exception {
315f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.password(TEST_PASSWD);
3165cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertEquals(KeyStore.State.UNLOCKED, mKeyStore.state());
317f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        mKeyStore.lock();
318f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang
319f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertFalse(mKeyStore.unlock(TEST_PASSWD2));
320f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang        assertTrue(mKeyStore.unlock(TEST_PASSWD));
321f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang    }
3225cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom
3235cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom    public void testIsEmpty() throws Exception {
3245cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertTrue(mKeyStore.isEmpty());
3255cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        mKeyStore.password(TEST_PASSWD);
3265cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertTrue(mKeyStore.isEmpty());
3275cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE);
3285cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertFalse(mKeyStore.isEmpty());
3295cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        mKeyStore.reset();
3305cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom        assertTrue(mKeyStore.isEmpty());
3315cfee3fabb3482c6a6df1c8b6f21e843cf214527Brian Carlstrom    }
3325423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3335423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGenerate_NotInitialized_Fail() throws Exception {
3345423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should fail when keystore is not initialized",
3355423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
3365423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
3375423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3385423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGenerate_Locked_Fail() throws Exception {
3395423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.password(TEST_PASSWD);
3405423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.lock();
3415423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should fail when keystore is locked", mKeyStore.generate(TEST_KEYNAME));
3425423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
3435423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3445423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGenerate_Success() throws Exception {
34578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
3465423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3475423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key when unlocked",
3485423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
34978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
35078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
35178ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
35278ad849163a7b01073b46fbd7d818392720005d1Kenny Root
35378ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testGenerate_grantedUid_Wifi_Success() throws Exception {
35478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
35578ad849163a7b01073b46fbd7d818392720005d1Kenny Root
35678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue("Should be able to generate key when unlocked",
35778ad849163a7b01073b46fbd7d818392720005d1Kenny Root                mKeyStore.generate(TEST_KEYNAME, Process.WIFI_UID));
35878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
35978ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
36078ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
36178ad849163a7b01073b46fbd7d818392720005d1Kenny Root
36278ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testGenerate_ungrantedUid_Bluetooth_Failure() throws Exception {
36378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
36478ad849163a7b01073b46fbd7d818392720005d1Kenny Root
36578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.generate(TEST_KEYNAME, Process.BLUETOOTH_UID));
36678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
36778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
36878ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
3695423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
3705423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3715423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testImport_Success() throws Exception {
37278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
3735423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3745423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to import key when unlocked",
3755423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES));
37678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
37778ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
37878ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
37978ad849163a7b01073b46fbd7d818392720005d1Kenny Root
38078ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testImport_grantedUid_Wifi_Success() throws Exception {
38178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
38278ad849163a7b01073b46fbd7d818392720005d1Kenny Root
38378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue("Should be able to import key when unlocked",
38478ad849163a7b01073b46fbd7d818392720005d1Kenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES, Process.WIFI_UID));
38578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
38678ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
38778ad849163a7b01073b46fbd7d818392720005d1Kenny Root    }
38878ad849163a7b01073b46fbd7d818392720005d1Kenny Root
38978ad849163a7b01073b46fbd7d818392720005d1Kenny Root    public void testImport_ungrantedUid_Bluetooth_Failure() throws Exception {
39078ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
39178ad849163a7b01073b46fbd7d818392720005d1Kenny Root
39278ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES, Process.BLUETOOTH_UID));
39378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
39478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
39578ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
3965423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
3975423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
3985423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testImport_Failure_BadEncoding() throws Exception {
3995423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.password(TEST_PASSWD);
4005423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4015423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Invalid DER-encoded key should not be imported",
4025423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.importKey(TEST_KEYNAME, TEST_DATA));
40378ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
40478ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
4055423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4065423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4075423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testSign_Success() throws Exception {
4085423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.password(TEST_PASSWD);
4095423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4105423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue(mKeyStore.generate(TEST_KEYNAME));
41178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
4125423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA);
4135423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4145423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertNotNull("Signature should not be null", signature);
4155423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4165423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4175423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testVerify_Success() throws Exception {
4185423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.password(TEST_PASSWD);
4195423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4205423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue(mKeyStore.generate(TEST_KEYNAME));
42178ad849163a7b01073b46fbd7d818392720005d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
4225423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        final byte[] signature = mKeyStore.sign(TEST_KEYNAME, TEST_DATA);
4235423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4245423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertNotNull("Signature should not be null", signature);
4255423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4265423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Signature should verify with same data",
4275423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.verify(TEST_KEYNAME, TEST_DATA, signature));
4285423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4295423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4305423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testSign_NotInitialized_Failure() throws Exception {
4315423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertNull("Should not be able to sign without first initializing the keystore",
4325423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.sign(TEST_KEYNAME, TEST_DATA));
4335423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4345423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4355423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testSign_NotGenerated_Failure() throws Exception {
4365423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        mKeyStore.password(TEST_PASSWD);
4375423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4385423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertNull("Should not be able to sign without first generating keys",
4395423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.sign(TEST_KEYNAME, TEST_DATA));
4405423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4415423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4425423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGrant_Generated_Success() throws Exception {
4435423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
4445423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
4455423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4465423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key for testcase",
4475423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
4485423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4495423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user",
4505423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
4515423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4525423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4535423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGrant_Imported_Success() throws Exception {
4545423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore", mKeyStore.password(TEST_PASSWD));
4555423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4565423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to import key for testcase",
4575423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES));
4585423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4595423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user", mKeyStore.grant(TEST_KEYNAME, 0));
4605423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4615423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4625423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGrant_NoKey_Failure() throws Exception {
4635423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to unlock keystore for test",
4645423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
4655423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4665423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should not be able to grant without first initializing the keystore",
4675423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
4685423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4695423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4705423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testGrant_NotInitialized_Failure() throws Exception {
4715423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should not be able to grant without first initializing the keystore",
4725423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
4735423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4745423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4755423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_Generated_Success() throws Exception {
4765423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
4775423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
4785423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4795423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key for testcase",
4805423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
4815423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4825423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user",
4835423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
4845423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4855423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to ungrant key to other user",
4865423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
4875423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
4885423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4895423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_Imported_Success() throws Exception {
4905423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
4915423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
4925423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4935423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to import key for testcase",
4945423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES));
4955423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4965423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user",
4975423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
4985423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
4995423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to ungrant key to other user",
5005423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5015423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
5025423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5035423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_NotInitialized_Failure() throws Exception {
5045423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should fail to ungrant key when keystore not initialized",
5055423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5065423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
5075423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5085423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_NoGrant_Failure() throws Exception {
5095423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
5105423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
5115423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5125423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key for testcase",
5135423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
5145423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5155423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should not be able to revoke not existent grant",
5165423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5175423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
5185423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5195423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_DoubleUngrant_Failure() throws Exception {
5205423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
5215423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
5225423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5235423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key for testcase",
5245423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
5255423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5265423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user",
5275423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
5285423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5295423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to ungrant key to other user",
5305423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5315423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5325423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should fail to ungrant key to other user second time",
5335423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5345423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
5355423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5365423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    public void testUngrant_DoubleGrantUngrant_Failure() throws Exception {
5375423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Password should work for keystore",
5385423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.password(TEST_PASSWD));
5395423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5405423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to generate key for testcase",
5415423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.generate(TEST_KEYNAME));
5425423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5435423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user",
5445423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
5455423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5465423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to grant key to other user a second time",
5475423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.grant(TEST_KEYNAME, 0));
5485423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5495423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertTrue("Should be able to ungrant key to other user",
5505423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5515423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root
5525423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root        assertFalse("Should fail to ungrant key to other user second time",
5535423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root                mKeyStore.ungrant(TEST_KEYNAME, 0));
5545423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root    }
555473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
5565f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root    public void testDuplicate_grantedUid_Wifi_Success() throws Exception {
557bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
558bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
559bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
560bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
561bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.generate(TEST_KEYNAME));
562bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
563bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
564bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
565bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
5665f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        // source doesn't exist
5675f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, -1, TEST_KEYNAME1, Process.WIFI_UID));
5685f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
569bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
5705f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        // Copy from current UID to granted UID
5715f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID));
5725f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
5735f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME1));
5745f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
5755f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
5765f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME1, Process.WIFI_UID));
5775f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root
5785f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        // Copy from granted UID to same granted UID
5795f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2,
5805f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root                Process.WIFI_UID));
5815f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.WIFI_UID));
5825f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME1, Process.WIFI_UID));
5835f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME2, Process.WIFI_UID));
5845f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME1, Process.WIFI_UID, TEST_KEYNAME2,
5855f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root                Process.WIFI_UID));
5865f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root
5875f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1));
5885f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
5895f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME1));
5905f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME2));
5915f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, -1));
592bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root    }
593bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
5945f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root    public void testDuplicate_ungrantedUid_Bluetooth_Failure() throws Exception {
595bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.password(TEST_PASSWD));
596bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
597bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME));
598bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
599bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.generate(TEST_KEYNAME));
600bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
601bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
602bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
603bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
6045f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME, -1, TEST_KEYNAME2, Process.BLUETOOTH_UID));
6055f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root        assertFalse(mKeyStore.duplicate(TEST_KEYNAME, Process.BLUETOOTH_UID, TEST_KEYNAME2,
6065f1d965f7d7e1df50981ffed8faa11fbcc17ca22Kenny Root                Process.BLUETOOTH_UID));
607bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
608bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertTrue(mKeyStore.contains(TEST_KEYNAME));
609bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root        assertFalse(mKeyStore.contains(TEST_KEYNAME, Process.BLUETOOTH_UID));
610bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root    }
611bd79419ef84ae31f3765721b50aa413fa462d1d1Kenny Root
612473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    /**
613473c712b19bad992ab4eafcd43175fdce77b913dKenny Root     * The amount of time to allow before and after expected time for variance
614473c712b19bad992ab4eafcd43175fdce77b913dKenny Root     * in timing tests.
615473c712b19bad992ab4eafcd43175fdce77b913dKenny Root     */
616473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    private static final long SLOP_TIME_MILLIS = 15000L;
617473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
618473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    public void testGetmtime_Success() throws Exception {
619473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertTrue("Password should work for keystore",
620473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                mKeyStore.password(TEST_PASSWD));
621473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
622473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertTrue("Should be able to import key when unlocked",
623473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES));
624473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
625473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        long now = System.currentTimeMillis();
626473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        long actual = mKeyStore.getmtime(TEST_KEYNAME);
627473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
628473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        long expectedAfter = now - SLOP_TIME_MILLIS;
629473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        long expectedBefore = now + SLOP_TIME_MILLIS;
630473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
631473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertLessThan("Time should be close to current time", expectedBefore, actual);
632473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertGreaterThan("Time should be close to current time", expectedAfter, actual);
633473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    }
634473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
635473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    private static void assertLessThan(String explanation, long expectedBefore, long actual) {
636473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        if (actual >= expectedBefore) {
637473c712b19bad992ab4eafcd43175fdce77b913dKenny Root            throw new AssertionFailedError(explanation + ": actual=" + actual
638473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                    + ", expected before: " + expectedBefore);
639473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        }
640473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    }
641473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
642473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    private static void assertGreaterThan(String explanation, long expectedAfter, long actual) {
643473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        if (actual <= expectedAfter) {
644473c712b19bad992ab4eafcd43175fdce77b913dKenny Root            throw new AssertionFailedError(explanation + ": actual=" + actual
645473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                    + ", expected after: " + expectedAfter);
646473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        }
647473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    }
648473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
649473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    public void testGetmtime_NonExist_Failure() throws Exception {
650473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertTrue("Password should work for keystore",
651473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                mKeyStore.password(TEST_PASSWD));
652473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
653473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertTrue("Should be able to import key when unlocked",
654473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES));
655473c712b19bad992ab4eafcd43175fdce77b913dKenny Root
656473c712b19bad992ab4eafcd43175fdce77b913dKenny Root        assertEquals("-1 should be returned for non-existent key",
657473c712b19bad992ab4eafcd43175fdce77b913dKenny Root                -1L, mKeyStore.getmtime(TEST_KEYNAME2));
658473c712b19bad992ab4eafcd43175fdce77b913dKenny Root    }
659f35e9663d7bdae523953185b4ad6b6f9e8e7d6caChung-yih Wang}
660