1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.security.tests;
import android.app.Activity;
import android.security.KeyStore;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
/**
* Junit / Instrumentation test case for KeyStore class
*
* Running the test suite:
*
* adb shell am instrument -w android.security.tests/.KeyStoreTestRunner
*/
@MediumTest
public class KeyStoreTest extends ActivityUnitTestCase<Activity> {
private static final String TEST_PASSWD = "12345678";
private static final String TEST_EMPTY_PASSWD = "";
private static final String TEST_SHORT_PASSWD = "short";
private static final String TEST_PASSWD2 = "87654321";
private static final String TEST_KEYNAME = "testkey";
private static final String TEST_KEYNAME1 = "testkey1";
private static final String TEST_KEYNAME2 = "testkey2";
private static final String TEST_KEYVALUE = "test value";
// "Hello, World" in Chinese
private static final String TEST_I18N = "\u4F60\u597D, \u4E16\u754C";
private KeyStore mKeyStore = null;
public KeyStoreTest() {
super(Activity.class);
}
@Override
protected void setUp() throws Exception {
mKeyStore = KeyStore.getInstance();
if (mKeyStore.test() != KeyStore.UNINITIALIZED) mKeyStore.reset();
assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test());
super.setUp();
}
@Override
protected void tearDown() throws Exception {
mKeyStore.reset();
super.tearDown();
}
public void testTest() throws Exception {
assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test());
}
public void testPassword() throws Exception {
//assertFalse(mKeyStore.password(TEST_EMPTY_PASSWD));
//assertFalse(mKeyStore.password(TEST_SHORT_PASSWD));
assertTrue(mKeyStore.password(TEST_PASSWD));
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
assertFalse(mKeyStore.password(TEST_PASSWD2, TEST_PASSWD2));
//assertFalse(mKeyStore.password(TEST_PASSWD, TEST_SHORT_PASSWD));
assertTrue(mKeyStore.password(TEST_PASSWD, TEST_PASSWD2));
}
public void testPut() throws Exception {
assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
}
public void testI18n() throws Exception {
assertFalse(mKeyStore.put(TEST_I18N, TEST_I18N));
assertFalse(mKeyStore.contains(TEST_I18N));
mKeyStore.password(TEST_I18N);
assertTrue(mKeyStore.put(TEST_I18N, TEST_I18N));
assertTrue(mKeyStore.contains(TEST_I18N));
}
public void testDelete() throws Exception {
assertTrue(mKeyStore.delete(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertTrue(mKeyStore.delete(TEST_KEYNAME));
mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE);
assertTrue(mKeyStore.delete(TEST_KEYNAME));
}
public void testContains() throws Exception {
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE);
assertTrue(mKeyStore.contains(TEST_KEYNAME));
}
public void testSaw() throws Exception {
String[] results = mKeyStore.saw(TEST_KEYNAME);
assertEquals(0, results.length);
mKeyStore.password(TEST_PASSWD);
mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE);
mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE);
results = mKeyStore.saw(TEST_KEYNAME);
assertEquals(2, results.length);
}
public void testLock() throws Exception {
assertFalse(mKeyStore.lock());
mKeyStore.password(TEST_PASSWD);
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
assertTrue(mKeyStore.lock());
assertEquals(KeyStore.LOCKED, mKeyStore.test());
}
public void testUnlock() throws Exception {
mKeyStore.password(TEST_PASSWD);
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
mKeyStore.lock();
assertFalse(mKeyStore.unlock(TEST_PASSWD2));
assertTrue(mKeyStore.unlock(TEST_PASSWD));
}
}
|