Debian Codegraph @

test/tests

raw
1 #!/bin/bash
2
3 . $(dirname $0)/include.sh
4
5 function test_login
6 {
7 login || return 1
8 assertz $?
9 }
10
11 function test_login_wrong_pw_should_fail
12 {
13 LPASS_ASKPASS=./askpass-wrong.sh login
14 local ret=$?
15 rm .askpass.lock 2>/dev/null
16 assert $ret
17 }
18
19 function test_add_account
20 {
21 login || return 1
22 local name="test-add-account"
23 local url="https://example.com"
24 local username="user27"
25 local password="999999999"
26
27 cat<<__EOM__ | lpass add --sync=no --non-interactive $name
28 Name: $name
29 URL: $url
30 Username: $username
31 Password: $password
32 __EOM__
33 assertz $? || return 1
34
35 assert_str_eq "$(lpass show --sync=no --password $name)" "$password"
36 }
37
38 function test_add_site_note
39 {
40 login || return 1
41 local name="test-add-site-note"
42 local url="https://example.com"
43 local username="user27"
44 local password="999999999"
45 local notes="here are some notes!!"
46
47 cat<<__EOM__ | lpass add --sync=no --non-interactive $name
48 Name: $name
49 URL: $url
50 Username: $username
51 Password: $password
52 Notes:
53 $notes
54 __EOM__
55 assertz $? || return 1
56
57 assert_str_eq "$(lpass show --sync=no --note $name)" "$notes"
58 }
59
60 function test_add_note
61 {
62 login || return 1
63 local name="test-add-note"
64 cat<<__EOM__ | lpass add --sync=no --note-type=ssn --non-interactive $name
65 Name: $name
66 Number: 000-00-0000
67 NoteType: Social Security
68 __EOM__
69 assertz $? || return 1
70
71 assert_str_eq "$(lpass show --sync=no --field=Number $name)" "000-00-0000"
72 }
73
74 function test_add_note_with_field
75 {
76 login || return 1
77 local name="test-add-note-with-notes"
78 cat<<__EOM__ | lpass add --sync=no --note-type=ssn --non-interactive $name
79 Name: $name
80 Number: 000-00-0000
81 Field: This is my SSN
82 NoteType: Social Security
83 __EOM__
84 assertz $? || return 1
85
86 assert_str_eq "$(lpass show --sync=no --field=Number $name)" "000-00-0000"
87 }
88
89 function test_add_ssn_name
90 {
91 login || return 1
92 local name="test-add-note"
93 local person_name="John Doe"
94 cat<<__EOM__ | lpass add --sync=no --note-type=ssn --non-interactive $name
95 Name: $name
96 Name: $person_name
97 Number: 000-00-0000
98 NoteType: Social Security
99 __EOM__
100 assertz $? || return 1
101
102 assert_str_eq "$(lpass show --sync=no --field=Name $name)" "$person_name"
103 }
104
105 function test_add_ssh_key
106 {
107 login || return 1
108 local name="test-add-ssh-key"
109 read -r -d '' privkey <<__EOM__
110 -----BEGIN EC PRIVATE KEY-----
111 Proc-Type: 4,ENCRYPTED
112 DEK-Info: AES-128-CBC,5D91CAD2A62C0E3EDCCB853FCB21054D
113
114 V/HhFCirRljWoJnjOwwhoFqdRnpWbXQsrppky/uT/Ttb9k5YmC9SLhEZyf8fAReJ
115 KGiE8MvnnXKvDMj5eqeWge/YleHsNvyR+8qPqfPha9X/vYCUeR/ZoGg/CKzMVBN3
116 bnghFVqB3npQykkkbiBEKLDwosTkR/0JO4I8PRzo34k=
117 -----END EC PRIVATE KEY-----
118 __EOM__
119 read -r -d '' pubkey <<__EOM__
120 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHiavhPDPg2OH2YjJWDpF9JHpKfnVGB0xc7cSojMXMJftgH4UvgUr8fVJhfp1ix/I8a/a8C0RiCo/Q/A6z3o+7U= blah
121 __EOM__
122 cat <<__EOM__ | lpass add --sync=no --note-type=ssh-key --non-interactive $name
123 Name: $name
124 Hostname: foobar
125 Private Key: $privkey
126 Public Key: $pubkey
127 __EOM__
128 assert_str_eq "$(lpass show --sync=no --field='Private Key' $name)" "$privkey" || return 1
129 assert_str_eq "$(lpass show --sync=no --field='Public Key' $name)" "$pubkey"
130 }
131
132 function test_edit_ssh_key
133 {
134 login || return 1
135 local name="test-edit-ssh-key"
136 read -r -d '' privkey <<__EOM__
137 -----BEGIN EC PRIVATE KEY-----
138 Proc-Type: 4,ENCRYPTED
139 DEK-Info: AES-128-CBC,5D91CAD2A62C0E3EDCCB853FCB21054D
140
141 V/HhFCirRljWoJnjOwwhoFqdRnpWbXQsrppky/uT/Ttb9k5YmC9SLhEZyf8fAReJ
142 KGiE8MvnnXKvDMj5eqeWge/YleHsNvyR+8qPqfPha9X/vYCUeR/ZoGg/CKzMVBN3
143 bnghFVqB3npQykkkbiBEKLDwosTkR/0JO4I8PRzo34k=
144 -----END EC PRIVATE KEY-----
145 __EOM__
146 read -r -d '' pubkey <<__EOM__
147 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHiavhPDPg2OH2YjJWDpF9JHpKfnVGB0xc7cSojMXMJftgH4UvgUr8fVJhfp1ix/I8a/a8C0RiCo/Q/A6z3o+7U= blah
148 __EOM__
149 cat <<__EOM__ | lpass add --sync=no --note-type=ssh-key --non-interactive $name
150 Name: $name
151 Hostname: foobar
152 __EOM__
153 echo "$privkey" | lpass edit --sync=no --field='Private Key' --non-interactive $name
154 echo "$pubkey" | lpass edit --sync=no --field='Public Key' --non-interactive $name
155 assert_str_eq "$(lpass show --sync=no --field='Private Key' $name)" "$privkey" || return 1
156 assert_str_eq "$(lpass show --sync=no --field='Public Key' $name)" "$pubkey"
157 }
158
159 function test_edit_username
160 {
161 login || return 1
162 local username="new-test-user"
163 echo $username | lpass edit --username --non-interactive test-account
164 assertz $? || return 1
165 assert_str_eq "$(lpass show --sync=no --username test-account)" $username
166 }
167
168 function test_edit_field
169 {
170 login || return 1
171 local hostname="quux.bar.com"
172 echo $hostname | lpass edit --field=Hostname --non-interactive test-note
173 assertz $? || return 1
174 assert_str_eq "$(lpass show --sync=no --field=Hostname test-note)" $hostname
175 }
176
177 function test_edit_reprompt
178 {
179 login || return 1
180 echo "Reprompt: No" | lpass edit --non-interactive test-reprompt-account
181 assertz $? || return 1
182 assert_str_eq "$(lpass show --sync=no test-reprompt-account | grep Reprompt)" ""
183
184 echo "Reprompt: Yes" | lpass edit --non-interactive test-reprompt-account
185 assertz $? || return 1
186 assert_str_eq "$(lpass show --sync=no test-reprompt-account | grep Reprompt)" "Reprompt: Yes"
187 }
188
189 function test_edit_reprompt_note
190 {
191 login || return 1
192 echo "Reprompt: No" | lpass edit --non-interactive test-reprompt-note
193 assertz $? || return 1
194 assert_str_eq "$(lpass show --sync=no test-reprompt-note | grep Reprompt)" "" || return 1
195
196 echo "Reprompt: Yes" | lpass edit --non-interactive test-reprompt-note
197 assertz $? || return 1
198 assert_str_eq "$(lpass show --sync=no test-reprompt-note | grep Reprompt)" "Reprompt: Yes"
199 }
200
201 function test_duplicate
202 {
203 login || return 1
204 lpass duplicate --sync=no test-account || return 1
205 local numaccts=$(lpass ls --sync=no | grep test-account | wc -l)
206 assert_eq $numaccts 2
207 }
208
209 function test_generate
210 {
211 login || return 1
212 lpass generate --sync=no test-generate 30 >/dev/null || return 1
213 local newpw=$(lpass show --sync=no --password test-generate)
214 assert_eq ${#newpw} 30
215 }
216
217 function test_show
218 {
219 login || return 1
220 read -r -d '' expected <<__EOM__
221 test-group/test-account [id: 0001]
222 Username: [email protected]
223 Password: test-account-password
224 URL: https://test-url.example.com/
225 __EOM__
226 local out=$(lpass show --sync=no test-account)
227 assert_str_eq "$expected" "$out"
228 }
229
230 function test_show_json
231 {
232 login || return 1
233 read -r -d '' expected <<__EOM__
234 [
235 {
236 "id": "0001",
237 "name": "test-account",
238 "fullname": "test-group/test-account",
239 "username": "[email protected]",
240 "password": "test-account-password",
241 "last_modified_gmt": "skipped",
242 "last_touch": "skipped",
243 "group": "test-group",
244 "url": "https://test-url.example.com/",
245 "note": ""
246 }
247 ]
248 __EOM__
249 local out=$(lpass show --sync=no --json test-account)
250 assert_str_eq "$expected" "$out"
251 }
252
253 function test_show_note
254 {
255 login || return 1
256 # note the extra space here because mock server includes one
257 # normally note fields are trimmed when saved
258 read -r -d '' expected <<__EOM__
259 test-group/test-note [id: 0002]
260 Username: test-note-user
261 Password: test-note-password
262 Hostname: foo.example.com
263 NoteType: Server
264 __EOM__
265 local out=$(lpass show --sync=no test-note)
266 assert_str_eq "$expected" "$out"
267 }
268
269 function test_show_reprompt
270 {
271 login || return 1
272 read -r -d '' expected <<__EOM__
273 test-group/test-reprompt-account [id: 0003]
274 Username: [email protected]
275 Password: test-account-password
276 URL: https://test-url.example.com/
277 Reprompt: Yes
278 __EOM__
279 local out=$(lpass show --sync=no test-reprompt-account)
280 assert_str_eq "$expected" "$out"
281 }
282
283 function test_ls
284 {
285 login || return 1
286 read -r -d '' expected <<__EOM__
287 test-group/test-account [id: 0001]
288 test-group/test-note [id: 0002]
289 test-group/test-reprompt-account [id: 0003]
290 test-group/test-reprompt-note [id: 0004]
291 __EOM__
292 local out=$(lpass ls --sync=no)
293 assert_str_eq "$expected" "$out"
294 }
295
296 function test_export
297 {
298 login || return 1
299 read -r -d '' expected <<__EOM__
300 url,username,password,extra,name,grouping,fav
301 http://sn,,,"NoteType: Server
302 Hostname: foo.example.com
303 Username: test-note-user
304 Password: test-note-password",test-reprompt-note,test-group,0
305 https://test-url.example.com/,[email protected],test-account-password,,test-reprompt-account,test-group,0
306 http://sn,,,"NoteType: Server
307 Hostname: foo.example.com
308 Username: test-note-user
309 Password: test-note-password",test-note,test-group,0
310 https://test-url.example.com/,[email protected],test-account-password,,test-account,test-group,0
311 __EOM__
312 local out=$(lpass export --sync=no | tr -d '\r')
313 assert_str_eq "$expected" "$out"
314 }
315
316 function test_export_extended
317 {
318 login || return 1
319 read -r -d '' expected <<__EOM__
320 grouping,url,username,password,extra,name,fav,id,group,fullname,last_touch,last_modified_gmt,unknown,attachpresent
321 test-group,http://sn,,,"NoteType: Server
322 Hostname: foo.example.com
323 Username: test-note-user
324 Password: test-note-password",test-reprompt-note,0,0004,test-group,test-group/test-reprompt-note,skipped,skipped,,0
325 test-group,https://test-url.example.com/,[email protected],test-account-password,,test-reprompt-account,0,0003,test-group,test-group/test-reprompt-account,skipped,skipped,,0
326 test-group,http://sn,,,"NoteType: Server
327 Hostname: foo.example.com
328 Username: test-note-user
329 Password: test-note-password",test-note,0,0002,test-group,test-group/test-note,skipped,skipped,,0
330 test-group,https://test-url.example.com/,[email protected],test-account-password,,test-account,0,0001,test-group,test-group/test-account,skipped,skipped,,0
331 __EOM__
332 local out=$(lpass export --sync=no --fields=grouping,url,username,password,extra,name,fav,id,group,fullname,last_touch,last_modified_gmt,unknown,attachpresent | tr -d '\r')
333 assert_str_eq "$expected" "$out"
334 }
335
336 runtests "$@"