aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorF. Jason Park2022-02-14 10:28:01 -0800
committerF. Jason Park2023-10-18 06:23:14 -0700
commit2061bf0645e1f577c380ec81805e463f3c6fec7c (patch)
treea87310150f2a00f9478d7b38f0b289c2b6d93f72 /test
parente93d99a4a0ce578249304dce350465c580a49892 (diff)
downloademacs-2061bf0645e1f577c380ec81805e463f3c6fec7c.tar.gz
emacs-2061bf0645e1f577c380ec81805e463f3c6fec7c.zip
Don't hard code server ports in SOCKS tests
* test/lisp/net/socks-tests.el (socks-tests-canned-server-create, socks-tests-filter-response-parsing-v4): Fix bug in process filter to prevent prepared outgoing responses from being implicitly encoded as UTF-8. Fix similar mistake in v4 filter test. (socks-tests-v4-basic, socks-tests-v5-auth-user-pass, socks-tests-v5-auth-user-pass-blank, socks-tests-v5-auth-none): Allow system to choose port instead of hard-coding it. (socks-tests-perform-hello-world-http-request): Add optional `method' parameter to specify a gateway method. (socks-tests-v5-auth-none): Move body to helper function of the same name. (socks-override-functions): New test ensuring top-level advice around `open-networks-stream' still supported. (Bug#53941)
Diffstat (limited to 'test')
-rw-r--r--test/lisp/net/socks-tests.el53
1 files changed, 37 insertions, 16 deletions
diff --git a/test/lisp/net/socks-tests.el b/test/lisp/net/socks-tests.el
index 958e2ff44a8..0890ace826f 100644
--- a/test/lisp/net/socks-tests.el
+++ b/test/lisp/net/socks-tests.el
@@ -63,21 +63,21 @@
63 (process-put proc 'socks-state socks-state-waiting) 63 (process-put proc 'socks-state socks-state-waiting)
64 (process-put proc 'socks-server-protocol 4) 64 (process-put proc 'socks-server-protocol 4)
65 (ert-info ("Receive initial incomplete segment") 65 (ert-info ("Receive initial incomplete segment")
66 (socks-filter proc (concat [0 90 0 0 93 184 216])) 66 (socks-filter proc (unibyte-string 0 90 0 0 93 184 216))
67 ;; From example.com: OK status ^ ^ msg start 67 ;; From example.com: OK status ^ ^ msg start
68 (ert-info ("State still set to waiting") 68 (ert-info ("State still set to waiting")
69 (should (eq (process-get proc 'socks-state) socks-state-waiting))) 69 (should (eq (process-get proc 'socks-state) socks-state-waiting)))
70 (ert-info ("Response field is nil because processing incomplete") 70 (ert-info ("Response field is nil because processing incomplete")
71 (should-not (process-get proc 'socks-response))) 71 (should-not (process-get proc 'socks-response)))
72 (ert-info ("Scratch field holds stashed partial payload") 72 (ert-info ("Scratch field holds stashed partial payload")
73 (should (string= (concat [0 90 0 0 93 184 216]) 73 (should (string= (unibyte-string 0 90 0 0 93 184 216)
74 (process-get proc 'socks-scratch))))) 74 (process-get proc 'socks-scratch)))))
75 (ert-info ("Last part arrives") 75 (ert-info ("Last part arrives")
76 (socks-filter proc "\42") ; ?\" 34 76 (socks-filter proc "\42") ; ?\" 34
77 (ert-info ("State transitions to complete (length check passes)") 77 (ert-info ("State transitions to complete (length check passes)")
78 (should (eq (process-get proc 'socks-state) socks-state-connected))) 78 (should (eq (process-get proc 'socks-state) socks-state-connected)))
79 (ert-info ("Scratch and response fields hold stash w. last chunk") 79 (ert-info ("Scratch and response fields hold stash w. last chunk")
80 (should (string= (concat [0 90 0 0 93 184 216 34]) 80 (should (string= (unibyte-string 0 90 0 0 93 184 216 34)
81 (process-get proc 'socks-response))) 81 (process-get proc 'socks-response)))
82 (should (string= (process-get proc 'socks-response) 82 (should (string= (process-get proc 'socks-response)
83 (process-get proc 'socks-scratch))))) 83 (process-get proc 'socks-scratch)))))
@@ -133,17 +133,19 @@ Vectors must match verbatim. Strings are considered regex patterns.")
133(defun socks-tests-canned-server-create () 133(defun socks-tests-canned-server-create ()
134 "Create and return a fake SOCKS server." 134 "Create and return a fake SOCKS server."
135 (let* ((port (nth 2 socks-server)) 135 (let* ((port (nth 2 socks-server))
136 (name (format "socks-server:%d" port)) 136 (name (format "socks-server:%s"
137 (if (numberp port) port (ert-test-name (ert-running-test)))))
137 (pats socks-tests-canned-server-patterns) 138 (pats socks-tests-canned-server-patterns)
138 (filt (lambda (proc line) 139 (filt (lambda (proc line)
139 (pcase-let ((`(,pat . ,resp) (pop pats))) 140 (pcase-let ((`(,pat . ,resp) (pop pats)))
140 (unless (or (and (vectorp pat) (equal pat (vconcat line))) 141 (unless (or (and (vectorp pat) (equal pat (vconcat line)))
141 (string-match-p pat line)) 142 (and (stringp pat) (string-match-p pat line)))
142 (error "Unknown request: %s" line)) 143 (error "Unknown request: %s" line))
144 (setq resp (apply #'unibyte-string (append resp nil)))
143 (let ((print-escape-control-characters t)) 145 (let ((print-escape-control-characters t))
144 (message "[%s] <- %s" name (prin1-to-string line)) 146 (message "[%s] <- %s" name (prin1-to-string line))
145 (message "[%s] -> %s" name (prin1-to-string resp))) 147 (message "[%s] -> %s" name (prin1-to-string resp)))
146 (process-send-string proc (concat resp))))) 148 (process-send-string proc resp))))
147 (serv (make-network-process :server 1 149 (serv (make-network-process :server 1
148 :buffer (get-buffer-create name) 150 :buffer (get-buffer-create name)
149 :filter filt 151 :filter filt
@@ -151,8 +153,10 @@ Vectors must match verbatim. Strings are considered regex patterns.")
151 :family 'ipv4 153 :family 'ipv4
152 :host 'local 154 :host 'local
153 :coding 'binary 155 :coding 'binary
154 :service port))) 156 :service (or port t))))
155 (set-process-query-on-exit-flag serv nil) 157 (set-process-query-on-exit-flag serv nil)
158 (unless (numberp (nth 2 socks-server))
159 (setf (nth 2 socks-server) (process-contact serv :service)))
156 serv)) 160 serv))
157 161
158(defvar socks-tests--hello-world-http-request-pattern 162(defvar socks-tests--hello-world-http-request-pattern
@@ -161,9 +165,9 @@ Vectors must match verbatim. Strings are considered regex patterns.")
161 "Content-Length: 13\r\n\r\n" 165 "Content-Length: 13\r\n\r\n"
162 "Hello World!\n"))) 166 "Hello World!\n")))
163 167
164(defun socks-tests-perform-hello-world-http-request () 168(defun socks-tests-perform-hello-world-http-request (&optional method)
165 "Start canned server, validate hello-world response, and finalize." 169 "Start canned server, validate hello-world response, and finalize."
166 (let* ((url-gateway-method 'socks) 170 (let* ((url-gateway-method (or method 'socks))
167 (url (url-generic-parse-url "http://example.com")) 171 (url (url-generic-parse-url "http://example.com"))
168 (server (socks-tests-canned-server-create)) 172 (server (socks-tests-canned-server-create))
169 ;; 173 ;;
@@ -191,7 +195,7 @@ Vectors must match verbatim. Strings are considered regex patterns.")
191 195
192(ert-deftest socks-tests-v4-basic () 196(ert-deftest socks-tests-v4-basic ()
193 "Show correct preparation of SOCKS4 connect command (Bug#46342)." 197 "Show correct preparation of SOCKS4 connect command (Bug#46342)."
194 (let ((socks-server '("server" "127.0.0.1" 10079 4)) 198 (let ((socks-server '("server" "127.0.0.1" t 4))
195 (url-user-agent "Test/4-basic") 199 (url-user-agent "Test/4-basic")
196 (socks-tests-canned-server-patterns 200 (socks-tests-canned-server-patterns
197 `(([4 1 0 80 93 184 216 34 ?f ?o ?o 0] . [0 90 0 0 0 0 0 0]) 201 `(([4 1 0 80 93 184 216 34 ?f ?o ?o 0] . [0 90 0 0 0 0 0 0])
@@ -213,7 +217,7 @@ Vectors must match verbatim. Strings are considered regex patterns.")
213(ert-deftest socks-tests-v5-auth-user-pass () 217(ert-deftest socks-tests-v5-auth-user-pass ()
214 "Verify correct handling of SOCKS5 user/pass authentication." 218 "Verify correct handling of SOCKS5 user/pass authentication."
215 (should (assq 2 socks-authentication-methods)) 219 (should (assq 2 socks-authentication-methods))
216 (let ((socks-server '("server" "127.0.0.1" 10080 5)) 220 (let ((socks-server '("server" "127.0.0.1" t 5))
217 (socks-username "foo") 221 (socks-username "foo")
218 (socks-password "bar") 222 (socks-password "bar")
219 (url-user-agent "Test/auth-user-pass") 223 (url-user-agent "Test/auth-user-pass")
@@ -247,7 +251,7 @@ Vectors must match verbatim. Strings are considered regex patterns.")
247(ert-deftest socks-tests-v5-auth-user-pass-blank () 251(ert-deftest socks-tests-v5-auth-user-pass-blank ()
248 "Verify correct SOCKS5 user/pass authentication with empty pass." 252 "Verify correct SOCKS5 user/pass authentication with empty pass."
249 (should (assq 2 socks-authentication-methods)) 253 (should (assq 2 socks-authentication-methods))
250 (let ((socks-server '("server" "127.0.0.1" 10081 5)) 254 (let ((socks-server '("server" "127.0.0.1" t 5))
251 (socks-username "foo") ; defaults to (user-login-name) 255 (socks-username "foo") ; defaults to (user-login-name)
252 (socks-password "") ; simulate user hitting enter when prompted 256 (socks-password "") ; simulate user hitting enter when prompted
253 (url-user-agent "Test/auth-user-pass-blank") 257 (url-user-agent "Test/auth-user-pass-blank")
@@ -264,9 +268,9 @@ Vectors must match verbatim. Strings are considered regex patterns.")
264;; against curl 7.71 with the following options: 268;; against curl 7.71 with the following options:
265;; $ curl --verbose --proxy socks5h://127.0.0.1:10082 example.com 269;; $ curl --verbose --proxy socks5h://127.0.0.1:10082 example.com
266 270
267(ert-deftest socks-tests-v5-auth-none () 271(defun socks-tests-v5-auth-none (method)
268 "Verify correct handling of SOCKS5 when auth method 0 requested." 272 "Verify correct handling of SOCKS5 when auth method 0 requested."
269 (let ((socks-server '("server" "127.0.0.1" 10082 5)) 273 (let ((socks-server '("server" "127.0.0.1" t 5))
270 (socks-authentication-methods (append socks-authentication-methods 274 (socks-authentication-methods (append socks-authentication-methods
271 nil)) 275 nil))
272 (url-user-agent "Test/auth-none") 276 (url-user-agent "Test/auth-none")
@@ -278,7 +282,24 @@ Vectors must match verbatim. Strings are considered regex patterns.")
278 (socks-unregister-authentication-method 2) 282 (socks-unregister-authentication-method 2)
279 (should-not (assq 2 socks-authentication-methods)) 283 (should-not (assq 2 socks-authentication-methods))
280 (ert-info ("Make HTTP request over SOCKS5 with no auth method") 284 (ert-info ("Make HTTP request over SOCKS5 with no auth method")
281 (socks-tests-perform-hello-world-http-request))) 285 (socks-tests-perform-hello-world-http-request method)))
282 (should (assq 2 socks-authentication-methods))) 286 (should (assq 2 socks-authentication-methods)))
283 287
288(ert-deftest socks-tests-v5-auth-none ()
289 (socks-tests-v5-auth-none 'socks))
290
291;; This simulates the top-level advice around `open-network-stream'
292;; that's applied when loading the library with a non-nil
293;; `socks-override-functions'.
294(ert-deftest socks-override-functions ()
295 (should-not socks-override-functions)
296 (should-not (advice-member-p #'socks--open-network-stream
297 'open-network-stream))
298 (advice-add 'open-network-stream :around #'socks--open-network-stream)
299 (unwind-protect (let ((socks-override-functions t))
300 (socks-tests-v5-auth-none 'native))
301 (advice-remove 'open-network-stream #'socks--open-network-stream))
302 (should-not (advice-member-p #'socks--open-network-stream
303 'open-network-stream)))
304
284;;; socks-tests.el ends here 305;;; socks-tests.el ends here