Commit | Line | Data |
---|---|---|
69927e78 | 1 | ;;; GNU Guix --- Functional package management for GNU |
045111e1 | 2 | ;;; Copyright © 2013, 2014 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) | |
21 | #:use-module (guix utils) | |
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. | |
42 | In 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")) | |
70 | (get))) | |
71 | ||
72 | (test-assert "port-sha256" | |
73 | (let* ((file (search-path %load-path "ice-9/psyntax.scm")) | |
74 | (size (stat:size (stat file))) | |
75 | (contents (call-with-input-file file get-bytevector-all))) | |
76 | (equal? (sha256 contents) | |
77 | (call-with-input-file file port-sha256)))) | |
78 | ||
045111e1 LC |
79 | (test-skip (if (supports-unbuffered-cbip?) 0 4)) |
80 | ||
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))))) | |
87 | ||
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))))) | |
96 | ||
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)))) | |
110 | ||
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)) | |
122 | ||
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)))) | |
127 | ||
69927e78 LC |
128 | (test-end) |
129 | ||
130 | \f | |
131 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |