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 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))
45 (open-connection-for-tests))
47 (define %reference (add-text-to-store %store "ref" "foo"))
49 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
51 (define (http-get-body uri)
52 (call-with-values (lambda () (http-get uri))
53 (lambda (response body) body)))
55 (define (publish-uri route)
56 (string-append "http://localhost:6789" route))
58 ;; Run a local publishing server in a separate thread.
61 (guix-publish "--port=6789"))) ; attempt to avoid port collision
63 ;; Wait until the server is accepting connections.
64 (let ((conn (socket PF_INET SOCK_STREAM 0)))
66 (unless (false-if-exception
67 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") 6789))
71 (test-begin "publish")
73 (test-equal "/nix-cache-info"
74 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
76 (http-get-body (publish-uri "/nix-cache-info")))
78 (test-equal "/*.narinfo"
79 (let* ((info (query-path-info %store %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
96 (canonical-sexp->string
97 ((@@ (guix scripts publish) signed-string)
99 (format #f "~aSignature: 1;~a;~a~%"
100 unsigned-info (gethostname) signature))
104 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
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))
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
125 (canonical-sexp->string
126 ((@@ (guix scripts publish) signed-string)
128 (format #f "~aSignature: 1;~a;~a~%"
129 unsigned-info (gethostname) signature))
131 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
135 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
139 (call-with-temporary-output-file
141 (let ((nar (utf8->string
144 (string-append "/nar/" (basename %item)))))))
145 (call-with-input-string nar (cut restore-file <> temp)))
146 (call-with-input-file temp read-string))))
148 (test-equal "/nar/ with properly encoded '+' sign"
150 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
151 (call-with-temporary-output-file
153 (let ((nar (utf8->string
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)))))
160 (test-equal "/nar/invalid"
163 (call-with-output-file (string-append (%store-prefix) "/invalid")
165 (display "This file is not a valid store item." port)))
166 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))