Debian Codegraph @

test-suite/tests/posix.test

raw
1 ;;;; posix.test --- Test suite for Guile POSIX functions. -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2003-2004, 2006-2007, 2010, 2012, 2015, 2017-2019, 2021-2023
4 ;;;; Free Software Foundation, Inc.
5 ;;;; Copyright 2021 Maxime Devos <[email protected]>
6 ;;;;
7 ;;;; This library is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Lesser General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;;
12 ;;;; This library is distributed in the hope that it will be useful,
13 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;;;; Lesser General Public License for more details.
16 ;;;;
17 ;;;; You should have received a copy of the GNU Lesser General Public
18 ;;;; License along with this library; if not, write to the Free Software
19 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 (define-module (test-suite test-posix)
22 #:use-module (test-suite lib)
23 #:use-module ((rnrs io ports) #:select (get-string-all)))
24
25 (define (skip-on-darwin)
26 (when (string-ci=? "darwin" (utsname:sysname (uname)))
27 (throw 'untested)))
28
29 (define (skip-on-freebsd)
30 (when (string-ci=? "FreeBSD" (utsname:sysname (uname)))
31 (throw 'untested)))
32
33 ;; FIXME: The following exec tests are disabled since on an i386 debian with
34 ;; glibc 2.3.2 they seem to interact badly with threads.test, the latter
35 ;; dies with signal 32 (one of the SIGRTs). Don't know how or why, or who's
36 ;; at fault (though it seems to happen with or without the recent memory
37 ;; leak fix in these error cases).
38
39 ;;
40 ;; execl
41 ;;
42
43 ;; (with-test-prefix "execl"
44 ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
45 ;; (execl "./nosuchprog" "./nosuchprog" "some arg")))
46
47 ;;
48 ;; execlp
49 ;;
50
51 ;; (with-test-prefix "execlp"
52 ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
53 ;; (execlp "./nosuchprog" "./nosuchprog" "some arg")))
54
55 ;;
56 ;; execle
57 ;;
58
59 ;; (with-test-prefix "execle"
60 ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
61 ;; (execle "./nosuchprog" '() "./nosuchprog" "some arg"))
62 ;; (pass-if-exception "./nosuchprog" '(system-error . ".*")
63 ;; (execle "./nosuchprog" '("FOO=1" "BAR=2") "./nosuchprog" "some arg")))
64
65
66 ;;
67 ;; mkstemp
68 ;;
69
70 (with-test-prefix "mkstemp"
71
72 ;; the temporary names used in the tests here are kept to 8 characters so
73 ;; they'll work on a DOS 8.3 file system
74
75 (define (string-copy str)
76 (list->string (string->list str)))
77
78 (pass-if-exception "number arg" exception:wrong-type-arg
79 (mkstemp! 123))
80
81 (pass-if "binary mode honored"
82 (let* ((outport (mkstemp "T-XXXXXX" "wb"))
83 (filename (port-filename outport)))
84 (display "\n" outport)
85 (close-port outport)
86 (let* ((inport (open-input-file filename #:binary #t))
87 (char1 (read-char inport))
88 (char2 (read-char inport))
89 (result (and (char=? char1 #\newline)
90 (eof-object? char2))))
91 (close-port inport)
92 (delete-file filename)
93 result))))
94
95 ;;
96 ;; putenv
97 ;;
98
99 (with-test-prefix "putenv"
100
101 (pass-if "something"
102 (putenv "FOO=something")
103 (equal? "something" (getenv "FOO")))
104
105 (pass-if "replacing"
106 (putenv "FOO=one")
107 (putenv "FOO=two")
108 (equal? "two" (getenv "FOO")))
109
110 (pass-if "empty"
111 (putenv "FOO=")
112 (equal? "" (getenv "FOO")))
113
114 (pass-if "removing"
115 (putenv "FOO=bar")
116 (putenv "FOO")
117 (not (getenv "FOO")))
118
119 (pass-if "modifying string doesn't change env"
120 (let ((s (string-copy "FOO=bar")))
121 (putenv s)
122 (string-set! s 5 #\x)
123 (equal? "bar" (getenv "FOO")))))
124
125 ;;
126 ;; setenv
127 ;;
128
129 (with-test-prefix "setenv"
130
131 (pass-if "something"
132 (setenv "FOO" "something")
133 (equal? "something" (getenv "FOO")))
134
135 (pass-if "replacing"
136 (setenv "FOO" "one")
137 (setenv "FOO" "two")
138 (equal? "two" (getenv "FOO")))
139
140 (pass-if "empty"
141 (setenv "FOO" "")
142 (equal? "" (getenv "FOO")))
143
144 (pass-if "removing"
145 (setenv "FOO" "something")
146 (setenv "FOO" #f)
147 (not (getenv "FOO"))))
148
149 ;;
150 ;; unsetenv
151 ;;
152
153 (with-test-prefix "unsetenv"
154
155 (pass-if "something"
156 (putenv "FOO=something")
157 (unsetenv "FOO")
158 (not (getenv "FOO")))
159
160 (pass-if "empty"
161 (putenv "FOO=")
162 (unsetenv "FOO")
163 (not (getenv "FOO"))))
164
165 ;;
166 ;; ttyname
167 ;;
168
169 (with-test-prefix "ttyname"
170
171 (pass-if-exception "non-tty argument" exception:system-error
172 ;; This used to crash in 1.8.1 and earlier.
173 (let ((file (false-if-exception
174 (open-output-file "/dev/null"))))
175 (if (not file)
176 (throw 'unsupported)
177 (ttyname file)))))
178
179 ;;
180 ;; utimes
181 ;;
182
183 (with-test-prefix "utime"
184
185 (pass-if "valid argument (second resolution)"
186 (let ((file "posix.test-utime"))
187 (dynamic-wind
188 (lambda ()
189 (close-port (open-output-file file)))
190 (lambda ()
191 (let* ((accessed (+ (current-time) 3600))
192 (modified (- accessed 1000)))
193 (utime file accessed modified)
194 (let ((info (stat file)))
195 (and (= (stat:atime info) accessed)
196 (= (stat:mtime info) modified)))))
197 (lambda ()
198 (delete-file file)))))
199
200 (pass-if-equal "AT_SYMLINK_NOFOLLOW"
201 '(1 1)
202 (if (defined? 'AT_SYMLINK_NOFOLLOW)
203 (let ((file "posix.test-utime"))
204 (dynamic-wind
205 (lambda ()
206 (symlink "/dev/null" file))
207 (lambda ()
208 (utime file 1 1 0 0 AT_SYMLINK_NOFOLLOW)
209 (let ((info (lstat file)))
210 (list (stat:atime info) (stat:mtime info))))
211 (lambda ()
212 (delete-file file))))
213 (throw 'unsupported)))
214
215 (define (utime-unless-unsupported oops . arguments)
216 (catch 'system-error
217 (lambda ()
218 (catch 'wrong-type-arg
219 (lambda ()
220 (apply utime arguments))
221 (lambda _
222 ;; 'futimens' is not supported on all platforms.
223 (oops))))
224 (lambda args
225 ;; On some platforms, 'futimens' returns ENOSYS according to Gnulib.
226 (if (= (system-error-errno args) ENOSYS)
227 (oops)
228 (apply throw args)))))
229
230 (pass-if-equal "file port"
231 '(1 1)
232 (let ((file "posix.test-utime"))
233 (false-if-exception (delete-file file))
234 (close-port (open-output-file file))
235 (define (delete)
236 (delete-file file))
237 (define (oops)
238 (delete)
239 (throw 'unsupported))
240 (call-with-input-file file
241 (lambda (port)
242 (utime-unless-unsupported oops port 1 1 0 0)
243 (define info (stat file))
244 (delete)
245 (list (stat:atime info) (stat:mtime info))))))
246
247 ;; This causes an EBADF system error on GNU/Linux with the 5.10.46 kernel.
248 #;
249 (pass-if-equal "file port (port representing symbolic link)"
250 '(1 1)
251 (let ((file "posix.test-utime"))
252 (unless (false-if-exception
253 (begin (symlink "/should-be-irrelevant" file)
254 #t))
255 (display "cannot create symlink, a utime test skipped\n")
256 (throw 'unresolved))
257 (unless (and (defined? 'O_NOFOLLOW)
258 (defined? 'O_PATH)
259 (not (= 0 O_NOFOLLOW))
260 (not (= 0 O_PATH)))
261 (display "cannot open symlinks, a utime test skipped\n")
262 (throw 'unresolved))
263 (define (delete)
264 (when port (close-port port))
265 (false-if-exception (delete-file file)))
266 (define (oops)
267 (delete)
268 (throw 'unsupported))
269 (define port #f)
270 (catch #t
271 (lambda ()
272 (set! port
273 (open file (logior O_NOFOLLOW O_PATH)))
274 (utime-unless-unsupported oops port 1 1 0 0))
275 (lambda args
276 (pk 'deleting file)
277 (delete)
278 (apply throw args)))
279 (define info (lstat file))
280 (delete)
281 (list (stat:mtime info) (stat:atime info)))))
282
283 ;;
284 ;; affinity
285 ;;
286
287 (with-test-prefix "affinity"
288
289 (pass-if "getaffinity"
290 (if (defined? 'getaffinity)
291 (> (bitvector-length (getaffinity (getpid))) 0)
292 (throw 'unresolved)))
293
294 (pass-if "setaffinity"
295 (if (and (defined? 'setaffinity) (defined? 'getaffinity))
296 (catch 'system-error
297 (lambda ()
298 (let ((mask (getaffinity (getpid))))
299 (setaffinity (getpid) mask)
300 (equal? mask (getaffinity (getpid)))))
301 (lambda args
302 ;; On some platforms such as sh4-linux-gnu, 'setaffinity'
303 ;; returns ENOSYS.
304 (let ((errno (system-error-errno args)))
305 (if (= errno ENOSYS)
306 (throw 'unresolved)
307 (apply throw args)))))
308 (throw 'unresolved))))
309
310 ;;
311 ;; pipe
312 ;;
313
314 (with-test-prefix "pipe"
315
316 (pass-if-equal "in and out"
317 "hi!\n"
318 (let ((in+out (pipe)))
319 (display "hi!\n" (cdr in+out))
320 (close-port (cdr in+out))
321 (let ((str (list->string (list (read-char (car in+out))
322 (read-char (car in+out))
323 (read-char (car in+out))
324 (read-char (car in+out))))))
325 (and (eof-object? (read-char (car in+out)))
326 (begin
327 (close-port (car in+out))
328 str)))))
329
330 (pass-if-equal "O_CLOEXEC"
331 (list FD_CLOEXEC FD_CLOEXEC)
332 (let* ((in+out (catch 'system-error
333 (lambda ()
334 (pipe O_CLOEXEC))
335 (lambda args
336 (if (= (system-error-errno args) ENOSYS)
337 (throw 'unresolved)
338 (apply throw args)))))
339 (flags (list (fcntl (car in+out) F_GETFD)
340 (fcntl (cdr in+out) F_GETFD))))
341 (close-port (car in+out))
342 (close-port (cdr in+out))
343 flags)))
344
345 ;;
346 ;; system*
347 ;;
348
349 (with-test-prefix "system*"
350
351 (pass-if "http://bugs.gnu.org/13166"
352 ;; With Guile up to 2.0.7 included, the child process launched by
353 ;; `system*' would remain alive after an `execvp' failure.
354 (let ((me (getpid)))
355 (and (not (zero? (system* "something-that-does-not-exist")))
356 (= me (getpid)))))
357
358 (pass-if-equal "exit code for nonexistent file"
359 127 ;aka. EX_NOTFOUND
360 (status:exit-val (system* "something-that-does-not-exist")))
361
362 (pass-if-equal "https://bugs.gnu.org/55596"
363 127
364 ;; The parameterization below used to cause 'start_child' to close
365 ;; fd 2 in the child process, which in turn would cause it to
366 ;; segfault, leading to a wrong exit code.
367 (parameterize ((current-output-port (current-error-port)))
368 (status:exit-val (system* "something-that-does-not-exist"))))
369
370 (pass-if-equal "https://bugs.gnu.org/52835"
371 "bong\n"
372 (let* ((port (mkstemp "T-XXXXXX"))
373 (file (port-filename port)))
374 ;; Redirect stdout and stderr to FILE.
375 (define status
376 (call-with-output-file file
377 (lambda (port)
378 (with-output-to-port port
379 (lambda ()
380 (with-error-to-port port
381 (lambda ()
382 (system* "sh" "-c" "echo bong >&2"))))))))
383
384 (delete-file file)
385 (and (zero? (status:exit-val status))
386 (get-string-all port))))
387
388 (pass-if-equal "https://bugs.gnu.org/63024"
389 0
390 (if (file-exists? "/proc/self/fd/0") ;on GNU/Linux?
391 (parameterize ((current-output-port (%make-void-port "w0")))
392 (system* "guile" "-c"
393 (object->string
394 '(exit (string=? "/dev/null"
395 (readlink "/proc/self/fd/1"))))))
396 (throw 'unresolved))))
397
398 ;;
399 ;; spawn
400 ;;
401
402 (with-test-prefix "spawn"
403
404 (pass-if-equal "basic"
405 0
406 (cdr (waitpid (spawn "true" '("true")))))
407
408 (pass-if-exception "non-file port argument" ;<https://bugs.gnu.org/61073>
409 exception:wrong-type-arg
410 (spawn "true" '("true")
411 #:error (%make-void-port "w")))
412
413 (pass-if-equal "uname with stdout redirect"
414 (list 0 ;exit value
415 (string-append (utsname:sysname (uname)) " "
416 (utsname:machine (uname)) "\n"))
417 (let* ((input+output (pipe))
418 (pid (spawn "uname" '("uname" "-s" "-m")
419 #:output (cdr input+output))))
420 (close-port (cdr input+output))
421 (let ((str (get-string-all (car input+output))))
422 (close-port (car input+output))
423 (list (cdr (waitpid pid)) str))))
424
425 (pass-if-equal "wc with stdin and stdout redirects"
426 "2\n"
427 (let* ((a+b (pipe))
428 (c+d (pipe))
429 (pid (spawn "wc" '("wc" "-w")
430 #:input (car a+b)
431 #:output (cdr c+d))))
432 (close-port (car a+b))
433 (close-port (cdr c+d))
434
435 (display "Hello world.\n" (cdr a+b))
436 (close-port (cdr a+b))
437
438 (let ((str (get-string-all (car c+d))))
439 (close-port (car c+d))
440 (waitpid pid)
441 (string-trim str))))
442
443 (pass-if-equal "env with #:environment and #:output"
444 "GNU=guile\n"
445 (let* ((input+output (pipe))
446 (pid (spawn "env" '("env")
447 #:environment '("GNU=guile")
448 #:output (cdr input+output))))
449 (close-port (cdr input+output))
450 (let ((str (get-string-all (car input+output))))
451 (close-port (car input+output))
452 (waitpid pid)
453
454 (let ((sysname (utsname:sysname (uname))))
455 (cond
456 ((string=? "GNU" sysname)
457 ;; On GNU/Hurd, the exec server prepends 'LD_ORIGIN_PATH' for
458 ;; every program: <https://bugs.gnu.org/62501>. Strip it.
459 (if (string-prefix? "LD_ORIGIN_PATH=" str)
460 (string-drop str (+ 1 (string-index str #\newline)))
461 str))
462 ((string-ci=? "darwin" sysname)
463 ;; MacOS appends '__CF_USER_TEXT_ENCODING' for every program. Strip
464 ;; it.
465 (let ((pos (string-contains str "__CF_USER_TEXT_ENCODING=")))
466 (if pos
467 (string-drop-right str (- (string-length str) pos))
468 str)))
469 (else
470 str))))))
471
472 (pass-if-equal "ls /proc/self/fd"
473 "0\n1\n2\n3\n" ;fourth FD is for /proc/self/fd
474 (if (file-exists? "/proc/self/fd") ;Linux
475 (let* ((input+output (pipe))
476 (pid (spawn "ls" '("ls" "/proc/self/fd")
477 #:output (cdr input+output))))
478 (close-port (cdr input+output))
479 (let ((str (get-string-all (car input+output))))
480 (close-port (car input+output))
481 (waitpid pid)
482 str))
483 (throw 'unresolved)))
484
485 (pass-if-equal "file not found"
486 ENOENT
487 ;; Actually "skip-if-posix_spawn-replaced" would be preferred, since the
488 ;; behavior below is not implemented in gnulib, but I do not know how to
489 ;; write such test. However due to posix_spawnp being buggy on macOS it is
490 ;; always replaced, so whatever.
491 (skip-on-darwin)
492 (skip-on-freebsd)
493 (catch 'system-error
494 (lambda ()
495 (spawn "this-does-not-exist" '("nope")
496 #:search-path? #f))
497 (lambda args
498 (system-error-errno args)))))
499
500 ;;
501 ;; crypt
502 ;;
503
504 (with-test-prefix "crypt"
505
506 (pass-if "basic usage"
507 (if (not (defined? 'crypt))
508 (throw 'unsupported)
509 (string? (crypt "pass" "abcdefg"))))
510
511 (pass-if "crypt invalid salt on glibc"
512 (begin
513 (unless (defined? 'crypt)
514 (throw 'unsupported))
515 (unless (string-contains %host-type "-gnu")
516 (throw 'unresolved))
517 (catch 'system-error
518 (lambda ()
519 ;; This used to deadlock on glibc while trying to throw to
520 ;; 'system-error'. This test uses the special
521 ;; interpretation of the salt that glibc does;
522 ;; specifically, we pass a salt that's probably
523 ;; syntactically invalid here. Note, whether it's invalid
524 ;; or not is system-defined, so it's possible it just works.
525 (string? (crypt "pass" "$X$abc")))
526 (lambda _ #t)))))