1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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-nar)
20 #:use-module (guix tests)
21 #:use-module (guix nar)
22 #:use-module (guix serialization)
23 #:use-module (guix store)
24 #:use-module ((gcrypt hash)
25 #:select (open-sha256-port open-sha256-input-port))
26 #:use-module ((guix packages)
28 #:use-module ((guix build utils)
29 #:select (find-files))
30 #:use-module (rnrs bytevectors)
31 #:use-module (rnrs io ports)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-11)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:use-module (srfi srfi-64)
38 #:use-module (ice-9 ftw)
39 #:use-module (ice-9 regex)
40 #:use-module ((ice-9 control) #:select (let/ec))
41 #:use-module (ice-9 match))
43 ;; Test the (guix nar) module.
47 ;;; File system testing tools, initially contributed to Guile, then libchop.
50 (define (random-file-size)
51 (define %average (* 1024 512)) ; 512 KiB
52 (define %stddev (* 1024 64)) ; 64 KiB
54 (max 0 (round (+ %average (* %stddev (random:normal)))))))
56 (define (make-file-tree dir tree)
57 "Make file system TREE at DIR."
61 (string-append dir "/" file))
64 (('directory name (body ...))
66 (for-each (cute loop (scope name) <>) body))
67 (('directory name (? integer? mode) (body ...))
69 (for-each (cute loop (scope name) <>) body)
70 (chmod (scope name) mode))
72 (populate-file (scope file) (random-file-size)))
73 ((file (? integer? mode))
74 (populate-file (scope file) (random-file-size))
75 (chmod (scope file) mode))
77 (symlink to (scope from))))))
79 (define (delete-file-tree dir tree)
80 "Delete file TREE from DIR."
84 (string-append dir "/" file))
87 (('directory name (body ...))
88 (for-each (cute loop (scope name) <>) body)
90 (('directory name (? integer? mode) (body ...))
91 (chmod (scope name) #o755) ; make sure it can be entered
92 (for-each (cute loop (scope name) <>) body)
95 (delete-file (scope from)))
97 (delete-file (scope file))))))
99 (define-syntax-rule (with-file-tree dir tree body ...)
102 (make-file-tree dir 'tree))
106 (delete-file-tree dir 'tree))))
108 (define (file-tree-equal? input output)
109 "Return #t if the file trees at INPUT and OUTPUT are equal."
111 (cute string-drop <> (string-length input)))
113 (compose (cut string-append output <>) strip))
115 (file-system-fold (const #t)
116 (lambda (name stat result) ; leaf
118 (file=? name (sibling name))))
119 (lambda (name stat result) ; down
121 (lambda (name stat result) ; up
124 (lambda (name stat errno result)
125 (pk 'error name stat errno)
131 (define (populate-file file size)
132 (call-with-output-file file
134 (put-bytevector p (random-bytevector size)))))
137 (file-system-fold (const #t) ; enter?
138 (lambda (file stat result) ; leaf
141 (lambda (dir stat result) ; up
150 ;; An output directory under $top_builddir.
151 (string-append (dirname (search-path %load-path "pre-inst-env"))
152 "/test-nar-" (number->string (getpid))))
157 (test-assert "write-file-tree + restore-file"
158 (let* ((file1 (search-path %load-path "guix.scm"))
159 (file2 (search-path %load-path "guix/base32.scm"))
160 (file3 "#!/bin/something")
161 (output (string-append %test-dir "/output")))
165 (define-values (port get-bytevector)
166 (open-bytevector-output-port))
167 (write-file-tree "root" port
171 (values 'directory 0))
173 (values 'regular (stat:size (stat file1))))
177 (values 'directory 0))
179 (values 'regular (stat:size (stat file2))))
181 (values 'executable (string-length file3))))
184 ("root/foo" (open-input-file file1))
185 ("root/dir/bar" (open-input-file file2))
186 ("root/dir/exe" (open-input-string file3)))
192 ("root" '("foo" "dir" "lnk"))
193 ("root/dir" '("bar" "exe"))))
198 (restore-file (open-bytevector-input-port (get-bytevector))
200 (and (file=? (string-append output "/foo") file1)
201 (string=? (readlink (string-append output "/lnk"))
203 (file=? (string-append output "/dir/bar") file2)
204 (string=? (call-with-input-file (string-append output "/dir/exe")
207 (> (logand (stat:mode (lstat (string-append output "/dir/exe")))
210 (equal? '("." ".." "bar" "exe")
211 (scandir (string-append output "/dir")))
212 (equal? '("." ".." "dir" "foo" "lnk")
215 (false-if-exception (rm-rf %test-dir))))))
217 (test-assert "write-file supports non-file output ports"
218 (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
220 (output (%make-void-port "w")))
221 (write-file input output)
224 (test-equal "write-file puts file in C locale collation order"
225 (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
226 (let ((input (string-append %test-dir ".input")))
230 (call-with-output-file (string-append input "/" file)
237 (symlink "B" (string-append input "/z")))
239 (let-values (((port get-hash) (open-sha256-port)))
240 (write-file input port)
246 (test-equal "restore-file with incomplete input"
247 (string-append %test-dir "/foo")
248 (let ((port (open-bytevector-input-port #vu8(1 2 3))))
249 (guard (c ((nar-error? c)
250 (and (eq? port (nar-error-port c))
251 (nar-error-file c))))
252 (restore-file port (string-append %test-dir "/foo"))
255 (test-assert "write-file + restore-file"
256 (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
259 (nar (string-append output ".nar")))
263 (call-with-output-file nar
264 (cut write-file input <>))
265 (call-with-input-file nar
266 (cut restore-file <> output))
267 (file-tree-equal? input output))
269 (false-if-exception (delete-file nar))
270 (false-if-exception (rm-rf output))))))
272 (test-assert "write-file + restore-file with symlinks"
273 (let ((input (string-append %test-dir ".input")))
278 (with-file-tree input
280 (("reg") ("exe" #o777) ("sym" -> "reg")))
281 (let* ((output %test-dir)
282 (nar (string-append output ".nar")))
286 (call-with-output-file nar
287 (cut write-file input <>))
288 (call-with-input-file nar
289 (cut restore-file <> output))
290 (file-tree-equal? input output))
292 (false-if-exception (delete-file nar))
293 (false-if-exception (rm-rf output)))))))
297 (test-assert "write-file #:select? + restore-file"
298 (let ((input (string-append %test-dir ".input")))
303 (with-file-tree input
305 ((directory "a" (("x") ("y") ("z")))
306 ("b") ("c") ("d" -> "b")))
307 (let* ((output %test-dir)
308 (nar (string-append output ".nar")))
312 (call-with-output-file nar
314 (write-file input port
317 (and (not (string=? (basename file)
319 (not (eq? (stat:type stat)
321 (call-with-input-file nar
322 (cut restore-file <> output))
324 ;; Make sure "a" and "d" have been filtered out.
325 (and (not (file-exists? (string-append output "/root/a")))
326 (file=? (string-append output "/root/b")
327 (string-append input "/root/b"))
328 (file=? (string-append output "/root/c")
329 (string-append input "/root/c"))
330 (not (file-exists? (string-append output "/root/d")))))
332 (false-if-exception (delete-file nar))
333 (false-if-exception (rm-rf output)))))))
337 (test-eq "restore-file with non-UTF8 locale" ;<https://bugs.gnu.org/33603>
339 (let* ((file (search-path %load-path "guix.scm"))
340 (output (string-append %test-dir "/output"))
341 (locale (setlocale LC_ALL "C")))
345 (define-values (port get-bytevector)
346 (open-bytevector-output-port))
348 (write-file-tree "root" port
351 ("root" (values 'directory 0))
352 ("root/λ" (values 'regular 0)))
353 #:file-port (const (%make-void-port "r"))
354 #:symlink-target (const #f)
355 #:directory-entries (const '("λ")))
359 (catch 'encoding-error
361 ;; This show throw to 'encoding-error.
362 (restore-file (open-bytevector-input-port (get-bytevector))
368 (false-if-exception (rm-rf %test-dir))
369 (setlocale LC_ALL locale)))))
371 (test-assert "restore-file-set (signed, valid)"
373 (let* ((texts (unfold (cut >= <> 10)
374 (lambda _ (random-text))
377 (files (map (cut add-text-to-store store "text" <>) texts))
378 (dump (call-with-bytevector-output-port
379 (cut export-paths store files <>))))
380 (delete-paths store files)
381 (and (every (negate file-exists?) files)
382 (let* ((source (open-bytevector-input-port dump))
383 (imported (restore-file-set source)))
384 (and (equal? imported files)
385 (every (lambda (file)
386 (and (file-exists? file)
387 (valid-path? store file)))
391 (call-with-input-file file
394 (every canonical-file? files)))))))
396 (test-assert "restore-file-set with directories (signed, valid)"
397 ;; <https://bugs.gnu.org/33361> describes a bug whereby directories
398 ;; containing files subject to deduplication were not canonicalized--i.e.,
399 ;; their mtime and permissions were not reset. Ensure that this bug is
402 (let* ((text1 (random-text))
403 (text2 (random-text))
404 (tree `("tree" directory
405 ("a" regular (data ,text1))
407 ("c" regular (data ,text2))
408 ("d" regular (data ,text1))))) ;duplicate
409 (file (add-file-tree-to-store store tree))
410 (dump (call-with-bytevector-output-port
411 (cute export-paths store (list file) <>))))
412 (delete-paths store (list file))
413 (and (not (file-exists? file))
414 (let* ((source (open-bytevector-input-port dump))
415 (imported (restore-file-set source)))
416 (and (equal? imported (list file))
418 (valid-path? store file)
420 (call-with-input-file (string-append file "/a")
423 (call-with-input-file
424 (string-append file "/b/c")
426 (= (stat:ino (stat (string-append file "/a"))) ;deduplication
427 (stat:ino (stat (string-append file "/b/d"))))
428 (every canonical-file?
429 (find-files file #:directories? #t))))))))
431 (test-assert "restore-file-set (missing signature)"
434 (let* ((file (add-text-to-store store "foo" (random-text)))
435 (dump (call-with-bytevector-output-port
436 (cute export-paths store (list file) <>
438 (delete-paths store (list file))
439 (and (not (file-exists? file))
440 (let ((source (open-bytevector-input-port dump)))
441 (guard (c ((nar-signature-error? c)
442 (let ((message (condition-message c))
443 (port (nar-error-port c)))
445 (and (string-match "lacks.*signature" message)
446 (string=? file (nar-error-file c))
447 (eq? source port))))))
448 (restore-file-set source))
451 (test-assert "restore-file-set (corrupt)"
454 (let* ((file (add-text-to-store store "foo"
456 (dump (call-with-bytevector-output-port
457 (cute export-paths store (list file) <>))))
458 (delete-paths store (list file))
460 ;; Flip a byte in the file contents.
462 (byte (bytevector-u8-ref dump index)))
463 (bytevector-u8-set! dump index (logxor #xff byte)))
465 (and (not (file-exists? file))
466 (let ((source (open-bytevector-input-port dump)))
467 (guard (c ((nar-invalid-hash-error? c)
468 (let ((message (condition-message c))
469 (port (nar-error-port c)))
471 (and (string-contains message "hash")
472 (string=? file (nar-error-file c))
473 (eq? source port))))))
474 (restore-file-set source))
480 ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)