Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / tests / challenge.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 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 (>>= (discrepancies (list out) (%test-substitute-urls))
73 (lift1 null? %store-monad))))))))
74
75 (test-assertm "one discrepancy"
76 (let ((text (random-text)))
77 (mlet* %store-monad ((drv (gexp->derivation "something"
78 #~(call-with-output-file
79 #$output
80 (lambda (port)
81 (display #$text port)))))
82 (out -> (derivation->output-path drv)))
83 (mbegin %store-monad
84 (built-derivations (list drv))
85 (mlet* %store-monad ((hash (query-path-hash* out))
86 (wrong-hash
87 -> (let* ((w (bytevector-copy hash))
88 (b (bytevector-u8-ref w 0)))
89 (bytevector-u8-set! w 0
90 (modulo (+ b 1) 128))
91 w)))
92 (with-derivation-narinfo* drv (sha256 => wrong-hash)
93 (>>= (discrepancies (list out) (%test-substitute-urls))
94 (match-lambda
95 ((discrepancy)
96 (return
97 (and (string=? out (discrepancy-item discrepancy))
98 (bytevector=? hash
99 (discrepancy-local-sha256
100 discrepancy))
101 (match (discrepancy-narinfos discrepancy)
102 ((bad)
103 (bytevector=? wrong-hash
104 (narinfo-hash->sha256
105 (narinfo-hash bad))))))))))))))))
106
107 (test-end)
108
109 ;;; Local Variables:
110 ;;; eval: (put 'with-derivation-narinfo* 'scheme-indent-function 2)
111 ;;; End: