gnu: linux-libre@4.14: Update to 4.14.198.
[jackhill/guix/guix.git] / tests / substitute.scm
CommitLineData
e9c6c584
NK
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
434138e2 3;;; Copyright © 2014, 2015, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
e9c6c584
NK
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
2c74fde0
LC
20(define-module (test-substitute)
21 #:use-module (guix scripts substitute)
e9c6c584 22 #:use-module (guix base64)
ca719424 23 #:use-module (gcrypt hash)
0363991a 24 #:use-module (guix serialization)
ca719424 25 #:use-module (gcrypt pk-crypto)
e9c6c584 26 #:use-module (guix pki)
cdea30e0 27 #:use-module (guix config)
e903b7c1 28 #:use-module (guix base32)
cdea30e0 29 #:use-module ((guix store) #:select (%store-prefix))
f84f8590 30 #:use-module ((guix ui) #:select (guix-warning-port))
b90ae065 31 #:use-module ((guix utils) #:select (call-with-compressed-output-port))
a9468b42 32 #:use-module ((guix build utils)
b90ae065 33 #:select (mkdir-p delete-file-recursively dump-port))
a9468b42 34 #:use-module (guix tests http)
e9c6c584 35 #:use-module (rnrs bytevectors)
cdea30e0
LC
36 #:use-module (rnrs io ports)
37 #:use-module (web uri)
52f80dfc 38 #:use-module (ice-9 regex)
cdea30e0 39 #:use-module (srfi srfi-26)
e9c6c584 40 #:use-module (srfi srfi-34)
52f80dfc 41 #:use-module (srfi srfi-35)
e9c6c584
NK
42 #:use-module ((srfi srfi-64) #:hide (test-error)))
43
f84f8590
LC
44(define-syntax-rule (test-quit name error-rx exp)
45 "Emit a test that passes when EXP throws to 'quit' with value 1, and when
46it writes to GUIX-WARNING-PORT a messages that matches ERROR-RX."
cdea30e0 47 (test-equal name
f84f8590
LC
48 '(1 #t)
49 (let ((error-output (open-output-string)))
50 (parameterize ((guix-warning-port error-output))
51 (catch 'quit
52 (lambda ()
53 exp
54 #f)
55 (lambda (key value)
56 (list value
57 (let ((message (get-output-string error-output)))
58 (->bool (string-match error-rx message))))))))))
e9c6c584
NK
59
60(define %public-key
cdea30e0
LC
61 ;; This key is known to be in the ACL by default.
62 (call-with-input-file (string-append %config-directory "/signing-key.pub")
63 (compose string->canonical-sexp get-string-all)))
e9c6c584
NK
64
65(define %private-key
cdea30e0
LC
66 (call-with-input-file (string-append %config-directory "/signing-key.sec")
67 (compose string->canonical-sexp get-string-all)))
e9c6c584 68
52f80dfc
LC
69(define* (signature-body bv #:key (public-key %public-key))
70 "Return the signature of BV as the base64-encoded body of a narinfo's
cdea30e0 71'Signature' field."
e9c6c584
NK
72 (base64-encode
73 (string->utf8
74 (canonical-sexp->string
52f80dfc 75 (signature-sexp (bytevector->hash-data (sha256 bv)
e9c6c584
NK
76 #:key-type 'rsa)
77 %private-key
cdea30e0 78 public-key)))))
e9c6c584 79
e9c6c584
NK
80(define %wrong-public-key
81 (string->canonical-sexp "(public-key
82 (rsa
83 (n #00E05873AC2B168760343145918E954EE9AB73C026355693B192E01EE835261AA689E9EF46642E895BCD65C648524059FC450E4BA77A68F4C52D0E39EF0CC9359709AB6AAB153B63782201871325B0FDA19CB401CD99FD0C31A91CA9000AA90A77E82B89E036FB63BC1D3961207469B3B12468977148D376F8012BB12A4B11A8F1#)
84 (e #010001#)
85 )
86 )"))
87
52f80dfc
LC
88(define* (signature-field bv-or-str
89 #:key (version "1") (public-key %public-key))
90 "Return the 'Signature' field value of bytevector/string BV-OR-STR, using
91PUBLIC-KEY as the signature's principal, and using VERSION as the signature
92version identifier.."
93 (string-append version ";example.gnu.org;"
94 (signature-body (if (string? bv-or-str)
95 (string->utf8 bv-or-str)
96 bv-or-str)
97 #:public-key public-key)))
98
e9c6c584 99
cdea30e0 100\f
2c74fde0 101(test-begin "substitute")
e9c6c584 102
f84f8590
LC
103(test-quit "not a number"
104 "signature version"
52f80dfc
LC
105 (narinfo-signature->canonical-sexp
106 (signature-field "foo" #:version "not a number")))
e9c6c584 107
f84f8590
LC
108(test-quit "wrong version number"
109 "unsupported.*version"
52f80dfc
LC
110 (narinfo-signature->canonical-sexp
111 (signature-field "foo" #:version "2")))
e9c6c584
NK
112
113(test-assert "valid narinfo-signature->canonical-sexp"
52f80dfc 114 (canonical-sexp? (narinfo-signature->canonical-sexp (signature-field "foo"))))
e9c6c584 115
e9c6c584 116
cdea30e0 117\f
a9468b42
LC
118(define %main-substitute-directory
119 ;; The place where 'call-with-narinfo' stores its data by default.
120 (uri-path (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
121
122(define %alternate-substitute-directory
123 ;; Another place.
124 (string-append (dirname %main-substitute-directory)
125 "/substituter-alt-data"))
126
e9c6c584 127(define %narinfo
52f80dfc 128 ;; Skeleton of the narinfo used below.
cdea30e0
LC
129 (string-append "StorePath: " (%store-prefix)
130 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
e903b7c1
LC
131URL: example.nar
132Compression: none
133NarHash: sha256:" (bytevector->nix-base32-string
134 (sha256 (string->utf8 "Substitutable data."))) "
e9c6c584
NK
135NarSize: 42
136References: bar baz
cdea30e0
LC
137Deriver: " (%store-prefix) "/foo.drv
138System: mips64el-linux\n"))
e9c6c584 139
a9468b42
LC
140(define* (call-with-narinfo narinfo thunk
141 #:optional
142 (narinfo-directory %main-substitute-directory))
143 "Call THUNK in a context where the directory at URL is populated with
cdea30e0 144a file for NARINFO."
a9468b42
LC
145 (mkdir-p narinfo-directory)
146 (let ((cache-directory (string-append (getenv "XDG_CACHE_HOME")
147 "/guix/substitute/")))
cdea30e0
LC
148 (dynamic-wind
149 (lambda ()
150 (when (file-exists? cache-directory)
151 (delete-file-recursively cache-directory))
152 (call-with-output-file (string-append narinfo-directory
153 "/nix-cache-info")
154 (lambda (port)
155 (format port "StoreDir: ~a\nWantMassQuery: 0\n"
156 (%store-prefix))))
157 (call-with-output-file (string-append narinfo-directory "/"
158 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
159 ".narinfo")
160 (cut display narinfo <>))
161
e903b7c1
LC
162 ;; Prepare the nar.
163 (call-with-output-file
164 (string-append narinfo-directory "/example.out")
165 (cut display "Substitutable data." <>))
166 (call-with-output-file
167 (string-append narinfo-directory "/example.nar")
168 (cute write-file
169 (string-append narinfo-directory "/example.out") <>))
170
434138e2 171 (%allow-unauthenticated-substitutes? #f))
cdea30e0
LC
172 thunk
173 (lambda ()
a9468b42
LC
174 (when (file-exists? cache-directory)
175 (delete-file-recursively cache-directory))))))
cdea30e0
LC
176
177(define-syntax-rule (with-narinfo narinfo body ...)
178 (call-with-narinfo narinfo (lambda () body ...)))
179
a9468b42
LC
180(define-syntax-rule (with-narinfo* narinfo directory body ...)
181 (call-with-narinfo narinfo (lambda () body ...) directory))
182
2c74fde0 183;; Transmit these options to 'guix substitute'.
218f6ecc 184(substitute-urls (list (getenv "GUIX_BINARY_SUBSTITUTE_URL")))
cdea30e0 185
e903b7c1
LC
186(test-equal "query narinfo without signature"
187 "" ; not substitutable
188
189 (with-narinfo %narinfo
190 (string-trim-both
191 (with-output-to-string
192 (lambda ()
193 (with-input-from-string (string-append "have " (%store-prefix)
194 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
195 (lambda ()
2c74fde0 196 (guix-substitute "--query"))))))))
e903b7c1 197
cdea30e0 198(test-equal "query narinfo with invalid hash"
52f80dfc 199 ;; The hash in the signature differs from the hash of %NARINFO.
cdea30e0
LC
200 ""
201
52f80dfc
LC
202 (with-narinfo (string-append %narinfo "Signature: "
203 (signature-field "different body")
204 "\n")
cdea30e0
LC
205 (string-trim-both
206 (with-output-to-string
207 (lambda ()
208 (with-input-from-string (string-append "have " (%store-prefix)
209 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
210 (lambda ()
2c74fde0 211 (guix-substitute "--query"))))))))
cdea30e0 212
60b04024
LC
213(test-equal "query narinfo with signature over nothing"
214 ;; The signature is computed over the empty string, not over the important
215 ;; parts, so the narinfo must be ignored.
216 ""
217
218 (with-narinfo (string-append "Signature: " (signature-field "") "\n"
219 %narinfo "\n")
220 (string-trim-both
221 (with-output-to-string
222 (lambda ()
223 (with-input-from-string (string-append "have " (%store-prefix)
224 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
225 (lambda ()
226 (guix-substitute "--query"))))))))
227
228(test-equal "query narinfo with signature over irrelevant bits"
229 ;; The signature is valid but it does not cover the
230 ;; StorePath/NarHash/References tuple and is thus irrelevant; the narinfo
231 ;; must be ignored.
232 ""
233
234 (let ((prefix (string-append "StorePath: " (%store-prefix)
235 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
236URL: example.nar
237Compression: none\n")))
238 (with-narinfo (string-append prefix
239 "Signature: " (signature-field prefix) "
240NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
241NarSize: 42
242References: bar baz
243Deriver: " (%store-prefix) "/foo.drv
244System: mips64el-linux\n")
245 (string-trim-both
246 (with-output-to-string
247 (lambda ()
248 (with-input-from-string (string-append "have " (%store-prefix)
249 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
250 (lambda ()
251 (guix-substitute "--query")))))))))
252
cdea30e0
LC
253(test-equal "query narinfo signed with authorized key"
254 (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
255
52f80dfc
LC
256 (with-narinfo (string-append %narinfo "Signature: "
257 (signature-field %narinfo)
258 "\n")
cdea30e0
LC
259 (string-trim-both
260 (with-output-to-string
261 (lambda ()
262 (with-input-from-string (string-append "have " (%store-prefix)
263 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
264 (lambda ()
2c74fde0 265 (guix-substitute "--query"))))))))
cdea30e0
LC
266
267(test-equal "query narinfo signed with unauthorized key"
268 "" ; not substitutable
269
52f80dfc
LC
270 (with-narinfo (string-append %narinfo "Signature: "
271 (signature-field
272 %narinfo
273 #:public-key %wrong-public-key)
274 "\n")
cdea30e0
LC
275 (string-trim-both
276 (with-output-to-string
277 (lambda ()
278 (with-input-from-string (string-append "have " (%store-prefix)
279 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
280 (lambda ()
2c74fde0 281 (guix-substitute "--query"))))))))
cdea30e0 282
f84f8590 283(test-quit "substitute, no signature"
a9468b42 284 "no valid substitute"
e903b7c1 285 (with-narinfo %narinfo
2c74fde0
LC
286 (guix-substitute "--substitute"
287 (string-append (%store-prefix)
288 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
289 "foo")))
e903b7c1 290
f84f8590 291(test-quit "substitute, invalid hash"
a9468b42 292 "no valid substitute"
52f80dfc
LC
293 ;; The hash in the signature differs from the hash of %NARINFO.
294 (with-narinfo (string-append %narinfo "Signature: "
295 (signature-field "different body")
296 "\n")
2c74fde0
LC
297 (guix-substitute "--substitute"
298 (string-append (%store-prefix)
299 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
300 "foo")))
cdea30e0 301
f84f8590 302(test-quit "substitute, unauthorized key"
a9468b42 303 "no valid substitute"
52f80dfc
LC
304 (with-narinfo (string-append %narinfo "Signature: "
305 (signature-field
306 %narinfo
307 #:public-key %wrong-public-key)
308 "\n")
2c74fde0
LC
309 (guix-substitute "--substitute"
310 (string-append (%store-prefix)
311 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
312 "foo")))
e9c6c584 313
e903b7c1
LC
314(test-equal "substitute, authorized key"
315 "Substitutable data."
316 (with-narinfo (string-append %narinfo "Signature: "
317 (signature-field %narinfo))
318 (dynamic-wind
319 (const #t)
320 (lambda ()
2c74fde0
LC
321 (guix-substitute "--substitute"
322 (string-append (%store-prefix)
323 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
324 "substitute-retrieved")
e903b7c1
LC
325 (call-with-input-file "substitute-retrieved" get-string-all))
326 (lambda ()
327 (false-if-exception (delete-file "substitute-retrieved"))))))
328
a9468b42
LC
329(test-equal "substitute, unauthorized narinfo comes first"
330 "Substitutable data."
331 (with-narinfo*
332 (string-append %narinfo "Signature: "
333 (signature-field
334 %narinfo
335 #:public-key %wrong-public-key))
336 %alternate-substitute-directory
337
338 (with-narinfo* (string-append %narinfo "Signature: "
339 (signature-field %narinfo))
340 %main-substitute-directory
341
342 (dynamic-wind
343 (const #t)
344 (lambda ()
345 ;; Remove this file so that the substitute can only be retrieved
346 ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
347 (delete-file (string-append %main-substitute-directory
348 "/example.nar"))
349
350 (parameterize ((substitute-urls
351 (map (cut string-append "file://" <>)
352 (list %alternate-substitute-directory
353 %main-substitute-directory))))
354 (guix-substitute "--substitute"
355 (string-append (%store-prefix)
356 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
357 "substitute-retrieved"))
358 (call-with-input-file "substitute-retrieved" get-string-all))
359 (lambda ()
360 (false-if-exception (delete-file "substitute-retrieved")))))))
361
362(test-equal "substitute, unsigned narinfo comes first"
363 "Substitutable data."
364 (with-narinfo* %narinfo ;not signed!
365 %alternate-substitute-directory
366
367 (with-narinfo* (string-append %narinfo "Signature: "
368 (signature-field %narinfo))
369 %main-substitute-directory
370
371 (dynamic-wind
372 (const #t)
373 (lambda ()
374 ;; Remove this file so that the substitute can only be retrieved
375 ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
376 (delete-file (string-append %main-substitute-directory
377 "/example.nar"))
378
379 (parameterize ((substitute-urls
380 (map (cut string-append "file://" <>)
381 (list %alternate-substitute-directory
382 %main-substitute-directory))))
383 (guix-substitute "--substitute"
384 (string-append (%store-prefix)
385 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
386 "substitute-retrieved"))
387 (call-with-input-file "substitute-retrieved" get-string-all))
388 (lambda ()
389 (false-if-exception (delete-file "substitute-retrieved")))))))
390
391(test-equal "substitute, first narinfo is unsigned and has wrong hash"
392 "Substitutable data."
393 (with-narinfo* (regexp-substitute #f
394 (string-match "NarHash: [[:graph:]]+"
395 %narinfo)
396 'pre
397 "NarHash: sha256:"
398 (bytevector->nix-base32-string
399 (make-bytevector 32))
400 'post)
401 %alternate-substitute-directory
402
403 (with-narinfo* (string-append %narinfo "Signature: "
404 (signature-field %narinfo))
405 %main-substitute-directory
406
407 (dynamic-wind
408 (const #t)
409 (lambda ()
410 ;; This time remove the file so that the substitute can only be
411 ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
412 (delete-file (string-append %alternate-substitute-directory
413 "/example.nar"))
414
415 (parameterize ((substitute-urls
416 (map (cut string-append "file://" <>)
417 (list %alternate-substitute-directory
418 %main-substitute-directory))))
419 (guix-substitute "--substitute"
420 (string-append (%store-prefix)
421 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
422 "substitute-retrieved"))
423 (call-with-input-file "substitute-retrieved" get-string-all))
424 (lambda ()
425 (false-if-exception (delete-file "substitute-retrieved")))))))
426
427(test-equal "substitute, first narinfo is unsigned and has wrong refs"
428 "Substitutable data."
429 (with-narinfo* (regexp-substitute #f
430 (string-match "References: ([^\n]+)\n"
431 %narinfo)
432 'pre "References: " 1
433 " wrong set of references\n"
434 'post)
435 %alternate-substitute-directory
436
437 (with-narinfo* (string-append %narinfo "Signature: "
438 (signature-field %narinfo))
439 %main-substitute-directory
440
441 (dynamic-wind
442 (const #t)
443 (lambda ()
444 ;; This time remove the file so that the substitute can only be
445 ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
446 (delete-file (string-append %alternate-substitute-directory
447 "/example.nar"))
448
449 (parameterize ((substitute-urls
450 (map (cut string-append "file://" <>)
451 (list %alternate-substitute-directory
452 %main-substitute-directory))))
453 (guix-substitute "--substitute"
454 (string-append (%store-prefix)
455 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
456 "substitute-retrieved"))
457 (call-with-input-file "substitute-retrieved" get-string-all))
458 (lambda ()
459 (false-if-exception (delete-file "substitute-retrieved")))))))
460
461(test-quit "substitute, two invalid narinfos"
462 "no valid substitute"
463 (with-narinfo* %narinfo ;not signed
464 %alternate-substitute-directory
465
466 (with-narinfo* (string-append %narinfo "Signature: " ;unauthorized
467 (signature-field
468 %narinfo
469 #:public-key %wrong-public-key))
470 %main-substitute-directory
471
472 (guix-substitute "--substitute"
473 (string-append (%store-prefix)
474 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
475 "substitute-retrieved"))))
476
b90ae065
LC
477(test-equal "substitute, narinfo with several URLs"
478 "Substitutable data."
479 (let ((narinfo (string-append "StorePath: " (%store-prefix)
480 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
481URL: example.nar.gz
482Compression: gzip
483URL: example.nar.lz
484Compression: lzip
485URL: example.nar
486Compression: none
487NarHash: sha256:" (bytevector->nix-base32-string
488 (sha256 (string->utf8 "Substitutable data."))) "
489NarSize: 42
490References: bar baz
491Deriver: " (%store-prefix) "/foo.drv
492System: mips64el-linux\n")))
493 (with-narinfo (string-append narinfo "Signature: "
494 (signature-field narinfo))
495 (dynamic-wind
496 (const #t)
497 (lambda ()
498 (define (compress input output compression)
499 (call-with-output-file output
500 (lambda (port)
501 (call-with-compressed-output-port compression port
502 (lambda (port)
503 (call-with-input-file input
504 (lambda (input)
505 (dump-port input port))))))))
506
507 (let ((nar (string-append %main-substitute-directory
508 "/example.nar")))
509 (compress nar (string-append nar ".gz") 'gzip)
4c0c65ac 510 (compress nar (string-append nar ".lz") 'lzip))
b90ae065
LC
511
512 (parameterize ((substitute-urls
513 (list (string-append "file://"
514 %main-substitute-directory))))
515 (guix-substitute "--substitute"
516 (string-append (%store-prefix)
517 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
518 "substitute-retrieved"))
519 (call-with-input-file "substitute-retrieved" get-string-all))
520 (lambda ()
521 (false-if-exception (delete-file "substitute-retrieved")))))))
522
2c74fde0 523(test-end "substitute")
e9c6c584 524
cdea30e0
LC
525;;; Local Variables:
526;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
a9468b42 527;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
f84f8590 528;;; eval: (put 'test-quit 'scheme-indent-function 2)
cdea30e0 529;;; End: