Add `base32-string->bytevector' and `nix-base32-string->bytevector'.
[guix.git] / tests / utils.scm
blobedea11db720094e0f500c07424d203a0a7e56abb
1 ;;; Guix --- Nix package management from Guile.         -*- coding: utf-8 -*-
2 ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of Guix.
5 ;;;
6 ;;; 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 ;;; 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 Guix.  If not, see <http://www.gnu.org/licenses/>.
20 (define-module (test-utils)
21   #:use-module (guix utils)
22   #:use-module (srfi srfi-1)
23   #:use-module (srfi srfi-26)
24   #:use-module (srfi srfi-64)
25   #:use-module (rnrs bytevectors)
26   #:use-module (rnrs io ports)
27   #:use-module (ice-9 rdelim)
28   #:use-module (ice-9 popen))
30 (test-begin "utils")
32 (test-assert "bytevector->base32-string"
33   (fold (lambda (bv expected result)
34           (and result
35                (string=? (bytevector->base32-string bv)
36                          expected)))
37         #t
39         ;; Examples from RFC 4648.
40         (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
41         '(""
42           "my"
43           "mzxq"
44           "mzxw6"
45           "mzxw6yq"
46           "mzxw6ytb"
47           "mzxw6ytboi")))
49 (test-assert "base32-string->bytevector"
50   (every (lambda (bv)
51            (equal? (base32-string->bytevector
52                     (bytevector->base32-string bv))
53                    bv))
54          ;; Examples from RFC 4648.
55          (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
57 (test-assert "nix-base32-string->bytevector"
58   (every (lambda (bv)
59            (equal? (nix-base32-string->bytevector
60                     (bytevector->nix-base32-string bv))
61                    bv))
62          ;; Examples from RFC 4648.
63          (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
65 ;; The following tests requires `nix-hash' in $PATH.
66 (test-skip (if (false-if-exception (system* "nix-hash" "--version"))
67                0
68                1))
70 (test-assert "sha256 & bytevector->nix-base32-string"
71   (let ((file (search-path %load-path "tests/test.drv")))
72     (equal? (bytevector->nix-base32-string
73              (sha256 (call-with-input-file file get-bytevector-all)))
74             (let* ((c (format #f "nix-hash --type sha256 --base32 --flat \"~a\""
75                               file))
76                    (p (open-input-pipe c))
77                    (l (read-line p)))
78               (close-pipe p)
79               l))))
81 (test-end)
84 (exit (= (test-runner-fail-count (test-runner-current)) 0))
86 ;;; Local Variables:
87 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
88 ;;; End: