123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- (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)))
-
- (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)
-
-
- (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 ()
-
-
- (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.")
- (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)
-
-
-
- (match (select (list socket) '() (list socket) 3)
- (((_) () ())
- (display "(+ 40 2)\n(quit)\n" socket)
- (read-string socket)))))
- (pass-if "HTTP inter-protocol attack"
- (with-repl-server socket
-
- (sigaction SIGPIPE SIG_IGN)
- (read-until-prompt socket %last-line-before-prompt)
-
- (write-request (build-request (string->uri "http://localhost"))
- socket)
-
-
- (catch 'system-error
- (lambda ()
- (let loop ((n 0))
- (display "(+ 40 2)\n(quit)\n" socket)
- (read-string socket)
- (if (> n 5)
- #f
- (begin
- (sleep 1)
- (loop (+ 1 n))))))
- (lambda args
- (->bool (memv (system-error-errno args)
- (list ECONNRESET EPIPE ECONNABORTED))))))))
|