00-repl-server.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;;; 00-repl-server.test --- REPL server. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2016, 2017 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. (define-module (repl-server)
  19. #:use-module (system repl server)
  20. #:use-module (ice-9 match)
  21. #:use-module (ice-9 rdelim)
  22. #:use-module (web uri)
  23. #:use-module (web request)
  24. #:use-module (test-suite lib))
  25. ;; FIXME: replace with mkdtemp! (or equivalent) when available
  26. (define (make-tempdir)
  27. (let loop ((try 0)
  28. (n (random:uniform)))
  29. (let* ((path (string-append "/tmp/repl-server-test-" (number->string n)))
  30. (dir (false-if-exception (mkdir path #o700))))
  31. (cond
  32. (dir path)
  33. ((> try 10)
  34. (error "Unable to create directory in /tmp for 00-repl-server.test"))
  35. (else (loop (1+ try) (random:uniform)))))))
  36. (define (call-with-repl-server proc)
  37. "Set up a REPL server in a separate process and call PROC with a
  38. socket connected to that server."
  39. (let* ((tmpdir (make-tempdir))
  40. (sockaddr (make-socket-address AF_UNIX (string-append tmpdir "/repl-server")))
  41. (client-socket (socket AF_UNIX SOCK_STREAM 0)))
  42. (false-if-exception (delete-file (sockaddr:path sockaddr)))
  43. ;; The REPL server requires thread. The test requires fork.
  44. (unless (and (provided? 'threads) (provided? 'fork))
  45. (throw 'unsupported))
  46. (match (primitive-fork)
  47. (0
  48. (dynamic-wind
  49. (const #t)
  50. (lambda ()
  51. (let ((server-socket (socket AF_UNIX SOCK_STREAM 0)))
  52. (bind server-socket sockaddr)
  53. (set! %load-verbosely #f)
  54. (close-fdes 2)
  55. ;; Arrange so that the alarming "possible break-in attempt"
  56. ;; message doesn't show up when running the test suite.
  57. (dup2 (open-fdes "/dev/null" O_WRONLY) 2)
  58. (run-server server-socket)))
  59. (lambda ()
  60. (primitive-exit 0))))
  61. (pid
  62. (sigaction SIGPIPE SIG_IGN)
  63. (dynamic-wind
  64. (const #t)
  65. (lambda ()
  66. ;; XXX: We can't synchronize with the server's 'accept' call
  67. ;; because it's buried inside 'run-server', hence this hack.
  68. (let loop ((tries 0))
  69. (catch 'system-error
  70. (lambda ()
  71. (connect client-socket sockaddr))
  72. (lambda args
  73. (when (memv (system-error-errno args)
  74. (list ENOENT ECONNREFUSED))
  75. (when (> tries 30)
  76. (throw 'unresolved))
  77. (usleep 100)
  78. (loop (+ tries 1))))))
  79. (proc client-socket))
  80. (lambda ()
  81. (false-if-exception (close-port client-socket))
  82. (false-if-exception (kill pid SIGTERM))
  83. (false-if-exception (delete-file (sockaddr:path sockaddr)))
  84. (false-if-exception (rmdir tmpdir))
  85. (sigaction SIGPIPE SIG_DFL)))))))
  86. (define-syntax-rule (with-repl-server client-socket body ...)
  87. "Evaluate BODY... in a context where CLIENT-SOCKET is bound to a
  88. socket connected to a fresh REPL server."
  89. (call-with-repl-server
  90. (lambda (client-socket)
  91. body ...)))
  92. (define (read-until-prompt port str)
  93. "Read from PORT until STR has been read or the end-of-file was
  94. reached."
  95. (let loop ()
  96. (match (read-line port)
  97. ((? eof-object?)
  98. #t)
  99. (line
  100. (or (string=? line str) (loop))))))
  101. (define %last-line-before-prompt
  102. "Enter `,help' for help.")
  103. ;;; REPL server tests.
  104. ;;;
  105. ;;; Since we call 'primitive-fork', these tests must run before any
  106. ;;; tests that create threads.
  107. (with-test-prefix "repl-server"
  108. (pass-if-equal "simple expression"
  109. "scheme@(repl-server)> $1 = 42\n"
  110. (with-repl-server socket
  111. (read-until-prompt socket %last-line-before-prompt)
  112. ;; Wait until 'repl-reader' in boot-9 has written the prompt.
  113. ;; Otherwise, if we write too quickly, 'repl-reader' checks for
  114. ;; 'char-ready?' and doesn't print the prompt.
  115. (match (select (list socket) '() (list socket) 3)
  116. (((_) () ())
  117. (display "(+ 40 2)\n(quit)\n" socket)
  118. (read-string socket)))))
  119. (pass-if "HTTP inter-protocol attack" ;CVE-2016-8606
  120. (with-repl-server socket
  121. ;; Avoid SIGPIPE when the server closes the connection.
  122. (sigaction SIGPIPE SIG_IGN)
  123. (read-until-prompt socket %last-line-before-prompt)
  124. ;; Simulate an HTTP inter-protocol attack.
  125. (write-request (build-request (string->uri "http://localhost"))
  126. socket)
  127. ;; Make sure the server reacts by closing the connection. If it
  128. ;; fails to do that, this test hangs.
  129. (catch 'system-error
  130. (lambda ()
  131. (let loop ((n 0))
  132. (display "(+ 40 2)\n(quit)\n" socket) ;trigger EPIPE
  133. (read-string socket)
  134. (if (> n 5)
  135. #f ;failure
  136. (begin
  137. (sleep 1)
  138. (loop (+ 1 n))))))
  139. (lambda args
  140. (->bool (memv (system-error-errno args)
  141. (list ECONNRESET EPIPE ECONNABORTED))))))))
  142. ;;; Local Variables:
  143. ;;; eval: (put 'with-repl-server 'scheme-indent-function 1)
  144. ;;; End: