gnu: easyrpg-player: Update to 0.6.2.2.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
1deca767 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
b4abdeb6 3;;; Copyright © 2015 David Thompson <davet@gnu.org>
cb21c14b 4;;; Copyright © 2020 Simon South <simon@simonsouth.net>
ea924134 5;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
29fa45f4
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (test-syscalls)
df3ce5c1 23 #:use-module (guix utils)
29fa45f4 24 #:use-module (guix build syscalls)
b7d48312 25 #:use-module (gnu build linux-container)
4d54785c 26 #:use-module (srfi srfi-1)
381ac93b 27 #:use-module (srfi srfi-26)
7585016f 28 #:use-module (srfi srfi-64)
fa73c193
LC
29 #:use-module (system foreign)
30 #:use-module ((ice-9 ftw) #:select (scandir))
7585016f 31 #:use-module (ice-9 match))
29fa45f4
LC
32
33;; Test the (guix build syscalls) module, although there's not much that can
34;; actually be tested without being root.
35
4e0ea3eb
LC
36(define temp-file
37 (string-append "t-utils-" (number->string (getpid))))
38
39\f
29fa45f4
LC
40(test-begin "syscalls")
41
42(test-equal "mount, ENOENT"
43 ENOENT
44 (catch 'system-error
45 (lambda ()
46 (mount "/dev/null" "/does-not-exist" "ext2")
47 #f)
48 (compose system-error-errno list)))
49
35066aa5 50(test-assert "umount, ENOENT/EPERM"
29fa45f4
LC
51 (catch 'system-error
52 (lambda ()
53 (umount "/does-not-exist")
54 #f)
35066aa5
LC
55 (lambda args
56 ;; Both return values have been encountered in the wild.
57 (memv (system-error-errno args) (list EPERM ENOENT)))))
29fa45f4 58
ccea821b 59(test-assert "mount-points"
381ac93b
LC
60 ;; Reportedly "/" is not always listed as a mount point, so check a few
61 ;; others (see <http://bugs.gnu.org/20261>.)
62 (any (cute member <> (mount-points))
63 '("/" "/proc" "/sys" "/dev")))
ccea821b 64
25c7ff6a
LC
65(false-if-exception (delete-file temp-file))
66(test-equal "utime with AT_SYMLINK_NOFOLLOW"
67 '(0 0)
68 (begin
69 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
70 ;; define as of Guile 2.2.4.
71 (symlink "/nowhere" temp-file)
72 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
73 (let ((st (lstat temp-file)))
74 (delete-file temp-file)
75 ;; Note: 'utimensat' does not change 'ctime'.
76 (list (stat:mtime st) (stat:atime st)))))
77
cb21c14b 78(test-assert "swapon, ENOSYS/ENOENT/EPERM"
715fc9d4
LC
79 (catch 'system-error
80 (lambda ()
81 (swapon "/does-not-exist")
82 #f)
83 (lambda args
cb21c14b 84 (memv (system-error-errno args) (list EPERM ENOENT ENOSYS)))))
715fc9d4 85
cb21c14b 86(test-assert "swapoff, ENOSYS/ENOENT/EINVAL/EPERM"
715fc9d4
LC
87 (catch 'system-error
88 (lambda ()
89 (swapoff "/does-not-exist")
90 #f)
91 (lambda args
cb21c14b 92 (memv (system-error-errno args) (list EPERM EINVAL ENOENT ENOSYS)))))
715fc9d4 93
b4abdeb6
DT
94(test-assert "mkdtemp!"
95 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
96 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
97 (and (file-exists? dir)
98 (begin
99 (rmdir dir)
100 #t))))
101
a1f70878
LC
102(test-equal "statfs, ENOENT"
103 ENOENT
104 (catch 'system-error
105 (lambda ()
106 (statfs "/does-not-exist"))
107 (compose system-error-errno list)))
108
109(test-assert "statfs"
110 (let ((fs (statfs "/")))
111 (and (file-system? fs)
112 (> (file-system-block-size fs) 0)
113 (>= (file-system-blocks-available fs) 0)
114 (>= (file-system-blocks-free fs)
115 (file-system-blocks-available fs)))))
116
8950ed11
DT
117(define (user-namespace pid)
118 (string-append "/proc/" (number->string pid) "/ns/user"))
119
b7d48312
DT
120(define perform-container-tests?
121 (and (user-namespace-supported?)
122 (unprivileged-user-namespace-supported?)))
123
124(unless perform-container-tests?
b62a3ebc 125 (test-skip 1))
8950ed11
DT
126(test-assert "clone"
127 (match (clone (logior CLONE_NEWUSER SIGCHLD))
128 (0 (primitive-exit 42))
129 (pid
130 ;; Check if user namespaces are different.
131 (and (not (equal? (readlink (user-namespace pid))
132 (readlink (user-namespace (getpid)))))
133 (match (waitpid pid)
134 ((_ . status)
135 (= 42 (status:exit-val status))))))))
136
b7d48312 137(unless perform-container-tests?
b62a3ebc 138 (test-skip 1))
43ace6ea
DT
139(test-assert "setns"
140 (match (clone (logior CLONE_NEWUSER SIGCHLD))
141 (0 (primitive-exit 0))
142 (clone-pid
143 (match (pipe)
144 ((in . out)
145 (match (primitive-fork)
146 (0
147 (close in)
148 ;; Join the user namespace.
149 (call-with-input-file (user-namespace clone-pid)
150 (lambda (port)
151 (setns (port->fdes port) 0)))
152 (write 'done out)
153 (close out)
154 (primitive-exit 0))
155 (fork-pid
156 (close out)
157 ;; Wait for the child process to join the namespace.
158 (read in)
159 (let ((result (and (equal? (readlink (user-namespace clone-pid))
160 (readlink (user-namespace fork-pid))))))
161 ;; Clean up.
162 (waitpid clone-pid)
163 (waitpid fork-pid)
164 result))))))))
8950ed11 165
1deca767 166(when (not perform-container-tests?)
b62a3ebc 167 (test-skip 1))
fe9bdb58 168(test-equal "pivot-root"
1deca767
LC
169 'success!
170 (match (socketpair AF_UNIX SOCK_STREAM 0)
171 ((parent . child)
df3ce5c1
DT
172 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
173 (0
fe9bdb58
LC
174 (dynamic-wind
175 (const #t)
176 (lambda ()
1deca767 177 (close parent)
fe9bdb58
LC
178 (call-with-temporary-directory
179 (lambda (root)
1deca767
LC
180 (display "ready\n" child)
181 (read child) ;wait for "go!"
fe9bdb58
LC
182 (let ((put-old (string-append root "/real-root")))
183 (mount "none" root "tmpfs")
184 (mkdir put-old)
185 (call-with-output-file (string-append root "/test")
186 (lambda (port)
187 (display "testing\n" port)))
188 (pivot-root root put-old)
189 ;; The test file should now be located inside the root directory.
1deca767
LC
190 (write (and (file-exists? "/test") 'success!) child)
191 (close child)))))
fe9bdb58
LC
192 (lambda ()
193 (primitive-exit 0))))
df3ce5c1 194 (pid
1deca767
LC
195 (close child)
196 (match (read parent)
197 ('ready
198 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
199 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
200 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
201 (lambda (port)
202 (display "deny" port)))
203 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
204 (lambda (port)
205 (format port "0 ~d 1" (getuid))))
206 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
207 (lambda (port)
208 (format port "0 ~d 1" (getgid))))
209 (display "go!\n" parent)
210 (let ((result (read parent)))
211 (close parent)
212 (and (zero? (match (waitpid pid)
213 ((_ . status)
214 (status:exit-val status))))
215 result)))))))))
df3ce5c1 216
fa73c193
LC
217(test-equal "scandir*, ENOENT"
218 ENOENT
219 (catch 'system-error
220 (lambda ()
221 (scandir* "/does/not/exist"))
222 (lambda args
223 (system-error-errno args))))
224
225(test-equal "scandir*, ASCII file names"
226 (scandir (dirname (search-path %load-path "guix/base32.scm"))
227 (const #t) string<?)
228 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
229 (((names . properties) ...)
230 names)))
231
232(test-equal "scandir*, UTF-8 file names"
233 '("." ".." "α" "λ")
234 (call-with-temporary-directory
235 (lambda (directory)
236 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
237 ;; name to the system call.
238 (let ((creat (pointer->procedure int
239 (dynamic-func "creat" (dynamic-link))
240 (list '* int))))
241 (creat (string->pointer (string-append directory "/α")
242 "UTF-8")
243 #o644)
244 (creat (string->pointer (string-append directory "/λ")
245 "UTF-8")
246 #o644)
247 (let ((locale (setlocale LC_ALL)))
248 (dynamic-wind
249 (lambda ()
250 ;; Make sure that even in a C locale we get the right result.
251 (setlocale LC_ALL "C"))
252 (lambda ()
253 (match (scandir* directory)
254 (((names . properties) ...)
255 names)))
256 (lambda ()
257 (setlocale LC_ALL locale))))))))
258
259(test-assert "scandir*, properties"
260 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
261 (every (lambda (entry name)
262 (match entry
263 ((name2 . properties)
264 (and (string=? name2 name)
265 (let* ((full (string-append directory "/" name))
266 (stat (lstat full))
267 (inode (assoc-ref properties 'inode))
268 (type (assoc-ref properties 'type)))
269 (and (= inode (stat:ino stat))
270 (or (eq? type 'unknown)
271 (eq? type (stat:type stat)))))))))
272 (scandir* directory)
273 (scandir directory (const #t) string<?))))
274
df058423
JN
275(false-if-exception (delete-file temp-file))
276(test-assert "getxattr, setxattr"
277 (let ((key "user.translator")
278 (value "/hurd/pfinet\0")
279 (file (open-file temp-file "w0")))
ea924134
MO
280 (catch 'system-error
281 (lambda ()
282 (setxattr temp-file key value)
283 (string=? (getxattr temp-file key) value))
284 (lambda args
285 ;; Accept ENOTSUP, if the file-system does not support extended user
286 ;; attributes.
287 (memv (system-error-errno args) (list ENOTSUP))))))
df058423 288
4e0ea3eb
LC
289(false-if-exception (delete-file temp-file))
290(test-equal "fcntl-flock wait"
291 42 ; the child's exit status
292 (let ((file (open-file temp-file "w0b")))
293 ;; Acquire an exclusive lock.
294 (fcntl-flock file 'write-lock)
295 (match (primitive-fork)
296 (0
297 (dynamic-wind
298 (const #t)
299 (lambda ()
300 ;; Reopen FILE read-only so we can have a read lock.
301 (let ((file (open-file temp-file "r0b")))
302 ;; Wait until we can acquire the lock.
303 (fcntl-flock file 'read-lock)
304 (primitive-exit (read file)))
305 (primitive-exit 1))
306 (lambda ()
307 (primitive-exit 2))))
308 (pid
309 ;; Write garbage and wait.
310 (display "hello, world!" file)
311 (force-output file)
312 (sleep 1)
313
314 ;; Write the real answer.
315 (seek file 0 SEEK_SET)
316 (truncate-file file 0)
317 (write 42 file)
318 (force-output file)
319
320 ;; Unlock, which should let the child continue.
321 (fcntl-flock file 'unlock)
322
323 (match (waitpid pid)
324 ((_ . status)
325 (let ((result (status:exit-val status)))
326 (close-port file)
327 result)))))))
328
329(test-equal "fcntl-flock non-blocking"
330 EAGAIN ; the child's exit status
331 (match (pipe)
332 ((input . output)
333 (match (primitive-fork)
334 (0
335 (dynamic-wind
336 (const #t)
337 (lambda ()
338 (close-port output)
339
340 ;; Wait for the green light.
341 (read-char input)
342
343 ;; Open FILE read-only so we can have a read lock.
344 (let ((file (open-file temp-file "w0")))
345 (catch 'flock-error
346 (lambda ()
347 ;; This attempt should throw EAGAIN.
348 (fcntl-flock file 'write-lock #:wait? #f))
349 (lambda (key errno)
350 (primitive-exit (pk 'errno errno)))))
351 (primitive-exit -1))
352 (lambda ()
353 (primitive-exit -2))))
354 (pid
355 (close-port input)
356 (let ((file (open-file temp-file "w0")))
357 ;; Acquire an exclusive lock.
358 (fcntl-flock file 'write-lock)
359
360 ;; Tell the child to continue.
361 (write 'green-light output)
362 (force-output output)
363
364 (match (waitpid pid)
365 ((_ . status)
366 (let ((result (status:exit-val status)))
367 (fcntl-flock file 'unlock)
368 (close-port file)
369 result)))))))))
370
aa401f9b
LC
371(test-equal "set-thread-name"
372 "Syscall Test"
373 (let ((name (thread-name)))
374 (set-thread-name "Syscall Test")
375 (let ((new-name (thread-name)))
376 (set-thread-name name)
377 new-name)))
378
b89e7405
LC
379(test-assert "all-network-interface-names"
380 (match (all-network-interface-names)
4d54785c
LC
381 (((? string? names) ..1)
382 (member "lo" names))))
383
b89e7405 384(test-assert "network-interface-names"
5e113cf4
MB
385 (match (remove (lambda (interface)
386 ;; Ignore interface aliases since they don't show up in
387 ;; (all-network-interface-names).
388 (string-contains interface ":"))
389 (network-interface-names))
7585016f 390 (((? string? names) ..1)
b89e7405 391 (lset<= string=? names (all-network-interface-names)))))
7585016f 392
973eea34 393(test-assert "network-interface-flags"
c9bf64d6 394 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
395 (flags (network-interface-flags sock "lo")))
396 (close-port sock)
397 (and (not (zero? (logand flags IFF_LOOPBACK)))
398 (not (zero? (logand flags IFF_UP))))))
399
400(test-equal "loopback-network-interface?"
401 ENODEV
402 (and (loopback-network-interface? "lo")
403 (catch 'system-error
404 (lambda ()
405 (loopback-network-interface? "nonexistent")
406 #f)
407 (lambda args
408 (system-error-errno args)))))
409
0bc6fe32
DM
410(test-equal "loopback-network-interface-running?"
411 ENODEV
412 (and (network-interface-running? "lo")
413 (catch 'system-error
414 (lambda ()
415 (network-interface-running? "nonexistent")
416 #f)
417 (lambda args
418 (system-error-errno args)))))
419
c9bf64d6 420(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 421(test-assert "set-network-interface-flags"
c9bf64d6
LC
422 (let ((sock (socket AF_INET SOCK_STREAM 0)))
423 (catch 'system-error
424 (lambda ()
425 (set-network-interface-flags sock "lo" IFF_UP))
426 (lambda args
427 (close-port sock)
d35c5e29
LC
428 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
429 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
430
431(test-equal "network-interface-address lo"
432 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
433 (let* ((sock (socket AF_INET SOCK_STREAM 0))
434 (addr (network-interface-address sock "lo")))
435 (close-port sock)
436 addr))
437
54e515eb 438(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 439(test-assert "set-network-interface-address"
c9bf64d6
LC
440 (let ((sock (socket AF_INET SOCK_STREAM 0)))
441 (catch 'system-error
442 (lambda ()
443 (set-network-interface-address sock "nonexistent"
444 (make-socket-address
445 AF_INET
446 (inet-pton AF_INET "127.12.14.15")
447 0)))
448 (lambda args
449 (close-port sock)
d35c5e29
LC
450 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
451 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 452
67e5f3b7
LC
453(test-equal "network-interface-netmask lo"
454 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
455 (let* ((sock (socket AF_INET SOCK_STREAM 0))
456 (addr (network-interface-netmask sock "lo")))
457 (close-port sock)
458 addr))
459
460(test-skip (if (zero? (getuid)) 1 0))
461(test-assert "set-network-interface-netmask"
462 (let ((sock (socket AF_INET SOCK_STREAM 0)))
463 (catch 'system-error
464 (lambda ()
465 (set-network-interface-netmask sock "nonexistent"
466 (make-socket-address
467 AF_INET
468 (inet-pton AF_INET "255.0.0.0")
469 0)))
470 (lambda args
471 (close-port sock)
472 (memv (system-error-errno args) (list EPERM EACCES))))))
473
e7f5691d
LC
474(test-equal "network-interfaces returns one or more interfaces"
475 '(#t #t #t)
476 (match (network-interfaces)
477 ((interfaces ..1)
478 (list (every interface? interfaces)
479 (every string? (map interface-name interfaces))
7adbe85e
LC
480 (every (lambda (sockaddr)
481 ;; Sometimes interfaces have no associated address.
482 (or (vector? sockaddr)
483 (not sockaddr)))
484 (map interface-address interfaces))))))
e7f5691d
LC
485
486(test-equal "network-interfaces returns \"lo\""
487 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
488 (match (filter (lambda (interface)
489 (string=? "lo" (interface-name interface)))
490 (network-interfaces))
491 ((loopbacks ..1)
492 (list (every (lambda (lo)
493 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
494 loopbacks)
495 (match (find (lambda (lo)
496 (= AF_INET (sockaddr:fam (interface-address lo))))
497 loopbacks)
498 (#f #f)
499 (lo (interface-address lo)))))))
500
9e38e3cf
LC
501(test-skip (if (zero? (getuid)) 1 0))
502(test-assert "add-network-route/gateway"
503 (let ((sock (socket AF_INET SOCK_STREAM 0))
504 (gateway (make-socket-address AF_INET
505 (inet-pton AF_INET "192.168.0.1")
506 0)))
507 (catch 'system-error
508 (lambda ()
509 (add-network-route/gateway sock gateway))
510 (lambda args
511 (close-port sock)
512 (memv (system-error-errno args) (list EPERM EACCES))))))
513
514(test-skip (if (zero? (getuid)) 1 0))
515(test-assert "delete-network-route"
516 (let ((sock (socket AF_INET SOCK_STREAM 0))
517 (destination (make-socket-address AF_INET INADDR_ANY 0)))
518 (catch 'system-error
519 (lambda ()
520 (delete-network-route sock destination))
521 (lambda args
522 (close-port sock)
523 (memv (system-error-errno args) (list EPERM EACCES))))))
524
ae4ff9f3
LC
525(test-equal "tcgetattr ENOTTY"
526 ENOTTY
527 (catch 'system-error
528 (lambda ()
529 (call-with-input-file "/dev/null"
530 (lambda (port)
531 (tcgetattr (fileno port)))))
532 (compose system-error-errno list)))
533
534(test-skip (if (and (file-exists? "/proc/self/fd/0")
535 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
536 0
537 2))
538
539(test-assert "tcgetattr"
540 (let ((termios (tcgetattr 0)))
541 (and (termios? termios)
542 (> (termios-input-speed termios) 0)
543 (> (termios-output-speed termios) 0))))
544
545(test-assert "tcsetattr"
546 (let ((first (tcgetattr 0)))
a8f3424b 547 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
ae4ff9f3
LC
548 (equal? first (tcgetattr 0))))
549
5cd25aad 550(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
551 (call-with-input-file "/dev/null"
552 (lambda (port)
553 (catch 'system-error
554 (lambda ()
555 (terminal-window-size port))
556 (lambda args
5cd25aad
LC
557 ;; Accept EINVAL, which some old Linux versions might return.
558 (memv (system-error-errno args)
559 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
560
561(test-assert "terminal-columns"
562 (> (terminal-columns) 0))
563
6d2b4391
LC
564(test-assert "terminal-columns non-file port"
565 (> (terminal-columns (open-input-string "Join us now, share the software!"))
566 0))
567
4593f5a6
LC
568(test-assert "terminal-rows"
569 (> (terminal-rows) 0))
570
15030972
LC
571(test-assert "utmpx-entries"
572 (match (utmpx-entries)
573 (((? utmpx? entries) ...)
574 (every (lambda (entry)
575 (match (utmpx-user entry)
576 ((? string?)
4aac8d05
LC
577 ;; Ensure we have a valid PID for those entries where it
578 ;; makes sense.
579 (or (not (memv (utmpx-login-type entry)
580 (list (login-type INIT_PROCESS)
581 (login-type LOGIN_PROCESS)
582 (login-type USER_PROCESS))))
a1a8b7f2 583 (> (utmpx-pid entry) 0)))
15030972
LC
584 (#f ;might be DEAD_PROCESS
585 #t)))
586 entries))))
587
3483f004
LC
588(test-assert "read-utmpx, EOF"
589 (eof-object? (read-utmpx (%make-void-port "r"))))
590
591(unless (access? "/var/run/utmpx" O_RDONLY)
f18eded8 592 (test-skip 1))
3483f004
LC
593(test-assert "read-utmpx"
594 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
595 (or (utmpx? result) (eof-object? result))))
596
5e5f7167
LC
597(when (zero? (getuid))
598 (test-skip 1))
599(test-equal "add-to-entropy-count"
600 EPERM
601 (call-with-output-file "/dev/urandom"
602 (lambda (port)
603 (catch 'system-error
604 (lambda ()
605 (add-to-entropy-count port 77)
606 #f)
607 (lambda args
608 (system-error-errno args))))))
609
29fa45f4 610(test-end)
4e0ea3eb
LC
611
612(false-if-exception (delete-file temp-file))