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)
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))))
35 (with-test (:name :run-program-cat-2)
36 ;; Tests that reading from a FIFO is interruptible.
37 (let* ((process (sb-ext:run-program "/bin/cat" '()
39 :output :stream :input :stream))
40 (in (process-input process))
41 (out (process-output process))
42 (sem (sb-thread:make-semaphore))
44 (writer (sb-thread:make-thread (lambda ()
45 (sb-thread:wait-on-semaphore sem)
54 (sb-thread:signal-semaphore sem)
58 (setf got (read-line out))
64 (assert (eq unwind :sleep))
65 (sb-thread:join-thread writer)
66 (assert (equal "OK" (read-line out)))))
68 (defclass buffer-stream (sb-gray:fundamental-binary-input-stream sb-gray:fundamental-binary-output-stream)
69 ((buffer :initform (make-array 128
70 :element-type '(unsigned-byte 8)
75 (defmethod stream-element-type ((stream buffer-stream))
78 (defmethod sb-gray:stream-read-sequence ((stream buffer-stream) seq &optional (start 0) end)
79 (let* ((buffer (slot-value stream 'buffer))
80 (end (or end (length seq)))
81 (mark (slot-value stream 'mark))
82 (fill-pointer (fill-pointer buffer))
83 (new-mark (+ mark (min fill-pointer (- end start)))))
84 (setf (slot-value stream 'mark) new-mark)
86 :start1 start :end1 end
87 :start2 mark :end2 fill-pointer)
88 (min end (+ start (- fill-pointer mark)))))
90 (defmethod sb-gray:stream-write-sequence ((stream buffer-stream) seq &optional (start 0) end)
91 (let* ((buffer (slot-value stream 'buffer))
92 (end (or end (length seq)))
93 (fill-pointer (fill-pointer buffer))
94 (new-fill (min (array-total-size buffer) (+ fill-pointer (- end start)))))
95 (setf (fill-pointer buffer) new-fill)
98 :start2 start :end2 end)
101 (with-test (:name :run-program-cat-3)
102 ;; User-defined binary input and output streams.
103 (let ((in (make-instance 'buffer-stream))
104 (out (make-instance 'buffer-stream))
105 (data #(0 1 2 3 4 5 6 7 8 9 10 11 12)))
106 (write-sequence data in)
107 (let ((process (sb-ext:run-program "/bin/cat" '() :wait t :output out :input in))
108 (buf (make-array (length data))))
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)
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.
132 (multiple-value-bind (in out) (sb-posix:pipe)
133 (let ((input (sb-sys:make-fd-stream in
135 :external-format :ascii
136 :buffering :none :name "in"))
137 (output (sb-sys:make-fd-stream out
139 :external-format :ascii
140 :buffering :none :name "out")))
141 (make-two-way-stream input output))))
143 (defparameter *cat-in-pipe* (make-pipe))
144 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
145 (defparameter *cat-out-pipe* (make-pipe))
146 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*))
148 (with-test (:name :run-program-cat-5)
149 (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
151 (dolist (test '("This is a test!"
152 "This is another test!"
153 "This is the last test...."))
154 (write-line test *cat-in*)
155 (assert (equal test (read-line *cat-out*))))
156 (process-close cat)))
158 ;;; The above test used to use ed, but there were buffering issues: on some platforms
159 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
160 ;;; agressive about flushing them. So, here's another test using :PTY.
162 (defparameter *tmpfile* "run-program-ed-test.tmp")
164 (with-open-file (f *tmpfile*
166 :if-exists :supersede)
167 (write-line "bar" f))
170 (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
172 (defparameter *ed-pipe* (make-two-way-stream (process-pty *ed*) (process-pty *ed*)))
173 (defparameter *ed-in* (make-synonym-stream '*ed-pipe*))
174 (defparameter *ed-out* (make-synonym-stream '*ed-pipe*))
176 (defun read-linish (stream)
177 (with-output-to-string (s)
178 (loop for c = (read-char stream)
179 while (and c (not (eq #\newline c)))
180 ;; Some eds like to send \r\n
181 do (unless (eq #\return c)
184 (defun assert-ed (command response)
186 (write-line command *ed-in*)
187 (force-output *ed-in*))
189 (let ((got (read-linish *ed-out*)))
190 (unless (equal response got)
191 (error "wanted '~A' from ed, got '~A'" response got))))
195 (with-test (:name :run-program-ed)
197 (assert-ed ".s/bar/baz/g" nil)
201 (with-open-file (f *tmpfile*)
202 (assert (equal "baz" (read-line f)))))
203 (delete-file *tmpfile*))
205 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
206 ;; pathname designator. Since these use the same code, it should
207 ;; suffice to test just :INPUT.
210 (progn (with-open-file (f "run-program-test.tmp" :direction :output)
211 (setf file (truename f))
212 (write-line "Foo" f))
213 (assert (run-program "cat" ()
214 :input file :output t
217 (delete-file file))))
219 ;;; This used to crash on Darwin and trigger recursive lock errors on
221 (with-test (:name (:run-program :stress))
222 ;; Do it a hundred times in batches of 10 so that with a low limit
223 ;; of the number of processes the test can have a chance to pass.
227 #'sb-ext:process-wait
230 (sb-ext:run-program "/bin/echo" '
231 ("It would be nice if this didn't crash.")
232 :wait nil :output nil)))))
234 (with-test (:name (:run-program :pty-stream))
237 (with-output-to-string (s)
238 (assert (= 42 (process-exit-code
239 (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
245 ;; Check whether RUN-PROGRAM puts its child process into the foreground
246 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
248 ;; We can't check for the signal itself since run-program.c resets the
249 ;; forked process' signal mask to defaults. But the default is `stop'
250 ;; of which we can be notified asynchronously by providing a status hook.
251 (with-test (:name (:run-program :inherit-stdin))
253 (flet ((status-hook (proc)
254 (case (sb-ext:process-status proc)
255 (:stopped (setf stopped t)))))
256 (let ((proc (sb-ext:run-program "/bin/ed" nil :search nil :wait nil
258 :status-hook #'status-hook)))
259 ;; Give the program a generous time to generate the SIGTTIN.
260 ;; If it hasn't done so after that time we can consider it
261 ;; to be working (i.e. waiting for input without generating SIGTTIN).
263 ;; either way we have to signal it to terminate
264 (process-kill proc sb-posix:sigterm)
266 (assert (not stopped))))))