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