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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
#!/usr/bin/guile \
--no-auto-compile -l pinentry-rofi.scm -s
!#
;; Copyright © 2020 Fredrik "PlaTFooT" Salomonsson
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(use-modules (srfi srfi-64)
(ice-9 popen)
(ice-9 textual-ports)
(pinentry-rofi))
(test-begin "pinentry-rofi")
(test-assert (string? pinentry-rofi-guile-version))
(test-begin "pinentry")
(let ((pinentry (make-pinentry #t "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry? pinentry))
(test-assert (pinentry-ok pinentry))
(test-equal "Prompt" (pinentry-prompt pinentry))
(test-equal "Ok" (pinentry-ok-button pinentry))
(test-equal "Cancel" (pinentry-cancel-button pinentry))
(test-equal ":1" (pinentry-display pinentry))
(test-equal "test.log" (pinentry-logfile pinentry))
(test-assert (not (pinentry-notok-button pinentry)))
(test-assert (not (pinentry-visibility pinentry)))
(test-assert (not (pinentry-error pinentry))))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (not (pinentry-ok pinentry))))
(test-end "pinentry")
(test-begin "utils")
(test-equal "Ok" (remove-underline "_Ok"))
(test-equal " Ok" (remove-underline " _Ok"))
(test-equal "foo_bar" (remove-underline "foo_bar"))
(test-equal "foo__bar" (remove-underline "foo__bar"))
(test-equal " __Ok" (remove-underline " __Ok"))
(test-equal "__Ok" (remove-underline "__Ok"))
(test-equal "Ok%0ACancel" (remove-underline "_Ok%0A_Cancel"))
(test-equal "Ok Cancel" (remove-underline "_Ok _Cancel"))
(test-equal "_Ok" (escape-underscore "_Ok"))
(test-equal " _Ok" (escape-underscore " _Ok"))
(test-equal "foo_bar" (escape-underscore "foo_bar"))
(test-equal "foo__bar" (escape-underscore "foo__bar"))
(test-equal " _Ok" (escape-underscore " __Ok"))
(test-equal "_Ok" (escape-underscore "__Ok"))
(test-equal "_Ok%0A_Cancel" (escape-underscore "__Ok%0A__Cancel"))
(test-equal "_Ok _Cancel" (escape-underscore "__Ok __Cancel"))
(test-assert (string-empty? ""))
(test-assert (not (string-empty? "foo")))
(test-end "utils")
(test-equal "This is one line\nThis is another%OA"
(hex->char "%54his is one line%0AThis is another%OA"))
(test-begin "html")
(test-equal "%54his is one line This is another%OA"
(html-newline "%54his is one line%0AThis is another%OA"))
(test-equal "%54his is one line\nThis is another%OA"
(html-newline "%54his is one line\nThis is another%OA"))
(test-equal "<u>O</u>k" (html-underline "_Ok"))
(test-equal " <u>O</u>k" (html-underline " _Ok"))
(test-equal "foo_bar" (html-underline "foo_bar"))
(test-equal "foo__bar" (html-underline "foo__bar"))
(test-equal " __Ok" (html-underline " __Ok"))
(test-equal "__Ok" (html-underline "__Ok"))
(test-equal "<u>O</u>k%0A<u>C</u>ancel" (html-underline "_Ok%0A_Cancel"))
(test-equal "<u>O</u>k <u>C</u>ancel" (html-underline "_Ok _Cancel"))
(test-end "html")
(test-equal "<u>T</u>his is one line <u>T</u>his is another%OA"
(pango-markup "_%54his is one line%0A_This is another%OA"))
(test-equal "Ok\nCancel"
(input-string "_Ok\nCancel"))
(test-equal "Ok\n_Cancel"
(input-string "_Ok\n__Cancel"))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(pinentry-set set-pinentry-notok-button! pinentry "Not ok")
(test-equal "Not ok" (pinentry-notok-button pinentry))
(test-assert (pinentry-ok pinentry)))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-error #t (pinentry-set pinentry-notok-button pinentry "Not ok")))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-option pinentry "OPTION default-ok=Okay"))
(test-equal "Okay" (pinentry-ok-button pinentry))
(test-equal "Cancel" (pinentry-cancel-button pinentry))
(test-equal "Prompt" (pinentry-prompt pinentry))
(test-assert (pinentry-ok pinentry)))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-option pinentry "OPTION default-cancel=No"))
(test-equal "Ok" (pinentry-ok-button pinentry))
(test-equal "No" (pinentry-cancel-button pinentry))
(test-equal "Prompt" (pinentry-prompt pinentry))
(test-assert (pinentry-ok pinentry)))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-option pinentry "OPTION default-prompt=Password:"))
(test-equal "Ok" (pinentry-ok-button pinentry))
(test-equal "Cancel" (pinentry-cancel-button pinentry))
(test-equal "Password:" (pinentry-prompt pinentry))
(test-assert (pinentry-ok pinentry)))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-option pinentry "OPTION foo bar")))
(let ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (not (pinentry-option pinentry " OPTION foo bar")))
(test-assert (not (pinentry-option pinentry "OPTION")))
(test-assert (not (pinentry-option pinentry "Foo"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log"))
(output "")
(fake-port (make-soft-port
(vector
(lambda (c) (set! output (string-append output c)))
(lambda (s) (set! output (string-append output s)))
(lambda () #t)
#f
(lambda () #t))
"w")))
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-getinfo pinentry "GETINFO pid"))
(test-equal (format #f "D ~a\n" (getpid)) output)))
(test-assert (pinentry-getinfo pinentry "GETINFO foo bar"))
(test-assert (not (pinentry-getinfo pinentry " GETINFO foo bar")))
(test-assert (not (pinentry-getinfo pinentry "GETINFO")))
(test-assert (not (pinentry-getinfo pinentry "Foo"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-setkeyinfo pinentry "SETKEYINFO Foo"))
(test-assert (pinentry-ok pinentry))
(test-assert (not (pinentry-setkeyinfo pinentry " SETKEYINFO foo bar")))
(test-assert (not (pinentry-setkeyinfo pinentry "SETKEYINFO")))
(test-assert (not (pinentry-setkeyinfo pinentry "Foo"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-setok pinentry "SETOK Foo"))
(test-equal "Foo" (pinentry-ok-button pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-setok pinentry "SETOK _Ok okay"))
(test-equal "Ok okay" (pinentry-ok-button pinentry))
(test-assert (not (pinentry-setok pinentry " SETOK foo bar")))
(test-equal "Ok okay" (pinentry-ok-button pinentry))
(test-assert (not (pinentry-setok pinentry "SETOK")))
(test-equal "Ok okay" (pinentry-ok-button pinentry))
(test-assert (not (pinentry-setok pinentry "Foo")))
(test-equal "Ok okay" (pinentry-ok-button pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-setnotok pinentry "SETNOTOK Foo"))
(test-equal "Foo" (pinentry-notok-button pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-setnotok pinentry "SETNOTOK Not _Ok"))
(test-equal "Not Ok" (pinentry-notok-button pinentry))
(test-assert (not (pinentry-setnotok pinentry " SETNOTOK foo bar")))
(test-equal "Not Ok" (pinentry-notok-button pinentry))
(test-assert (not (pinentry-setnotok pinentry "SETNOTOK")))
(test-equal "Not Ok" (pinentry-notok-button pinentry))
(test-assert (not (pinentry-setnotok pinentry "Foo")))
(test-equal "Not Ok" (pinentry-notok-button pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log")))
(test-assert (pinentry-setcancel pinentry "SETCANCEL Foo"))
(test-equal "Foo" (pinentry-cancel-button pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-setcancel pinentry "SETCANCEL _Abort"))
(test-equal "Abort" (pinentry-cancel-button pinentry))
(test-assert (not (pinentry-setcancel pinentry " SETCANCEL foo bar")))
(test-equal "Abort" (pinentry-cancel-button pinentry))
(test-assert (not (pinentry-setcancel pinentry "SETCANCEL")))
(test-equal "Abort" (pinentry-cancel-button pinentry))
(test-assert (not (pinentry-setcancel pinentry "Foo")))
(test-equal "Abort" (pinentry-cancel-button pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Desc" ":1" "test.log")))
(test-assert (pinentry-setdesc pinentry "SETDESC Foo"))
(test-equal "Foo" (pinentry-desc pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-setdesc pinentry "SETDESC _%54his is a description%0A_On two lines"))
(test-equal "<u>T</u>his is a description <u>O</u>n two lines" (pinentry-desc pinentry))
(test-assert (not (pinentry-setdesc pinentry " SETDESC foo bar")))
(test-equal "<u>T</u>his is a description <u>O</u>n two lines" (pinentry-desc pinentry))
(test-assert (not (pinentry-setdesc pinentry "SETDESC")))
(test-equal "<u>T</u>his is a description <u>O</u>n two lines" (pinentry-desc pinentry))
(test-assert (not (pinentry-setdesc pinentry "Foo")))
(test-equal "<u>T</u>his is a description <u>O</u>n two lines" (pinentry-desc pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Error" ":1" "test.log")))
(test-assert (pinentry-seterror pinentry "SETERROR Foo"))
(test-equal "Foo" (pinentry-error pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-seterror pinentry "SETERROR _%54his is an error%0A_On two lines"))
(test-equal "<u>T</u>his is an error <u>O</u>n two lines" (pinentry-error pinentry))
(test-assert (not (pinentry-seterror pinentry " SETERROR foo bar")))
(test-equal "<u>T</u>his is an error <u>O</u>n two lines" (pinentry-error pinentry))
(test-assert (not (pinentry-seterror pinentry "SETERROR")))
(test-equal "<u>T</u>his is an error <u>O</u>n two lines" (pinentry-error pinentry))
(test-assert (not (pinentry-seterror pinentry "Foo")))
(test-equal "<u>T</u>his is an error <u>O</u>n two lines" (pinentry-error pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Prompt" ":1" "test.log")))
(test-assert (pinentry-setprompt pinentry "SETPROMPT Foo"))
(test-equal "Foo" (pinentry-prompt pinentry))
(test-assert (pinentry-ok pinentry))
(test-assert (pinentry-setprompt pinentry "SETPROMPT _%54his is a prompt%0A_On two lines"))
(test-equal "<u>T</u>his is a prompt <u>O</u>n two lines" (pinentry-prompt pinentry))
(test-assert (not (pinentry-setprompt pinentry " SETPROMPT foo bar")))
(test-equal "<u>T</u>his is a prompt <u>O</u>n two lines" (pinentry-prompt pinentry))
(test-assert (not (pinentry-setprompt pinentry "SETPROMPT")))
(test-equal "<u>T</u>his is a prompt <u>O</u>n two lines" (pinentry-prompt pinentry))
(test-assert (not (pinentry-setprompt pinentry "Foo")))
(test-equal "<u>T</u>his is a prompt <u>O</u>n two lines" (pinentry-prompt pinentry)))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Prompt" ":1" "test.log")))
(test-error "(quit #t)" (pinentry-bye pinentry "BYE"))
(test-assert (not (pinentry-bye pinentry "Hej då"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log"))
(output "")
(fake-port (make-soft-port
(vector
(lambda (c) (set! output (string-append output c)))
(lambda (s) (set! output (string-append output s)))
(lambda () #t)
#f
(lambda () #t))
"w"))
(description "This is a description")
(error "Something went wrong")
(display ":1"))
(set-pinentry-desc! pinentry description)
(set-pinentry-display! pinentry display)
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-getpin pinentry "GETPIN"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal "Prompt" prompt)
(test-equal description message)
(test-assert (not visibility))
(test-assert (not only-match))
(test-assert (not buttons))
(test-equal `(("DISPLAY" . ,display)) env)
"password")))
(test-equal (format #f "D password") output)))
(set-pinentry-error! pinentry error)
(set! output "")
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-getpin pinentry "GETPIN"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal "Prompt" prompt)
(test-equal (format #f "~a ~a" error description)
message)
(test-assert (not visibility))
(test-assert (not only-match))
(test-assert (not buttons))
(test-equal `(("DISPLAY" . ,display)) env)
"password")))
(test-equal (format #f "D password") output)))
(set! output "")
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-getpin pinentry "GETPIN"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal "Prompt" prompt)
(test-equal (format #f "~a ~a" error description)
message)
(test-assert (not visibility))
(test-assert (not only-match))
(test-assert (not buttons))
(test-equal `(("DISPLAY" . ,display)) env)
"")))
(test-equal (format #f "ERR 83886179 Operation cancelled <rofi>\n") output)))
(set! output "")
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-getpin pinentry "GETPIN"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal "Prompt" prompt)
(test-equal (format #f "~a ~a" error description)
message)
(test-assert (not visibility))
(test-assert (not only-match))
(test-assert (not buttons))
(test-equal `(("DISPLAY" . ,display)) env)
" ")))
(test-equal (format #f "ERR 83886179 Operation cancelled <rofi>\n") output)))
(test-assert (not (pinentry-getinfo pinentry " GETPIN")))
(test-assert (not (pinentry-getinfo pinentry "Foo"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log"))
(description "This is a description")
(error "Something went wrong")
(display ":1"))
(set-pinentry-desc! pinentry description)
(set-pinentry-display! pinentry display)
(test-assert (pinentry-confirm pinentry "CONFIRM"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal ">" prompt)
(test-equal description message)
(test-assert visibility)
(test-assert only-match)
(test-equal `("Ok" "Cancel") buttons)
(test-equal `(("DISPLAY" . ,display)) env)
"Ok")))
(test-assert (pinentry-ok pinentry))
(set-pinentry-error! pinentry error)
(let* ((output "")
(fake-port (make-soft-port
(vector
(lambda (c) (set! output (string-append output c)))
(lambda (s) (set! output (string-append output s)))
(lambda () #t)
#f
(lambda () #t))
"w")))
(with-output-to-port
fake-port
(lambda ()
(test-assert (pinentry-confirm
pinentry
"CONFIRM"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal ">" prompt)
(test-equal (format #f "~a ~a" error description)
message)
(test-assert visibility)
(test-assert only-match)
(test-equal `("Ok" "Cancel") buttons)
(test-equal `(("DISPLAY" . ,display)) env)
"Cancel")))
(test-equal (format #f "ERR 277 Operation cancelled\n") output))))
(test-assert (not (pinentry-ok pinentry)))
(test-assert (not (pinentry-getinfo pinentry " CONFIRM")))
(test-assert (not (pinentry-getinfo pinentry "Foo"))))
(let* ((pinentry (make-pinentry #f "Prompt" "Ok" "Cancel" ":1" "test.log"))
(description "This is a description")
(display ":1"))
(set-pinentry-desc! pinentry description)
(set-pinentry-display! pinentry display)
(test-assert (pinentry-confirm pinentry "CONFIRM --one-button"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal ">" prompt)
(test-equal description message)
(test-assert visibility)
(test-assert only-match)
(test-equal `("Ok") buttons)
(test-equal `(("DISPLAY" . ,display)) env)
"Ok")))
(test-assert (pinentry-ok pinentry))
(set-pinentry-ok! pinentry #f)
(test-assert (pinentry-confirm pinentry "MESSAGE"
(lambda* (#:key (env '())
visibility
(prompt ">")
message
buttons
only-match)
(test-equal ">" prompt)
(test-equal description message)
(test-assert visibility)
(test-assert only-match)
(test-equal `("Ok") buttons)
(test-equal `(("DISPLAY" . ,display)) env)
"Ok")))
(test-assert (pinentry-ok pinentry))
(test-assert (not (pinentry-getinfo pinentry " CONFIRM --one-button")))
(test-assert (not (pinentry-getinfo pinentry "MESSAGE --one-button")))
(test-assert (not (pinentry-getinfo pinentry " MESSAGE")))
(test-assert (not (pinentry-getinfo pinentry "Foo"))))
(test-end "pinentry-rofi")
|