base32.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2015 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-base32)
  19. #:use-module (guix hash)
  20. #:use-module (guix base32)
  21. #:use-module (guix utils)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-34)
  24. #:use-module (srfi srfi-64)
  25. #:use-module (ice-9 rdelim)
  26. #:use-module (ice-9 popen)
  27. #:use-module (ice-9 match)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (rnrs io ports))
  30. ;; Test the (guix base32) module.
  31. (define %nix-hash
  32. (or (and=> (getenv "NIX_HASH")
  33. (match-lambda
  34. ("" #f)
  35. (val val)))
  36. "nix-hash"))
  37. (define %have-nix-hash?
  38. ;; Note: Use `system', not `system*', because of <http://bugs.gnu.org/13166>.
  39. (false-if-exception
  40. (zero? (system (string-append %nix-hash " --version")))))
  41. (test-begin "base32")
  42. (test-assert "bytevector->base32-string"
  43. (fold (lambda (bv expected result)
  44. (and result
  45. (string=? (bytevector->base32-string bv)
  46. expected)))
  47. #t
  48. ;; Examples from RFC 4648.
  49. (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
  50. '(""
  51. "my"
  52. "mzxq"
  53. "mzxw6"
  54. "mzxw6yq"
  55. "mzxw6ytb"
  56. "mzxw6ytboi")))
  57. (test-assert "base32-string->bytevector"
  58. (every (lambda (bv)
  59. (equal? (base32-string->bytevector
  60. (bytevector->base32-string bv))
  61. bv))
  62. ;; Examples from RFC 4648.
  63. (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
  64. (test-assert "nix-base32-string->bytevector"
  65. (every (lambda (bv)
  66. (equal? (nix-base32-string->bytevector
  67. (bytevector->nix-base32-string bv))
  68. bv))
  69. ;; Examples from RFC 4648.
  70. (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
  71. (test-equal "&invalid-base32-character"
  72. #\e
  73. (guard (c ((invalid-base32-character? c)
  74. (invalid-base32-character-value c)))
  75. (nix-base32-string->bytevector
  76. (string-append (make-string 51 #\a) "e"))))
  77. ;; The following test requires `nix-hash' in $PATH.
  78. (unless %have-nix-hash?
  79. (test-skip 1))
  80. (test-assert "sha256 & bytevector->nix-base32-string"
  81. (let ((file (search-path %load-path "tests/test.drv")))
  82. (equal? (bytevector->nix-base32-string
  83. (sha256 (call-with-input-file file get-bytevector-all)))
  84. (let* ((c (format #f "~a --type sha256 --base32 --flat \"~a\""
  85. %nix-hash file))
  86. (p (open-input-pipe c))
  87. (l (read-line p)))
  88. (close-pipe p)
  89. l))))
  90. (test-end)