pack: Add '--relocatable'.
[guix.git] / tests / hash.scm
blobda87616eecd1bc03909ac5280605055b8071808b
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2017, 2018 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/>.
19 (define-module (test-hash)
20   #:use-module (guix hash)
21   #:use-module (guix base16)
22   #:use-module (srfi srfi-1)
23   #:use-module (srfi srfi-11)
24   #:use-module (srfi srfi-64)
25   #:use-module (rnrs bytevectors)
26   #:use-module (rnrs io ports))
28 ;; Test the (guix hash) module.
30 (define %empty-sha256
31   ;; SHA256 hash of the empty string.
32   (base16-string->bytevector
33    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
35 (define %hello-sha256
36   ;; SHA256 hash of "hello world"
37   (base16-string->bytevector
38    "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))
41 (test-begin "hash")
43 (test-equal "sha1, empty"
44   (base16-string->bytevector "da39a3ee5e6b4b0d3255bfef95601890afd80709")
45   (sha1 #vu8()))
47 (test-equal "sha1, hello"
48   (base16-string->bytevector "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
49   (sha1 (string->utf8 "hello world")))
51 (test-equal "sha256, empty"
52   %empty-sha256
53   (sha256 #vu8()))
55 (test-equal "sha256, hello"
56   %hello-sha256
57   (sha256 (string->utf8 "hello world")))
59 (test-equal "open-sha256-port, empty"
60   %empty-sha256
61   (let-values (((port get)
62                 (open-sha256-port)))
63     (close-port port)
64     (get)))
66 (test-equal "open-sha256-port, hello"
67   %hello-sha256
68   (let-values (((port get)
69                 (open-sha256-port)))
70     (put-bytevector port (string->utf8 "hello world"))
71     (force-output port)
72     (get)))
74 (test-assert "port-sha256"
75   (let* ((file     (search-path %load-path "ice-9/psyntax.scm"))
76          (size     (stat:size (stat file)))
77          (contents (call-with-input-file file get-bytevector-all)))
78     (equal? (sha256 contents)
79             (call-with-input-file file port-sha256))))
81 (test-equal "open-sha256-input-port, empty"
82   `("" ,%empty-sha256)
83   (let-values (((port get)
84                 (open-sha256-input-port (open-string-input-port ""))))
85     (let ((str (get-string-all port)))
86       (list str (get)))))
88 (test-equal "open-sha256-input-port, hello"
89   `("hello world" ,%hello-sha256)
90   (let-values (((port get)
91                 (open-sha256-input-port
92                  (open-bytevector-input-port
93                   (string->utf8 "hello world")))))
94     (let ((str (get-string-all port)))
95       (list str (get)))))
97 (test-equal "open-sha256-input-port, hello, one two"
98   (list (string->utf8 "hel") (string->utf8 "lo")
99         (base16-string->bytevector                ; echo -n hello | sha256sum
100          "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
101         " world")
102   (let-values (((port get)
103                 (open-sha256-input-port
104                  (open-bytevector-input-port (string->utf8 "hello world")))))
105     (let* ((one   (get-bytevector-n port 3))
106            (two   (get-bytevector-n port 2))
107            (hash  (get))
108            (three (get-string-all port)))
109       (list one two hash three))))
111 (test-equal "open-sha256-input-port, hello, read from wrapped port"
112   (list (string->utf8 "hello")
113         (base16-string->bytevector                ; echo -n hello | sha256sum
114          "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
115         " world")
116   (let*-values (((wrapped)
117                  (open-bytevector-input-port (string->utf8 "hello world")))
118                 ((port get)
119                  (open-sha256-input-port wrapped)))
120     (let* ((hello (get-bytevector-n port 5))
121            (hash  (get))
123            ;; Now read from WRAPPED to make sure its current position is
124            ;; correct.
125            (world (get-string-all wrapped)))
126       (list hello hash world))))
128 (test-end)