Merge branch 'master' into gnome-updates
[jackhill/guix/guix.git] / tests / challenge.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 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
19 (define-module (test-challenge)
20 #:use-module (guix tests)
21 #:use-module (guix 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))
33
34 (define %store
35 (open-connection-for-tests))
36
37 (define query-path-hash*
38 (store-lift query-path-hash))
39
40 (define-syntax-rule (test-assertm name exp)
41 (test-assert name
42 (run-with-store %store exp
43 #:guile-for-build (%guile-for-build))))
44
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
50 (define-syntax with-derivation-narinfo*
51 (syntax-rules (sha256 =>)
52 ((_ drv (sha256 => hash) body ...)
53 (call-with-derivation-narinfo* drv
54 (lambda () body ...)
55 hash))))
56
57 \f
58 (test-begin "challenge")
59
60 (test-assertm "no discrepancies"
61 (let ((text (random-text)))
62 (mlet* %store-monad ((drv (gexp->derivation "something"
63 #~(call-with-output-file
64 #$output
65 (lambda (port)
66 (display #$text port)))))
67 (out -> (derivation->output-path drv)))
68 (mbegin %store-monad
69 (built-derivations (list drv))
70 (mlet %store-monad ((hash (query-path-hash* out)))
71 (with-derivation-narinfo* drv (sha256 => hash)
72 (>>= (compare-contents (list out) (%test-substitute-urls))
73 (match-lambda
74 ((report)
75 (return
76 (and (string=? out (comparison-report-item report))
77 (bytevector=?
78 (comparison-report-local-sha256 report)
79 hash)
80 (comparison-report-match? report))))))))))))
81
82 (test-assertm "one discrepancy"
83 (let ((text (random-text)))
84 (mlet* %store-monad ((drv (gexp->derivation "something"
85 #~(call-with-output-file
86 #$output
87 (lambda (port)
88 (display #$text port)))))
89 (out -> (derivation->output-path drv)))
90 (mbegin %store-monad
91 (built-derivations (list drv))
92 (mlet* %store-monad ((hash (query-path-hash* out))
93 (wrong-hash
94 -> (let* ((w (bytevector-copy hash))
95 (b (bytevector-u8-ref w 0)))
96 (bytevector-u8-set! w 0
97 (modulo (+ b 1) 128))
98 w)))
99 (with-derivation-narinfo* drv (sha256 => wrong-hash)
100 (>>= (compare-contents (list out) (%test-substitute-urls))
101 (match-lambda
102 ((report)
103 (return
104 (and (string=? out (comparison-report-item (pk report)))
105 (eq? 'mismatch (comparison-report-result report))
106 (bytevector=? hash
107 (comparison-report-local-sha256
108 report))
109 (match (comparison-report-narinfos report)
110 ((bad)
111 (bytevector=? wrong-hash
112 (narinfo-hash->sha256
113 (narinfo-hash bad))))))))))))))))
114
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
130 (test-assertm "inconclusive: no local build"
131 (let ((text (random-text)))
132 (mlet* %store-monad ((drv (gexp->derivation "something"
133 #~(list #$output #$text)))
134 (out -> (derivation->output-path drv))
135 (hash -> (sha256 #vu8())))
136 (with-derivation-narinfo* drv (sha256 => hash)
137 (>>= (compare-contents (list out) (%test-substitute-urls))
138 (match-lambda
139 ((report)
140 (return
141 (and (string=? out (comparison-report-item report))
142 (comparison-report-inconclusive? report)
143 (not (comparison-report-local-sha256 report))
144 (match (comparison-report-narinfos report)
145 ((narinfo)
146 (bytevector=? (narinfo-hash->sha256
147 (narinfo-hash narinfo))
148 hash))))))))))))
149
150
151 (test-end)
152
153 ;;; Local Variables:
154 ;;; eval: (put 'with-derivation-narinfo* 'scheme-indent-function 2)
155 ;;; End: