Introduce xref-prompt-for-identifier
[emacs.git] / test / automated / epg-tests.el
bloba958d82bd03f6333bbcb312f257a76b379ca2af8
1 ;;; epg-tests.el --- Test suite for epg.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2015 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/>.
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-gpg-usable (&optional require-passphrase)
34 (and (executable-find epg-gpg-program)
35 (condition-case nil
36 (progn
37 (epg-check-configuration (epg-configuration))
38 (if require-passphrase
39 (string-match "\\`1\\."
40 (cdr (assq 'version (epg-configuration))))
41 t))
42 (error nil))))
44 (defun epg-tests-passphrase-callback (_c _k _d)
45 ;; Need to create a copy here, since the string will be wiped out
46 ;; after the use.
47 (copy-sequence "test0123456789"))
49 (cl-defmacro with-epg-tests ((&optional &key require-passphrase
50 require-public-key
51 require-secret-key)
52 &rest body)
53 "Set up temporary locations and variables for testing."
54 (declare (indent 1))
55 `(let* ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
56 (unwind-protect
57 (let ((context (epg-make-context 'OpenPGP)))
58 (setf (epg-context-home-directory context)
59 epg-tests-home-directory)
60 (setenv "GPG_AGENT_INFO")
61 ,(if require-passphrase
62 `(epg-context-set-passphrase-callback
63 context
64 #'epg-tests-passphrase-callback))
65 ,(if require-public-key
66 `(epg-import-keys-from-file
67 context
68 (expand-file-name "pubkey.asc" epg-tests-data-directory)))
69 ,(if require-secret-key
70 `(epg-import-keys-from-file
71 context
72 (expand-file-name "seckey.asc" epg-tests-data-directory)))
73 (with-temp-buffer
74 (make-local-variable 'epg-tests-context)
75 (setq epg-tests-context context)
76 ,@body))
77 (when (file-directory-p epg-tests-home-directory)
78 (delete-directory epg-tests-home-directory t)))))
80 (ert-deftest epg-decrypt-1 ()
81 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
82 (with-epg-tests (:require-passphrase t)
83 (should (equal "test"
84 (epg-decrypt-string epg-tests-context "\
85 -----BEGIN PGP MESSAGE-----
86 Version: GnuPG v2
88 jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
89 =U8z7
90 -----END PGP MESSAGE-----")))))
92 (ert-deftest epg-roundtrip-1 ()
93 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
94 (with-epg-tests (:require-passphrase t)
95 (let ((cipher (epg-encrypt-string epg-tests-context "symmetric" nil)))
96 (should (equal "symmetric"
97 (epg-decrypt-string epg-tests-context cipher))))))
99 (ert-deftest epg-roundtrip-2 ()
100 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
101 (with-epg-tests (:require-passphrase t
102 :require-public-key t
103 :require-secret-key t)
104 (let* ((recipients (epg-list-keys epg-tests-context "joe@example.com"))
105 (cipher (epg-encrypt-string epg-tests-context "public key"
106 recipients nil t)))
107 (should (equal "public key"
108 (epg-decrypt-string epg-tests-context cipher))))))
110 (ert-deftest epg-sign-verify-1 ()
111 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
112 (with-epg-tests (:require-passphrase t
113 :require-public-key t
114 :require-secret-key t)
115 (let (signature verify-result)
116 (setf (epg-context-signers epg-tests-context)
117 (epg-list-keys epg-tests-context "joe@example.com"))
118 (setq signature (epg-sign-string epg-tests-context "signed" t))
119 (epg-verify-string epg-tests-context signature "signed")
120 (setq verify-result (epg-context-result-for context 'verify))
121 (should (= 1 (length verify-result)))
122 (should (eq 'good (epg-signature-status (car verify-result)))))))
124 (ert-deftest epg-sign-verify-2 ()
125 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
126 (with-epg-tests (:require-passphrase t
127 :require-public-key t
128 :require-secret-key t)
129 (let (signature verify-result)
130 (setf (epg-context-signers epg-tests-context)
131 (epg-list-keys epg-tests-context "joe@example.com"))
132 (setq signature (epg-sign-string epg-tests-context "clearsigned" 'clear))
133 ;; Clearsign signature always ends with a new line.
134 (should (equal "clearsigned\n"
135 (epg-verify-string epg-tests-context signature)))
136 (setq verify-result (epg-context-result-for context 'verify))
137 (should (= 1 (length verify-result)))
138 (should (eq 'good (epg-signature-status (car verify-result)))))))
140 (ert-deftest epg-sign-verify-3 ()
141 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
142 (with-epg-tests (:require-passphrase t
143 :require-public-key t
144 :require-secret-key t)
145 (let (signature verify-result)
146 (setf (epg-context-signers epg-tests-context)
147 (epg-list-keys epg-tests-context "joe@example.com"))
148 (setq signature (epg-sign-string epg-tests-context "normal signed"))
149 (should (equal "normal signed"
150 (epg-verify-string epg-tests-context signature)))
151 (setq verify-result (epg-context-result-for context 'verify))
152 (should (= 1 (length verify-result)))
153 (should (eq 'good (epg-signature-status (car verify-result)))))))
155 (ert-deftest epg-import-1 ()
156 (skip-unless (epg-tests-gpg-usable 'require-passphrase))
157 (with-epg-tests (:require-passphrase nil)
158 (should (= 0 (length (epg-list-keys epg-tests-context))))
159 (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
160 (with-epg-tests (:require-passphrase nil
161 :require-public-key t)
162 (should (= 1 (length (epg-list-keys epg-tests-context))))
163 (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))
164 (with-epg-tests (:require-public-key nil
165 :require-public-key t
166 :require-secret-key t)
167 (should (= 1 (length (epg-list-keys epg-tests-context))))
168 (should (= 1 (length (epg-list-keys epg-tests-context nil t))))))
170 (provide 'epg-tests)
172 ;;; epg-tests.el ends here