publish: Encore URIs that appear in narinfos.
[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 base32)
30 #:use-module (guix base64)
31 #:use-module ((guix serialization) #:select (restore-file))
32 #:use-module (guix pk-crypto)
33 #:use-module (web uri)
34 #:use-module (web client)
35 #:use-module (web response)
36 #:use-module (rnrs bytevectors)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-26)
39 #:use-module (srfi srfi-64)
40 #:use-module (ice-9 format)
41 #:use-module (ice-9 match)
42 #:use-module (ice-9 rdelim))
43
44 (define %store
45 (open-connection-for-tests))
46
47 (define %reference (add-text-to-store %store "ref" "foo"))
48
49 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
50
51 (define (http-get-body uri)
52 (call-with-values (lambda () (http-get uri))
53 (lambda (response body) body)))
54
55 (define (publish-uri route)
56 (string-append "http://localhost:6789" route))
57
58 ;; Run a local publishing server in a separate thread.
59 (call-with-new-thread
60 (lambda ()
61 (guix-publish "--port=6789"))) ; attempt to avoid port collision
62
63 ;; Wait until the server is accepting connections.
64 (let ((conn (socket PF_INET SOCK_STREAM 0)))
65 (let loop ()
66 (unless (false-if-exception
67 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") 6789))
68 (loop))))
69
70 \f
71 (test-begin "publish")
72
73 (test-equal "/nix-cache-info"
74 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
75 %store-directory)
76 (http-get-body (publish-uri "/nix-cache-info")))
77
78 (test-equal "/*.narinfo"
79 (let* ((info (query-path-info %store %item))
80 (unsigned-info
81 (format #f
82 "StorePath: ~a
83 URL: nar/~a
84 Compression: none
85 NarHash: sha256:~a
86 NarSize: ~d
87 References: ~a~%"
88 %item
89 (basename %item)
90 (bytevector->nix-base32-string
91 (path-info-hash info))
92 (path-info-nar-size info)
93 (basename (first (path-info-references info)))))
94 (signature (base64-encode
95 (string->utf8
96 (canonical-sexp->string
97 ((@@ (guix scripts publish) signed-string)
98 unsigned-info))))))
99 (format #f "~aSignature: 1;~a;~a~%"
100 unsigned-info (gethostname) signature))
101 (utf8->string
102 (http-get-body
103 (publish-uri
104 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
105
106 (test-equal "/*.narinfo with properly encoded '+' sign"
107 ;; See <http://bugs.gnu.org/21888>.
108 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
109 (info (query-path-info %store item))
110 (unsigned-info
111 (format #f
112 "StorePath: ~a
113 URL: nar/~a
114 Compression: none
115 NarHash: sha256:~a
116 NarSize: ~d
117 References: ~%"
118 item
119 (uri-encode (basename item))
120 (bytevector->nix-base32-string
121 (path-info-hash info))
122 (path-info-nar-size info)))
123 (signature (base64-encode
124 (string->utf8
125 (canonical-sexp->string
126 ((@@ (guix scripts publish) signed-string)
127 unsigned-info))))))
128 (format #f "~aSignature: 1;~a;~a~%"
129 unsigned-info (gethostname) signature))
130
131 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
132 (utf8->string
133 (http-get-body
134 (publish-uri
135 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
136
137 (test-equal "/nar/*"
138 "bar"
139 (call-with-temporary-output-file
140 (lambda (temp port)
141 (let ((nar (utf8->string
142 (http-get-body
143 (publish-uri
144 (string-append "/nar/" (basename %item)))))))
145 (call-with-input-string nar (cut restore-file <> temp)))
146 (call-with-input-file temp read-string))))
147
148 (test-equal "/nar/ with properly encoded '+' sign"
149 "Congrats!"
150 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
151 (call-with-temporary-output-file
152 (lambda (temp port)
153 (let ((nar (utf8->string
154 (http-get-body
155 (publish-uri
156 (string-append "/nar/" (uri-encode (basename item))))))))
157 (call-with-input-string nar (cut restore-file <> temp)))
158 (call-with-input-file temp read-string)))))
159
160 (test-equal "/nar/invalid"
161 404
162 (begin
163 (call-with-output-file (string-append (%store-prefix) "/invalid")
164 (lambda (port)
165 (display "This file is not a valid store item." port)))
166 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
167
168 (test-end "publish")