1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-substitute-binary)
#:use-module (guix scripts substitute-binary)
#:use-module (guix base64)
#:use-module (guix hash)
#:use-module (guix pk-crypto)
#:use-module (guix pki)
#:use-module (rnrs bytevectors)
#:use-module ((srfi srfi-64) #:hide (test-error)))
;; XXX: Replace with 'test-error' from SRFI-64 as soon as it allows to catch
;; specific exceptions.
(define (test-error name key thunk val)
"Test whether THUNK throws a particular error KEY, e.g., 'misc-error, by
comparing the expected VAL and the one returned by the handler. This
procedure assumes that THUNK itself will never return VAL, which is
error-prone but better than catching everything with 'test-error' from
SRFI-64."
(test-equal name val
(catch key
thunk
(const val))))
(define (test-error* name thunk)
;; XXX: This catches all calls to 'exit', which is also error-prone, so it
;; should be replaced in the future.
(test-error name 'quit thunk #t))
(define 1024-bit-rsa
(string->canonical-sexp "(genkey (rsa (nbits 4:1024)))"))
(define %keypair
(generate-key 1024-bit-rsa))
(define %public-key
(find-sexp-token %keypair 'public-key))
(define %private-key
(find-sexp-token %keypair 'private-key))
(define %signature-body
;; XXX: Can we assume UTF-8 here?
(base64-encode
(string->utf8
(canonical-sexp->string
(signature-sexp (bytevector->hash-data (sha256 (string->utf8 "secret")))
%private-key
%public-key)))))
(define %wrong-public-key
(find-sexp-token (generate-key 1024-bit-rsa) 'public-key))
(define %wrong-signature
(let* ((body (string->canonical-sexp
(utf8->string
(base64-decode %signature-body))))
(data (canonical-sexp->string (find-sexp-token body 'data)))
(sig-val (canonical-sexp->string (find-sexp-token body 'sig-val)))
(public-key (canonical-sexp->string %wrong-public-key))
;; XXX: Can we assume UTF-8 here?
(body* (base64-encode
(string->utf8
(string-append "(signature \n" data sig-val
public-key " )\n")))))
(string-append "1;irrelevant;" body*)))
(define (signature str)
(string-append str ";irrelevant;" %signature-body))
(define %acl
(public-keys->acl (list %public-key)))
(test-begin "parse-signature")
(test-error* "not a number"
(lambda ()
(parse-signature (signature "not-a-number") %acl)))
(test-error* "wrong version number"
(lambda ()
(parse-signature (signature "2") %acl)))
(test-error* "unauthorized key"
(lambda ()
(parse-signature (signature "1") (public-keys->acl '()))))
(test-error* "invalid signature"
(lambda ()
(parse-signature %wrong-signature
(public-keys->acl (list %wrong-public-key)))))
(test-assert "valid"
(lambda ()
(parse-signature (signature "1") %acl)))
(test-error* "invalid signature format"
(lambda ()
(parse-signature "no signature here" %acl)))
(test-end "parse-signature")
|