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