unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 81db9ad948f46c0738af34b311a5a309013f3a77 14701 bytes (raw)
name: test/lisp/erc/erc-sasl-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 
;;; erc-sasl-tests.el --- Tests for erc-sasl.  -*- lexical-binding:t -*-

;; Copyright (C) 2022 Free Software Foundation, Inc.
;;
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'ert-x)
(require 'erc-sasl)

(ert-deftest erc-sasl--mechanism-offered-p ()
  (let ((erc-sasl--options '((mechanism . external))))
    (should (erc-sasl--mechanism-offered-p "foo,external"))
    (should (erc-sasl--mechanism-offered-p "external,bar"))
    (should (erc-sasl--mechanism-offered-p "foo,external,bar"))
    (should-not (erc-sasl--mechanism-offered-p "fooexternal"))
    (should-not (erc-sasl--mechanism-offered-p "externalbar"))))

(ert-deftest erc-sasl--read-password ()
  (ert-info ("Explicit erc-sasl-password")
    (let ((erc-sasl--options '((password . "foo"))))
      (should (string= (erc-sasl--read-password nil) "foo"))))

  (ert-info ("Fallback to erc-session-password")
    (let ((erc-session-password "bar")
          (erc-networks--id (erc-networks--id-create nil)))
      (should (string= (erc-sasl--read-password nil) "bar")))
    (let ((erc-session-password "bar")
          (erc-sasl--options '((user . "tester") (password)))
          (erc-networks--id (erc-networks--id-create nil)))
      (should (string= (erc-sasl--read-password nil) "bar"))))

  (let* ((entries (list
                   "machine FSF.chat port 6697 user bob password sesame"
                   ;; This must come *after* ^, else *1 (below) always passes
                   "machine GNU/chat port 6697 user bob password spam"
                   "machine MyHost port irc password 123"))
         (netrc-file (make-temp-file "auth-source-test" nil nil
                                     (mapconcat 'identity entries "\n")))
         (auth-sources (list netrc-file))
         (erc-session-server "irc.gnu.org")
         (erc-session-port 6697)
         (erc-networks--id (erc-networks--id-create nil))
         ;;
         (erc-sasl-auth-source-function #'erc--auth-source-search)
         erc-server-announced-name ; too early
         auth-source-do-cache)

    (unwind-protect
        (ert-info ("Auth source")

          (ert-info ("Symbol as password specifies machine")
            (let ((erc-sasl--options '((user . "bob")
                                       (password . FSF.chat)))
                  (erc-networks--id (make-erc-networks--id)))
              (should (string= (erc-sasl--read-password nil) "sesame"))))

          (ert-info ("Use session ID when password empty") ; *1
            (let ((erc-sasl--options '((user . "bob") (password)))
                  (erc-networks--id (erc-networks--id-create 'GNU/chat)))
              (should (string= (erc-sasl--read-password nil) "spam")))))

      (delete-file netrc-file))

    (ert-info ("Prompt when search fails and server password null")
      (let ((erc-sasl-auth-source-function #'ignore))
        (should (string= (ert-simulate-keys "baz\r"
                           (erc-sasl--read-password "pwd:"))
                         "baz"))))))

(ert-deftest erc-sasl-create-client--plain ()
  (let* ((erc-session-password "password123")
         (erc-server-current-nick "tester")
         (erc-session-port 1667)
         (erc-session-server "localhost")
         (client (erc-sasl--create-client 'plain))
         (result (sasl-next-step client nil)))
    (should (equal (format "%S" [erc-sasl--plain-response
                                 "\0tester\0password123"])
                   (format "%S" result)))
    (should (string= (sasl-step-data result) "\0tester\0password123"))
    (should-not (sasl-next-step client result)))
  (should (equal (assoc-default "PLAIN" sasl-mechanism-alist) '(sasl-plain))))

(ert-deftest erc-sasl-create-client--external ()
  (let* ((erc-server-current-nick "tester")
         (client (erc-sasl--create-client 'external))
         (result (sasl-next-step client nil)))
    (should (equal (format "%S" [ignore nil]) (format "%S" result)))
    (should-not (sasl-step-data result))
    (should-not (sasl-next-step client result)))
  (should-not (member "EXTERNAL" sasl-mechanisms))
  (should-not (assoc-default "EXTERNAL" sasl-mechanism-alist)))

(ert-deftest erc-sasl-create-client--scram-sha-1 ()
  (let* ((erc-server-current-nick "jilles")
         (erc-session-password "sesame")
         (erc-sasl--options '((authzid . "jilles")))
         (mock-rvs (list "c5RqLCZy0L4fGkKAZ0hujFBs" ""))
         (sasl-unique-id-function (lambda () (pop mock-rvs)))
         (client (erc-sasl--create-client 'scram-sha-1))
         (step (sasl-next-step client nil)))
    (ert-info ("Client's initial request")
      (let ((req "n,a=jilles,n=jilles,r=c5RqLCZy0L4fGkKAZ0hujFBs"))
        (should (equal (format "%S"
                               `[erc-compat--sasl-scram-client-first-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's initial response")
      (let ((resp (concat "r=c5RqLCZy0L4fGkKAZ0hujFBsXQoKcivqCw9iDZPSpb,"
                          "s=5mJO6d4rjCnsBU1X,"
                          "i=4096"))
            (req (concat "c=bixhPWppbGxlcyw=,"
                         "r=c5RqLCZy0L4fGkKAZ0hujFBsXQoKcivqCw9iDZPSpb,"
                         "p=OVUhgPu8wEm2cDoVLfaHzVUYPWU=")))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should (equal (format "%S"
                               `[erc-sasl--scram-sha-1-client-final-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's final message")
      (let ((resp "v=ZWR23c9MJir0ZgfGf5jEtLOn6Ng="))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should-not (sasl-step-data step)))))
  (should (eq sasl-unique-id-function #'sasl-unique-id-function)))

(ert-deftest erc-sasl-create-client--scram-sha-256 ()
  (unless (featurep 'sasl-scram-sha256)
    (ert-skip "Emacs lacks sasl-scram-sha256"))
  (let* ((erc-server-current-nick "jilles")
         (erc-session-password "sesame")
         (erc-sasl--options '((authzid . "jilles")))
         (mock-rvs (list "c5RqLCZy0L4fGkKAZ0hujFBs" ""))
         (sasl-unique-id-function (lambda () (pop mock-rvs)))
         (client (erc-sasl--create-client 'scram-sha-256))
         (step (sasl-next-step client nil)))
    (ert-info ("Client's initial request")
      (let ((req "n,a=jilles,n=jilles,r=c5RqLCZy0L4fGkKAZ0hujFBs"))
        (should (equal (format "%S"
                               `[erc-compat--sasl-scram-client-first-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's initial response")
      (let ((resp (concat
                   "r=c5RqLCZy0L4fGkKAZ0hujFBse697140729d8445fb95ec94ceacb14b3,"
                   "s=MTk2M2VkMzM5ZmU0NDRiYmI0MzIyOGVhN2YwNzYwNmI=,"
                   "i=4096"))
            (req (concat
                  "c=bixhPWppbGxlcyw=,"
                  "r=c5RqLCZy0L4fGkKAZ0hujFBse697140729d8445fb95ec94ceacb14b3,"
                  "p=1vDesVBzJmv0lX0Ae1kHFtdVHkC6j4gISKVqaR45HFg=")))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should (equal (format "%S"
                               `[erc-sasl--scram-sha-256-client-final-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's final message")
      (let ((resp "v=gUePTYSZN9xgcE06KSyKO9fUmSwH26qifoapXyEs75s="))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should-not (sasl-step-data step)))))
  (should (eq sasl-unique-id-function #'sasl-unique-id-function)))

(ert-deftest erc-sasl-create-client--scram-sha-256--no-authzid ()
  (unless (featurep 'sasl-scram-sha256)
    (ert-skip "Emacs lacks sasl-scram-sha256"))
  (let* ((erc-server-current-nick "jilles")
         (erc-session-password "sesame")
         (mock-rvs (list "c5RqLCZy0L4fGkKAZ0hujFBs" ""))
         (sasl-unique-id-function (lambda () (pop mock-rvs)))
         (client (erc-sasl--create-client 'scram-sha-256))
         (step (sasl-next-step client nil)))
    (ert-info ("Client's initial request")
      (let ((req "n,,n=jilles,r=c5RqLCZy0L4fGkKAZ0hujFBs"))
        (should (equal (format "%S"
                               `[erc-compat--sasl-scram-client-first-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's initial response")
      (let ((resp (concat
                   "r=c5RqLCZy0L4fGkKAZ0hujFBsd4067f0afdb54c3dbd4fe645b84cae37,"
                   "s=ZTg1MmE1YmFhZGI1NDcyMjk3NzYwZmRjZDM3Y2I1OTM=,"
                   "i=4096"))
            (req (concat
                  "c=biws,"
                  "r=c5RqLCZy0L4fGkKAZ0hujFBsd4067f0afdb54c3dbd4fe645b84cae37,"
                  "p=LP4sjJrjJKp5qTsARyZCppXpKLu4FMM284hNESPvGhI=")))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should (equal (format "%S"
                               `[erc-sasl--scram-sha-256-client-final-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's final message")
      (let ((resp "v=847WXfnmReGyE1qlq1And6R4bPBNROTZ7EMS/QrJtUM="))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should-not (sasl-step-data step)))))
  (should (eq sasl-unique-id-function #'sasl-unique-id-function)))

(ert-deftest erc-sasl-create-client--scram-sha-512--no-authzid ()
  (unless (featurep 'sasl-scram-sha256)
    (ert-skip "Emacs lacks sasl-scram-sha512"))
  (let* ((erc-server-current-nick "jilles")
         (erc-session-password "sesame")
         (mock-rvs (list "c5RqLCZy0L4fGkKAZ0hujFBs" ""))
         (sasl-unique-id-function (lambda () (pop mock-rvs)))
         (client (erc-sasl--create-client 'scram-sha-512))
         (step (sasl-next-step client nil)))
    (ert-info ("Client's initial request")
      (let ((req "n,,n=jilles,r=c5RqLCZy0L4fGkKAZ0hujFBs"))
        (should (equal (format "%S"
                               `[erc-compat--sasl-scram-client-first-message
                                 ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's initial response")
      (let ((resp (concat
                   "r=c5RqLCZy0L4fGkKAZ0hujFBs54c592745ce14e559fcc3f27b15464f6,"
                   "s=YzMzOWZiY2U0YzcwNDA0M2I4ZGE2M2ZjOTBjODExZTM=,"
                   "i=4096"))
            (req (concat
                  "c=biws,"
                  "r=c5RqLCZy0L4fGkKAZ0hujFBs54c592745ce14e559fcc3f27b15464f6,"
                  "p=vMBb9tKxFAfBtel087/GLbo4objAIYr1wM+mFv/jYLKXE"
                  "NUF0vynm81qQbywQE5ScqFFdAfwYMZq/lj4s0V1OA==")))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should (equal (format
                        "%S" `[erc-sasl--scram-sha-512-client-final-message
                               ,req])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) req))))
    (ert-info ("Server's final message")
      (let ((resp (concat "v=Va7NIvt8wCdhvxnv+bZriSxGoto6On5EVnRHO/ece8zs0"
                          "qpQassdqir1Zlwh3e3EmBq+kcSy+ClNCsbzBpXe/w==")))
        (sasl-step-set-data step resp)
        (setq step (sasl-next-step client step))
        (should-not (sasl-step-data step)))))
  (should (eq sasl-unique-id-function #'sasl-unique-id-function)))

(defconst erc-sasl-tests-ecdsa-key-file "
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIJueQ3W2IrGbe9wKdOI75yGS7PYZSj6W4tg854hlsvmoAoGCCqGSM49
AwEHoUQDQgAEAZmaVhNSMmV5r8FXPvKuMnqDKyIA9pDHN5TNMfiF3mMeikGgK10W
IRX9cyi2wdYg9mUUYyh9GKdBCYHGUJAiCA==
-----END EC PRIVATE KEY-----
")

(ert-deftest erc-sasl-create-client-ecdsa ()
  :tags '(:unstable)
  ;; This is currently useless because it just roundtrips shelling out
  ;; to pkeyutl.
  (ert-skip "Placeholder")
  (unless (executable-find "openssl")
    (ert-skip "System lacks openssl"))
  (ert-with-temp-file keyfile
    :prefix "ecdsa_key"
    :suffix ".pem"
    :text erc-sasl-tests-ecdsa-key-file
    (let* ((erc-server-current-nick "jilles")
           (erc-sasl--options `((password . ,keyfile)))
           (client (erc-sasl--create-client 'ecdsa-nist256p-challenge))
           (step (sasl-next-step client nil)))
      (ert-info ("Client's initial request")
        (should (equal (format "%S" [erc-sasl--ecdsa-first "jilles"])
                       (format "%S" step)))
        (should (string= (sasl-step-data step) "jilles")))
      (ert-info ("Server's initial response")
        (let ((resp (concat "\0\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"
                            "\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37")))
          (sasl-step-set-data step resp)
          (setq step (sasl-next-step client step))
          (ert-with-temp-file sigfile
            :prefix "ecdsa_sig"
            :suffix ".sig"
            :text (sasl-step-data step)
            (with-temp-buffer
              (set-buffer-multibyte nil)
              (insert resp)
              (let ((ec (call-process-region
                         (point-min) (point-max)
                         "openssl" 'delete t nil "pkeyutl"
                         "-inkey" keyfile "-sigfile" sigfile
                         "-verify")))
                (unless (zerop ec)
                  (message "%s" (buffer-string)))
                (should (zerop ec)))))))
      (should-not (sasl-next-step client step)))))

;;; erc-sasl-tests.el ends here

debug log:

solving 81db9ad948 ...
found 81db9ad948 in https://yhetil.org/emacs-bugs/87y1sdk1fg.fsf__277.595808596232$1668476372$gmane$org@neverwas.me/ ||
	https://yhetil.org/emacs-bugs/87o7taoohd.fsf__5476.25507913239$1668353859$gmane$org@neverwas.me/ ||
	https://yhetil.org/emacs-bugs/87a64unifk.fsf@neverwas.me/ ||
	https://yhetil.org/emacs-bugs/877czuks8k.fsf__25904.9538218946$1668666695$gmane$org@neverwas.me/

applying [1/1] https://yhetil.org/emacs-bugs/87y1sdk1fg.fsf__277.595808596232$1668476372$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-sasl-tests.el b/test/lisp/erc/erc-sasl-tests.el
new file mode 100644
index 0000000000..81db9ad948

Checking patch test/lisp/erc/erc-sasl-tests.el...
Applied patch test/lisp/erc/erc-sasl-tests.el cleanly.

skipping https://yhetil.org/emacs-bugs/87o7taoohd.fsf__5476.25507913239$1668353859$gmane$org@neverwas.me/ for 81db9ad948
skipping https://yhetil.org/emacs-bugs/87o7taoohd.fsf__5476.25507913239$1668353859$gmane$org@neverwas.me/ for 81db9ad948
skipping https://yhetil.org/emacs-bugs/87a64unifk.fsf@neverwas.me/ for 81db9ad948
skipping https://yhetil.org/emacs-bugs/877czuks8k.fsf__25904.9538218946$1668666695$gmane$org@neverwas.me/ for 81db9ad948
index at:
100644 81db9ad948f46c0738af34b311a5a309013f3a77	test/lisp/erc/erc-sasl-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).