gnupg.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 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 (guix tests gnupg)
  19. #:use-module (guix openpgp)
  20. #:use-module (guix utils)
  21. #:use-module (guix build utils)
  22. #:use-module (rnrs io ports)
  23. #:use-module (ice-9 match)
  24. #:export (gpg-command
  25. gpgconf-command
  26. with-fresh-gnupg-setup
  27. %ed25519-public-key-file
  28. %ed25519-secret-key-file
  29. %ed25519bis-public-key-file
  30. %ed25519bis-secret-key-file
  31. read-openpgp-packet
  32. key-fingerprint
  33. key-id))
  34. (define gpg-command
  35. (make-parameter "gpg"))
  36. (define gpgconf-command
  37. (make-parameter "gpgconf"))
  38. (define (call-with-fresh-gnupg-setup imported thunk)
  39. (call-with-temporary-directory
  40. (lambda (home)
  41. (with-environment-variables `(("GNUPGHOME" ,home))
  42. (dynamic-wind
  43. (lambda ()
  44. (for-each (lambda (file)
  45. (invoke (gpg-command) "--import" file))
  46. imported))
  47. thunk
  48. (lambda ()
  49. ;; Terminate 'gpg-agent' & co.
  50. (invoke (gpgconf-command) "--kill" "all")))))))
  51. (define-syntax-rule (with-fresh-gnupg-setup imported exp ...)
  52. "Evaluate EXP in the context of a fresh GnuPG setup where all the files
  53. listed in IMPORTED, and only them, have been imported. This sets 'GNUPGHOME'
  54. such that the user's real GnuPG files are left untouched. The 'gpg-agent'
  55. process is terminated afterwards."
  56. (call-with-fresh-gnupg-setup imported (lambda () exp ...)))
  57. (define %ed25519-public-key-file
  58. (search-path %load-path "tests/ed25519.key"))
  59. (define %ed25519-secret-key-file
  60. (search-path %load-path "tests/ed25519.sec"))
  61. (define %ed25519bis-public-key-file
  62. (search-path %load-path "tests/ed25519bis.key"))
  63. (define %ed25519bis-secret-key-file
  64. (search-path %load-path "tests/ed25519bis.sec"))
  65. (define (read-openpgp-packet file)
  66. (get-openpgp-packet
  67. (open-bytevector-input-port
  68. (call-with-input-file file read-radix-64))))
  69. (define key-fingerprint
  70. (compose openpgp-format-fingerprint
  71. openpgp-public-key-fingerprint
  72. read-openpgp-packet))