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
20(define-module (test-suite test-ice-9-popen)
21  #:use-module (test-suite lib)
22  #:use-module (ice-9 receive)
23  #:use-module (ice-9 rdelim))
24
25
26(define mingw?
27  (string-contains %host-type "-mingw32"))
28
29;; read from PORT until eof is reached, return what's read as a string
30(define (read-string-to-eof port)
31  (do ((lst '() (cons c lst))
32       (c (read-char port) (read-char port)))
33      ((eof-object? c)
34       (list->string (reverse! lst)))))
35
36;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
37;; generated rather than a SIGPIPE signal
38(define (with-epipe thunk)
39  (dynamic-wind
40      (lambda ()
41	(sigaction SIGPIPE SIG_IGN))
42      thunk
43      restore-signals))
44
45(define-syntax-rule (if-supported body ...)
46  (begin body ...))
47
48(if-supported
49 (use-modules (ice-9 popen))
50
51
52 ;;
53 ;; open-input-pipe
54 ;;
55
56 (with-test-prefix "open-input-pipe"
57
58   (pass-if-exception "no args" exception:wrong-num-args
59     (open-input-pipe))
60
61   (pass-if "port?"
62     (port? (open-input-pipe "echo hello")))
63
64   (pass-if "echo hello"
65     (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
66
67   ;; exercise file descriptor setups when stdin is the same as stderr
68   (pass-if "stdin==stderr"
69     (let ((port (open-file "/dev/null" "r+")))
70       (with-input-from-port port
71         (lambda ()
72           (with-error-to-port port
73             (lambda ()
74               (open-input-pipe "echo hello"))))))
75     #t)
76
77   ;; exercise file descriptor setups when stdout is the same as stderr
78   (pass-if "stdout==stderr"
79     (let ((port (open-file "/dev/null" "r+")))
80       (with-output-to-port port
81         (lambda ()
82           (with-error-to-port port
83             (lambda ()
84               (open-input-pipe "echo hello"))))))
85     #t)
86
87   (pass-if "open-input-pipe process gets (current-input-port) as stdin"
88     (let* ((p2c (pipe))
89            (port (with-input-from-port (car p2c)
90                    (lambda ()
91                      (open-input-pipe "read line && echo $line")))))
92       (display "hello\n" (cdr p2c))
93       (force-output (cdr p2c))
94       (let ((result (eq? (read port) 'hello)))
95         (close-port (cdr p2c))
96         (close-pipe port)
97         result)))
98
99   ;; After the child closes stdout (which it indicates here by writing
100   ;; "closed" to stderr), the parent should see eof.  In Guile 1.6.4
101   ;; and earlier a duplicate of stdout existed in the child, meaning
102   ;; eof was not seen.
103   ;;
104   ;; Note that the objective here is to test that the parent sees EOF
105   ;; while the child is still alive.  (It is obvious that the parent
106   ;; must see EOF once the child has died.)  The use of the `p2c'
107   ;; pipe, and `echo closed' and `read' in the child, allows us to be
108   ;; sure that we are testing what the parent sees at a point where
109   ;; the child has closed stdout but is still alive.
110   (pass-if "no duplicate"
111     (when mingw? (throw 'unresolved))
112     (let* ((c2p (pipe))
113            (p2c (pipe))
114            (port (with-error-to-port (cdr c2p)
115                    (lambda ()
116                      (with-input-from-port (car p2c)
117                        (lambda ()
118                          (open-input-pipe
119                           (format #f "exec 1>~a; echo closed 1>&2; \
120exec 2>~a; read REPLY"
121                                   %null-device %null-device))))))))
122       (close-port (cdr c2p)) ;; write side
123       (let ((result (eof-object? (read-char port))))
124         (display "hello!\n" (cdr p2c))
125         (force-output (cdr p2c))
126         (close-pipe port)
127         result))))
128
129 ;;
130 ;; open-output-pipe
131 ;;
132
133 (with-test-prefix "open-output-pipe"
134
135   (pass-if-exception "no args" exception:wrong-num-args
136     (open-output-pipe))
137
138   (pass-if "port?"
139     (port? (open-output-pipe "exit 0")))
140
141   ;; exercise file descriptor setups when stdin is the same as stderr
142   (pass-if "stdin==stderr"
143     (let ((port (open-file "/dev/null" "r+")))
144       (with-input-from-port port
145         (lambda ()
146           (with-error-to-port port
147             (lambda ()
148               (open-output-pipe "exit 0"))))))
149     #t)
150
151   ;; exercise file descriptor setups when stdout is the same as stderr
152   (pass-if "stdout==stderr"
153     (let ((port (open-file "/dev/null" "r+")))
154       (with-output-to-port port
155         (lambda ()
156           (with-error-to-port port
157             (lambda ()
158               (open-output-pipe "exit 0"))))))
159     #t)
160
161   ;; After the child closes stdin (which it indicates here by writing
162   ;; "closed" to stderr), the parent should see a broken pipe.  We
163   ;; setup to see this as EPIPE (rather than SIGPIPE).  In Guile 1.6.4
164   ;; and earlier a duplicate of stdin existed in the child, preventing
165   ;; the broken pipe occurring.
166   ;;
167   ;; Note that the objective here is to test that the parent sees a
168   ;; broken pipe while the child is still alive.  (It is obvious that
169   ;; the parent will see a broken pipe once the child has died.)  The
170   ;; use of the `c2p' pipe, and the repeated `echo closed' in the
171   ;; child, allows us to be sure that we are testing what the parent
172   ;; sees at a point where the child has closed stdin but is still
173   ;; alive.
174   ;;
175   ;; Note that `with-epipe' must apply only to the parent and not to
176   ;; the child process; we rely on the child getting SIGPIPE, to
177   ;; terminate it (and avoid leaving a zombie).
178   (pass-if "no duplicate"
179     (let* ((c2p (pipe))
180            (port (with-error-to-port (cdr c2p)
181                    (lambda ()
182                      (open-output-pipe
183                       (string-append "exec guile --no-auto-compile -s \""
184                                      (getenv "TEST_SUITE_DIR")
185                                      "/tests/popen-child.scm\""))))))
186       (close-port (cdr c2p)) ;; write side
187       (with-epipe
188        (lambda ()
189          (let ((result
190                 (and (char? (read-char (car c2p))) ;; wait for child to do its thing
191                      (catch 'system-error
192                        (lambda ()
193                          (write-char #\x port)
194                          (force-output port)
195                          #f)
196                        (lambda (key name fmt args errno-list)
197                          (= (car errno-list) EPIPE))))))
198            ;; Now close our reading end of the pipe.  This should give
199            ;; the child a broken pipe and so allow it to exit.
200            (close-port (car c2p))
201            (close-pipe port)
202            result))))))
203
204
205 (with-test-prefix "open-pipe*"
206
207   (pass-if-equal "OPEN_BOTH"
208       '(0 (good!))
209     ;; This test ensures that the ports that underlie the read/write
210     ;; port are unbuffered.  If they were buffered, the child process
211     ;; would wait in 'read' forever.
212     (let ((pipe (open-pipe* OPEN_BOTH "guile" "-c"
213                             (object->string
214                              '(begin
215                                 (setvbuf (current-output-port) 'line)
216                                 (write '(hello!))
217                                 (newline)
218                                 (let ((greeting (read)))
219                                   (write '(good!))))))))
220       (setvbuf pipe 'line)
221       (let ((return (read pipe)))
222         (write '(hi!) pipe)
223         (newline pipe)
224         (let ((last (read pipe)))
225           (list (close-pipe pipe) last))))))
226
227 ;;
228 ;; close-pipe
229 ;;
230
231 (with-test-prefix "close-pipe"
232
233   (pass-if-exception "no args" exception:wrong-num-args
234     (close-pipe))
235
236   (pass-if "exit 0"
237     (let ((st (close-pipe (open-output-pipe "exit 0"))))
238       (and (status:exit-val st)
239            (= 0 (status:exit-val st)))))
240
241   (pass-if "exit 1"
242     (let ((st (close-pipe (open-output-pipe "exit 1"))))
243       (and (status:exit-val st)
244            (= 1 (status:exit-val st)))))))
245
246
247;;
248;; pipeline related tests
249;;
250
251(pass-if-equal "open-process"
252    '("HELLO WORLD" 0)
253  (receive (from to pid)
254      ((@@ (ice-9 popen) open-process) OPEN_BOTH
255       "tr" "[:lower:]" "[:upper:]")
256    (display "hello world" to) (close to)
257    (list (read-string from)
258          (status:exit-val (cdr (waitpid pid))))))
259
260(pass-if-equal "piped-process"
261    42
262  (status:exit-val
263   (cdr (waitpid ((@@ (ice-9 popen) piped-process)
264                  "./meta/guile" '("-c" "(exit 42)"))))))
265
266(pass-if-equal "piped-process: with output"
267    '("foo bar\n" 0)
268  (let* ((p (pipe))
269         (pid ((@@ (ice-9 popen) piped-process) "echo" '("foo" "bar")
270               (cons (port->fdes (car p))
271                     (port->fdes (cdr p))))))
272    (list (read-string (car p))
273          (status:exit-val (cdr (waitpid pid))))))
274
275(pass-if-equal "pipeline"
276    '("HELLO WORLD\n" (0 0))
277  (receive (from to pids)
278      (pipeline '(("echo" "hello world")
279                  ("tr" "[:lower:]" "[:upper:]")))
280    (list (read-string from)
281          (map (compose status:exit-val cdr waitpid) pids))))
282