nar.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2021 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. (unless (eq? 'symlink (stat:type stat))
  126. (chmod file #o644))
  127. (delete-file file))
  128. (lambda (dir stat result) ; down
  129. (chmod dir #o755))
  130. (lambda (dir stat result) ; up
  131. (rmdir dir))
  132. (const #t) ; skip
  133. (const #t) ; error
  134. #t
  135. dir
  136. lstat))
  137. (define %test-dir
  138. ;; An output directory under $top_builddir.
  139. (string-append (dirname (search-path %load-path "pre-inst-env"))
  140. "/test-nar-" (number->string (getpid))))
  141. (test-begin "nar")
  142. (test-assert "write-file-tree + restore-file"
  143. (let* ((file1 (search-path %load-path "guix.scm"))
  144. (file2 (search-path %load-path "guix/base32.scm"))
  145. (file3 "#!/bin/something")
  146. (output (string-append %test-dir "/output")))
  147. (dynamic-wind
  148. (lambda () #t)
  149. (lambda ()
  150. (define-values (port get-bytevector)
  151. (open-bytevector-output-port))
  152. (write-file-tree "root" port
  153. #:file-type+size
  154. (match-lambda
  155. ("root"
  156. (values 'directory 0))
  157. ("root/foo"
  158. (values 'regular (stat:size (stat file1))))
  159. ("root/lnk"
  160. (values 'symlink 0))
  161. ("root/dir"
  162. (values 'directory 0))
  163. ("root/dir/bar"
  164. (values 'regular (stat:size (stat file2))))
  165. ("root/dir/exe"
  166. (values 'executable (string-length file3))))
  167. #:file-port
  168. (match-lambda
  169. ("root/foo" (open-input-file file1))
  170. ("root/dir/bar" (open-input-file file2))
  171. ("root/dir/exe" (open-input-string file3)))
  172. #:symlink-target
  173. (match-lambda
  174. ("root/lnk" "foo"))
  175. #:directory-entries
  176. (match-lambda
  177. ("root" '("foo" "dir" "lnk"))
  178. ("root/dir" '("bar" "exe"))))
  179. (close-port port)
  180. (rm-rf %test-dir)
  181. (mkdir %test-dir)
  182. (restore-file (open-bytevector-input-port (get-bytevector))
  183. output)
  184. (and (file=? (string-append output "/foo") file1)
  185. (string=? (readlink (string-append output "/lnk"))
  186. "foo")
  187. (file=? (string-append output "/dir/bar") file2)
  188. (string=? (call-with-input-file (string-append output "/dir/exe")
  189. get-string-all)
  190. file3)
  191. (> (logand (stat:mode (lstat (string-append output "/dir/exe")))
  192. #o100)
  193. 0)
  194. (equal? '("." ".." "bar" "exe")
  195. (scandir (string-append output "/dir")))
  196. (equal? '("." ".." "dir" "foo" "lnk")
  197. (scandir output))))
  198. (lambda ()
  199. (false-if-exception (rm-rf %test-dir))))))
  200. (test-equal "write-file-tree + fold-archive"
  201. '(("R" directory #f)
  202. ("R/dir" directory #f)
  203. ("R/dir/exe" executable "1234")
  204. ("R/dir" directory-complete #f)
  205. ("R/foo" regular "abcdefg")
  206. ("R/lnk" symlink "foo")
  207. ("R" directory-complete #f))
  208. (let ()
  209. (define-values (port get-bytevector)
  210. (open-bytevector-output-port))
  211. (write-file-tree "root" port
  212. #:file-type+size
  213. (match-lambda
  214. ("root"
  215. (values 'directory 0))
  216. ("root/foo"
  217. (values 'regular 7))
  218. ("root/lnk"
  219. (values 'symlink 0))
  220. ("root/dir"
  221. (values 'directory 0))
  222. ("root/dir/exe"
  223. (values 'executable 4)))
  224. #:file-port
  225. (match-lambda
  226. ("root/foo" (open-input-string "abcdefg"))
  227. ("root/dir/exe" (open-input-string "1234")))
  228. #:symlink-target
  229. (match-lambda
  230. ("root/lnk" "foo"))
  231. #:directory-entries
  232. (match-lambda
  233. ("root" '("foo" "dir" "lnk"))
  234. ("root/dir" '("exe"))))
  235. (close-port port)
  236. (reverse
  237. (fold-archive (lambda (file type contents result)
  238. (let ((contents (if (memq type '(regular executable))
  239. (utf8->string
  240. (get-bytevector-n (car contents)
  241. (cdr contents)))
  242. contents)))
  243. (cons `(,file ,type ,contents)
  244. result)))
  245. '()
  246. (open-bytevector-input-port (get-bytevector))
  247. "R"))))
  248. (test-equal "write-file-tree + fold-archive, flat file"
  249. '(("R" regular "abcdefg"))
  250. (let ()
  251. (define-values (port get-bytevector)
  252. (open-bytevector-output-port))
  253. (write-file-tree "root" port
  254. #:file-type+size
  255. (match-lambda
  256. ("root" (values 'regular 7)))
  257. #:file-port
  258. (match-lambda
  259. ("root" (open-input-string "abcdefg"))))
  260. (close-port port)
  261. (reverse
  262. (fold-archive (lambda (file type contents result)
  263. (let ((contents (utf8->string
  264. (get-bytevector-n (car contents)
  265. (cdr contents)))))
  266. (cons `(,file ,type ,contents) result)))
  267. '()
  268. (open-bytevector-input-port (get-bytevector))
  269. "R"))))
  270. (test-assert "write-file supports non-file output ports"
  271. (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
  272. "/guix"))
  273. (output (%make-void-port "w")))
  274. (write-file input output)
  275. #t))
  276. (test-equal "write-file puts file in C locale collation order"
  277. (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
  278. (let ((input (string-append %test-dir ".input")))
  279. (dynamic-wind
  280. (lambda ()
  281. (define (touch file)
  282. (call-with-output-file (string-append input "/" file)
  283. (const #t)))
  284. (mkdir input)
  285. (touch "B")
  286. (touch "Z")
  287. (touch "a")
  288. (symlink "B" (string-append input "/z")))
  289. (lambda ()
  290. (let-values (((port get-hash) (open-sha256-port)))
  291. (write-file input port)
  292. (close-port port)
  293. (get-hash)))
  294. (lambda ()
  295. (rm-rf input)))))
  296. (test-equal "restore-file with incomplete input"
  297. (string-append %test-dir "/foo")
  298. (let ((port (open-bytevector-input-port #vu8(1 2 3))))
  299. (guard (c ((nar-error? c)
  300. (and (eq? port (nar-error-port c))
  301. (nar-error-file c))))
  302. (restore-file port (string-append %test-dir "/foo"))
  303. #f)))
  304. (test-assert "write-file + restore-file"
  305. (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
  306. "/guix"))
  307. (output %test-dir)
  308. (nar (string-append output ".nar")))
  309. (dynamic-wind
  310. (lambda () #t)
  311. (lambda ()
  312. (call-with-output-file nar
  313. (cut write-file input <>))
  314. (call-with-input-file nar
  315. (cut restore-file <> output))
  316. (file-tree-equal? input output))
  317. (lambda ()
  318. (false-if-exception (delete-file nar))
  319. (false-if-exception (rm-rf output))))))
  320. (test-assert "write-file + restore-file with symlinks"
  321. (let ((input (string-append %test-dir ".input")))
  322. (mkdir input)
  323. (dynamic-wind
  324. (const #t)
  325. (lambda ()
  326. (with-file-tree input
  327. (directory "root"
  328. (("reg") ("exe" #o777) ("sym" -> "reg")))
  329. (let* ((output %test-dir)
  330. (nar (string-append output ".nar")))
  331. (dynamic-wind
  332. (lambda () #t)
  333. (lambda ()
  334. (call-with-output-file nar
  335. (cut write-file input <>))
  336. (call-with-input-file nar
  337. (cut restore-file <> output))
  338. (and (file-tree-equal? input output)
  339. (every (lambda (file)
  340. (canonical-file?
  341. (string-append output "/" file)))
  342. '("root" "root/reg" "root/exe"))))
  343. (lambda ()
  344. (false-if-exception (delete-file nar))
  345. (false-if-exception (rm-rf output)))))))
  346. (lambda ()
  347. (rmdir input)))))
  348. (test-assert "write-file #:select? + restore-file"
  349. (let ((input (string-append %test-dir ".input")))
  350. (mkdir input)
  351. (dynamic-wind
  352. (const #t)
  353. (lambda ()
  354. (with-file-tree input
  355. (directory "root"
  356. ((directory "a" (("x") ("y") ("z")))
  357. ("b") ("c") ("d" -> "b")))
  358. (let* ((output %test-dir)
  359. (nar (string-append output ".nar")))
  360. (dynamic-wind
  361. (lambda () #t)
  362. (lambda ()
  363. (call-with-output-file nar
  364. (lambda (port)
  365. (write-file input port
  366. #:select?
  367. (lambda (file stat)
  368. (and (not (string=? (basename file)
  369. "a"))
  370. (not (eq? (stat:type stat)
  371. 'symlink)))))))
  372. (call-with-input-file nar
  373. (cut restore-file <> output))
  374. ;; Make sure "a" and "d" have been filtered out.
  375. (and (not (file-exists? (string-append output "/root/a")))
  376. (file=? (string-append output "/root/b")
  377. (string-append input "/root/b"))
  378. (file=? (string-append output "/root/c")
  379. (string-append input "/root/c"))
  380. (not (file-exists? (string-append output "/root/d")))))
  381. (lambda ()
  382. (false-if-exception (delete-file nar))
  383. (false-if-exception (rm-rf output)))))))
  384. (lambda ()
  385. (rmdir input)))))
  386. (test-eq "restore-file with non-UTF8 locale" ;<https://bugs.gnu.org/33603>
  387. 'encoding-error
  388. (let* ((file (search-path %load-path "guix.scm"))
  389. (output (string-append %test-dir "/output"))
  390. (locale (setlocale LC_ALL "C")))
  391. (dynamic-wind
  392. (lambda () #t)
  393. (lambda ()
  394. (define-values (port get-bytevector)
  395. (open-bytevector-output-port))
  396. (write-file-tree "root" port
  397. #:file-type+size
  398. (match-lambda
  399. ("root" (values 'directory 0))
  400. ("root/λ" (values 'regular 0)))
  401. #:file-port (const (%make-void-port "r"))
  402. #:symlink-target (const #f)
  403. #:directory-entries (const '("λ")))
  404. (close-port port)
  405. (mkdir %test-dir)
  406. (catch 'encoding-error
  407. (lambda ()
  408. ;; This show throw to 'encoding-error.
  409. (restore-file (open-bytevector-input-port (get-bytevector))
  410. output)
  411. (scandir output))
  412. (lambda args
  413. 'encoding-error)))
  414. (lambda ()
  415. (false-if-exception (rm-rf %test-dir))
  416. (setlocale LC_ALL locale)))))
  417. ;; XXX: Tell the 'deduplicate' procedure what store we're actually using.
  418. (setenv "NIX_STORE" (%store-prefix))
  419. (test-assert "restore-file-set (signed, valid)"
  420. (with-store store
  421. (let* ((texts (unfold (cut >= <> 10)
  422. (lambda _ (random-text))
  423. 1+
  424. 0))
  425. (files (map (cut add-text-to-store store "text" <>) texts))
  426. (dump (call-with-bytevector-output-port
  427. (cut export-paths store files <>))))
  428. (delete-paths store files)
  429. (and (every (negate file-exists?) files)
  430. (let* ((source (open-bytevector-input-port dump))
  431. (imported (restore-file-set source)))
  432. (and (equal? imported files)
  433. (every (lambda (file)
  434. (and (file-exists? file)
  435. (valid-path? store file)))
  436. files)
  437. (equal? texts
  438. (map (lambda (file)
  439. (call-with-input-file file
  440. get-string-all))
  441. files))
  442. (every canonical-file? files)))))))
  443. (test-assert "restore-file-set with directories (signed, valid)"
  444. ;; <https://bugs.gnu.org/33361> describes a bug whereby directories
  445. ;; containing files subject to deduplication were not canonicalized--i.e.,
  446. ;; their mtime and permissions were not reset. Ensure that this bug is
  447. ;; gone.
  448. (with-store store
  449. ;; Note: TEXT1 and TEXT2 must be longer than %DEDUPLICATION-MINIMUM-SIZE.
  450. (let* ((text1 (string-concatenate (make-list 200 (random-text))))
  451. (text2 (string-concatenate (make-list 200 (random-text))))
  452. (tree `("tree" directory
  453. ("a" regular (data ,text1))
  454. ("b" directory
  455. ("c" regular (data ,text2))
  456. ("d" regular (data ,text1))))) ;duplicate
  457. (file (add-file-tree-to-store store tree))
  458. (dump (call-with-bytevector-output-port
  459. (cute export-paths store (list file) <>))))
  460. (delete-paths store (list file))
  461. (and (not (file-exists? file))
  462. (let* ((source (open-bytevector-input-port dump))
  463. (imported (restore-file-set source)))
  464. (and (equal? imported (list file))
  465. (file-exists? file)
  466. (valid-path? store file)
  467. (string=? text1
  468. (call-with-input-file (string-append file "/a")
  469. get-string-all))
  470. (string=? text2
  471. (call-with-input-file
  472. (string-append file "/b/c")
  473. get-string-all))
  474. (= (stat:ino (stat (string-append file "/a"))) ;deduplication
  475. (stat:ino (stat (string-append file "/b/d"))))
  476. (every canonical-file?
  477. (find-files file #:directories? #t))))))))
  478. (test-assert "restore-file-set (missing signature)"
  479. (let/ec return
  480. (with-store store
  481. (let* ((file (add-text-to-store store "foo" (random-text)))
  482. (dump (call-with-bytevector-output-port
  483. (cute export-paths store (list file) <>
  484. #:sign? #f))))
  485. (delete-paths store (list file))
  486. (and (not (file-exists? file))
  487. (let ((source (open-bytevector-input-port dump)))
  488. (guard (c ((nar-signature-error? c)
  489. (let ((message (condition-message c))
  490. (port (nar-error-port c)))
  491. (return
  492. (and (string-match "lacks.*signature" message)
  493. (string=? file (nar-error-file c))
  494. (eq? source port))))))
  495. (restore-file-set source))
  496. #f))))))
  497. (test-assert "restore-file-set (corrupt)"
  498. (let/ec return
  499. (with-store store
  500. (let* ((file (add-text-to-store store "foo"
  501. (random-text)))
  502. (dump (call-with-bytevector-output-port
  503. (cute export-paths store (list file) <>))))
  504. (delete-paths store (list file))
  505. ;; Flip a byte in the file contents.
  506. (let* ((index 120)
  507. (byte (bytevector-u8-ref dump index)))
  508. (bytevector-u8-set! dump index (logxor #xff byte)))
  509. (and (not (file-exists? file))
  510. (let ((source (open-bytevector-input-port dump)))
  511. (guard (c ((nar-invalid-hash-error? c)
  512. (let ((message (condition-message c))
  513. (port (nar-error-port c)))
  514. (return
  515. (and (string-contains message "hash")
  516. (string=? file (nar-error-file c))
  517. (eq? source port))))))
  518. (restore-file-set source))
  519. #f))))))
  520. (test-end "nar")
  521. ;;; Local Variables:
  522. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  523. ;;; End: