gnu: et: Fix typo in description.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
15030972 2;;; Copyright © 2014, 2015, 2016, 2017 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
715fc9d4
LC
63(test-assert "swapon, ENOENT/EPERM"
64 (catch 'system-error
65 (lambda ()
66 (swapon "/does-not-exist")
67 #f)
68 (lambda args
69 (memv (system-error-errno args) (list EPERM ENOENT)))))
70
2793c0fb 71(test-assert "swapoff, ENOENT/EINVAL/EPERM"
715fc9d4
LC
72 (catch 'system-error
73 (lambda ()
74 (swapoff "/does-not-exist")
75 #f)
76 (lambda args
2793c0fb 77 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
715fc9d4 78
b4abdeb6
DT
79(test-assert "mkdtemp!"
80 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
81 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
82 (and (file-exists? dir)
83 (begin
84 (rmdir dir)
85 #t))))
86
a1f70878
LC
87(test-equal "statfs, ENOENT"
88 ENOENT
89 (catch 'system-error
90 (lambda ()
91 (statfs "/does-not-exist"))
92 (compose system-error-errno list)))
93
94(test-assert "statfs"
95 (let ((fs (statfs "/")))
96 (and (file-system? fs)
97 (> (file-system-block-size fs) 0)
98 (>= (file-system-blocks-available fs) 0)
99 (>= (file-system-blocks-free fs)
100 (file-system-blocks-available fs)))))
101
8950ed11
DT
102(define (user-namespace pid)
103 (string-append "/proc/" (number->string pid) "/ns/user"))
104
b7d48312
DT
105(define perform-container-tests?
106 (and (user-namespace-supported?)
107 (unprivileged-user-namespace-supported?)))
108
109(unless perform-container-tests?
b62a3ebc 110 (test-skip 1))
8950ed11
DT
111(test-assert "clone"
112 (match (clone (logior CLONE_NEWUSER SIGCHLD))
113 (0 (primitive-exit 42))
114 (pid
115 ;; Check if user namespaces are different.
116 (and (not (equal? (readlink (user-namespace pid))
117 (readlink (user-namespace (getpid)))))
118 (match (waitpid pid)
119 ((_ . status)
120 (= 42 (status:exit-val status))))))))
121
b7d48312 122(unless perform-container-tests?
b62a3ebc 123 (test-skip 1))
43ace6ea
DT
124(test-assert "setns"
125 (match (clone (logior CLONE_NEWUSER SIGCHLD))
126 (0 (primitive-exit 0))
127 (clone-pid
128 (match (pipe)
129 ((in . out)
130 (match (primitive-fork)
131 (0
132 (close in)
133 ;; Join the user namespace.
134 (call-with-input-file (user-namespace clone-pid)
135 (lambda (port)
136 (setns (port->fdes port) 0)))
137 (write 'done out)
138 (close out)
139 (primitive-exit 0))
140 (fork-pid
141 (close out)
142 ;; Wait for the child process to join the namespace.
143 (read in)
144 (let ((result (and (equal? (readlink (user-namespace clone-pid))
145 (readlink (user-namespace fork-pid))))))
146 ;; Clean up.
147 (waitpid clone-pid)
148 (waitpid fork-pid)
149 result))))))))
8950ed11 150
a91d75ec
LC
151;; XXX: Skip this test when running Linux > 4.7.5 to work around
152;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
153(when (or (not perform-container-tests?)
154 (version>? (utsname:release (uname)) "4.7.5"))
b62a3ebc 155 (test-skip 1))
fe9bdb58
LC
156(test-equal "pivot-root"
157 #t
df3ce5c1
DT
158 (match (pipe)
159 ((in . out)
160 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
161 (0
fe9bdb58
LC
162 (dynamic-wind
163 (const #t)
164 (lambda ()
165 (close in)
166 (call-with-temporary-directory
167 (lambda (root)
168 (let ((put-old (string-append root "/real-root")))
169 (mount "none" root "tmpfs")
170 (mkdir put-old)
171 (call-with-output-file (string-append root "/test")
172 (lambda (port)
173 (display "testing\n" port)))
174 (pivot-root root put-old)
175 ;; The test file should now be located inside the root directory.
176 (write (file-exists? "/test") out)
177 (close out)))))
178 (lambda ()
179 (primitive-exit 0))))
df3ce5c1
DT
180 (pid
181 (close out)
182 (let ((result (read in)))
183 (close in)
184 (and (zero? (match (waitpid pid)
185 ((_ . status)
186 (status:exit-val status))))
187 (eq? #t result))))))))
188
fa73c193
LC
189(test-equal "scandir*, ENOENT"
190 ENOENT
191 (catch 'system-error
192 (lambda ()
193 (scandir* "/does/not/exist"))
194 (lambda args
195 (system-error-errno args))))
196
197(test-equal "scandir*, ASCII file names"
198 (scandir (dirname (search-path %load-path "guix/base32.scm"))
199 (const #t) string<?)
200 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
201 (((names . properties) ...)
202 names)))
203
204(test-equal "scandir*, UTF-8 file names"
205 '("." ".." "α" "λ")
206 (call-with-temporary-directory
207 (lambda (directory)
208 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
209 ;; name to the system call.
210 (let ((creat (pointer->procedure int
211 (dynamic-func "creat" (dynamic-link))
212 (list '* int))))
213 (creat (string->pointer (string-append directory "/α")
214 "UTF-8")
215 #o644)
216 (creat (string->pointer (string-append directory "/λ")
217 "UTF-8")
218 #o644)
219 (let ((locale (setlocale LC_ALL)))
220 (dynamic-wind
221 (lambda ()
222 ;; Make sure that even in a C locale we get the right result.
223 (setlocale LC_ALL "C"))
224 (lambda ()
225 (match (scandir* directory)
226 (((names . properties) ...)
227 names)))
228 (lambda ()
229 (setlocale LC_ALL locale))))))))
230
231(test-assert "scandir*, properties"
232 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
233 (every (lambda (entry name)
234 (match entry
235 ((name2 . properties)
236 (and (string=? name2 name)
237 (let* ((full (string-append directory "/" name))
238 (stat (lstat full))
239 (inode (assoc-ref properties 'inode))
240 (type (assoc-ref properties 'type)))
241 (and (= inode (stat:ino stat))
242 (or (eq? type 'unknown)
243 (eq? type (stat:type stat)))))))))
244 (scandir* directory)
245 (scandir directory (const #t) string<?))))
246
4e0ea3eb
LC
247(false-if-exception (delete-file temp-file))
248(test-equal "fcntl-flock wait"
249 42 ; the child's exit status
250 (let ((file (open-file temp-file "w0b")))
251 ;; Acquire an exclusive lock.
252 (fcntl-flock file 'write-lock)
253 (match (primitive-fork)
254 (0
255 (dynamic-wind
256 (const #t)
257 (lambda ()
258 ;; Reopen FILE read-only so we can have a read lock.
259 (let ((file (open-file temp-file "r0b")))
260 ;; Wait until we can acquire the lock.
261 (fcntl-flock file 'read-lock)
262 (primitive-exit (read file)))
263 (primitive-exit 1))
264 (lambda ()
265 (primitive-exit 2))))
266 (pid
267 ;; Write garbage and wait.
268 (display "hello, world!" file)
269 (force-output file)
270 (sleep 1)
271
272 ;; Write the real answer.
273 (seek file 0 SEEK_SET)
274 (truncate-file file 0)
275 (write 42 file)
276 (force-output file)
277
278 ;; Unlock, which should let the child continue.
279 (fcntl-flock file 'unlock)
280
281 (match (waitpid pid)
282 ((_ . status)
283 (let ((result (status:exit-val status)))
284 (close-port file)
285 result)))))))
286
287(test-equal "fcntl-flock non-blocking"
288 EAGAIN ; the child's exit status
289 (match (pipe)
290 ((input . output)
291 (match (primitive-fork)
292 (0
293 (dynamic-wind
294 (const #t)
295 (lambda ()
296 (close-port output)
297
298 ;; Wait for the green light.
299 (read-char input)
300
301 ;; Open FILE read-only so we can have a read lock.
302 (let ((file (open-file temp-file "w0")))
303 (catch 'flock-error
304 (lambda ()
305 ;; This attempt should throw EAGAIN.
306 (fcntl-flock file 'write-lock #:wait? #f))
307 (lambda (key errno)
308 (primitive-exit (pk 'errno errno)))))
309 (primitive-exit -1))
310 (lambda ()
311 (primitive-exit -2))))
312 (pid
313 (close-port input)
314 (let ((file (open-file temp-file "w0")))
315 ;; Acquire an exclusive lock.
316 (fcntl-flock file 'write-lock)
317
318 ;; Tell the child to continue.
319 (write 'green-light output)
320 (force-output output)
321
322 (match (waitpid pid)
323 ((_ . status)
324 (let ((result (status:exit-val status)))
325 (fcntl-flock file 'unlock)
326 (close-port file)
327 result)))))))))
328
aa401f9b
LC
329(test-equal "set-thread-name"
330 "Syscall Test"
331 (let ((name (thread-name)))
332 (set-thread-name "Syscall Test")
333 (let ((new-name (thread-name)))
334 (set-thread-name name)
335 new-name)))
336
b89e7405
LC
337(test-assert "all-network-interface-names"
338 (match (all-network-interface-names)
4d54785c
LC
339 (((? string? names) ..1)
340 (member "lo" names))))
341
b89e7405
LC
342(test-assert "network-interface-names"
343 (match (network-interface-names)
7585016f 344 (((? string? names) ..1)
b89e7405 345 (lset<= string=? names (all-network-interface-names)))))
7585016f 346
973eea34 347(test-assert "network-interface-flags"
c9bf64d6 348 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
349 (flags (network-interface-flags sock "lo")))
350 (close-port sock)
351 (and (not (zero? (logand flags IFF_LOOPBACK)))
352 (not (zero? (logand flags IFF_UP))))))
353
354(test-equal "loopback-network-interface?"
355 ENODEV
356 (and (loopback-network-interface? "lo")
357 (catch 'system-error
358 (lambda ()
359 (loopback-network-interface? "nonexistent")
360 #f)
361 (lambda args
362 (system-error-errno args)))))
363
c9bf64d6 364(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 365(test-assert "set-network-interface-flags"
c9bf64d6
LC
366 (let ((sock (socket AF_INET SOCK_STREAM 0)))
367 (catch 'system-error
368 (lambda ()
369 (set-network-interface-flags sock "lo" IFF_UP))
370 (lambda args
371 (close-port sock)
d35c5e29
LC
372 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
373 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
374
375(test-equal "network-interface-address lo"
376 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
377 (let* ((sock (socket AF_INET SOCK_STREAM 0))
378 (addr (network-interface-address sock "lo")))
379 (close-port sock)
380 addr))
381
54e515eb 382(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 383(test-assert "set-network-interface-address"
c9bf64d6
LC
384 (let ((sock (socket AF_INET SOCK_STREAM 0)))
385 (catch 'system-error
386 (lambda ()
387 (set-network-interface-address sock "nonexistent"
388 (make-socket-address
389 AF_INET
390 (inet-pton AF_INET "127.12.14.15")
391 0)))
392 (lambda args
393 (close-port sock)
d35c5e29
LC
394 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
395 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 396
67e5f3b7
LC
397(test-equal "network-interface-netmask lo"
398 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
399 (let* ((sock (socket AF_INET SOCK_STREAM 0))
400 (addr (network-interface-netmask sock "lo")))
401 (close-port sock)
402 addr))
403
404(test-skip (if (zero? (getuid)) 1 0))
405(test-assert "set-network-interface-netmask"
406 (let ((sock (socket AF_INET SOCK_STREAM 0)))
407 (catch 'system-error
408 (lambda ()
409 (set-network-interface-netmask sock "nonexistent"
410 (make-socket-address
411 AF_INET
412 (inet-pton AF_INET "255.0.0.0")
413 0)))
414 (lambda args
415 (close-port sock)
416 (memv (system-error-errno args) (list EPERM EACCES))))))
417
e7f5691d
LC
418(test-equal "network-interfaces returns one or more interfaces"
419 '(#t #t #t)
420 (match (network-interfaces)
421 ((interfaces ..1)
422 (list (every interface? interfaces)
423 (every string? (map interface-name interfaces))
7adbe85e
LC
424 (every (lambda (sockaddr)
425 ;; Sometimes interfaces have no associated address.
426 (or (vector? sockaddr)
427 (not sockaddr)))
428 (map interface-address interfaces))))))
e7f5691d
LC
429
430(test-equal "network-interfaces returns \"lo\""
431 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
432 (match (filter (lambda (interface)
433 (string=? "lo" (interface-name interface)))
434 (network-interfaces))
435 ((loopbacks ..1)
436 (list (every (lambda (lo)
437 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
438 loopbacks)
439 (match (find (lambda (lo)
440 (= AF_INET (sockaddr:fam (interface-address lo))))
441 loopbacks)
442 (#f #f)
443 (lo (interface-address lo)))))))
444
9e38e3cf
LC
445(test-skip (if (zero? (getuid)) 1 0))
446(test-assert "add-network-route/gateway"
447 (let ((sock (socket AF_INET SOCK_STREAM 0))
448 (gateway (make-socket-address AF_INET
449 (inet-pton AF_INET "192.168.0.1")
450 0)))
451 (catch 'system-error
452 (lambda ()
453 (add-network-route/gateway sock gateway))
454 (lambda args
455 (close-port sock)
456 (memv (system-error-errno args) (list EPERM EACCES))))))
457
458(test-skip (if (zero? (getuid)) 1 0))
459(test-assert "delete-network-route"
460 (let ((sock (socket AF_INET SOCK_STREAM 0))
461 (destination (make-socket-address AF_INET INADDR_ANY 0)))
462 (catch 'system-error
463 (lambda ()
464 (delete-network-route sock destination))
465 (lambda args
466 (close-port sock)
467 (memv (system-error-errno args) (list EPERM EACCES))))))
468
ae4ff9f3
LC
469(test-equal "tcgetattr ENOTTY"
470 ENOTTY
471 (catch 'system-error
472 (lambda ()
473 (call-with-input-file "/dev/null"
474 (lambda (port)
475 (tcgetattr (fileno port)))))
476 (compose system-error-errno list)))
477
478(test-skip (if (and (file-exists? "/proc/self/fd/0")
479 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
480 0
481 2))
482
483(test-assert "tcgetattr"
484 (let ((termios (tcgetattr 0)))
485 (and (termios? termios)
486 (> (termios-input-speed termios) 0)
487 (> (termios-output-speed termios) 0))))
488
489(test-assert "tcsetattr"
490 (let ((first (tcgetattr 0)))
a8f3424b 491 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
ae4ff9f3
LC
492 (equal? first (tcgetattr 0))))
493
5cd25aad 494(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
495 (call-with-input-file "/dev/null"
496 (lambda (port)
497 (catch 'system-error
498 (lambda ()
499 (terminal-window-size port))
500 (lambda args
5cd25aad
LC
501 ;; Accept EINVAL, which some old Linux versions might return.
502 (memv (system-error-errno args)
503 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
504
505(test-assert "terminal-columns"
506 (> (terminal-columns) 0))
507
6d2b4391
LC
508(test-assert "terminal-columns non-file port"
509 (> (terminal-columns (open-input-string "Join us now, share the software!"))
510 0))
511
15030972
LC
512(test-assert "utmpx-entries"
513 (match (utmpx-entries)
514 (((? utmpx? entries) ...)
515 (every (lambda (entry)
516 (match (utmpx-user entry)
517 ((? string?)
a1a8b7f2
LC
518 (or (eqv? (login-type BOOT_TIME) (utmpx-login-type entry))
519 (> (utmpx-pid entry) 0)))
15030972
LC
520 (#f ;might be DEAD_PROCESS
521 #t)))
522 entries))))
523
3483f004
LC
524(test-assert "read-utmpx, EOF"
525 (eof-object? (read-utmpx (%make-void-port "r"))))
526
527(unless (access? "/var/run/utmpx" O_RDONLY)
f18eded8 528 (test-skip 1))
3483f004
LC
529(test-assert "read-utmpx"
530 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
531 (or (utmpx? result) (eof-object? result))))
532
29fa45f4 533(test-end)
4e0ea3eb
LC
534
535(false-if-exception (delete-file temp-file))