diff options
| author | F. Jason Park | 2021-01-10 12:40:53 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2021-01-10 12:40:53 +0100 |
| commit | 09ee2d0095b6cbc4e28e417d3c42e61b319a2745 (patch) | |
| tree | 9f42422747d603d5e2c89c332c6755376f58ccb7 | |
| parent | 690cf6b8d8b8827f046bc1e24b2e556afeff976c (diff) | |
| download | emacs-09ee2d0095b6cbc4e28e417d3c42e61b319a2745.tar.gz emacs-09ee2d0095b6cbc4e28e417d3c42e61b319a2745.zip | |
Create new test file for socks.el
* test/lisp/net/socks-tests.el (socks-tests-auth-filter-url-http): Add
SOCKS5 authentication test and fake server (bug#45162).
| -rw-r--r-- | test/lisp/net/socks-tests.el | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/test/lisp/net/socks-tests.el b/test/lisp/net/socks-tests.el new file mode 100644 index 00000000000..b378ed2964e --- /dev/null +++ b/test/lisp/net/socks-tests.el | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | ;;; socks-tests.el --- tests for SOCKS -*- coding: utf-8; lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2021 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 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. | ||
| 11 | |||
| 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. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'socks) | ||
| 25 | (require 'url-http) | ||
| 26 | |||
| 27 | (defvar socks-tests-canned-server-port nil) | ||
| 28 | |||
| 29 | (defun socks-tests-canned-server-create (verbatim patterns) | ||
| 30 | "Create a fake SOCKS server and return the process. | ||
| 31 | |||
| 32 | `VERBATIM' and `PATTERNS' are dotted alists containing responses. | ||
| 33 | Requests are tried in order. On failure, an error is raised." | ||
| 34 | (let* ((buf (generate-new-buffer "*canned-socks-server*")) | ||
| 35 | (filt (lambda (proc line) | ||
| 36 | (let ((resp (or (assoc-default line verbatim | ||
| 37 | (lambda (k s) ; s is line | ||
| 38 | (string= (concat k) s))) | ||
| 39 | (assoc-default line patterns | ||
| 40 | (lambda (p s) | ||
| 41 | (string-match-p p s)))))) | ||
| 42 | (unless resp | ||
| 43 | (error "Unknown request: %s" line)) | ||
| 44 | (let ((print-escape-control-characters t)) | ||
| 45 | (princ (format "<- %s\n" (prin1-to-string line)) buf) | ||
| 46 | (princ (format "-> %s\n" (prin1-to-string resp)) buf)) | ||
| 47 | (process-send-string proc (concat resp))))) | ||
| 48 | (srv (make-network-process :server 1 | ||
| 49 | :buffer buf | ||
| 50 | :filter filt | ||
| 51 | :name "server" | ||
| 52 | :family 'ipv4 | ||
| 53 | :host 'local | ||
| 54 | :service socks-tests-canned-server-port))) | ||
| 55 | (set-process-query-on-exit-flag srv nil) | ||
| 56 | (princ (format "[%s] Listening on localhost:10080\n" srv) buf) | ||
| 57 | srv)) | ||
| 58 | |||
| 59 | ;; Add ([5 3 0 1 2] . [5 2]) to the `verbatim' list below to validate | ||
| 60 | ;; against curl 7.71 with the following options: | ||
| 61 | ;; $ curl --verbose -U foo:bar --proxy socks5h://127.0.0.1:10080 example.com | ||
| 62 | ;; | ||
| 63 | ;; If later implementing version 4a, try these: | ||
| 64 | ;; [4 1 0 80 0 0 0 1 0 ?e ?x ?a ?m ?p ?l ?e ?. ?c ?o ?m 0] . [0 90 0 0 0 0 0 0] | ||
| 65 | ;; $ curl --verbose --proxy socks4a://127.0.0.1:10080 example.com | ||
| 66 | |||
| 67 | (ert-deftest socks-tests-auth-filter-url-http () | ||
| 68 | "Verify correct handling of SOCKS5 user/pass authentication." | ||
| 69 | (let* ((socks-server '("server" "127.0.0.1" 10080 5)) | ||
| 70 | (socks-username "foo") | ||
| 71 | (socks-password "bar") | ||
| 72 | (url-gateway-method 'socks) | ||
| 73 | (url (url-generic-parse-url "http://example.com")) | ||
| 74 | (verbatim '(([5 2 0 2] . [5 2]) | ||
| 75 | ([1 3 ?f ?o ?o 3 ?b ?a ?r] . [1 0]) | ||
| 76 | ([5 1 0 3 11 ?e ?x ?a ?m ?p ?l ?e ?. ?c ?o ?m 0 80] | ||
| 77 | . [5 0 0 1 0 0 0 0 0 0]))) | ||
| 78 | (patterns | ||
| 79 | `(("^GET /" . ,(concat "HTTP/1.1 200 OK\r\n" | ||
| 80 | "Content-Type: text/plain; charset=UTF-8\r\n" | ||
| 81 | "Content-Length: 13\r\n\r\n" | ||
| 82 | "Hello World!\n")))) | ||
| 83 | (socks-tests-canned-server-port 10080) | ||
| 84 | (server (socks-tests-canned-server-create verbatim patterns)) | ||
| 85 | (tries 10) | ||
| 86 | ;; | ||
| 87 | done | ||
| 88 | ;; | ||
| 89 | (cb (lambda (&rest _r) | ||
| 90 | (goto-char (point-min)) | ||
| 91 | (should (search-forward "Hello World" nil t)) | ||
| 92 | (setq done t))) | ||
| 93 | (buf (url-http url cb '(nil)))) | ||
| 94 | (ert-info ("Connect to HTTP endpoint over SOCKS5 with USER/PASS method") | ||
| 95 | (while (and (not done) (< 0 (cl-decf tries))) ; cl-lib via url-http | ||
| 96 | (sleep-for 0.1))) | ||
| 97 | (should done) | ||
| 98 | (delete-process server) | ||
| 99 | (kill-buffer (process-buffer server)) | ||
| 100 | (kill-buffer buf) | ||
| 101 | (ignore url-gateway-method))) | ||
| 102 | |||
| 103 | ;;; socks-tests.el ends here | ||