1 ;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2016 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 <http://www.gnu.org/licenses/>.
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 (defconst epg-tests-program-alist-for-passphrase-callback
38 (defun epg-tests-find-usable-gpg-configuration (&optional require-passphrase
)
39 (epg-find-configuration
42 (if require-passphrase
43 epg-tests-program-alist-for-passphrase-callback
)))
45 (defun epg-tests-passphrase-callback (_c _k _d
)
46 ;; Need to create a copy here, since the string will be wiped out
48 (copy-sequence "test0123456789"))
50 (cl-defmacro with-epg-tests
((&optional
&key require-passphrase
54 "Set up temporary locations and variables for testing."
56 `(let ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t
)))
58 (let ((context (epg-make-context 'OpenPGP
)))
59 (setf (epg-context-program context
)
61 (epg-tests-find-usable-gpg-configuration
62 ,(if require-passphrase
63 `'require-passphrase
))))
64 (setf (epg-context-home-directory context
)
65 epg-tests-home-directory
)
66 (setenv "GPG_AGENT_INFO")
67 ,(if require-passphrase
68 `(epg-context-set-passphrase-callback
70 #'epg-tests-passphrase-callback
))
71 ,(if require-public-key
72 `(epg-import-keys-from-file
74 (expand-file-name "pubkey.asc" epg-tests-data-directory
)))
75 ,(if require-secret-key
76 `(epg-import-keys-from-file
78 (expand-file-name "seckey.asc" epg-tests-data-directory
)))
80 (make-local-variable 'epg-tests-context
)
81 (setq epg-tests-context context
)
83 (when (file-directory-p epg-tests-home-directory
)
84 (delete-directory epg-tests-home-directory t
)))))
86 (ert-deftest epg-decrypt-1
()
87 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
88 (with-epg-tests (:require-passphrase t
)
90 (epg-decrypt-string epg-tests-context
"\
91 -----BEGIN PGP MESSAGE-----
94 jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
96 -----END PGP MESSAGE-----")))))
98 (ert-deftest epg-roundtrip-1
()
99 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
100 (with-epg-tests (:require-passphrase t
)
101 (let ((cipher (epg-encrypt-string epg-tests-context
"symmetric" nil
)))
102 (should (equal "symmetric"
103 (epg-decrypt-string epg-tests-context cipher
))))))
105 (ert-deftest epg-roundtrip-2
()
106 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
107 (with-epg-tests (:require-passphrase t
108 :require-public-key t
109 :require-secret-key t
)
110 (let* ((recipients (epg-list-keys epg-tests-context
"joe@example.com"))
111 (cipher (epg-encrypt-string epg-tests-context
"public key"
113 (should (equal "public key"
114 (epg-decrypt-string epg-tests-context cipher
))))))
116 (ert-deftest epg-sign-verify-1
()
117 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
118 (with-epg-tests (:require-passphrase t
119 :require-public-key t
120 :require-secret-key t
)
121 (let (signature verify-result
)
122 (setf (epg-context-signers epg-tests-context
)
123 (epg-list-keys epg-tests-context
"joe@example.com"))
124 (setq signature
(epg-sign-string epg-tests-context
"signed" t
))
125 (epg-verify-string epg-tests-context signature
"signed")
126 (setq verify-result
(epg-context-result-for context
'verify
))
127 (should (= 1 (length verify-result
)))
128 (should (eq 'good
(epg-signature-status (car verify-result
)))))))
130 (ert-deftest epg-sign-verify-2
()
131 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
132 (with-epg-tests (:require-passphrase t
133 :require-public-key t
134 :require-secret-key t
)
135 (let (signature verify-result
)
136 (setf (epg-context-signers epg-tests-context
)
137 (epg-list-keys epg-tests-context
"joe@example.com"))
138 (setq signature
(epg-sign-string epg-tests-context
"clearsigned" 'clear
))
139 ;; Clearsign signature always ends with a new line.
140 (should (equal "clearsigned\n"
141 (epg-verify-string epg-tests-context signature
)))
142 (setq verify-result
(epg-context-result-for context
'verify
))
143 (should (= 1 (length verify-result
)))
144 (should (eq 'good
(epg-signature-status (car verify-result
)))))))
146 (ert-deftest epg-sign-verify-3
()
147 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
148 (with-epg-tests (:require-passphrase t
149 :require-public-key t
150 :require-secret-key t
)
151 (let (signature verify-result
)
152 (setf (epg-context-signers epg-tests-context
)
153 (epg-list-keys epg-tests-context
"joe@example.com"))
154 (setq signature
(epg-sign-string epg-tests-context
"normal signed"))
155 (should (equal "normal signed"
156 (epg-verify-string epg-tests-context signature
)))
157 (setq verify-result
(epg-context-result-for context
'verify
))
158 (should (= 1 (length verify-result
)))
159 (should (eq 'good
(epg-signature-status (car verify-result
)))))))
161 (ert-deftest epg-import-1
()
162 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase
))
163 (with-epg-tests (:require-passphrase nil
)
164 (should (= 0 (length (epg-list-keys epg-tests-context
))))
165 (should (= 0 (length (epg-list-keys epg-tests-context nil t
)))))
166 (with-epg-tests (:require-passphrase nil
167 :require-public-key t
)
168 (should (= 1 (length (epg-list-keys epg-tests-context
))))
169 (should (= 0 (length (epg-list-keys epg-tests-context nil t
)))))
170 (with-epg-tests (:require-public-key nil
171 :require-public-key t
172 :require-secret-key t
)
173 (should (= 1 (length (epg-list-keys epg-tests-context
))))
174 (should (= 1 (length (epg-list-keys epg-tests-context nil t
))))))
178 ;;; epg-tests.el ends here