gnu: cool-retro-term: Upgrade to 1.1.1.
[jackhill/guix/guix.git] / tests / base32.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
1a706ff5 2;;; Copyright © 2012, 2013, 2015 Ludovic Courtès <ludo@gnu.org>
ddc29a78 3;;;
233e7676 4;;; This file is part of GNU Guix.
ddc29a78 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
ddc29a78
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
ddc29a78
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
ddc29a78
LC
18
19(define-module (test-base32)
ca719424 20 #:use-module (gcrypt hash)
ddc29a78
LC
21 #:use-module (guix base32)
22 #:use-module (guix utils)
23 #:use-module (srfi srfi-1)
1a706ff5 24 #:use-module (srfi srfi-34)
ddc29a78
LC
25 #:use-module (srfi srfi-64)
26 #:use-module (ice-9 rdelim)
27 #:use-module (ice-9 popen)
fdb50f8d 28 #:use-module (ice-9 match)
ddc29a78
LC
29 #:use-module (rnrs bytevectors)
30 #:use-module (rnrs io ports))
31
32;; Test the (guix base32) module.
33
34(define %nix-hash
fdb50f8d
LC
35 (or (and=> (getenv "NIX_HASH")
36 (match-lambda
37 ("" #f)
38 (val val)))
ddc29a78
LC
39 "nix-hash"))
40
fdb50f8d
LC
41(define %have-nix-hash?
42 ;; Note: Use `system', not `system*', because of <http://bugs.gnu.org/13166>.
43 (false-if-exception
44 (zero? (system (string-append %nix-hash " --version")))))
45
ddc29a78
LC
46(test-begin "base32")
47
48(test-assert "bytevector->base32-string"
49 (fold (lambda (bv expected result)
50 (and result
51 (string=? (bytevector->base32-string bv)
52 expected)))
53 #t
54
55 ;; Examples from RFC 4648.
56 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
57 '(""
58 "my"
59 "mzxq"
60 "mzxw6"
61 "mzxw6yq"
62 "mzxw6ytb"
63 "mzxw6ytboi")))
64
65(test-assert "base32-string->bytevector"
66 (every (lambda (bv)
67 (equal? (base32-string->bytevector
68 (bytevector->base32-string bv))
69 bv))
70 ;; Examples from RFC 4648.
71 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
72
73(test-assert "nix-base32-string->bytevector"
74 (every (lambda (bv)
75 (equal? (nix-base32-string->bytevector
76 (bytevector->nix-base32-string bv))
77 bv))
78 ;; Examples from RFC 4648.
79 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
80
1a706ff5
LC
81(test-equal "&invalid-base32-character"
82 #\e
83 (guard (c ((invalid-base32-character? c)
84 (invalid-base32-character-value c)))
85 (nix-base32-string->bytevector
86 (string-append (make-string 51 #\a) "e"))))
87
fdb50f8d
LC
88;; The following test requires `nix-hash' in $PATH.
89(unless %have-nix-hash?
90 (test-skip 1))
ddc29a78
LC
91
92(test-assert "sha256 & bytevector->nix-base32-string"
93 (let ((file (search-path %load-path "tests/test.drv")))
94 (equal? (bytevector->nix-base32-string
95 (sha256 (call-with-input-file file get-bytevector-all)))
96 (let* ((c (format #f "~a --type sha256 --base32 --flat \"~a\""
97 %nix-hash file))
98 (p (open-input-pipe c))
99 (l (read-line p)))
100 (close-pipe p)
101 l))))
102
103(test-end)