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