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))))
34 ;;; Test driving an external program (cat) through pipes wrapped in
35 ;;; composite streams.
40 (multiple-value-bind (in out) (sb-posix:pipe)
41 (let ((input (sb-sys:make-fd-stream in
43 :external-format :ascii
44 :buffering :none :name "in"))
45 (output (sb-sys:make-fd-stream out
47 :external-format :ascii
48 :buffering :none :name "out")))
49 (make-two-way-stream input output))))
51 (defparameter *cat-in-pipe* (make-pipe))
52 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
53 (defparameter *cat-out-pipe* (make-pipe))
54 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*))
56 (with-test (:name :run-program-cat-2)
57 (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
59 (dolist (test '("This is a test!"
60 "This is another test!"
61 "This is the last test...."))
62 (write-line test *cat-in*)
63 (assert (equal test (read-line *cat-out*))))
66 ;;; The above test used to use ed, but there were buffering issues: on some platforms
67 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
68 ;;; agressive about flushing them. So, here's another test using :PTY.
70 (defparameter *tmpfile* "run-program-ed-test.tmp")
72 (with-open-file (f *tmpfile*
74 :if-exists :supersede)
78 (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
80 (defparameter *ed-pipe* (make-two-way-stream (process-pty *ed*) (process-pty *ed*)))
81 (defparameter *ed-in* (make-synonym-stream '*ed-pipe*))
82 (defparameter *ed-out* (make-synonym-stream '*ed-pipe*))
84 (defun read-linish (stream)
85 (with-output-to-string (s)
86 (loop for c = (read-char stream)
87 while (and c (not (eq #\newline c)))
88 ;; Some eds like to send \r\n
89 do (unless (eq #\return c)
92 (defun assert-ed (command response)
94 (write-line command *ed-in*)
95 (force-output *ed-in*))
97 (let ((got (read-linish *ed-out*)))
98 (unless (equal response got)
99 (error "wanted '~A' from ed, got '~A'" response got))))
103 (with-test (:name :run-program-ed)
105 (assert-ed ".s/bar/baz/g" nil)
109 (with-open-file (f *tmpfile*)
110 (assert (equal "baz" (read-line f)))))
111 (delete-file *tmpfile*))
113 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
114 ;; pathname designator. Since these use the same code, it should
115 ;; suffice to test just :INPUT.
118 (progn (with-open-file (f "run-program-test.tmp" :direction :output)
119 (setf file (truename f))
120 (write-line "Foo" f))
121 (assert (run-program "cat" ()
122 :input file :output t
125 (delete-file file))))
127 ;;; This used to crash on Darwin and trigger recursive lock errors on
129 (with-test (:name (:run-program :stress))
130 ;; Do it a hundred times in batches of 10 so that with a low limit
131 ;; of the number of processes the test can have a chance to pass.
135 #'sb-ext:process-wait
138 (sb-ext:run-program "/bin/echo" '
139 ("It would be nice if this didn't crash.")
140 :wait nil :output nil)))))
142 (with-test (:name (:run-program :pty-stream))
145 (with-output-to-string (s)
146 (assert (= 42 (process-exit-code
147 (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
153 ;; Check whether RUN-PROGRAM puts its child process into the foreground
154 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
156 ;; We can't check for the signal itself since run-program.c resets the
157 ;; forked process' signal mask to defaults. But the default is `stop'
158 ;; of which we can be notified asynchronously by providing a status hook.
159 (with-test (:name (:run-program :inherit-stdin))
161 (flet ((status-hook (proc)
162 (case (sb-ext:process-status proc)
163 (:stopped (setf stopped t)))))
164 (let ((proc (sb-ext:run-program "/bin/ed" nil :search nil :wait nil
166 :status-hook #'status-hook)))
167 ;; Give the program a generous time to generate the SIGTTIN.
168 ;; If it hasn't done so after that time we can consider it
169 ;; to be working (i.e. waiting for input without generating SIGTTIN).
171 ;; either way we have to signal it to terminate
172 (process-kill proc sb-posix:sigterm)
174 (assert (not stopped))))))