1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 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 nar)
21 #:use-module (guix store)
22 #:use-module ((guix hash) #:select (open-sha256-input-port))
23 #:use-module (rnrs bytevectors)
24 #:use-module (rnrs io ports)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-34)
28 #:use-module (srfi srfi-35)
29 #:use-module (srfi srfi-64)
30 #:use-module (ice-9 ftw)
31 #:use-module (ice-9 regex)
32 #:use-module (ice-9 match))
34 ;; Test the (guix nar) module.
38 ;;; File system testing tools, initially contributed to Guile, then libchop.
41 (define (random-file-size)
42 (define %average (* 1024 512)) ; 512 KiB
43 (define %stddev (* 1024 64)) ; 64 KiB
45 (max 0 (round (+ %average (* %stddev (random:normal)))))))
47 (define (make-file-tree dir tree)
48 "Make file system TREE at DIR."
52 (string-append dir "/" file))
55 (('directory name (body ...))
57 (for-each (cute loop (scope name) <>) body))
58 (('directory name (? integer? mode) (body ...))
60 (for-each (cute loop (scope name) <>) body)
61 (chmod (scope name) mode))
63 (populate-file (scope file) (random-file-size)))
64 ((file (? integer? mode))
65 (populate-file (scope file) (random-file-size))
66 (chmod (scope file) mode))
68 (symlink to (scope from))))))
70 (define (delete-file-tree dir tree)
71 "Delete file TREE from DIR."
75 (string-append dir "/" file))
78 (('directory name (body ...))
79 (for-each (cute loop (scope name) <>) body)
81 (('directory name (? integer? mode) (body ...))
82 (chmod (scope name) #o755) ; make sure it can be entered
83 (for-each (cute loop (scope name) <>) body)
86 (delete-file (scope from)))
88 (delete-file (scope file))))))
90 (define-syntax-rule (with-file-tree dir tree body ...)
93 (make-file-tree dir 'tree))
97 (delete-file-tree dir 'tree))))
99 (define (file-tree-equal? input output)
100 "Return #t if the file trees at INPUT and OUTPUT are equal."
102 (cute string-drop <> (string-length input)))
104 (compose (cut string-append output <>) strip))
106 (and (eq? (stat:type (lstat a)) (stat:type (lstat b)))
107 (case (stat:type (lstat a))
110 (call-with-input-file a get-bytevector-all)
111 (call-with-input-file b get-bytevector-all)))
113 (string=? (readlink a) (readlink b)))
115 (error "what?" (lstat a))))))
117 (file-system-fold (const #t)
118 (lambda (name stat result) ; leaf
120 (file=? name (sibling name))))
121 (lambda (name stat result) ; down
123 (lambda (name stat result) ; up
126 (lambda (name stat errno result)
127 (pk 'error name stat errno)
129 (> (stat:nlink (stat output)) 2)
133 (define (make-random-bytevector n)
134 (let ((bv (make-bytevector n)))
138 (bytevector-u8-set! bv i (random 256))
142 (define (populate-file file size)
143 (call-with-output-file file
145 (put-bytevector p (make-random-bytevector size)))))
148 (file-system-fold (const #t) ; enter?
149 (lambda (file stat result) ; leaf
152 (lambda (dir stat result) ; up
161 ;; An output directory under $top_builddir.
162 (string-append (dirname (search-path %load-path "pre-inst-env"))
163 "/test-nar-" (number->string (getpid))))
167 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
169 (define (random-text)
170 (number->string (random (expt 2 256) %seed) 16))
172 (define-syntax-rule (let/ec k exp...)
173 ;; This one appeared in Guile 2.0.9, so provide a copy here.
174 (let ((tag (make-prompt-tag)))
175 (call-with-prompt tag
177 (let ((k (lambda args
178 (apply abort-to-prompt tag args))))
181 (apply values args)))))
186 (test-assert "write-file supports non-file output ports"
187 (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
189 (output (%make-void-port "w")))
190 (write-file input output)
193 (test-assert "write-file + restore-file"
194 (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
197 (nar (string-append output ".nar")))
201 (call-with-output-file nar
202 (cut write-file input <>))
203 (call-with-input-file nar
204 (cut restore-file <> output))
205 (file-tree-equal? input output))
207 (false-if-exception (delete-file nar))
208 (false-if-exception (rm-rf output))))))
210 (test-assert "write-file + restore-file with symlinks"
211 (let ((input (string-append %test-dir ".input")))
216 (with-file-tree input
218 (("reg") ("exe" #o777) ("sym" -> "reg")))
219 (let* ((output %test-dir)
220 (nar (string-append output ".nar")))
224 (call-with-output-file nar
225 (cut write-file input <>))
226 (call-with-input-file nar
227 (cut restore-file <> output))
228 (file-tree-equal? input output))
230 (false-if-exception (delete-file nar))
231 (false-if-exception (rm-rf output)))))))
235 ;; 'restore-file-set' depends on 'open-sha256-input-port', which in turn
236 ;; relies on a Guile 2.0.10+ feature.
237 (test-skip (if (false-if-exception
238 (open-sha256-input-port (%make-void-port "r")))
242 (test-assert "restore-file-set (signed, valid)"
244 (let* ((texts (unfold (cut >= <> 10)
245 (lambda _ (random-text))
248 (files (map (cut add-text-to-store store "text" <>) texts))
249 (dump (call-with-bytevector-output-port
250 (cut export-paths store files <>))))
251 (delete-paths store files)
252 (and (every (negate file-exists?) files)
253 (let* ((source (open-bytevector-input-port dump))
254 (imported (restore-file-set source)))
255 (and (equal? imported files)
256 (every (lambda (file)
257 (and (file-exists? file)
258 (valid-path? store file)))
262 (call-with-input-file file
266 (test-assert "restore-file-set (missing signature)"
269 (let* ((file (add-text-to-store store "foo" "Hello, world!"))
270 (dump (call-with-bytevector-output-port
271 (cute export-paths store (list file) <>
273 (delete-paths store (list file))
274 (and (not (file-exists? file))
275 (let ((source (open-bytevector-input-port dump)))
276 (guard (c ((nar-signature-error? c)
277 (let ((message (condition-message c))
278 (port (nar-error-port c)))
280 (and (string-match "lacks.*signature" message)
281 (string=? file (nar-error-file c))
282 (eq? source port))))))
283 (restore-file-set source))
286 (test-assert "restore-file-set (corrupt)"
289 (let* ((file (add-text-to-store store "foo"
291 (dump (call-with-bytevector-output-port
292 (cute export-paths store (list file) <>))))
293 (delete-paths store (list file))
295 ;; Flip a byte in the file contents.
297 (byte (bytevector-u8-ref dump index)))
298 (bytevector-u8-set! dump index (logxor #xff byte)))
300 (and (not (file-exists? file))
301 (let ((source (open-bytevector-input-port dump)))
302 (guard (c ((nar-invalid-hash-error? c)
303 (let ((message (condition-message c))
304 (port (nar-error-port c)))
306 (and (string-contains message "hash")
307 (string=? file (nar-error-file c))
308 (eq? source port))))))
309 (restore-file-set source))
315 (exit (= (test-runner-fail-count (test-runner-current)) 0))
318 ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)