uuid.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2017, 2018, 2019 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-uuid)
  19. #:use-module (gnu system uuid)
  20. #:use-module (srfi srfi-64)
  21. #:use-module (rnrs bytevectors))
  22. (test-begin "uuid")
  23. (test-equal "uuid->string"
  24. "c5307e6b-d1ba-499d-89c5-cb0b143577c4"
  25. (uuid->string
  26. #vu8(197 48 126 107 209 186 73 157 137 197 203 11 20 53 119 196)))
  27. (test-equal "string->uuid"
  28. '(16 "4dab5feb-d176-45de-b287-9b0a6e4c01cb")
  29. (let ((uuid (string->uuid "4dab5feb-d176-45de-b287-9b0a6e4c01cb")))
  30. (list (bytevector-length uuid) (uuid->string uuid))))
  31. (test-assert "uuid"
  32. (let ((str "4dab5feb-d176-45de-b287-9b0a6e4c01cb"))
  33. (bytevector=? (uuid-bytevector
  34. (uuid "4dab5feb-d176-45de-b287-9b0a6e4c01cb"))
  35. (string->uuid "4dab5feb-d176-45de-b287-9b0a6e4c01cb"))))
  36. (test-assert "uuid, syntax error"
  37. (catch 'syntax-error
  38. (lambda ()
  39. (eval '(uuid "foobar") (current-module))
  40. #f)
  41. (lambda (key proc message location form . args)
  42. (and (eq? proc 'uuid)
  43. (string-contains message "invalid UUID")
  44. (equal? form '(uuid "foobar" 'dce))))))
  45. (test-equal "uuid, ISO-9660, format preserved"
  46. "1970-01-01-17-14-42-99"
  47. (uuid->string (uuid "1970-01-01-17-14-42-99" 'iso9660)))
  48. (test-equal "uuid, FAT32, format preserved"
  49. "1234-ABCD"
  50. (uuid->string (uuid "1234-abcd" 'fat32)))
  51. (test-equal "uuid, FAT32, leading zeros preserved"
  52. "00CA-050E" ;<https://bugs.gnu.org/35582>
  53. (uuid->string (uuid "00CA-050E" 'fat32)))
  54. (test-assert "uuid, dynamic value"
  55. (let* ((good "4dab5feb-d176-45de-b287-9b0a6e4c01cb")
  56. (bad (string-drop good 3)))
  57. (and (uuid? (uuid good))
  58. (string=? good (uuid->string (uuid good)))
  59. (not (uuid bad)))))
  60. (test-assert "uuid=?"
  61. (and (uuid=? (uuid-bytevector (uuid "1234-abcd" 'fat32))
  62. (uuid "1234-abcd" 'fat32))
  63. (uuid=? (uuid "1234-abcd" 'fat32)
  64. (uuid "1234-abcd" 'fat))))
  65. (test-end)