nar.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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/>.
  18. (define-module (test-nar)
  19. #:use-module (guix tests)
  20. #:use-module (guix nar)
  21. #:use-module (guix serialization)
  22. #:use-module (guix store)
  23. #:use-module ((gcrypt hash)
  24. #:select (open-sha256-port open-sha256-input-port))
  25. #:use-module ((guix packages)
  26. #:select (base32))
  27. #:use-module ((guix build utils)
  28. #:select (find-files))
  29. #:use-module (rnrs bytevectors)
  30. #:use-module (rnrs io ports)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-11)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-34)
  35. #:use-module (srfi srfi-35)
  36. #:use-module (srfi srfi-64)
  37. #:use-module (ice-9 ftw)
  38. #:use-module (ice-9 regex)
  39. #:use-module ((ice-9 control) #:select (let/ec))
  40. #:use-module (ice-9 match))
  41. ;; Test the (guix nar) module.
  42. ;;;
  43. ;;; File system testing tools, initially contributed to Guile, then libchop.
  44. ;;;
  45. (define (random-file-size)
  46. (define %average (* 1024 512)) ; 512 KiB
  47. (define %stddev (* 1024 64)) ; 64 KiB
  48. (inexact->exact
  49. (max 0 (round (+ %average (* %stddev (random:normal)))))))
  50. (define (make-file-tree dir tree)
  51. "Make file system TREE at DIR."
  52. (let loop ((dir dir)
  53. (tree tree))
  54. (define (scope file)
  55. (string-append dir "/" file))
  56. (match tree
  57. (('directory name (body ...))
  58. (mkdir (scope name))
  59. (for-each (cute loop (scope name) <>) body))
  60. (('directory name (? integer? mode) (body ...))
  61. (mkdir (scope name))
  62. (for-each (cute loop (scope name) <>) body)
  63. (chmod (scope name) mode))
  64. ((file)
  65. (populate-file (scope file) (random-file-size)))
  66. ((file (? integer? mode))
  67. (populate-file (scope file) (random-file-size))
  68. (chmod (scope file) mode))
  69. ((from '-> to)
  70. (symlink to (scope from))))))
  71. (define (delete-file-tree dir tree)
  72. "Delete file TREE from DIR."
  73. (let loop ((dir dir)
  74. (tree tree))
  75. (define (scope file)
  76. (string-append dir "/" file))
  77. (match tree
  78. (('directory name (body ...))
  79. (for-each (cute loop (scope name) <>) body)
  80. (rmdir (scope name)))
  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)
  84. (rmdir (scope name)))
  85. ((from '-> _)
  86. (delete-file (scope from)))
  87. ((file _ ...)
  88. (delete-file (scope file))))))
  89. (define-syntax-rule (with-file-tree dir tree body ...)
  90. (dynamic-wind
  91. (lambda ()
  92. (make-file-tree dir 'tree))
  93. (lambda ()
  94. body ...)
  95. (lambda ()
  96. (delete-file-tree dir 'tree))))
  97. (define (file-tree-equal? input output)
  98. "Return #t if the file trees at INPUT and OUTPUT are equal."
  99. (define strip
  100. (cute string-drop <> (string-length input)))
  101. (define sibling
  102. (compose (cut string-append output <>) strip))
  103. (file-system-fold (const #t)
  104. (lambda (name stat result) ; leaf
  105. (and result
  106. (file=? name (sibling name))))
  107. (lambda (name stat result) ; down
  108. result)
  109. (lambda (name stat result) ; up
  110. result)
  111. (const #f) ; skip
  112. (lambda (name stat errno result)
  113. (pk 'error name stat errno)
  114. #f)
  115. #t ; result
  116. input
  117. lstat))
  118. (define (populate-file file size)
  119. (call-with-output-file file
  120. (lambda (p)
  121. (put-bytevector p (random-bytevector size)))))
  122. (define (rm-rf dir)
  123. (file-system-fold (const #t) ; enter?
  124. (lambda (file stat result) ; leaf
  125. (delete-file file))
  126. (const #t) ; down
  127. (lambda (dir stat result) ; up
  128. (rmdir dir))
  129. (const #t) ; skip
  130. (const #t) ; error
  131. #t
  132. dir
  133. lstat))
  134. (define %test-dir
  135. ;; An output directory under $top_builddir.
  136. (string-append (dirname (search-path %load-path "pre-inst-env"))
  137. "/test-nar-" (number->string (getpid))))
  138. (test-begin "nar")
  139. (test-assert "write-file-tree + restore-file"
  140. (let* ((file1 (search-path %load-path "guix.scm"))
  141. (file2 (search-path %load-path "guix/base32.scm"))
  142. (file3 "#!/bin/something")
  143. (output (string-append %test-dir "/output")))
  144. (dynamic-wind
  145. (lambda () #t)
  146. (lambda ()
  147. (define-values (port get-bytevector)
  148. (open-bytevector-output-port))
  149. (write-file-tree "root" port
  150. #:file-type+size
  151. (match-lambda
  152. ("root"
  153. (values 'directory 0))
  154. ("root/foo"
  155. (values 'regular (stat:size (stat file1))))
  156. ("root/lnk"
  157. (values 'symlink 0))
  158. ("root/dir"
  159. (values 'directory 0))
  160. ("root/dir/bar"
  161. (values 'regular (stat:size (stat file2))))
  162. ("root/dir/exe"
  163. (values 'executable (string-length file3))))
  164. #:file-port
  165. (match-lambda
  166. ("root/foo" (open-input-file file1))
  167. ("root/dir/bar" (open-input-file file2))
  168. ("root/dir/exe" (open-input-string file3)))
  169. #:symlink-target
  170. (match-lambda
  171. ("root/lnk" "foo"))
  172. #:directory-entries
  173. (match-lambda
  174. ("root" '("foo" "dir" "lnk"))
  175. ("root/dir" '("bar" "exe"))))
  176. (close-port port)
  177. (rm-rf %test-dir)
  178. (mkdir %test-dir)
  179. (restore-file (open-bytevector-input-port (get-bytevector))
  180. output)
  181. (and (file=? (string-append output "/foo") file1)
  182. (string=? (readlink (string-append output "/lnk"))
  183. "foo")
  184. (file=? (string-append output "/dir/bar") file2)
  185. (string=? (call-with-input-file (string-append output "/dir/exe")
  186. get-string-all)
  187. file3)
  188. (> (logand (stat:mode (lstat (string-append output "/dir/exe")))
  189. #o100)
  190. 0)
  191. (equal? '("." ".." "bar" "exe")
  192. (scandir (string-append output "/dir")))
  193. (equal? '("." ".." "dir" "foo" "lnk")
  194. (scandir output))))
  195. (lambda ()
  196. (false-if-exception (rm-rf %test-dir))))))
  197. (test-equal "write-file-tree + fold-archive"
  198. '(("R" directory #f)
  199. ("R/dir" directory #f)
  200. ("R/dir/exe" executable "1234")
  201. ("R/foo" regular "abcdefg")
  202. ("R/lnk" symlink "foo"))
  203. (let ()
  204. (define-values (port get-bytevector)
  205. (open-bytevector-output-port))
  206. (write-file-tree "root" port
  207. #:file-type+size
  208. (match-lambda
  209. ("root"
  210. (values 'directory 0))
  211. ("root/foo"
  212. (values 'regular 7))
  213. ("root/lnk"
  214. (values 'symlink 0))
  215. ("root/dir"
  216. (values 'directory 0))
  217. ("root/dir/exe"
  218. (values 'executable 4)))
  219. #:file-port
  220. (match-lambda
  221. ("root/foo" (open-input-string "abcdefg"))
  222. ("root/dir/exe" (open-input-string "1234")))
  223. #:symlink-target
  224. (match-lambda
  225. ("root/lnk" "foo"))
  226. #:directory-entries
  227. (match-lambda
  228. ("root" '("foo" "dir" "lnk"))
  229. ("root/dir" '("exe"))))
  230. (close-port port)
  231. (reverse
  232. (fold-archive (lambda (file type contents result)
  233. (let ((contents (if (memq type '(regular executable))
  234. (utf8->string
  235. (get-bytevector-n (car contents)
  236. (cdr contents)))
  237. contents)))
  238. (cons `(,file ,type ,contents)
  239. result)))
  240. '()
  241. (open-bytevector-input-port (get-bytevector))
  242. "R"))))
  243. (test-equal "write-file-tree + fold-archive, flat file"
  244. '(("R" regular "abcdefg"))
  245. (let ()
  246. (define-values (port get-bytevector)
  247. (open-bytevector-output-port))
  248. (write-file-tree "root" port
  249. #:file-type+size
  250. (match-lambda
  251. ("root" (values 'regular 7)))
  252. #:file-port
  253. (match-lambda
  254. ("root" (open-input-string "abcdefg"))))
  255. (close-port port)
  256. (reverse
  257. (fold-archive (lambda (file type contents result)
  258. (let ((contents (utf8->string
  259. (get-bytevector-n (car contents)
  260. (cdr contents)))))
  261. (cons `(,file ,type ,contents) result)))
  262. '()
  263. (open-bytevector-input-port (get-bytevector))
  264. "R"))))
  265. (test-assert "write-file supports non-file output ports"
  266. (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
  267. "/guix"))
  268. (output (%make-void-port "w")))
  269. (write-file input output)
  270. #t))
  271. (test-equal "write-file puts file in C locale collation order"
  272. (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
  273. (let ((input (string-append %test-dir ".input")))
  274. (dynamic-wind
  275. (lambda ()
  276. (define (touch file)
  277. (call-with-output-file (string-append input "/" file)
  278. (const #t)))
  279. (mkdir input)
  280. (touch "B")
  281. (touch "Z")
  282. (touch "a")
  283. (symlink "B" (string-append input "/z")))
  284. (lambda ()
  285. (let-values (((port get-hash) (open-sha256-port)))
  286. (write-file input port)
  287. (close-port port)
  288. (get-hash)))
  289. (lambda ()
  290. (rm-rf input)))))
  291. (test-equal "restore-file with incomplete input"
  292. (string-append %test-dir "/foo")
  293. (let ((port (open-bytevector-input-port #vu8(1 2 3))))
  294. (guard (c ((nar-error? c)
  295. (and (eq? port (nar-error-port c))
  296. (nar-error-file c))))
  297. (restore-file port (string-append %test-dir "/foo"))
  298. #f)))
  299. (test-assert "write-file + restore-file"
  300. (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
  301. "/guix"))
  302. (output %test-dir)
  303. (nar (string-append output ".nar")))
  304. (dynamic-wind
  305. (lambda () #t)
  306. (lambda ()
  307. (call-with-output-file nar
  308. (cut write-file input <>))
  309. (call-with-input-file nar
  310. (cut restore-file <> output))
  311. (file-tree-equal? input output))
  312. (lambda ()
  313. (false-if-exception (delete-file nar))
  314. (false-if-exception (rm-rf output))))))
  315. (test-assert "write-file + restore-file with symlinks"
  316. (let ((input (string-append %test-dir ".input")))
  317. (mkdir input)
  318. (dynamic-wind
  319. (const #t)
  320. (lambda ()
  321. (with-file-tree input
  322. (directory "root"
  323. (("reg") ("exe" #o777) ("sym" -> "reg")))
  324. (let* ((output %test-dir)
  325. (nar (string-append output ".nar")))
  326. (dynamic-wind
  327. (lambda () #t)
  328. (lambda ()
  329. (call-with-output-file nar
  330. (cut write-file input <>))
  331. (call-with-input-file nar
  332. (cut restore-file <> output))
  333. (file-tree-equal? input output))
  334. (lambda ()
  335. (false-if-exception (delete-file nar))
  336. (false-if-exception (rm-rf output)))))))
  337. (lambda ()
  338. (rmdir input)))))
  339. (test-assert "write-file #:select? + restore-file"
  340. (let ((input (string-append %test-dir ".input")))
  341. (mkdir input)
  342. (dynamic-wind
  343. (const #t)
  344. (lambda ()
  345. (with-file-tree input
  346. (directory "root"
  347. ((directory "a" (("x") ("y") ("z")))
  348. ("b") ("c") ("d" -> "b")))
  349. (let* ((output %test-dir)
  350. (nar (string-append output ".nar")))
  351. (dynamic-wind
  352. (lambda () #t)
  353. (lambda ()
  354. (call-with-output-file nar
  355. (lambda (port)
  356. (write-file input port
  357. #:select?
  358. (lambda (file stat)
  359. (and (not (string=? (basename file)
  360. "a"))
  361. (not (eq? (stat:type stat)
  362. 'symlink)))))))
  363. (call-with-input-file nar
  364. (cut restore-file <> output))
  365. ;; Make sure "a" and "d" have been filtered out.
  366. (and (not (file-exists? (string-append output "/root/a")))
  367. (file=? (string-append output "/root/b")
  368. (string-append input "/root/b"))
  369. (file=? (string-append output "/root/c")
  370. (string-append input "/root/c"))
  371. (not (file-exists? (string-append output "/root/d")))))
  372. (lambda ()
  373. (false-if-exception (delete-file nar))
  374. (false-if-exception (rm-rf output)))))))
  375. (lambda ()
  376. (rmdir input)))))
  377. (test-eq "restore-file with non-UTF8 locale" ;<https://bugs.gnu.org/33603>
  378. 'encoding-error
  379. (let* ((file (search-path %load-path "guix.scm"))
  380. (output (string-append %test-dir "/output"))
  381. (locale (setlocale LC_ALL "C")))
  382. (dynamic-wind
  383. (lambda () #t)
  384. (lambda ()
  385. (define-values (port get-bytevector)
  386. (open-bytevector-output-port))
  387. (write-file-tree "root" port
  388. #:file-type+size
  389. (match-lambda
  390. ("root" (values 'directory 0))
  391. ("root/λ" (values 'regular 0)))
  392. #:file-port (const (%make-void-port "r"))
  393. #:symlink-target (const #f)
  394. #:directory-entries (const '("λ")))
  395. (close-port port)
  396. (mkdir %test-dir)
  397. (catch 'encoding-error
  398. (lambda ()
  399. ;; This show throw to 'encoding-error.
  400. (restore-file (open-bytevector-input-port (get-bytevector))
  401. output)
  402. (scandir output))
  403. (lambda args
  404. 'encoding-error)))
  405. (lambda ()
  406. (false-if-exception (rm-rf %test-dir))
  407. (setlocale LC_ALL locale)))))
  408. (test-assert "restore-file-set (signed, valid)"
  409. (with-store store
  410. (let* ((texts (unfold (cut >= <> 10)
  411. (lambda _ (random-text))
  412. 1+
  413. 0))
  414. (files (map (cut add-text-to-store store "text" <>) texts))
  415. (dump (call-with-bytevector-output-port
  416. (cut export-paths store files <>))))
  417. (delete-paths store files)
  418. (and (every (negate file-exists?) files)
  419. (let* ((source (open-bytevector-input-port dump))
  420. (imported (restore-file-set source)))
  421. (and (equal? imported files)
  422. (every (lambda (file)
  423. (and (file-exists? file)
  424. (valid-path? store file)))
  425. files)
  426. (equal? texts
  427. (map (lambda (file)
  428. (call-with-input-file file
  429. get-string-all))
  430. files))
  431. (every canonical-file? files)))))))
  432. (test-assert "restore-file-set with directories (signed, valid)"
  433. ;; <https://bugs.gnu.org/33361> describes a bug whereby directories
  434. ;; containing files subject to deduplication were not canonicalized--i.e.,
  435. ;; their mtime and permissions were not reset. Ensure that this bug is
  436. ;; gone.
  437. (with-store store
  438. (let* ((text1 (random-text))
  439. (text2 (random-text))
  440. (tree `("tree" directory
  441. ("a" regular (data ,text1))
  442. ("b" directory
  443. ("c" regular (data ,text2))
  444. ("d" regular (data ,text1))))) ;duplicate
  445. (file (add-file-tree-to-store store tree))
  446. (dump (call-with-bytevector-output-port
  447. (cute export-paths store (list file) <>))))
  448. (delete-paths store (list file))
  449. (and (not (file-exists? file))
  450. (let* ((source (open-bytevector-input-port dump))
  451. (imported (restore-file-set source)))
  452. (and (equal? imported (list file))
  453. (file-exists? file)
  454. (valid-path? store file)
  455. (string=? text1
  456. (call-with-input-file (string-append file "/a")
  457. get-string-all))
  458. (string=? text2
  459. (call-with-input-file
  460. (string-append file "/b/c")
  461. get-string-all))
  462. (= (stat:ino (stat (string-append file "/a"))) ;deduplication
  463. (stat:ino (stat (string-append file "/b/d"))))
  464. (every canonical-file?
  465. (find-files file #:directories? #t))))))))
  466. (test-assert "restore-file-set (missing signature)"
  467. (let/ec return
  468. (with-store store
  469. (let* ((file (add-text-to-store store "foo" (random-text)))
  470. (dump (call-with-bytevector-output-port
  471. (cute export-paths store (list file) <>
  472. #:sign? #f))))
  473. (delete-paths store (list file))
  474. (and (not (file-exists? file))
  475. (let ((source (open-bytevector-input-port dump)))
  476. (guard (c ((nar-signature-error? c)
  477. (let ((message (condition-message c))
  478. (port (nar-error-port c)))
  479. (return
  480. (and (string-match "lacks.*signature" message)
  481. (string=? file (nar-error-file c))
  482. (eq? source port))))))
  483. (restore-file-set source))
  484. #f))))))
  485. (test-assert "restore-file-set (corrupt)"
  486. (let/ec return
  487. (with-store store
  488. (let* ((file (add-text-to-store store "foo"
  489. (random-text)))
  490. (dump (call-with-bytevector-output-port
  491. (cute export-paths store (list file) <>))))
  492. (delete-paths store (list file))
  493. ;; Flip a byte in the file contents.
  494. (let* ((index 120)
  495. (byte (bytevector-u8-ref dump index)))
  496. (bytevector-u8-set! dump index (logxor #xff byte)))
  497. (and (not (file-exists? file))
  498. (let ((source (open-bytevector-input-port dump)))
  499. (guard (c ((nar-invalid-hash-error? c)
  500. (let ((message (condition-message c))
  501. (port (nar-error-port c)))
  502. (return
  503. (and (string-contains message "hash")
  504. (string=? file (nar-error-file c))
  505. (eq? source port))))))
  506. (restore-file-set source))
  507. #f))))))
  508. (test-end "nar")
  509. ;;; Local Variables:
  510. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  511. ;;; End: