From c379523c177ea0188f8c270585efc6077901479a Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Thu, 24 Nov 2022 21:03:03 -0800 Subject: [PATCH 2/2] [POC] Demo spaces in hosts/users among auth-source backends --- test/lisp/auth-source-pass-tests.el | 255 ++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el index d6d42ce942..59a0c1252f 100644 --- a/test/lisp/auth-source-pass-tests.el +++ b/test/lisp/auth-source-pass-tests.el @@ -752,6 +752,261 @@ auth-source-pass-extra-query-keywords--user-priorities (:host "g" :user "u" :port 2 :secret "@") ; ** (:host "g" :user "u" :port 2 :secret "/")))))))) +;;;; Whitespace demo + +;; These demonstrate that all back ends support spaces in host and +;; user fields. + +;; netrc + +(ert-deftest auth-source-pass-extra-query-keywords--ws-host-netrc () + (ert-with-temp-file netrc-file + :text "\ +machine \"hello world\" password a +machine localhost password b +" + (let* ((auth-sources (list netrc-file)) + (auth-source-do-cache nil) + (results (auth-source-search :host "hello world"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '((:host "hello world" :secret "a"))))))) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-user-netrc () + (ert-with-temp-file netrc-file + :text "\ +machine localhost login onetwo password a +machine localhost login \"one two\" password b +" + (let* ((auth-sources (list netrc-file)) + (auth-source-do-cache nil) + (results (auth-source-search :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :host "localhost" + :user "one two" + :secret "b"))))))) + +;; plstore + +(require 'epg) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-host-plstore () + (ert-with-temp-file plstore-file + :suffix ".plist" + :text "\ +;;; public entries -*- mode: plstore -*- +((\"8faf07aac16e46c49857598b6cd6dd809762c5cb\" + :secret-secret t :host \"hello world\") + (\"12d4700ff04a5dbadec60b55319ff3f473d026fa\" + :secret-secret t :host \"localhost\")) +;;; secret entries +((\"8faf07aac16e46c49857598b6cd6dd809762c5cb\" :secret \"a\") + (\"12d4700ff04a5dbadec60b55319ff3f473d026fa\" :secret \"b\")) +" + (cl-letf (((symbol-function 'epg-decrypt-string) + (lambda (&rest r) (prin1-to-string (cadr r)))) + ((symbol-function 'epg-find-configuration) + (lambda (&rest _) '((program . "/bin/true"))))) + (let* ((auth-sources (list plstore-file)) + (auth-source-do-cache nil) + (results (auth-source-search :host "hello world"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :login nil + :port nil + :secret "a" + :host "hello world")))))))) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-user-plstore () + (ert-with-temp-file plstore-file + :suffix ".plist" + :text "\ +;;; public entries -*- mode: plstore -*- +((\"8b23ccce4b95bee4b9a8676409a7f196f1adc59e\" + :secret-secret t + :host \"localhost\" + :user \"onetwo\") + (\"e4c4fcb6c505d389ff72a58314571f37fb936365\" + :secret-secret t + :host \"localhost\" + :user \"one two\")) +;;; secret entries +((\"8b23ccce4b95bee4b9a8676409a7f196f1adc59e\" :secret \"a\") + (\"e4c4fcb6c505d389ff72a58314571f37fb936365\" :secret \"b\")) +" + (cl-letf (((symbol-function 'epg-decrypt-string) + (lambda (&rest r) (prin1-to-string (cadr r)))) + ((symbol-function 'epg-find-configuration) + (lambda (&rest _) '((program . "/bin/true"))))) + (let* ((auth-sources (list plstore-file)) + (auth-source-do-cache nil) + (results (auth-source-search :host "localhost" + :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :login nil + :port nil + :secret "b" + :host "localhost" + :user "one two")))))))) + +;; json + +(ert-deftest auth-source-pass-extra-query-keywords--ws-host-json () + (ert-with-temp-file json-store + :suffix ".json" + :text "\ +[{\"host\":\"hello world\",\"secret\":\"a\"}, + {\"host\":\"localhost\",\"secret\":\"b\"}] +" + (let* ((auth-sources (list json-store)) + (auth-source-do-cache nil) + (results (auth-source-search :host "hello world"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results + '(( :host "hello world" + :secret "a"))))))) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-user-json () + (ert-with-temp-file json-store + :suffix ".json" + :text "\ +[{\"host\":\"localhost\", + \"user\":\"onetwo\", + \"secret\":\"a\"}, + {\"host\":\"localhost\", + \"user\":\"one two\", + \"secret\":\"b\"}] +" + (let* ((auth-sources (list json-store)) + (auth-source-do-cache nil) + (results (auth-source-search :host "localhost" :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results + '(( :host "localhost" + :user "one two" + :secret "b"))))))) + +;; secrets + +(require 'secrets) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-host-secrets () + (let ((auth-sources '("secrets:Test")) + (auth-source-do-cache nil) + (entries '(("nil@hello world:nil" + (:host . "hello world") + (:xdg:schema . "org.freedesktop.Secret.Generic")) + ("nil@localhost:nil" + (:host . "localhost") + (:xdg:schema . "org.freedesktop.Secret.Generic")))) + (secrets '(("nil@hello world:nil" . "a") + ("nil@localhost:nil" . "b")))) + + (cl-letf (((symbol-function 'secrets-search-items) + (lambda (_ &rest r) + (mapcan (lambda (s) + (and (seq-every-p (pcase-lambda (`(,k . ,v)) + (equal v (alist-get k (cdr s)))) + (map-pairs r)) + (list (car s)))) + entries))) + ((symbol-function 'secrets-get-secret) + (lambda (_ label) (assoc-default label secrets))) + ((symbol-function 'secrets-get-attributes) + (lambda (_ label) (assoc-default label entries)))) + + (let ((results (auth-source-search :host "hello world"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results + '(( :login nil + :port nil + :secret "a" + :host "hello world" + :xdg:schema "org.freedesktop.Secret.Generic")))))))) + +(ert-deftest auth-source-pass-extra-query-keywords--secrets-joao-user () + (let ((auth-sources '("secrets:Test")) + (auth-source-do-cache nil) + (entries '(("onetwo@localhost:nil" + (:host . "localhost") + (:user . "onetwo") + (:xdg:schema . "org.freedesktop.Secret.Generic")) + ("one two@localhost:nil" + (:host . "localhost") + (:user . "one two") + (:xdg:schema . "org.freedesktop.Secret.Generic")))) + (secrets '(("onetwo@localhost:nil" . "a") + ("one two@localhost:nil" . "b")))) + + (cl-letf (((symbol-function 'secrets-search-items) + (lambda (_ &rest r) + (mapcan (lambda (s) + (and (seq-every-p (pcase-lambda (`(,k . ,v)) + (equal v (alist-get k (cdr s)))) + (map-pairs r)) + (list (car s)))) + entries))) + ((symbol-function 'secrets-get-secret) + (lambda (_ label) (assoc-default label secrets))) + ((symbol-function 'secrets-get-attributes) + (lambda (_ label) (assoc-default label entries)))) + + (let ((results (auth-source-search :host "localhost" :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results + '(( :login nil + :port nil + :secret "b" + :host "localhost" + :user "one two" + :xdg:schema "org.freedesktop.Secret.Generic")))))))) + +;; Pass + +(ert-deftest auth-source-pass-extra-query-keywords--ws-host-pass () + (auth-source-pass--with-store '(("hello world:80" (secret . "a")) + ("localhost:80" (secret . "b"))) + (auth-source-pass-enable) + (let* ((auth-source-pass-extra-query-keywords t) + (results (auth-source-search :host "hello world"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :host "hello world" + :port "80" + :secret "a"))))))) + +(ert-deftest auth-source-pass-extra-query-keywords--ws-user-pass () + ;; "suffix" syntax + (auth-source-pass--with-store '(("localhost:80/onetwo" (secret . "a")) + ("localhost:80/one two" (secret . "b"))) + (auth-source-pass-enable) + (let* ((auth-source-pass-extra-query-keywords t) + (results (auth-source-search :host "localhost" :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :host "localhost" + :user "one two" + :port "80" + :secret "b")))))) + ;; "prefix" syntax + (auth-source-pass--with-store '(("onetwo@localhost:80" (secret . "a")) + ("one two@localhost:80" (secret . "b"))) + (auth-source-pass-enable) + (let* ((auth-source-pass-extra-query-keywords t) + (results (auth-source-search :host "localhost" :user "one two"))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results '(( :host "localhost" + :user "one two" + :port "80" + :secret "b"))))))) + (provide 'auth-source-pass-tests) ;;; auth-source-pass-tests.el ends here -- 2.38.1