dyndebug_init: fix gencgc_verbose handling.
[sbcl.git] / tests / run-program.impure.lisp
blob90faa080f80c32508e20579e1c0e989a3333dde0
1 ;;;; various RUN-PROGRAM tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
8 ;;;; from CMU CL.
9 ;;;;
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 (run-program "cat" '() :wait nil
24 :search t :output :stream :input :stream))
25 (out (process-input process))
26 (in (process-output process)))
27 (unwind-protect
28 (loop for i from 0 to 255 do
29 (write-byte i out)
30 (force-output out)
31 (assert (= (read-byte in) i)))
32 (process-close process))))
34 (with-test (:name (run-program :cat 2)
35 :skipped-on (or (not :sb-thread) :win32))
36 ;; Tests that reading from a FIFO is interruptible.
37 (let* ((process (run-program "/bin/cat" '()
38 :wait nil :output :stream :input :stream))
39 (in (process-input process))
40 (out (process-output process))
41 (sem (sb-thread:make-semaphore))
42 (state :init)
43 (writer (sb-thread:make-thread (lambda ()
44 (sb-thread:wait-on-semaphore sem)
45 (setf state :sleep)
46 (sleep 2)
47 (setf state :write)
48 (write-line "OK" in)
49 (finish-output in))))
50 (timeout nil)
51 (got nil)
52 (unwind nil))
53 (sb-thread:signal-semaphore sem)
54 (handler-case
55 (with-timeout 0.1
56 (unwind-protect
57 (setf got (read-line out))
58 (setf unwind state)))
59 (timeout ()
60 (setf timeout t)))
61 (assert (not got))
62 (assert timeout)
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)
70 :adjustable t
71 :fill-pointer 0))
72 (mark :initform 0)))
74 (defmethod stream-element-type ((stream buffer-stream))
75 '(unsigned-byte 8))
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)
84 (replace seq buffer
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)
95 (replace buffer seq
96 :start1 fill-pointer
97 :start2 start :end2 end)
98 seq))
100 (with-test (:name (run-program :cat 3))
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 (run-program "cat" '()
107 :search t
108 :wait t
109 :output out :input in))
110 (buf (make-array (length data))))
111 (declare (ignore process))
112 (assert (= 13 (read-sequence buf out)))
113 (assert (= 0 (read-sequence (make-array 8) out)))
114 (assert (equalp buf data)))))
116 (with-test (:name (run-program :cat 4))
117 ;; Null broadcast stream as output
118 (let* ((process (run-program "cat" '() :wait nil
119 :search t
120 :output (make-broadcast-stream)
121 :input :stream))
122 (in (process-input process)))
123 (unwind-protect
124 (progn
125 (write-string "foobar" in)
126 (close in)
127 (process-wait process))
128 (process-close process))))
130 ;;; Test driving an external program (cat) through pipes wrapped in
131 ;;; composite streams.
133 (require :sb-posix)
135 #-win32
136 (progn
137 (defun make-pipe ()
138 (multiple-value-bind (in out) (sb-posix:pipe)
139 (let ((input (sb-sys:make-fd-stream in
140 :input t
141 :external-format :ascii
142 :buffering :none :name "in"))
143 (output (sb-sys:make-fd-stream out
144 :output t
145 :external-format :ascii
146 :buffering :none :name "out")))
147 (make-two-way-stream input output))))
149 (defparameter *cat-in-pipe* (make-pipe))
150 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
151 (defparameter *cat-out-pipe* (make-pipe))
152 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*)))
154 (with-test (:name (run-program :cat 5) :fails-on :win32)
155 (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
156 :wait nil)))
157 (dolist (test '("This is a test!"
158 "This is another test!"
159 "This is the last test...."))
160 (write-line test *cat-in*)
161 (assert (equal test (read-line *cat-out*))))
162 (process-close cat)))
164 ;;; The above test used to use ed, but there were buffering issues: on some platforms
165 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
166 ;;; agressive about flushing them. So, here's another test using :PTY.
168 #-win32
169 (with-test (:name :is-/bin/ed-installed?)
170 (assert (probe-file "/bin/ed")))
172 #-win32
173 (progn
174 (defparameter *tmpfile* "run-program-ed-test.tmp")
176 (with-test (:name (run-program :/bin/ed))
177 (with-open-file (f *tmpfile*
178 :direction :output
179 :if-exists :supersede)
180 (write-line "bar" f))
181 (unwind-protect
182 (let* ((ed (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
183 (ed-in (process-pty ed))
184 (ed-out (process-pty ed)))
185 (labels ((read-linish (stream)
186 (with-output-to-string (s)
187 (loop for c = (read-char stream)
188 while (and c (not (eq #\newline c)))
189 ;; Some eds like to send \r\n
190 do (unless (eq #\return c)
191 (write-char c s)))))
192 (assert-ed (command response)
193 (when command
194 (write-line command ed-in)
195 (force-output ed-in))
196 (when response
197 (let ((got (read-linish ed-out)))
198 (unless (equal response got)
199 (error "wanted '~A' from ed, got '~A'" response got))))
200 ed))
201 (assert-ed nil "4")
202 (assert-ed ".s/bar/baz/g" nil)
203 (assert-ed "w" "4")
204 (assert-ed "q" nil)
205 (process-wait ed)
206 (with-open-file (f *tmpfile*)
207 (assert (equal "baz" (read-line f))))))
208 (delete-file *tmpfile*)))) ;; #-win32
210 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
211 ;; pathname designator. Since these use the same code, it should
212 ;; suffice to test just :INPUT.
213 (with-test (:name (run-program :input :output pathname))
214 (let ((file))
215 (unwind-protect
216 (progn (with-open-file (f "run-program-test.tmp" :direction :output)
217 (setf file (truename f))
218 (write-line "Foo" f))
219 (assert (run-program "cat" ()
220 :input file :output t
221 :search t :wait t)))
222 (when file
223 (delete-file file)))))
225 ;;; This used to crash on Darwin and trigger recursive lock errors on
226 ;;; every platform.
227 (with-test (:name (run-program :stress))
228 ;; Do it a hundred times in batches of 10 so that with a low limit
229 ;; of the number of processes the test can have a chance to pass.
230 (loop
231 repeat 10 do
232 (map nil #'process-wait
233 (loop repeat 10
234 collect
235 (run-program "echo"
236 '("It would be nice if this didn't crash.")
237 :search t :wait nil :output nil)))))
239 (with-test (:name (run-program :pty-stream) :fails-on :win32)
240 (assert (equal "OK"
241 (handler-case
242 (with-timeout 1
243 (subseq
244 (with-output-to-string (s)
245 (assert (= 42 (process-exit-code
246 (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
247 :pty s))))
251 (timeout () "timeout")))))
253 ;; Check whether RUN-PROGRAM puts its child process into the foreground
254 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
256 ;; We can't check for the signal itself since run-program.c resets the
257 ;; forked process' signal mask to defaults. But the default is `stop'
258 ;; of which we can be notified asynchronously by providing a status hook.
259 (with-test (:name (run-program :inherit-stdin) :fails-on :win32)
260 (let (stopped)
261 (flet ((status-hook (proc)
262 (case (process-status proc)
263 (:stopped (setf stopped t)))))
264 (let ((proc (run-program "/bin/ed" nil :search nil :wait nil
265 :input t :output t
266 :status-hook #'status-hook)))
267 ;; Give the program a generous time to generate the SIGTTIN.
268 ;; If it hasn't done so after that time we can consider it
269 ;; to be working (i.e. waiting for input without generating SIGTTIN).
270 (sleep 0.5)
271 ;; either way we have to signal it to terminate
272 (process-kill proc sb-posix:sigterm)
273 (process-close proc)
274 (assert (not stopped))))))
277 ;; Check that in when you do run-program with :wait t that causes
278 ;; encoding error, it does not affect the following run-program
279 (with-test (:name (run-program :clean-exit-after-encoding-error)
280 :fails-on :win32)
281 (let ((had-error-p nil))
282 (flet ((barf (&optional (format :default))
283 (with-output-to-string (stream)
284 (run-program #-netbsd "/usr/bin/perl" #+netbsd "/usr/pkg/bin/perl"
285 '("-e" "print \"\\x20\\xfe\\xff\\x0a\"")
286 :output stream
287 :external-format format)))
288 (no-barf ()
289 (with-output-to-string (stream)
290 (run-program "/bin/echo"
291 '("This is a test")
292 :output stream))))
293 (handler-case
294 (barf :utf-8)
295 (error ()
296 (setq had-error-p t)))
297 (assert had-error-p)
298 ;; now run the harmless program
299 (setq had-error-p nil)
300 (handler-case
301 (no-barf)
302 (error ()
303 (setq had-error-p t)))
304 (assert (not had-error-p)))))
306 (with-test (:name (run-program :no-such-thing))
307 (assert (search "Couldn't execute"
308 (handler-case
309 (progn (run-program "no-such-program-we-hope" '()) nil)
310 (error (e)
311 (princ-to-string e))))))
313 (with-test (:name (run-program :not-executable))
314 (assert (search "Couldn't execute"
315 (handler-case
316 (progn (run-program "run-program.impure.lisp" '()) nil)
317 (error (e)
318 (princ-to-string e))))))
320 #-win32
321 (with-test (:name (run-program :if-input-does-not-exist))
322 (let ((file (pathname (sb-posix:mktemp "rpXXXXXX"))))
323 (assert (null (run-program "/bin/cat" '() :input file)))
324 (assert (null (run-program "/bin/cat" '() :output #.(or *compile-file-truename*
325 *load-truename*)
326 :if-output-exists nil)))))
329 (with-test (:name (run-program :set-directory))
330 (let* ((directory #-win32 "/"
331 #+win32 "c:\\")
332 (out (process-output
333 (run-program #-win32 "/bin/sh"
334 #-win32 '("-c" "pwd")
335 #+win32 "cmd.exe"
336 #+win32 '("/c" "cd")
337 :output :stream
338 :directory directory
339 :search t))))
340 (assert
341 (equal directory
342 (string-right-trim '(#\Return) (read-line out))))))
344 (with-test (:name (run-program :directory-nil))
345 (run-program #-win32 "/bin/sh"
346 #-win32 '("-c" "pwd")
347 #+win32 "cmd.exe"
348 #+win32 '("/c" "cd")
349 :directory nil
350 :search t))
352 (with-test (:name (run-program :bad-options))
353 (assert-error
354 (run-program #-win32 "/bin/sh"
355 #-win32 '("-c" "pwd")
356 #+win32 "cmd.exe"
357 #+win32 '("/c" "cd")
358 :search t
359 :output :bad)))
361 (with-test (:name (run-program :stop+continue) :skipped-on :win32)
362 (let ((process (run-program "cat" '() :search t :input :stream :wait nil)))
363 (flet ((kill-and-check-status (signal expected-status)
364 (process-kill process signal)
365 (loop :repeat 2000
366 :when (eq (process-status process) expected-status)
367 :do (return)
368 :do (sleep 1/100)
369 :finally (error "~@<Process ~A did not change its ~
370 status to ~S within 20 seconds.~@:>"
371 process expected-status))))
372 (kill-and-check-status sb-posix:sigstop :stopped)
373 (kill-and-check-status sb-posix:sigcont :running)
374 (kill-and-check-status sb-posix:sigkill :signaled)
375 (process-wait process)
376 (assert (not (process-alive-p process))))))
378 #-win32
379 (with-test (:name (run-program :stop+continue :posix-kill))
380 (let ((process (run-program "cat" '() :search t :input :stream :wait nil)))
381 (flet ((kill-and-check-status (signal expected-status)
382 (sb-posix:kill (process-pid process) signal)
383 (loop :repeat 2000
384 :when (eq (process-status process) expected-status)
385 :do (return)
386 :do (sleep 1/100)
387 :finally (error "~@<Process ~A did not change its ~
388 status to ~S within 20 seconds.~@:>"
389 process expected-status))))
390 (kill-and-check-status sb-posix:sigstop :stopped)
391 (kill-and-check-status sb-posix:sigcont :running)
392 (kill-and-check-status sb-posix:sigkill :signaled)
393 (process-wait process)
394 (assert (not (process-alive-p process))))))