epg-tests.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (require 'ert)
  17. (require 'epg)
  18. (defvar epg-tests-context nil)
  19. (defvar epg-tests-data-directory
  20. (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY"))
  21. "Directory containing epg test data.")
  22. (defconst epg-tests-program-alist-for-passphrase-callback
  23. '((OpenPGP
  24. nil
  25. ("gpg" . "1.4.3"))))
  26. (defun epg-tests-find-usable-gpg-configuration (&optional require-passphrase)
  27. (epg-find-configuration
  28. 'OpenPGP
  29. 'no-cache
  30. (if require-passphrase
  31. epg-tests-program-alist-for-passphrase-callback)))
  32. (defun epg-tests-passphrase-callback (_c _k _d)
  33. ;; Need to create a copy here, since the string will be wiped out
  34. ;; after the use.
  35. (copy-sequence "test0123456789"))
  36. (cl-defmacro with-epg-tests ((&optional &key require-passphrase
  37. require-public-key
  38. require-secret-key)
  39. &rest body)
  40. "Set up temporary locations and variables for testing."
  41. (declare (indent 1))
  42. `(let ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
  43. (unwind-protect
  44. (let ((context (epg-make-context 'OpenPGP)))
  45. (setf (epg-context-program context)
  46. (alist-get 'program
  47. (epg-tests-find-usable-gpg-configuration
  48. ,(if require-passphrase
  49. `'require-passphrase))))
  50. (setf (epg-context-home-directory context)
  51. epg-tests-home-directory)
  52. (setenv "GPG_AGENT_INFO")
  53. ,(if require-passphrase
  54. `(epg-context-set-passphrase-callback
  55. context
  56. #'epg-tests-passphrase-callback))
  57. ,(if require-public-key
  58. `(epg-import-keys-from-file
  59. context
  60. (expand-file-name "pubkey.asc" epg-tests-data-directory)))
  61. ,(if require-secret-key
  62. `(epg-import-keys-from-file
  63. context
  64. (expand-file-name "seckey.asc" epg-tests-data-directory)))
  65. (with-temp-buffer
  66. (make-local-variable 'epg-tests-context)
  67. (setq epg-tests-context context)
  68. ,@body))
  69. (when (file-directory-p epg-tests-home-directory)
  70. (delete-directory epg-tests-home-directory t)))))
  71. (ert-deftest epg-decrypt-1 ()
  72. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  73. (with-epg-tests (:require-passphrase t)
  74. (should (equal "test"
  75. (epg-decrypt-string epg-tests-context "\
  76. -----BEGIN PGP MESSAGE-----
  77. Version: GnuPG v2
  78. jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
  79. =U8z7
  80. -----END PGP MESSAGE-----")))))
  81. (ert-deftest epg-roundtrip-1 ()
  82. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  83. (with-epg-tests (:require-passphrase t)
  84. (let ((cipher (epg-encrypt-string epg-tests-context "symmetric" nil)))
  85. (should (equal "symmetric"
  86. (epg-decrypt-string epg-tests-context cipher))))))
  87. (ert-deftest epg-roundtrip-2 ()
  88. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  89. (with-epg-tests (:require-passphrase t
  90. :require-public-key t
  91. :require-secret-key t)
  92. (let* ((recipients (epg-list-keys epg-tests-context "joe@example.com"))
  93. (cipher (epg-encrypt-string epg-tests-context "public key"
  94. recipients nil t)))
  95. (should (equal "public key"
  96. (epg-decrypt-string epg-tests-context cipher))))))
  97. (ert-deftest epg-sign-verify-1 ()
  98. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  99. (with-epg-tests (:require-passphrase t
  100. :require-public-key t
  101. :require-secret-key t)
  102. (let (signature verify-result)
  103. (setf (epg-context-signers epg-tests-context)
  104. (epg-list-keys epg-tests-context "joe@example.com"))
  105. (setq signature (epg-sign-string epg-tests-context "signed" t))
  106. (epg-verify-string epg-tests-context signature "signed")
  107. (setq verify-result (epg-context-result-for context 'verify))
  108. (should (= 1 (length verify-result)))
  109. (should (eq 'good (epg-signature-status (car verify-result)))))))
  110. (ert-deftest epg-sign-verify-2 ()
  111. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  112. (with-epg-tests (:require-passphrase t
  113. :require-public-key t
  114. :require-secret-key t)
  115. (let (signature verify-result)
  116. (setf (epg-context-signers epg-tests-context)
  117. (epg-list-keys epg-tests-context "joe@example.com"))
  118. (setq signature (epg-sign-string epg-tests-context "clearsigned" 'clear))
  119. ;; Clearsign signature always ends with a new line.
  120. (should (equal "clearsigned\n"
  121. (epg-verify-string epg-tests-context signature)))
  122. (setq verify-result (epg-context-result-for context 'verify))
  123. (should (= 1 (length verify-result)))
  124. (should (eq 'good (epg-signature-status (car verify-result)))))))
  125. (ert-deftest epg-sign-verify-3 ()
  126. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  127. (with-epg-tests (:require-passphrase t
  128. :require-public-key t
  129. :require-secret-key t)
  130. (let (signature verify-result)
  131. (setf (epg-context-signers epg-tests-context)
  132. (epg-list-keys epg-tests-context "joe@example.com"))
  133. (setq signature (epg-sign-string epg-tests-context "normal signed"))
  134. (should (equal "normal signed"
  135. (epg-verify-string epg-tests-context signature)))
  136. (setq verify-result (epg-context-result-for context 'verify))
  137. (should (= 1 (length verify-result)))
  138. (should (eq 'good (epg-signature-status (car verify-result)))))))
  139. (ert-deftest epg-import-1 ()
  140. (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
  141. (with-epg-tests (:require-passphrase nil)
  142. (should (= 0 (length (epg-list-keys epg-tests-context))))
  143. (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
  144. (with-epg-tests (:require-passphrase nil
  145. :require-public-key t)
  146. (should (= 1 (length (epg-list-keys epg-tests-context))))
  147. (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
  148. (with-epg-tests (:require-public-key nil
  149. :require-public-key t
  150. :require-secret-key t)
  151. (should (= 1 (length (epg-list-keys epg-tests-context))))
  152. (should (= 1 (length (epg-list-keys epg-tests-context nil t))))))
  153. (provide 'epg-tests)
  154. ;;; epg-tests.el ends here