Add Deck to 'AUTHORS'.
[jackhill/guix/guix.git] / tests / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
023d9892 2;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3259877d 3;;;
233e7676 4;;; This file is part of GNU Guix.
3259877d 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
3259877d
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
3259877d
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
3259877d 18
3259877d 19(define-module (test-store)
c1bc358f 20 #:use-module (guix tests)
3259877d
LC
21 #:use-module (guix store)
22 #:use-module (guix utils)
72626a71 23 #:use-module (guix hash)
3259877d 24 #:use-module (guix base32)
0f3d2504
LC
25 #:use-module (guix packages)
26 #:use-module (guix derivations)
0363991a 27 #:use-module (guix serialization)
fae31edc 28 #:use-module (gnu packages)
1ffa7090 29 #:use-module (gnu packages bootstrap)
3259877d 30 #:use-module (ice-9 match)
526382ff 31 #:use-module (rnrs bytevectors)
fe0cff14 32 #:use-module (rnrs io ports)
f65cf81a 33 #:use-module (web uri)
3259877d
LC
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
526382ff 36 #:use-module (srfi srfi-26)
c3eb878f 37 #:use-module (srfi srfi-34)
3259877d
LC
38 #:use-module (srfi srfi-64))
39
40;; Test the (guix store) module.
41
42(define %store
c1bc358f 43 (open-connection-for-tests))
3259877d
LC
44
45\f
46(test-begin "store")
47
2c6ab6cc
LC
48(test-equal "store-path-hash-part"
49 "283gqy39v3g9dxjy26rynl0zls82fmcg"
50 (store-path-hash-part
51 (string-append (%store-prefix)
52 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
53
54(test-equal "store-path-hash-part #f"
55 #f
56 (store-path-hash-part
57 (string-append (%store-prefix)
58 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
59
c61a5b4a
LC
60(test-equal "store-path-package-name"
61 "guile-2.0.7"
62 (store-path-package-name
63 (string-append (%store-prefix)
64 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
65
66(test-equal "store-path-package-name #f"
67 #f
68 (store-path-package-name
69 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
70
9336e5b5
LC
71(test-assert "direct-store-path?"
72 (and (direct-store-path?
73 (string-append (%store-prefix)
74 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
75 (not (direct-store-path?
76 (string-append
77 (%store-prefix)
eee21271
LC
78 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
79 (not (direct-store-path? (%store-prefix)))))
9336e5b5 80
e297d8fc
LC
81(test-skip (if %store 0 13))
82
83(test-assert "valid-path? live"
84 (let ((p (add-text-to-store %store "hello" "hello, world")))
85 (valid-path? %store p)))
86
87(test-assert "valid-path? false"
88 (not (valid-path? %store
89 (string-append (%store-prefix) "/"
90 (make-string 32 #\e) "-foobar"))))
91
92(test-assert "valid-path? error"
93 (with-store s
94 (guard (c ((nix-protocol-error? c) #t))
95 (valid-path? s "foo")
96 #f)))
97
98(test-assert "valid-path? recovery"
99 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
100 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
101 ;; file name. See
102 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
103 ;; details.
104 (with-store s
105 (let-syntax ((true-if-error (syntax-rules ()
106 ((_ exp)
107 (guard (c ((nix-protocol-error? c) #t))
108 exp #f)))))
109 (and (true-if-error (valid-path? s "foo"))
110 (true-if-error (valid-path? s "bar"))
111 (true-if-error (valid-path? s "baz"))
112 (true-if-error (valid-path? s "chbouib"))
113 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
11e7a6cf
LC
114
115(test-assert "hash-part->path"
116 (let ((p (add-text-to-store %store "hello" "hello, world")))
117 (equal? (hash-part->path %store (store-path-hash-part p))
118 p)))
3259877d
LC
119
120(test-assert "dead-paths"
cfbf9160 121 (let ((p (add-text-to-store %store "random-text" (random-text))))
3259877d
LC
122 (member p (dead-paths %store))))
123
124;; FIXME: Find a test for `live-paths'.
125;;
126;; (test-assert "temporary root is in live-paths"
127;; (let* ((p1 (add-text-to-store %store "random-text"
128;; (random-text) '()))
129;; (b (add-text-to-store %store "link-builder"
130;; (format #f "echo ~a > $out" p1)
131;; '()))
a987d2c0
LC
132;; (d1 (derivation %store "link"
133;; "/bin/sh" `("-e" ,b)
134;; #:inputs `((,b) (,p1))))
59688fc4 135;; (p2 (derivation->output-path d1)))
3259877d
LC
136;; (and (add-temp-root %store p2)
137;; (build-derivations %store (list d1))
138;; (valid-path? %store p1)
139;; (member (pk p2) (live-paths %store)))))
140
a9d2a105
LC
141(test-assert "permanent root"
142 (let* ((p (with-store store
143 (let ((p (add-text-to-store store "random-text"
144 (random-text))))
145 (add-permanent-root p)
146 (add-permanent-root p) ; should not throw
147 p))))
148 (and (member p (live-paths %store))
149 (begin
150 (remove-permanent-root p)
151 (->bool (member p (dead-paths %store)))))))
152
3259877d
LC
153(test-assert "dead path can be explicitly collected"
154 (let ((p (add-text-to-store %store "random-text"
155 (random-text) '())))
156 (let-values (((paths freed) (delete-paths %store (list p))))
157 (and (equal? paths (list p))
158 (> freed 0)
159 (not (file-exists? p))))))
160
000c59b6
LC
161(test-assert "add-text-to-store vs. delete-paths"
162 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
163 ;; is no longer valid.
164 (with-store store
165 (let* ((text (random-text))
166 (path (add-text-to-store store "delete-me" text))
167 (deleted (delete-paths store (list path)))
168 (path2 (add-text-to-store store "delete-me" text)))
169 (and (string=? path path2)
170 (equal? deleted (list path))
171 (valid-path? store path)
172 (file-exists? path)))))
173
174(test-assert "add-to-store vs. delete-paths"
175 ;; Same as above.
176 (with-store store
177 (let* ((file (search-path %load-path "guix.scm"))
178 (path (add-to-store store "delete-me" #t "sha256" file))
179 (deleted (delete-paths store (list path)))
180 (path2 (add-to-store store "delete-me" #t "sha256" file)))
181 (and (string=? path path2)
182 (equal? deleted (list path))
183 (valid-path? store path)
184 (file-exists? path)))))
185
fae31edc
LC
186(test-assert "references"
187 (let* ((t1 (add-text-to-store %store "random1"
cfbf9160 188 (random-text)))
fae31edc
LC
189 (t2 (add-text-to-store %store "random2"
190 (random-text) (list t1))))
191 (and (equal? (list t1) (references %store t2))
192 (equal? (list t2) (referrers %store t1))
193 (null? (references %store t1))
194 (null? (referrers %store t2)))))
195
3f1e6939
LC
196(test-assert "requisites"
197 (let* ((t1 (add-text-to-store %store "random1"
198 (random-text) '()))
199 (t2 (add-text-to-store %store "random2"
200 (random-text) (list t1)))
201 (t3 (add-text-to-store %store "random3"
202 (random-text) (list t2)))
203 (t4 (add-text-to-store %store "random4"
204 (random-text) (list t1 t3))))
205 (define (same? x y)
206 (and (= (length x) (length y))
207 (lset= equal? x y)))
208
209 (and (same? (requisites %store t1) (list t1))
210 (same? (requisites %store t2) (list t1 t2))
211 (same? (requisites %store t3) (list t1 t2 t3))
212 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
213
fae31edc
LC
214(test-assert "derivers"
215 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
216 (s (add-to-store %store "bash" #t "sha256"
217 (search-bootstrap-binary "bash"
218 (%current-system))))
a987d2c0
LC
219 (d (derivation %store "the-thing"
220 s `("-e" ,b)
221 #:env-vars `(("foo" . ,(random-text)))
222 #:inputs `((,b) (,s))))
59688fc4 223 (o (derivation->output-path d)))
fae31edc 224 (and (build-derivations %store (list d))
59688fc4 225 (equal? (query-derivation-outputs %store (derivation-file-name d))
fae31edc
LC
226 (list o))
227 (equal? (valid-derivers %store o)
59688fc4 228 (list (derivation-file-name d))))))
fae31edc 229
50add477
LC
230(test-assert "topologically-sorted, one item"
231 (let* ((a (add-text-to-store %store "a" "a"))
232 (b (add-text-to-store %store "b" "b" (list a)))
233 (c (add-text-to-store %store "c" "c" (list b)))
234 (d (add-text-to-store %store "d" "d" (list c)))
235 (s (topologically-sorted %store (list d))))
236 (equal? s (list a b c d))))
237
238(test-assert "topologically-sorted, several items"
239 (let* ((a (add-text-to-store %store "a" "a"))
240 (b (add-text-to-store %store "b" "b" (list a)))
241 (c (add-text-to-store %store "c" "c" (list b)))
242 (d (add-text-to-store %store "d" "d" (list c)))
243 (s1 (topologically-sorted %store (list d a c b)))
244 (s2 (topologically-sorted %store (list b d c a b d))))
245 (equal? s1 s2 (list a b c d))))
246
247(test-assert "topologically-sorted, more difficult"
248 (let* ((a (add-text-to-store %store "a" "a"))
249 (b (add-text-to-store %store "b" "b" (list a)))
250 (c (add-text-to-store %store "c" "c" (list b)))
251 (d (add-text-to-store %store "d" "d" (list c)))
252 (w (add-text-to-store %store "w" "w"))
253 (x (add-text-to-store %store "x" "x" (list w)))
254 (y (add-text-to-store %store "y" "y" (list x d)))
255 (s1 (topologically-sorted %store (list y)))
256 (s2 (topologically-sorted %store (list c y)))
257 (s3 (topologically-sorted %store (cons y (references %store y)))))
58cbbe4b
LC
258 ;; The order in which 'references' returns the references of Y is
259 ;; unspecified, so accommodate.
260 (let* ((x-then-d? (equal? (references %store y) (list x d))))
261 (and (equal? s1
262 (if x-then-d?
263 (list w x a b c d y)
264 (list a b c d w x y)))
265 (equal? s2
266 (if x-then-d?
267 (list a b c w x d y)
268 (list a b c d w x y)))
269 (lset= string=? s1 s3)))))
50add477 270
eddd4077
LC
271(test-assert "log-file, derivation"
272 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
273 (s (add-to-store %store "bash" #t "sha256"
274 (search-bootstrap-binary "bash"
275 (%current-system))))
276 (d (derivation %store "the-thing"
277 s `("-e" ,b)
278 #:env-vars `(("foo" . ,(random-text)))
279 #:inputs `((,b) (,s)))))
280 (and (build-derivations %store (list d))
281 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
282
283(test-assert "log-file, output file name"
284 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
285 (s (add-to-store %store "bash" #t "sha256"
286 (search-bootstrap-binary "bash"
287 (%current-system))))
288 (d (derivation %store "the-thing"
289 s `("-e" ,b)
290 #:env-vars `(("foo" . ,(random-text)))
291 #:inputs `((,b) (,s))))
292 (o (derivation->output-path d)))
293 (and (build-derivations %store (list d))
294 (file-exists? (pk (log-file %store o)))
295 (string=? (log-file %store (derivation-file-name d))
296 (log-file %store o)))))
297
0f3d2504
LC
298(test-assert "no substitutes"
299 (let* ((s (open-connection))
300 (d1 (package-derivation s %bootstrap-guile (%current-system)))
301 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
59688fc4 302 (o (map derivation->output-path (list d1 d2))))
0f3d2504 303 (set-build-options s #:use-substitutes? #f)
59688fc4
LC
304 (and (not (has-substitutes? s (derivation-file-name d1)))
305 (not (has-substitutes? s (derivation-file-name d2)))
0f3d2504
LC
306 (null? (substitutable-paths s o))
307 (null? (substitutable-path-info s o)))))
308
f65cf81a
LC
309(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
310
311(test-assert "substitute query"
312 (let* ((s (open-connection))
313 (d (package-derivation s %bootstrap-guile (%current-system)))
59688fc4 314 (o (derivation->output-path d))
f65cf81a
LC
315 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
316 (compose uri-path string->uri))))
317 ;; Create fake substituter data, to be read by `substitute-binary'.
318 (call-with-output-file (string-append dir "/nix-cache-info")
319 (lambda (p)
320 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
fe0cff14 321 (%store-prefix))))
f65cf81a
LC
322 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
323 ".narinfo")
324 (lambda (p)
325 (format p "StorePath: ~a
326URL: ~a
327Compression: none
328NarSize: 1234
329References:
330System: ~a
331Deriver: ~a~%"
332 o ; StorePath
333 (string-append dir "/example.nar") ; URL
334 (%current-system) ; System
59688fc4
LC
335 (basename
336 (derivation-file-name d))))) ; Deriver
f65cf81a 337
eba783b7
LC
338 ;; Remove entry from the local cache.
339 (false-if-exception
340 (delete-file (string-append (getenv "XDG_CACHE_HOME")
341 "/guix/substitute-binary/"
342 (store-path-hash-part o))))
343
f65cf81a
LC
344 ;; Make sure `substitute-binary' correctly communicates the above data.
345 (set-build-options s #:use-substitutes? #t)
346 (and (has-substitutes? s o)
347 (equal? (list o) (substitutable-paths s (list o)))
348 (match (pk 'spi (substitutable-path-info s (list o)))
349 (((? substitutable? s))
59688fc4 350 (and (string=? (substitutable-deriver s) (derivation-file-name d))
f65cf81a
LC
351 (null? (substitutable-references s))
352 (equal? (substitutable-nar-size s) 1234)))))))
353
fe0cff14
LC
354(test-assert "substitute"
355 (let* ((s (open-connection))
356 (c (random-text)) ; contents of the output
357 (d (build-expression->derivation
dd1a5a15 358 s "substitute-me"
fe0cff14
LC
359 `(call-with-output-file %output
360 (lambda (p)
361 (exit 1) ; would actually fail
362 (display ,c p)))
fe0cff14
LC
363 #:guile-for-build
364 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 365 (o (derivation->output-path d))
fe0cff14
LC
366 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
367 (compose uri-path string->uri))))
368 ;; Create fake substituter data, to be read by `substitute-binary'.
369 (call-with-output-file (string-append dir "/nix-cache-info")
370 (lambda (p)
371 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
372 (%store-prefix))))
373 (call-with-output-file (string-append dir "/example.out")
374 (lambda (p)
375 (display c p)))
376 (call-with-output-file (string-append dir "/example.nar")
377 (lambda (p)
378 (write-file (string-append dir "/example.out") p)))
379 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
380 ".narinfo")
381 (lambda (p)
382 (format p "StorePath: ~a
383URL: ~a
384Compression: none
385NarSize: 1234
386NarHash: sha256:~a
387References:
388System: ~a
389Deriver: ~a~%"
390 o ; StorePath
391 "example.nar" ; relative URL
392 (call-with-input-file (string-append dir "/example.nar")
393 (compose bytevector->nix-base32-string sha256
394 get-bytevector-all))
395 (%current-system) ; System
59688fc4
LC
396 (basename
397 (derivation-file-name d))))) ; Deriver
fe0cff14
LC
398
399 ;; Make sure we use `substitute-binary'.
400 (set-build-options s #:use-substitutes? #t)
401 (and (has-substitutes? s o)
402 (build-derivations s (list d))
403 (equal? c (call-with-input-file o get-string-all)))))
404
491e6de7
LC
405(test-assert "substitute, corrupt output hash"
406 ;; Tweak the substituter into installing a substitute whose hash doesn't
407 ;; match the one announced in the narinfo. The daemon must notice this and
408 ;; raise an error.
409 (let* ((s (open-connection))
410 (c "hello, world") ; contents of the output
411 (d (build-expression->derivation
412 s "corrupt-substitute"
413 `(mkdir %output)
414 #:guile-for-build
415 (package-derivation s %bootstrap-guile (%current-system))))
416 (o (derivation->output-path d))
417 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
418 (compose uri-path string->uri))))
419 ;; Create fake substituter data, to be read by `substitute-binary'.
420 (call-with-output-file (string-append dir "/nix-cache-info")
421 (lambda (p)
422 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
423 (%store-prefix))))
424 (call-with-output-file (string-append dir "/example.out")
425 (lambda (p)
426 (display "The contents here do not match C." p)))
427 (call-with-output-file (string-append dir "/example.nar")
428 (lambda (p)
429 (write-file (string-append dir "/example.out") p)))
430 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
431 ".narinfo")
432 (lambda (p)
433 (format p "StorePath: ~a
434URL: ~a
435Compression: none
436NarSize: 1234
437NarHash: sha256:~a
438References:
439System: ~a
440Deriver: ~a~%"
441 o ; StorePath
442 "example.nar" ; relative URL
443 (bytevector->nix-base32-string
444 (sha256 (string->utf8 c)))
445 (%current-system) ; System
446 (basename
447 (derivation-file-name d))))) ; Deriver
448
449 ;; Make sure we use `substitute-binary'.
450 (set-build-options s
451 #:use-substitutes? #t
452 #:fallback? #f)
453 (and (has-substitutes? s o)
454 (guard (c ((nix-protocol-error? c)
455 ;; XXX: the daemon writes "hash mismatch in downloaded
456 ;; path", but the actual error returned to the client
457 ;; doesn't mention that.
458 (pk 'corrupt c)
459 (not (zero? (nix-protocol-error-status c)))))
460 (build-derivations s (list d))
461 #f))))
462
c3eb878f
LC
463(test-assert "substitute --fallback"
464 (let* ((s (open-connection))
465 (t (random-text)) ; contents of the output
466 (d (build-expression->derivation
dd1a5a15 467 s "substitute-me-not"
c3eb878f
LC
468 `(call-with-output-file %output
469 (lambda (p)
470 (display ,t p)))
c3eb878f
LC
471 #:guile-for-build
472 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 473 (o (derivation->output-path d))
c3eb878f
LC
474 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
475 (compose uri-path string->uri))))
476 ;; Create fake substituter data, to be read by `substitute-binary'.
477 (call-with-output-file (string-append dir "/nix-cache-info")
478 (lambda (p)
479 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
480 (%store-prefix))))
481 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
482 ".narinfo")
483 (lambda (p)
484 (format p "StorePath: ~a
485URL: ~a
486Compression: none
487NarSize: 1234
488NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
489References:
490System: ~a
491Deriver: ~a~%"
492 o ; StorePath
493 "does-not-exist.nar" ; relative URL
494 (%current-system) ; System
59688fc4
LC
495 (basename
496 (derivation-file-name d))))) ; Deriver
c3eb878f
LC
497
498 ;; Make sure we use `substitute-binary'.
499 (set-build-options s #:use-substitutes? #t)
500 (and (has-substitutes? s o)
501 (guard (c ((nix-protocol-error? c)
502 ;; The substituter failed as expected. Now make sure that
503 ;; #:fallback? #t works correctly.
504 (set-build-options s
505 #:use-substitutes? #t
506 #:fallback? #t)
507 (and (build-derivations s (list d))
508 (equal? t (call-with-input-file o get-string-all)))))
509 ;; Should fail.
510 (build-derivations s (list d))
511 #f))))
512
526382ff
LC
513(test-assert "export/import several paths"
514 (let* ((texts (unfold (cut >= <> 10)
515 (lambda _ (random-text))
516 1+
517 0))
518 (files (map (cut add-text-to-store %store "text" <>) texts))
519 (dump (call-with-bytevector-output-port
520 (cut export-paths %store files <>))))
521 (delete-paths %store files)
522 (and (every (negate file-exists?) files)
523 (let* ((source (open-bytevector-input-port dump))
524 (imported (import-paths %store source)))
525 (and (equal? imported files)
526 (every file-exists? files)
527 (equal? texts
528 (map (lambda (file)
529 (call-with-input-file file
530 get-string-all))
531 files)))))))
532
99fbddf9 533(test-assert "export/import paths, ensure topological order"
cafb92d8
LC
534 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
535 (file1 (add-text-to-store %store "foo" (random-text)
536 (list file0)))
99fbddf9
LC
537 (file2 (add-text-to-store %store "bar" (random-text)
538 (list file1)))
539 (files (list file1 file2))
540 (dump1 (call-with-bytevector-output-port
541 (cute export-paths %store (list file1 file2) <>)))
542 (dump2 (call-with-bytevector-output-port
543 (cute export-paths %store (list file2 file1) <>))))
544 (delete-paths %store files)
545 (and (every (negate file-exists?) files)
546 (bytevector=? dump1 dump2)
547 (let* ((source (open-bytevector-input-port dump1))
548 (imported (import-paths %store source)))
cafb92d8 549 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
99fbddf9
LC
550 (and (equal? imported (list file1 file2))
551 (every file-exists? files)
cafb92d8 552 (equal? (list file0) (references %store file1))
99fbddf9
LC
553 (equal? (list file1) (references %store file2)))))))
554
5b3d863f
LC
555(test-assert "export/import incomplete"
556 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
557 (file1 (add-text-to-store %store "foo" (random-text)
558 (list file0)))
559 (file2 (add-text-to-store %store "bar" (random-text)
560 (list file1)))
561 (dump (call-with-bytevector-output-port
562 (cute export-paths %store (list file2) <>))))
563 (delete-paths %store (list file0 file1 file2))
564 (guard (c ((nix-protocol-error? c)
565 (and (not (zero? (nix-protocol-error-status c)))
566 (string-contains (nix-protocol-error-message c)
567 "not valid"))))
568 ;; Here we get an exception because DUMP does not include FILE0 and
569 ;; FILE1, which are dependencies of FILE2.
570 (import-paths %store (open-bytevector-input-port dump)))))
571
572(test-assert "export/import recursive"
573 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
574 (file1 (add-text-to-store %store "foo" (random-text)
575 (list file0)))
576 (file2 (add-text-to-store %store "bar" (random-text)
577 (list file1)))
578 (dump (call-with-bytevector-output-port
579 (cute export-paths %store (list file2) <>
580 #:recursive? #t))))
581 (delete-paths %store (list file0 file1 file2))
582 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
583 (and (equal? imported (list file0 file1 file2))
584 (every file-exists? (list file0 file1 file2))
585 (equal? (list file0) (references %store file1))
586 (equal? (list file1) (references %store file2))))))
587
526382ff
LC
588(test-assert "import corrupt path"
589 (let* ((text (random-text))
590 (file (add-text-to-store %store "text" text))
591 (dump (call-with-bytevector-output-port
592 (cut export-paths %store (list file) <>))))
593 (delete-paths %store (list file))
594
6df1fb89
LC
595 ;; Flip a bit in the stream's payload.
596 (let* ((index (quotient (bytevector-length dump) 4))
526382ff
LC
597 (byte (bytevector-u8-ref dump index)))
598 (bytevector-u8-set! dump index (logxor #xff byte)))
599
600 (and (not (file-exists? file))
601 (guard (c ((nix-protocol-error? c)
602 (pk 'c c)
603 (and (not (zero? (nix-protocol-error-status c)))
604 (string-contains (nix-protocol-error-message c)
605 "corrupt"))))
606 (let* ((source (open-bytevector-input-port dump))
607 (imported (import-paths %store source)))
608 (pk 'corrupt-imported imported)
609 #f)))))
610
6bfec3ed
LC
611(test-assert "register-path"
612 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
613 "-fake")))
614 (when (valid-path? %store file)
615 (delete-paths %store (list file)))
616 (false-if-exception (delete-file file))
617
618 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
619 (drv (string-append file ".drv")))
620 (call-with-output-file file
621 (cut display "This is a fake store item.\n" <>))
622 (register-path file
623 #:references (list ref)
624 #:deriver drv)
625
626 (and (valid-path? %store file)
627 (equal? (references %store file) (list ref))
628 (null? (valid-derivers %store file))
629 (null? (referrers %store file))))))
630
023d9892
LC
631(test-equal "store-lower"
632 "Lowered."
633 (let* ((add (store-lower text-file))
634 (file (add %store "foo" "Lowered.")))
635 (call-with-input-file file get-string-all)))
636
3259877d
LC
637(test-end "store")
638
639\f
640(exit (= (test-runner-fail-count (test-runner-current)) 0))