]> code.delx.au - gnu-emacs/blob - test/lisp/epg-tests.el
epg: Add a way to detect gpg1 executable for tests
[gnu-emacs] / test / lisp / epg-tests.el
1 ;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'ert)
25 (require 'epg)
26
27 (defvar epg-tests-context nil)
28
29 (defvar epg-tests-data-directory
30 (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY"))
31 "Directory containing epg test data.")
32
33 (defconst epg-tests-program-alist-for-passphrase-callback
34 '((OpenPGP
35 nil
36 ("gpg" . "1.4.3"))))
37
38 (defun epg-tests-find-usable-gpg-configuration (&optional require-passphrase)
39 (epg-find-configuration
40 'OpenPGP
41 'no-cache
42 (if require-passphrase
43 epg-tests-program-alist-for-passphrase-callback)))
44
45 (defun epg-tests-passphrase-callback (_c _k _d)
46 ;; Need to create a copy here, since the string will be wiped out
47 ;; after the use.
48 (copy-sequence "test0123456789"))
49
50 (cl-defmacro with-epg-tests ((&optional &key require-passphrase
51 require-public-key
52 require-secret-key)
53 &rest body)
54 "Set up temporary locations and variables for testing."
55 (declare (indent 1))
56 `(let ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
57 (unwind-protect
58 (let ((context (epg-make-context 'OpenPGP)))
59 (setf (epg-context-program context)
60 (alist-get 'program
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
69 context
70 #'epg-tests-passphrase-callback))
71 ,(if require-public-key
72 `(epg-import-keys-from-file
73 context
74 (expand-file-name "pubkey.asc" epg-tests-data-directory)))
75 ,(if require-secret-key
76 `(epg-import-keys-from-file
77 context
78 (expand-file-name "seckey.asc" epg-tests-data-directory)))
79 (with-temp-buffer
80 (make-local-variable 'epg-tests-context)
81 (setq epg-tests-context context)
82 ,@body))
83 (when (file-directory-p epg-tests-home-directory)
84 (delete-directory epg-tests-home-directory t)))))
85
86 (ert-deftest epg-decrypt-1 ()
87 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
88 (with-epg-tests (:require-passphrase t)
89 (should (equal "test"
90 (epg-decrypt-string epg-tests-context "\
91 -----BEGIN PGP MESSAGE-----
92 Version: GnuPG v2
93
94 jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
95 =U8z7
96 -----END PGP MESSAGE-----")))))
97
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))))))
104
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"
112 recipients nil t)))
113 (should (equal "public key"
114 (epg-decrypt-string epg-tests-context cipher))))))
115
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)))))))
129
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)))))))
145
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)))))))
160
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))))))
175
176 (provide 'epg-tests)
177
178 ;;; epg-tests.el ends here