blob: 82b276fe7666989d82c88c825bf7c1a5fdb2b5b6 [file] [log] [blame]
Brian Carlstromce4b51d2011-06-23 00:58:19 -07001#!/bin/bash
2#
3# Copyright 2011, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19prefix=$0
20log_file=$prefix.log
21baseline_file=$prefix.baseline
22
23function cleanup_output() {
24 rm -f $log_file
25 rm -f $baseline_file
26}
27
28function log() {
29 echo "$@"
30 append $log_file \# "$@"
31 append $baseline_file \# "$@"
32}
33
34function expect() {
35 append $baseline_file "$@"
36}
37
38function append() {
39 declare -r file=$1
40 shift
41 echo "$@" >> $file
42}
43
44function run() {
45 # strip out carriage returns from adb
46 # strip out date/time from ls -l
47 "$@" | tr --delete '\r' | sed -E 's/[0-9]{4}-[0-9]{2}-[0-9]{2} +[0-9]{1,2}:[0-9]{2} //' >> $log_file
48}
49
50function keystore() {
51 declare -r user=$1
52 shift
53 run adb shell su $user keystore_cli "$@"
54}
55
56function list_keystore_directory() {
57 run adb shell ls -al /data/misc/keystore
58}
59
60function compare() {
61 log "comparing $baseline_file and $log_file"
62 diff $baseline_file $log_file || (log $tag FAILED && exit 1)
63}
64
65function test_basic() {
66
67 #
68 # reset
69 #
70 log "reset keystore as system user"
71 keystore system r
72 expect "1 No error"
73 list_keystore_directory
74
75 #
76 # basic tests as system/root
77 #
78 log "root does not have permission to run test"
79 keystore root t
80 expect "6 Permission denied"
81
82 log "but system user does"
83 keystore system t
84 expect "3 Uninitialized"
85 list_keystore_directory
86
87 log "password is now bar"
88 keystore system p bar
89 expect "1 No error"
90 list_keystore_directory
91 expect "-rw------- keystore keystore 84 .masterkey"
92
93 log "no error implies initialized and unlocked"
94 keystore system t
95 expect "1 No error"
96
97 log "saw with no argument"
98 keystore system s
99 expect "5 Protocol error"
100
101 log "saw nothing"
102 keystore system s ""
103 expect "1 No error"
104
105 log "add key baz"
106 keystore system i baz quux
107 expect "1 No error"
108
109 log "1000 is uid of system"
110 list_keystore_directory
111 expect "-rw------- keystore keystore 84 .masterkey"
112 expect "-rw------- keystore keystore 52 1000_baz"
113
114 log "saw baz"
115 keystore system s ""
116 expect "1 No error"
117 expect "baz"
118
119 log "system does not have access to read any keys"
120 keystore system g baz
121 expect "6 Permission denied"
122
123 log "however, root can read system user keys (as can wifi or vpn users)"
124 keystore root g baz
125 expect "1 No error"
126 expect "quux"
127
128 #
129 # app user tests
130 #
131
132 # app_0 has uid 10000, as seen below
133 log "other uses cannot see the system keys"
134 keystore app_0 g baz
135 expect "7 Key not found"
136
137 log "app user cannot use reset, password, lock, unlock"
138 keystore app_0 r
139 expect "6 Permission denied"
140 keystore app_0 p
141 expect "6 Permission denied"
142 keystore app_0 l
143 expect "6 Permission denied"
144 keystore app_0 u
145 expect "6 Permission denied"
146
147 log "install app_0 key"
148 keystore app_0 i 0x deadbeef
149 expect 1 No error
150 list_keystore_directory
151 expect "-rw------- keystore keystore 84 .masterkey"
152 expect "-rw------- keystore keystore 52 10000_0x"
153 expect "-rw------- keystore keystore 52 1000_baz"
154
155 log "get with no argument"
156 keystore app_0 g
157 expect "5 Protocol error"
158
159 keystore app_0 g 0x
160 expect "1 No error"
161 expect "deadbeef"
162
163 keystore app_0 i fred barney
164 expect "1 No error"
165
166 keystore app_0 s ""
167 expect "1 No error"
168 expect "0x"
169 expect "fred"
170
171 log "note that saw returns the suffix of prefix matches"
172 keystore app_0 s fr # fred
173 expect "1 No error"
174 expect "ed" # fred
175
176 #
177 # lock tests
178 #
179 log "lock the store as system"
180 keystore system l
181 expect "1 No error"
182 keystore system t
183 expect "2 Locked"
184
185 log "saw works while locked"
186 keystore app_0 s ""
187 expect "1 No error"
188 expect "0x"
189 expect "fred"
190
191 log "...but cannot read keys..."
192 keystore app_0 g 0x
193 expect "2 Locked"
194
195 log "...but they can be deleted."
196 keystore app_0 e 0x
197 expect "1 No error"
198 keystore app_0 d 0x
199 expect "1 No error"
200 keystore app_0 e 0x
201 expect "7 Key not found"
202
203 #
204 # password
205 #
206 log "wrong password"
207 keystore system u foo
208 expect "13 Wrong password (4 tries left)"
209 log "right password"
210 keystore system u bar
211 expect "1 No error"
212
213 log "make the password foo"
214 keystore system p foo
215 expect "1 No error"
216
217 #
218 # final reset
219 #
220 log "reset wipes everything for all users"
221 keystore system r
222 expect "1 No error"
223 list_keystore_directory
224
225 keystore system t
226 expect "3 Uninitialized"
227
228}
229
230function test_4599735() {
231 # http://b/4599735
232 log "start regression test for b/4599735"
233 keystore system r
234 expect "1 No error"
235
236 keystore system p foo
237 expect "1 No error"
238
239 keystore system i baz quux
240 expect "1 No error"
241
242 keystore root g baz
243 expect "1 No error"
244 expect "quux"
245
246 keystore system l
247 expect "1 No error"
248
249 keystore system p foo
250 expect "1 No error"
251
252 log "after unlock, regression led to result of '8 Value corrupted'"
253 keystore root g baz
254 expect "1 No error"
255 expect "quux"
256
257 keystore system r
258 expect "1 No error"
259 log "end regression test for b/4599735"
260}
261
262function main() {
263 cleanup_output
264 log $tag START
265 test_basic
266 test_4599735
267 compare
268 log $tag PASSED
269 cleanup_output
270}
271
272main