Debian Codegraph @

test/system.scm

raw
1 ;;
2 ;; test for system related procedures
3 ;;
4
5 #!no-fold-case
6
7 (use gauche.test)
8 (use gauche.config)
9 (use scheme.list)
10 (use srfi.13)
11
12 (test-start "system")
13
14 ;;-------------------------------------------------------------------
15 (test-section "system")
16 ;; test this first, so that we can use system commands to verify our results.
17
18 (test* "system" #t (begin (sys-system ":") #t))
19 (test* "system" #t (begin (sys-system "") #t))
20
21 ;; shorthand of normalizing pathname. this doesn't do anything on
22 ;; unix, but on Windows the separator in PATHNAME is replaced.
23 (define (n pathname) (sys-normalize-pathname pathname))
24
25 ;; some common operations via command
26 ;; NB: We do not use sys-* interface yet, for we're going to test them!
27 (define (cmd-rmrf dir)
28 (cond-expand
29 (gauche.os.windows
30 (sys-system #"rmdir /q /s ~(n dir) > NUL 2>&1")
31 (sys-system #"del /q ~(n dir) > NUL 2>&1"))
32 (else
33 (sys-system #"rm -rf ~dir > /dev/null"))))
34
35 (define (cmd-mkdir dir)
36 (cond-expand
37 (gauche.os.windows (sys-system #"mkdir ~(n dir)"))
38 (else (sys-system #"mkdir ~dir"))))
39
40 (define (cmd-touch path)
41 (cond-expand
42 (gauche.os.windows (sys-system #"echo \"\" > ~(n path)"))
43 (else (sys-system #"touch ~path"))))
44
45 (define (get-command-output command)
46 (cmd-rmrf "test.out")
47 (sys-system (format #f "~a > test.out" command))
48 (call-with-input-file "test.out"
49 (lambda (in)
50 (let loop ((line (read-line in)) (lines '()))
51 (if (eof-object? line)
52 (begin (close-input-port in)
53 (cmd-rmrf "test.out")
54 (string-join (reverse lines) " "))
55 (loop (read-line in) (cons line lines)))))))
56
57 (define (get-lsmode file)
58 (string-take (get-command-output (format #f "ls -ld ~a" file)) 10))
59
60 (define (get-pwd-via-pwd)
61 ;; use pwd command to get pwd. avoid using shell's built-in pwd,
62 ;; for it may be confused by symlinks.
63 (cond-expand
64 (gauche.os.windows (get-command-output "cd"))
65 (else
66 (cond
67 [(sys-access "/bin/pwd" X_OK)
68 ;; On MacOSX, /bin/pwd returns _logical_ pathname by default,
69 ;; which is IMHO a bad decision (the behavior is /bin/pwd -L in
70 ;; other BSDs). Anyway we have to cope with it.
71 (if (string-contains (gauche-config "--arch") "darwin")
72 (get-command-output "/bin/pwd -P")
73 (get-command-output "/bin/pwd"))]
74 [(sys-access "/usr/bin/pwd" X_OK) (get-command-output "/usr/bin/pwd")]
75 [(sys-access "/sbin/pwd" X_OK) (get-command-output "/sbin/pwd")]
76 [else (get-command-output "pwd")]))))
77
78 ;;-------------------------------------------------------------------
79 (test-section "environment")
80
81 (test* "getenv"
82 (string-trim-both
83 (get-command-output (cond-expand
84 (gauche.os.windows "echo %PATH%")
85 (else "echo $PATH"))))
86 (sys-getenv "PATH"))
87
88 (test* "getcwd" (get-pwd-via-pwd)
89 (sys-getcwd))
90
91 ;; putenv
92
93 (cond-expand
94 [gauche.sys.setenv
95 (test* "sys-putenv" "foo"
96 (begin
97 (sys-putenv "ZZGGGBBB=foo")
98 (sys-getenv "ZZGGGBBB")))
99 (test* "sys-putenv" "foo"
100 (begin
101 (sys-putenv "ZZGGGBBB" "foo") ;;old API
102 (sys-getenv "ZZGGGBBB")))]
103 [else])
104
105 ;; setenv
106
107 (cond-expand
108 [gauche.sys.setenv
109 (test* "sys-setenv" "foo"
110 (begin
111 (sys-setenv "ZZGGGBBB" "foo" #t)
112 (sys-getenv "ZZGGGBBB")))
113 (test* "sys-setenv" "foo"
114 (begin
115 (sys-setenv "ZZGGGBBB" "bar" #f)
116 (sys-getenv "ZZGGGBBB")))]
117 [else])
118
119 ;; unsetenv
120
121 (cond-expand
122 [gauche.sys.unsetenv
123 (test* "sys-unsetenv" #f
124 (begin
125 (sys-setenv "ZZGGGBBB" "foo" #t)
126 (sys-unsetenv "ZZGGGBBB")
127 (sys-getenv "ZZGGGBBB")))]
128 [else])
129
130 ;; environ
131 (test* "sys-environ->alist" '(("A" . "B") ("A" . "") ("" . "B") ("A" . "B=C"))
132 (sys-environ->alist '("A=B" "A=" "=B" "A=B=C")))
133
134 (let ([envs (sys-environ)])
135 (define (env-test var)
136 (test* #"sys-environ (~var)" #t
137 (cond [(sys-getenv var)
138 => (^[val] (boolean (member #"~|var|=~|val|" envs)))]
139 [else #t])))
140 (env-test "HOME")
141 (env-test "USER")
142 (env-test "LANG")
143 (env-test "PWD")
144 (env-test "TERM")
145 (env-test "SHELL"))
146
147 ;;-------------------------------------------------------------------
148 (test-section "pathnames")
149
150 (test* "basename" "ghi.jkl" (sys-basename "/abc/def/ghi.jkl"))
151 (test* "dirname" "/abc/def" (sys-dirname "/abc/def/ghi.jkl"))
152 (test* "basename" "ghi.jkl" (sys-basename "/abc/def/ghi.jkl/"))
153 (test* "dirname" "/abc/def" (sys-dirname "/abc/def/ghi.jkl/"))
154 (test* "basename" "ghi.jkl" (sys-basename "/abc//def//ghi.jkl//"))
155 (test* "dirname" "/abc//def" (sys-dirname "/abc//def//ghi.jkl//"))
156 (test* "basename" "ghi.jkl" (sys-basename "ghi.jkl"))
157 (test* "dirname" "." (sys-dirname "ghi.jkl"))
158
159 (test* "basename" "" (sys-basename ""))
160 (test* "dirname" "." (sys-dirname ""))
161 (test* "basename" "" (sys-basename "/"))
162 (test* "dirname" (n "/") (sys-dirname "/"))
163 (test* "basename" "" (sys-basename "//"))
164 (test* "dirname" (n "/") (sys-dirname "//"))
165 (test* "basename" "abc" (sys-basename "/abc"))
166 (test* "dirname" (n "/") (sys-dirname "/abc"))
167 (test* "basename" "abc" (sys-basename "//abc"))
168 (test* "dirname" (n "/") (sys-dirname "//abc"))
169
170 (test* "basename" ".." (sys-basename "../"))
171 (test* "dirname" "." (sys-dirname "../"))
172 (test* "basename" ".." (sys-basename "../.."))
173 (test* "dirname" ".." (sys-dirname "../.."))
174
175 (cond-expand
176 (gauche.os.windows
177 ;; test with a drive letter
178 (test* "dirname" "d:\\" (sys-dirname "d:"))
179 (test* "basename" "" (sys-basename "d:"))
180 (test* "dirname" "d:\\" (sys-dirname "d:/"))
181 (test* "basename" "" (sys-basename "d:/"))
182 (test* "dirname" "d:\\" (sys-dirname "d:/z"))
183 (test* "basename" "z" (sys-basename "d:/z"))
184 (test* "dirname" "d:/z" (sys-dirname "d:/z/y"))
185 (test* "basename" "y" (sys-basename "d:/z/y"))
186 (test* "dirname" "d:." (sys-dirname "d:z"))
187 (test* "basename" "z" (sys-basename "d:z"))
188 (test* "dirname" "d:z" (sys-dirname "d:z/y"))
189 (test* "basename" "y" (sys-basename "d:z/y"))
190 )
191 (else #f))
192
193 (test* "normalize" (n (string-append (get-pwd-via-pwd) "/."))
194 (sys-normalize-pathname "." :absolute #t))
195 (test* "normalize" (n (string-append (get-pwd-via-pwd) "/"))
196 (sys-normalize-pathname "" :absolute #t))
197 (test* "normalize"
198 (cond-expand
199 [gauche.os.windows
200 (n (string-append (or (sys-getenv "HOME") ; MSYS
201 (sys-getenv "USERPROFILE") ; cmd.exe
202 "")
203 "/abc"))]
204 [else
205 (n (string-append (slot-ref (sys-getpwuid (sys-getuid)) 'dir) "/abc"))])
206 (sys-normalize-pathname "~/abc" :expand #t))
207 (test* "normalize" (n "/a/b/c/d/e")
208 (sys-normalize-pathname "/a/b//.///c//d/./e"
209 :canonicalize #t))
210 (test* "normalize" (n "/a/b/c/d/e/")
211 (sys-normalize-pathname "/a/b//.///c//d/./e/"
212 :canonicalize #t))
213 (test* "normalize" (n "/a/b/c/d/e/")
214 (sys-normalize-pathname "/a/B//./../c/d/../../b//c/d/e/f/.."
215 :canonicalize #t))
216 (test* "normalize" (n "/a/b/")
217 (sys-normalize-pathname "/a/b/c/d/../.."
218 :canonicalize #t))
219 (test* "normalize" (n "/c/d/")
220 (sys-normalize-pathname "/c/d/e/f/../../"
221 :canonicalize #t))
222 (test* "normalize" (n "/e/f/")
223 (sys-normalize-pathname "/e/f/g/h/../../."
224 :canonicalize #t))
225
226 (test* "normalize" ""
227 (sys-normalize-pathname ""
228 :canonicalize #t))
229 (test* "normalize" (n "../..")
230 (sys-normalize-pathname "a/b/c/../../../../.."
231 :canonicalize #t))
232 (test* "normalize" (n "../../x/y")
233 (sys-normalize-pathname "a/b/c/../../../../../x/y"
234 :canonicalize #t))
235
236 ;;-------------------------------------------------------------------
237 (test-section "glob")
238
239 ;; NB: Actual globbing on filesystems is tested in file.scm.
240 ;; Here we test the internal component, glob-component->regexp.
241
242 (let ((data '("a.c" ".a.c" "...c" ".c" "a?.c" "a*.c")))
243 (define (t mode pattern expected)
244 (test* (string-append "glob pattern " (x->string mode) " " pattern)
245 expected
246 (filter (glob-component->regexp pattern :mode mode) data)))
247 (define (t-glob pattern expected) (t :glob pattern expected))
248 (define (t-shell pattern expected) (t :shell pattern expected))
249
250 (t-glob "*.c" '("a.c" "a?.c" "a*.c"))
251 (t-glob "??.c" '("a?.c" "a*.c"))
252 (t-glob ".*.c" '(".a.c" "...c"))
253 (t-glob ".*c" '(".a.c" "...c" ".c"))
254 (t-glob "*?.c" '("a.c" "a?.c" "a*.c"))
255 (t-glob "*\\?.c" '("a?.c"))
256 (t-glob "*\\*.c" '("a*.c"))
257
258 (t-shell "*.c" '("a.c" ".a.c" "...c" ".c" "a?.c" "a*.c"))
259 (t-shell "??.c" '(".a.c" "...c" "a?.c" "a*.c"))
260 (t-shell ".*.c" '(".a.c" "...c" ))
261 (t-shell ".*c" '(".a.c" "...c" ".c"))
262 (t-shell "*?.c" '("a.c" ".a.c" "...c" "a?.c" "a*.c"))
263 (t-shell "*\\?.c" '("a?.c"))
264 (t-shell "*\\*.c" '("a*.c"))
265 )
266
267 ;;-------------------------------------------------------------------
268 (test-section "filesystem")
269
270 (cmd-rmrf "test.dir")
271
272 (test* "access" '(#f #f #f #f)
273 (map (lambda (flag) (sys-access "test.dir" flag))
274 (list F_OK R_OK W_OK X_OK)))
275
276 (cmd-touch "test.dir")
277
278 (test* "unlink" #f
279 (begin
280 (sys-unlink "test.dir") (sys-access "test.dir" F_OK)))
281
282 (cond-expand
283 (gauche.os.windows
284 ;; we need entirey different scheme here, but for the time being we
285 ;; just omit the test.
286 (sys-mkdir "test.dir" #o750)
287 )
288 (else
289 (test* "mkdir" #/drw[sx]r-[sx]---/
290 (begin
291 (sys-mkdir "test.dir" #o750)
292 (get-lsmode "test.dir"))
293 rxmatch)
294
295 (test* "chmod" #/drw[sx]r-[sx]r-x/
296 (begin
297 (sys-chmod "test.dir" #o755)
298 (get-lsmode "test.dir"))
299 rxmatch)
300
301 (test* "fchmod" #/drw[sx]r-[sx]---/
302 (begin
303 (call-with-input-file "test.dir"
304 (cut sys-fchmod <> #o750))
305 (get-lsmode "test.dir"))
306 rxmatch)
307 ))
308
309 (define *fs-test-str* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
310
311 (with-output-to-file "test.dir/xyzzy"
312 (lambda () (display *fs-test-str*)))
313
314 (test* "rename" '(#f #t)
315 (begin
316 (sys-rename "test.dir/xyzzy" "test.dir/zzZzz")
317 (list (sys-access "test.dir/xyzzy" F_OK)
318 (sys-access "test.dir/zzZzz" F_OK))))
319
320 (test* "readdir" '("." ".." "zzZzz")
321 (sort (sys-readdir "test.dir")))
322
323 (test* "link" '("." ".." "xyzzy" "zzZzz")
324 (begin
325 (sys-link "test.dir/zzZzz" "test.dir/xyzzy")
326 (sort (sys-readdir "test.dir"))))
327
328 (test* "unlink" '("." ".." "xyzzy")
329 (begin
330 (sys-unlink "test.dir/zzZzz")
331 (sort (sys-readdir "test.dir"))))
332
333 (test* "rename" '("." ".." "zzZzz")
334 (begin
335 (sys-rename "test.dir/xyzzy" "test.dir/zzZzz")
336 (sort (sys-readdir "test.dir"))))
337
338 (test* "truncate" "abcdefghijklmno"
339 (begin
340 (sys-truncate "test.dir/zzZzz" 15)
341 (call-with-input-file "test.dir/zzZzz" read-line)))
342
343 (test* "ftruncate" "abcde"
344 (begin
345 (call-with-output-file "test.dir/zzZzz"
346 (cut sys-ftruncate <> 5)
347 :if-exists :append)
348 (call-with-input-file "test.dir/zzZzz" read-line)))
349
350 (test* "rmdir" #f
351 (begin
352 (sys-unlink "test.dir/zzZzz")
353 (sys-rmdir "test.dir")
354 (sys-access "test.dir" F_OK)))
355
356 ;; sys-realpath
357
358 (define (expected-path p)
359 (let1 pp (sys-normalize-pathname p :absolute #t :canonicalize #t)
360 (if (eqv? (string-ref pp (- (string-length pp) 1)) #\/)
361 (substring pp 0 (- (string-length pp) 1))
362 pp)))
363
364 (test* "sys-realpath (/)" (sys-normalize-pathname "/") (sys-realpath "/"))
365 (test* "sys-realpath (.)" (expected-path ".") (sys-realpath "."))
366
367 (cond-expand
368 [gauche.sys.symlink
369 (sys-unlink "test1.o")
370 (sys-unlink "test2.o")
371 (with-output-to-file "test1.o" (cut print))
372
373 (sys-symlink "test1.o" "test2.o")
374 (test* "sys-realpath (symlink)"
375 (expected-path "./test1.o")
376 (sys-realpath "./test2.o"))
377 (sys-unlink "test2.o")
378
379 (sys-symlink "./test1.o" "test2.o")
380 (test* "sys-realpath (symlink)"
381 (expected-path "./test1.o")
382 (sys-realpath "./test2.o"))
383 (sys-unlink "test2.o")
384
385 (sys-symlink "../src/test1.o" "test2.o")
386 (test* "sys-realpath (symlink)"
387 (expected-path "./test1.o")
388 (sys-realpath "./test2.o"))
389
390 (sys-unlink "test1.o")
391 (test* "sys-realpath (dangling)"
392 (test-error)
393 (sys-realpath "./test2.o"))
394
395 (sys-mkdir "test1.o" #o777)
396 (with-output-to-file "test1.o/test.o" (cut print))
397 (test* "sys-realpath (symlink to dir)"
398 (expected-path "./test1.o/test.o")
399 (sys-realpath "./test2.o/test.o"))
400
401 (sys-unlink "test1.o/test.o")
402 (sys-rmdir "test1.o")
403 (sys-unlink "test2.o")
404
405 (test* "sys-realpath (NOENT)"
406 (test-error <system-error>)
407 (sys-realpath "./test2.o/test.o"))
408 ]
409 [else])
410
411 ;; sys-mkstemp/sys-mkdtemp
412 (let ()
413 ;; Check if multiple temporary files or directories are created.
414 (define (test-mkxtemp name creator tester remover)
415 (test* name '(#t #t)
416 (let1 temps (map creator (iota 3))
417 (begin0 (list (every tester temps)
418 (let1 sorted (sort temps)
419 (every (complement equal?) sorted (cdr sorted))))
420 (for-each remover temps)))))
421
422 (test-mkxtemp "sys-mkstemp"
423 (^_ (receive (p n) (sys-mkstemp "test.o")
424 (close-output-port p)
425 n))
426 file-is-regular?
427 sys-unlink)
428
429 (test-mkxtemp "sys-mkdtemp"
430 (^_ (sys-mkdtemp "test.dir"))
431 file-is-directory?
432 sys-rmdir))
433
434 ;;-------------------------------------------------------------------
435 (test-section "time")
436
437 (test* "srfi time" #t (time? (current-time)))
438 (test* "srfi time time->seconds" #t
439 (let1 t (current-time)
440 (= (ref t'second) (floor->exact (time->seconds t)))))
441 (test* "srfi time seconds->time" '(98765432109876 500000000)
442 (let1 t (seconds->time 98765432109876.5)
443 (list (ref t'second) (ref t'nanosecond))))
444
445 ;; <time> should be immutable.
446 '(test* "srfi time setter" (make <time> :second -98765432109876 :nanosecond 4)
447 (let1 t (make <time>)
448 (set! (ref t'second) -98765432109876)
449 (set! (ref t'nanosecond) 4)
450 t))
451
452 (let* ([t0 (current-time)]
453 [t1 (make <time> :second (~ t0'second) :nanosecond #e999_999_999)]
454 [t2 (make <time> :second (~ t0'second) :nanosecond 0)])
455 (define (t->list t :optional (delta-s 0) (delta-ns 0))
456 (list (+ (~ t'second) delta-s) (+ (~ t'nanosecond) delta-ns)))
457 (test* "absolute-time (integer second)"
458 (t->list t0 1)
459 (t->list (absolute-time 1 t0)))
460 (test* "absolute-time (negative integer second)"
461 (t->list t0 -1)
462 (t->list (absolute-time -1 t0)))
463 (test* "absolute-time (real second, t0)"
464 (test-one-of
465 (t->list t0 1 #e5e8)
466 (t->list t0 2 #e-5e8))
467 (t->list (absolute-time 1.5 t0)))
468 (test* "absolute-time (real second, t1)"
469 (t->list t1 2 #e-5e8)
470 (t->list (absolute-time 1.5 t1)))
471 (test* "absolute-time (negative real second, t0)"
472 (test-one-of
473 (t->list t0 0 #e-25e7)
474 (t->list t0 -1 #e75e7))
475 (t->list (absolute-time -0.25 t0)))
476 (test* "absolute-time (negative real second, t2)"
477 (t->list t2 -1 #e75e7)
478 (t->list (absolute-time -0.25 t2)))
479 (test* "absolute-time (duration)"
480 (t->list t1 11 -999_999_999)
481 (t->list (absolute-time (make <time>
482 :type 'time-duration
483 :second 10
484 :nanosecond 1)
485 t1)))
486 (test* "absolute-time (negative duration)"
487 (t->list t2 -10 1)
488 (t->list (absolute-time (make <time>
489 :type 'time-duration
490 :second -10
491 :nanosecond 1)
492 t2)))
493 )
494
495 ;;-------------------------------------------------------------------
496 (test-section "stat")
497
498 (let ()
499 (define (mask unix win)
500 (cond-expand
501 (gauche.os.windows win)
502 (else unix)))
503
504 (cmd-rmrf "test.dir")
505 (with-output-to-file "test.dir" (lambda () (display "01234")))
506 (sys-chmod "test.dir" #o654)
507
508
509 (test* "stat" `(,(mask #o654 #o666) regular 5)
510 (let ((s (sys-stat "test.dir")))
511 (list (logand #o777 (sys-stat->mode s))
512 (sys-stat->file-type s)
513 (sys-stat->size s))))
514
515 (test* "fstat" `(,(mask #o654 #o666) regular 5)
516 (call-with-input-file "test.dir"
517 (lambda (p)
518 (let ((s (sys-fstat p)))
519 (list (logand #o777 (sys-stat->mode s))
520 (sys-stat->file-type s)
521 (sys-stat->size s))))))
522
523 (sys-unlink "test.dir")
524 (sys-mkdir "test.dir" #o700)
525
526 (test* "stat" `(,(mask #o700 #o777) directory)
527 (let ((s (sys-stat "test.dir")))
528 (list (logand #o777 (sys-stat->mode s))
529 (sys-stat->file-type s))))
530
531 ;; on windows you cannot use open-input-file on a directory.
532 (cond-expand
533 (gauche.os.windows)
534 (else
535 (test* "fstat" `(,(mask #o700 #o777) directory)
536 (call-with-input-file "test.dir"
537 (lambda (p)
538 (let ((s (sys-fstat p)))
539 (list (logand #o777 (sys-stat->mode s))
540 (sys-stat->file-type s))))))))
541 )
542
543 (sys-rmdir "test.dir")
544
545 ;;-------------------------------------------------------------------
546 (test-section "pipe")
547
548 (test* "pipe" "abc"
549 (receive (in out) (sys-pipe)
550 (display "abc\n" out) (flush out)
551 (let1 r (read-line in)
552 (close-input-port in)
553 (close-output-port out)
554 r)))
555
556 (test* "pipe and char-ready? (none)" '(#f #t #f)
557 (receive (in out) (sys-pipe :buffering :none)
558 (display "a" out) (read-char in)
559 (let1 f1 (char-ready? in)
560 (display "bc" out) (read-char in)
561 (let1 f2 (char-ready? in)
562 (read-char in)
563 (let1 f3 (char-ready? in)
564 (close-input-port in) (close-output-port out)
565 (list f1 f2 f3))))))
566
567 (test* "pipe and char-ready? (line)" '(#f #t #t)
568 (receive (in out) (sys-pipe :buffering :line)
569 (display "a" out)
570 (let1 f1 (char-ready? in)
571 (display "\n" out)
572 (let1 f2 (char-ready? in)
573 (read-char in)
574 (let1 f3 (char-ready? in)
575 (close-input-port in) (close-output-port out)
576 (list f1 f2 f3))))))
577
578 (test* "pipe and char-ready? (full)" '(#f #f #t)
579 (receive (in out) (sys-pipe :buffering :full)
580 (display "a" out)
581 (let1 f1 (char-ready? in)
582 (display "\n" out)
583 (let1 f2 (char-ready? in)
584 (flush out) (read-char in)
585 (let1 f3 (char-ready? in)
586 (close-input-port in) (close-output-port out)
587 (list f1 f2 f3))))))
588
589 (test* "pipe and read-block(none)" 2
590 (receive (in out) (sys-pipe :buffering :none)
591 (display "ab" out)
592 (let1 r (string-size (read-block 1000 in))
593 (close-input-port in)
594 (close-output-port out)
595 r)))
596
597 (test* "pipe and read-block(line)" 2
598 (receive (in out) (sys-pipe :buffering :line)
599 (display "a\n" out)
600 (let1 r (string-size (read-block 1000 in))
601 (close-input-port in)
602 (close-output-port out)
603 r)))
604
605 ;;-------------------------------------------------------------------
606 (test-section "fork&exec")
607
608 (define (nap)
609 (cond-expand
610 (gauche.sys.nanosleep (sys-nanosleep 200000000)) ;0.2s
611 (else (sys-sleep 1))))
612
613 (cond-expand
614 [(and (not gauche.os.windows) ;; win32 doesn't support fork at all.
615 (not gauche.os.cygwin)) ;; cygwin's fork is not reliable.
616 (test* "fork & wait" #t
617 (let ((pid (sys-fork)))
618 (if (= pid 0)
619 (sys-exit 5)
620 (receive (rpid code) (sys-wait)
621 (and (= rpid pid)
622 (sys-wait-exited? code)
623 (= (sys-wait-exit-status code) 5))))))
624
625 (test* "fork & waitpid" #t
626 (let ((pid (sys-fork)))
627 (if (= pid 0)
628 (sys-exit 10)
629 (receive (rpid code) (sys-waitpid pid)
630 (and (= rpid pid)
631 (sys-wait-exited? code)
632 (= (sys-wait-exit-status code) 10))))))
633
634 (test* "fork, wait & kill" #t
635 (let ((pid (sys-fork)))
636 (if (= pid 0)
637 (begin (sys-pause) (sys-exit 0))
638 (begin
639 (sys-kill pid SIGKILL)
640 (receive (rpid code) (sys-wait)
641 (and (= rpid pid)
642 (sys-wait-signaled? code)
643 (= (sys-wait-termsig code) SIGKILL)))))))
644
645 (test* "fork, wait, kill & sleep" #t
646 (let1 pid (sys-fork)
647 (if (= pid 0)
648 (begin (nap) (sys-exit 0))
649 (begin
650 (sys-kill pid SIGSTOP)
651 (receive (rpid code) (sys-waitpid pid :untraced #t)
652 (and (= rpid pid)
653 (sys-wait-stopped? code)
654 (= (sys-wait-stopsig code) SIGSTOP)
655 (begin (sys-kill pid SIGCONT)
656 (receive (rpid code) (sys-wait)
657 (and (= rpid pid)
658 (sys-wait-exited? code)
659 (= (sys-wait-exit-status code) 0)
660 )))
661 )))
662 ))
663 )
664
665 (test* "fork & pipe" 70000
666 (receive (in out) (sys-pipe)
667 (let1 pid (sys-fork)
668 (if (= pid 0)
669 (begin (close-input-port in)
670 (display (make-string 69999) out)
671 (with-error-handler
672 (^e (sys-exit 0))
673 (lambda ()
674 (newline out)
675 (close-output-port out)
676 (sys-pause))))
677 (let loop ((toread 70000)
678 (nread 0))
679 (let1 r (string-size (read-block toread in))
680 (if (>= (+ nread r) 70000)
681 (begin (sys-kill pid SIGTERM)
682 (sys-waitpid pid)
683 (+ nread r))
684 (loop (- toread r) (+ nread r)))))
685 ))))
686
687 (test* "fork, exec and signal mask" #t
688 (let ((nmask (make <sys-sigset>))
689 (cmask (make <sys-sigset>)))
690 (sys-sigset-fill! nmask)
691 (let ((omask (sys-sigmask SIG_SETMASK nmask))
692 (zero (open-input-file "/dev/zero")))
693 (receive (in out) (sys-pipe :buffering :none)
694 (let1 pid
695 (sys-fork-and-exec "cat" '("cat")
696 :iomap `((0 . ,zero) (1 . ,out))
697 :sigmask cmask)
698 (read-byte in) ;; make sure 'cat' is started
699 (sys-kill pid SIGINT)
700 (sys-sigmask SIG_SETMASK omask)
701 (sys-waitpid pid)
702 #t)))))
703
704 ;; Testing fork&exec and detached process
705 ;; NB: these tests assume we're running the testing gosh in the
706 ;; current directory.
707 (when (file-exists? "./gosh")
708 (cmd-rmrf "test.out")
709 ;; The child process. Gets the expected ppid in the first arg.
710 ;; If :detached is true, we expect ppid to be 1. However, the
711 ;; immediate parent may take time to exit, so we loop to synchronize.
712 (with-output-to-file "test.out"
713 (lambda ()
714 (write '(define (main args)
715 (let1 expected-ppid (x->integer (cadr args))
716 (let loop ([c 0])
717 (sys-nanosleep #e2e8)
718 (cond [(or (eqv? (sys-getppid) expected-ppid)
719 (>= c 50)) ; takes about 10s
720 (write (list (sys-getpid) (sys-getpgrp)))]
721 [else (loop (+ c 1))])))))))
722 (let ()
723 (define (run-and-read-test msg ppid detached)
724 (test* msg detached
725 (receive (in out) (sys-pipe :buffering :none)
726 (let1 pid (sys-fork-and-exec "./gosh"
727 `("./gosh" "-ftest" "./test.out"
728 ,(x->string ppid))
729 :iomap `((1 . ,out))
730 :detached detached)
731 (close-port out)
732 (let1 result (read in)
733 (sys-waitpid pid)
734 (eqv? (car result) (cadr result)))))))
735 (run-and-read-test "fork, exec and detached process (not detached)"
736 (sys-getpid) #f)
737 (run-and-read-test "fork, exec and detached process (detached)"
738 1 #t)))
739 (cmd-rmrf "test.out")
740
741 ;; Testing GC in forked process---we don't explicitly spawn a thread here,
742 ;; but some architecture (OSX 10.7.3, at least) seems to create a thread
743 ;; implicitly. In the child process all threads are gone except one,
744 ;; and GC needs to recognize that. The following test fails on OSX 10.7.3
745 ;; without HANDLE_FORK defined in gc.
746 ;; NB: HANDLE_FORK doesn't seem to work on OSX 10.7.4. Disabling for now.
747 ;; (test* "GC in forked process" "done"
748 ;; (receive (in out) (sys-pipe)
749 ;; (let ([pid (sys-fork)])
750 ;; (if (= pid 0)
751 ;; (begin
752 ;; (dotimes (i 1000)
753 ;; (make-vector 10000))
754 ;; (gc)
755 ;; (display "done\n" out)
756 ;; (sys-exit 0))
757 ;; (let ((line (read-line in)))
758 ;; (sys-waitpid pid)
759 ;; line)))))
760
761 ] ; !gauche.os.windows
762 [else])
763
764 ;;-------------------------------------------------------------------
765 (test-section "select")
766
767 (cond-expand
768 [gauche.sys.select
769 (test* "fdset" '(3 #t #f #t #t #f)
770 (let ((fdset (make <sys-fdset>)))
771 (set! (sys-fdset-ref fdset (current-input-port)) #t)
772 (sys-fdset-set! fdset (current-error-port) #t)
773 (sys-fdset-set! fdset 3 #t)
774 (sys-fdset-set! fdset 4 #f)
775 (cons (sys-fdset-max-fd fdset)
776 (map (^i (sys-fdset-ref fdset i)) (iota 5)))))
777
778 (test* "fdset" '(-1 7 7 4 10 10 -1)
779 (let ((fdset (make <sys-fdset>))
780 (result '()))
781 (define (push-result)
782 (set! result (cons (sys-fdset-max-fd fdset) result)))
783 (push-result)
784 (sys-fdset-set! fdset 7 #t)
785 (push-result)
786 (sys-fdset-set! fdset 4 #t)
787 (push-result)
788 (sys-fdset-set! fdset 7 #f)
789 (push-result)
790 (sys-fdset-set! fdset 10 #t)
791 (push-result)
792 (sys-fdset-set! fdset 4 #f)
793 (push-result)
794 (sys-fdset-set! fdset 10 #f)
795 (push-result)
796 (reverse result)))
797
798 (test* "sys-fdset" `(,(port-file-number (current-input-port)) 9 10)
799 (sys-fdset->list (sys-fdset 9 (current-input-port) 10)))
800 (test* "list->sys-fdset" '(1 3 5 7 9)
801 (sys-fdset->list (list->sys-fdset (list (sys-fdset 3 9)
802 7
803 (sys-fdset 1 3 5)))))
804 (test* "sys-fdset-copy!" '(2 4 5)
805 (let1 dst (make <sys-fdset>)
806 (sys-fdset-copy! dst (sys-fdset 5 4 2))
807 (sys-fdset->list dst)))
808
809 (test* "sys-fdset-clear!" '()
810 (sys-fdset->list (sys-fdset-clear! (sys-fdset 1 2 3))))
811
812 ;; NB: Windows' select() can't select on non-socket fds, so we skip
813 ;; this test.
814
815 (cond-expand
816 [(not gauche.os.windows)
817 (test* "select" '(0 #f #f #f #f 1 #t #f #f #t #\x)
818 (let*-values (((in out) (sys-pipe))
819 ((pid) (sys-fork)))
820 (if (= pid 0)
821 (begin (sys-select #f #f #f 100000)
822 (display "x" out)
823 (close-output-port out)
824 (sys-exit 0))
825 (let ((rfds (make <sys-fdset>)))
826 (sys-fdset-set! rfds in #t)
827 (receive (an ar aw ae)
828 (sys-select rfds #f #f 0)
829 (receive (bn br bw be)
830 (sys-select! rfds #f #f #f)
831 (begin0
832 (list an (eq? ar rfds) aw ae
833 (sys-fdset-ref ar in)
834 bn (eq? br rfds) bw be
835 (sys-fdset-ref rfds in)
836 (read-char in))
837 (sys-waitpid pid)))))
838 ))
839 )]
840 [else]) ; cond-expand (not gauche.os.windows)
841 ]
842 [else]) ; cond-expand gauche.sys.select
843
844 ;;-------------------------------------------------------------------
845 (test-section "signal handling")
846
847 (cond-expand
848 [(not gauche.os.windows)
849
850 (test* "sigalrm1" SIGALRM
851 (call/cc
852 (lambda (k)
853 (with-signal-handlers
854 ((SIGALRM => k)
855 (#t (k 0)))
856 (lambda ()
857 (sys-alarm 1)
858 (sys-pause))))))
859
860 (test* "sigalrm2" 0
861 (call/cc
862 (lambda (k)
863 (with-signal-handlers
864 ((#t (k 0))
865 (SIGALRM => k))
866 (lambda ()
867 (sys-alarm 1)
868 (sys-pause))))))
869
870 (test* "sigalrm3" (test-error)
871 (call/cc
872 (lambda (k)
873 (with-signal-handlers
874 ((SIGINT => k)
875 (SIGUSR1 => k))
876 (lambda ()
877 (sys-alarm 1)
878 (sys-pause))))))
879
880 (test* "sigalrm4 (interrupting syscall)" SIGALRM
881 (call/cc
882 (lambda (k)
883 (with-signal-handlers
884 ((SIGALRM => k))
885 (lambda ()
886 (receive (in out) (sys-pipe)
887 (sys-alarm 1)
888 (read in)))))))
889
890 (test* "sigalrm5 (interrupting syscall - restart)" '(a)
891 (receive (in out) (sys-pipe)
892 (with-signal-handlers
893 ((SIGALRM (write '(a) out) (flush out)))
894 (lambda ()
895 (sys-alarm 1)
896 (read in)))))
897
898 (when (module-binds? 'gauche 'sys-select)
899 (test* "sigalrm6 (interrupting syscall - restart)" '(#t 0)
900 (let1 r #f
901 (with-signal-handlers
902 ((SIGALRM (set! r #t)))
903 (lambda ()
904 (sys-alarm 1)
905 (let1 s (sys-select #f #f #f 1500000)
906 (list r s))))))
907 )
908
909 (test* "fork & sigint" #t
910 (let ((pid (sys-fork))
911 (sigint #f)
912 (sigchld #f))
913 (if (= pid 0)
914 (let ((parent (sys-getppid)))
915 (nap)
916 (sys-kill parent SIGINT)
917 (sys-exit 0))
918 (with-signal-handlers
919 ((SIGINT (set! sigint #t))
920 (SIGCHLD (sys-waitpid pid) (set! sigchld #t)))
921 (lambda ()
922 (let loop ()
923 (if (and sigint sigchld)
924 #t
925 (begin (sys-pause) (loop))))))
926 )))
927
928 (test* "sigchld" SIGCHLD
929 (call/cc
930 (lambda (k)
931 (with-signal-handlers
932 ((SIGCHLD (sys-wait) (k SIGCHLD)))
933 (lambda ()
934 (let ((pid (sys-fork)))
935 (if (= pid 0)
936 (sys-exit 0)
937 (sys-pause))))))))
938
939 ;; NB: on cygwin (as of 1.5.25), sigmask doesn't work reliably so
940 ;; we skip test for now.
941 (cond-expand
942 [(not gauche.os.cygwin)
943 (test* "sigmask" 'hup
944 (let ((sig #f)
945 (chld #f)
946 (mask1 (sys-sigset SIGINT)))
947 (call/cc
948 (lambda (k)
949 (set-signal-handler! SIGINT k)
950 (set-signal-handler! SIGCHLD (^k (sys-wait) (set! chld #t)))
951 (set-signal-handler! SIGHUP (^k (set! sig 'hup)))
952 (sys-sigmask SIG_BLOCK mask1)
953 (let ((pid (sys-fork)))
954 (if (= pid 0)
955 (begin
956 (sys-kill (sys-getppid) SIGINT)
957 (nap) ;; solaris seems to lose SIGHUP without this
958 (sys-kill (sys-getppid) SIGHUP)
959 (sys-exit 0))
960 (begin
961 (let loop ()
962 (nap)
963 (unless sig (loop)))
964 (set-signal-handler! SIGINT #f)
965 (sys-sigmask SIG_UNBLOCK mask1)
966 ;;Some systems appear to lose this SIGCHLD (esp. cygwin)
967 ;;(let loop ()
968 ;; (unless chld (loop)))
969 sig)))))))
970
971 (let ()
972 (define (test-double-signal signals mask fire-sig)
973 (let ((flag #f)
974 (count 0))
975 (let/cc break
976 (set-signal-handler!
977 signals
978 (lambda (n)
979 (unless flag
980 (inc! count)
981 (when (> count 1) (break 'boo)) ;; avoid infinite reentrance
982 (sys-kill (sys-getpid) fire-sig)
983 (set! flag #t)))
984 mask)
985 (sys-kill (sys-getpid) SIGHUP)
986 flag)))
987
988 (test* "sigmask during interrupt handler (default)" #t
989 (test-double-signal SIGHUP #f SIGHUP))
990
991 (test* "sigmask during interrupt handler (explicit)" #t
992 (test-double-signal SIGHUP (sys-sigset SIGHUP) SIGHUP))
993
994 (test* "sigmask during interrupt handler (multi/default)" #t
995 (test-double-signal (sys-sigset SIGHUP SIGINT)
996 #f SIGINT))
997
998 (test* "sigmask during interrupt handler (multi/explicit)" #t
999 (test-double-signal (sys-sigset SIGHUP SIGINT)
1000 (sys-sigset SIGINT) SIGINT))
1001
1002 (test* "sigmask during interrupt handler (reentrance)" 'boo
1003 (test-double-signal SIGHUP (sys-sigset) SIGHUP))
1004
1005 (test* "sigmask during interrupt handler (multi/reentrance)" 'boo
1006 (test-double-signal (sys-sigset SIGHUP SIGINT)
1007 (sys-sigset) SIGHUP))
1008
1009 (set-signal-handler! SIGINT #f)
1010 )]
1011 [else]);; !cygwin
1012
1013 ;; sys-sigwait
1014 (cond-expand
1015 [(and gauche.sys.sigwait
1016 (not gauche.os.cygwin))
1017 (let ()
1018 (define z (^n (raise 'foo)))
1019
1020 (set-signal-handler! SIGCHLD #t)
1021 (set-signal-handler! SIGINT z)
1022
1023 (test* "sys-sigwait" SIGHUP
1024 (receive (in out) (sys-pipe)
1025 (let1 pid (sys-fork)
1026 (cond ((= pid 0)
1027 (close-output-port out)
1028 ;; synchronize with parent process
1029 (read-char in)
1030 (sys-kill (sys-getppid) SIGHUP)
1031 (sys-exit 0))
1032 (else
1033 (close-input-port in)
1034 (let* ((sigset (sys-sigset SIGHUP SIGINT))
1035 (oldmask (sys-sigmask SIG_BLOCK sigset)))
1036 ;; close the pipe to synchronize with child process
1037 (close-output-port out)
1038 (let1 signo (sys-sigwait sigset)
1039 (sys-waitpid pid)
1040 (sys-sigmask SIG_SETMASK oldmask)
1041 signo)))))))
1042
1043 (test* "sys-sigwait / signal handler restoration" 'foo
1044 (guard (e (else e))
1045 (sys-kill (sys-getpid) SIGINT))))
1046 ]
1047 [else]) ; (not (and gauche.sys.sigwait (not gauche.os.cygwin)))
1048
1049 ]
1050 [else]) ; gauche.os.windows
1051
1052 (test-end)