]> code.delx.au - gnu-emacs/blob - test/automated/auth-source-tests.el
dd70d546d5ccf20d570a94a7165d61d6952f5bfb
[gnu-emacs] / test / automated / auth-source-tests.el
1 ;;; auth-source-tests.el --- Tests for auth-source.el -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Damien Cassou <damien@cassou.me>,
6 ;; Nicolas Petton <nicolas@petton.fr>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;
26
27 ;;; Code:
28
29 (require 'ert)
30 (require 'auth-source)
31
32 (defvar secrets-enabled t
33 "Enable the secrets backend to test its features.")
34
35 (defun auth-source-validate-backend (source validation-alist)
36 (let ((backend (auth-source-backend-parse source)))
37 (should (auth-source-backend-p backend))
38 (dolist (pair validation-alist)
39 (should (equal (eieio-oref backend (car pair)) (cdr pair))))))
40
41 (ert-deftest auth-source-backend-parse-macos-keychain ()
42 (auth-source-validate-backend '(:source (:macos-keychain-generic foobar))
43 '((:source . "foobar")
44 (:type . macos-keychain-generic)
45 (:search-function . auth-source-macos-keychain-search)
46 (:create-function . auth-source-macos-keychain-create))))
47
48 (ert-deftest auth-source-backend-parse-macos-keychain-generic-string ()
49 (auth-source-validate-backend "macos-keychain-generic:foobar"
50 '((:source . "foobar")
51 (:type . macos-keychain-generic)
52 (:search-function . auth-source-macos-keychain-search)
53 (:create-function . auth-source-macos-keychain-create))))
54
55 (ert-deftest auth-source-backend-parse-macos-keychain-internet-string ()
56 (auth-source-validate-backend "macos-keychain-internet:foobar"
57 '((:source . "foobar")
58 (:type . macos-keychain-internet)
59 (:search-function . auth-source-macos-keychain-search)
60 (:create-function . auth-source-macos-keychain-create))))
61
62 (ert-deftest auth-source-backend-parse-macos-keychain-internet-symbol ()
63 (auth-source-validate-backend 'macos-keychain-internet
64 '((:source . "default")
65 (:type . macos-keychain-internet)
66 (:search-function . auth-source-macos-keychain-search)
67 (:create-function . auth-source-macos-keychain-create))))
68
69 (ert-deftest auth-source-backend-parse-macos-keychain-generic-symbol ()
70 (auth-source-validate-backend 'macos-keychain-generic
71 '((:source . "default")
72 (:type . macos-keychain-generic)
73 (:search-function . auth-source-macos-keychain-search)
74 (:create-function . auth-source-macos-keychain-create))))
75
76 (ert-deftest auth-source-backend-parse-macos-keychain-internet-default-string ()
77 (auth-source-validate-backend 'macos-keychain-internet
78 '((:source . "default")
79 (:type . macos-keychain-internet)
80 (:search-function . auth-source-macos-keychain-search)
81 (:create-function . auth-source-macos-keychain-create))))
82
83 (ert-deftest auth-source-backend-parse-plstore ()
84 (auth-source-validate-backend '(:source "foo.plist")
85 '((:source . "foo.plist")
86 (:type . plstore)
87 (:search-function . auth-source-plstore-search)
88 (:create-function . auth-source-plstore-create))))
89
90 (ert-deftest auth-source-backend-parse-netrc ()
91 (auth-source-validate-backend '(:source "foo")
92 '((:source . "foo")
93 (:type . netrc)
94 (:search-function . auth-source-netrc-search)
95 (:create-function . auth-source-netrc-create))))
96
97 (ert-deftest auth-source-backend-parse-netrc-string ()
98 (auth-source-validate-backend "foo"
99 '((:source . "foo")
100 (:type . netrc)
101 (:search-function . auth-source-netrc-search)
102 (:create-function . auth-source-netrc-create))))
103
104 (ert-deftest auth-source-backend-parse-secrets ()
105 (provide 'secrets) ; simulates the presence of the `secrets' package
106 (let ((secrets-enabled t))
107 (auth-source-validate-backend '(:source (:secrets "foo"))
108 '((:source . "foo")
109 (:type . secrets)
110 (:search-function . auth-source-secrets-search)
111 (:create-function . auth-source-secrets-create)))))
112
113 (ert-deftest auth-source-backend-parse-secrets-strings ()
114 (provide 'secrets) ; simulates the presence of the `secrets' package
115 (let ((secrets-enabled t))
116 (auth-source-validate-backend "secrets:foo"
117 '((:source . "foo")
118 (:type . secrets)
119 (:search-function . auth-source-secrets-search)
120 (:create-function . auth-source-secrets-create)))))
121
122 (ert-deftest auth-source-backend-parse-secrets-nil-source ()
123 (provide 'secrets) ; simulates the presence of the `secrets' package
124 (let ((secrets-enabled t))
125 (auth-source-validate-backend '(:source (:secrets nil))
126 '((:source . "session")
127 (:type . secrets)
128 (:search-function . auth-source-secrets-search)
129 (:create-function . auth-source-secrets-create)))))
130
131 (ert-deftest auth-source-backend-parse-secrets-alias ()
132 (provide 'secrets) ; simulates the presence of the `secrets' package
133 (let ((secrets-enabled t))
134 ;; Redefine `secrets-get-alias' to map 'foo to "foo"
135 (cl-letf (((symbol-function 'secrets-get-alias) (lambda (_) "foo")))
136 (auth-source-validate-backend '(:source (:secrets foo))
137 '((:source . "foo")
138 (:type . secrets)
139 (:search-function . auth-source-secrets-search)
140 (:create-function . auth-source-secrets-create))))))
141
142 (ert-deftest auth-source-backend-parse-secrets-symbol ()
143 (provide 'secrets) ; simulates the presence of the `secrets' package
144 (let ((secrets-enabled t))
145 ;; Redefine `secrets-get-alias' to map 'default to "foo"
146 (cl-letf (((symbol-function 'secrets-get-alias) (lambda (_) "foo")))
147 (auth-source-validate-backend 'default
148 '((:source . "foo")
149 (:type . secrets)
150 (:search-function . auth-source-secrets-search)
151 (:create-function . auth-source-secrets-create))))))
152
153 (ert-deftest auth-source-backend-parse-secrets-no-alias ()
154 (provide 'secrets) ; simulates the presence of the `secrets' package
155 (let ((secrets-enabled t))
156 ;; Redefine `secrets-get-alias' to map 'foo to nil (so that
157 ;; "Login" is used by default
158 (cl-letf (((symbol-function 'secrets-get-alias) (lambda (_) nil)))
159 (auth-source-validate-backend '(:source (:secrets foo))
160 '((:source . "Login")
161 (:type . secrets)
162 (:search-function . auth-source-secrets-search)
163 (:create-function . auth-source-secrets-create))))))
164
165 ;; TODO This test shows suspicious behavior of auth-source: the
166 ;; "secrets" source is used even though nothing in the input indicates
167 ;; that is what we want
168 (ert-deftest auth-source-backend-parse-secrets-no-source ()
169 (provide 'secrets) ; simulates the presence of the `secrets' package
170 (let ((secrets-enabled t))
171 (auth-source-validate-backend '(:source '(foo))
172 '((:source . "session")
173 (:type . secrets)
174 (:search-function . auth-source-secrets-search)
175 (:create-function . auth-source-secrets-create)))))
176
177 (defun auth-source--test-netrc-parse-entry (entry host user port)
178 "Parse a netrc entry from buffer."
179 (auth-source-forget-all-cached)
180 (setq port (auth-source-ensure-strings port))
181 (with-temp-buffer
182 (insert entry)
183 (goto-char (point-min))
184 (let* ((check (lambda(alist)
185 (and alist
186 (auth-source-search-collection
187 host
188 (or
189 (auth-source--aget alist "machine")
190 (auth-source--aget alist "host")
191 t))
192 (auth-source-search-collection
193 user
194 (or
195 (auth-source--aget alist "login")
196 (auth-source--aget alist "account")
197 (auth-source--aget alist "user")
198 t))
199 (auth-source-search-collection
200 port
201 (or
202 (auth-source--aget alist "port")
203 (auth-source--aget alist "protocol")
204 t)))))
205 (entries (auth-source-netrc-parse-entries check 1)))
206 entries)))
207
208 (ert-deftest auth-source-test-netrc-parse-entry ()
209 (should (equal (auth-source--test-netrc-parse-entry
210 "machine mymachine1 login user1 password pass1\n" t t t)
211 '((("password" . "pass1")
212 ("login" . "user1")
213 ("machine" . "mymachine1")))))
214 (should (equal (auth-source--test-netrc-parse-entry
215 "machine mymachine1 login user1 password pass1 port 100\n"
216 t t t)
217 '((("port" . "100")
218 ("password" . "pass1")
219 ("login" . "user1")
220 ("machine" . "mymachine1"))))))
221
222 (provide 'auth-source-tests)
223 ;;; auth-source-tests.el ends here