daemon: Make 'profiles/per-user' non-world-writable.
[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 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)
26 #:use-module (guix base32)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29 #:use-module (guix serialization)
30 #:use-module (guix build utils)
31 #:use-module (guix gexp)
32 #:use-module (gnu packages)
33 #:use-module (gnu packages bootstrap)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (rnrs bytevectors)
37 #:use-module (rnrs io ports)
38 #:use-module (web uri)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-64))
44
45 ;; Test the (guix store) module.
46
47 (define %store
48 (open-connection-for-tests))
49
50 (define %shell
51 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
52
53 \f
54 (test-begin "store")
55
56 (test-assert "open-connection with file:// URI"
57 (let ((store (open-connection (string-append "file://"
58 (%daemon-socket-uri)))))
59 (and (add-text-to-store store "foo" "bar")
60 (begin
61 (close-connection store)
62 #t))))
63
64 (test-equal "connection handshake error"
65 EPROTO
66 (let ((port (%make-void-port "rw")))
67 (guard (c ((store-connection-error? c)
68 (and (eq? port (store-connection-error-file c))
69 (store-connection-error-code c))))
70 (open-connection #f #:port port)
71 'broken)))
72
73 (test-equal "store-path-hash-part"
74 "283gqy39v3g9dxjy26rynl0zls82fmcg"
75 (store-path-hash-part
76 (string-append (%store-prefix)
77 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
78
79 (test-equal "store-path-hash-part #f"
80 #f
81 (store-path-hash-part
82 (string-append (%store-prefix)
83 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
84
85 (test-equal "store-path-package-name"
86 "guile-2.0.7"
87 (store-path-package-name
88 (string-append (%store-prefix)
89 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
90
91 (test-equal "store-path-package-name #f"
92 #f
93 (store-path-package-name
94 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
95
96 (test-assert "direct-store-path?"
97 (and (direct-store-path?
98 (string-append (%store-prefix)
99 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
100 (not (direct-store-path?
101 (string-append
102 (%store-prefix)
103 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
104 (not (direct-store-path? (%store-prefix)))))
105
106 (test-skip (if %store 0 15))
107
108 (test-equal "profiles/per-user exists and is not writable"
109 #o755
110 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
111
112 (test-equal "profiles/per-user/$USER exists"
113 (list (getuid) #o755)
114 (let ((s (stat (string-append %state-directory "/profiles/per-user/"
115 (passwd:name (getpwuid (getuid)))))))
116 (list (stat:uid s) (stat:perms s))))
117
118 (test-equal "add-data-to-store"
119 #vu8(1 2 3 4 5)
120 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
121 get-bytevector-all))
122
123 (test-assert "valid-path? live"
124 (let ((p (add-text-to-store %store "hello" "hello, world")))
125 (valid-path? %store p)))
126
127 (test-assert "valid-path? false"
128 (not (valid-path? %store
129 (string-append (%store-prefix) "/"
130 (make-string 32 #\e) "-foobar"))))
131
132 (test-assert "valid-path? error"
133 (with-store s
134 (guard (c ((store-protocol-error? c) #t))
135 (valid-path? s "foo")
136 #f)))
137
138 (test-assert "valid-path? recovery"
139 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
140 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
141 ;; file name. See
142 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
143 ;; details.
144 (with-store s
145 (let-syntax ((true-if-error (syntax-rules ()
146 ((_ exp)
147 (guard (c ((store-protocol-error? c) #t))
148 exp #f)))))
149 (and (true-if-error (valid-path? s "foo"))
150 (true-if-error (valid-path? s "bar"))
151 (true-if-error (valid-path? s "baz"))
152 (true-if-error (valid-path? s "chbouib"))
153 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
154
155 (test-assert "hash-part->path"
156 (let ((p (add-text-to-store %store "hello" "hello, world")))
157 (equal? (hash-part->path %store (store-path-hash-part p))
158 p)))
159
160 (test-assert "dead-paths"
161 (let ((p (add-text-to-store %store "random-text" (random-text))))
162 (->bool (member p (dead-paths %store)))))
163
164 ;; FIXME: Find a test for `live-paths'.
165 ;;
166 ;; (test-assert "temporary root is in live-paths"
167 ;; (let* ((p1 (add-text-to-store %store "random-text"
168 ;; (random-text) '()))
169 ;; (b (add-text-to-store %store "link-builder"
170 ;; (format #f "echo ~a > $out" p1)
171 ;; '()))
172 ;; (d1 (derivation %store "link"
173 ;; "/bin/sh" `("-e" ,b)
174 ;; #:inputs `((,b) (,p1))))
175 ;; (p2 (derivation->output-path d1)))
176 ;; (and (add-temp-root %store p2)
177 ;; (build-derivations %store (list d1))
178 ;; (valid-path? %store p1)
179 ;; (member (pk p2) (live-paths %store)))))
180
181 (test-assert "permanent root"
182 (let* ((p (with-store store
183 (let ((p (add-text-to-store store "random-text"
184 (random-text))))
185 (add-permanent-root p)
186 (add-permanent-root p) ; should not throw
187 p))))
188 (and (member p (live-paths %store))
189 (begin
190 (remove-permanent-root p)
191 (->bool (member p (dead-paths %store)))))))
192
193 (test-assert "dead path can be explicitly collected"
194 (let ((p (add-text-to-store %store "random-text"
195 (random-text) '())))
196 (let-values (((paths freed) (delete-paths %store (list p))))
197 (and (equal? paths (list p))
198 ;; XXX: On some file systems (notably Btrfs), freed
199 ;; may return 0. See <https://bugs.gnu.org/29363>.
200 ;;(> freed 0)
201 (not (file-exists? p))))))
202
203 (test-assert "add-text-to-store vs. delete-paths"
204 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
205 ;; is no longer valid.
206 (with-store store
207 (let* ((text (random-text))
208 (path (add-text-to-store store "delete-me" text))
209 (deleted (delete-paths store (list path)))
210 (path2 (add-text-to-store store "delete-me" text)))
211 (and (string=? path path2)
212 (equal? deleted (list path))
213 (valid-path? store path)
214 (file-exists? path)))))
215
216 (test-assert "add-to-store vs. delete-paths"
217 ;; Same as above.
218 (with-store store
219 (let* ((file (search-path %load-path "guix.scm"))
220 (path (add-to-store store "delete-me" #t "sha256" file))
221 (deleted (delete-paths store (list path)))
222 (path2 (add-to-store store "delete-me" #t "sha256" file)))
223 (and (string=? path path2)
224 (equal? deleted (list path))
225 (valid-path? store path)
226 (file-exists? path)))))
227
228 (test-equal "add-file-tree-to-store"
229 `(42
230 ("." directory #t)
231 ("./bar" directory #t)
232 ("./foo" directory #t)
233 ("./foo/a" regular "file a")
234 ("./foo/b" symlink "a")
235 ("./foo/c" directory #t)
236 ("./foo/c/p" regular "file p")
237 ("./foo/c/q" directory #t)
238 ("./foo/c/q/x" regular
239 ,(string-append "#!" %shell "\nexit 42"))
240 ("./foo/c/q/y" symlink "..")
241 ("./foo/c/q/z" directory #t))
242 (let* ((tree `("file-tree" directory
243 ("foo" directory
244 ("a" regular (data "file a"))
245 ("b" symlink "a")
246 ("c" directory
247 ("p" regular (data ,(string->utf8 "file p")))
248 ("q" directory
249 ("x" executable
250 (data ,(string-append "#!" %shell "\nexit 42")))
251 ("y" symlink "..")
252 ("z" directory))))
253 ("bar" directory)))
254 (result (add-file-tree-to-store %store tree)))
255 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
256 (with-directory-excursion result
257 (map (lambda (file)
258 (let ((type (stat:type (lstat file))))
259 `(,file ,type
260 ,(match type
261 ((or 'regular 'executable)
262 (call-with-input-file file
263 get-string-all))
264 ('symlink (readlink file))
265 ('directory #t)))))
266 (find-files "." #:directories? #t))))))
267
268 (test-equal "add-file-tree-to-store, flat"
269 "Hello, world!"
270 (let* ((tree `("flat-file" regular (data "Hello, world!")))
271 (result (add-file-tree-to-store %store tree)))
272 (and (file-exists? result)
273 (call-with-input-file result get-string-all))))
274
275 (test-assert "references"
276 (let* ((t1 (add-text-to-store %store "random1"
277 (random-text)))
278 (t2 (add-text-to-store %store "random2"
279 (random-text) (list t1))))
280 (and (equal? (list t1) (references %store t2))
281 (equal? (list t2) (referrers %store t1))
282 (null? (references %store t1))
283 (null? (referrers %store t2)))))
284
285 (test-assert "references/substitutes missing reference info"
286 (with-store s
287 (set-build-options s #:use-substitutes? #f)
288 (guard (c ((store-protocol-error? c) #t))
289 (let* ((b (add-to-store s "bash" #t "sha256"
290 (search-bootstrap-binary "bash"
291 (%current-system))))
292 (d (derivation s "the-thing" b '("--help")
293 #:inputs `((,b)))))
294 (references/substitutes s (list (derivation->output-path d) b))
295 #f))))
296
297 (test-assert "references/substitutes with substitute info"
298 (with-store s
299 (set-build-options s #:use-substitutes? #t)
300 (let* ((t1 (add-text-to-store s "random1" (random-text)))
301 (t2 (add-text-to-store s "random2" (random-text)
302 (list t1)))
303 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
304 (b (add-to-store s "bash" #t "sha256"
305 (search-bootstrap-binary "bash"
306 (%current-system))))
307 (d (derivation s "the-thing" b `("-e" ,t3)
308 #:inputs `((,b) (,t3) (,t2))
309 #:env-vars `(("t2" . ,t2))))
310 (o (derivation->output-path d)))
311 (with-derivation-narinfo d
312 (sha256 => (sha256 (string->utf8 t2)))
313 (references => (list t2))
314
315 (equal? (references/substitutes s (list o t3 t2 t1))
316 `((,t2) ;refs of O
317 () ;refs of T3
318 (,t1) ;refs of T2
319 ())))))) ;refs of T1
320
321 (test-equal "substitutable-path-info when substitutes are turned off"
322 '()
323 (with-store s
324 (set-build-options s #:use-substitutes? #f)
325 (let* ((b (add-to-store s "bash" #t "sha256"
326 (search-bootstrap-binary "bash"
327 (%current-system))))
328 (d (derivation s "the-thing" b '("--version")
329 #:inputs `((,b))))
330 (o (derivation->output-path d)))
331 (with-derivation-narinfo d
332 (substitutable-path-info s (list o))))))
333
334 (test-equal "substitutable-paths when substitutes are turned off"
335 '()
336 (with-store s
337 (set-build-options s #:use-substitutes? #f)
338 (let* ((b (add-to-store s "bash" #t "sha256"
339 (search-bootstrap-binary "bash"
340 (%current-system))))
341 (d (derivation s "the-thing" b '("--version")
342 #:inputs `((,b))))
343 (o (derivation->output-path d)))
344 (with-derivation-narinfo d
345 (substitutable-paths s (list o))))))
346
347 (test-assert "requisites"
348 (let* ((t1 (add-text-to-store %store "random1"
349 (random-text) '()))
350 (t2 (add-text-to-store %store "random2"
351 (random-text) (list t1)))
352 (t3 (add-text-to-store %store "random3"
353 (random-text) (list t2)))
354 (t4 (add-text-to-store %store "random4"
355 (random-text) (list t1 t3))))
356 (define (same? x y)
357 (and (= (length x) (length y))
358 (lset= equal? x y)))
359
360 (and (same? (requisites %store (list t1)) (list t1))
361 (same? (requisites %store (list t2)) (list t1 t2))
362 (same? (requisites %store (list t3)) (list t1 t2 t3))
363 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
364 (same? (requisites %store (list t1 t2 t3 t4))
365 (list t1 t2 t3 t4)))))
366
367 (test-assert "derivers"
368 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
369 (s (add-to-store %store "bash" #t "sha256"
370 (search-bootstrap-binary "bash"
371 (%current-system))))
372 (d (derivation %store "the-thing"
373 s `("-e" ,b)
374 #:env-vars `(("foo" . ,(random-text)))
375 #:inputs `((,b) (,s))))
376 (o (derivation->output-path d)))
377 (and (build-derivations %store (list d))
378 (equal? (query-derivation-outputs %store (derivation-file-name d))
379 (list o))
380 (equal? (valid-derivers %store o)
381 (list (derivation-file-name d))))))
382
383 (test-assert "topologically-sorted, one item"
384 (let* ((a (add-text-to-store %store "a" "a"))
385 (b (add-text-to-store %store "b" "b" (list a)))
386 (c (add-text-to-store %store "c" "c" (list b)))
387 (d (add-text-to-store %store "d" "d" (list c)))
388 (s (topologically-sorted %store (list d))))
389 (equal? s (list a b c d))))
390
391 (test-assert "topologically-sorted, several items"
392 (let* ((a (add-text-to-store %store "a" "a"))
393 (b (add-text-to-store %store "b" "b" (list a)))
394 (c (add-text-to-store %store "c" "c" (list b)))
395 (d (add-text-to-store %store "d" "d" (list c)))
396 (s1 (topologically-sorted %store (list d a c b)))
397 (s2 (topologically-sorted %store (list b d c a b d))))
398 (equal? s1 s2 (list a b c d))))
399
400 (test-assert "topologically-sorted, more difficult"
401 (let* ((a (add-text-to-store %store "a" "a"))
402 (b (add-text-to-store %store "b" "b" (list a)))
403 (c (add-text-to-store %store "c" "c" (list b)))
404 (d (add-text-to-store %store "d" "d" (list c)))
405 (w (add-text-to-store %store "w" "w"))
406 (x (add-text-to-store %store "x" "x" (list w)))
407 (y (add-text-to-store %store "y" "y" (list x d)))
408 (s1 (topologically-sorted %store (list y)))
409 (s2 (topologically-sorted %store (list c y)))
410 (s3 (topologically-sorted %store (cons y (references %store y)))))
411 ;; The order in which 'references' returns the references of Y is
412 ;; unspecified, so accommodate.
413 (let* ((x-then-d? (equal? (references %store y) (list x d))))
414 (and (equal? s1
415 (if x-then-d?
416 (list w x a b c d y)
417 (list a b c d w x y)))
418 (equal? s2
419 (if x-then-d?
420 (list a b c w x d y)
421 (list a b c d w x y)))
422 (lset= string=? s1 s3)))))
423
424 (test-assert "current-build-output-port, UTF-8"
425 ;; Are UTF-8 strings in the build log properly interpreted?
426 (string-contains
427 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
428 (call-with-output-string
429 (lambda (port)
430 (parameterize ((current-build-output-port port))
431 (let* ((s "Here’s a Greek letter: λ.")
432 (d (build-expression->derivation
433 %store "foo" `(display ,s)
434 #:guile-for-build
435 (package-derivation s %bootstrap-guile (%current-system)))))
436 (guard (c ((store-protocol-error? c) #t))
437 (build-derivations %store (list d))))))))
438 "Here’s a Greek letter: λ."))
439
440 (test-assert "current-build-output-port, UTF-8 + garbage"
441 ;; What about a mixture of UTF-8 + garbage?
442 (string-contains
443 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
444 (call-with-output-string
445 (lambda (port)
446 (parameterize ((current-build-output-port port))
447 (let ((d (build-expression->derivation
448 %store "foo"
449 `(begin
450 (use-modules (rnrs io ports))
451 (display "garbage: ")
452 (put-bytevector (current-output-port) #vu8(128))
453 (display "lambda: λ\n"))
454 #:guile-for-build
455 (package-derivation %store %bootstrap-guile))))
456 (guard (c ((store-protocol-error? c) #t))
457 (build-derivations %store (list d))))))))
458 "garbage: �lambda: λ"))
459
460 (test-assert "log-file, derivation"
461 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
462 (s (add-to-store %store "bash" #t "sha256"
463 (search-bootstrap-binary "bash"
464 (%current-system))))
465 (d (derivation %store "the-thing"
466 s `("-e" ,b)
467 #:env-vars `(("foo" . ,(random-text)))
468 #:inputs `((,b) (,s)))))
469 (and (build-derivations %store (list d))
470 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
471
472 (test-assert "log-file, output file name"
473 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
474 (s (add-to-store %store "bash" #t "sha256"
475 (search-bootstrap-binary "bash"
476 (%current-system))))
477 (d (derivation %store "the-thing"
478 s `("-e" ,b)
479 #:env-vars `(("foo" . ,(random-text)))
480 #:inputs `((,b) (,s))))
481 (o (derivation->output-path d)))
482 (and (build-derivations %store (list d))
483 (file-exists? (pk (log-file %store o)))
484 (string=? (log-file %store (derivation-file-name d))
485 (log-file %store o)))))
486
487 (test-assert "no substitutes"
488 (with-store s
489 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
490 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
491 (o (map derivation->output-path (list d1 d2))))
492 (set-build-options s #:use-substitutes? #f)
493 (and (not (has-substitutes? s (derivation-file-name d1)))
494 (not (has-substitutes? s (derivation-file-name d2)))
495 (null? (substitutable-paths s o))
496 (null? (substitutable-path-info s o))))))
497
498 (test-assert "build-things with output path"
499 (with-store s
500 (let* ((c (random-text)) ;contents of the output
501 (d (build-expression->derivation
502 s "substitute-me"
503 `(call-with-output-file %output
504 (lambda (p)
505 (display ,c p)))
506 #:guile-for-build
507 (package-derivation s %bootstrap-guile (%current-system))))
508 (o (derivation->output-path d)))
509 (set-build-options s #:use-substitutes? #f)
510
511 ;; Pass 'build-things' the output file name, O. However, since there
512 ;; are no substitutes for O, it will just do nothing.
513 (build-things s (list o))
514 (not (valid-path? s o)))))
515
516 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
517
518 (test-assert "substitute query"
519 (with-store s
520 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
521 (o (derivation->output-path d)))
522 ;; Create fake substituter data, to be read by 'guix substitute'.
523 (with-derivation-narinfo d
524 ;; Remove entry from the local cache.
525 (false-if-exception
526 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
527 "/guix/substitute")))
528
529 ;; Make sure 'guix substitute' correctly communicates the above
530 ;; data.
531 (set-build-options s #:use-substitutes? #t
532 #:substitute-urls (%test-substitute-urls))
533 (and (has-substitutes? s o)
534 (equal? (list o) (substitutable-paths s (list o)))
535 (match (pk 'spi (substitutable-path-info s (list o)))
536 (((? substitutable? s))
537 (and (string=? (substitutable-deriver s)
538 (derivation-file-name d))
539 (null? (substitutable-references s))
540 (equal? (substitutable-nar-size s) 1234)))))))))
541
542 (test-assert "substitute query, alternating URLs"
543 (let* ((d (with-store s
544 (package-derivation s %bootstrap-guile (%current-system))))
545 (o (derivation->output-path d)))
546 (with-derivation-narinfo d
547 ;; Remove entry from the local cache.
548 (false-if-exception
549 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
550 "/guix/substitute")))
551
552 ;; Note: We reconnect to the daemon to force a new instance of 'guix
553 ;; substitute' to be used; otherwise the #:substitute-urls of
554 ;; 'set-build-options' would have no effect.
555
556 (and (with-store s ;the right substitute URL
557 (set-build-options s #:use-substitutes? #t
558 #:substitute-urls (%test-substitute-urls))
559 (has-substitutes? s o))
560 (with-store s ;the wrong one
561 (set-build-options s #:use-substitutes? #t
562 #:substitute-urls (list
563 "http://does-not-exist"))
564 (not (has-substitutes? s o)))
565 (with-store s ;the right one again
566 (set-build-options s #:use-substitutes? #t
567 #:substitute-urls (%test-substitute-urls))
568 (has-substitutes? s o))
569 (with-store s ;empty list of URLs
570 (set-build-options s #:use-substitutes? #t
571 #:substitute-urls '())
572 (not (has-substitutes? s o)))))))
573
574 (test-assert "substitute"
575 (with-store s
576 (let* ((c (random-text)) ; contents of the output
577 (d (build-expression->derivation
578 s "substitute-me"
579 `(call-with-output-file %output
580 (lambda (p)
581 (exit 1) ; would actually fail
582 (display ,c p)))
583 #:guile-for-build
584 (package-derivation s %bootstrap-guile (%current-system))))
585 (o (derivation->output-path d)))
586 (with-derivation-substitute d c
587 (set-build-options s #:use-substitutes? #t
588 #:substitute-urls (%test-substitute-urls))
589 (and (has-substitutes? s o)
590 (build-derivations s (list d))
591 (equal? c (call-with-input-file o get-string-all)))))))
592
593 (test-assert "substitute + build-things with output path"
594 (with-store s
595 (let* ((c (random-text)) ;contents of the output
596 (d (build-expression->derivation
597 s "substitute-me"
598 `(call-with-output-file %output
599 (lambda (p)
600 (exit 1) ;would actually fail
601 (display ,c p)))
602 #:guile-for-build
603 (package-derivation s %bootstrap-guile (%current-system))))
604 (o (derivation->output-path d)))
605 (with-derivation-substitute d c
606 (set-build-options s #:use-substitutes? #t
607 #:substitute-urls (%test-substitute-urls))
608 (and (has-substitutes? s o)
609 (build-things s (list o)) ;give the output path
610 (valid-path? s o)
611 (equal? c (call-with-input-file o get-string-all)))))))
612
613 (test-assert "substitute + build-things with specific output"
614 (with-store s
615 (let* ((c (random-text)) ;contents of the output
616 (d (build-expression->derivation
617 s "substitute-me" `(begin ,c (exit 1)) ;would fail
618 #:outputs '("out" "one" "two")
619 #:guile-for-build
620 (package-derivation s %bootstrap-guile (%current-system))))
621 (o (derivation->output-path d)))
622 (with-derivation-substitute d c
623 (set-build-options s #:use-substitutes? #t
624 #:substitute-urls (%test-substitute-urls))
625 (and (has-substitutes? s o)
626
627 ;; Ask for nothing but the "out" output of D.
628 (build-things s `((,(derivation-file-name d) . "out")))
629
630 (valid-path? s o)
631 (equal? c (call-with-input-file o get-string-all)))))))
632
633 (test-assert "substitute, corrupt output hash"
634 ;; Tweak the substituter into installing a substitute whose hash doesn't
635 ;; match the one announced in the narinfo. The daemon must notice this and
636 ;; raise an error.
637 (with-store s
638 (let* ((c "hello, world") ; contents of the output
639 (d (build-expression->derivation
640 s "corrupt-substitute"
641 `(mkdir %output)
642 #:guile-for-build
643 (package-derivation s %bootstrap-guile (%current-system))))
644 (o (derivation->output-path d)))
645 (with-derivation-substitute d c
646 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
647
648 ;; Make sure we use 'guix substitute'.
649 (set-build-options s
650 #:use-substitutes? #t
651 #:fallback? #f
652 #:substitute-urls (%test-substitute-urls))
653 (and (has-substitutes? s o)
654 (guard (c ((store-protocol-error? c)
655 ;; XXX: the daemon writes "hash mismatch in downloaded
656 ;; path", but the actual error returned to the client
657 ;; doesn't mention that.
658 (pk 'corrupt c)
659 (not (zero? (store-protocol-error-status c)))))
660 (build-derivations s (list d))
661 #f))))))
662
663 (test-assert "substitute --fallback"
664 (with-store s
665 (let* ((t (random-text)) ; contents of the output
666 (d (build-expression->derivation
667 s "substitute-me-not"
668 `(call-with-output-file %output
669 (lambda (p)
670 (display ,t p)))
671 #:guile-for-build
672 (package-derivation s %bootstrap-guile (%current-system))))
673 (o (derivation->output-path d)))
674 ;; Create fake substituter data, to be read by 'guix substitute'.
675 (with-derivation-narinfo d
676 ;; Make sure we use 'guix substitute'.
677 (set-build-options s #:use-substitutes? #t
678 #:substitute-urls (%test-substitute-urls))
679 (and (has-substitutes? s o)
680 (guard (c ((store-protocol-error? c)
681 ;; The substituter failed as expected. Now make
682 ;; sure that #:fallback? #t works correctly.
683 (set-build-options s
684 #:use-substitutes? #t
685 #:substitute-urls
686 (%test-substitute-urls)
687 #:fallback? #t)
688 (and (build-derivations s (list d))
689 (equal? t (call-with-input-file o
690 get-string-all)))))
691 ;; Should fail.
692 (build-derivations s (list d))
693 #f))))))
694
695 (test-assert "export/import several paths"
696 (let* ((texts (unfold (cut >= <> 10)
697 (lambda _ (random-text))
698 1+
699 0))
700 (files (map (cut add-text-to-store %store "text" <>) texts))
701 (dump (call-with-bytevector-output-port
702 (cut export-paths %store files <>))))
703 (delete-paths %store files)
704 (and (every (negate file-exists?) files)
705 (let* ((source (open-bytevector-input-port dump))
706 (imported (import-paths %store source)))
707 (and (equal? imported files)
708 (every file-exists? files)
709 (equal? texts
710 (map (lambda (file)
711 (call-with-input-file file
712 get-string-all))
713 files)))))))
714
715 (test-assert "export/import paths, ensure topological order"
716 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
717 (file1 (add-text-to-store %store "foo" (random-text)
718 (list file0)))
719 (file2 (add-text-to-store %store "bar" (random-text)
720 (list file1)))
721 (files (list file1 file2))
722 (dump1 (call-with-bytevector-output-port
723 (cute export-paths %store (list file1 file2) <>)))
724 (dump2 (call-with-bytevector-output-port
725 (cute export-paths %store (list file2 file1) <>))))
726 (delete-paths %store files)
727 (and (every (negate file-exists?) files)
728 (bytevector=? dump1 dump2)
729 (let* ((source (open-bytevector-input-port dump1))
730 (imported (import-paths %store source)))
731 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
732 (and (equal? imported (list file1 file2))
733 (every file-exists? files)
734 (equal? (list file0) (references %store file1))
735 (equal? (list file1) (references %store file2)))))))
736
737 (test-assert "export/import incomplete"
738 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
739 (file1 (add-text-to-store %store "foo" (random-text)
740 (list file0)))
741 (file2 (add-text-to-store %store "bar" (random-text)
742 (list file1)))
743 (dump (call-with-bytevector-output-port
744 (cute export-paths %store (list file2) <>))))
745 (delete-paths %store (list file0 file1 file2))
746 (guard (c ((store-protocol-error? c)
747 (and (not (zero? (store-protocol-error-status c)))
748 (string-contains (store-protocol-error-message c)
749 "not valid"))))
750 ;; Here we get an exception because DUMP does not include FILE0 and
751 ;; FILE1, which are dependencies of FILE2.
752 (import-paths %store (open-bytevector-input-port dump)))))
753
754 (test-assert "export/import recursive"
755 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
756 (file1 (add-text-to-store %store "foo" (random-text)
757 (list file0)))
758 (file2 (add-text-to-store %store "bar" (random-text)
759 (list file1)))
760 (dump (call-with-bytevector-output-port
761 (cute export-paths %store (list file2) <>
762 #:recursive? #t))))
763 (delete-paths %store (list file0 file1 file2))
764 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
765 (and (equal? imported (list file0 file1 file2))
766 (every file-exists? (list file0 file1 file2))
767 (equal? (list file0) (references %store file1))
768 (equal? (list file1) (references %store file2))))))
769
770 (test-assert "write-file & export-path yield the same result"
771 ;; Here we compare 'write-file' and the daemon's own implementation.
772 ;; 'write-file' is the reference because we know it sorts file
773 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
774 ;; currently happen to be sorted as a side-effect of some unrelated
775 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
776 ;; changes there.
777 (run-with-store %store
778 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
779 (out1 -> (derivation->output-path drv1))
780 (data -> (unfold (cut >= <> 26)
781 (lambda (i)
782 (random-bytevector 128))
783 1+ 0))
784 (build
785 -> #~(begin
786 (use-modules (rnrs io ports) (srfi srfi-1))
787 (let ()
788 (define letters
789 (map (lambda (i)
790 (string
791 (integer->char
792 (+ i (char->integer #\a)))))
793 (iota 26)))
794 (define (touch file data)
795 (call-with-output-file file
796 (lambda (port)
797 (put-bytevector port data))))
798
799 (mkdir #$output)
800 (chdir #$output)
801
802 ;; The files must be different so they have
803 ;; different inode numbers, and the inode
804 ;; order must differ from the lexicographic
805 ;; order.
806 (for-each touch
807 (append (drop letters 10)
808 (take letters 10))
809 (list #$@data))
810 #t)))
811 (drv2 (gexp->derivation "bunch" build))
812 (out2 -> (derivation->output-path drv2))
813 (item-info -> (store-lift query-path-info)))
814 (mbegin %store-monad
815 (built-derivations (list drv1 drv2))
816 (foldm %store-monad
817 (lambda (item result)
818 (define ref-hash
819 (let-values (((port get) (open-sha256-port)))
820 (write-file item port)
821 (close-port port)
822 (get)))
823
824 ;; 'query-path-info' returns a hash produced by using the
825 ;; daemon's C++ 'dump' function, which is the implementation
826 ;; under test.
827 (>>= (item-info item)
828 (lambda (info)
829 (return
830 (and result
831 (bytevector=? (path-info-hash info) ref-hash))))))
832 #t
833 (list out1 out2))))
834 #:guile-for-build (%guile-for-build)))
835
836 (test-assert "import corrupt path"
837 (let* ((text (random-text))
838 (file (add-text-to-store %store "text" text))
839 (dump (call-with-bytevector-output-port
840 (cut export-paths %store (list file) <>))))
841 (delete-paths %store (list file))
842
843 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
844 ;; the file contents in DUMP, regardless of the store prefix.
845 (let* ((index #x70)
846 (byte (bytevector-u8-ref dump index)))
847 (bytevector-u8-set! dump index (logxor #xff byte)))
848
849 (and (not (file-exists? file))
850 (guard (c ((store-protocol-error? c)
851 (pk 'c c)
852 (and (not (zero? (store-protocol-error-status c)))
853 (string-contains (store-protocol-error-message c)
854 "corrupt"))))
855 (let* ((source (open-bytevector-input-port dump))
856 (imported (import-paths %store source)))
857 (pk 'corrupt-imported imported)
858 #f)))))
859
860 (test-assert "verify-store"
861 (let* ((text (random-text))
862 (file1 (add-text-to-store %store "foo" text))
863 (file2 (add-text-to-store %store "bar" (random-text)
864 (list file1))))
865 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
866 (begin
867 (delete-file file1)
868 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
869 (begin
870 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
871 ;; without actually creating the file. ;
872 (call-with-output-file file1
873 (lambda (port)
874 (display text port)))
875 (pk 'verify3 (verify-store %store)))))) ;OK again
876
877 (test-assert "verify-store + check-contents"
878 ;; XXX: This test is I/O intensive.
879 (with-store s
880 (let* ((text (random-text))
881 (drv (build-expression->derivation
882 s "corrupt"
883 `(let ((out (assoc-ref %outputs "out")))
884 (call-with-output-file out
885 (lambda (port)
886 (display ,text port)))
887 #t)
888 #:guile-for-build
889 (package-derivation s %bootstrap-guile (%current-system))))
890 (file (derivation->output-path drv)))
891 (with-derivation-substitute drv text
892 (and (build-derivations s (list drv))
893 (verify-store s #:check-contents? #t) ;should be OK
894 (begin
895 (chmod file #o644)
896 (call-with-output-file file
897 (lambda (port)
898 (display "corrupt!" port)))
899 #t)
900
901 ;; Make sure the corruption is detected. We don't test repairing
902 ;; because only "trusted" users are allowed to do it, but we
903 ;; don't expose that notion of trusted users that nix-daemon
904 ;; supports because it seems dubious and redundant with what the
905 ;; OS provides (in Nix "trusted" users have additional
906 ;; privileges, such as overriding the set of substitute URLs, but
907 ;; we instead want to allow anyone to modify them, provided
908 ;; substitutes are signed by a root-approved key.)
909 (not (verify-store s #:check-contents? #t))
910
911 ;; Delete the corrupt item to leave the store in a clean state.
912 (delete-paths s (list file)))))))
913
914 (test-assert "build-things, check mode"
915 (with-store store
916 (call-with-temporary-output-file
917 (lambda (entropy entropy-port)
918 (write (random-text) entropy-port)
919 (force-output entropy-port)
920 (let* ((drv (build-expression->derivation
921 store "non-deterministic"
922 `(begin
923 (use-modules (rnrs io ports))
924 (let ((out (assoc-ref %outputs "out")))
925 (call-with-output-file out
926 (lambda (port)
927 ;; Rely on the fact that tests do not use the
928 ;; chroot, and thus ENTROPY is readable.
929 (display (call-with-input-file ,entropy
930 get-string-all)
931 port)))
932 #t))
933 #:guile-for-build
934 (package-derivation store %bootstrap-guile (%current-system))))
935 (file (derivation->output-path drv)))
936 (and (build-things store (list (derivation-file-name drv)))
937 (begin
938 (write (random-text) entropy-port)
939 (force-output entropy-port)
940 (guard (c ((store-protocol-error? c)
941 (pk 'determinism-exception c)
942 (and (not (zero? (store-protocol-error-status c)))
943 (string-contains (store-protocol-error-message c)
944 "deterministic"))))
945 ;; This one will produce a different result. Since we're in
946 ;; 'check' mode, this must fail.
947 (build-things store (list (derivation-file-name drv))
948 (build-mode check))
949 #f))))))))
950
951 (test-assert "build-succeeded trace in check mode"
952 (string-contains
953 (call-with-output-string
954 (lambda (port)
955 (let ((d (build-expression->derivation
956 %store "foo" '(mkdir (assoc-ref %outputs "out"))
957 #:guile-for-build
958 (package-derivation %store %bootstrap-guile))))
959 (build-derivations %store (list d))
960 (parameterize ((current-build-output-port port))
961 (build-derivations %store (list d) (build-mode check))))))
962 "@ build-succeeded"))
963
964 (test-assert "build multiple times"
965 (with-store store
966 ;; Ask to build twice.
967 (set-build-options store #:rounds 2 #:use-substitutes? #f)
968
969 (call-with-temporary-output-file
970 (lambda (entropy entropy-port)
971 (write (random-text) entropy-port)
972 (force-output entropy-port)
973 (let* ((drv (build-expression->derivation
974 store "non-deterministic"
975 `(begin
976 (use-modules (rnrs io ports))
977 (let ((out (assoc-ref %outputs "out")))
978 (call-with-output-file out
979 (lambda (port)
980 ;; Rely on the fact that tests do not use the
981 ;; chroot, and thus ENTROPY is accessible.
982 (display (call-with-input-file ,entropy
983 get-string-all)
984 port)
985 (call-with-output-file ,entropy
986 (lambda (port)
987 (write 'foobar port)))))
988 #t))
989 #:guile-for-build
990 (package-derivation store %bootstrap-guile (%current-system))))
991 (file (derivation->output-path drv)))
992 (guard (c ((store-protocol-error? c)
993 (pk 'multiple-build c)
994 (and (not (zero? (store-protocol-error-status c)))
995 (string-contains (store-protocol-error-message c)
996 "deterministic"))))
997 ;; This one will produce a different result on the second run.
998 (current-build-output-port (current-error-port))
999 (build-things store (list (derivation-file-name drv)))
1000 #f))))))
1001
1002 (test-equal "store-lower"
1003 "Lowered."
1004 (let* ((add (store-lower text-file))
1005 (file (add %store "foo" "Lowered.")))
1006 (call-with-input-file file get-string-all)))
1007
1008 (test-equal "current-system"
1009 "bar"
1010 (parameterize ((%current-system "frob"))
1011 (run-with-store %store
1012 (mbegin %store-monad
1013 (set-current-system "bar")
1014 (current-system))
1015 #:system "foo")))
1016
1017 (test-assert "query-path-info"
1018 (let* ((ref (add-text-to-store %store "ref" "foo"))
1019 (item (add-text-to-store %store "item" "bar" (list ref)))
1020 (info (query-path-info %store item)))
1021 (and (equal? (path-info-references info) (list ref))
1022 (equal? (path-info-hash info)
1023 (sha256
1024 (string->utf8
1025 (call-with-output-string (cut write-file item <>))))))))
1026
1027 (test-assert "path-info-deriver"
1028 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1029 (s (add-to-store %store "bash" #t "sha256"
1030 (search-bootstrap-binary "bash"
1031 (%current-system))))
1032 (d (derivation %store "the-thing"
1033 s `("-e" ,b)
1034 #:env-vars `(("foo" . ,(random-text)))
1035 #:inputs `((,b) (,s))))
1036 (o (derivation->output-path d)))
1037 (and (build-derivations %store (list d))
1038 (not (path-info-deriver (query-path-info %store b)))
1039 (string=? (derivation-file-name d)
1040 (path-info-deriver (query-path-info %store o))))))
1041
1042 (test-equal "build-cores"
1043 (list 0 42)
1044 (with-store store
1045 (let* ((build (add-text-to-store store "build.sh"
1046 "echo $NIX_BUILD_CORES > $out"))
1047 (bash (add-to-store store "bash" #t "sha256"
1048 (search-bootstrap-binary "bash"
1049 (%current-system))))
1050 (drv1 (derivation store "the-thing" bash
1051 `("-e" ,build)
1052 #:inputs `((,bash) (,build))
1053 #:env-vars `(("x" . ,(random-text)))))
1054 (drv2 (derivation store "the-thing" bash
1055 `("-e" ,build)
1056 #:inputs `((,bash) (,build))
1057 #:env-vars `(("x" . ,(random-text))))))
1058 (and (build-derivations store (list drv1))
1059 (begin
1060 (set-build-options store #:build-cores 42)
1061 (build-derivations store (list drv2)))
1062 (list (call-with-input-file (derivation->output-path drv1)
1063 read)
1064 (call-with-input-file (derivation->output-path drv2)
1065 read))))))
1066
1067 (test-equal "multiplexed-build-output"
1068 '("Hello from first." "Hello from second.")
1069 (with-store store
1070 (let* ((build (add-text-to-store store "build.sh"
1071 "echo Hello from $NAME.; echo > $out"))
1072 (bash (add-to-store store "bash" #t "sha256"
1073 (search-bootstrap-binary "bash"
1074 (%current-system))))
1075 (drv1 (derivation store "one" bash
1076 `("-e" ,build)
1077 #:inputs `((,bash) (,build))
1078 #:env-vars `(("NAME" . "first")
1079 ("x" . ,(random-text)))))
1080 (drv2 (derivation store "two" bash
1081 `("-e" ,build)
1082 #:inputs `((,bash) (,build))
1083 #:env-vars `(("NAME" . "second")
1084 ("x" . ,(random-text))))))
1085 (set-build-options store
1086 #:print-build-trace #t
1087 #:multiplexed-build-output? #t
1088 #:max-build-jobs 10)
1089 (let ((port (open-output-string)))
1090 ;; Send the build log to PORT.
1091 (parameterize ((current-build-output-port port))
1092 (build-derivations store (list drv1 drv2)))
1093
1094 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1095 ;; traces that allow us to retrieve each builder's output (we assume
1096 ;; there's exactly one "build-output" trace for each builder, which is
1097 ;; reasonable.)
1098 (let* ((log (get-output-string port))
1099 (started (fold-matches
1100 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1101 log '() cons))
1102 (done (fold-matches
1103 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1104 log '() cons))
1105 (output (fold-matches
1106 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1107 log '() cons))
1108 (drv-pid (lambda (name)
1109 (lambda (m)
1110 (let ((drv (match:substring m 1))
1111 (pid (string->number
1112 (match:substring m 4))))
1113 (and (string-suffix? name drv) pid)))))
1114 (pid-log (lambda (pid)
1115 (lambda (m)
1116 (let ((n (string->number
1117 (match:substring m 1)))
1118 (len (string->number
1119 (match:substring m 2)))
1120 (str (match:substring m 3)))
1121 (and (= pid n)
1122 (= (string-length str) (- len 1))
1123 str)))))
1124 (pid1 (any (drv-pid "one.drv") started))
1125 (pid2 (any (drv-pid "two.drv") started)))
1126 (list (any (pid-log pid1) output)
1127 (any (pid-log pid2) output)))))))
1128
1129 (test-end "store")