00-repl-server.test 5.5 KB

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