tests: Work around Guile bug with unbuffered custom binary input ports.
[jackhill/guix/guix.git] / tests / publish.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@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 ;; Avoid interference.
20 (unsetenv "http_proxy")
21
22 (define-module (test-publish)
23 #:use-module (guix scripts publish)
24 #:use-module (guix tests)
25 #:use-module (guix config)
26 #:use-module (guix utils)
27 #:use-module (guix hash)
28 #:use-module (guix store)
29 #:use-module (guix derivations)
30 #:use-module (guix gexp)
31 #:use-module (guix base32)
32 #:use-module (guix base64)
33 #:use-module ((guix records) #:select (recutils->alist))
34 #:use-module ((guix serialization) #:select (restore-file))
35 #:use-module (guix pk-crypto)
36 #:use-module (guix zlib)
37 #:use-module (web uri)
38 #:use-module (web client)
39 #:use-module (web response)
40 #:use-module (rnrs bytevectors)
41 #:use-module (ice-9 binary-ports)
42 #:use-module (srfi srfi-1)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-64)
45 #:use-module (ice-9 format)
46 #:use-module (ice-9 match)
47 #:use-module (ice-9 rdelim))
48
49 (define %store
50 (open-connection-for-tests))
51
52 (define %reference (add-text-to-store %store "ref" "foo"))
53
54 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
55
56 (define (http-get-body uri)
57 (call-with-values (lambda () (http-get uri))
58 (lambda (response body) body)))
59
60 (define (http-get-port uri)
61 (let ((socket (open-socket-for-uri uri)))
62 ;; Make sure to use an unbuffered port so that we can then peek at the
63 ;; underlying file descriptor via 'call-with-gzip-input-port'.
64 (setvbuf socket _IONBF)
65 (call-with-values
66 (lambda ()
67 (http-get uri #:port socket #:streaming? #t))
68 (lambda (response port)
69 ;; Don't (setvbuf port _IONBF) because of <http://bugs.gnu.org/19610>
70 ;; (PORT might be a custom binary input port).
71 port))))
72
73 (define (publish-uri route)
74 (string-append "http://localhost:6789" route))
75
76 ;; Run a local publishing server in a separate thread.
77 (call-with-new-thread
78 (lambda ()
79 (guix-publish "--port=6789" "-C0"))) ;attempt to avoid port collision
80
81 (define (wait-until-ready port)
82 ;; Wait until the server is accepting connections.
83 (let ((conn (socket PF_INET SOCK_STREAM 0)))
84 (let loop ()
85 (unless (false-if-exception
86 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
87 (loop)))))
88
89 ;; Wait until the two servers are ready.
90 (wait-until-ready 6789)
91
92 \f
93 (test-begin "publish")
94
95 (test-equal "/nix-cache-info"
96 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
97 %store-directory)
98 (http-get-body (publish-uri "/nix-cache-info")))
99
100 (test-equal "/*.narinfo"
101 (let* ((info (query-path-info %store %item))
102 (unsigned-info
103 (format #f
104 "StorePath: ~a
105 URL: nar/~a
106 Compression: none
107 NarHash: sha256:~a
108 NarSize: ~d
109 References: ~a~%"
110 %item
111 (basename %item)
112 (bytevector->nix-base32-string
113 (path-info-hash info))
114 (path-info-nar-size info)
115 (basename (first (path-info-references info)))))
116 (signature (base64-encode
117 (string->utf8
118 (canonical-sexp->string
119 ((@@ (guix scripts publish) signed-string)
120 unsigned-info))))))
121 (format #f "~aSignature: 1;~a;~a~%"
122 unsigned-info (gethostname) signature))
123 (utf8->string
124 (http-get-body
125 (publish-uri
126 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
127
128 (test-equal "/*.narinfo with properly encoded '+' sign"
129 ;; See <http://bugs.gnu.org/21888>.
130 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
131 (info (query-path-info %store item))
132 (unsigned-info
133 (format #f
134 "StorePath: ~a
135 URL: nar/~a
136 Compression: none
137 NarHash: sha256:~a
138 NarSize: ~d
139 References: ~%"
140 item
141 (uri-encode (basename item))
142 (bytevector->nix-base32-string
143 (path-info-hash info))
144 (path-info-nar-size info)))
145 (signature (base64-encode
146 (string->utf8
147 (canonical-sexp->string
148 ((@@ (guix scripts publish) signed-string)
149 unsigned-info))))))
150 (format #f "~aSignature: 1;~a;~a~%"
151 unsigned-info (gethostname) signature))
152
153 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
154 (utf8->string
155 (http-get-body
156 (publish-uri
157 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
158
159 (test-equal "/nar/*"
160 "bar"
161 (call-with-temporary-output-file
162 (lambda (temp port)
163 (let ((nar (utf8->string
164 (http-get-body
165 (publish-uri
166 (string-append "/nar/" (basename %item)))))))
167 (call-with-input-string nar (cut restore-file <> temp)))
168 (call-with-input-file temp read-string))))
169
170 (unless (zlib-available?)
171 (test-skip 1))
172 (test-equal "/nar/gzip/*"
173 "bar"
174 (call-with-temporary-output-file
175 (lambda (temp port)
176 (let ((nar (http-get-port
177 (publish-uri
178 (string-append "/nar/gzip/" (basename %item))))))
179 (call-with-gzip-input-port nar
180 (cut restore-file <> temp)))
181 (call-with-input-file temp read-string))))
182
183 (unless (zlib-available?)
184 (test-skip 1))
185 (test-equal "/*.narinfo with compression"
186 `(("StorePath" . ,%item)
187 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
188 ("Compression" . "gzip"))
189 (let ((thread (call-with-new-thread
190 (lambda ()
191 (guix-publish "--port=6799" "-C5")))))
192 (wait-until-ready 6799)
193 (let* ((url (string-append "http://localhost:6799/"
194 (store-path-hash-part %item) ".narinfo"))
195 (body (http-get-port url)))
196 (filter (lambda (item)
197 (match item
198 (("Compression" . _) #t)
199 (("StorePath" . _) #t)
200 (("URL" . _) #t)
201 (_ #f)))
202 (recutils->alist body)))))
203
204 (unless (zlib-available?)
205 (test-skip 1))
206 (test-equal "/*.narinfo for a compressed file"
207 '("none" "nar") ;compression-less nar
208 ;; Assume 'guix publish -C' is already running on port 6799.
209 (let* ((item (add-text-to-store %store "fake.tar.gz"
210 "This is a fake compressed file."))
211 (url (string-append "http://localhost:6799/"
212 (store-path-hash-part item) ".narinfo"))
213 (body (http-get-port url))
214 (info (recutils->alist body)))
215 (list (assoc-ref info "Compression")
216 (dirname (assoc-ref info "URL")))))
217
218 (test-equal "/nar/ with properly encoded '+' sign"
219 "Congrats!"
220 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
221 (call-with-temporary-output-file
222 (lambda (temp port)
223 (let ((nar (utf8->string
224 (http-get-body
225 (publish-uri
226 (string-append "/nar/" (uri-encode (basename item))))))))
227 (call-with-input-string nar (cut restore-file <> temp)))
228 (call-with-input-file temp read-string)))))
229
230 (test-equal "/nar/invalid"
231 404
232 (begin
233 (call-with-output-file (string-append (%store-prefix) "/invalid")
234 (lambda (port)
235 (display "This file is not a valid store item." port)))
236 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
237
238 (test-equal "/file/NAME/sha256/HASH"
239 "Hello, Guix world!"
240 (let* ((data "Hello, Guix world!")
241 (hash (call-with-input-string data port-sha256))
242 (drv (run-with-store %store
243 (gexp->derivation "the-file.txt"
244 #~(call-with-output-file #$output
245 (lambda (port)
246 (display #$data port)))
247 #:hash-algo 'sha256
248 #:hash hash)))
249 (out (build-derivations %store (list drv))))
250 (utf8->string
251 (http-get-body
252 (publish-uri
253 (string-append "/file/the-file.txt/sha256/"
254 (bytevector->nix-base32-string hash)))))))
255
256 (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
257 404
258 (let ((uri (publish-uri
259 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
260 (response-code (http-get uri))))
261
262 (test-equal "/file/NAME/sha256/INVALID-HASH"
263 404
264 (let ((uri (publish-uri
265 (string-append "/file/the-file.txt/sha256/"
266 (bytevector->nix-base32-string
267 (call-with-input-string "" port-sha256))))))
268 (response-code (http-get uri))))
269
270 (test-end "publish")