1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 ;; Avoid interference.
20 (unsetenv "http_proxy")
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 base32)
30 #:use-module (guix base64)
31 #:use-module ((guix records) #:select (recutils->alist))
32 #:use-module ((guix serialization) #:select (restore-file))
33 #:use-module (guix pk-crypto)
34 #:use-module (guix zlib)
35 #:use-module (web uri)
36 #:use-module (web client)
37 #:use-module (web response)
38 #:use-module (rnrs bytevectors)
39 #:use-module (ice-9 binary-ports)
40 #:use-module (srfi srfi-1)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-64)
43 #:use-module (ice-9 format)
44 #:use-module (ice-9 match)
45 #:use-module (ice-9 rdelim))
48 (open-connection-for-tests))
50 (define %reference (add-text-to-store %store "ref" "foo"))
52 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
54 (define (http-get-body uri)
55 (call-with-values (lambda () (http-get uri))
56 (lambda (response body) body)))
58 (define (http-get-port uri)
59 (call-with-values (lambda () (http-get uri #:streaming? #t))
60 (lambda (response port) port)))
62 (define (publish-uri route)
63 (string-append "http://localhost:6789" route))
65 ;; Run a local publishing server in a separate thread.
68 (guix-publish "--port=6789" "-C0"))) ;attempt to avoid port collision
70 (define (wait-until-ready port)
71 ;; Wait until the server is accepting connections.
72 (let ((conn (socket PF_INET SOCK_STREAM 0)))
74 (unless (false-if-exception
75 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
78 ;; Wait until the two servers are ready.
79 (wait-until-ready 6789)
82 (test-begin "publish")
84 (test-equal "/nix-cache-info"
85 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
87 (http-get-body (publish-uri "/nix-cache-info")))
89 (test-equal "/*.narinfo"
90 (let* ((info (query-path-info %store %item))
101 (bytevector->nix-base32-string
102 (path-info-hash info))
103 (path-info-nar-size info)
104 (basename (first (path-info-references info)))))
105 (signature (base64-encode
107 (canonical-sexp->string
108 ((@@ (guix scripts publish) signed-string)
110 (format #f "~aSignature: 1;~a;~a~%"
111 unsigned-info (gethostname) signature))
115 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
117 (test-equal "/*.narinfo with properly encoded '+' sign"
118 ;; See <http://bugs.gnu.org/21888>.
119 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
120 (info (query-path-info %store item))
130 (uri-encode (basename item))
131 (bytevector->nix-base32-string
132 (path-info-hash info))
133 (path-info-nar-size info)))
134 (signature (base64-encode
136 (canonical-sexp->string
137 ((@@ (guix scripts publish) signed-string)
139 (format #f "~aSignature: 1;~a;~a~%"
140 unsigned-info (gethostname) signature))
142 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
146 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
150 (call-with-temporary-output-file
152 (let ((nar (utf8->string
155 (string-append "/nar/" (basename %item)))))))
156 (call-with-input-string nar (cut restore-file <> temp)))
157 (call-with-input-file temp read-string))))
159 (unless (zlib-available?)
161 (test-equal "/nar/gzip/*"
163 (call-with-temporary-output-file
165 (let ((nar (http-get-port
167 (string-append "/nar/gzip/" (basename %item))))))
168 (call-with-gzip-input-port nar
169 (cut restore-file <> temp)))
170 (call-with-input-file temp read-string))))
172 (unless (zlib-available?)
174 (test-equal "/*.narinfo with compression"
175 `(("StorePath" . ,%item)
176 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
177 ("Compression" . "gzip"))
178 (let ((thread (call-with-new-thread
180 (guix-publish "--port=6799" "-C5")))))
181 (wait-until-ready 6799)
182 (let* ((url (string-append "http://localhost:6799/"
183 (store-path-hash-part %item) ".narinfo"))
184 (body (http-get-port url)))
185 (filter (lambda (item)
187 (("Compression" . _) #t)
188 (("StorePath" . _) #t)
191 (recutils->alist body)))))
193 (test-equal "/nar/ with properly encoded '+' sign"
195 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
196 (call-with-temporary-output-file
198 (let ((nar (utf8->string
201 (string-append "/nar/" (uri-encode (basename item))))))))
202 (call-with-input-string nar (cut restore-file <> temp)))
203 (call-with-input-file temp read-string)))))
205 (test-equal "/nar/invalid"
208 (call-with-output-file (string-append (%store-prefix) "/invalid")
210 (display "This file is not a valid store item." port)))
211 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))