popen.test 6.6 KB

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