Merge remote-tracking branch 'origin/master' into core-updates
[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 (setvbuf port _IONBF)
70 port))))
71
72 (define (publish-uri route)
73 (string-append "http://localhost:6789" route))
74
75 ;; Run a local publishing server in a separate thread.
76 (call-with-new-thread
77 (lambda ()
78 (guix-publish "--port=6789" "-C0"))) ;attempt to avoid port collision
79
80 (define (wait-until-ready port)
81 ;; Wait until the server is accepting connections.
82 (let ((conn (socket PF_INET SOCK_STREAM 0)))
83 (let loop ()
84 (unless (false-if-exception
85 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
86 (loop)))))
87
88 ;; Wait until the two servers are ready.
89 (wait-until-ready 6789)
90
91 \f
92 (test-begin "publish")
93
94 (test-equal "/nix-cache-info"
95 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
96 %store-directory)
97 (http-get-body (publish-uri "/nix-cache-info")))
98
99 (test-equal "/*.narinfo"
100 (let* ((info (query-path-info %store %item))
101 (unsigned-info
102 (format #f
103 "StorePath: ~a
104 URL: nar/~a
105 Compression: none
106 NarHash: sha256:~a
107 NarSize: ~d
108 References: ~a~%"
109 %item
110 (basename %item)
111 (bytevector->nix-base32-string
112 (path-info-hash info))
113 (path-info-nar-size info)
114 (basename (first (path-info-references info)))))
115 (signature (base64-encode
116 (string->utf8
117 (canonical-sexp->string
118 ((@@ (guix scripts publish) signed-string)
119 unsigned-info))))))
120 (format #f "~aSignature: 1;~a;~a~%"
121 unsigned-info (gethostname) signature))
122 (utf8->string
123 (http-get-body
124 (publish-uri
125 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
126
127 (test-equal "/*.narinfo with properly encoded '+' sign"
128 ;; See <http://bugs.gnu.org/21888>.
129 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
130 (info (query-path-info %store item))
131 (unsigned-info
132 (format #f
133 "StorePath: ~a
134 URL: nar/~a
135 Compression: none
136 NarHash: sha256:~a
137 NarSize: ~d
138 References: ~%"
139 item
140 (uri-encode (basename item))
141 (bytevector->nix-base32-string
142 (path-info-hash info))
143 (path-info-nar-size info)))
144 (signature (base64-encode
145 (string->utf8
146 (canonical-sexp->string
147 ((@@ (guix scripts publish) signed-string)
148 unsigned-info))))))
149 (format #f "~aSignature: 1;~a;~a~%"
150 unsigned-info (gethostname) signature))
151
152 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
153 (utf8->string
154 (http-get-body
155 (publish-uri
156 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
157
158 (test-equal "/nar/*"
159 "bar"
160 (call-with-temporary-output-file
161 (lambda (temp port)
162 (let ((nar (utf8->string
163 (http-get-body
164 (publish-uri
165 (string-append "/nar/" (basename %item)))))))
166 (call-with-input-string nar (cut restore-file <> temp)))
167 (call-with-input-file temp read-string))))
168
169 (unless (zlib-available?)
170 (test-skip 1))
171 (test-equal "/nar/gzip/*"
172 "bar"
173 (call-with-temporary-output-file
174 (lambda (temp port)
175 (let ((nar (http-get-port
176 (publish-uri
177 (string-append "/nar/gzip/" (basename %item))))))
178 (call-with-gzip-input-port nar
179 (cut restore-file <> temp)))
180 (call-with-input-file temp read-string))))
181
182 (unless (zlib-available?)
183 (test-skip 1))
184 (test-equal "/*.narinfo with compression"
185 `(("StorePath" . ,%item)
186 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
187 ("Compression" . "gzip"))
188 (let ((thread (call-with-new-thread
189 (lambda ()
190 (guix-publish "--port=6799" "-C5")))))
191 (wait-until-ready 6799)
192 (let* ((url (string-append "http://localhost:6799/"
193 (store-path-hash-part %item) ".narinfo"))
194 (body (http-get-port url)))
195 (filter (lambda (item)
196 (match item
197 (("Compression" . _) #t)
198 (("StorePath" . _) #t)
199 (("URL" . _) #t)
200 (_ #f)))
201 (recutils->alist body)))))
202
203 (test-equal "/nar/ with properly encoded '+' sign"
204 "Congrats!"
205 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
206 (call-with-temporary-output-file
207 (lambda (temp port)
208 (let ((nar (utf8->string
209 (http-get-body
210 (publish-uri
211 (string-append "/nar/" (uri-encode (basename item))))))))
212 (call-with-input-string nar (cut restore-file <> temp)))
213 (call-with-input-file temp read-string)))))
214
215 (test-equal "/nar/invalid"
216 404
217 (begin
218 (call-with-output-file (string-append (%store-prefix) "/invalid")
219 (lambda (port)
220 (display "This file is not a valid store item." port)))
221 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
222
223 (test-equal "/file/NAME/sha256/HASH"
224 "Hello, Guix world!"
225 (let* ((data "Hello, Guix world!")
226 (hash (call-with-input-string data port-sha256))
227 (drv (run-with-store %store
228 (gexp->derivation "the-file.txt"
229 #~(call-with-output-file #$output
230 (lambda (port)
231 (display #$data port)))
232 #:hash-algo 'sha256
233 #:hash hash)))
234 (out (build-derivations %store (list drv))))
235 (utf8->string
236 (http-get-body
237 (publish-uri
238 (string-append "/file/the-file.txt/sha256/"
239 (bytevector->nix-base32-string hash)))))))
240
241 (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
242 404
243 (let ((uri (publish-uri
244 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
245 (response-code (http-get uri))))
246
247 (test-equal "/file/NAME/sha256/INVALID-HASH"
248 404
249 (let ((uri (publish-uri
250 (string-append "/file/the-file.txt/sha256/"
251 (bytevector->nix-base32-string
252 (call-with-input-string "" port-sha256))))))
253 (response-code (http-get uri))))
254
255 (test-end "publish")