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