aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaiki Ueno2016-05-19 18:05:19 +0900
committerDaiki Ueno2016-05-19 18:11:06 +0900
commitd4ae6d7033b34e8b75c59aaf1584131e439ef2d5 (patch)
treebf3d9d66e3e2356d3933236e6f0897677d721d5c
parentebc3a94e27ec9dcbe24790795741c062bed2c1a0 (diff)
downloademacs-d4ae6d7033b34e8b75c59aaf1584131e439ef2d5.tar.gz
emacs-d4ae6d7033b34e8b75c59aaf1584131e439ef2d5.zip
epg: Add a way to detect gpg1 executable for tests
Fixes bug#23561. * test/automated/epg-tests.el (epg-tests-program-alist-for-passphrase-callback): New constant. (epg-tests-find-usable-gpg-configuration): New function, renamed from `epg-tests-gpg-usable'. All callers changed. (epg-tests-gpg-usable): Remove. * lisp/epg-config.el (epg-config--program-alist): Factor out constructor element to... (epg-config--configuration-constructor-alist): ...here. (epg-find-configuration): Rename FORCE argument to NO-CACHE, and add PROGRAM-ALIST argument.
-rw-r--r--lisp/epg-config.el82
-rw-r--r--test/automated/epg-tests.el42
2 files changed, 71 insertions, 53 deletions
diff --git a/lisp/epg-config.el b/lisp/epg-config.el
index 8a208044cba..9179e04dcc1 100644
--- a/lisp/epg-config.el
+++ b/lisp/epg-config.el
@@ -81,57 +81,69 @@ Note that the buffer name starts with a space."
81(defconst epg-config--program-alist 81(defconst epg-config--program-alist
82 '((OpenPGP 82 '((OpenPGP
83 epg-gpg-program 83 epg-gpg-program
84 epg-config--make-gpg-configuration
85 ("gpg2" . "2.1.6") ("gpg" . "1.4.3")) 84 ("gpg2" . "2.1.6") ("gpg" . "1.4.3"))
86 (CMS 85 (CMS
87 epg-gpgsm-program 86 epg-gpgsm-program
88 epg-config--make-gpgsm-configuration
89 ("gpgsm" . "2.0.4"))) 87 ("gpgsm" . "2.0.4")))
90 "Alist used to obtain the usable configuration of executables. 88 "Alist used to obtain the usable configuration of executables.
91The first element of each entry is protocol symbol, which is 89The first element of each entry is protocol symbol, which is
92either `OpenPGP' or `CMS'. The second element is a symbol where 90either `OpenPGP' or `CMS'. The second element is a symbol where
93the executable name is remembered. The third element is a 91the executable name is remembered. The rest of the entry is an
94function which constructs a configuration object (actually a 92alist mapping executable names to the minimum required version
95plist). The rest of the entry is an alist mapping executable 93suitable for the use with Emacs.")
96names to the minimum required version suitable for the use with 94
97Emacs.") 95(defconst epg-config--configuration-constructor-alist
96 '((OpenPGP . epg-config--make-gpg-configuration)
97 (CMS . epg-config--make-gpgsm-configuration))
98 "Alist used to obtain the usable configuration of executables.
99The first element of each entry is protocol symbol, which is
100either `OpenPGP' or `CMS'. The second element is a function
101which constructs a configuration object (actually a plist).")
98 102
99(defvar epg--configurations nil) 103(defvar epg--configurations nil)
100 104
101;;;###autoload 105;;;###autoload
102(defun epg-find-configuration (protocol &optional force) 106(defun epg-find-configuration (protocol &optional no-cache program-alist)
103 "Find or create a usable configuration to handle PROTOCOL. 107 "Find or create a usable configuration to handle PROTOCOL.
104This function first looks at the existing configuration found by 108This function first looks at the existing configuration found by
105the previous invocation of this function, unless FORCE is non-nil. 109the previous invocation of this function, unless NO-CACHE is non-nil.
106 110
107Then it walks through `epg-config--program-alist'. If 111Then it walks through PROGRAM-ALIST or
108`epg-gpg-program' or `epg-gpgsm-program' is already set with 112`epg-config--program-alist'. If `epg-gpg-program' or
109custom, use it. Otherwise, it tries the programs listed in the 113`epg-gpgsm-program' is already set with custom, use it.
110entry until the version requirement is met." 114Otherwise, it tries the programs listed in the entry until the
111 (let ((entry (assq protocol epg-config--program-alist))) 115version requirement is met."
116 (unless program-alist
117 (setq program-alist epg-config--program-alist))
118 (let ((entry (assq protocol program-alist)))
112 (unless entry 119 (unless entry
113 (error "Unknown protocol %S" protocol)) 120 (error "Unknown protocol %S" protocol))
114 (cl-destructuring-bind (symbol constructor . alist) 121 (cl-destructuring-bind (symbol . alist)
115 (cdr entry) 122 (cdr entry)
116 (or (and (not force) (alist-get protocol epg--configurations)) 123 (let ((constructor
117 ;; If the executable value is already set with M-x 124 (alist-get protocol epg-config--configuration-constructor-alist)))
118 ;; customize, use it without checking. 125 (or (and (not no-cache) (alist-get protocol epg--configurations))
119 (if (get symbol 'saved-value) 126 ;; If the executable value is already set with M-x
120 (let ((configuration (funcall constructor (symbol-value symbol)))) 127 ;; customize, use it without checking.
121 (push (cons protocol configuration) epg--configurations) 128 (if (and symbol (get symbol 'saved-value))
122 configuration) 129 (let ((configuration
123 (catch 'found 130 (funcall constructor (symbol-value symbol))))
124 (dolist (program-version alist) 131 (push (cons protocol configuration) epg--configurations)
125 (let ((executable (executable-find (car program-version)))) 132 configuration)
126 (when executable 133 (catch 'found
127 (let ((configuration 134 (dolist (program-version alist)
128 (funcall constructor executable))) 135 (let ((executable (executable-find (car program-version))))
129 (when (ignore-errors 136 (when executable
130 (epg-check-configuration configuration 137 (let ((configuration
131 (cdr program-version)) 138 (funcall constructor executable)))
132 t) 139 (when (ignore-errors
133 (push (cons protocol configuration) epg--configurations) 140 (epg-check-configuration configuration
134 (throw 'found configuration)))))))))))) 141 (cdr program-version))
142 t)
143 (unless no-cache
144 (push (cons protocol configuration)
145 epg--configurations))
146 (throw 'found configuration)))))))))))))
135 147
136;; Create an `epg-configuration' object for `gpg', using PROGRAM. 148;; Create an `epg-configuration' object for `gpg', using PROGRAM.
137(defun epg-config--make-gpg-configuration (program) 149(defun epg-config--make-gpg-configuration (program)
diff --git a/test/automated/epg-tests.el b/test/automated/epg-tests.el
index 4a317974ef5..d51ab23f71e 100644
--- a/test/automated/epg-tests.el
+++ b/test/automated/epg-tests.el
@@ -30,16 +30,17 @@
30 (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY")) 30 (expand-file-name "data/epg" (getenv "EMACS_TEST_DIRECTORY"))
31 "Directory containing epg test data.") 31 "Directory containing epg test data.")
32 32
33(defun epg-tests-gpg-usable (&optional require-passphrase) 33(defconst epg-tests-program-alist-for-passphrase-callback
34 (and (executable-find epg-gpg-program) 34 '((OpenPGP
35 (condition-case nil 35 nil
36 (progn 36 ("gpg" . "1.4.3"))))
37 (epg-check-configuration (epg-configuration)) 37
38 (if require-passphrase 38(defun epg-tests-find-usable-gpg-configuration (&optional require-passphrase)
39 (string-match "\\`1\\." 39 (epg-find-configuration
40 (cdr (assq 'version (epg-configuration)))) 40 'OpenPGP
41 t)) 41 'no-cache
42 (error nil)))) 42 (if require-passphrase
43 epg-tests-program-alist-for-passphrase-callback)))
43 44
44(defun epg-tests-passphrase-callback (_c _k _d) 45(defun epg-tests-passphrase-callback (_c _k _d)
45 ;; Need to create a copy here, since the string will be wiped out 46 ;; Need to create a copy here, since the string will be wiped out
@@ -52,9 +53,14 @@
52 &rest body) 53 &rest body)
53 "Set up temporary locations and variables for testing." 54 "Set up temporary locations and variables for testing."
54 (declare (indent 1)) 55 (declare (indent 1))
55 `(let* ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t))) 56 `(let ((epg-tests-home-directory (make-temp-file "epg-tests-homedir" t)))
56 (unwind-protect 57 (unwind-protect
57 (let ((context (epg-make-context 'OpenPGP))) 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))))
58 (setf (epg-context-home-directory context) 64 (setf (epg-context-home-directory context)
59 epg-tests-home-directory) 65 epg-tests-home-directory)
60 (setenv "GPG_AGENT_INFO") 66 (setenv "GPG_AGENT_INFO")
@@ -78,7 +84,7 @@
78 (delete-directory epg-tests-home-directory t))))) 84 (delete-directory epg-tests-home-directory t)))))
79 85
80(ert-deftest epg-decrypt-1 () 86(ert-deftest epg-decrypt-1 ()
81 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 87 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
82 (with-epg-tests (:require-passphrase t) 88 (with-epg-tests (:require-passphrase t)
83 (should (equal "test" 89 (should (equal "test"
84 (epg-decrypt-string epg-tests-context "\ 90 (epg-decrypt-string epg-tests-context "\
@@ -90,14 +96,14 @@ jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
90-----END PGP MESSAGE-----"))))) 96-----END PGP MESSAGE-----")))))
91 97
92(ert-deftest epg-roundtrip-1 () 98(ert-deftest epg-roundtrip-1 ()
93 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 99 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
94 (with-epg-tests (:require-passphrase t) 100 (with-epg-tests (:require-passphrase t)
95 (let ((cipher (epg-encrypt-string epg-tests-context "symmetric" nil))) 101 (let ((cipher (epg-encrypt-string epg-tests-context "symmetric" nil)))
96 (should (equal "symmetric" 102 (should (equal "symmetric"
97 (epg-decrypt-string epg-tests-context cipher)))))) 103 (epg-decrypt-string epg-tests-context cipher))))))
98 104
99(ert-deftest epg-roundtrip-2 () 105(ert-deftest epg-roundtrip-2 ()
100 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 106 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
101 (with-epg-tests (:require-passphrase t 107 (with-epg-tests (:require-passphrase t
102 :require-public-key t 108 :require-public-key t
103 :require-secret-key t) 109 :require-secret-key t)
@@ -108,7 +114,7 @@ jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
108 (epg-decrypt-string epg-tests-context cipher)))))) 114 (epg-decrypt-string epg-tests-context cipher))))))
109 115
110(ert-deftest epg-sign-verify-1 () 116(ert-deftest epg-sign-verify-1 ()
111 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 117 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
112 (with-epg-tests (:require-passphrase t 118 (with-epg-tests (:require-passphrase t
113 :require-public-key t 119 :require-public-key t
114 :require-secret-key t) 120 :require-secret-key t)
@@ -122,7 +128,7 @@ jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
122 (should (eq 'good (epg-signature-status (car verify-result))))))) 128 (should (eq 'good (epg-signature-status (car verify-result)))))))
123 129
124(ert-deftest epg-sign-verify-2 () 130(ert-deftest epg-sign-verify-2 ()
125 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 131 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
126 (with-epg-tests (:require-passphrase t 132 (with-epg-tests (:require-passphrase t
127 :require-public-key t 133 :require-public-key t
128 :require-secret-key t) 134 :require-secret-key t)
@@ -138,7 +144,7 @@ jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
138 (should (eq 'good (epg-signature-status (car verify-result))))))) 144 (should (eq 'good (epg-signature-status (car verify-result)))))))
139 145
140(ert-deftest epg-sign-verify-3 () 146(ert-deftest epg-sign-verify-3 ()
141 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 147 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
142 (with-epg-tests (:require-passphrase t 148 (with-epg-tests (:require-passphrase t
143 :require-public-key t 149 :require-public-key t
144 :require-secret-key t) 150 :require-secret-key t)
@@ -153,7 +159,7 @@ jA0EAwMCE19JBLTvvmhgyRrGGglRbnKkK9PJG8fDwO5ccjysrR7IcdNcnA==
153 (should (eq 'good (epg-signature-status (car verify-result))))))) 159 (should (eq 'good (epg-signature-status (car verify-result)))))))
154 160
155(ert-deftest epg-import-1 () 161(ert-deftest epg-import-1 ()
156 (skip-unless (epg-tests-gpg-usable 'require-passphrase)) 162 (skip-unless (epg-tests-find-usable-gpg-configuration 'require-passphrase))
157 (with-epg-tests (:require-passphrase nil) 163 (with-epg-tests (:require-passphrase nil)
158 (should (= 0 (length (epg-list-keys epg-tests-context)))) 164 (should (= 0 (length (epg-list-keys epg-tests-context))))
159 (should (= 0 (length (epg-list-keys epg-tests-context nil t))))) 165 (should (= 0 (length (epg-list-keys epg-tests-context nil t)))))