1 ;;; network-stream-tests.el --- tests for network processes -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
5 ;; Author: Lars Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (ert-deftest make-local-unix-server ()
30 (skip-unless (featurep 'make-network-process '(:family local)))
31 (let* ((file (make-temp-name "/tmp/server-test"))
36 :buffer (get-buffer-create "*server*")
40 (should (equal (process-contact server :local) file))
41 (delete-file (process-contact server :local))))
43 (ert-deftest make-ipv4-tcp-server-with-unspecified-port ()
52 (should (and (arrayp (process-contact server :local))
53 (numberp (aref (process-contact server :local) 4))
54 (> (aref (process-contact server :local) 4) 0)))
55 (delete-process server)))
57 (ert-deftest make-ipv4-tcp-server-with-specified-port ()
66 (should (and (arrayp (process-contact server :local))
67 (= (aref (process-contact server :local) 4) 57869)))
68 (delete-process server)))
70 (defun make-server (host)
76 :coding 'raw-text-unix
77 :buffer (get-buffer-create "*server*")
79 :sentinel 'server-sentinel
80 :filter 'server-process-filter
83 (defun server-sentinel (_proc _msg)
86 (defun server-process-filter (proc string)
87 (message "Received %s" string)
88 (let ((prev (process-get proc 'previous-string)))
90 (setq string (concat prev string))
91 (process-put proc 'previous-string nil)))
92 (if (and (not (string-match "\n" string))
93 (> (length string) 0))
94 (process-put proc 'previous-string string))
95 (let ((command (split-string string)))
97 ((equal (car command) "echo")
98 (process-send-string proc (concat (cadr command) "\n")))
102 (ert-deftest echo-server-with-dns ()
103 (let* ((server (make-server (system-name)))
104 (port (aref (process-contact server :local) 4))
105 (proc (make-network-process :name "foo"
106 :buffer (generate-new-buffer "*foo*")
109 (with-current-buffer (process-buffer proc)
110 (process-send-string proc "echo foo")
112 (should (equal (buffer-string) "foo\n")))
113 (delete-process server)))
115 (ert-deftest echo-server-with-localhost ()
116 (let* ((server (make-server 'local))
117 (port (aref (process-contact server :local) 4))
118 (proc (make-network-process :name "foo"
119 :buffer (generate-new-buffer "*foo*")
122 (with-current-buffer (process-buffer proc)
123 (process-send-string proc "echo foo")
125 (should (equal (buffer-string) "foo\n")))
126 (delete-process server)))
128 (ert-deftest echo-server-with-ip ()
129 (let* ((server (make-server 'local))
130 (port (aref (process-contact server :local) 4))
131 (proc (make-network-process :name "foo"
132 :buffer (generate-new-buffer "*foo*")
135 (with-current-buffer (process-buffer proc)
136 (process-send-string proc "echo foo")
138 (should (equal (buffer-string) "foo\n")))
139 (delete-process server)))
141 (ert-deftest echo-server-nowait ()
142 (let* ((server (make-server 'local))
143 (port (aref (process-contact server :local) 4))
144 (proc (make-network-process :name "foo"
145 :buffer (generate-new-buffer "*foo*")
150 (should (eq (process-status proc) 'connect))
151 (while (eq (process-status proc) 'connect)
153 (with-current-buffer (process-buffer proc)
154 (process-send-string proc "echo foo")
156 (should (equal (buffer-string) "foo\n")))
157 (delete-process server)))
159 (defun make-tls-server (port)
160 (start-process "gnutls" (generate-new-buffer "*tls*")
161 "gnutls-serv" "--http"
162 "--x509keyfile" "data/net/key.pem"
163 "--x509certfile" "data/net/cert.pem"
164 "--port" (format "%s" port)))
166 (ert-deftest connect-to-tls-ipv4-wait ()
167 (skip-unless (executable-find "gnutls-serv"))
168 (skip-unless (gnutls-available-p))
169 (let ((server (make-tls-server 44332))
173 (with-current-buffer (process-buffer server)
174 (message "gnutls-serv: %s" (buffer-string)))
176 ;; It takes a while for gnutls-serv to start.
177 (while (and (null (ignore-errors
178 (setq proc (make-network-process
180 :buffer (generate-new-buffer "*foo*")
183 (< (setq times (1+ times)) 10))
186 (gnutls-negotiate :process proc
187 :type 'gnutls-x509pki
188 :hostname "localhost")
189 (delete-process server)
190 (setq status (gnutls-peer-status proc))
191 (should (consp status))
192 (delete-process proc)
193 ;; This sleep-for is needed for the native MS-Windows build. If
194 ;; it is removed, the next test mysteriously fails because the
195 ;; initial part of the echo is not received.
197 (let ((issuer (plist-get (plist-get status :certificate) :issuer)))
198 (should (stringp issuer))
199 (setq issuer (split-string issuer ","))
200 (should (equal (nth 3 issuer) "O=Emacs Test Servicess LLC")))))
202 (ert-deftest connect-to-tls-ipv4-nowait ()
203 (skip-unless (executable-find "gnutls-serv"))
204 (skip-unless (gnutls-available-p))
205 (let ((server (make-tls-server 44331))
209 (with-current-buffer (process-buffer server)
210 (message "gnutls-serv: %s" (buffer-string)))
212 ;; It takes a while for gnutls-serv to start.
213 (while (and (null (ignore-errors
214 (setq proc (make-network-process
216 :buffer (generate-new-buffer "*foo*")
219 (cons 'gnutls-x509pki
220 (gnutls-boot-parameters
221 :hostname "localhost"))
224 (< (setq times (1+ times)) 10))
227 (while (eq (process-status proc) 'connect)
229 (delete-process server)
230 (setq status (gnutls-peer-status proc))
231 (should (consp status))
232 (delete-process proc)
233 (let ((issuer (plist-get (plist-get status :certificate) :issuer)))
234 (should (stringp issuer))
235 (setq issuer (split-string issuer ","))
236 (should (equal (nth 3 issuer) "O=Emacs Test Servicess LLC")))))
238 (ert-deftest connect-to-tls-ipv6-nowait ()
239 (skip-unless (executable-find "gnutls-serv"))
240 (skip-unless (gnutls-available-p))
241 (skip-unless (not (eq system-type 'windows-nt)))
242 (skip-unless (featurep 'make-network-process '(:family ipv6)))
243 (let ((server (make-tls-server 44333))
247 (with-current-buffer (process-buffer server)
248 (message "gnutls-serv: %s" (buffer-string)))
250 ;; It takes a while for gnutls-serv to start.
251 (while (and (null (ignore-errors
252 (setq proc (make-network-process
254 :buffer (generate-new-buffer "*foo*")
258 (cons 'gnutls-x509pki
259 (gnutls-boot-parameters
260 :hostname "localhost"))
263 (< (setq times (1+ times)) 10))
266 (while (eq (process-status proc) 'connect)
268 (delete-process server)
269 (setq status (gnutls-peer-status proc))
270 (should (consp status))
271 (delete-process proc)
272 (let ((issuer (plist-get (plist-get status :certificate) :issuer)))
273 (should (stringp issuer))
274 (setq issuer (split-string issuer ","))
275 (should (equal (nth 3 issuer) "O=Emacs Test Servicess LLC")))))
277 ;;; network-stream-tests.el ends here