nar.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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-assert "write-file supports non-file output ports"
  198. (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
  199. "/guix"))
  200. (output (%make-void-port "w")))
  201. (write-file input output)
  202. #t))
  203. (test-equal "write-file puts file in C locale collation order"
  204. (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
  205. (let ((input (string-append %test-dir ".input")))
  206. (dynamic-wind
  207. (lambda ()
  208. (define (touch file)
  209. (call-with-output-file (string-append input "/" file)
  210. (const #t)))
  211. (mkdir input)
  212. (touch "B")
  213. (touch "Z")
  214. (touch "a")
  215. (symlink "B" (string-append input "/z")))
  216. (lambda ()
  217. (let-values (((port get-hash) (open-sha256-port)))
  218. (write-file input port)
  219. (close-port port)
  220. (get-hash)))
  221. (lambda ()
  222. (rm-rf input)))))
  223. (test-equal "restore-file with incomplete input"
  224. (string-append %test-dir "/foo")
  225. (let ((port (open-bytevector-input-port #vu8(1 2 3))))
  226. (guard (c ((nar-error? c)
  227. (and (eq? port (nar-error-port c))
  228. (nar-error-file c))))
  229. (restore-file port (string-append %test-dir "/foo"))
  230. #f)))
  231. (test-assert "write-file + restore-file"
  232. (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
  233. "/guix"))
  234. (output %test-dir)
  235. (nar (string-append output ".nar")))
  236. (dynamic-wind
  237. (lambda () #t)
  238. (lambda ()
  239. (call-with-output-file nar
  240. (cut write-file input <>))
  241. (call-with-input-file nar
  242. (cut restore-file <> output))
  243. (file-tree-equal? input output))
  244. (lambda ()
  245. (false-if-exception (delete-file nar))
  246. (false-if-exception (rm-rf output))))))
  247. (test-assert "write-file + restore-file with symlinks"
  248. (let ((input (string-append %test-dir ".input")))
  249. (mkdir input)
  250. (dynamic-wind
  251. (const #t)
  252. (lambda ()
  253. (with-file-tree input
  254. (directory "root"
  255. (("reg") ("exe" #o777) ("sym" -> "reg")))
  256. (let* ((output %test-dir)
  257. (nar (string-append output ".nar")))
  258. (dynamic-wind
  259. (lambda () #t)
  260. (lambda ()
  261. (call-with-output-file nar
  262. (cut write-file input <>))
  263. (call-with-input-file nar
  264. (cut restore-file <> output))
  265. (file-tree-equal? input output))
  266. (lambda ()
  267. (false-if-exception (delete-file nar))
  268. (false-if-exception (rm-rf output)))))))
  269. (lambda ()
  270. (rmdir input)))))
  271. (test-assert "write-file #:select? + restore-file"
  272. (let ((input (string-append %test-dir ".input")))
  273. (mkdir input)
  274. (dynamic-wind
  275. (const #t)
  276. (lambda ()
  277. (with-file-tree input
  278. (directory "root"
  279. ((directory "a" (("x") ("y") ("z")))
  280. ("b") ("c") ("d" -> "b")))
  281. (let* ((output %test-dir)
  282. (nar (string-append output ".nar")))
  283. (dynamic-wind
  284. (lambda () #t)
  285. (lambda ()
  286. (call-with-output-file nar
  287. (lambda (port)
  288. (write-file input port
  289. #:select?
  290. (lambda (file stat)
  291. (and (not (string=? (basename file)
  292. "a"))
  293. (not (eq? (stat:type stat)
  294. 'symlink)))))))
  295. (call-with-input-file nar
  296. (cut restore-file <> output))
  297. ;; Make sure "a" and "d" have been filtered out.
  298. (and (not (file-exists? (string-append output "/root/a")))
  299. (file=? (string-append output "/root/b")
  300. (string-append input "/root/b"))
  301. (file=? (string-append output "/root/c")
  302. (string-append input "/root/c"))
  303. (not (file-exists? (string-append output "/root/d")))))
  304. (lambda ()
  305. (false-if-exception (delete-file nar))
  306. (false-if-exception (rm-rf output)))))))
  307. (lambda ()
  308. (rmdir input)))))
  309. (test-eq "restore-file with non-UTF8 locale" ;<https://bugs.gnu.org/33603>
  310. 'encoding-error
  311. (let* ((file (search-path %load-path "guix.scm"))
  312. (output (string-append %test-dir "/output"))
  313. (locale (setlocale LC_ALL "C")))
  314. (dynamic-wind
  315. (lambda () #t)
  316. (lambda ()
  317. (define-values (port get-bytevector)
  318. (open-bytevector-output-port))
  319. (write-file-tree "root" port
  320. #:file-type+size
  321. (match-lambda
  322. ("root" (values 'directory 0))
  323. ("root/λ" (values 'regular 0)))
  324. #:file-port (const (%make-void-port "r"))
  325. #:symlink-target (const #f)
  326. #:directory-entries (const '("λ")))
  327. (close-port port)
  328. (mkdir %test-dir)
  329. (catch 'encoding-error
  330. (lambda ()
  331. ;; This show throw to 'encoding-error.
  332. (restore-file (open-bytevector-input-port (get-bytevector))
  333. output)
  334. (scandir output))
  335. (lambda args
  336. 'encoding-error)))
  337. (lambda ()
  338. (false-if-exception (rm-rf %test-dir))
  339. (setlocale LC_ALL locale)))))
  340. (test-assert "restore-file-set (signed, valid)"
  341. (with-store store
  342. (let* ((texts (unfold (cut >= <> 10)
  343. (lambda _ (random-text))
  344. 1+
  345. 0))
  346. (files (map (cut add-text-to-store store "text" <>) texts))
  347. (dump (call-with-bytevector-output-port
  348. (cut export-paths store files <>))))
  349. (delete-paths store files)
  350. (and (every (negate file-exists?) files)
  351. (let* ((source (open-bytevector-input-port dump))
  352. (imported (restore-file-set source)))
  353. (and (equal? imported files)
  354. (every (lambda (file)
  355. (and (file-exists? file)
  356. (valid-path? store file)))
  357. files)
  358. (equal? texts
  359. (map (lambda (file)
  360. (call-with-input-file file
  361. get-string-all))
  362. files))
  363. (every canonical-file? files)))))))
  364. (test-assert "restore-file-set with directories (signed, valid)"
  365. ;; <https://bugs.gnu.org/33361> describes a bug whereby directories
  366. ;; containing files subject to deduplication were not canonicalized--i.e.,
  367. ;; their mtime and permissions were not reset. Ensure that this bug is
  368. ;; gone.
  369. (with-store store
  370. (let* ((text1 (random-text))
  371. (text2 (random-text))
  372. (tree `("tree" directory
  373. ("a" regular (data ,text1))
  374. ("b" directory
  375. ("c" regular (data ,text2))
  376. ("d" regular (data ,text1))))) ;duplicate
  377. (file (add-file-tree-to-store store tree))
  378. (dump (call-with-bytevector-output-port
  379. (cute export-paths store (list file) <>))))
  380. (delete-paths store (list file))
  381. (and (not (file-exists? file))
  382. (let* ((source (open-bytevector-input-port dump))
  383. (imported (restore-file-set source)))
  384. (and (equal? imported (list file))
  385. (file-exists? file)
  386. (valid-path? store file)
  387. (string=? text1
  388. (call-with-input-file (string-append file "/a")
  389. get-string-all))
  390. (string=? text2
  391. (call-with-input-file
  392. (string-append file "/b/c")
  393. get-string-all))
  394. (= (stat:ino (stat (string-append file "/a"))) ;deduplication
  395. (stat:ino (stat (string-append file "/b/d"))))
  396. (every canonical-file?
  397. (find-files file #:directories? #t))))))))
  398. (test-assert "restore-file-set (missing signature)"
  399. (let/ec return
  400. (with-store store
  401. (let* ((file (add-text-to-store store "foo" (random-text)))
  402. (dump (call-with-bytevector-output-port
  403. (cute export-paths store (list file) <>
  404. #:sign? #f))))
  405. (delete-paths store (list file))
  406. (and (not (file-exists? file))
  407. (let ((source (open-bytevector-input-port dump)))
  408. (guard (c ((nar-signature-error? c)
  409. (let ((message (condition-message c))
  410. (port (nar-error-port c)))
  411. (return
  412. (and (string-match "lacks.*signature" message)
  413. (string=? file (nar-error-file c))
  414. (eq? source port))))))
  415. (restore-file-set source))
  416. #f))))))
  417. (test-assert "restore-file-set (corrupt)"
  418. (let/ec return
  419. (with-store store
  420. (let* ((file (add-text-to-store store "foo"
  421. (random-text)))
  422. (dump (call-with-bytevector-output-port
  423. (cute export-paths store (list file) <>))))
  424. (delete-paths store (list file))
  425. ;; Flip a byte in the file contents.
  426. (let* ((index 120)
  427. (byte (bytevector-u8-ref dump index)))
  428. (bytevector-u8-set! dump index (logxor #xff byte)))
  429. (and (not (file-exists? file))
  430. (let ((source (open-bytevector-input-port dump)))
  431. (guard (c ((nar-invalid-hash-error? c)
  432. (let ((message (condition-message c))
  433. (port (nar-error-port c)))
  434. (return
  435. (and (string-contains message "hash")
  436. (string=? file (nar-error-file c))
  437. (eq? source port))))))
  438. (restore-file-set source))
  439. #f))))))
  440. (test-end "nar")
  441. ;;; Local Variables:
  442. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  443. ;;; End: