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
|
;;;; 00-repl-server.test --- REPL server. -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; Copyright (C) 2016, 2017 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (repl-server)
#:use-module (system repl server)
#:use-module (ice-9 match)
#:use-module (ice-9 rdelim)
#:use-module (web uri)
#:use-module (web request)
#:use-module (test-suite lib))
(define (call-with-repl-server proc)
"Set up a REPL server in a separate process and call PROC with a
socket connected to that server."
(let ((sockaddr (make-socket-address AF_UNIX "/tmp/repl-server"))
(client-socket (socket AF_UNIX SOCK_STREAM 0)))
(false-if-exception
(delete-file (sockaddr:path sockaddr)))
;; The REPL server requires thread. The test requires fork.
(unless (and (provided? 'threads) (provided? 'fork))
(throw 'unsupported))
(match (primitive-fork)
(0
(dynamic-wind
(const #t)
(lambda ()
(let ((server-socket (socket AF_UNIX SOCK_STREAM 0)))
(bind server-socket sockaddr)
(set! %load-verbosely #f)
(close-fdes 2)
;; Arrange so that the alarming "possible break-in attempt"
;; message doesn't show up when running the test suite.
(dup2 (open-fdes "/dev/null" O_WRONLY) 2)
(run-server server-socket)))
(lambda ()
(primitive-exit 0))))
(pid
(sigaction SIGPIPE SIG_IGN)
(dynamic-wind
(const #t)
(lambda ()
;; XXX: We can't synchronize with the server's 'accept' call
;; because it's buried inside 'run-server', hence this hack.
(let loop ((tries 0))
(catch 'system-error
(lambda ()
(connect client-socket sockaddr))
(lambda args
(when (memv (system-error-errno args)
(list ENOENT ECONNREFUSED))
(when (> tries 30)
(throw 'unresolved))
(usleep 100)
(loop (+ tries 1))))))
(proc client-socket))
(lambda ()
(false-if-exception (close-port client-socket))
(false-if-exception (kill pid SIGTERM))
(sigaction SIGPIPE SIG_DFL)))))))
(define-syntax-rule (with-repl-server client-socket body ...)
"Evaluate BODY... in a context where CLIENT-SOCKET is bound to a
socket connected to a fresh REPL server."
(call-with-repl-server
(lambda (client-socket)
body ...)))
(define (read-until-prompt port str)
"Read from PORT until STR has been read or the end-of-file was
reached."
(let loop ()
(match (read-line port)
((? eof-object?)
#t)
(line
(or (string=? line str) (loop))))))
(define %last-line-before-prompt
"Enter `,help' for help.")
;;; REPL server tests.
;;;
;;; Since we call 'primitive-fork', these tests must run before any
;;; tests that create threads.
(with-test-prefix "repl-server"
(pass-if-equal "simple expression"
"scheme@(repl-server)> $1 = 42\n"
(with-repl-server socket
(read-until-prompt socket %last-line-before-prompt)
;; Wait until 'repl-reader' in boot-9 has written the prompt.
;; Otherwise, if we write too quickly, 'repl-reader' checks for
;; 'char-ready?' and doesn't print the prompt.
(match (select (list socket) '() (list socket) 3)
(((_) () ())
(display "(+ 40 2)\n(quit)\n" socket)
(read-string socket)))))
(pass-if "HTTP inter-protocol attack" ;CVE-2016-8606
(with-repl-server socket
;; Avoid SIGPIPE when the server closes the connection.
(sigaction SIGPIPE SIG_IGN)
(read-until-prompt socket %last-line-before-prompt)
;; Simulate an HTTP inter-protocol attack.
(write-request (build-request (string->uri "http://localhost"))
socket)
;; Make sure the server reacts by closing the connection. If it
;; fails to do that, this test hangs.
(catch 'system-error
(lambda ()
(let loop ((n 0))
(display "(+ 40 2)\n(quit)\n" socket) ;trigger EPIPE
(read-string socket)
(if (> n 5)
#f ;failure
(begin
(sleep 1)
(loop (+ 1 n))))))
(lambda args
(->bool (memv (system-error-errno args)
(list ECONNRESET EPIPE ECONNABORTED))))))))
;;; Local Variables:
;;; eval: (put 'with-repl-server 'scheme-indent-function 1)
;;; End:
|