Update license headers.
[jackhill/guix/guix.git] / tests / base32.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012 Ludovic Courtès <ludo@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 (define-module (test-base32)
20 #:use-module (guix base32)
21 #:use-module (guix utils)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-64)
24 #:use-module (ice-9 rdelim)
25 #:use-module (ice-9 popen)
26 #:use-module (ice-9 match)
27 #:use-module (rnrs bytevectors)
28 #:use-module (rnrs io ports))
29
30 ;; Test the (guix base32) module.
31
32 (define %nix-hash
33 (or (and=> (getenv "NIX_HASH")
34 (match-lambda
35 ("" #f)
36 (val val)))
37 "nix-hash"))
38
39 (define %have-nix-hash?
40 ;; Note: Use `system', not `system*', because of <http://bugs.gnu.org/13166>.
41 (false-if-exception
42 (zero? (system (string-append %nix-hash " --version")))))
43
44 (test-begin "base32")
45
46 (test-assert "bytevector->base32-string"
47 (fold (lambda (bv expected result)
48 (and result
49 (string=? (bytevector->base32-string bv)
50 expected)))
51 #t
52
53 ;; Examples from RFC 4648.
54 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
55 '(""
56 "my"
57 "mzxq"
58 "mzxw6"
59 "mzxw6yq"
60 "mzxw6ytb"
61 "mzxw6ytboi")))
62
63 (test-assert "base32-string->bytevector"
64 (every (lambda (bv)
65 (equal? (base32-string->bytevector
66 (bytevector->base32-string bv))
67 bv))
68 ;; Examples from RFC 4648.
69 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
70
71 (test-assert "nix-base32-string->bytevector"
72 (every (lambda (bv)
73 (equal? (nix-base32-string->bytevector
74 (bytevector->nix-base32-string bv))
75 bv))
76 ;; Examples from RFC 4648.
77 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
78
79 ;; The following test requires `nix-hash' in $PATH.
80 (unless %have-nix-hash?
81 (test-skip 1))
82
83 (test-assert "sha256 & bytevector->nix-base32-string"
84 (let ((file (search-path %load-path "tests/test.drv")))
85 (equal? (bytevector->nix-base32-string
86 (sha256 (call-with-input-file file get-bytevector-all)))
87 (let* ((c (format #f "~a --type sha256 --base32 --flat \"~a\""
88 %nix-hash file))
89 (p (open-input-pipe c))
90 (l (read-line p)))
91 (close-pipe p)
92 l))))
93
94 (test-end)
95
96 \f
97 (exit (= (test-runner-fail-count (test-runner-current)) 0))
98
99 ;;; Local Variables:
100 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
101 ;;; End: