utils: Move base16 procedures to (guix base16).
[jackhill/guix/guix.git] / tests / hash.scm
CommitLineData
69927e78 1;;; GNU Guix --- Functional package management for GNU
6f389600 2;;; Copyright © 2013, 2014, 2017 Ludovic Courtès <ludo@gnu.org>
69927e78
LC
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-hash)
20 #:use-module (guix hash)
4c0c4db0 21 #:use-module (guix base16)
69927e78
LC
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))
27
28;; Test the (guix hash) module.
29
30(define %empty-sha256
31 ;; SHA256 hash of the empty string.
32 (base16-string->bytevector
33 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
34
35(define %hello-sha256
36 ;; SHA256 hash of "hello world"
37 (base16-string->bytevector
38 "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))
39
045111e1
LC
40(define (supports-unbuffered-cbip?)
41 "Return #t if unbuffered custom binary input ports (CBIPs) are supported.
42In Guile <= 2.0.9, CBIPs were always fully buffered, so the
43'open-sha256-input-port' does not work there."
44 (false-if-exception
45 (setvbuf (make-custom-binary-input-port "foo" pk #f #f #f) _IONBF)))
46
47\f
69927e78
LC
48(test-begin "hash")
49
50(test-equal "sha256, empty"
51 %empty-sha256
52 (sha256 #vu8()))
53
54(test-equal "sha256, hello"
55 %hello-sha256
56 (sha256 (string->utf8 "hello world")))
57
58(test-equal "open-sha256-port, empty"
59 %empty-sha256
60 (let-values (((port get)
61 (open-sha256-port)))
62 (close-port port)
63 (get)))
64
65(test-equal "open-sha256-port, hello"
66 %hello-sha256
67 (let-values (((port get)
68 (open-sha256-port)))
69 (put-bytevector port (string->utf8 "hello world"))
6f389600 70 (force-output port)
69927e78
LC
71 (get)))
72
73(test-assert "port-sha256"
74 (let* ((file (search-path %load-path "ice-9/psyntax.scm"))
75 (size (stat:size (stat file)))
76 (contents (call-with-input-file file get-bytevector-all)))
77 (equal? (sha256 contents)
78 (call-with-input-file file port-sha256))))
79
045111e1
LC
80(test-skip (if (supports-unbuffered-cbip?) 0 4))
81
82(test-equal "open-sha256-input-port, empty"
83 `("" ,%empty-sha256)
84 (let-values (((port get)
85 (open-sha256-input-port (open-string-input-port ""))))
86 (let ((str (get-string-all port)))
87 (list str (get)))))
88
89(test-equal "open-sha256-input-port, hello"
90 `("hello world" ,%hello-sha256)
91 (let-values (((port get)
92 (open-sha256-input-port
93 (open-bytevector-input-port
94 (string->utf8 "hello world")))))
95 (let ((str (get-string-all port)))
96 (list str (get)))))
97
98(test-equal "open-sha256-input-port, hello, one two"
99 (list (string->utf8 "hel") (string->utf8 "lo")
100 (base16-string->bytevector ; echo -n hello | sha256sum
101 "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
102 " world")
103 (let-values (((port get)
104 (open-sha256-input-port
105 (open-bytevector-input-port (string->utf8 "hello world")))))
106 (let* ((one (get-bytevector-n port 3))
107 (two (get-bytevector-n port 2))
108 (hash (get))
109 (three (get-string-all port)))
110 (list one two hash three))))
111
112(test-equal "open-sha256-input-port, hello, read from wrapped port"
113 (list (string->utf8 "hello")
114 (base16-string->bytevector ; echo -n hello | sha256sum
115 "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
116 " world")
117 (let*-values (((wrapped)
118 (open-bytevector-input-port (string->utf8 "hello world")))
119 ((port get)
120 (open-sha256-input-port wrapped)))
121 (let* ((hello (get-bytevector-n port 5))
122 (hash (get))
123
124 ;; Now read from WRAPPED to make sure its current position is
125 ;; correct.
126 (world (get-string-all wrapped)))
127 (list hello hash world))))
128
69927e78 129(test-end)