popen.test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;;; popen.test --- exercise ice-9/popen.scm -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2003, 2006, 2010, 2011, 2013, 2014 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 (test-suite test-ice-9-popen)
  19. #:use-module (test-suite lib))
  20. ;; read from PORT until eof is reached, return what's read as a string
  21. (define (read-string-to-eof port)
  22. (do ((lst '() (cons c lst))
  23. (c (read-char port) (read-char port)))
  24. ((eof-object? c)
  25. (list->string (reverse! lst)))))
  26. ;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
  27. ;; generated rather than a SIGPIPE signal
  28. (define (with-epipe thunk)
  29. (dynamic-wind
  30. (lambda ()
  31. (sigaction SIGPIPE SIG_IGN))
  32. thunk
  33. restore-signals))
  34. (define-syntax-rule (if-supported body ...)
  35. (begin body ...))
  36. (if-supported
  37. (use-modules (ice-9 popen))
  38. ;;
  39. ;; open-input-pipe
  40. ;;
  41. (with-test-prefix "open-input-pipe"
  42. (pass-if-exception "no args" exception:wrong-num-args
  43. (open-input-pipe))
  44. (pass-if "port?"
  45. (port? (open-input-pipe "echo hello")))
  46. (pass-if "echo hello"
  47. (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
  48. ;; exercise file descriptor setups when stdin is the same as stderr
  49. (pass-if "stdin==stderr"
  50. (let ((port (open-file "/dev/null" "r+")))
  51. (with-input-from-port port
  52. (lambda ()
  53. (with-error-to-port port
  54. (lambda ()
  55. (open-input-pipe "echo hello"))))))
  56. #t)
  57. ;; exercise file descriptor setups when stdout is the same as stderr
  58. (pass-if "stdout==stderr"
  59. (let ((port (open-file "/dev/null" "r+")))
  60. (with-output-to-port port
  61. (lambda ()
  62. (with-error-to-port port
  63. (lambda ()
  64. (open-input-pipe "echo hello"))))))
  65. #t)
  66. (pass-if "open-input-pipe process gets (current-input-port) as stdin"
  67. (let* ((p2c (pipe))
  68. (port (with-input-from-port (car p2c)
  69. (lambda ()
  70. (open-input-pipe "read line && echo $line")))))
  71. (display "hello\n" (cdr p2c))
  72. (force-output (cdr p2c))
  73. (let ((result (eq? (read port) 'hello)))
  74. (close-port (cdr p2c))
  75. (close-pipe port)
  76. result)))
  77. ;; After the child closes stdout (which it indicates here by writing
  78. ;; "closed" to stderr), the parent should see eof. In Guile 1.6.4
  79. ;; and earlier a duplicate of stdout existed in the child, meaning
  80. ;; eof was not seen.
  81. ;;
  82. ;; Note that the objective here is to test that the parent sees EOF
  83. ;; while the child is still alive. (It is obvious that the parent
  84. ;; must see EOF once the child has died.) The use of the `p2c'
  85. ;; pipe, and `echo closed' and `read' in the child, allows us to be
  86. ;; sure that we are testing what the parent sees at a point where
  87. ;; the child has closed stdout but is still alive.
  88. (pass-if "no duplicate"
  89. (let* ((c2p (pipe))
  90. (p2c (pipe))
  91. (port (with-error-to-port (cdr c2p)
  92. (lambda ()
  93. (with-input-from-port (car p2c)
  94. (lambda ()
  95. (open-input-pipe
  96. (format #f "exec 1>~a; echo closed 1>&2; \
  97. exec 2>~a; read REPLY"
  98. %null-device %null-device))))))))
  99. (close-port (cdr c2p)) ;; write side
  100. (let ((result (eof-object? (read-char port))))
  101. (display "hello!\n" (cdr p2c))
  102. (force-output (cdr p2c))
  103. (close-pipe port)
  104. result))))
  105. ;;
  106. ;; open-output-pipe
  107. ;;
  108. (with-test-prefix "open-output-pipe"
  109. (pass-if-exception "no args" exception:wrong-num-args
  110. (open-output-pipe))
  111. (pass-if "port?"
  112. (port? (open-output-pipe "exit 0")))
  113. ;; exercise file descriptor setups when stdin is the same as stderr
  114. (pass-if "stdin==stderr"
  115. (let ((port (open-file "/dev/null" "r+")))
  116. (with-input-from-port port
  117. (lambda ()
  118. (with-error-to-port port
  119. (lambda ()
  120. (open-output-pipe "exit 0"))))))
  121. #t)
  122. ;; exercise file descriptor setups when stdout is the same as stderr
  123. (pass-if "stdout==stderr"
  124. (let ((port (open-file "/dev/null" "r+")))
  125. (with-output-to-port port
  126. (lambda ()
  127. (with-error-to-port port
  128. (lambda ()
  129. (open-output-pipe "exit 0"))))))
  130. #t)
  131. ;; After the child closes stdin (which it indicates here by writing
  132. ;; "closed" to stderr), the parent should see a broken pipe. We
  133. ;; setup to see this as EPIPE (rather than SIGPIPE). In Guile 1.6.4
  134. ;; and earlier a duplicate of stdin existed in the child, preventing
  135. ;; the broken pipe occurring.
  136. ;;
  137. ;; Note that the objective here is to test that the parent sees a
  138. ;; broken pipe while the child is still alive. (It is obvious that
  139. ;; the parent will see a broken pipe once the child has died.) The
  140. ;; use of the `c2p' pipe, and the repeated `echo closed' in the
  141. ;; child, allows us to be sure that we are testing what the parent
  142. ;; sees at a point where the child has closed stdin but is still
  143. ;; alive.
  144. ;;
  145. ;; Note that `with-epipe' must apply only to the parent and not to
  146. ;; the child process; we rely on the child getting SIGPIPE, to
  147. ;; terminate it (and avoid leaving a zombie).
  148. (pass-if "no duplicate"
  149. (let* ((c2p (pipe))
  150. (port (with-error-to-port (cdr c2p)
  151. (lambda ()
  152. (open-output-pipe
  153. (string-append "exec guile --no-auto-compile -s \""
  154. (getenv "TEST_SUITE_DIR")
  155. "/tests/popen-child.scm\""))))))
  156. (close-port (cdr c2p)) ;; write side
  157. (with-epipe
  158. (lambda ()
  159. (let ((result
  160. (and (char? (read-char (car c2p))) ;; wait for child to do its thing
  161. (catch 'system-error
  162. (lambda ()
  163. (write-char #\x port)
  164. (force-output port)
  165. #f)
  166. (lambda (key name fmt args errno-list)
  167. (= (car errno-list) EPIPE))))))
  168. ;; Now close our reading end of the pipe. This should give
  169. ;; the child a broken pipe and so allow it to exit.
  170. (close-port (car c2p))
  171. (close-pipe port)
  172. result))))))
  173. ;;
  174. ;; close-pipe
  175. ;;
  176. (with-test-prefix "close-pipe"
  177. (pass-if-exception "no args" exception:wrong-num-args
  178. (close-pipe))
  179. (pass-if "exit 0"
  180. (let ((st (close-pipe (open-output-pipe "exit 0"))))
  181. (and (status:exit-val st)
  182. (= 0 (status:exit-val st)))))
  183. (pass-if "exit 1"
  184. (let ((st (close-pipe (open-output-pipe "exit 1"))))
  185. (and (status:exit-val st)
  186. (= 1 (status:exit-val st)))))))