substitute: Make substitute URLs a SRFI-39 parameter.
[jackhill/guix/guix.git] / tests / substitute.scm
CommitLineData
e9c6c584
NK
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
2c74fde0 3;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
e9c6c584
NK
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
2c74fde0
LC
20(define-module (test-substitute)
21 #:use-module (guix scripts substitute)
e9c6c584
NK
22 #:use-module (guix base64)
23 #:use-module (guix hash)
0363991a 24 #:use-module (guix serialization)
e9c6c584
NK
25 #:use-module (guix pk-crypto)
26 #:use-module (guix pki)
cdea30e0 27 #:use-module (guix config)
e903b7c1 28 #:use-module (guix base32)
cdea30e0 29 #:use-module ((guix store) #:select (%store-prefix))
f84f8590 30 #:use-module ((guix ui) #:select (guix-warning-port))
cdea30e0 31 #:use-module ((guix build utils) #:select (delete-file-recursively))
e9c6c584 32 #:use-module (rnrs bytevectors)
cdea30e0
LC
33 #:use-module (rnrs io ports)
34 #:use-module (web uri)
52f80dfc 35 #:use-module (ice-9 regex)
cdea30e0 36 #:use-module (srfi srfi-26)
e9c6c584 37 #:use-module (srfi srfi-34)
52f80dfc 38 #:use-module (srfi srfi-35)
e9c6c584
NK
39 #:use-module ((srfi srfi-64) #:hide (test-error)))
40
f84f8590
LC
41(define-syntax-rule (test-quit name error-rx exp)
42 "Emit a test that passes when EXP throws to 'quit' with value 1, and when
43it writes to GUIX-WARNING-PORT a messages that matches ERROR-RX."
cdea30e0 44 (test-equal name
f84f8590
LC
45 '(1 #t)
46 (let ((error-output (open-output-string)))
47 (parameterize ((guix-warning-port error-output))
48 (catch 'quit
49 (lambda ()
50 exp
51 #f)
52 (lambda (key value)
53 (list value
54 (let ((message (get-output-string error-output)))
55 (->bool (string-match error-rx message))))))))))
e9c6c584
NK
56
57(define %public-key
cdea30e0
LC
58 ;; This key is known to be in the ACL by default.
59 (call-with-input-file (string-append %config-directory "/signing-key.pub")
60 (compose string->canonical-sexp get-string-all)))
e9c6c584
NK
61
62(define %private-key
cdea30e0
LC
63 (call-with-input-file (string-append %config-directory "/signing-key.sec")
64 (compose string->canonical-sexp get-string-all)))
e9c6c584 65
52f80dfc
LC
66(define* (signature-body bv #:key (public-key %public-key))
67 "Return the signature of BV as the base64-encoded body of a narinfo's
cdea30e0 68'Signature' field."
e9c6c584
NK
69 (base64-encode
70 (string->utf8
71 (canonical-sexp->string
52f80dfc 72 (signature-sexp (bytevector->hash-data (sha256 bv)
e9c6c584
NK
73 #:key-type 'rsa)
74 %private-key
cdea30e0 75 public-key)))))
e9c6c584 76
e9c6c584
NK
77(define %wrong-public-key
78 (string->canonical-sexp "(public-key
79 (rsa
80 (n #00E05873AC2B168760343145918E954EE9AB73C026355693B192E01EE835261AA689E9EF46642E895BCD65C648524059FC450E4BA77A68F4C52D0E39EF0CC9359709AB6AAB153B63782201871325B0FDA19CB401CD99FD0C31A91CA9000AA90A77E82B89E036FB63BC1D3961207469B3B12468977148D376F8012BB12A4B11A8F1#)
81 (e #010001#)
82 )
83 )"))
84
52f80dfc
LC
85(define* (signature-field bv-or-str
86 #:key (version "1") (public-key %public-key))
87 "Return the 'Signature' field value of bytevector/string BV-OR-STR, using
88PUBLIC-KEY as the signature's principal, and using VERSION as the signature
89version identifier.."
90 (string-append version ";example.gnu.org;"
91 (signature-body (if (string? bv-or-str)
92 (string->utf8 bv-or-str)
93 bv-or-str)
94 #:public-key public-key)))
95
e9c6c584 96
cdea30e0 97\f
2c74fde0 98(test-begin "substitute")
e9c6c584 99
f84f8590
LC
100(test-quit "not a number"
101 "signature version"
52f80dfc
LC
102 (narinfo-signature->canonical-sexp
103 (signature-field "foo" #:version "not a number")))
e9c6c584 104
f84f8590
LC
105(test-quit "wrong version number"
106 "unsupported.*version"
52f80dfc
LC
107 (narinfo-signature->canonical-sexp
108 (signature-field "foo" #:version "2")))
e9c6c584
NK
109
110(test-assert "valid narinfo-signature->canonical-sexp"
52f80dfc 111 (canonical-sexp? (narinfo-signature->canonical-sexp (signature-field "foo"))))
e9c6c584 112
e9c6c584 113
cdea30e0 114\f
e9c6c584 115(define %narinfo
52f80dfc 116 ;; Skeleton of the narinfo used below.
cdea30e0
LC
117 (string-append "StorePath: " (%store-prefix)
118 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
e903b7c1
LC
119URL: example.nar
120Compression: none
121NarHash: sha256:" (bytevector->nix-base32-string
122 (sha256 (string->utf8 "Substitutable data."))) "
e9c6c584
NK
123NarSize: 42
124References: bar baz
cdea30e0
LC
125Deriver: " (%store-prefix) "/foo.drv
126System: mips64el-linux\n"))
e9c6c584 127
cdea30e0
LC
128(define (call-with-narinfo narinfo thunk)
129 "Call THUNK in a context where $GUIX_BINARY_SUBSTITUTE_URL is populated with
130a file for NARINFO."
131 (let ((narinfo-directory (and=> (string->uri (getenv
132 "GUIX_BINARY_SUBSTITUTE_URL"))
133 uri-path))
134 (cache-directory (string-append (getenv "XDG_CACHE_HOME")
614c2188 135 "/guix/substitute/")))
cdea30e0
LC
136 (dynamic-wind
137 (lambda ()
138 (when (file-exists? cache-directory)
139 (delete-file-recursively cache-directory))
140 (call-with-output-file (string-append narinfo-directory
141 "/nix-cache-info")
142 (lambda (port)
143 (format port "StoreDir: ~a\nWantMassQuery: 0\n"
144 (%store-prefix))))
145 (call-with-output-file (string-append narinfo-directory "/"
146 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
147 ".narinfo")
148 (cut display narinfo <>))
149
e903b7c1
LC
150 ;; Prepare the nar.
151 (call-with-output-file
152 (string-append narinfo-directory "/example.out")
153 (cut display "Substitutable data." <>))
154 (call-with-output-file
155 (string-append narinfo-directory "/example.nar")
156 (cute write-file
157 (string-append narinfo-directory "/example.out") <>))
158
2c74fde0 159 (set! (@@ (guix scripts substitute)
cdea30e0
LC
160 %allow-unauthenticated-substitutes?)
161 #f))
162 thunk
163 (lambda ()
164 (delete-file-recursively cache-directory)))))
165
166(define-syntax-rule (with-narinfo narinfo body ...)
167 (call-with-narinfo narinfo (lambda () body ...)))
168
2c74fde0 169;; Transmit these options to 'guix substitute'.
218f6ecc 170(substitute-urls (list (getenv "GUIX_BINARY_SUBSTITUTE_URL")))
cdea30e0 171
e903b7c1
LC
172(test-equal "query narinfo without signature"
173 "" ; not substitutable
174
175 (with-narinfo %narinfo
176 (string-trim-both
177 (with-output-to-string
178 (lambda ()
179 (with-input-from-string (string-append "have " (%store-prefix)
180 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
181 (lambda ()
2c74fde0 182 (guix-substitute "--query"))))))))
e903b7c1 183
cdea30e0 184(test-equal "query narinfo with invalid hash"
52f80dfc 185 ;; The hash in the signature differs from the hash of %NARINFO.
cdea30e0
LC
186 ""
187
52f80dfc
LC
188 (with-narinfo (string-append %narinfo "Signature: "
189 (signature-field "different body")
190 "\n")
cdea30e0
LC
191 (string-trim-both
192 (with-output-to-string
193 (lambda ()
194 (with-input-from-string (string-append "have " (%store-prefix)
195 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
196 (lambda ()
2c74fde0 197 (guix-substitute "--query"))))))))
cdea30e0
LC
198
199(test-equal "query narinfo signed with authorized key"
200 (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
201
52f80dfc
LC
202 (with-narinfo (string-append %narinfo "Signature: "
203 (signature-field %narinfo)
204 "\n")
cdea30e0
LC
205 (string-trim-both
206 (with-output-to-string
207 (lambda ()
208 (with-input-from-string (string-append "have " (%store-prefix)
209 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
210 (lambda ()
2c74fde0 211 (guix-substitute "--query"))))))))
cdea30e0
LC
212
213(test-equal "query narinfo signed with unauthorized key"
214 "" ; not substitutable
215
52f80dfc
LC
216 (with-narinfo (string-append %narinfo "Signature: "
217 (signature-field
218 %narinfo
219 #:public-key %wrong-public-key)
220 "\n")
cdea30e0
LC
221 (string-trim-both
222 (with-output-to-string
223 (lambda ()
224 (with-input-from-string (string-append "have " (%store-prefix)
225 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
226 (lambda ()
2c74fde0 227 (guix-substitute "--query"))))))))
cdea30e0 228
f84f8590
LC
229(test-quit "substitute, no signature"
230 "lacks a signature"
e903b7c1 231 (with-narinfo %narinfo
2c74fde0
LC
232 (guix-substitute "--substitute"
233 (string-append (%store-prefix)
234 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
235 "foo")))
e903b7c1 236
f84f8590
LC
237(test-quit "substitute, invalid hash"
238 "hash"
52f80dfc
LC
239 ;; The hash in the signature differs from the hash of %NARINFO.
240 (with-narinfo (string-append %narinfo "Signature: "
241 (signature-field "different body")
242 "\n")
2c74fde0
LC
243 (guix-substitute "--substitute"
244 (string-append (%store-prefix)
245 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
246 "foo")))
cdea30e0 247
f84f8590
LC
248(test-quit "substitute, unauthorized key"
249 "unauthorized"
52f80dfc
LC
250 (with-narinfo (string-append %narinfo "Signature: "
251 (signature-field
252 %narinfo
253 #:public-key %wrong-public-key)
254 "\n")
2c74fde0
LC
255 (guix-substitute "--substitute"
256 (string-append (%store-prefix)
257 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
258 "foo")))
e9c6c584 259
e903b7c1
LC
260(test-equal "substitute, authorized key"
261 "Substitutable data."
262 (with-narinfo (string-append %narinfo "Signature: "
263 (signature-field %narinfo))
264 (dynamic-wind
265 (const #t)
266 (lambda ()
2c74fde0
LC
267 (guix-substitute "--substitute"
268 (string-append (%store-prefix)
269 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
270 "substitute-retrieved")
e903b7c1
LC
271 (call-with-input-file "substitute-retrieved" get-string-all))
272 (lambda ()
273 (false-if-exception (delete-file "substitute-retrieved"))))))
274
2c74fde0 275(test-end "substitute")
e9c6c584 276
cdea30e0
LC
277;;; Local Variables:
278;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
f84f8590 279;;; eval: (put 'test-quit 'scheme-indent-function 2)
cdea30e0 280;;; End: