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