gnu: gdm: Fix config file path.
[guix.git] / tests / syscalls.scm
blob2b5c4c3be11b0b0075d555f5ea9034aaa81f9d4a
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
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/>.
20 (define-module (test-syscalls)
21   #:use-module (guix utils)
22   #:use-module (guix build syscalls)
23   #:use-module (gnu build linux-container)
24   #:use-module (srfi srfi-1)
25   #:use-module (srfi srfi-26)
26   #:use-module (srfi srfi-64)
27   #:use-module (system foreign)
28   #:use-module ((ice-9 ftw) #:select (scandir))
29   #:use-module (ice-9 match))
31 ;; Test the (guix build syscalls) module, although there's not much that can
32 ;; actually be tested without being root.
34 (define temp-file
35   (string-append "t-utils-" (number->string (getpid))))
38 (test-begin "syscalls")
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)))
48 (test-assert "umount, ENOENT/EPERM"
49   (catch 'system-error
50     (lambda ()
51       (umount "/does-not-exist")
52       #f)
53     (lambda args
54       ;; Both return values have been encountered in the wild.
55       (memv (system-error-errno args) (list EPERM ENOENT)))))
57 (test-assert "mount-points"
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")))
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)))))
71 (test-assert "swapoff, ENOENT/EINVAL/EPERM"
72   (catch 'system-error
73     (lambda ()
74       (swapoff "/does-not-exist")
75       #f)
76     (lambda args
77       (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
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))))
87 (test-equal "statfs, ENOENT"
88   ENOENT
89   (catch 'system-error
90     (lambda ()
91       (statfs "/does-not-exist"))
92     (compose system-error-errno list)))
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)))))
102 (define (user-namespace pid)
103   (string-append "/proc/" (number->string pid) "/ns/user"))
105 (define perform-container-tests?
106   (and (user-namespace-supported?)
107        (unprivileged-user-namespace-supported?)))
109 (unless perform-container-tests?
110   (test-skip 1))
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))))))))
122 (unless perform-container-tests?
123   (test-skip 1))
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))))))))
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"))
155   (test-skip 1))
156 (test-equal "pivot-root"
157   #t
158   (match (pipe)
159     ((in . out)
160      (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
161        (0
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))))
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))))))))
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))))
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)))
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))))))))
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<?))))
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)
272        ;; Write the real answer.
273        (seek file 0 SEEK_SET)
274        (truncate-file file 0)
275        (write 42 file)
276        (force-output file)
278        ;; Unlock, which should let the child continue.
279        (fcntl-flock file 'unlock)
281        (match (waitpid pid)
282          ((_  . status)
283           (let ((result (status:exit-val status)))
284             (close-port file)
285             result)))))))
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)
298             ;; Wait for the green light.
299             (read-char input)
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)
318           ;; Tell the child to continue.
319           (write 'green-light output)
320           (force-output output)
322           (match (waitpid pid)
323             ((_  . status)
324              (let ((result (status:exit-val status)))
325                (fcntl-flock file 'unlock)
326                (close-port file)
327                result)))))))))
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)))
337 (test-assert "all-network-interface-names"
338   (match (all-network-interface-names)
339     (((? string? names) ..1)
340      (member "lo" names))))
342 (test-assert "network-interface-names"
343   (match (network-interface-names)
344     (((? string? names) ..1)
345      (lset<= string=? names (all-network-interface-names)))))
347 (test-assert "network-interface-flags"
348   (let* ((sock  (socket AF_INET SOCK_STREAM 0))
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))))))
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)))))
364 (test-equal "loopback-network-interface-running?"
365   ENODEV
366   (and (network-interface-running? "lo")
367        (catch 'system-error
368          (lambda ()
369            (network-interface-running? "nonexistent")
370            #f)
371          (lambda args
372            (system-error-errno args)))))
374 (test-skip (if (zero? (getuid)) 1 0))
375 (test-assert "set-network-interface-flags"
376   (let ((sock (socket AF_INET SOCK_STREAM 0)))
377     (catch 'system-error
378       (lambda ()
379         (set-network-interface-flags sock "lo" IFF_UP))
380       (lambda args
381         (close-port sock)
382         ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
383         (memv (system-error-errno args) (list EPERM EACCES))))))
385 (test-equal "network-interface-address lo"
386   (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
387   (let* ((sock (socket AF_INET SOCK_STREAM 0))
388          (addr (network-interface-address sock "lo")))
389     (close-port sock)
390     addr))
392 (test-skip (if (zero? (getuid)) 1 0))
393 (test-assert "set-network-interface-address"
394   (let ((sock (socket AF_INET SOCK_STREAM 0)))
395     (catch 'system-error
396       (lambda ()
397         (set-network-interface-address sock "nonexistent"
398                                        (make-socket-address
399                                         AF_INET
400                                         (inet-pton AF_INET "127.12.14.15")
401                                         0)))
402       (lambda args
403         (close-port sock)
404         ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
405         (memv (system-error-errno args) (list EPERM EACCES))))))
407 (test-equal "network-interface-netmask lo"
408   (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
409   (let* ((sock (socket AF_INET SOCK_STREAM 0))
410          (addr (network-interface-netmask sock "lo")))
411     (close-port sock)
412     addr))
414 (test-skip (if (zero? (getuid)) 1 0))
415 (test-assert "set-network-interface-netmask"
416   (let ((sock (socket AF_INET SOCK_STREAM 0)))
417     (catch 'system-error
418       (lambda ()
419         (set-network-interface-netmask sock "nonexistent"
420                                        (make-socket-address
421                                         AF_INET
422                                         (inet-pton AF_INET "255.0.0.0")
423                                         0)))
424       (lambda args
425         (close-port sock)
426         (memv (system-error-errno args) (list EPERM EACCES))))))
428 (test-equal "network-interfaces returns one or more interfaces"
429   '(#t #t #t)
430   (match (network-interfaces)
431     ((interfaces ..1)
432      (list (every interface? interfaces)
433            (every string? (map interface-name interfaces))
434            (every (lambda (sockaddr)
435                     ;; Sometimes interfaces have no associated address.
436                     (or (vector? sockaddr)
437                         (not sockaddr)))
438                   (map interface-address interfaces))))))
440 (test-equal "network-interfaces returns \"lo\""
441   (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
442   (match (filter (lambda (interface)
443                    (string=? "lo" (interface-name interface)))
444                  (network-interfaces))
445     ((loopbacks ..1)
446      (list (every (lambda (lo)
447                     (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
448                   loopbacks)
449            (match (find (lambda (lo)
450                           (= AF_INET (sockaddr:fam (interface-address lo))))
451                         loopbacks)
452              (#f #f)
453              (lo (interface-address lo)))))))
455 (test-skip (if (zero? (getuid)) 1 0))
456 (test-assert "add-network-route/gateway"
457   (let ((sock    (socket AF_INET SOCK_STREAM 0))
458         (gateway (make-socket-address AF_INET
459                                       (inet-pton AF_INET "192.168.0.1")
460                                       0)))
461     (catch 'system-error
462       (lambda ()
463         (add-network-route/gateway sock gateway))
464       (lambda args
465         (close-port sock)
466         (memv (system-error-errno args) (list EPERM EACCES))))))
468 (test-skip (if (zero? (getuid)) 1 0))
469 (test-assert "delete-network-route"
470   (let ((sock        (socket AF_INET SOCK_STREAM 0))
471         (destination (make-socket-address AF_INET INADDR_ANY 0)))
472     (catch 'system-error
473       (lambda ()
474         (delete-network-route sock destination))
475       (lambda args
476         (close-port sock)
477         (memv (system-error-errno args) (list EPERM EACCES))))))
479 (test-equal "tcgetattr ENOTTY"
480   ENOTTY
481   (catch 'system-error
482     (lambda ()
483       (call-with-input-file "/dev/null"
484         (lambda (port)
485           (tcgetattr (fileno port)))))
486     (compose system-error-errno list)))
488 (test-skip (if (and (file-exists? "/proc/self/fd/0")
489                     (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
490                0
491                2))
493 (test-assert "tcgetattr"
494   (let ((termios (tcgetattr 0)))
495     (and (termios? termios)
496          (> (termios-input-speed termios) 0)
497          (> (termios-output-speed termios) 0))))
499 (test-assert "tcsetattr"
500   (let ((first (tcgetattr 0)))
501     (tcsetattr 0 (tcsetattr-action TCSANOW) first)
502     (equal? first (tcgetattr 0))))
504 (test-assert "terminal-window-size ENOTTY"
505   (call-with-input-file "/dev/null"
506     (lambda (port)
507       (catch 'system-error
508         (lambda ()
509           (terminal-window-size port))
510         (lambda args
511           ;; Accept EINVAL, which some old Linux versions might return.
512           (memv (system-error-errno args)
513                 (list ENOTTY EINVAL)))))))
515 (test-assert "terminal-columns"
516   (> (terminal-columns) 0))
518 (test-assert "terminal-columns non-file port"
519   (> (terminal-columns (open-input-string "Join us now, share the software!"))
520      0))
522 (test-assert "utmpx-entries"
523   (match (utmpx-entries)
524     (((? utmpx? entries) ...)
525      (every (lambda (entry)
526               (match (utmpx-user entry)
527                 ((? string?)
528                  (or (eqv? (login-type BOOT_TIME) (utmpx-login-type entry))
529                      (> (utmpx-pid entry) 0)))
530                 (#f                               ;might be DEAD_PROCESS
531                  #t)))
532             entries))))
534 (test-assert "read-utmpx, EOF"
535   (eof-object? (read-utmpx (%make-void-port "r"))))
537 (unless (access? "/var/run/utmpx" O_RDONLY)
538   (test-skip 1))
539 (test-assert "read-utmpx"
540   (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
541     (or (utmpx? result) (eof-object? result))))
543 (test-end)
545 (false-if-exception (delete-file temp-file))