1.0.42.29: tweak run-program.impure.lisp
[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 ;;; Test driving an external program (cat) through pipes wrapped in
35 ;;; composite streams.
36
37 (require :sb-posix)
38
39 (defun make-pipe ()
40   (multiple-value-bind (in out) (sb-posix:pipe)
41     (let ((input (sb-sys:make-fd-stream in
42                                         :input t
43                                         :external-format :ascii
44                                         :buffering :none :name "in"))
45           (output (sb-sys:make-fd-stream out
46                                          :output t
47                                          :external-format :ascii
48                                          :buffering :none :name "out")))
49       (make-two-way-stream input output))))
50
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*))
55
56 (with-test (:name :run-program-cat-2)
57   (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
58                           :wait nil)))
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*))))
64     (process-close cat)))
65
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.
69
70 (defparameter *tmpfile* "run-program-ed-test.tmp")
71
72 (with-open-file (f *tmpfile*
73                    :direction :output
74                    :if-exists :supersede)
75   (write-line "bar" f))
76
77 (defparameter *ed*
78   (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
79
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*))
83
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)
90                (write-char c s)))))
91
92 (defun assert-ed (command response)
93   (when command
94     (write-line command *ed-in*)
95     (force-output *ed-in*))
96   (when response
97     (let ((got (read-linish *ed-out*)))
98       (unless (equal response got)
99         (error "wanted '~A' from ed, got '~A'" response got))))
100   *ed*)
101
102 (unwind-protect
103      (with-test (:name :run-program-ed)
104        (assert-ed nil "4")
105        (assert-ed ".s/bar/baz/g" nil)
106        (assert-ed "w" "4")
107        (assert-ed "q" nil)
108        (process-wait *ed*)
109        (with-open-file (f *tmpfile*)
110          (assert (equal "baz" (read-line f)))))
111   (delete-file *tmpfile*))
112
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.
116 (let ((file))
117   (unwind-protect
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
123                                        :search t :wait t)))
124     (when file
125       (delete-file file))))
126
127 ;;; This used to crash on Darwin and trigger recursive lock errors on
128 ;;; every platform.
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.
132   (loop
133    repeat 10 do
134    (map nil
135         #'sb-ext:process-wait
136         (loop repeat 10
137               collect
138               (sb-ext:run-program "/bin/echo" '
139                                   ("It would be nice if this didn't crash.")
140                                   :wait nil :output nil)))))
141
142 (with-test (:name (:run-program :pty-stream))
143   (assert (equal "OK"
144                  (subseq
145                   (with-output-to-string (s)
146                     (assert (= 42 (process-exit-code
147                                    (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
148                                                 :pty s))))
149                     s)
150                   0
151                   2))))
152
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.
155 ;;
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))
160   (let (stopped)
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
165                                       :input t :output t
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).
170         (sleep 0.5)
171         ;; either way we have to signal it to terminate
172         (process-kill proc sb-posix:sigterm)
173         (process-close proc)
174         (assert (not stopped))))))
175