hash.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2017 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/>.
  18. (define-module (test-hash)
  19. #:use-module (guix hash)
  20. #:use-module (guix base16)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-11)
  23. #:use-module (srfi srfi-64)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (rnrs io ports))
  26. ;; Test the (guix hash) module.
  27. (define %empty-sha256
  28. ;; SHA256 hash of the empty string.
  29. (base16-string->bytevector
  30. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
  31. (define %hello-sha256
  32. ;; SHA256 hash of "hello world"
  33. (base16-string->bytevector
  34. "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))
  35. (define (supports-unbuffered-cbip?)
  36. "Return #t if unbuffered custom binary input ports (CBIPs) are supported.
  37. In Guile <= 2.0.9, CBIPs were always fully buffered, so the
  38. 'open-sha256-input-port' does not work there."
  39. (false-if-exception
  40. (setvbuf (make-custom-binary-input-port "foo" pk #f #f #f) _IONBF)))
  41. (test-begin "hash")
  42. (test-equal "sha256, empty"
  43. %empty-sha256
  44. (sha256 #vu8()))
  45. (test-equal "sha256, hello"
  46. %hello-sha256
  47. (sha256 (string->utf8 "hello world")))
  48. (test-equal "open-sha256-port, empty"
  49. %empty-sha256
  50. (let-values (((port get)
  51. (open-sha256-port)))
  52. (close-port port)
  53. (get)))
  54. (test-equal "open-sha256-port, hello"
  55. %hello-sha256
  56. (let-values (((port get)
  57. (open-sha256-port)))
  58. (put-bytevector port (string->utf8 "hello world"))
  59. (force-output port)
  60. (get)))
  61. (test-assert "port-sha256"
  62. (let* ((file (search-path %load-path "ice-9/psyntax.scm"))
  63. (size (stat:size (stat file)))
  64. (contents (call-with-input-file file get-bytevector-all)))
  65. (equal? (sha256 contents)
  66. (call-with-input-file file port-sha256))))
  67. (test-skip (if (supports-unbuffered-cbip?) 0 4))
  68. (test-equal "open-sha256-input-port, empty"
  69. `("" ,%empty-sha256)
  70. (let-values (((port get)
  71. (open-sha256-input-port (open-string-input-port ""))))
  72. (let ((str (get-string-all port)))
  73. (list str (get)))))
  74. (test-equal "open-sha256-input-port, hello"
  75. `("hello world" ,%hello-sha256)
  76. (let-values (((port get)
  77. (open-sha256-input-port
  78. (open-bytevector-input-port
  79. (string->utf8 "hello world")))))
  80. (let ((str (get-string-all port)))
  81. (list str (get)))))
  82. (test-equal "open-sha256-input-port, hello, one two"
  83. (list (string->utf8 "hel") (string->utf8 "lo")
  84. (base16-string->bytevector ; echo -n hello | sha256sum
  85. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  86. " world")
  87. (let-values (((port get)
  88. (open-sha256-input-port
  89. (open-bytevector-input-port (string->utf8 "hello world")))))
  90. (let* ((one (get-bytevector-n port 3))
  91. (two (get-bytevector-n port 2))
  92. (hash (get))
  93. (three (get-string-all port)))
  94. (list one two hash three))))
  95. (test-equal "open-sha256-input-port, hello, read from wrapped port"
  96. (list (string->utf8 "hello")
  97. (base16-string->bytevector ; echo -n hello | sha256sum
  98. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  99. " world")
  100. (let*-values (((wrapped)
  101. (open-bytevector-input-port (string->utf8 "hello world")))
  102. ((port get)
  103. (open-sha256-input-port wrapped)))
  104. (let* ((hello (get-bytevector-n port 5))
  105. (hash (get))
  106. ;; Now read from WRAPPED to make sure its current position is
  107. ;; correct.
  108. (world (get-string-all wrapped)))
  109. (list hello hash world))))
  110. (test-end)