summaryrefslogtreecommitdiff
path: root/test-suite/tests/popen.test
blob: 1dd2bc78e62faeda42de9aa866ef49a345dc70fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;;;; popen.test --- exercise ice-9/popen.scm      -*- scheme -*-
;;;;
;;;; Copyright 2003, 2006 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2.1 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-ice-9-popen)
  #:use-module (test-suite lib)
  #:use-module (ice-9 popen))


;; read from PORT until eof is reached, return what's read as a string
(define (read-string-to-eof port)
  (do ((lst '() (cons c lst))
       (c (read-char port) (read-char port)))
      ((eof-object? c)
       (list->string (reverse! lst)))))

;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
;; generated rather than a SIGPIPE signal
(define (with-epipe thunk)
  (dynamic-wind
      (lambda ()
	(sigaction SIGPIPE SIG_IGN))
      thunk
      restore-signals))


;;
;; open-input-pipe
;;

(with-test-prefix "open-input-pipe"
  
  (pass-if-exception "no args" exception:wrong-num-args
    (open-input-pipe))
  
  (pass-if "port?"
    (port? (open-input-pipe "echo hello")))
  
  (pass-if "echo hello"
    (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
  
  ;; exercise file descriptor setups when stdin is the same as stderr  
  (pass-if "stdin==stderr"
    (let ((port (open-file "/dev/null" "r+")))
      (with-input-from-port port
	(lambda ()
	  (with-error-to-port port
	    (lambda ()
	      (open-input-pipe "echo hello"))))))
    #t)
  
  ;; exercise file descriptor setups when stdout is the same as stderr  
  (pass-if "stdout==stderr"
    (let ((port (open-file "/dev/null" "r+")))
      (with-output-to-port port
	(lambda ()
	  (with-error-to-port port
	    (lambda ()
	      (open-input-pipe "echo hello"))))))
    #t)
  
  ;; After the child closes stdout (which it indicates here by writing
  ;; "closed" to stderr), the parent should see eof.  In Guile 1.6.4 and
  ;; earlier a duplicate of stdout existed in the child, meaning eof was not
  ;; seen.
  (pass-if "no duplicate"
    (let* ((pair (pipe))
	   (port (with-error-to-port (cdr pair)
		   (lambda ()
		     (open-input-pipe
		      "exec 1>/dev/null; echo closed 1>&2; exec 2>/dev/null; sleep 999")))))
      (close-port (cdr pair))   ;; write side
      (and (char? (read-char (car pair))) ;; wait for child to do its thing
	   (char-ready? port)
	   (eof-object? (read-char port))))))

;;
;; open-output-pipe
;;

(with-test-prefix "open-output-pipe"
  
  (pass-if-exception "no args" exception:wrong-num-args
    (open-output-pipe))
  
  (pass-if "port?"
    (port? (open-output-pipe "exit 0")))
  
  ;; exercise file descriptor setups when stdin is the same as stderr  
  (pass-if "stdin==stderr"
    (let ((port (open-file "/dev/null" "r+")))
      (with-input-from-port port
	(lambda ()
	  (with-error-to-port port
	    (lambda ()
	      (open-output-pipe "exit 0"))))))
    #t)
  
  ;; exercise file descriptor setups when stdout is the same as stderr
  (pass-if "stdout==stderr"
    (let ((port (open-file "/dev/null" "r+")))
      (with-output-to-port port
	(lambda ()
	  (with-error-to-port port
	    (lambda ()
	      (open-output-pipe "exit 0"))))))
    #t)
  
  ;; After the child closes stdin (which it indicates here by writing
  ;; "closed" to stderr), the parent should see a broken pipe.  We setup to
  ;; see this as EPIPE (rather than SIGPIPE).  In Guile 1.6.4 and earlier a
  ;; duplicate of stdin existed in the child, preventing the broken pipe
  ;; occurring.
  (pass-if "no duplicate"
    (with-epipe
     (lambda ()
       (let* ((pair (pipe))
	      (port (with-error-to-port (cdr pair)
		      (lambda ()
			(open-output-pipe
			 "exec 0</dev/null; echo closed 1>&2; exec 2>/dev/null; sleep 999")))))
	 (close-port (cdr pair))   ;; write side
	 (and (char? (read-char (car pair))) ;; wait for child to do its thing
	      (catch 'system-error
		(lambda ()
		  (write-char #\x port)
		  (force-output port)
		  #f)
		(lambda (key name fmt args errno-list)
		  (= (car errno-list) EPIPE)))))))))

;;
;; close-pipe
;;

(with-test-prefix "close-pipe"
  
  (pass-if-exception "no args" exception:wrong-num-args
    (close-pipe))
  
  (pass-if "exit 0"
    (let ((st (close-pipe (open-output-pipe "exit 0"))))
      (and (status:exit-val st)
	   (= 0 (status:exit-val st)))))
  
  (pass-if "exit 1"
    (let ((st (close-pipe (open-output-pipe "exit 1"))))
      (and (status:exit-val st)
	   (= 1 (status:exit-val st))))))