Rework test infrastructure to keep track of tests which are disabled
[sbcl.git] / tests / run-program.impure.lisp
1 ;;;; various RUN-PROGRAM tests with side effects
2
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.
13
14 (cl:in-package :cl-user)
15
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).
21
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)))
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))))
33
34 (with-test (:name :run-program-cat-2 :skipped-on '(not :sb-thread))
35   ;; Tests that reading from a FIFO is interruptible.
36   (let* ((process (sb-ext:run-program "/bin/cat" '()
37                                       :wait nil
38                                       :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)))))
66
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)))
73
74 (defmethod stream-element-type ((stream buffer-stream))
75   '(unsigned-byte 8))
76
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)))))
88
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))
99
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 (sb-ext:run-program "/bin/cat" '() :wait t :output out :input in))
107           (buf (make-array (length data))))
108       (assert (= 13 (read-sequence buf out)))
109       (assert (= 0 (read-sequence (make-array 8) out)))
110       (assert (equalp buf data)))))
111
112 (with-test (:name :run-program-cat-4)
113   ;; Null broadcast stream as output
114   (let* ((process (sb-ext:run-program "/bin/cat" '() :wait nil
115                                       :output (make-broadcast-stream)
116                                       :input :stream))
117          (in (process-input process)))
118     (unwind-protect
119          (progn
120            (write-string "foobar" in)
121            (close in)
122            (process-wait process))
123       (process-close process))))
124
125 ;;; Test driving an external program (cat) through pipes wrapped in
126 ;;; composite streams.
127
128 (require :sb-posix)
129
130 (defun make-pipe ()
131   (multiple-value-bind (in out) (sb-posix:pipe)
132     (let ((input (sb-sys:make-fd-stream in
133                                         :input t
134                                         :external-format :ascii
135                                         :buffering :none :name "in"))
136           (output (sb-sys:make-fd-stream out
137                                          :output t
138                                          :external-format :ascii
139                                          :buffering :none :name "out")))
140       (make-two-way-stream input output))))
141
142 (defparameter *cat-in-pipe* (make-pipe))
143 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
144 (defparameter *cat-out-pipe* (make-pipe))
145 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*))
146
147 (with-test (:name :run-program-cat-5)
148   (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
149                           :wait nil)))
150     (dolist (test '("This is a test!"
151                     "This is another test!"
152                     "This is the last test...."))
153       (write-line test *cat-in*)
154       (assert (equal test (read-line *cat-out*))))
155     (process-close cat)))
156
157 ;;; The above test used to use ed, but there were buffering issues: on some platforms
158 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
159 ;;; agressive about flushing them. So, here's another test using :PTY.
160
161 (defparameter *tmpfile* "run-program-ed-test.tmp")
162
163 (with-open-file (f *tmpfile*
164                    :direction :output
165                    :if-exists :supersede)
166   (write-line "bar" f))
167
168 (defparameter *ed*
169   (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
170
171 (defparameter *ed-pipe* (make-two-way-stream (process-pty *ed*) (process-pty *ed*)))
172 (defparameter *ed-in* (make-synonym-stream '*ed-pipe*))
173 (defparameter *ed-out* (make-synonym-stream '*ed-pipe*))
174
175 (defun read-linish (stream)
176   (with-output-to-string (s)
177     (loop for c = (read-char stream)
178           while (and c (not (eq #\newline c)))
179              ;; Some eds like to send \r\n
180           do (unless (eq #\return c)
181                (write-char c s)))))
182
183 (defun assert-ed (command response)
184   (when command
185     (write-line command *ed-in*)
186     (force-output *ed-in*))
187   (when response
188     (let ((got (read-linish *ed-out*)))
189       (unless (equal response got)
190         (error "wanted '~A' from ed, got '~A'" response got))))
191   *ed*)
192
193 (unwind-protect
194      (with-test (:name :run-program-ed)
195        (assert-ed nil "4")
196        (assert-ed ".s/bar/baz/g" nil)
197        (assert-ed "w" "4")
198        (assert-ed "q" nil)
199        (process-wait *ed*)
200        (with-open-file (f *tmpfile*)
201          (assert (equal "baz" (read-line f)))))
202   (delete-file *tmpfile*))
203
204 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
205 ;; pathname designator.  Since these use the same code, it should
206 ;; suffice to test just :INPUT.
207 (let ((file))
208   (unwind-protect
209        (progn (with-open-file (f "run-program-test.tmp" :direction :output)
210                 (setf file (truename f))
211                 (write-line "Foo" f))
212                   (assert (run-program "cat" ()
213                                        :input file :output t
214                                        :search t :wait t)))
215     (when file
216       (delete-file file))))
217
218 ;;; This used to crash on Darwin and trigger recursive lock errors on
219 ;;; every platform.
220 (with-test (:name (:run-program :stress))
221   ;; Do it a hundred times in batches of 10 so that with a low limit
222   ;; of the number of processes the test can have a chance to pass.
223   (loop
224    repeat 10 do
225    (map nil
226         #'sb-ext:process-wait
227         (loop repeat 10
228               collect
229               (sb-ext:run-program "/bin/echo" '
230                                   ("It would be nice if this didn't crash.")
231                                   :wait nil :output nil)))))
232
233 (with-test (:name (:run-program :pty-stream))
234   (assert (equal "OK"
235                  (subseq
236                   (with-output-to-string (s)
237                     (assert (= 42 (process-exit-code
238                                    (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
239                                                 :pty s))))
240                     s)
241                   0
242                   2))))
243
244 ;; Check whether RUN-PROGRAM puts its child process into the foreground
245 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
246 ;;
247 ;; We can't check for the signal itself since run-program.c resets the
248 ;; forked process' signal mask to defaults. But the default is `stop'
249 ;; of which we can be notified asynchronously by providing a status hook.
250 (with-test (:name (:run-program :inherit-stdin))
251   (let (stopped)
252     (flet ((status-hook (proc)
253              (case (sb-ext:process-status proc)
254                (:stopped (setf stopped t)))))
255       (let ((proc (sb-ext:run-program "/bin/ed" nil :search nil :wait nil
256                                       :input t :output t
257                                       :status-hook #'status-hook)))
258         ;; Give the program a generous time to generate the SIGTTIN.
259         ;; If it hasn't done so after that time we can consider it
260         ;; to be working (i.e. waiting for input without generating SIGTTIN).
261         (sleep 0.5)
262         ;; either way we have to signal it to terminate
263         (process-kill proc sb-posix:sigterm)
264         (process-close proc)
265         (assert (not stopped))))))
266