1.0.37.74: support NetBSD-current
[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)) (not (eq #\return c)))
88        do (write-char c s))))
89
90 (defun assert-ed (command response)
91   (when command
92     (write-line command *ed-in*)
93     (force-output *ed-in*))
94   (let ((got (read-linish *ed-out*)))
95     (unless (equal response got)
96       (error "wanted ~S from ed, got ~S" response got)))
97   *ed*)
98
99 (unwind-protect
100      (with-test (:name :run-program-ed)
101        (assert-ed nil "4")
102        (assert-ed ".s/bar/baz/g" "")
103        (assert-ed "w" "4")
104        (assert-ed "q" "")
105        (process-wait *ed*)
106        (with-open-file (f *tmpfile*)
107          (assert (equal "baz" (read-line f)))))
108   (delete-file *tmpfile*))
109
110 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
111 ;; pathname designator.  Since these use the same code, it should
112 ;; suffice to test just :INPUT.
113 (let ((file))
114   (unwind-protect
115        (progn (with-open-file (f "run-program-test.tmp" :direction :output)
116                 (setf file (truename f))
117                 (write-line "Foo" f))
118                   (assert (run-program "cat" ()
119                                        :input file :output t
120                                        :search t :wait t)))
121     (when file
122       (delete-file file))))
123
124 ;;; This used to crash on Darwin and trigger recursive lock errors on
125 ;;; every platform.
126 (with-test (:name (:run-program :stress))
127   ;; Do it a hundred times in batches of 10 so that with a low limit
128   ;; of the number of processes the test can have a chance to pass.
129   (loop
130    repeat 10 do
131    (map nil
132         #'sb-ext:process-wait
133         (loop repeat 10
134               collect
135               (sb-ext:run-program "/bin/echo" '
136                                   ("It would be nice if this didn't crash.")
137                                   :wait nil :output nil)))))
138
139 (with-test (:name (:run-program :pty-stream))
140   (assert (equal "OK"
141                  (subseq
142                   (with-output-to-string (s)
143                     (assert (= 42 (process-exit-code
144                                    (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
145                                                 :pty s))))
146                     s)
147                   0
148                   2))))
149
150 ;; Check whether RUN-PROGRAM puts its child process into the foreground
151 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
152 ;;
153 ;; We can't check for the signal itself since run-program.c resets the
154 ;; forked process' signal mask to defaults. But the default is `stop'
155 ;; of which we can be notified asynchronously by providing a status hook.
156 (with-test (:name (:run-program :inherit-stdin)
157             :fails-on :sbcl)
158   (error "Hangs at least on threaded Darwin and threaded x86-64/Linux.")
159   (let (stopped)
160     (flet ((status-hook (proc)
161              (case (sb-ext:process-status proc)
162                (:stopped (setf stopped t)))))
163       (let ((proc (sb-ext:run-program "/bin/ed" nil :search nil :wait nil
164                                       :input t :output t
165                                       :status-hook #'status-hook)))
166         ;; Give the program a generous time to generate the SIGTTIN.
167         ;; If it hasn't done so after that time we can consider it
168         ;; to be working (i.e. waiting for input without generating SIGTTIN).
169         (sleep 0.5)
170         ;; either way we have to signal it to terminate
171         (process-kill proc sb-posix:sigterm)
172         (process-close proc)
173         (assert (not stopped))))))
174