Debian Codegraph @

tests/openpgp/README

raw
1 # Emacs, this is an -*- org -*- file.
2 #+STARTUP: showall
3
4 * How to run the test suite
5 ** tldr: How to run all tests fast.
6
7 obj $ make check-all TESTFLAGS=--parallel
8
9 You can use --parallel=N to request N parallel jobs. Hint: Tuck
10 TESTFLAGS=--parallel in your environment.
11
12 ** Running individual test suites or tests
13
14 From your build directory, run
15
16 obj $ make -C tests/openpgp check
17
18 to run all tests or
19
20 obj $ make -C tests/openpgp check TESTS=your-test.scm
21
22 to run a specific test (or any number of tests separated by spaces).
23
24 If you want to debug a test, add verbose=1 to see messages printed by
25 spawned programs to their standard error stream, verbose=2 to see what
26 programs are executed, or verbose=3 to see even more program output
27 and exit codes.
28
29 If you want to run gpg under valgrind add with_valgrind=1.
30
31
32 ** Inspecting the test environment
33
34 To inspect the environment in which tests are running, or to quickly
35 create keys for debugging or testing, you can start a shell. There is
36 one test that does just that:
37
38 obj $ make -C tests/openpgp check TESTS=shell.scm
39 PASS: tests/openpgp/setup.scm
40 Load legacy test environment? [Y/n] y
41 Drop 'batch' from gpg.conf? [Y/n] y
42
43 Enjoy your test environment. Type 'exit' to exit it, it will be cleaned up after you.
44
45 ... $ gpg -k Alfa
46 gpg: NOTE: THIS IS A DEVELOPMENT VERSION!
47 gpg: It is only intended for test purposes and should NOT be
48 gpg: used in a production environment or with production keys!
49 gpg: /tmp/gpgscm-20170809T144032-run-tests-PFfybw/trustdb.gpg: trustdb created
50 pub dsa1024 1999-03-08 [SCA]
51 A0FF4590BB6122EDEF6E3C542D727CC768697734
52 uid [ unknown] Alfa Test (demo key) <[email protected]>
53 uid [ unknown] Alpha Test (demo key) <[email protected]>
54 uid [ unknown] Alice (demo key)
55 sub elg1024 1999-03-08 [E]
56
57 PATH is adjusted so that you will use the tools from the build tree.
58 Note that the directory is removed when you exit the shell.
59
60 ** Passing options to the test driver
61
62 You can set TESTFLAGS to pass flags to 'run-tests.scm'. For example,
63 to speed up the test suite when bisecting, do
64
65 obj $ make -C tests/openpgp check TESTFLAGS=--parallel
66
67 See below for the arguments supported by the driver.
68
69 ** Calling the test driver directly
70 This is a bit tricky because one needs to manually set some
71 environment variables. We should make that easier. See discussion
72 below. From your build directory, do:
73
74 obj $ srcdir=<path to>/tests/openpgp \
75 GPGSCM_PATH=<path to>/tests/gpgscm:<path to>/tests/openpgp \
76 $(pwd)/tests/gpgscm/gpgscm [gpgscm args] \
77 run-tests.scm [test suite runner args]
78
79 *** Arguments supported by the test suite runner
80 The test suite runner supports two modes of operation, '--sequential'
81 and '--parallel'. By default the tests are run in sequential order,
82 each one in a clean environment.
83
84 You can specify the tests to run as positional arguments relative to
85 srcdir (e.g. just 'version.scm'). Note that you do not have to
86 specify setup.scm and finish.scm, they are executed implicitly.
87
88 The test suite runner can be executed in any location that the current
89 user can write to. It will create temporary files and directories,
90 but will in general clean up all of them.
91 *** Discussion of the various environment variables
92 **** srcdir
93 Must be set to the source of the openpgp test suite. Used to locate
94 data files.
95 **** GPGSCM_PATH
96 Used to locate the Scheme library as well as code used by the test
97 suite.
98 **** GNUPG_BUILD_ROOT
99 This envvar gives the root directory of the build tree. See
100 tests/gpgconf.ctl.in for the way we tell the GnuPG components this
101 location. Note that we can't use that envvar directly because this
102 would allow user scripts and other software to accidentally mess up the
103 used components.
104 **** argv[0]
105 run-tests.scm depends on being able to re-exec gpgscm. It uses
106 argv[0] for that. Therefore you must use an absolute path to invoke
107 gpgscm.
108 * How to write tests
109 gpgscm provides a number of functions to aid you in writing tests, as
110 well as bindings to process management abstractions provided by GnuPG.
111 For the Scheme environment provided by TinySCHEME, see the TinySCHEME
112 manual that is included in tests/gpgscm/Manual.txt.
113
114 For a quick start, please have a look at various tests that are
115 already implemented, e.g. 'encrypt.scm'.
116 ** The test framework
117 The functions info, error, and skip display their first argument and
118 flush the output buffers. error and skip will also terminate the
119 process, signaling that the test failed or should be skipped.
120
121 (for-each-p msg proc list) will display msg, and call proc with each
122 element of list while displaying the progress appropriately.
123 for-each-p' is similar, but accepts another callback before the 'list'
124 argument to format each item. for-each-p can be safely nested, and
125 the inner progress indicator will be abbreviated using '.'.
126 ** Debugging tests
127
128 Say you are working on a new test called 'your-test.scm', you can run
129 it on its own using
130
131 obj $ make -C tests/openpgp check TESTS=your-test.scm
132
133 but something isn't working as expected. There are several little
134 gadgets that might help. The first one is 'trace', a function that
135 prints the value given to it and evaluates to it. E.g.
136
137 (trace (+ 2 3))
138
139 prints '5' and evaluates to 5. Also, there is an 'assert' macro that
140 aborts the execution if its argument does not evaluate to a trueish
141 value. Feel free to express invariants with it.
142
143 You can also get an interactive repl by dropping
144
145 (interactive-repl (current-environment))
146
147 anywhere you like. Or, if you want to examine the environment from an
148 operating system shell, use
149
150 (interactive-shell)
151
152 ** Interfacing with gpg
153
154 defs.scm defines several convenience functions. Say you want to parse
155 the colon output from gpg, there is gpg-with-colons that splits the
156 result at newlines and colons, so you can use the result like this:
157
158 (define (fpr some-key)
159 (list-ref (assoc "fpr" (gpg-with-colons
160 `(--with-fingerprint
161 --list-secret-keys ,some-key)))
162 9))
163
164 Or if you want to count all non-revoked uids for a given key, do
165
166 (define (count-uids-of-secret-key some-key)
167 (length (filter (lambda (x) (and (string=? "uid" (car x))
168 (string=? "u" (cadr x))))
169 (gpg-with-colons
170 `(--with-fingerprint
171 --list-secret-keys ,some-key)))))
172
173 ** Temporary files
174 (lettmp <bindings> <body>) will create and delete temporary files that
175 you can use in <body>. (with-temporary-working-directory <body>) will
176 create a temporary director, change to that, and clean it up after
177 executing <body>).
178
179 make-temporary-file will create a temporary file. You can optionally
180 provide an argument to that function that will serve as tag so you can
181 distinguish the files for debugging. remove-temporary-file will
182 delete a file created using make-temporary-file.
183
184 ** Monadic transformer and pipe support
185 Tests often perform sequential transformations on files, or connect
186 processes using pipes. To aid you in this, the test framework
187 provides two monadic data structures.
188
189 (Currently, the implementation mashes the 'bind' operation together
190 with the application of the monad. Also, there is no 'return'
191 operation. I guess all of that could be implemented on top of
192 call/cc, but it isn't at the moment.)
193 *** pipe
194 The pipe monad constructs pipe lines. It consists of a function
195 pipe:do that binds the functions together and manages the execution of
196 the child processes, a family of functions that act as sources, a
197 function to spawn processes, and a family of functions acting as
198 sinks.
199
200 Sources are pipe:open, pipe:defer, pipe:echo. To spawn a process use
201 pipe:spawn, or the convenience function pipe:gpg. To sink the data
202 use pipe:splice, or pipe:write-to.
203
204 Example:
205
206 (pipe:do
207 (pipe:echo "3\n1\n2\n")
208 (pipe:spawn '("/usr/bin/sort"))
209 (pipe:write-to "sorted" (logior O_WRONLY O_CREAT) #o600))
210
211 Caveats: Due to the single-threaded nature of gpgscm you cannot use
212 both a source and sink that is implemented in Scheme. pipe:defer and
213 pipe:echo are executing in gpgscm, and so does pipe:splice.
214 *** tr
215 The transformer monad describes sequential file transformations.
216
217 There is one source function, tr:open. To describe a transformation
218 using some process, use tr:spawn, tr:gpg, or tr:pipe-do. There are
219 several sinks, although sink is not quite the right term, because the
220 data is not consumed, and hence one can use them at any position. The
221 "sinks" are tr:write-to, tr:call-with-content, tr:assert-identity,
222 tr:assert-weak-identity, and tr:assert-same.
223
224 A somewhat contrived example demonstrating many functions is:
225
226 (tr:do
227 (tr:pipe-do
228 (pipe:echo "3\n1\n2\n")
229 (pipe:spawn '("/usr/bin/sort")))
230 (tr:write-to "reference")
231 (tr:call-with-content
232 (lambda (c)
233 (echo "currently, c contains" (string-length c) "bytes")))
234 (tr:spawn "" '("/usr/bin/gcc" -x c "-E" -o **out** **in**))
235 (tr:pipe-do
236 (pipe:spawn '("/bin/grep" -v "#")))
237 (tr:assert-identity "reference"))
238
239 Caveats: As a convenience, gpgscm allows one to specify command line
240 arguments as Scheme symbols. Scheme symbols, however, are
241 case-insensitive, and get converted to lower case. Therefore, the -E
242 argument must be given as a string in the example above. Similarly,
243 you need to quote numerical values.
244 ** Process management
245 If you just need to execute a single command, there is (call-with-fds
246 cmdline infd outfd errfd) which executes cmdline with the given file
247 descriptors bound to it, and waits for its completion returning the
248 status code. There is (call cmdline) which is similar, but calls the
249 command with a closed stdin, connecting stdout and stderr to stderr if
250 gpgscm is executed with --verbose. (call-check cmdline) raises an
251 exception if the command does not return 0.
252
253 (call-popen cmdline input) calls a command, writes input to its stdin,
254 and returns any output from stdout, or raises an exception containing
255 stderr on failure.
256 * Sample messages