1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-challenge)
20 #:use-module (guix tests)
21 #:use-module (gcrypt hash)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix derivations)
25 #:use-module (guix gexp)
26 #:use-module (guix scripts challenge)
27 #:use-module (guix scripts substitute)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs bytevectors)
32 #:use-module (ice-9 match))
34 (define query-path-hash*
35 (store-lift query-path-hash))
37 (define* (call-with-derivation-narinfo* drv thunk hash)
39 (with-derivation-narinfo drv (sha256 => hash)
40 (values (run-with-store store (thunk)) store))))
42 (define-syntax with-derivation-narinfo*
43 (syntax-rules (sha256 =>)
44 ((_ drv (sha256 => hash) body ...)
45 (call-with-derivation-narinfo* drv
50 (test-begin "challenge")
52 (test-assertm "no discrepancies"
53 (let ((text (random-text)))
54 (mlet* %store-monad ((drv (gexp->derivation "something"
55 #~(call-with-output-file
58 (display #$text port)))))
59 (out -> (derivation->output-path drv)))
61 (built-derivations (list drv))
62 (mlet %store-monad ((hash (query-path-hash* out)))
63 (with-derivation-narinfo* drv (sha256 => hash)
64 (>>= (compare-contents (list out) (%test-substitute-urls))
68 (and (string=? out (comparison-report-item report))
70 (comparison-report-local-sha256 report)
72 (comparison-report-match? report))))))))))))
74 (test-assertm "one discrepancy"
75 (let ((text (random-text)))
76 (mlet* %store-monad ((drv (gexp->derivation "something"
77 #~(call-with-output-file
80 (display #$text port)))))
81 (out -> (derivation->output-path drv)))
83 (built-derivations (list drv))
84 (mlet* %store-monad ((hash (query-path-hash* out))
86 -> (let* ((w (bytevector-copy hash))
87 (b (bytevector-u8-ref w 0)))
88 (bytevector-u8-set! w 0
91 (with-derivation-narinfo* drv (sha256 => wrong-hash)
92 (>>= (compare-contents (list out) (%test-substitute-urls))
96 (and (string=? out (comparison-report-item (pk report)))
97 (eq? 'mismatch (comparison-report-result report))
99 (comparison-report-local-sha256
101 (match (comparison-report-narinfos report)
103 (bytevector=? wrong-hash
104 (narinfo-hash->sha256
105 (narinfo-hash bad))))))))))))))))
107 (test-assertm "inconclusive: no substitutes"
108 (mlet* %store-monad ((drv (gexp->derivation "foo" #~(mkdir #$output)))
109 (out -> (derivation->output-path drv))
110 (_ (built-derivations (list drv)))
111 (hash (query-path-hash* out)))
112 (>>= (compare-contents (list out) (%test-substitute-urls))
116 (and (string=? out (comparison-report-item report))
117 (comparison-report-inconclusive? report)
118 (null? (comparison-report-narinfos report))
119 (bytevector=? (comparison-report-local-sha256 report)
122 (test-assertm "inconclusive: no local build"
123 (let ((text (random-text)))
124 (mlet* %store-monad ((drv (gexp->derivation "something"
125 #~(list #$output #$text)))
126 (out -> (derivation->output-path drv))
127 (hash -> (sha256 #vu8())))
128 (with-derivation-narinfo* drv (sha256 => hash)
129 (>>= (compare-contents (list out) (%test-substitute-urls))
133 (and (string=? out (comparison-report-item report))
134 (comparison-report-inconclusive? report)
135 (not (comparison-report-local-sha256 report))
136 (match (comparison-report-narinfos report)
138 (bytevector=? (narinfo-hash->sha256
139 (narinfo-hash narinfo))
146 ;;; eval: (put 'with-derivation-narinfo* 'scheme-indent-function 2)