nar.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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 ((guix hash)
  24. #:select (open-sha256-port open-sha256-input-port))
  25. #:use-module ((guix packages)
  26. #:select (base32))
  27. #:use-module (rnrs bytevectors)
  28. #:use-module (rnrs io ports)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-11)
  31. #:use-module (srfi srfi-26)
  32. #:use-module (srfi srfi-34)
  33. #:use-module (srfi srfi-35)
  34. #:use-module (srfi srfi-64)
  35. #:use-module (ice-9 ftw)
  36. #:use-module (ice-9 regex)
  37. #:use-module ((ice-9 control) #:select (let/ec))
  38. #:use-module (ice-9 match))
  39. ;; Test the (guix nar) module.
  40. ;;;
  41. ;;; File system testing tools, initially contributed to Guile, then libchop.
  42. ;;;
  43. (define (random-file-size)
  44. (define %average (* 1024 512)) ; 512 KiB
  45. (define %stddev (* 1024 64)) ; 64 KiB
  46. (inexact->exact
  47. (max 0 (round (+ %average (* %stddev (random:normal)))))))
  48. (define (make-file-tree dir tree)
  49. "Make file system TREE at DIR."
  50. (let loop ((dir dir)
  51. (tree tree))
  52. (define (scope file)
  53. (string-append dir "/" file))
  54. (match tree
  55. (('directory name (body ...))
  56. (mkdir (scope name))
  57. (for-each (cute loop (scope name) <>) body))
  58. (('directory name (? integer? mode) (body ...))
  59. (mkdir (scope name))
  60. (for-each (cute loop (scope name) <>) body)
  61. (chmod (scope name) mode))
  62. ((file)
  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))
  67. ((from '-> to)
  68. (symlink to (scope from))))))
  69. (define (delete-file-tree dir tree)
  70. "Delete file TREE from DIR."
  71. (let loop ((dir dir)
  72. (tree tree))
  73. (define (scope file)
  74. (string-append dir "/" file))
  75. (match tree
  76. (('directory name (body ...))
  77. (for-each (cute loop (scope name) <>) body)
  78. (rmdir (scope name)))
  79. (('directory name (? integer? mode) (body ...))
  80. (chmod (scope name) #o755) ; make sure it can be entered
  81. (for-each (cute loop (scope name) <>) body)
  82. (rmdir (scope name)))
  83. ((from '-> _)
  84. (delete-file (scope from)))
  85. ((file _ ...)
  86. (delete-file (scope file))))))
  87. (define-syntax-rule (with-file-tree dir tree body ...)
  88. (dynamic-wind
  89. (lambda ()
  90. (make-file-tree dir 'tree))
  91. (lambda ()
  92. body ...)
  93. (lambda ()
  94. (delete-file-tree dir 'tree))))
  95. (define (file-tree-equal? input output)
  96. "Return #t if the file trees at INPUT and OUTPUT are equal."
  97. (define strip
  98. (cute string-drop <> (string-length input)))
  99. (define sibling
  100. (compose (cut string-append output <>) strip))
  101. (file-system-fold (const #t)
  102. (lambda (name stat result) ; leaf
  103. (and result
  104. (file=? name (sibling name))))
  105. (lambda (name stat result) ; down
  106. result)
  107. (lambda (name stat result) ; up
  108. result)
  109. (const #f) ; skip
  110. (lambda (name stat errno result)
  111. (pk 'error name stat errno)
  112. #f)
  113. #t ; result
  114. input
  115. lstat))
  116. (define (populate-file file size)
  117. (call-with-output-file file
  118. (lambda (p)
  119. (put-bytevector p (random-bytevector size)))))
  120. (define (rm-rf dir)
  121. (file-system-fold (const #t) ; enter?
  122. (lambda (file stat result) ; leaf
  123. (delete-file file))
  124. (const #t) ; down
  125. (lambda (dir stat result) ; up
  126. (rmdir dir))
  127. (const #t) ; skip
  128. (const #t) ; error
  129. #t
  130. dir
  131. lstat))
  132. (define %test-dir
  133. ;; An output directory under $top_builddir.
  134. (string-append (dirname (search-path %load-path "pre-inst-env"))
  135. "/test-nar-" (number->string (getpid))))
  136. (test-begin "nar")
  137. (test-assert "write-file supports non-file output ports"
  138. (let ((input (string-append (dirname (search-path %load-path "guix.scm"))
  139. "/guix"))
  140. (output (%make-void-port "w")))
  141. (write-file input output)
  142. #t))
  143. (test-equal "write-file puts file in C locale collation order"
  144. (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3")
  145. (let ((input (string-append %test-dir ".input")))
  146. (dynamic-wind
  147. (lambda ()
  148. (define (touch file)
  149. (call-with-output-file (string-append input "/" file)
  150. (const #t)))
  151. (mkdir input)
  152. (touch "B")
  153. (touch "Z")
  154. (touch "a")
  155. (symlink "B" (string-append input "/z")))
  156. (lambda ()
  157. (let-values (((port get-hash) (open-sha256-port)))
  158. (write-file input port)
  159. (close-port port)
  160. (get-hash)))
  161. (lambda ()
  162. (rm-rf input)))))
  163. (test-equal "restore-file with incomplete input"
  164. (string-append %test-dir "/foo")
  165. (let ((port (open-bytevector-input-port #vu8(1 2 3))))
  166. (guard (c ((nar-error? c)
  167. (and (eq? port (nar-error-port c))
  168. (nar-error-file c))))
  169. (restore-file port (string-append %test-dir "/foo"))
  170. #f)))
  171. (test-assert "write-file + restore-file"
  172. (let* ((input (string-append (dirname (search-path %load-path "guix.scm"))
  173. "/guix"))
  174. (output %test-dir)
  175. (nar (string-append output ".nar")))
  176. (dynamic-wind
  177. (lambda () #t)
  178. (lambda ()
  179. (call-with-output-file nar
  180. (cut write-file input <>))
  181. (call-with-input-file nar
  182. (cut restore-file <> output))
  183. (file-tree-equal? input output))
  184. (lambda ()
  185. (false-if-exception (delete-file nar))
  186. (false-if-exception (rm-rf output))))))
  187. (test-assert "write-file + restore-file with symlinks"
  188. (let ((input (string-append %test-dir ".input")))
  189. (mkdir input)
  190. (dynamic-wind
  191. (const #t)
  192. (lambda ()
  193. (with-file-tree input
  194. (directory "root"
  195. (("reg") ("exe" #o777) ("sym" -> "reg")))
  196. (let* ((output %test-dir)
  197. (nar (string-append output ".nar")))
  198. (dynamic-wind
  199. (lambda () #t)
  200. (lambda ()
  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))
  206. (lambda ()
  207. (false-if-exception (delete-file nar))
  208. (false-if-exception (rm-rf output)))))))
  209. (lambda ()
  210. (rmdir input)))))
  211. (test-assert "write-file #:select? + restore-file"
  212. (let ((input (string-append %test-dir ".input")))
  213. (mkdir input)
  214. (dynamic-wind
  215. (const #t)
  216. (lambda ()
  217. (with-file-tree input
  218. (directory "root"
  219. ((directory "a" (("x") ("y") ("z")))
  220. ("b") ("c") ("d" -> "b")))
  221. (let* ((output %test-dir)
  222. (nar (string-append output ".nar")))
  223. (dynamic-wind
  224. (lambda () #t)
  225. (lambda ()
  226. (call-with-output-file nar
  227. (lambda (port)
  228. (write-file input port
  229. #:select?
  230. (lambda (file stat)
  231. (and (not (string=? (basename file)
  232. "a"))
  233. (not (eq? (stat:type stat)
  234. 'symlink)))))))
  235. (call-with-input-file nar
  236. (cut restore-file <> output))
  237. ;; Make sure "a" and "d" have been filtered out.
  238. (and (not (file-exists? (string-append output "/root/a")))
  239. (file=? (string-append output "/root/b")
  240. (string-append input "/root/b"))
  241. (file=? (string-append output "/root/c")
  242. (string-append input "/root/c"))
  243. (not (file-exists? (string-append output "/root/d")))))
  244. (lambda ()
  245. (false-if-exception (delete-file nar))
  246. (false-if-exception (rm-rf output)))))))
  247. (lambda ()
  248. (rmdir input)))))
  249. ;; 'restore-file-set' depends on 'open-sha256-input-port', which in turn
  250. ;; relies on a Guile 2.0.10+ feature.
  251. (test-skip (if (false-if-exception
  252. (open-sha256-input-port (%make-void-port "r")))
  253. 0
  254. 3))
  255. (test-assert "restore-file-set (signed, valid)"
  256. (with-store store
  257. (let* ((texts (unfold (cut >= <> 10)
  258. (lambda _ (random-text))
  259. 1+
  260. 0))
  261. (files (map (cut add-text-to-store store "text" <>) texts))
  262. (dump (call-with-bytevector-output-port
  263. (cut export-paths store files <>))))
  264. (delete-paths store files)
  265. (and (every (negate file-exists?) files)
  266. (let* ((source (open-bytevector-input-port dump))
  267. (imported (restore-file-set source)))
  268. (and (equal? imported files)
  269. (every (lambda (file)
  270. (and (file-exists? file)
  271. (valid-path? store file)))
  272. files)
  273. (equal? texts
  274. (map (lambda (file)
  275. (call-with-input-file file
  276. get-string-all))
  277. files))))))))
  278. (test-assert "restore-file-set (missing signature)"
  279. (let/ec return
  280. (with-store store
  281. (let* ((file (add-text-to-store store "foo" (random-text)))
  282. (dump (call-with-bytevector-output-port
  283. (cute export-paths store (list file) <>
  284. #:sign? #f))))
  285. (delete-paths store (list file))
  286. (and (not (file-exists? file))
  287. (let ((source (open-bytevector-input-port dump)))
  288. (guard (c ((nar-signature-error? c)
  289. (let ((message (condition-message c))
  290. (port (nar-error-port c)))
  291. (return
  292. (and (string-match "lacks.*signature" message)
  293. (string=? file (nar-error-file c))
  294. (eq? source port))))))
  295. (restore-file-set source))
  296. #f))))))
  297. (test-assert "restore-file-set (corrupt)"
  298. (let/ec return
  299. (with-store store
  300. (let* ((file (add-text-to-store store "foo"
  301. (random-text)))
  302. (dump (call-with-bytevector-output-port
  303. (cute export-paths store (list file) <>))))
  304. (delete-paths store (list file))
  305. ;; Flip a byte in the file contents.
  306. (let* ((index 120)
  307. (byte (bytevector-u8-ref dump index)))
  308. (bytevector-u8-set! dump index (logxor #xff byte)))
  309. (and (not (file-exists? file))
  310. (let ((source (open-bytevector-input-port dump)))
  311. (guard (c ((nar-invalid-hash-error? c)
  312. (let ((message (condition-message c))
  313. (port (nar-error-port c)))
  314. (return
  315. (and (string-contains message "hash")
  316. (string=? file (nar-error-file c))
  317. (eq? source port))))))
  318. (restore-file-set source))
  319. #f))))))
  320. (test-end "nar")
  321. ;;; Local Variables:
  322. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  323. ;;; End: