challenge.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2017, 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-challenge)
  20. #:use-module (guix tests)
  21. #:use-module (guix tests http)
  22. #:use-module ((gcrypt hash) #:prefix gcrypt:)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix derivations)
  26. #:use-module (guix serialization)
  27. #:use-module (guix packages)
  28. #:use-module (guix gexp)
  29. #:use-module (guix base32)
  30. #:use-module (guix narinfo)
  31. #:use-module (guix scripts challenge)
  32. #:use-module ((guix build utils) #:select (find-files))
  33. #:use-module (gnu packages bootstrap)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-26)
  36. #:use-module (srfi srfi-64)
  37. #:use-module (rnrs bytevectors)
  38. #:use-module (rnrs io ports)
  39. #:use-module (ice-9 match))
  40. (define query-path-hash*
  41. (store-lift query-path-hash))
  42. (define (query-path-size item)
  43. (mlet %store-monad ((info (query-path-info* item)))
  44. (return (path-info-nar-size info))))
  45. (define* (call-with-derivation-narinfo* drv thunk hash)
  46. (lambda (store)
  47. (with-derivation-narinfo drv (sha256 => hash)
  48. (values (run-with-store store (thunk)) store))))
  49. (define-syntax with-derivation-narinfo*
  50. (syntax-rules (sha256 =>)
  51. ((_ drv (sha256 => hash) body ...)
  52. (call-with-derivation-narinfo* drv
  53. (lambda () body ...)
  54. hash))))
  55. (define-syntax-rule (with-http-server/monadic responses+body body ...)
  56. ;; Like 'with-http-server' but for use in a monadic context.
  57. (lambda (store)
  58. (values (with-http-server responses+body
  59. (run-with-store store (begin body ...)))
  60. store)))
  61. (test-begin "challenge")
  62. (test-assertm "no discrepancies"
  63. (let ((text (random-text)))
  64. (mlet* %store-monad ((drv (gexp->derivation "something"
  65. #~(call-with-output-file
  66. #$output
  67. (lambda (port)
  68. (display #$text port)))))
  69. (out -> (derivation->output-path drv)))
  70. (mbegin %store-monad
  71. (built-derivations (list drv))
  72. (mlet %store-monad ((hash (query-path-hash* out)))
  73. (with-derivation-narinfo* drv (sha256 => hash)
  74. (>>= (compare-contents (list out) (%test-substitute-urls))
  75. (match-lambda
  76. ((report)
  77. (return
  78. (and (string=? out (comparison-report-item report))
  79. (bytevector=?
  80. (comparison-report-local-sha256 report)
  81. hash)
  82. (comparison-report-match? report))))))))))))
  83. (test-assertm "one discrepancy"
  84. (let ((text (random-text)))
  85. (mlet* %store-monad ((drv (gexp->derivation "something"
  86. #~(call-with-output-file
  87. #$output
  88. (lambda (port)
  89. (display #$text port)))))
  90. (out -> (derivation->output-path drv)))
  91. (mbegin %store-monad
  92. (built-derivations (list drv))
  93. (mlet* %store-monad ((hash (query-path-hash* out))
  94. (wrong-hash
  95. -> (let* ((w (bytevector-copy hash))
  96. (b (bytevector-u8-ref w 0)))
  97. (bytevector-u8-set! w 0
  98. (modulo (+ b 1) 128))
  99. w)))
  100. (with-derivation-narinfo* drv (sha256 => wrong-hash)
  101. (>>= (compare-contents (list out) (%test-substitute-urls))
  102. (match-lambda
  103. ((report)
  104. (return
  105. (and (string=? out (comparison-report-item (pk report)))
  106. (eq? 'mismatch (comparison-report-result report))
  107. (bytevector=? hash
  108. (comparison-report-local-sha256
  109. report))
  110. (match (comparison-report-narinfos report)
  111. ((bad)
  112. (bytevector=? wrong-hash
  113. (narinfo-hash->sha256
  114. (narinfo-hash bad))))))))))))))))
  115. (test-assertm "inconclusive: no substitutes"
  116. (mlet* %store-monad ((drv (gexp->derivation "foo" #~(mkdir #$output)))
  117. (out -> (derivation->output-path drv))
  118. (_ (built-derivations (list drv)))
  119. (hash (query-path-hash* out)))
  120. (>>= (compare-contents (list out) (%test-substitute-urls))
  121. (match-lambda
  122. ((report)
  123. (return
  124. (and (string=? out (comparison-report-item report))
  125. (comparison-report-inconclusive? report)
  126. (null? (comparison-report-narinfos report))
  127. (bytevector=? (comparison-report-local-sha256 report)
  128. hash))))))))
  129. (test-assertm "inconclusive: no local build"
  130. (let ((text (random-text)))
  131. (mlet* %store-monad ((drv (gexp->derivation "something"
  132. #~(list #$output #$text)))
  133. (out -> (derivation->output-path drv))
  134. (hash -> (gcrypt:sha256 #vu8())))
  135. (with-derivation-narinfo* drv (sha256 => hash)
  136. (>>= (compare-contents (list out) (%test-substitute-urls))
  137. (match-lambda
  138. ((report)
  139. (return
  140. (and (string=? out (comparison-report-item report))
  141. (comparison-report-inconclusive? report)
  142. (not (comparison-report-local-sha256 report))
  143. (match (comparison-report-narinfos report)
  144. ((narinfo)
  145. (bytevector=? (narinfo-hash->sha256
  146. (narinfo-hash narinfo))
  147. hash))))))))))))
  148. (define %nar-location "nar/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  149. (define (make-narinfo item size hash)
  150. (format #f "StorePath: ~a
  151. Compression: none
  152. URL: ~a
  153. NarSize: ~d
  154. NarHash: sha256:~a
  155. References: ~%" item %nar-location size (bytevector->nix-base32-string hash)))
  156. (define (call-mismatch-test proc)
  157. "Pass PROC a <comparison-report> for a mismatch and return its return
  158. value."
  159. ;; Pretend we have two different results for the same store item, ITEM, with
  160. ;; "/bin/guile" differing between the two nars.
  161. (mlet* %store-monad
  162. ((drv1 (package->derivation %bootstrap-guile))
  163. (drv2 (gexp->derivation
  164. "broken-guile"
  165. (with-imported-modules '((guix build utils))
  166. #~(begin
  167. (use-modules (guix build utils))
  168. (copy-recursively #$drv1 #$output)
  169. (chmod (string-append #$output "/bin/guile")
  170. #o755)
  171. (call-with-output-file (string-append
  172. #$output
  173. "/bin/guile")
  174. (lambda (port)
  175. (display "corrupt!" port)))))))
  176. (out1 -> (derivation->output-path drv1))
  177. (out2 -> (derivation->output-path drv2))
  178. (item -> (string-append (%store-prefix) "/"
  179. (bytevector->nix-base32-string
  180. (random-bytevector 32))
  181. "-foo"
  182. (number->string (current-time) 16))))
  183. (mbegin %store-monad
  184. (built-derivations (list drv1 drv2))
  185. (mlet* %store-monad ((size1 (query-path-size out1))
  186. (size2 (query-path-size out2))
  187. (hash1 (query-path-hash* out1))
  188. (hash2 (query-path-hash* out2))
  189. (nar1 -> (call-with-bytevector-output-port
  190. (lambda (port)
  191. (write-file out1 port))))
  192. (nar2 -> (call-with-bytevector-output-port
  193. (lambda (port)
  194. (write-file out2 port)))))
  195. (with-http-server/monadic
  196. `((,(string-append "/foo/bar/" (store-path-hash-part item) ".narinfo")
  197. 200 ,(make-narinfo item size1 hash1))
  198. (,(string-append "/foo/bar/" %nar-location) 200 ,nar1))
  199. (let ((url1 (%local-url)))
  200. (with-http-server/monadic
  201. `((,(string-append "/foo/bar/" (store-path-hash-part item) ".narinfo")
  202. 200 ,(make-narinfo item size2 hash2))
  203. (,(string-append "/foo/bar/" %nar-location) 200 ,nar2))
  204. (mlet* %store-monad ((urls -> (list url1 (%local-url)))
  205. (reports (compare-contents (list item)
  206. urls)))
  207. (return (proc (car reports)))))))))))
  208. (test-assertm "differing-files"
  209. (call-mismatch-test
  210. (lambda (report)
  211. (equal? (differing-files report) '("/bin/guile")))))
  212. (test-assertm "call-with-mismatches"
  213. (call-mismatch-test
  214. (lambda (report)
  215. (call-with-mismatches
  216. report
  217. (lambda (directory1 directory2)
  218. (let* ((files1 (find-files directory1))
  219. (files2 (find-files directory2))
  220. (files (map (cute string-drop <> (string-length directory1))
  221. files1)))
  222. (and (equal? files
  223. (map (cute string-drop <> (string-length directory2))
  224. files2))
  225. (equal? (remove (lambda (file)
  226. (file=? (string-append directory1 "/" file)
  227. (string-append directory2 "/" file)))
  228. files)
  229. '("/bin/guile")))))))))
  230. (test-end)
  231. ;;; Local Variables:
  232. ;;; eval: (put 'with-derivation-narinfo* 'scheme-indent-function 2)
  233. ;;; End: