daemon: Read substitute nar size as 'unsigned long long'.
[jackhill/guix/guix.git] / tests / store.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (test-store)
20 #:use-module (guix tests)
21 #:use-module (guix config)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix monads)
25 #:use-module ((gcrypt hash) #:prefix gcrypt:)
26 #:use-module ((gcrypt pk-crypto) #:prefix gcrypt:)
27 #:use-module (guix pki)
28 #:use-module (guix base32)
29 #:use-module (guix packages)
30 #:use-module (guix derivations)
31 #:use-module (guix serialization)
32 #:use-module (guix build utils)
33 #:use-module (guix gexp)
34 #:use-module (gnu packages)
35 #:use-module (gnu packages bootstrap)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 regex)
38 #:use-module (rnrs bytevectors)
39 #:use-module (rnrs io ports)
40 #:use-module (web uri)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-11)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-64))
46
47 ;; Test the (guix store) module.
48
49 (define %store
50 (open-connection-for-tests))
51
52 (define %shell
53 (or (getenv "SHELL") (getenv "CONFIG_SHELL") "/bin/sh"))
54
55 \f
56 (test-begin "store")
57
58 (test-assert "open-connection with file:// URI"
59 (let ((store (open-connection (string-append "file://"
60 (%daemon-socket-uri)))))
61 (and (add-text-to-store store "foo" "bar")
62 (begin
63 (close-connection store)
64 #t))))
65
66 (test-equal "connection handshake error"
67 EPROTO
68 (let ((port (%make-void-port "rw")))
69 (guard (c ((store-connection-error? c)
70 (and (eq? port (store-connection-error-file c))
71 (store-connection-error-code c))))
72 (open-connection #f #:port port)
73 'broken)))
74
75 (test-equal "store-path-hash-part"
76 "283gqy39v3g9dxjy26rynl0zls82fmcg"
77 (store-path-hash-part
78 (string-append (%store-prefix)
79 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
80
81 (test-equal "store-path-hash-part #f"
82 #f
83 (store-path-hash-part
84 (string-append (%store-prefix)
85 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
86
87 (test-equal "store-path-package-name"
88 "guile-2.0.7"
89 (store-path-package-name
90 (string-append (%store-prefix)
91 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
92
93 (test-equal "store-path-package-name #f"
94 #f
95 (store-path-package-name
96 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
97
98 (test-assert "direct-store-path?"
99 (and (direct-store-path?
100 (string-append (%store-prefix)
101 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
102 (not (direct-store-path?
103 (string-append
104 (%store-prefix)
105 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
106 (not (direct-store-path? (%store-prefix)))))
107
108 (test-skip (if %store 0 15))
109
110 (test-equal "profiles/per-user exists and is not writable"
111 #o755
112 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
113
114 (test-equal "profiles/per-user/$USER exists"
115 (list (getuid) #o755)
116 (let ((s (stat (string-append %state-directory "/profiles/per-user/"
117 (passwd:name (getpwuid (getuid)))))))
118 (list (stat:uid s) (stat:perms s))))
119
120 (test-equal "add-to-store"
121 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256")
122 (let* ((file (search-path %load-path "guix.scm"))
123 (content (call-with-input-file file get-bytevector-all)))
124 (map (lambda (hash-algo)
125 (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
126 (and (direct-store-path? file)
127 (bytevector=? (call-with-input-file file get-bytevector-all)
128 content)
129 hash-algo)))
130 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
131
132 (test-equal "add-data-to-store"
133 #vu8(1 2 3 4 5)
134 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
135 get-bytevector-all))
136
137 (test-assert "valid-path? live"
138 (let ((p (add-text-to-store %store "hello" "hello, world")))
139 (valid-path? %store p)))
140
141 (test-assert "valid-path? false"
142 (not (valid-path? %store
143 (string-append (%store-prefix) "/"
144 (make-string 32 #\e) "-foobar"))))
145
146 (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
147 '(1 2 3)
148 (call-with-values
149 (lambda ()
150 (with-store s
151 (add-text-to-store s "foo" "bar")
152 (values 1 2 3)))
153 list))
154
155 (test-assert "valid-path? error"
156 (with-store s
157 (guard (c ((store-protocol-error? c) #t))
158 (valid-path? s "foo")
159 #f)))
160
161 (test-assert "valid-path? recovery"
162 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
163 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
164 ;; file name. See
165 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
166 ;; details.
167 (with-store s
168 (let-syntax ((true-if-error (syntax-rules ()
169 ((_ exp)
170 (guard (c ((store-protocol-error? c) #t))
171 exp #f)))))
172 (and (true-if-error (valid-path? s "foo"))
173 (true-if-error (valid-path? s "bar"))
174 (true-if-error (valid-path? s "baz"))
175 (true-if-error (valid-path? s "chbouib"))
176 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
177
178 (test-assert "hash-part->path"
179 (let ((p (add-text-to-store %store "hello" "hello, world")))
180 (equal? (hash-part->path %store (store-path-hash-part p))
181 p)))
182
183 (test-assert "dead-paths"
184 (let ((p (add-text-to-store %store "random-text" (random-text))))
185 (->bool (member p (dead-paths %store)))))
186
187 ;; FIXME: Find a test for `live-paths'.
188 ;;
189 ;; (test-assert "temporary root is in live-paths"
190 ;; (let* ((p1 (add-text-to-store %store "random-text"
191 ;; (random-text) '()))
192 ;; (b (add-text-to-store %store "link-builder"
193 ;; (format #f "echo ~a > $out" p1)
194 ;; '()))
195 ;; (d1 (derivation %store "link"
196 ;; "/bin/sh" `("-e" ,b)
197 ;; #:inputs `((,b) (,p1))))
198 ;; (p2 (derivation->output-path d1)))
199 ;; (and (add-temp-root %store p2)
200 ;; (build-derivations %store (list d1))
201 ;; (valid-path? %store p1)
202 ;; (member (pk p2) (live-paths %store)))))
203
204 (test-assert "add-indirect-root and find-roots"
205 (call-with-temporary-directory
206 (lambda (directory)
207 (let* ((item (add-text-to-store %store "something" (random-text)))
208 (root (string-append directory "/gc-root")))
209 (symlink item root)
210 (add-indirect-root %store root)
211 (let ((result (member (cons root item) (find-roots %store))))
212 (delete-file root)
213 result)))))
214
215 (test-assert "permanent root"
216 (let* ((p (with-store store
217 (let ((p (add-text-to-store store "random-text"
218 (random-text))))
219 (add-permanent-root p)
220 (add-permanent-root p) ; should not throw
221 p))))
222 (and (member p (live-paths %store))
223 (begin
224 (remove-permanent-root p)
225 (->bool (member p (dead-paths %store)))))))
226
227 (test-assert "dead path can be explicitly collected"
228 (let ((p (add-text-to-store %store "random-text"
229 (random-text) '())))
230 (let-values (((paths freed) (delete-paths %store (list p))))
231 (and (equal? paths (list p))
232 ;; XXX: On some file systems (notably Btrfs), freed
233 ;; may return 0. See <https://bugs.gnu.org/29363>.
234 ;;(> freed 0)
235 (not (file-exists? p))))))
236
237 (test-assert "add-text-to-store/add-to-store vs. delete-paths"
238 ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
239 ;; store item without noticing that it is no longer valid.
240 (with-store store
241 (let* ((text (random-text))
242 (file (search-path %load-path "guix.scm"))
243 (path1 (add-text-to-store store "delete-me" text))
244 (path2 (add-to-store store "delete-me" #t "sha256" file))
245 (deleted (delete-paths store (list path1 path2))))
246 (and (string=? path1 (add-text-to-store store "delete-me" text))
247 (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
248 (lset= string=? deleted (list path1 path2))
249 (valid-path? store path1)
250 (valid-path? store path2)
251 (file-exists? path1)
252 (file-exists? path2)))))
253
254 (test-equal "add-file-tree-to-store"
255 `(42
256 ("." directory #t)
257 ("./bar" directory #t)
258 ("./foo" directory #t)
259 ("./foo/a" regular "file a")
260 ("./foo/b" symlink "a")
261 ("./foo/c" directory #t)
262 ("./foo/c/p" regular "file p")
263 ("./foo/c/q" directory #t)
264 ("./foo/c/q/x" regular
265 ,(string-append "#!" %shell "\nexit 42"))
266 ("./foo/c/q/y" symlink "..")
267 ("./foo/c/q/z" directory #t))
268 (let* ((tree `("file-tree" directory
269 ("foo" directory
270 ("a" regular (data "file a"))
271 ("b" symlink "a")
272 ("c" directory
273 ("p" regular (data ,(string->utf8 "file p")))
274 ("q" directory
275 ("x" executable
276 (data ,(string-append "#!" %shell "\nexit 42")))
277 ("y" symlink "..")
278 ("z" directory))))
279 ("bar" directory)))
280 (result (add-file-tree-to-store %store tree)))
281 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
282 (with-directory-excursion result
283 (map (lambda (file)
284 (let ((type (stat:type (lstat file))))
285 `(,file ,type
286 ,(match type
287 ((or 'regular 'executable)
288 (call-with-input-file file
289 get-string-all))
290 ('symlink (readlink file))
291 ('directory #t)))))
292 (find-files "." #:directories? #t))))))
293
294 (test-equal "add-file-tree-to-store, flat"
295 "Hello, world!"
296 (let* ((tree `("flat-file" regular (data "Hello, world!")))
297 (result (add-file-tree-to-store %store tree)))
298 (and (file-exists? result)
299 (call-with-input-file result get-string-all))))
300
301 (test-assert "references"
302 (let* ((t1 (add-text-to-store %store "random1"
303 (random-text)))
304 (t2 (add-text-to-store %store "random2"
305 (random-text) (list t1))))
306 (and (equal? (list t1) (references %store t2))
307 (equal? (list t2) (referrers %store t1))
308 (null? (references %store t1))
309 (null? (referrers %store t2)))))
310
311 (test-equal "substitutable-path-info when substitutes are turned off"
312 '()
313 (with-store s
314 (set-build-options s #:use-substitutes? #f)
315 (let* ((b (add-to-store s "bash" #t "sha256"
316 (search-bootstrap-binary "bash"
317 (%current-system))))
318 (d (derivation s "the-thing" b '("--version")
319 #:inputs `((,b))))
320 (o (derivation->output-path d)))
321 (with-derivation-narinfo d
322 (substitutable-path-info s (list o))))))
323
324 (test-equal "substitutable-paths when substitutes are turned off"
325 '()
326 (with-store s
327 (set-build-options s #:use-substitutes? #f)
328 (let* ((b (add-to-store s "bash" #t "sha256"
329 (search-bootstrap-binary "bash"
330 (%current-system))))
331 (d (derivation s "the-thing" b '("--version")
332 #:inputs `((,b))))
333 (o (derivation->output-path d)))
334 (with-derivation-narinfo d
335 (substitutable-paths s (list o))))))
336
337 (test-assert "requisites"
338 (let* ((t1 (add-text-to-store %store "random1"
339 (random-text) '()))
340 (t2 (add-text-to-store %store "random2"
341 (random-text) (list t1)))
342 (t3 (add-text-to-store %store "random3"
343 (random-text) (list t2)))
344 (t4 (add-text-to-store %store "random4"
345 (random-text) (list t1 t3))))
346 (define (same? x y)
347 (and (= (length x) (length y))
348 (lset= equal? x y)))
349
350 (and (same? (requisites %store (list t1)) (list t1))
351 (same? (requisites %store (list t2)) (list t1 t2))
352 (same? (requisites %store (list t3)) (list t1 t2 t3))
353 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
354 (same? (requisites %store (list t1 t2 t3 t4))
355 (list t1 t2 t3 t4)))))
356
357 (test-assert "derivers"
358 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
359 (s (add-to-store %store "bash" #t "sha256"
360 (search-bootstrap-binary "bash"
361 (%current-system))))
362 (d (derivation %store "the-thing"
363 s `("-e" ,b)
364 #:env-vars `(("foo" . ,(random-text)))
365 #:inputs `((,b) (,s))))
366 (o (derivation->output-path d)))
367 (and (build-derivations %store (list d))
368 (equal? (query-derivation-outputs %store (derivation-file-name d))
369 (list o))
370 (equal? (valid-derivers %store o)
371 (list (derivation-file-name d))))))
372
373 (test-equal "with-build-handler"
374 'success
375 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
376 (s (add-to-store %store "bash" #t "sha256"
377 (search-bootstrap-binary "bash"
378 (%current-system))))
379 (d1 (derivation %store "the-thing"
380 s `("-e" ,b)
381 #:env-vars `(("foo" . ,(random-text)))
382 #:sources (list b s)))
383 (d2 (derivation %store "the-thing"
384 s `("-e" ,b)
385 #:env-vars `(("foo" . ,(random-text))
386 ("bar" . "baz"))
387 #:sources (list b s)))
388 (o1 (derivation->output-path d1))
389 (o2 (derivation->output-path d2)))
390 (with-build-handler
391 (let ((counter 0))
392 (lambda (continue store things mode)
393 (match things
394 ((drv)
395 (set! counter (+ 1 counter))
396 (if (string=? drv (derivation-file-name d1))
397 (continue #t)
398 (and (string=? drv (derivation-file-name d2))
399 (= counter 2)
400 'success))))))
401 (build-derivations %store (list d1))
402 (build-derivations %store (list d2))
403 'fail)))
404
405 (test-equal "with-build-handler + with-store"
406 'success
407 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
408 ;; even though 'with-build-handler' is outside the dynamic extent of
409 ;; 'with-store'.
410 (with-build-handler (lambda (continue store things mode)
411 (match things
412 ((drv)
413 (and (string-suffix? "thingie.drv" drv)
414 (not (port-closed?
415 (store-connection-socket store)))
416 (continue #t)))))
417 (with-store store
418 (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
419 (s (add-to-store store "bash" #t "sha256"
420 (search-bootstrap-binary "bash"
421 (%current-system))))
422 (d (derivation store "thingie"
423 s `("-e" ,b)
424 #:env-vars `(("foo" . ,(random-text)))
425 #:sources (list b s))))
426 (build-derivations store (list d))
427
428 ;; Here STORE's socket should still be open.
429 (and (valid-path? store (derivation->output-path d))
430 'success)))))
431
432 (test-assert "map/accumulate-builds"
433 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
434 (s (add-to-store %store "bash" #t "sha256"
435 (search-bootstrap-binary "bash"
436 (%current-system))))
437 (d1 (derivation %store "the-thing"
438 s `("-e" ,b)
439 #:env-vars `(("foo" . ,(random-text)))
440 #:sources (list b s)))
441 (d2 (derivation %store "the-thing"
442 s `("-e" ,b)
443 #:env-vars `(("foo" . ,(random-text))
444 ("bar" . "baz"))
445 #:sources (list b s))))
446 (with-build-handler (lambda (continue store things mode)
447 (equal? (map derivation-file-name (list d1 d2))
448 things))
449 (map/accumulate-builds %store
450 (lambda (drv)
451 (build-derivations %store (list drv))
452 (add-to-store %store "content-addressed"
453 #t "sha256"
454 (derivation->output-path drv)))
455 (list d1 d2)))))
456
457 (test-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
458 (iota 20)
459
460 ;; Make sure that, when the cutoff is reached, 'map/accumulate-builds' still
461 ;; returns the right result and calls the build handler by batches.
462 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
463 (s (add-to-store %store "bash" #t "sha256"
464 (search-bootstrap-binary "bash"
465 (%current-system))))
466 (d (map (lambda (i)
467 (derivation %store (string-append "the-thing-"
468 (number->string i))
469 s `("-e" ,b)
470 #:env-vars `(("foo" . ,(random-text)))
471 #:sources (list b s)
472 #:properties `((n . ,i))))
473 (iota 20)))
474 (calls '()))
475 (define lst
476 (with-build-handler (lambda (continue store things mode)
477 (set! calls (cons things calls))
478 (continue #f))
479 (map/accumulate-builds %store
480 (lambda (d)
481 (build-derivations %store (list d))
482 (assq-ref (derivation-properties d) 'n))
483 d
484 #:cutoff 7)))
485
486 (match (reverse calls)
487 (((batch1 ...) (batch2 ...) (batch3 ...))
488 (and (equal? (map derivation-file-name (take d 8)) batch1)
489 (equal? (map derivation-file-name (take (drop d 8) 8)) batch2)
490 (equal? (map derivation-file-name (drop d 16)) batch3)
491 lst)))))
492
493 (test-equal "map/accumulate-builds and different store"
494 '(d2) ;see <https://issues.guix.gnu.org/46756>
495 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
496 (s (add-to-store %store "bash" #t "sha256"
497 (search-bootstrap-binary "bash"
498 (%current-system))))
499 (d1 (derivation %store "first"
500 s `("-e" ,b)
501 #:env-vars `(("foo" . ,(random-text)))
502 #:sources (list b s)))
503 (d2 (derivation %store "second"
504 s `("-e" ,b)
505 #:env-vars `(("foo" . ,(random-text))
506 ("bar" . "baz"))
507 #:sources (list b s))))
508 (with-store alternate-store
509 (with-build-handler (lambda (continue store things mode)
510 ;; If this handler is called, it means that
511 ;; 'map/accumulate-builds' triggered a build,
512 ;; which it shouldn't since the inner
513 ;; 'build-derivations' call is for another store.
514 'failed)
515 (map/accumulate-builds %store
516 (lambda (drv)
517 (build-derivations alternate-store (list d2))
518 'd2)
519 (list d1))))))
520
521 (test-assert "mapm/accumulate-builds"
522 (let* ((d1 (run-with-store %store
523 (gexp->derivation "foo" #~(mkdir #$output))))
524 (d2 (run-with-store %store
525 (gexp->derivation "bar" #~(mkdir #$output)))))
526 (with-build-handler (lambda (continue store things mode)
527 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
528 (pk 'XX things)))
529 (run-with-store %store
530 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
531
532 (test-equal "mapm/accumulate-builds, %current-target-system"
533 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
534 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
535 ;; right #:target.
536 (run-with-store %store
537 (mlet %store-monad ((lst1 (mapm %store-monad
538 (lambda _
539 (current-target-system))
540 '(a b)))
541 (lst2 (mapm/accumulate-builds
542 (lambda _
543 (current-target-system))
544 '(a b))))
545 (return (list lst1 lst2)))
546 #:system system
547 #:target "i586-pc-gnu"))
548
549 (test-assert "topologically-sorted, one item"
550 (let* ((a (add-text-to-store %store "a" "a"))
551 (b (add-text-to-store %store "b" "b" (list a)))
552 (c (add-text-to-store %store "c" "c" (list b)))
553 (d (add-text-to-store %store "d" "d" (list c)))
554 (s (topologically-sorted %store (list d))))
555 (equal? s (list a b c d))))
556
557 (test-assert "topologically-sorted, several items"
558 (let* ((a (add-text-to-store %store "a" "a"))
559 (b (add-text-to-store %store "b" "b" (list a)))
560 (c (add-text-to-store %store "c" "c" (list b)))
561 (d (add-text-to-store %store "d" "d" (list c)))
562 (s1 (topologically-sorted %store (list d a c b)))
563 (s2 (topologically-sorted %store (list b d c a b d))))
564 (equal? s1 s2 (list a b c d))))
565
566 (test-assert "topologically-sorted, more difficult"
567 (let* ((a (add-text-to-store %store "a" "a"))
568 (b (add-text-to-store %store "b" "b" (list a)))
569 (c (add-text-to-store %store "c" "c" (list b)))
570 (d (add-text-to-store %store "d" "d" (list c)))
571 (w (add-text-to-store %store "w" "w"))
572 (x (add-text-to-store %store "x" "x" (list w)))
573 (y (add-text-to-store %store "y" "y" (list x d)))
574 (s1 (topologically-sorted %store (list y)))
575 (s2 (topologically-sorted %store (list c y)))
576 (s3 (topologically-sorted %store (cons y (references %store y)))))
577 ;; The order in which 'references' returns the references of Y is
578 ;; unspecified, so accommodate.
579 (let* ((x-then-d? (equal? (references %store y) (list x d))))
580 (and (equal? s1
581 (if x-then-d?
582 (list w x a b c d y)
583 (list a b c d w x y)))
584 (equal? s2
585 (if x-then-d?
586 (list a b c w x d y)
587 (list a b c d w x y)))
588 (lset= string=? s1 s3)))))
589
590 (test-assert "current-build-output-port, UTF-8"
591 ;; Are UTF-8 strings in the build log properly interpreted?
592 (string-contains
593 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
594 (call-with-output-string
595 (lambda (port)
596 (parameterize ((current-build-output-port port))
597 (let* ((s "Here’s a Greek letter: λ.")
598 (d (build-expression->derivation
599 %store "foo" `(display ,s)
600 #:guile-for-build
601 (package-derivation s %bootstrap-guile (%current-system)))))
602 (guard (c ((store-protocol-error? c) #t))
603 (build-derivations %store (list d))))))))
604 "Here’s a Greek letter: λ."))
605
606 (test-assert "current-build-output-port, UTF-8 + garbage"
607 ;; What about a mixture of UTF-8 + garbage?
608 (string-contains
609 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
610 (call-with-output-string
611 (lambda (port)
612 (parameterize ((current-build-output-port port))
613 (let ((d (build-expression->derivation
614 %store "foo"
615 `(begin
616 (use-modules (rnrs io ports))
617 (display "garbage: ")
618 (put-bytevector (current-output-port) #vu8(128))
619 (display "lambda: λ\n"))
620 #:guile-for-build
621 (package-derivation %store %bootstrap-guile))))
622 (guard (c ((store-protocol-error? c) #t))
623 (build-derivations %store (list d))))))))
624 "garbage: �lambda: λ"))
625
626 (test-assert "log-file, derivation"
627 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
628 (s (add-to-store %store "bash" #t "sha256"
629 (search-bootstrap-binary "bash"
630 (%current-system))))
631 (d (derivation %store "the-thing"
632 s `("-e" ,b)
633 #:env-vars `(("foo" . ,(random-text)))
634 #:inputs `((,b) (,s)))))
635 (and (build-derivations %store (list d))
636 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
637
638 (test-assert "log-file, output file name"
639 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
640 (s (add-to-store %store "bash" #t "sha256"
641 (search-bootstrap-binary "bash"
642 (%current-system))))
643 (d (derivation %store "the-thing"
644 s `("-e" ,b)
645 #:env-vars `(("foo" . ,(random-text)))
646 #:inputs `((,b) (,s))))
647 (o (derivation->output-path d)))
648 (and (build-derivations %store (list d))
649 (file-exists? (pk (log-file %store o)))
650 (string=? (log-file %store (derivation-file-name d))
651 (log-file %store o)))))
652
653 (test-assert "no substitutes"
654 (with-store s
655 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
656 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
657 (o (map derivation->output-path (list d1 d2))))
658 (set-build-options s #:use-substitutes? #f)
659 (and (not (has-substitutes? s (derivation-file-name d1)))
660 (not (has-substitutes? s (derivation-file-name d2)))
661 (null? (substitutable-paths s o))
662 (null? (substitutable-path-info s o))))))
663
664 (test-assert "build-things with output path"
665 (with-store s
666 (let* ((c (random-text)) ;contents of the output
667 (d (build-expression->derivation
668 s "substitute-me"
669 `(call-with-output-file %output
670 (lambda (p)
671 (display ,c p)))
672 #:guile-for-build
673 (package-derivation s %bootstrap-guile (%current-system))))
674 (o (derivation->output-path d)))
675 (set-build-options s #:use-substitutes? #f)
676
677 ;; Pass 'build-things' the output file name, O. However, since there
678 ;; are no substitutes for O, it will just do nothing.
679 (build-things s (list o))
680 (not (valid-path? s o)))))
681
682 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
683
684 (test-assert "substitute query"
685 (with-store s
686 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
687 (o (derivation->output-path d)))
688 ;; Create fake substituter data, to be read by 'guix substitute'.
689 (with-derivation-narinfo d
690 ;; Remove entry from the local cache.
691 (false-if-exception
692 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
693 "/guix/substitute")))
694
695 ;; Make sure 'guix substitute' correctly communicates the above
696 ;; data.
697 (set-build-options s #:use-substitutes? #t
698 #:substitute-urls (%test-substitute-urls))
699 (and (has-substitutes? s o)
700 (equal? (list o) (substitutable-paths s (list o)))
701 (match (pk 'spi (substitutable-path-info s (list o)))
702 (((? substitutable? s))
703 (and (string=? (substitutable-deriver s)
704 (derivation-file-name d))
705 (null? (substitutable-references s))
706 (equal? (substitutable-nar-size s) 1234)))))))))
707
708 (test-assert "substitute query, alternating URLs"
709 (let* ((d (with-store s
710 (package-derivation s %bootstrap-guile (%current-system))))
711 (o (derivation->output-path d)))
712 (with-derivation-narinfo d
713 ;; Remove entry from the local cache.
714 (false-if-exception
715 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
716 "/guix/substitute")))
717
718 ;; Note: We reconnect to the daemon to force a new instance of 'guix
719 ;; substitute' to be used; otherwise the #:substitute-urls of
720 ;; 'set-build-options' would have no effect.
721
722 (and (with-store s ;the right substitute URL
723 (set-build-options s #:use-substitutes? #t
724 #:substitute-urls (%test-substitute-urls))
725 (has-substitutes? s o))
726 (with-store s ;the wrong one
727 (set-build-options s #:use-substitutes? #t
728 #:substitute-urls (list
729 "http://does-not-exist"))
730 (not (has-substitutes? s o)))
731 (with-store s ;the right one again
732 (set-build-options s #:use-substitutes? #t
733 #:substitute-urls (%test-substitute-urls))
734 (has-substitutes? s o))
735 (with-store s ;empty list of URLs
736 (set-build-options s #:use-substitutes? #t
737 #:substitute-urls '())
738 (not (has-substitutes? s o)))))))
739
740 (test-assert "substitute"
741 (with-store s
742 (let* ((c (random-text)) ; contents of the output
743 (d (build-expression->derivation
744 s "substitute-me"
745 `(call-with-output-file %output
746 (lambda (p)
747 (exit 1) ; would actually fail
748 (display ,c p)))
749 #:guile-for-build
750 (package-derivation s %bootstrap-guile (%current-system))))
751 (o (derivation->output-path d)))
752 (with-derivation-substitute d c
753 (set-build-options s #:use-substitutes? #t
754 #:substitute-urls (%test-substitute-urls))
755 (and (has-substitutes? s o)
756 (build-derivations s (list d))
757 (canonical-file? o)
758 (equal? c (call-with-input-file o get-string-all)))))))
759
760 (test-assert "substitute, deduplication"
761 (with-store s
762 ;; Note: C must be longer than %DEDUPLICATION-MINIMUM-SIZE.
763 (let* ((c (string-concatenate
764 (make-list 200 (random-text)))) ; contents of the output
765 (g (package-derivation s %bootstrap-guile))
766 (d1 (build-expression->derivation s "substitute-me"
767 `(begin ,c (exit 1))
768 #:guile-for-build g))
769 (d2 (build-expression->derivation s "build-me"
770 `(call-with-output-file %output
771 (lambda (p)
772 (display ,c p)))
773 #:guile-for-build g))
774 (o1 (derivation->output-path d1))
775 (o2 (derivation->output-path d2)))
776 (with-derivation-substitute d1 c
777 (set-build-options s #:use-substitutes? #t
778 #:substitute-urls (%test-substitute-urls))
779 (and (has-substitutes? s o1)
780 (build-derivations s (list d2)) ;build
781 (build-derivations s (list d1)) ;substitute
782 (canonical-file? o1)
783 (equal? c (call-with-input-file o1 get-string-all))
784 (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
785
786 (test-assert "substitute + build-things with output path"
787 (with-store s
788 (let* ((c (random-text)) ;contents of the output
789 (d (build-expression->derivation
790 s "substitute-me"
791 `(call-with-output-file %output
792 (lambda (p)
793 (exit 1) ;would actually fail
794 (display ,c p)))
795 #:guile-for-build
796 (package-derivation s %bootstrap-guile (%current-system))))
797 (o (derivation->output-path d)))
798 (with-derivation-substitute d c
799 (set-build-options s #:use-substitutes? #t
800 #:substitute-urls (%test-substitute-urls))
801 (and (has-substitutes? s o)
802 (build-things s (list o)) ;give the output path
803 (valid-path? s o)
804 (canonical-file? o)
805 (equal? c (call-with-input-file o get-string-all)))))))
806
807 (test-assert "substitute + build-things with specific output"
808 (with-store s
809 (let* ((c (random-text)) ;contents of the output
810 (d (build-expression->derivation
811 s "substitute-me" `(begin ,c (exit 1)) ;would fail
812 #:outputs '("out" "one" "two")
813 #:guile-for-build
814 (package-derivation s %bootstrap-guile (%current-system))))
815 (o (derivation->output-path d)))
816 (with-derivation-substitute d c
817 (set-build-options s #:use-substitutes? #t
818 #:substitute-urls (%test-substitute-urls))
819 (and (has-substitutes? s o)
820
821 ;; Ask for nothing but the "out" output of D.
822 (build-things s `((,(derivation-file-name d) . "out")))
823
824 (valid-path? s o)
825 (canonical-file? o)
826 (equal? c (call-with-input-file o get-string-all)))))))
827
828 (test-assert "substitute, corrupt output hash"
829 ;; Tweak the substituter into installing a substitute whose hash doesn't
830 ;; match the one announced in the narinfo. The daemon must notice this and
831 ;; raise an error.
832 (with-store s
833 (let* ((c "hello, world") ; contents of the output
834 (d (build-expression->derivation
835 s "corrupt-substitute"
836 `(mkdir %output)
837 #:guile-for-build
838 (package-derivation s %bootstrap-guile (%current-system))))
839 (o (derivation->output-path d)))
840 (with-derivation-substitute d c
841 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
842
843 ;; Make sure we use 'guix substitute'.
844 (set-build-options s
845 #:use-substitutes? #t
846 #:fallback? #f
847 #:substitute-urls (%test-substitute-urls))
848 (and (has-substitutes? s o)
849 (guard (c ((store-protocol-error? c)
850 ;; XXX: the daemon writes "hash mismatch in downloaded
851 ;; path", but the actual error returned to the client
852 ;; doesn't mention that.
853 (pk 'corrupt c)
854 (not (zero? (store-protocol-error-status c)))))
855 (build-derivations s (list d))
856 #f))))))
857
858 (test-assert "substitute, corrupt output hash, build trace"
859 ;; Likewise, and check the build trace.
860 (with-store s
861 (let* ((c "hello, world") ; contents of the output
862 (d (build-expression->derivation
863 s "corrupt-substitute"
864 `(mkdir %output)
865 #:guile-for-build
866 (package-derivation s %bootstrap-guile (%current-system))))
867 (o (derivation->output-path d)))
868 ;; Make sure we use 'guix substitute'.
869 (set-build-options s
870 #:print-build-trace #t
871 #:use-substitutes? #t
872 #:fallback? #f
873 #:substitute-urls (%test-substitute-urls))
874
875 (with-derivation-substitute d c
876 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
877
878 (define output
879 (call-with-output-string
880 (lambda (port)
881 (parameterize ((current-build-output-port port))
882 (guard (c ((store-protocol-error? c) #t))
883 (build-derivations s (list d))
884 #f)))))
885
886 (define actual-hash
887 (let-values (((port get-hash)
888 (gcrypt:open-hash-port
889 (gcrypt:hash-algorithm gcrypt:sha256))))
890 (write-file-tree "foo" port
891 #:file-type+size
892 (lambda _
893 (values 'regular (string-length c)))
894 #:file-port
895 (lambda _
896 (open-input-string c)))
897 (close-port port)
898 (bytevector->nix-base32-string (get-hash))))
899
900 (define expected-hash
901 (bytevector->nix-base32-string (make-bytevector 32 0)))
902
903 (define mismatch
904 (string-append "@ hash-mismatch " o " sha256 "
905 expected-hash " " actual-hash "\n"))
906
907 (define failure
908 (string-append "@ substituter-failed " o))
909
910 (and (string-contains output mismatch)
911 (string-contains output failure))))))
912
913 (test-assert "substitute --fallback"
914 (with-store s
915 (let* ((t (random-text)) ; contents of the output
916 (d (build-expression->derivation
917 s "substitute-me-not"
918 `(call-with-output-file %output
919 (lambda (p)
920 (display ,t p)))
921 #:guile-for-build
922 (package-derivation s %bootstrap-guile (%current-system))))
923 (o (derivation->output-path d)))
924 ;; Create fake substituter data, to be read by 'guix substitute'.
925 (with-derivation-narinfo d
926 ;; Make sure we use 'guix substitute'.
927 (set-build-options s #:use-substitutes? #t
928 #:substitute-urls (%test-substitute-urls))
929 (and (has-substitutes? s o)
930 (guard (c ((store-protocol-error? c)
931 ;; The substituter failed as expected. Now make
932 ;; sure that #:fallback? #t works correctly.
933 (set-build-options s
934 #:use-substitutes? #t
935 #:substitute-urls
936 (%test-substitute-urls)
937 #:fallback? #t)
938 (and (build-derivations s (list d))
939 (equal? t (call-with-input-file o
940 get-string-all)))))
941 ;; Should fail.
942 (build-derivations s (list d))
943 #f))))))
944
945 (test-equal "substitute query and large size"
946 (+ 100 (expt 2 63)) ;<https://issues.guix.gnu.org/51983>
947 (with-store s
948 (let* ((size (+ 100 (expt 2 63))) ;does not fit in signed 'long long'
949 (item (string-append (%store-prefix)
950 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size")))
951 ;; Create fake substituter data, to be read by 'guix substitute'.
952 (call-with-output-file (string-append (%substitute-directory)
953 "/" (store-path-hash-part item)
954 ".narinfo")
955 (lambda (port)
956 (format port "StorePath: ~a
957 URL: http://example.org
958 Compression: none
959 NarSize: ~a
960 NarHash: sha256:0fj9vhblff2997pi7qjj7lhmy7wzhnjwmkm2hmq6gr4fzmg10s0w
961 References:
962 System: x86_64-linux~%"
963 item size)))
964
965 ;; Remove entry from the local cache.
966 (false-if-exception
967 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
968 "/guix/substitute")))
969
970 ;; Make sure 'guix substitute' correctly communicates the above
971 ;; data.
972 (set-build-options s #:use-substitutes? #t
973 #:substitute-urls (%test-substitute-urls))
974 (match (pk 'spi (substitutable-path-info s (list item)))
975 (((? substitutable? s))
976 (and (equal? (substitutable-path s) item)
977 (substitutable-nar-size s)))))))
978
979 (test-equal "substitute and large size"
980 (+ 100 (expt 2 31)) ;<https://issues.guix.gnu.org/46212>
981 (with-store s
982 (let* ((size (+ 100 (expt 2 31))) ;does not fit in signed 'int'
983 (item (string-append (%store-prefix)
984 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size-"
985 (random-text)))
986 (nar (string-append (%substitute-directory) "/nar")))
987 ;; Create a dummy nar to allow for substitution.
988 (call-with-output-file nar
989 (lambda (port)
990 (write-file-tree (store-path-package-name item) port
991 #:file-type+size (lambda _
992 (values 'regular 12))
993 #:file-port (lambda _
994 (open-input-string "Hello world.")))))
995
996 ;; Create fake substituter data, to be read by 'guix substitute'.
997 (call-with-output-file (string-append (%substitute-directory)
998 "/" (store-path-hash-part item)
999 ".narinfo")
1000 (lambda (port)
1001 (format port "StorePath: ~a
1002 URL: file://~a
1003 Compression: none
1004 NarSize: ~a
1005 NarHash: sha256:~a
1006 References:
1007 System: x86_64-linux~%"
1008 item nar size
1009 (bytevector->nix-base32-string (gcrypt:file-sha256 nar)))))
1010
1011 ;; Remove entry from the local cache.
1012 (false-if-exception
1013 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
1014 "/guix/substitute")))
1015
1016 ;; Make sure 'guix substitute' correctly communicates the above
1017 ;; data.
1018 (set-build-options s #:use-substitutes? #t
1019 #:substitute-urls (%test-substitute-urls))
1020 (ensure-path s item)
1021 (path-info-nar-size (query-path-info s item)))))
1022
1023 (test-assert "export/import several paths"
1024 (let* ((texts (unfold (cut >= <> 10)
1025 (lambda _ (random-text))
1026 1+
1027 0))
1028 (files (map (cut add-text-to-store %store "text" <>) texts))
1029 (dump (call-with-bytevector-output-port
1030 (cut export-paths %store files <>))))
1031 (delete-paths %store files)
1032 (and (every (negate file-exists?) files)
1033 (let* ((source (open-bytevector-input-port dump))
1034 (imported (import-paths %store source)))
1035 (and (equal? imported files)
1036 (every file-exists? files)
1037 (equal? texts
1038 (map (lambda (file)
1039 (call-with-input-file file
1040 get-string-all))
1041 files)))))))
1042
1043 (test-assert "export/import paths, ensure topological order"
1044 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1045 (file1 (add-text-to-store %store "foo" (random-text)
1046 (list file0)))
1047 (file2 (add-text-to-store %store "bar" (random-text)
1048 (list file1)))
1049 (files (list file1 file2))
1050 (dump1 (call-with-bytevector-output-port
1051 (cute export-paths %store (list file1 file2) <>)))
1052 (dump2 (call-with-bytevector-output-port
1053 (cute export-paths %store (list file2 file1) <>))))
1054 (delete-paths %store files)
1055 (and (every (negate file-exists?) files)
1056 (bytevector=? dump1 dump2)
1057 (let* ((source (open-bytevector-input-port dump1))
1058 (imported (import-paths %store source)))
1059 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
1060 (and (equal? imported (list file1 file2))
1061 (every file-exists? files)
1062 (equal? (list file0) (references %store file1))
1063 (equal? (list file1) (references %store file2)))))))
1064
1065 (test-assert "export/import incomplete"
1066 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1067 (file1 (add-text-to-store %store "foo" (random-text)
1068 (list file0)))
1069 (file2 (add-text-to-store %store "bar" (random-text)
1070 (list file1)))
1071 (dump (call-with-bytevector-output-port
1072 (cute export-paths %store (list file2) <>))))
1073 (delete-paths %store (list file0 file1 file2))
1074 (guard (c ((store-protocol-error? c)
1075 (and (not (zero? (store-protocol-error-status c)))
1076 (string-contains (store-protocol-error-message c)
1077 "not valid"))))
1078 ;; Here we get an exception because DUMP does not include FILE0 and
1079 ;; FILE1, which are dependencies of FILE2.
1080 (import-paths %store (open-bytevector-input-port dump)))))
1081
1082 (test-assert "export/import recursive"
1083 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1084 (file1 (add-text-to-store %store "foo" (random-text)
1085 (list file0)))
1086 (file2 (add-text-to-store %store "bar" (random-text)
1087 (list file1)))
1088 (dump (call-with-bytevector-output-port
1089 (cute export-paths %store (list file2) <>
1090 #:recursive? #t))))
1091 (delete-paths %store (list file0 file1 file2))
1092 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
1093 (and (equal? imported (list file0 file1 file2))
1094 (every file-exists? (list file0 file1 file2))
1095 (equal? (list file0) (references %store file1))
1096 (equal? (list file1) (references %store file2))))))
1097
1098 (test-assert "write-file & export-path yield the same result"
1099 ;; Here we compare 'write-file' and the daemon's own implementation.
1100 ;; 'write-file' is the reference because we know it sorts file
1101 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
1102 ;; currently happen to be sorted as a side-effect of some unrelated
1103 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
1104 ;; changes there.
1105 (run-with-store %store
1106 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
1107 (out1 -> (derivation->output-path drv1))
1108 (data -> (unfold (cut >= <> 26)
1109 (lambda (i)
1110 (random-bytevector 128))
1111 1+ 0))
1112 (build
1113 -> #~(begin
1114 (use-modules (rnrs io ports) (srfi srfi-1))
1115 (let ()
1116 (define letters
1117 (map (lambda (i)
1118 (string
1119 (integer->char
1120 (+ i (char->integer #\a)))))
1121 (iota 26)))
1122 (define (touch file data)
1123 (call-with-output-file file
1124 (lambda (port)
1125 (put-bytevector port data))))
1126
1127 (mkdir #$output)
1128 (chdir #$output)
1129
1130 ;; The files must be different so they have
1131 ;; different inode numbers, and the inode
1132 ;; order must differ from the lexicographic
1133 ;; order.
1134 (for-each touch
1135 (append (drop letters 10)
1136 (take letters 10))
1137 (list #$@data))
1138 #t)))
1139 (drv2 (gexp->derivation "bunch" build))
1140 (out2 -> (derivation->output-path drv2))
1141 (item-info -> (store-lift query-path-info)))
1142 (mbegin %store-monad
1143 (built-derivations (list drv1 drv2))
1144 (foldm %store-monad
1145 (lambda (item result)
1146 (define ref-hash
1147 (let-values (((port get) (gcrypt:open-sha256-port)))
1148 (write-file item port)
1149 (close-port port)
1150 (get)))
1151
1152 ;; 'query-path-info' returns a hash produced by using the
1153 ;; daemon's C++ 'dump' function, which is the implementation
1154 ;; under test.
1155 (>>= (item-info item)
1156 (lambda (info)
1157 (return
1158 (and result
1159 (bytevector=? (path-info-hash info) ref-hash))))))
1160 #t
1161 (list out1 out2))))
1162 #:guile-for-build (%guile-for-build)))
1163
1164
1165 (test-assert "import not signed"
1166 (let* ((text (random-text))
1167 (file (add-file-tree-to-store %store
1168 `("tree" directory
1169 ("text" regular (data ,text))
1170 ("link" symlink "text"))))
1171 (dump (call-with-bytevector-output-port
1172 (lambda (port)
1173 (write-int 1 port) ;start
1174
1175 (write-file file port) ;contents
1176 (write-int #x4558494e port) ;%export-magic
1177 (write-string file port) ;store item
1178 (write-string-list '() port) ;references
1179 (write-string "" port) ;deriver
1180 (write-int 0 port) ;not signed
1181
1182 (write-int 0 port))))) ;done
1183
1184 ;; Ensure 'import-paths' raises an exception.
1185 (guard (c ((store-protocol-error? c)
1186 (and (not (zero? (store-protocol-error-status c)))
1187 (string-contains (store-protocol-error-message c)
1188 "lacks a signature"))))
1189 (let* ((source (open-bytevector-input-port dump))
1190 (imported (import-paths %store source)))
1191 (pk 'unsigned-imported imported)
1192 #f))))
1193
1194 (test-assert "import signed by unauthorized key"
1195 (let* ((text (random-text))
1196 (file (add-file-tree-to-store %store
1197 `("tree" directory
1198 ("text" regular (data ,text))
1199 ("link" symlink "text"))))
1200 (key (gcrypt:generate-key
1201 (gcrypt:string->canonical-sexp
1202 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1203 (dump (call-with-bytevector-output-port
1204 (lambda (port)
1205 (write-int 1 port) ;start
1206
1207 (write-file file port) ;contents
1208 (write-int #x4558494e port) ;%export-magic
1209 (write-string file port) ;store item
1210 (write-string-list '() port) ;references
1211 (write-string "" port) ;deriver
1212 (write-int 1 port) ;signed
1213 (write-string (gcrypt:canonical-sexp->string
1214 (signature-sexp
1215 (gcrypt:bytevector->hash-data
1216 (gcrypt:sha256 #vu8(0 1 2))
1217 #:key-type 'ecc)
1218 (gcrypt:find-sexp-token key 'private-key)
1219 (gcrypt:find-sexp-token key 'public-key)))
1220 port)
1221
1222 (write-int 0 port))))) ;done
1223
1224 ;; Ensure 'import-paths' raises an exception.
1225 (guard (c ((store-protocol-error? c)
1226 (and (not (zero? (store-protocol-error-status c)))
1227 (string-contains (store-protocol-error-message c)
1228 "unauthorized public key"))))
1229 (let* ((source (open-bytevector-input-port dump))
1230 (imported (import-paths %store source)))
1231 (pk 'unauthorized-imported imported)
1232 #f))))
1233
1234 (test-assert "import corrupt path"
1235 (let* ((text (random-text))
1236 (file (add-text-to-store %store "text" text))
1237 (dump (call-with-bytevector-output-port
1238 (cut export-paths %store (list file) <>))))
1239 (delete-paths %store (list file))
1240
1241 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1242 ;; the file contents in DUMP, regardless of the store prefix.
1243 (let* ((index #x70)
1244 (byte (bytevector-u8-ref dump index)))
1245 (bytevector-u8-set! dump index (logxor #xff byte)))
1246
1247 (and (not (file-exists? file))
1248 (guard (c ((store-protocol-error? c)
1249 (pk 'c c)
1250 (and (not (zero? (store-protocol-error-status c)))
1251 (string-contains (store-protocol-error-message c)
1252 "corrupt"))))
1253 (let* ((source (open-bytevector-input-port dump))
1254 (imported (import-paths %store source)))
1255 (pk 'corrupt-imported imported)
1256 #f)))))
1257
1258 (test-assert "verify-store"
1259 (let* ((text (random-text))
1260 (file1 (add-text-to-store %store "foo" text))
1261 (file2 (add-text-to-store %store "bar" (random-text)
1262 (list file1))))
1263 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1264 (begin
1265 (delete-file file1)
1266 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1267 (begin
1268 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1269 ;; without actually creating the file. ;
1270 (call-with-output-file file1
1271 (lambda (port)
1272 (display text port)))
1273 (pk 'verify3 (verify-store %store)))))) ;OK again
1274
1275 (test-assert "verify-store + check-contents"
1276 ;; XXX: This test is I/O intensive.
1277 (with-store s
1278 (let* ((text (random-text))
1279 (drv (build-expression->derivation
1280 s "corrupt"
1281 `(let ((out (assoc-ref %outputs "out")))
1282 (call-with-output-file out
1283 (lambda (port)
1284 (display ,text port)))
1285 #t)
1286 #:guile-for-build
1287 (package-derivation s %bootstrap-guile (%current-system))))
1288 (file (derivation->output-path drv)))
1289 (with-derivation-substitute drv text
1290 (and (build-derivations s (list drv))
1291 (verify-store s #:check-contents? #t) ;should be OK
1292 (begin
1293 (chmod file #o644)
1294 (call-with-output-file file
1295 (lambda (port)
1296 (display "corrupt!" port)))
1297 #t)
1298
1299 ;; Make sure the corruption is detected. We don't test repairing
1300 ;; because only "trusted" users are allowed to do it, but we
1301 ;; don't expose that notion of trusted users that nix-daemon
1302 ;; supports because it seems dubious and redundant with what the
1303 ;; OS provides (in Nix "trusted" users have additional
1304 ;; privileges, such as overriding the set of substitute URLs, but
1305 ;; we instead want to allow anyone to modify them, provided
1306 ;; substitutes are signed by a root-approved key.)
1307 (not (verify-store s #:check-contents? #t))
1308
1309 ;; Delete the corrupt item to leave the store in a clean state.
1310 (delete-paths s (list file)))))))
1311
1312 (test-assert "build-things, check mode"
1313 (with-store store
1314 (call-with-temporary-output-file
1315 (lambda (entropy entropy-port)
1316 (write (random-text) entropy-port)
1317 (force-output entropy-port)
1318 (let* ((drv (build-expression->derivation
1319 store "non-deterministic"
1320 `(begin
1321 (use-modules (rnrs io ports))
1322 (let ((out (assoc-ref %outputs "out")))
1323 (call-with-output-file out
1324 (lambda (port)
1325 ;; Rely on the fact that tests do not use the
1326 ;; chroot, and thus ENTROPY is readable.
1327 (display (call-with-input-file ,entropy
1328 get-string-all)
1329 port)))
1330 #t))
1331 #:guile-for-build
1332 (package-derivation store %bootstrap-guile (%current-system))))
1333 (file (derivation->output-path drv)))
1334 (and (build-things store (list (derivation-file-name drv)))
1335 (begin
1336 (write (random-text) entropy-port)
1337 (force-output entropy-port)
1338 (guard (c ((store-protocol-error? c)
1339 (pk 'determinism-exception c)
1340 (and (not (zero? (store-protocol-error-status c)))
1341 (string-contains (store-protocol-error-message c)
1342 "deterministic"))))
1343 ;; This one will produce a different result. Since we're in
1344 ;; 'check' mode, this must fail.
1345 (build-things store (list (derivation-file-name drv))
1346 (build-mode check))
1347 #f))))))))
1348
1349 (test-assert "build-succeeded trace in check mode"
1350 (string-contains
1351 (call-with-output-string
1352 (lambda (port)
1353 (let ((d (build-expression->derivation
1354 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1355 #:guile-for-build
1356 (package-derivation %store %bootstrap-guile))))
1357 (build-derivations %store (list d))
1358 (parameterize ((current-build-output-port port))
1359 (build-derivations %store (list d) (build-mode check))))))
1360 "@ build-succeeded"))
1361
1362 (test-assert "build multiple times"
1363 (with-store store
1364 ;; Ask to build twice.
1365 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1366
1367 (call-with-temporary-output-file
1368 (lambda (entropy entropy-port)
1369 (write (random-text) entropy-port)
1370 (force-output entropy-port)
1371 (let* ((drv (build-expression->derivation
1372 store "non-deterministic"
1373 `(begin
1374 (use-modules (rnrs io ports))
1375 (let ((out (assoc-ref %outputs "out")))
1376 (call-with-output-file out
1377 (lambda (port)
1378 ;; Rely on the fact that tests do not use the
1379 ;; chroot, and thus ENTROPY is accessible.
1380 (display (call-with-input-file ,entropy
1381 get-string-all)
1382 port)
1383 (call-with-output-file ,entropy
1384 (lambda (port)
1385 (write 'foobar port)))))
1386 #t))
1387 #:guile-for-build
1388 (package-derivation store %bootstrap-guile (%current-system))))
1389 (file (derivation->output-path drv)))
1390 (guard (c ((store-protocol-error? c)
1391 (pk 'multiple-build c)
1392 (and (not (zero? (store-protocol-error-status c)))
1393 (string-contains (store-protocol-error-message c)
1394 "deterministic"))))
1395 ;; This one will produce a different result on the second run.
1396 (current-build-output-port (current-error-port))
1397 (build-things store (list (derivation-file-name drv)))
1398 #f))))))
1399
1400 (test-equal "store-lower"
1401 "Lowered."
1402 (let* ((add (store-lower text-file))
1403 (file (add %store "foo" "Lowered.")))
1404 (call-with-input-file file get-string-all)))
1405
1406 (test-equal "current-system"
1407 "bar"
1408 (parameterize ((%current-system "frob"))
1409 (run-with-store %store
1410 (mbegin %store-monad
1411 (set-current-system "bar")
1412 (current-system))
1413 #:system "foo")))
1414
1415 (test-assert "query-path-info"
1416 (let* ((ref (add-text-to-store %store "ref" "foo"))
1417 (item (add-text-to-store %store "item" "bar" (list ref)))
1418 (info (query-path-info %store item)))
1419 (and (equal? (path-info-references info) (list ref))
1420 (equal? (path-info-hash info)
1421 (gcrypt:sha256
1422 (string->utf8
1423 (call-with-output-string (cut write-file item <>))))))))
1424
1425 (test-assert "path-info-deriver"
1426 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1427 (s (add-to-store %store "bash" #t "sha256"
1428 (search-bootstrap-binary "bash"
1429 (%current-system))))
1430 (d (derivation %store "the-thing"
1431 s `("-e" ,b)
1432 #:env-vars `(("foo" . ,(random-text)))
1433 #:inputs `((,b) (,s))))
1434 (o (derivation->output-path d)))
1435 (and (build-derivations %store (list d))
1436 (not (path-info-deriver (query-path-info %store b)))
1437 (string=? (derivation-file-name d)
1438 (path-info-deriver (query-path-info %store o))))))
1439
1440 (test-equal "build-cores"
1441 (list 0 42)
1442 (with-store store
1443 (let* ((build (add-text-to-store store "build.sh"
1444 "echo $NIX_BUILD_CORES > $out"))
1445 (bash (add-to-store store "bash" #t "sha256"
1446 (search-bootstrap-binary "bash"
1447 (%current-system))))
1448 (drv1 (derivation store "the-thing" bash
1449 `("-e" ,build)
1450 #:inputs `((,bash) (,build))
1451 #:env-vars `(("x" . ,(random-text)))))
1452 (drv2 (derivation store "the-thing" bash
1453 `("-e" ,build)
1454 #:inputs `((,bash) (,build))
1455 #:env-vars `(("x" . ,(random-text))))))
1456 (and (build-derivations store (list drv1))
1457 (begin
1458 (set-build-options store #:build-cores 42)
1459 (build-derivations store (list drv2)))
1460 (list (call-with-input-file (derivation->output-path drv1)
1461 read)
1462 (call-with-input-file (derivation->output-path drv2)
1463 read))))))
1464
1465 (test-equal "multiplexed-build-output"
1466 '("Hello from first." "Hello from second.")
1467 (with-store store
1468 (let* ((build (add-text-to-store store "build.sh"
1469 "echo Hello from $NAME.; echo > $out"))
1470 (bash (add-to-store store "bash" #t "sha256"
1471 (search-bootstrap-binary "bash"
1472 (%current-system))))
1473 (drv1 (derivation store "one" bash
1474 `("-e" ,build)
1475 #:inputs `((,bash) (,build))
1476 #:env-vars `(("NAME" . "first")
1477 ("x" . ,(random-text)))))
1478 (drv2 (derivation store "two" bash
1479 `("-e" ,build)
1480 #:inputs `((,bash) (,build))
1481 #:env-vars `(("NAME" . "second")
1482 ("x" . ,(random-text))))))
1483 (set-build-options store
1484 #:print-build-trace #t
1485 #:multiplexed-build-output? #t
1486 #:max-build-jobs 10)
1487 (let ((port (open-output-string)))
1488 ;; Send the build log to PORT.
1489 (parameterize ((current-build-output-port port))
1490 (build-derivations store (list drv1 drv2)))
1491
1492 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1493 ;; traces that allow us to retrieve each builder's output (we assume
1494 ;; there's exactly one "build-output" trace for each builder, which is
1495 ;; reasonable.)
1496 (let* ((log (get-output-string port))
1497 (started (fold-matches
1498 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1499 log '() cons))
1500 (done (fold-matches
1501 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1502 log '() cons))
1503 (output (fold-matches
1504 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1505 log '() cons))
1506 (drv-pid (lambda (name)
1507 (lambda (m)
1508 (let ((drv (match:substring m 1))
1509 (pid (string->number
1510 (match:substring m 4))))
1511 (and (string-suffix? name drv) pid)))))
1512 (pid-log (lambda (pid)
1513 (lambda (m)
1514 (let ((n (string->number
1515 (match:substring m 1)))
1516 (len (string->number
1517 (match:substring m 2)))
1518 (str (match:substring m 3)))
1519 (and (= pid n)
1520 (= (string-length str) (- len 1))
1521 str)))))
1522 (pid1 (any (drv-pid "one.drv") started))
1523 (pid2 (any (drv-pid "two.drv") started)))
1524 (list (any (pid-log pid1) output)
1525 (any (pid-log pid2) output)))))))
1526
1527 (test-end "store")