1 ;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 (defvar epg-tests-context nil)
27 (defvar epg-tests-data-directory
28 (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY"))
29 "Directory containing epg test data.")
31 (defun epg-tests-gpg-usable (&optional require-passphrase)
32 (and (executable-find epg-gpg-program)
35 (epg-check-configuration (epg-configuration))
36 (if require-passphrase
37 (string-match "\\`1\\."
38 (cdr (assq 'version (epg-configuration))))
42 (defun epg-tests-passphrase-callback (_c _k _d)
43 ;; Need to create a copy here, since the string will be wiped out
45 (copy-sequence "test0123456789"))
47 (cl-defmacro with-epg-tests ((&optional &key require-passphrase
51 "Set up temporary locations and variables for testing."
53 `(let* ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
55 (let ((context (epg-make-context 'OpenPGP)))
56 (setf (epg-context-home-directory context)
57 epg-tests-home-directory)
58 (setenv "GPG_AGENT_INFO")
59 ,(if require-passphrase
60 `(epg-context-set-passphrase-callback
62 #'epg-tests-passphrase-callback))
63 ,(if require-public-key
64 `(epg-import-keys-from-file
66 (expand-file-name "pubkey.asc" epg-tests-data-directory)))
67 ,(if require-secret-key
68 `(epg-import-keys-from-file
70 (expand-file-name "seckey.asc" epg-tests-data-directory)))
72 (make-local-variable 'epg-tests-context)
73 (setq epg-tests-context context)
75 (when (file-directory-p epg-tests-home-directory)
76 (delete-directory epg-tests-home-directory t)))))
78 (ert-deftest epg-decrypt-1 ()
79 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
80 (with-epg-tests (:require-passphrase t)
82 (epg-decrypt-string epg-tests-context "\
83 -----BEGIN PGP MESSAGE-----
86 jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
88 -----END PGP MESSAGE-----")))))
90 (ert-deftest epg-roundtrip-1 ()
91 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
92 (with-epg-tests (:require-passphrase t)
93 (let ((cipher (epg-encrypt-string epg-tests-context "symmetric" nil)))
94 (should (equal "symmetric"
95 (epg-decrypt-string epg-tests-context cipher))))))
97 (ert-deftest epg-roundtrip-2 ()
98 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
99 (with-epg-tests (:require-passphrase t
100 :require-public-key t
101 :require-secret-key t)
102 (let* ((recipients (epg-list-keys epg-tests-context "joe@example.com"))
103 (cipher (epg-encrypt-string epg-tests-context "public key"
105 (should (equal "public key"
106 (epg-decrypt-string epg-tests-context cipher))))))
108 (ert-deftest epg-sign-verify-1 ()
109 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
110 (with-epg-tests (:require-passphrase t
111 :require-public-key t
112 :require-secret-key t)
113 (let (signature verify-result)
114 (setf (epg-context-signers epg-tests-context)
115 (epg-list-keys epg-tests-context "joe@example.com"))
116 (setq signature (epg-sign-string epg-tests-context "signed" t))
117 (epg-verify-string epg-tests-context signature "signed")
118 (setq verify-result (epg-context-result-for context 'verify))
119 (should (= 1 (length verify-result)))
120 (should (eq 'good (epg-signature-status (car verify-result)))))))
122 (ert-deftest epg-sign-verify-2 ()
123 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
124 (with-epg-tests (:require-passphrase t
125 :require-public-key t
126 :require-secret-key t)
127 (let (signature verify-result)
128 (setf (epg-context-signers epg-tests-context)
129 (epg-list-keys epg-tests-context "joe@example.com"))
130 (setq signature (epg-sign-string epg-tests-context "clearsigned" 'clear))
131 ;; Clearsign signature always ends with a new line.
132 (should (equal "clearsigned\n"
133 (epg-verify-string epg-tests-context signature)))
134 (setq verify-result (epg-context-result-for context 'verify))
135 (should (= 1 (length verify-result)))
136 (should (eq 'good (epg-signature-status (car verify-result)))))))
138 (ert-deftest epg-sign-verify-3 ()
139 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
140 (with-epg-tests (:require-passphrase t
141 :require-public-key t
142 :require-secret-key t)
143 (let (signature verify-result)
144 (setf (epg-context-signers epg-tests-context)
145 (epg-list-keys epg-tests-context "joe@example.com"))
146 (setq signature (epg-sign-string epg-tests-context "normal signed"))
147 (should (equal "normal signed"
148 (epg-verify-string epg-tests-context signature)))
149 (setq verify-result (epg-context-result-for context 'verify))
150 (should (= 1 (length verify-result)))
151 (should (eq 'good (epg-signature-status (car verify-result)))))))
153 (ert-deftest epg-import-1 ()
154 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
155 (with-epg-tests (:require-passphrase nil)
156 (should (= 0 (length (epg-list-keys epg-tests-context))))
157 (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
158 (with-epg-tests (:require-passphrase nil
159 :require-public-key t)
160 (should (= 1 (length (epg-list-keys epg-tests-context))))
161 (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
162 (with-epg-tests (:require-public-key nil
163 :require-public-key t
164 :require-secret-key t)
165 (should (= 1 (length (epg-list-keys epg-tests-context))))
166 (should (= 1 (length (epg-list-keys epg-tests-context nil t))))))
170 ;;; epg-tests.el ends here