| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 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)
|
| 27 | #:select (base32))
|
| 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 binary-ports)
|
| 39 | #:use-module (ice-9 ftw)
|
| 40 | #:use-module (ice-9 regex)
|
| 41 | #:use-module ((ice-9 control) #:select (let/ec))
|
| 42 | #:use-module (ice-9 match))
|
| 43 |
|
| 44 | |
| 45 |
|
| 46 |
|
| 47 | |
| 48 | |
| 49 | |
| 50 |
|
| 51 | (define (random-file-size)
|
| 52 | (define %average (* 1024 512)) |
| 53 | (define %stddev (* 1024 64)) |
| 54 | (inexact->exact
|
| 55 | (max 0 (round (+ %average (* %stddev (random:normal)))))))
|
| 56 |
|
| 57 | (define (make-file-tree dir tree)
|
| 58 | "Make file system TREE at DIR."
|
| 59 | (let loop ((dir dir)
|
| 60 | (tree tree))
|
| 61 | (define (scope file)
|
| 62 | (string-append dir "/" file))
|
| 63 |
|
| 64 | (match tree
|
| 65 | (('directory name (body ...))
|
| 66 | (mkdir (scope name))
|
| 67 | (for-each (cute loop (scope name) <>) body))
|
| 68 | (('directory name (? integer? mode) (body ...))
|
| 69 | (mkdir (scope name))
|
| 70 | (for-each (cute loop (scope name) <>) body)
|
| 71 | (chmod (scope name) mode))
|
| 72 | ((file)
|
| 73 | (populate-file (scope file) (random-file-size)))
|
| 74 | ((file (? integer? mode))
|
| 75 | (populate-file (scope file) (random-file-size))
|
| 76 | (chmod (scope file) mode))
|
| 77 | ((from '-> to)
|
| 78 | (symlink to (scope from))))))
|
| 79 |
|
| 80 | (define (delete-file-tree dir tree)
|
| 81 | "Delete file TREE from DIR."
|
| 82 | (let loop ((dir dir)
|
| 83 | (tree tree))
|
| 84 | (define (scope file)
|
| 85 | (string-append dir "/" file))
|
| 86 |
|
| 87 | (match tree
|
| 88 | (('directory name (body ...))
|
| 89 | (for-each (cute loop (scope name) <>) body)
|
| 90 | (rmdir (scope name)))
|
| 91 | (('directory name (? integer? mode) (body ...))
|
| 92 | (chmod (scope name) #o755) |
| 93 | (for-each (cute loop (scope name) <>) body)
|
| 94 | (rmdir (scope name)))
|
| 95 | ((from '-> _)
|
| 96 | (delete-file (scope from)))
|
| 97 | ((file _ ...)
|
| 98 | (delete-file (scope file))))))
|
| 99 |
|
| 100 | (define-syntax-rule (with-file-tree dir tree body ...)
|
| 101 | (dynamic-wind
|
| 102 | (lambda ()
|
| 103 | (make-file-tree dir 'tree))
|
| 104 | (lambda ()
|
| 105 | body ...)
|
| 106 | (lambda ()
|
| 107 | (delete-file-tree dir 'tree))))
|
| 108 |
|
| 109 | (define (file-tree-equal? input output)
|
| 110 | "Return #t if the file trees at INPUT and OUTPUT are equal."
|
| 111 | (define strip
|
| 112 | (cute string-drop <> (string-length input)))
|
| 113 | (define sibling
|
| 114 | (compose (cut string-append output <>) strip))
|
| 115 |
|
| 116 | (file-system-fold (const #t)
|
| 117 | (lambda (name stat result) |
| 118 | (and result
|
| 119 | (file=? name (sibling name))))
|
| 120 | (lambda (name stat result) |
| 121 | result)
|
| 122 | (lambda (name stat result) |
| 123 | result)
|
| 124 | (const #f) |
| 125 | (lambda (name stat errno result)
|
| 126 | (pk 'error name stat errno)
|
| 127 | #f)
|
| 128 | #t |
| 129 | input
|
| 130 | lstat))
|
| 131 |
|
| 132 | (define (populate-file file size)
|
| 133 | (call-with-output-file file
|
| 134 | (lambda (p)
|
| 135 | (put-bytevector p (random-bytevector size)))))
|
| 136 |
|
| 137 | (define (rm-rf dir)
|
| 138 | (file-system-fold (const #t) |
| 139 | (lambda (file stat result) |
| 140 | (unless (eq? 'symlink (stat:type stat))
|
| 141 | (chmod file #o644))
|
| 142 | (delete-file file))
|
| 143 | (lambda (dir stat result) |
| 144 | (chmod dir #o755))
|
| 145 | (lambda (dir stat result) |
| 146 | (rmdir dir))
|
| 147 | (const #t) |
| 148 | (const #t) |
| 149 | #t
|
| 150 | dir
|
| 151 | lstat))
|
| 152 |
|
| 153 | (define %test-dir
|
| 154 | |
| 155 | (string-append (dirname (search-path %load-path "pre-inst-env"))
|
| 156 | "/test-nar-" (number->string (getpid))))
|
| 157 |
|
| 158 |
|
| 159 | (test-begin "nar")
|
| 160 |
|
| 161 | (test-assert "write-file-tree + restore-file"
|
| 162 | (let* ((file1 (search-path %load-path "guix.scm"))
|
| 163 | (file2 (search-path %load-path "guix/base32.scm"))
|
| 164 | (file3 "#!/bin/something")
|
| 165 | (output (string-append %test-dir "/output")))
|
| 166 | (dynamic-wind
|
| 167 | (lambda () #t)
|
| 168 | (lambda ()
|
| 169 | (define-values (port get-bytevector)
|
| 170 | (open-bytevector-output-port))
|
| 171 | (write-file-tree "root" port
|
| 172 | #:file-type+size
|
| 173 | (match-lambda
|
| 174 | ("root"
|
| 175 | (values 'directory 0))
|
| 176 | ("root/foo"
|
| 177 | (values 'regular (stat:size (stat file1))))
|
| 178 | ("root/lnk"
|
| 179 | (values 'symlink 0))
|
| 180 | ("root/dir"
|
| 181 | (values 'directory 0))
|
| 182 | ("root/dir/bar"
|
| 183 | (values 'regular (stat:size (stat file2))))
|
| 184 | ("root/dir/exe"
|
| 185 | (values 'executable (string-length file3))))
|
| 186 | #:file-port
|
| 187 | (match-lambda
|
| 188 | ("root/foo" (open-input-file file1))
|
| 189 | ("root/dir/bar" (open-input-file file2))
|
| 190 | ("root/dir/exe" (open-input-string file3)))
|
| 191 | #:symlink-target
|
| 192 | (match-lambda
|
| 193 | ("root/lnk" "foo"))
|
| 194 | #:directory-entries
|
| 195 | (match-lambda
|
| 196 | ("root" '("foo" "dir" "lnk"))
|
| 197 | ("root/dir" '("bar" "exe"))))
|
| 198 | (close-port port)
|
| 199 |
|
| 200 | (rm-rf %test-dir)
|
| 201 | (mkdir %test-dir)
|
| 202 | (restore-file (open-bytevector-input-port (get-bytevector))
|
| 203 | output)
|
| 204 | (and (file=? (string-append output "/foo") file1)
|
| 205 | (string=? (readlink (string-append output "/lnk"))
|
| 206 | "foo")
|
| 207 | (file=? (string-append output "/dir/bar") file2)
|
| 208 | (string=? (call-with-input-file (string-append output "/dir/exe")
|
| 209 | get-string-all)
|
| 210 | file3)
|
| 211 | (> (logand (stat:mode (lstat (string-append output "/dir/exe")))
|
| 212 | #o100)
|
| 213 | 0)
|
| 214 | (equal? '("." ".." "bar" "exe")
|
| 215 | (scandir (string-append output "/dir")))
|
| 216 | (equal? '("." ".." "dir" "foo" "lnk")
|
| 217 | (scandir output))))
|
| 218 | (lambda ()
|
| 219 | (false-if-exception (rm-rf %test-dir))))))
|
| 220 |
|
| 221 | (test-equal "write-file-tree + fold-archive"
|
| 222 | '(("R" directory #f)
|
| 223 | ("R/dir" directory #f)
|
| 224 | ("R/dir/exe" executable "1234")
|
| 225 | ("R/dir" directory-complete #f)
|
| 226 | ("R/foo" regular "abcdefg")
|
| 227 | ("R/lnk" symlink "foo")
|
| 228 | ("R" directory-complete #f))
|
| 229 |
|
| 230 | (let ()
|
| 231 | (define-values (port get-bytevector)
|
| 232 | (open-bytevector-output-port))
|
| 233 | (write-file-tree "root" port
|
| 234 | #:file-type+size
|
| 235 | (match-lambda
|
| 236 | ("root"
|
| 237 | (values 'directory 0))
|
| 238 | ("root/foo"
|
| 239 | (values 'regular 7))
|
| 240 | ("root/lnk"
|
| 241 | (values 'symlink 0))
|
| 242 | ("root/dir"
|
| 243 | (values 'directory 0))
|
| 244 | ("root/dir/exe"
|
| 245 | (values 'executable 4)))
|
| 246 | #:file-port
|
| 247 | (match-lambda
|
| 248 | ("root/foo" (open-input-string "abcdefg"))
|
| 249 | ("root/dir/exe" (open-input-string "1234")))
|
| 250 | #:symlink-target
|
| 251 | (match-lambda
|
| 252 | ("root/lnk" "foo"))
|
| 253 | #:directory-entries
|
| 254 | (match-lambda
|
| 255 | ("root" '("foo" "dir" "lnk"))
|
| 256 | ("root/dir" '("exe"))))
|
| 257 | (close-port port)
|
| 258 |
|
| 259 | (reverse
|
| 260 | (fold-archive (lambda (file type contents result)
|
| 261 | (let ((contents (if (memq type '(regular executable))
|
| 262 | (utf8->string
|
| 263 | (get-bytevector-n (car contents)
|
| 264 | (cdr contents)))
|
| 265 | contents)))
|
| 266 | (cons `(,file ,type ,contents)
|
| 267 | result)))
|
| 268 | '()
|
| 269 | (open-bytevector-input-port (get-bytevector))
|
| 270 | "R"))))
|
| 271 |
|
| 272 | (define (call-with-tree-port tree proc)
|
| 273 | (let ((bv (call-with-output-bytevector
|
| 274 | (lambda (port)
|
| 275 | (apply write-file-tree "root" port tree)))))
|
| 276 | (call-with-input-bytevector bv proc)))
|
| 277 |
|
| 278 | (define (port-bad-nar? port)
|
| 279 | (guard (c ((nar-error? c)
|
| 280 | (pk 'nar-error c)
|
| 281 | #t))
|
| 282 | (dynamic-wind
|
| 283 | (const #t)
|
| 284 | (lambda ()
|
| 285 | (rm-rf %test-dir)
|
| 286 | (mkdir %test-dir)
|
| 287 | (restore-file port (string-append %test-dir "/foo")))
|
| 288 | (lambda ()
|
| 289 | (false-if-exception (rm-rf %test-dir))))))
|
| 290 |
|
| 291 | (test-assert "write-file-tree + fold-archive, unsorted directory entries"
|
| 292 | (let* ((unsorted-tree (list #:file-type+size
|
| 293 | (match-lambda
|
| 294 | ("root" (values 'directory 0))
|
| 295 | ("root/c" (values 'regular 1))
|
| 296 | ("root/b" (values 'regular 1))
|
| 297 | ("root/a" (values 'regular 1)))
|
| 298 | #:file-port
|
| 299 | (match-lambda
|
| 300 | ("root/c" (open-input-string "c"))
|
| 301 | ("root/b" (open-input-string "b"))
|
| 302 | ("root/a" (open-input-string "a")))
|
| 303 | #:directory-entries
|
| 304 | (match-lambda
|
| 305 | ("root" '("c" "b" "a")))
|
| 306 | |
| 307 | |
| 308 | #:postprocess-entries identity)))
|
| 309 | (call-with-tree-port unsorted-tree port-bad-nar?)))
|
| 310 |
|
| 311 | (test-assert "write-file-tree + fold-archive, duplicate directory entries"
|
| 312 | (let* ((duplicates-tree (list #:file-type+size
|
| 313 | (match-lambda
|
| 314 | ("root" (values 'directory 0))
|
| 315 | ("root/a" (values 'regular 1)))
|
| 316 | #:file-port
|
| 317 | (match-lambda
|
| 318 | ("root/a" (open-input-string "a")))
|
| 319 | #:directory-entries
|
| 320 | (match-lambda
|
| 321 | ("root" '("a" "a" "a")))
|
| 322 | |
| 323 | |
| 324 | #:postprocess-entries identity)))
|
| 325 | (call-with-tree-port duplicates-tree port-bad-nar?)))
|
| 326 |
|
| 327 | (test-assert "write-file-tree + fold-archive, invalid directory entries"
|
| 328 | (let* ((invalid-names (list "." ".." "" "../" "./"
|
| 329 | "/" "/bin/shh" "../../etc/passwwwd"
|
| 330 | (string #\nul) (string #\. #\/ #\nul)))
|
| 331 | (invalid-name-trees (map (lambda (name)
|
| 332 | (list #:file-type+size
|
| 333 | (match-lambda
|
| 334 | ("root" (values 'directory 0))
|
| 335 | (_ (values 'regular 1)))
|
| 336 | #:file-port
|
| 337 | (lambda (_)
|
| 338 | (open-input-string "a"))
|
| 339 | #:directory-entries
|
| 340 | (match-lambda
|
| 341 | ("root" (list name)))
|
| 342 | |
| 343 | |
| 344 | #:postprocess-entries identity))
|
| 345 | invalid-names)))
|
| 346 | (every (lambda (tree)
|
| 347 | (call-with-tree-port tree port-bad-nar?))
|
| 348 | invalid-name-trees)))
|
| 349 |
|
| 350 | (test-equal "write-file-tree + fold-archive, flat file"
|
| 351 | '(("R" regular "abcdefg"))
|
| 352 |
|
| 353 | (let ()
|
| 354 | (define-values (port get-bytevector)
|
| 355 | (open-bytevector-output-port))
|
| 356 | (write-file-tree "root" port
|
| 357 | #:file-type+size
|
| 358 | (match-lambda
|
| 359 | ("root" (values 'regular 7)))
|
| 360 | #:file-port
|
| 361 | (match-lambda
|
| 362 | ("root" (open-input-string "abcdefg"))))
|
| 363 | (close-port port)
|
| 364 |
|
| 365 | (reverse
|
| 366 | (fold-archive (lambda (file type contents result)
|
| 367 | (let ((contents (utf8->string
|
| 368 | (get-bytevector-n (car contents)
|
| 369 | (cdr contents)))))
|
| 370 | (cons `(,file ,type ,contents) result)))
|
| 371 | '()
|
| 372 | (open-bytevector-input-port (get-bytevector))
|
| 373 | "R"))))
|
| 374 |
|
| 375 | (test-assert "write-file supports non-file output ports"
|
| 376 | (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
|
| 377 | "/guix"))
|
| 378 | (output (%make-void-port "w")))
|
| 379 | (write-file input output)
|
| 380 | #t))
|
| 381 |
|
| 382 | (test-equal "write-file puts file in C locale collation order"
|
| 383 | (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
|
| 384 | (let ((input (string-append %test-dir ".input")))
|
| 385 | (dynamic-wind
|
| 386 | (lambda ()
|
| 387 | (define (touch file)
|
| 388 | (call-with-output-file (string-append input "/" file)
|
| 389 | (const #t)))
|
| 390 |
|
| 391 | (mkdir input)
|
| 392 | (touch "B")
|
| 393 | (touch "Z")
|
| 394 | (touch "a")
|
| 395 | (symlink "B" (string-append input "/z")))
|
| 396 | (lambda ()
|
| 397 | (let-values (((port get-hash) (open-sha256-port)))
|
| 398 | (write-file input port)
|
| 399 | (close-port port)
|
| 400 | (get-hash)))
|
| 401 | (lambda ()
|
| 402 | (rm-rf input)))))
|
| 403 |
|
| 404 | (test-equal "restore-file with incomplete input"
|
| 405 | (string-append %test-dir "/foo")
|
| 406 | (let ((port (open-bytevector-input-port #vu8(1 2 3))))
|
| 407 | (guard (c ((nar-error? c)
|
| 408 | (and (eq? port (nar-error-port c))
|
| 409 | (nar-error-file c))))
|
| 410 | (restore-file port (string-append %test-dir "/foo"))
|
| 411 | #f)))
|
| 412 |
|
| 413 | (test-assert "write-file + restore-file"
|
| 414 | (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
|
| 415 | "/guix"))
|
| 416 | (output %test-dir)
|
| 417 | (nar (string-append output ".nar")))
|
| 418 | (dynamic-wind
|
| 419 | (lambda () #t)
|
| 420 | (lambda ()
|
| 421 | (call-with-output-file nar
|
| 422 | (cut write-file input <>))
|
| 423 | (call-with-input-file nar
|
| 424 | (cut restore-file <> output))
|
| 425 | (file-tree-equal? input output))
|
| 426 | (lambda ()
|
| 427 | (false-if-exception (delete-file nar))
|
| 428 | (false-if-exception (rm-rf output))))))
|
| 429 |
|
| 430 | (test-assert "write-file + restore-file with symlinks"
|
| 431 | (let ((input (string-append %test-dir ".input")))
|
| 432 | (mkdir input)
|
| 433 | (dynamic-wind
|
| 434 | (const #t)
|
| 435 | (lambda ()
|
| 436 | (with-file-tree input
|
| 437 | (directory "root"
|
| 438 | (("reg") ("exe" #o777) ("sym" -> "reg")))
|
| 439 | (let* ((output %test-dir)
|
| 440 | (nar (string-append output ".nar")))
|
| 441 | (dynamic-wind
|
| 442 | (lambda () #t)
|
| 443 | (lambda ()
|
| 444 | (call-with-output-file nar
|
| 445 | (cut write-file input <>))
|
| 446 | (call-with-input-file nar
|
| 447 | (cut restore-file <> output))
|
| 448 |
|
| 449 | (and (file-tree-equal? input output)
|
| 450 | (every (lambda (file)
|
| 451 | (canonical-file?
|
| 452 | (string-append output "/" file)))
|
| 453 | '("root" "root/reg" "root/exe"))))
|
| 454 | (lambda ()
|
| 455 | (false-if-exception (delete-file nar))
|
| 456 | (false-if-exception (rm-rf output)))))))
|
| 457 | (lambda ()
|
| 458 | (rmdir input)))))
|
| 459 |
|
| 460 | (test-assert "write-file #:select? + restore-file"
|
| 461 | (let ((input (string-append %test-dir ".input")))
|
| 462 | (mkdir input)
|
| 463 | (dynamic-wind
|
| 464 | (const #t)
|
| 465 | (lambda ()
|
| 466 | (with-file-tree input
|
| 467 | (directory "root"
|
| 468 | ((directory "a" (("x") ("y") ("z")))
|
| 469 | ("b") ("c") ("d" -> "b")))
|
| 470 | (let* ((output %test-dir)
|
| 471 | (nar (string-append output ".nar")))
|
| 472 | (dynamic-wind
|
| 473 | (lambda () #t)
|
| 474 | (lambda ()
|
| 475 | (call-with-output-file nar
|
| 476 | (lambda (port)
|
| 477 | (write-file input port
|
| 478 | #:select?
|
| 479 | (lambda (file stat)
|
| 480 | (and (not (string=? (basename file)
|
| 481 | "a"))
|
| 482 | (not (eq? (stat:type stat)
|
| 483 | 'symlink)))))))
|
| 484 | (call-with-input-file nar
|
| 485 | (cut restore-file <> output))
|
| 486 |
|
| 487 | |
| 488 | (and (not (file-exists? (string-append output "/root/a")))
|
| 489 | (file=? (string-append output "/root/b")
|
| 490 | (string-append input "/root/b"))
|
| 491 | (file=? (string-append output "/root/c")
|
| 492 | (string-append input "/root/c"))
|
| 493 | (not (file-exists? (string-append output "/root/d")))))
|
| 494 | (lambda ()
|
| 495 | (false-if-exception (delete-file nar))
|
| 496 | (false-if-exception (rm-rf output)))))))
|
| 497 | (lambda ()
|
| 498 | (rmdir input)))))
|
| 499 |
|
| 500 | (test-eq "restore-file with non-UTF8 locale" |
| 501 | 'encoding-error
|
| 502 | (let* ((file (search-path %load-path "guix.scm"))
|
| 503 | (output (string-append %test-dir "/output"))
|
| 504 | (locale (setlocale LC_ALL "C")))
|
| 505 | (dynamic-wind
|
| 506 | (lambda () #t)
|
| 507 | (lambda ()
|
| 508 | (define-values (port get-bytevector)
|
| 509 | (open-bytevector-output-port))
|
| 510 |
|
| 511 | (write-file-tree "root" port
|
| 512 | #:file-type+size
|
| 513 | (match-lambda
|
| 514 | ("root" (values 'directory 0))
|
| 515 | ("root/λ" (values 'regular 0)))
|
| 516 | #:file-port (const (%make-void-port "r"))
|
| 517 | #:symlink-target (const #f)
|
| 518 | #:directory-entries (const '("λ")))
|
| 519 | (close-port port)
|
| 520 |
|
| 521 | (mkdir %test-dir)
|
| 522 | (catch 'encoding-error
|
| 523 | (lambda ()
|
| 524 | |
| 525 | (restore-file (open-bytevector-input-port (get-bytevector))
|
| 526 | output)
|
| 527 | (scandir output))
|
| 528 | (lambda args
|
| 529 | 'encoding-error)))
|
| 530 | (lambda ()
|
| 531 | (false-if-exception (rm-rf %test-dir))
|
| 532 | (setlocale LC_ALL locale)))))
|
| 533 |
|
| 534 | |
| 535 | (setenv "NIX_STORE" (%store-prefix))
|
| 536 |
|
| 537 | (test-assert "restore-file-set (signed, valid)"
|
| 538 | (with-store store
|
| 539 | (let* ((texts (unfold (cut >= <> 10)
|
| 540 | (lambda _ (random-text))
|
| 541 | 1+
|
| 542 | 0))
|
| 543 | (files (map (cut add-text-to-store store "text" <>) texts))
|
| 544 | (dump (call-with-bytevector-output-port
|
| 545 | (cut export-paths store files <>))))
|
| 546 | (delete-paths store files)
|
| 547 | (and (every (negate file-exists?) files)
|
| 548 | (let* ((source (open-bytevector-input-port dump))
|
| 549 | (imported (restore-file-set source)))
|
| 550 | (and (equal? imported files)
|
| 551 | (every (lambda (file)
|
| 552 | (and (file-exists? file)
|
| 553 | (valid-path? store file)))
|
| 554 | files)
|
| 555 | (equal? texts
|
| 556 | (map (lambda (file)
|
| 557 | (call-with-input-file file
|
| 558 | get-string-all))
|
| 559 | files))
|
| 560 | (every canonical-file? files)))))))
|
| 561 |
|
| 562 | (test-assert "restore-file-set with directories (signed, valid)"
|
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | (with-store store
|
| 568 | |
| 569 | (let* ((text1 (string-concatenate (make-list 200 (random-text))))
|
| 570 | (text2 (string-concatenate (make-list 200 (random-text))))
|
| 571 | (tree `("tree" directory
|
| 572 | ("a" regular (data ,text1))
|
| 573 | ("b" directory
|
| 574 | ("c" regular (data ,text2))
|
| 575 | ("d" regular (data ,text1))))) |
| 576 | (file (add-file-tree-to-store store tree))
|
| 577 | (dump (call-with-bytevector-output-port
|
| 578 | (cute export-paths store (list file) <>))))
|
| 579 | (delete-paths store (list file))
|
| 580 | (and (not (file-exists? file))
|
| 581 | (let* ((source (open-bytevector-input-port dump))
|
| 582 | (imported (restore-file-set source)))
|
| 583 | (and (equal? imported (list file))
|
| 584 | (file-exists? file)
|
| 585 | (valid-path? store file)
|
| 586 | (string=? text1
|
| 587 | (call-with-input-file (string-append file "/a")
|
| 588 | get-string-all))
|
| 589 | (string=? text2
|
| 590 | (call-with-input-file
|
| 591 | (string-append file "/b/c")
|
| 592 | get-string-all))
|
| 593 | (= (stat:ino (stat (string-append file "/a"))) |
| 594 | (stat:ino (stat (string-append file "/b/d"))))
|
| 595 | (every canonical-file?
|
| 596 | (find-files file #:directories? #t))))))))
|
| 597 |
|
| 598 | (test-assert "restore-file-set (missing signature)"
|
| 599 | (let/ec return
|
| 600 | (with-store store
|
| 601 | (let* ((file (add-text-to-store store "foo" (random-text)))
|
| 602 | (dump (call-with-bytevector-output-port
|
| 603 | (cute export-paths store (list file) <>
|
| 604 | #:sign? #f))))
|
| 605 | (delete-paths store (list file))
|
| 606 | (and (not (file-exists? file))
|
| 607 | (let ((source (open-bytevector-input-port dump)))
|
| 608 | (guard (c ((nar-signature-error? c)
|
| 609 | (let ((message (condition-message c))
|
| 610 | (port (nar-error-port c)))
|
| 611 | (return
|
| 612 | (and (string-match "lacks.*signature" message)
|
| 613 | (string=? file (nar-error-file c))
|
| 614 | (eq? source port))))))
|
| 615 | (restore-file-set source))
|
| 616 | #f))))))
|
| 617 |
|
| 618 | (test-assert "restore-file-set (corrupt)"
|
| 619 | (let/ec return
|
| 620 | (with-store store
|
| 621 | (let* ((file (add-text-to-store store "foo"
|
| 622 | (random-text)))
|
| 623 | (dump (call-with-bytevector-output-port
|
| 624 | (cute export-paths store (list file) <>))))
|
| 625 | (delete-paths store (list file))
|
| 626 |
|
| 627 | |
| 628 | (let* ((index 120)
|
| 629 | (byte (bytevector-u8-ref dump index)))
|
| 630 | (bytevector-u8-set! dump index (logxor #xff byte)))
|
| 631 |
|
| 632 | (and (not (file-exists? file))
|
| 633 | (let ((source (open-bytevector-input-port dump)))
|
| 634 | (guard (c ((nar-invalid-hash-error? c)
|
| 635 | (let ((message (condition-message c))
|
| 636 | (port (nar-error-port c)))
|
| 637 | (return
|
| 638 | (and (string-contains message "hash")
|
| 639 | (string=? file (nar-error-file c))
|
| 640 | (eq? source port))))))
|
| 641 | (restore-file-set source))
|
| 642 | #f))))))
|
| 643 |
|
| 644 | (test-end "nar")
|
| 645 |
|
| 646 | |
| 647 | |
| 648 | |