You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

129 lines
4.3 KiB

  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/>.
  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. (test-begin "hash")
  36. (test-equal "sha1, empty"
  37. (base16-string->bytevector "da39a3ee5e6b4b0d3255bfef95601890afd80709")
  38. (sha1 #vu8()))
  39. (test-equal "sha1, hello"
  40. (base16-string->bytevector "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
  41. (sha1 (string->utf8 "hello world")))
  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. (list %hello-sha256 (string-length "hello world"))
  56. (let-values (((port get)
  57. (open-sha256-port)))
  58. (put-bytevector port (string->utf8 "hello world"))
  59. (force-output port)
  60. (list (get) (port-position port))))
  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-equal "open-sha256-input-port, empty"
  68. `("" ,%empty-sha256)
  69. (let-values (((port get)
  70. (open-sha256-input-port (open-string-input-port ""))))
  71. (let ((str (get-string-all port)))
  72. (list str (get)))))
  73. (test-equal "open-sha256-input-port, hello"
  74. `("hello world" ,%hello-sha256)
  75. (let-values (((port get)
  76. (open-sha256-input-port
  77. (open-bytevector-input-port
  78. (string->utf8 "hello world")))))
  79. (let ((str (get-string-all port)))
  80. (list str (get)))))
  81. (test-equal "open-sha256-input-port, hello, one two"
  82. (list (string->utf8 "hel") (string->utf8 "lo")
  83. (base16-string->bytevector ; echo -n hello | sha256sum
  84. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  85. " world")
  86. (let-values (((port get)
  87. (open-sha256-input-port
  88. (open-bytevector-input-port (string->utf8 "hello world")))))
  89. (let* ((one (get-bytevector-n port 3))
  90. (two (get-bytevector-n port 2))
  91. (hash (get))
  92. (three (get-string-all port)))
  93. (list one two hash three))))
  94. (test-equal "open-sha256-input-port, hello, read from wrapped port"
  95. (list (string->utf8 "hello")
  96. (base16-string->bytevector ; echo -n hello | sha256sum
  97. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  98. " world")
  99. (let*-values (((wrapped)
  100. (open-bytevector-input-port (string->utf8 "hello world")))
  101. ((port get)
  102. (open-sha256-input-port wrapped)))
  103. (let* ((hello (get-bytevector-n port 5))
  104. (hash (get))
  105. ;; Now read from WRAPPED to make sure its current position is
  106. ;; correct.
  107. (world (get-string-all wrapped)))
  108. (list hello hash world))))
  109. (test-end)