popen.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ;;;; popen.test --- exercise ice-9/popen.scm -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2003, 2006, 2010, 2011, 2013, 2014, 2020
  4. ;;;; 2021 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-ice-9-popen)
  20. #:use-module (test-suite lib)
  21. #:use-module (ice-9 receive)
  22. #:use-module (ice-9 rdelim))
  23. (define mingw?
  24. (string-contains %host-type "-mingw32"))
  25. ;; read from PORT until eof is reached, return what's read as a string
  26. (define (read-string-to-eof port)
  27. (do ((lst '() (cons c lst))
  28. (c (read-char port) (read-char port)))
  29. ((eof-object? c)
  30. (list->string (reverse! lst)))))
  31. ;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
  32. ;; generated rather than a SIGPIPE signal
  33. (define (with-epipe thunk)
  34. (dynamic-wind
  35. (lambda ()
  36. (sigaction SIGPIPE SIG_IGN))
  37. thunk
  38. restore-signals))
  39. (define-syntax-rule (if-supported body ...)
  40. (begin body ...))
  41. (if-supported
  42. (use-modules (ice-9 popen))
  43. ;;
  44. ;; open-input-pipe
  45. ;;
  46. (with-test-prefix "open-input-pipe"
  47. (pass-if-exception "no args" exception:wrong-num-args
  48. (open-input-pipe))
  49. (pass-if "port?"
  50. (port? (open-input-pipe "echo hello")))
  51. (pass-if "echo hello"
  52. (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
  53. ;; exercise file descriptor setups when stdin is the same as stderr
  54. (pass-if "stdin==stderr"
  55. (let ((port (open-file "/dev/null" "r+")))
  56. (with-input-from-port port
  57. (lambda ()
  58. (with-error-to-port port
  59. (lambda ()
  60. (open-input-pipe "echo hello"))))))
  61. #t)
  62. ;; exercise file descriptor setups when stdout is the same as stderr
  63. (pass-if "stdout==stderr"
  64. (let ((port (open-file "/dev/null" "r+")))
  65. (with-output-to-port port
  66. (lambda ()
  67. (with-error-to-port port
  68. (lambda ()
  69. (open-input-pipe "echo hello"))))))
  70. #t)
  71. (pass-if "open-input-pipe process gets (current-input-port) as stdin"
  72. (let* ((p2c (pipe))
  73. (port (with-input-from-port (car p2c)
  74. (lambda ()
  75. (open-input-pipe "read line && echo $line")))))
  76. (display "hello\n" (cdr p2c))
  77. (force-output (cdr p2c))
  78. (let ((result (eq? (read port) 'hello)))
  79. (close-port (cdr p2c))
  80. (close-pipe port)
  81. result)))
  82. ;; After the child closes stdout (which it indicates here by writing
  83. ;; "closed" to stderr), the parent should see eof. In Guile 1.6.4
  84. ;; and earlier a duplicate of stdout existed in the child, meaning
  85. ;; eof was not seen.
  86. ;;
  87. ;; Note that the objective here is to test that the parent sees EOF
  88. ;; while the child is still alive. (It is obvious that the parent
  89. ;; must see EOF once the child has died.) The use of the `p2c'
  90. ;; pipe, and `echo closed' and `read' in the child, allows us to be
  91. ;; sure that we are testing what the parent sees at a point where
  92. ;; the child has closed stdout but is still alive.
  93. (pass-if "no duplicate"
  94. (when mingw? (throw 'unresolved))
  95. (let* ((c2p (pipe))
  96. (p2c (pipe))
  97. (port (with-error-to-port (cdr c2p)
  98. (lambda ()
  99. (with-input-from-port (car p2c)
  100. (lambda ()
  101. (open-input-pipe
  102. (format #f "exec 1>~a; echo closed 1>&2; \
  103. exec 2>~a; read REPLY"
  104. %null-device %null-device))))))))
  105. (close-port (cdr c2p)) ;; write side
  106. (let ((result (eof-object? (read-char port))))
  107. (display "hello!\n" (cdr p2c))
  108. (force-output (cdr p2c))
  109. (close-pipe port)
  110. result))))
  111. ;;
  112. ;; open-output-pipe
  113. ;;
  114. (with-test-prefix "open-output-pipe"
  115. (pass-if-exception "no args" exception:wrong-num-args
  116. (open-output-pipe))
  117. (pass-if "port?"
  118. (port? (open-output-pipe "exit 0")))
  119. ;; exercise file descriptor setups when stdin is the same as stderr
  120. (pass-if "stdin==stderr"
  121. (let ((port (open-file "/dev/null" "r+")))
  122. (with-input-from-port port
  123. (lambda ()
  124. (with-error-to-port port
  125. (lambda ()
  126. (open-output-pipe "exit 0"))))))
  127. #t)
  128. ;; exercise file descriptor setups when stdout is the same as stderr
  129. (pass-if "stdout==stderr"
  130. (let ((port (open-file "/dev/null" "r+")))
  131. (with-output-to-port port
  132. (lambda ()
  133. (with-error-to-port port
  134. (lambda ()
  135. (open-output-pipe "exit 0"))))))
  136. #t)
  137. ;; After the child closes stdin (which it indicates here by writing
  138. ;; "closed" to stderr), the parent should see a broken pipe. We
  139. ;; setup to see this as EPIPE (rather than SIGPIPE). In Guile 1.6.4
  140. ;; and earlier a duplicate of stdin existed in the child, preventing
  141. ;; the broken pipe occurring.
  142. ;;
  143. ;; Note that the objective here is to test that the parent sees a
  144. ;; broken pipe while the child is still alive. (It is obvious that
  145. ;; the parent will see a broken pipe once the child has died.) The
  146. ;; use of the `c2p' pipe, and the repeated `echo closed' in the
  147. ;; child, allows us to be sure that we are testing what the parent
  148. ;; sees at a point where the child has closed stdin but is still
  149. ;; alive.
  150. ;;
  151. ;; Note that `with-epipe' must apply only to the parent and not to
  152. ;; the child process; we rely on the child getting SIGPIPE, to
  153. ;; terminate it (and avoid leaving a zombie).
  154. (pass-if "no duplicate"
  155. (let* ((c2p (pipe))
  156. (port (with-error-to-port (cdr c2p)
  157. (lambda ()
  158. (open-output-pipe
  159. (string-append "exec guile --no-auto-compile -s \""
  160. (getenv "TEST_SUITE_DIR")
  161. "/tests/popen-child.scm\""))))))
  162. (close-port (cdr c2p)) ;; write side
  163. (with-epipe
  164. (lambda ()
  165. (let ((result
  166. (and (char? (read-char (car c2p))) ;; wait for child to do its thing
  167. (catch 'system-error
  168. (lambda ()
  169. (write-char #\x port)
  170. (force-output port)
  171. #f)
  172. (lambda (key name fmt args errno-list)
  173. (= (car errno-list) EPIPE))))))
  174. ;; Now close our reading end of the pipe. This should give
  175. ;; the child a broken pipe and so allow it to exit.
  176. (close-port (car c2p))
  177. (close-pipe port)
  178. result))))))
  179. (with-test-prefix "open-pipe*"
  180. (pass-if-equal "OPEN_BOTH"
  181. '(0 (good!))
  182. ;; This test ensures that the ports that underlie the read/write
  183. ;; port are unbuffered. If they were buffered, the child process
  184. ;; would wait in 'read' forever.
  185. (let ((pipe (open-pipe* OPEN_BOTH "guile" "-c"
  186. (object->string
  187. '(begin
  188. (setvbuf (current-output-port) 'line)
  189. (write '(hello!))
  190. (newline)
  191. (let ((greeting (read)))
  192. (write '(good!))))))))
  193. (setvbuf pipe 'line)
  194. (let ((return (read pipe)))
  195. (write '(hi!) pipe)
  196. (newline pipe)
  197. (let ((last (read pipe)))
  198. (list (close-pipe pipe) last))))))
  199. ;;
  200. ;; close-pipe
  201. ;;
  202. (with-test-prefix "close-pipe"
  203. (pass-if-exception "no args" exception:wrong-num-args
  204. (close-pipe))
  205. (pass-if "exit 0"
  206. (let ((st (close-pipe (open-output-pipe "exit 0"))))
  207. (and (status:exit-val st)
  208. (= 0 (status:exit-val st)))))
  209. (pass-if "exit 1"
  210. (let ((st (close-pipe (open-output-pipe "exit 1"))))
  211. (and (status:exit-val st)
  212. (= 1 (status:exit-val st)))))))
  213. ;;
  214. ;; pipeline related tests
  215. ;;
  216. (pass-if-equal "open-process"
  217. '("HELLO WORLD" 0)
  218. (receive (from to pid)
  219. ((@@ (ice-9 popen) open-process) OPEN_BOTH
  220. "tr" "[:lower:]" "[:upper:]")
  221. (display "hello world" to) (close to)
  222. (list (read-string from)
  223. (status:exit-val (cdr (waitpid pid))))))
  224. (pass-if-equal "piped-process"
  225. 42
  226. (status:exit-val
  227. (cdr (waitpid ((@@ (ice-9 popen) piped-process)
  228. "./meta/guile" '("-c" "(exit 42)"))))))
  229. (pass-if-equal "piped-process: with output"
  230. '("foo bar\n" 0)
  231. (let* ((p (pipe))
  232. (pid ((@@ (ice-9 popen) piped-process) "echo" '("foo" "bar")
  233. (cons (port->fdes (car p))
  234. (port->fdes (cdr p))))))
  235. (list (read-string (car p))
  236. (status:exit-val (cdr (waitpid pid))))))
  237. (pass-if-equal "pipeline"
  238. '("HELLO WORLD\n" (0 0))
  239. (receive (from to pids)
  240. (pipeline '(("echo" "hello world")
  241. ("tr" "[:lower:]" "[:upper:]")))
  242. (list (read-string from)
  243. (map (compose status:exit-val cdr waitpid) pids))))