aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaiki Ueno2015-03-24 14:46:35 +0900
committerDaiki Ueno2015-03-24 14:47:54 +0900
commite5ce95fa4a0b311792b9b9dcc3b8901fc1f24a0d (patch)
treeb61069295988527fd7678d0a3b2ebfbf8072280f
parentd7d72624b29f0eeb2c242e976703e4755c6d7bef (diff)
downloademacs-e5ce95fa4a0b311792b9b9dcc3b8901fc1f24a0d.tar.gz
emacs-e5ce95fa4a0b311792b9b9dcc3b8901fc1f24a0d.zip
Add tests for epg.el
* automated/epg-tests.el: New file. * automated/data/epg/pubkey.asc: New file. * automated/data/epg/seckey.asc: New file.
-rw-r--r--test/ChangeLog6
-rw-r--r--test/automated/data/epg/pubkey.asc20
-rw-r--r--test/automated/data/epg/seckey.asc33
-rw-r--r--test/automated/epg-tests.el170
4 files changed, 229 insertions, 0 deletions
diff --git a/test/ChangeLog b/test/ChangeLog
index b12e9c044f5..9949e2c1efb 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,9 @@
12015-03-24 Daiki Ueno <ueno@gnu.org>
2
3 * automated/epg-tests.el: New file.
4 * automated/data/epg/pubkey.asc: New file.
5 * automated/data/epg/seckey.asc: New file.
6
12015-03-22 Dmitry Gutov <dgutov@yandex.ru> 72015-03-22 Dmitry Gutov <dgutov@yandex.ru>
2 8
3 * automated/json-tests.el: New file. 9 * automated/json-tests.el: New file.
diff --git a/test/automated/data/epg/pubkey.asc b/test/automated/data/epg/pubkey.asc
new file mode 100644
index 00000000000..c0bf28f6200
--- /dev/null
+++ b/test/automated/data/epg/pubkey.asc
@@ -0,0 +1,20 @@
1-----BEGIN PGP PUBLIC KEY BLOCK-----
2Version: GnuPG v1
3
4mI0EVRDxCAEEALcScrRmxq5N+Hh+NxPg75RJJdtEi824pwtqMlT/3wG1esmP5gNu
5ZIPVaTTSGNZkEzeYdhaLXBUe5qD+RQIQVh+MLt9nisF9nD35imyOrhHwAHnglOPx
6GdylH8nQ/tIO5p/lfUlw+iCBlPH7eZHqFJhwP0hJML4PKE8ArWG6RtsxABEBAAG0
7J0pvZSBUZXN0ZXIgKHRlc3Qga2V5KSA8am9lQGV4YW1wbGUuY29tPoi4BBMBAgAi
8BQJVEPEIAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRAoscCWMvu4GGYO
9A/0Zzoc2z/dvAtFVLh4ovKqP2qliQt2qschJHVP30hJnKT7dmJfJl7kz9mXmMfSt
10Ym0luYmeSzdeWORM9SygLRYXuDfN6G4ZPJTlsRhgnARhNzNhSx+YlcFh48Z+a5zR
11goBMn7DgYVqfU4UteZOSXMlnuA2Z5ao1qgGhVqESSJgU5riNBFUQ8QgBBADacLkK
12D0U11nmlsScxPGkrDr0aJPrG8MEaDRnKjHJKNp3XTp1psGBUpWF/ErjQAIu+psFt
13LO8owCGsg/vJM7CzTv2dVBRbrZXjIKvdq7HdivosTMaHArQBpEtSO9rmgVHO+jaQ
14q/M2oGvNEB86zo3nfTWhOgBiB32m8kttWRiuWQARAQABiJ8EGAECAAkFAlUQ8QgC
15GwwACgkQKLHAljL7uBj44AQAkMJRm7VJUryrDKFtfIfytQx/vmyU/cZcVV6IpKqP
16KhztgR+QD9czlHvQhz+y3hqtLRShu2Eyf75dNexcUvKs/lS4LIDXg5V7pWSRk9eQ
17G403muqR/NGu6+QmUx09rJl72trdaGxNkyHA7Zy7ZDGkcMvQsd3qoSNGsPR5TKes
18w7Q=
19=NMxb
20-----END PGP PUBLIC KEY BLOCK-----
diff --git a/test/automated/data/epg/seckey.asc b/test/automated/data/epg/seckey.asc
new file mode 100644
index 00000000000..4ac7ba4a502
--- /dev/null
+++ b/test/automated/data/epg/seckey.asc
@@ -0,0 +1,33 @@
1-----BEGIN PGP PRIVATE KEY BLOCK-----
2Version: GnuPG v1
3
4lQHYBFUQ8QgBBAC3EnK0ZsauTfh4fjcT4O+USSXbRIvNuKcLajJU/98BtXrJj+YD
5bmSD1Wk00hjWZBM3mHYWi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AAP7B8uNtb/DLvGoRfL+mA0Q
22REhgOJ1WpRcU6rvKYNPh8xTkKMvM+EK0nVU/znBedEpXjb0pY1WRT0uvXs2pzY2V
23YeaugyKIkdUpPWnyWoEQwI8hFvHOWmU2rNHyXLW0MY7bxcGgqv2XbkL4m7/D6VQS
24SR8hQ2CxBbW+9ov6aBMwv/UCAOW89+5xxuzkv48AVraWlMnaU0ggVOf6ht0Qa40+
25+uw2yziNlD403gAAAycoICiB/oqwslx61B2xOHn0laCKrgsCAPNpIsHRlAwWbAsq
26uCtfIQxg+C3mPXkqsNTMjeK5NjLNytrmO49NXco36zVEG6q7qz5Zj9d9IPYoGOSa
27I+dQZ6sB/RKF5aonR5/e7IHJgc8BG7I0yiya4llE0AB9ghnRI/3uHwnCBnmo/32a
28n4+rQkx6vm+rg3JA/09Gi7W4R9SwV+ane4ifBBgBAgAJBQJVEPEIAhsMAAoJECix
29wJYy+7gY+OAEAJDCUZu1SVK8qwyhbXyH8rUMf75slP3GXFVeiKSqjyoc7YEfkA/X
30M5R70Ic/st4arS0UobthMn++XTXsXFLyrP5UuCyA14OVe6VkkZPXkBuNN5rqkfzR
31ruvkJlMdPayZe9ra3WhsTZMhwO2cu2QxpHDL0LHd6qEjRrD0eUynrMO0
32=iCIm
33-----END PGP PRIVATE KEY BLOCK-----
diff --git a/test/automated/epg-tests.el b/test/automated/epg-tests.el
new file mode 100644
index 00000000000..a7fa5a1da58
--- /dev/null
+++ b/test/automated/epg-tests.el
@@ -0,0 +1,170 @@
1;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.
17
18;;; Commentary:
19
20;;; Code:
21
22(require 'ert)
23(require 'epg)
24
25(defvar epg-tests-context nil)
26
27(defvar epg-tests-data-directory
28 (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY"))
29 "Directory containing epg test data.")
30
31(defun epg-tests-gpg-usable (&optional require-passphrase)
32 (and (executable-find epg-gpg-program)
33 (condition-case nil
34 (progn
35 (epg-check-configuration (epg-configuration))
36 (if require-passphrase
37 (string-match "\\`1\\."
38 (cdr (assq 'version (epg-configuration))))
39 t))
40 (error nil))))
41
42(defun epg-tests-passphrase-callback (_c _k _d)
43 ;; Need to create a copy here, since the string will be wiped out
44 ;; after the use.
45 (copy-sequence "test0123456789"))
46
47(cl-defmacro with-epg-tests ((&optional &key require-passphrase
48 require-public-key
49 require-secret-key)
50 &rest body)
51 "Set up temporary locations and variables for testing."
52 (declare (indent 1))
53 `(let* ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
54 (unwind-protect
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
61 context
62 #'epg-tests-passphrase-callback))
63 ,(if require-public-key
64 `(epg-import-keys-from-file
65 context
66 (expand-file-name "pubkey.asc" epg-tests-data-directory)))
67 ,(if require-secret-key
68 `(epg-import-keys-from-file
69 context
70 (expand-file-name "seckey.asc" epg-tests-data-directory)))
71 (with-temp-buffer
72 (make-local-variable 'epg-tests-context)
73 (setq epg-tests-context context)
74 ,@body))
75 (when (file-directory-p epg-tests-home-directory)
76 (delete-directory epg-tests-home-directory t)))))
77
78(ert-deftest epg-decrypt-1 ()
79 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
80 (with-epg-tests (:require-passphrase t)
81 (should (equal "test"
82 (epg-decrypt-string epg-tests-context "\
83-----BEGIN PGP MESSAGE-----
84Version: GnuPG v2
85
86jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
87=U8z7
88-----END PGP MESSAGE-----")))))
89
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))))))
96
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"
104 recipients nil t)))
105 (should (equal "public key"
106 (epg-decrypt-string epg-tests-context cipher))))))
107
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)))))))
121
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)))))))
137
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)))))))
152
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))))))
167
168(provide 'epg-tests)
169
170;;; epg-tests.el ends here