1 ;;;; various RUN-PROGRAM tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (cl:in-package
:cl-user
)
16 ;; In addition to definitions lower down the impurity we're avoiding
17 ;; is the sigchld handler that RUN-PROGRAM sets up, which interfers
18 ;; with the manual unix process control done by the test framework
19 ;; (sometimes the handler will manage to WAIT3 a process before
20 ;; run-tests WAITPIDs it).
22 (with-test (:name
:run-program-cat-1
:skipped-on
:win32
)
23 (let* ((process (sb-ext:run-program
"/bin/cat" '() :wait nil
24 :output
:stream
:input
:stream
))
25 (out (process-input process
))
26 (in (process-output process
)))
28 (loop for i from
0 to
255 do
31 (assert (= (read-byte in
) i
)))
32 (process-close process
))))
34 (with-test (:name
:run-program-cat-2
:skipped-on
'(or (not :sb-thread
) :win32
))
35 ;; Tests that reading from a FIFO is interruptible.
36 (let* ((process (sb-ext:run-program
"/bin/cat" '()
38 :output
:stream
:input
:stream
))
39 (in (process-input process
))
40 (out (process-output process
))
41 (sem (sb-thread:make-semaphore
))
43 (writer (sb-thread:make-thread
(lambda ()
44 (sb-thread:wait-on-semaphore sem
)
53 (sb-thread:signal-semaphore sem
)
57 (setf got
(read-line out
))
63 (assert (eq unwind
:sleep
))
64 (sb-thread:join-thread writer
)
65 (assert (equal "OK" (read-line out
)))))
67 (defclass buffer-stream
(sb-gray:fundamental-binary-input-stream sb-gray
:fundamental-binary-output-stream
)
68 ((buffer :initform
(make-array 128
69 :element-type
'(unsigned-byte 8)
74 (defmethod stream-element-type ((stream buffer-stream
))
77 (defmethod sb-gray:stream-read-sequence
((stream buffer-stream
) seq
&optional
(start 0) end
)
78 (let* ((buffer (slot-value stream
'buffer
))
79 (end (or end
(length seq
)))
80 (mark (slot-value stream
'mark
))
81 (fill-pointer (fill-pointer buffer
))
82 (new-mark (+ mark
(min fill-pointer
(- end start
)))))
83 (setf (slot-value stream
'mark
) new-mark
)
85 :start1 start
:end1 end
86 :start2 mark
:end2 fill-pointer
)
87 (min end
(+ start
(- fill-pointer mark
)))))
89 (defmethod sb-gray:stream-write-sequence
((stream buffer-stream
) seq
&optional
(start 0) end
)
90 (let* ((buffer (slot-value stream
'buffer
))
91 (end (or end
(length seq
)))
92 (fill-pointer (fill-pointer buffer
))
93 (new-fill (min (array-total-size buffer
) (+ fill-pointer
(- end start
)))))
94 (setf (fill-pointer buffer
) new-fill
)
97 :start2 start
:end2 end
)
100 (with-test (:name
:run-program-cat-3
:skipped-on
:win32
)
101 ;; User-defined binary input and output streams.
102 (let ((in (make-instance 'buffer-stream
))
103 (out (make-instance 'buffer-stream
))
104 (data #(0 1 2 3 4 5 6 7 8 9 10 11 12)))
105 (write-sequence data in
)
106 (let ((process (sb-ext:run-program
"/bin/cat" '() :wait t
:output out
:input in
))
107 (buf (make-array (length data
))))
108 (declare (ignore process
))
109 (assert (= 13 (read-sequence buf out
)))
110 (assert (= 0 (read-sequence (make-array 8) out
)))
111 (assert (equalp buf data
)))))
113 (with-test (:name
:run-program-cat-4
:skipped-on
:win32
)
114 ;; Null broadcast stream as output
115 (let* ((process (sb-ext:run-program
"/bin/cat" '() :wait nil
116 :output
(make-broadcast-stream)
118 (in (process-input process
)))
121 (write-string "foobar" in
)
123 (process-wait process
))
124 (process-close process
))))
126 ;;; Test driving an external program (cat) through pipes wrapped in
127 ;;; composite streams.
134 (multiple-value-bind (in out
) (sb-posix:pipe
)
135 (let ((input (sb-sys:make-fd-stream in
137 :external-format
:ascii
138 :buffering
:none
:name
"in"))
139 (output (sb-sys:make-fd-stream out
141 :external-format
:ascii
142 :buffering
:none
:name
"out")))
143 (make-two-way-stream input output
))))
145 (defparameter *cat-in-pipe
* (make-pipe))
146 (defparameter *cat-in
* (make-synonym-stream '*cat-in-pipe
*))
147 (defparameter *cat-out-pipe
* (make-pipe))
148 (defparameter *cat-out
* (make-synonym-stream '*cat-out-pipe
*)))
150 (with-test (:name
:run-program-cat-5
:fails-on
:win32
)
151 (let ((cat (run-program "/bin/cat" nil
:input
*cat-in
* :output
*cat-out
*
153 (dolist (test '("This is a test!"
154 "This is another test!"
155 "This is the last test...."))
156 (write-line test
*cat-in
*)
157 (assert (equal test
(read-line *cat-out
*))))
158 (process-close cat
)))
160 ;;; The above test used to use ed, but there were buffering issues: on some platforms
161 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
162 ;;; agressive about flushing them. So, here's another test using :PTY.
165 (with-test (:name
:is-
/bin
/ed-installed?
)
166 (assert (probe-file "/bin/ed")))
170 (defparameter *tmpfile
* "run-program-ed-test.tmp")
172 (with-test (:name
:run-program-
/bin
/ed
)
173 (with-open-file (f *tmpfile
*
175 :if-exists
:supersede
)
176 (write-line "bar" f
))
178 (let* ((ed (run-program "/bin/ed" (list *tmpfile
*) :wait nil
:pty t
))
179 (ed-in (process-pty ed
))
180 (ed-out (process-pty ed
)))
181 (labels ((read-linish (stream)
182 (with-output-to-string (s)
183 (loop for c
= (read-char stream
)
184 while
(and c
(not (eq #\newline c
)))
185 ;; Some eds like to send \r\n
186 do
(unless (eq #\return c
)
188 (assert-ed (command response
)
190 (write-line command ed-in
)
191 (force-output ed-in
))
193 (let ((got (read-linish ed-out
)))
194 (unless (equal response got
)
195 (error "wanted '~A' from ed, got '~A'" response got
))))
198 (assert-ed ".s/bar/baz/g" nil
)
202 (with-open-file (f *tmpfile
*)
203 (assert (equal "baz" (read-line f
))))))
204 (delete-file *tmpfile
*)))) ;; #-win32
206 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
207 ;; pathname designator. Since these use the same code, it should
208 ;; suffice to test just :INPUT.
211 (progn (with-open-file (f "run-program-test.tmp" :direction
:output
)
212 (setf file
(truename f
))
213 (write-line "Foo" f
))
214 (assert (run-program "cat" ()
215 :input file
:output t
218 (delete-file file
))))
220 ;;; This used to crash on Darwin and trigger recursive lock errors on
222 (with-test (:name
(:run-program
:stress
) :fails-on
:win32
)
223 ;; Do it a hundred times in batches of 10 so that with a low limit
224 ;; of the number of processes the test can have a chance to pass.
228 #'sb-ext
:process-wait
231 (sb-ext:run-program
"/bin/echo" '
232 ("It would be nice if this didn't crash.")
233 :wait nil
:output nil
)))))
235 (with-test (:name
(:run-program
:pty-stream
) :fails-on
:win32
)
240 (with-output-to-string (s)
241 (assert (= 42 (process-exit-code
242 (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
247 (timeout () "timeout")))))
249 ;; Check whether RUN-PROGRAM puts its child process into the foreground
250 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
252 ;; We can't check for the signal itself since run-program.c resets the
253 ;; forked process' signal mask to defaults. But the default is `stop'
254 ;; of which we can be notified asynchronously by providing a status hook.
255 (with-test (:name
(:run-program
:inherit-stdin
) :fails-on
:win32
)
257 (flet ((status-hook (proc)
258 (case (sb-ext:process-status proc
)
259 (:stopped
(setf stopped t
)))))
260 (let ((proc (sb-ext:run-program
"/bin/ed" nil
:search nil
:wait nil
262 :status-hook
#'status-hook
)))
263 ;; Give the program a generous time to generate the SIGTTIN.
264 ;; If it hasn't done so after that time we can consider it
265 ;; to be working (i.e. waiting for input without generating SIGTTIN).
267 ;; either way we have to signal it to terminate
268 (process-kill proc sb-posix
:sigterm
)
270 (assert (not stopped
))))))
273 ;; Check that in when you do run-program with :wait t that causes
274 ;; encoding error, it does not affect the following run-program
275 (with-test (:name
(:run-program
:clean-exit-after-encoding-error
)
277 (let ((had-error-p nil
))
278 (flet ((barf (&optional
(format :default
))
279 (with-output-to-string (stream)
280 (run-program #-netbsd
"/usr/bin/perl" #+netbsd
"/usr/pkg/bin/perl"
281 '("-e" "print \"\\x20\\xfe\\xff\\x0a\"")
283 :external-format format
)))
285 (with-output-to-string (stream)
286 (run-program "/bin/echo"
292 (setq had-error-p t
)))
294 ;; now run the harmless program
295 (setq had-error-p nil
)
299 (setq had-error-p t
)))
300 (assert (not had-error-p
)))))
302 (with-test (:name
(:run-program
:no-such-thing
))
303 (assert (search "Couldn't execute"
305 (progn (run-program "no-such-program-we-hope" '()) nil
)
307 (princ-to-string e
))))))
309 (with-test (:name
(:run-program
:not-executable
))
310 (assert (search "Couldn't execute"
312 (progn (run-program "run-program.impure.lisp" '()) nil
)
314 (princ-to-string e
))))))
317 (with-test (:name
(:run-program
:if-input-does-not-exist
))
318 (let ((file (pathname (sb-posix:mktemp
"rpXXXXXX"))))
319 (assert (null (sb-ext:run-program
"/bin/cat" '() :input file
)))
320 (assert (null (sb-ext:run-program
"/bin/cat" '() :output
#.
(or *compile-file-truename
*
322 :if-output-exists nil
)))))
325 (with-test (:name
(:run-program
:set-directory
))
326 (let* ((directory #-win32
"/"
328 (out (sb-ext:process-output
329 (sb-ext:run-program
#-win32
"/bin/sh"
330 #-win32
'("-c" "pwd")
338 (string-right-trim '(#\Return
) (read-line out
))))))
340 (with-test (:name
(:run-program
:directory-nil
))
341 (sb-ext:run-program
#-win32
"/bin/sh"
342 #-win32
'("-c" "pwd")