00-repl-server.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;;; 00-repl-server.test --- REPL server. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2016 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. (let ((sockaddr (make-socket-address AF_UNIX "/tmp/repl-server"))
  29. (client-socket (socket AF_UNIX SOCK_STREAM 0)))
  30. (false-if-exception
  31. (delete-file (sockaddr:path sockaddr)))
  32. ;; The REPL server requires threads.
  33. (unless (provided? 'threads)
  34. (throw 'unsupported))
  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. (sigaction SIGPIPE SIG_DFL)))))))
  73. (define-syntax-rule (with-repl-server client-socket body ...)
  74. "Evaluate BODY... in a context where CLIENT-SOCKET is bound to a
  75. socket connected to a fresh REPL server."
  76. (call-with-repl-server
  77. (lambda (client-socket)
  78. body ...)))
  79. (define (read-until-prompt port str)
  80. "Read from PORT until STR has been read or the end-of-file was
  81. reached."
  82. (let loop ()
  83. (match (read-line port)
  84. ((? eof-object?)
  85. #t)
  86. (line
  87. (or (string=? line str) (loop))))))
  88. (define %last-line-before-prompt
  89. "Enter `,help' for help.")
  90. ;;; REPL server tests.
  91. ;;;
  92. ;;; Since we call 'primitive-fork', these tests must run before any
  93. ;;; tests that create threads.
  94. (with-test-prefix "repl-server"
  95. (pass-if-equal "simple expression"
  96. "scheme@(repl-server)> $1 = 42\n"
  97. (with-repl-server socket
  98. (read-until-prompt socket %last-line-before-prompt)
  99. ;; Wait until 'repl-reader' in boot-9 has written the prompt.
  100. ;; Otherwise, if we write too quickly, 'repl-reader' checks for
  101. ;; 'char-ready?' and doesn't print the prompt.
  102. (match (select (list socket) '() (list socket) 3)
  103. (((_) () ())
  104. (display "(+ 40 2)\n(quit)\n" socket)
  105. (read-string socket)))))
  106. (pass-if "HTTP inter-protocol attack" ;CVE-2016-8606
  107. (with-repl-server socket
  108. ;; Avoid SIGPIPE when the server closes the connection.
  109. (sigaction SIGPIPE SIG_IGN)
  110. (read-until-prompt socket %last-line-before-prompt)
  111. ;; Simulate an HTTP inter-protocol attack.
  112. (write-request (build-request (string->uri "http://localhost"))
  113. socket)
  114. ;; Make sure the server reacts by closing the connection. If it
  115. ;; fails to do that, this test hangs.
  116. (catch 'system-error
  117. (lambda ()
  118. (let loop ((n 0))
  119. (display "(+ 40 2)\n(quit)\n" socket) ;trigger EPIPE
  120. (read-string socket)
  121. (if (> n 5)
  122. #f ;failure
  123. (begin
  124. (sleep 1)
  125. (loop (+ 1 n))))))
  126. (lambda args
  127. (->bool (memv (system-error-errno args)
  128. (list ECONNRESET EPIPE))))))))
  129. ;;; Local Variables:
  130. ;;; eval: (put 'with-repl-server 'scheme-indent-function 1)
  131. ;;; End: