1#!/bin/bash
2
3. $(dirname $0)/include.sh
4
5function test_login
6{
7	login || return 1
8	assertz $?
9}
10
11function 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
19function 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
28Name: $name
29URL: $url
30Username: $username
31Password: $password
32__EOM__
33	assertz $? || return 1
34
35	assert_str_eq "$(lpass show --sync=no --password $name)" "$password"
36}
37
38function 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
48Name: $name
49URL: $url
50Username: $username
51Password: $password
52Notes:
53$notes
54__EOM__
55	assertz $? || return 1
56
57	assert_str_eq "$(lpass show --sync=no --note $name)" "$notes"
58}
59
60function 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
65Name: $name
66Number: 000-00-0000
67NoteType: 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
74function 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
79Name: $name
80Number: 000-00-0000
81Field: This is my SSN
82NoteType: 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
89function 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
95Name: $name
96Name: $person_name
97Number: 000-00-0000
98NoteType: Social Security
99__EOM__
100	assertz $? || return 1
101
102	assert_str_eq "$(lpass show --sync=no --field=Name $name)" "$person_name"
103}
104
105function 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-----
111Proc-Type: 4,ENCRYPTED
112DEK-Info: AES-128-CBC,5D91CAD2A62C0E3EDCCB853FCB21054D
113
114V/HhFCirRljWoJnjOwwhoFqdRnpWbXQsrppky/uT/Ttb9k5YmC9SLhEZyf8fAReJ
115KGiE8MvnnXKvDMj5eqeWge/YleHsNvyR+8qPqfPha9X/vYCUeR/ZoGg/CKzMVBN3
116bnghFVqB3npQykkkbiBEKLDwosTkR/0JO4I8PRzo34k=
117-----END EC PRIVATE KEY-----
118__EOM__
119    read -r -d '' pubkey <<__EOM__
120ecdsa-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
123Name: $name
124Hostname: foobar
125Private Key: $privkey
126Public 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
132function 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-----
138Proc-Type: 4,ENCRYPTED
139DEK-Info: AES-128-CBC,5D91CAD2A62C0E3EDCCB853FCB21054D
140
141V/HhFCirRljWoJnjOwwhoFqdRnpWbXQsrppky/uT/Ttb9k5YmC9SLhEZyf8fAReJ
142KGiE8MvnnXKvDMj5eqeWge/YleHsNvyR+8qPqfPha9X/vYCUeR/ZoGg/CKzMVBN3
143bnghFVqB3npQykkkbiBEKLDwosTkR/0JO4I8PRzo34k=
144-----END EC PRIVATE KEY-----
145__EOM__
146    read -r -d '' pubkey <<__EOM__
147ecdsa-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
150Name: $name
151Hostname: 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
159function 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
168function 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
177function 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
189function 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
201function 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
209function 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
217function test_show
218{
219	login || return 1
220	read -r -d '' expected <<__EOM__
221test-group/test-account [id: 0001]
222Username: xyz@example.com
223Password: test-account-password
224URL: https://test-url.example.com/
225__EOM__
226	local out=$(lpass show --sync=no test-account)
227	assert_str_eq "$expected" "$out"
228}
229
230function 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": "xyz@example.com",
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
253function 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__
259test-group/test-note [id: 0002]
260Username:  test-note-user
261Password:  test-note-password
262Hostname:  foo.example.com
263NoteType:  Server
264__EOM__
265	local out=$(lpass show --sync=no test-note)
266	assert_str_eq "$expected" "$out"
267}
268
269function test_show_reprompt
270{
271	login || return 1
272	read -r -d '' expected <<__EOM__
273test-group/test-reprompt-account [id: 0003]
274Username: xyz@example.com
275Password: test-account-password
276URL: https://test-url.example.com/
277Reprompt: Yes
278__EOM__
279	local out=$(lpass show --sync=no test-reprompt-account)
280	assert_str_eq "$expected" "$out"
281}
282
283function test_ls
284{
285	login || return 1
286	read -r -d '' expected <<__EOM__
287test-group/test-account [id: 0001]
288test-group/test-note [id: 0002]
289test-group/test-reprompt-account [id: 0003]
290test-group/test-reprompt-note [id: 0004]
291__EOM__
292	local out=$(lpass ls --sync=no)
293	assert_str_eq "$expected" "$out"
294}
295
296function test_export
297{
298	login || return 1
299	read -r -d '' expected <<__EOM__
300url,username,password,extra,name,grouping,fav
301http://sn,,,"NoteType: Server
302Hostname: foo.example.com
303Username: test-note-user
304Password: test-note-password",test-reprompt-note,test-group,0
305https://test-url.example.com/,xyz@example.com,test-account-password,,test-reprompt-account,test-group,0
306http://sn,,,"NoteType: Server
307Hostname: foo.example.com
308Username: test-note-user
309Password: test-note-password",test-note,test-group,0
310https://test-url.example.com/,xyz@example.com,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
316function test_export_extended
317{
318	login || return 1
319	read -r -d '' expected <<__EOM__
320grouping,url,username,password,extra,name,fav,id,group,fullname,last_touch,last_modified_gmt,unknown,attachpresent
321test-group,http://sn,,,"NoteType: Server
322Hostname: foo.example.com
323Username: test-note-user
324Password: test-note-password",test-reprompt-note,0,0004,test-group,test-group/test-reprompt-note,skipped,skipped,,0
325test-group,https://test-url.example.com/,xyz@example.com,test-account-password,,test-reprompt-account,0,0003,test-group,test-group/test-reprompt-account,skipped,skipped,,0
326test-group,http://sn,,,"NoteType: Server
327Hostname: foo.example.com
328Username: test-note-user
329Password: test-note-password",test-note,0,0002,test-group,test-group/test-note,skipped,skipped,,0
330test-group,https://test-url.example.com/,xyz@example.com,test-account-password,,test-account,0,0001,test-group,test-group/test-account,skipped,skipped,,0
331__EOM__
332local 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
336runtests "$@"
337