Debian Codegraph @

test-suite/tests/00-repl-server.test

raw
1 ;;;; 00-repl-server.test --- REPL server. -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2016, 2017, 2021 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (repl-server)
20 #:use-module (system repl server)
21 #:use-module (ice-9 match)
22 #:use-module (ice-9 rdelim)
23 #:use-module (web uri)
24 #:use-module (web request)
25 #:use-module (test-suite lib))
26
27 (define (call-with-repl-server proc)
28 "Set up a REPL server in a separate process and call PROC with a
29 socket connected to that server."
30 ;; The REPL server requires thread. The test requires fork.
31 (unless (and (provided? 'threads) (provided? 'fork) (defined? 'mkdtemp))
32 (throw 'unsupported))
33
34 (let* ((tmpdir (mkdtemp "/tmp/repl-server-test-XXXXXX"))
35 (sockaddr (make-socket-address AF_UNIX (string-append tmpdir "/repl-server")))
36 (client-socket (socket AF_UNIX SOCK_STREAM 0)))
37 (false-if-exception (delete-file (sockaddr:path sockaddr)))
38
39 (match (primitive-fork)
40 (0
41 (dynamic-wind
42 (const #t)
43 (lambda ()
44 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0)))
45 (bind server-socket sockaddr)
46 (set! %load-verbosely #f)
47
48 (close-fdes 2)
49
50 ;; Arrange so that the alarming "possible break-in attempt"
51 ;; message doesn't show up when running the test suite.
52 (dup2 (open-fdes "/dev/null" O_WRONLY) 2)
53
54 (run-server server-socket)))
55 (lambda ()
56 (primitive-exit 0))))
57 (pid
58 (sigaction SIGPIPE SIG_IGN)
59 (dynamic-wind
60 (const #t)
61 (lambda ()
62 ;; XXX: We can't synchronize with the server's 'accept' call
63 ;; because it's buried inside 'run-server', hence this hack.
64 (let loop ((tries 0))
65 (catch 'system-error
66 (lambda ()
67 (connect client-socket sockaddr))
68 (lambda args
69 (when (memv (system-error-errno args)
70 (list ENOENT ECONNREFUSED))
71 (when (> tries 30)
72 (throw 'unresolved))
73 (usleep 100)
74 (loop (+ tries 1))))))
75
76 (proc client-socket))
77 (lambda ()
78 (false-if-exception (close-port client-socket))
79 (false-if-exception (kill pid SIGTERM))
80 (false-if-exception (delete-file (sockaddr:path sockaddr)))
81 (false-if-exception (rmdir tmpdir))
82 (sigaction SIGPIPE SIG_DFL)))))))
83
84 (define-syntax-rule (with-repl-server client-socket body ...)
85 "Evaluate BODY... in a context where CLIENT-SOCKET is bound to a
86 socket connected to a fresh REPL server."
87 (call-with-repl-server
88 (lambda (client-socket)
89 body ...)))
90
91 (define (read-until-prompt port str)
92 "Read from PORT until STR has been read or the end-of-file was
93 reached."
94 (let loop ()
95 (match (read-line port)
96 ((? eof-object?)
97 #t)
98 (line
99 (or (string=? line str) (loop))))))
100
101 (define %last-line-before-prompt
102 "Enter `,help' for help.")
103
104
105 ;;; REPL server tests.
106 ;;;
107 ;;; Since we call 'primitive-fork', these tests must run before any
108 ;;; tests that create threads.
109
110 (with-test-prefix "repl-server"
111
112 (pass-if-equal "simple expression"
113 "scheme@(repl-server)> $1 = 42\n"
114 (with-repl-server socket
115 (read-until-prompt socket %last-line-before-prompt)
116
117 ;; Wait until 'repl-reader' in boot-9 has written the prompt.
118 ;; Otherwise, if we write too quickly, 'repl-reader' checks for
119 ;; 'char-ready?' and doesn't print the prompt.
120 (match (select (list socket) '() (list socket) 3)
121 (((_) () ())
122 (display "(+ 40 2)\n(quit)\n" socket)
123 (read-string socket)))))
124
125 (pass-if "HTTP inter-protocol attack" ;CVE-2016-8606
126 (with-repl-server socket
127 ;; Avoid SIGPIPE when the server closes the connection.
128 (sigaction SIGPIPE SIG_IGN)
129
130 (read-until-prompt socket %last-line-before-prompt)
131
132 ;; Make sure the server reacts by closing the connection. If it
133 ;; fails to do that, this test hangs.
134 (catch 'system-error
135 (lambda ()
136 ;; Simulate an HTTP inter-protocol attack.
137 (write-request (build-request (string->uri "http://localhost"))
138 socket)
139
140 (let loop ((n 0))
141 (display "(+ 40 2)\n(quit)\n" socket) ;trigger EPIPE
142 (read-string socket)
143 (if (> n 5)
144 #f ;failure
145 (begin
146 (sleep 1)
147 (loop (+ 1 n))))))
148 (lambda args
149 (->bool (memv (system-error-errno args)
150 (list ECONNRESET EPIPE ECONNABORTED))))))))
151
152 ;;; Local Variables:
153 ;;; eval: (put 'with-repl-server 'scheme-indent-function 1)
154 ;;; End: