syscalls: Implement arrays in 'define-c-struct' and use it.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
7adbe85e 2;;; Copyright © 2014, 2015, 2016 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
LC
26 #:use-module (srfi srfi-64)
27 #:use-module (ice-9 match))
29fa45f4
LC
28
29;; Test the (guix build syscalls) module, although there's not much that can
30;; actually be tested without being root.
31
32(test-begin "syscalls")
33
34(test-equal "mount, ENOENT"
35 ENOENT
36 (catch 'system-error
37 (lambda ()
38 (mount "/dev/null" "/does-not-exist" "ext2")
39 #f)
40 (compose system-error-errno list)))
41
35066aa5 42(test-assert "umount, ENOENT/EPERM"
29fa45f4
LC
43 (catch 'system-error
44 (lambda ()
45 (umount "/does-not-exist")
46 #f)
35066aa5
LC
47 (lambda args
48 ;; Both return values have been encountered in the wild.
49 (memv (system-error-errno args) (list EPERM ENOENT)))))
29fa45f4 50
ccea821b 51(test-assert "mount-points"
381ac93b
LC
52 ;; Reportedly "/" is not always listed as a mount point, so check a few
53 ;; others (see <http://bugs.gnu.org/20261>.)
54 (any (cute member <> (mount-points))
55 '("/" "/proc" "/sys" "/dev")))
ccea821b 56
715fc9d4
LC
57(test-assert "swapon, ENOENT/EPERM"
58 (catch 'system-error
59 (lambda ()
60 (swapon "/does-not-exist")
61 #f)
62 (lambda args
63 (memv (system-error-errno args) (list EPERM ENOENT)))))
64
2793c0fb 65(test-assert "swapoff, ENOENT/EINVAL/EPERM"
715fc9d4
LC
66 (catch 'system-error
67 (lambda ()
68 (swapoff "/does-not-exist")
69 #f)
70 (lambda args
2793c0fb 71 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
715fc9d4 72
b4abdeb6
DT
73(test-assert "mkdtemp!"
74 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
75 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
76 (and (file-exists? dir)
77 (begin
78 (rmdir dir)
79 #t))))
80
a1f70878
LC
81(test-equal "statfs, ENOENT"
82 ENOENT
83 (catch 'system-error
84 (lambda ()
85 (statfs "/does-not-exist"))
86 (compose system-error-errno list)))
87
88(test-assert "statfs"
89 (let ((fs (statfs "/")))
90 (and (file-system? fs)
91 (> (file-system-block-size fs) 0)
92 (>= (file-system-blocks-available fs) 0)
93 (>= (file-system-blocks-free fs)
94 (file-system-blocks-available fs)))))
95
8950ed11
DT
96(define (user-namespace pid)
97 (string-append "/proc/" (number->string pid) "/ns/user"))
98
b7d48312
DT
99(define perform-container-tests?
100 (and (user-namespace-supported?)
101 (unprivileged-user-namespace-supported?)))
102
103(unless perform-container-tests?
b62a3ebc 104 (test-skip 1))
8950ed11
DT
105(test-assert "clone"
106 (match (clone (logior CLONE_NEWUSER SIGCHLD))
107 (0 (primitive-exit 42))
108 (pid
109 ;; Check if user namespaces are different.
110 (and (not (equal? (readlink (user-namespace pid))
111 (readlink (user-namespace (getpid)))))
112 (match (waitpid pid)
113 ((_ . status)
114 (= 42 (status:exit-val status))))))))
115
b7d48312 116(unless perform-container-tests?
b62a3ebc 117 (test-skip 1))
43ace6ea
DT
118(test-assert "setns"
119 (match (clone (logior CLONE_NEWUSER SIGCHLD))
120 (0 (primitive-exit 0))
121 (clone-pid
122 (match (pipe)
123 ((in . out)
124 (match (primitive-fork)
125 (0
126 (close in)
127 ;; Join the user namespace.
128 (call-with-input-file (user-namespace clone-pid)
129 (lambda (port)
130 (setns (port->fdes port) 0)))
131 (write 'done out)
132 (close out)
133 (primitive-exit 0))
134 (fork-pid
135 (close out)
136 ;; Wait for the child process to join the namespace.
137 (read in)
138 (let ((result (and (equal? (readlink (user-namespace clone-pid))
139 (readlink (user-namespace fork-pid))))))
140 ;; Clean up.
141 (waitpid clone-pid)
142 (waitpid fork-pid)
143 result))))))))
8950ed11 144
b7d48312 145(unless perform-container-tests?
b62a3ebc 146 (test-skip 1))
df3ce5c1
DT
147(test-assert "pivot-root"
148 (match (pipe)
149 ((in . out)
150 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
151 (0
152 (close in)
153 (call-with-temporary-directory
154 (lambda (root)
155 (let ((put-old (string-append root "/real-root")))
156 (mount "none" root "tmpfs")
157 (mkdir put-old)
158 (call-with-output-file (string-append root "/test")
159 (lambda (port)
160 (display "testing\n" port)))
161 (pivot-root root put-old)
162 ;; The test file should now be located inside the root directory.
163 (write (file-exists? "/test") out)
164 (close out))))
165 (primitive-exit 0))
166 (pid
167 (close out)
168 (let ((result (read in)))
169 (close in)
170 (and (zero? (match (waitpid pid)
171 ((_ . status)
172 (status:exit-val status))))
173 (eq? #t result))))))))
174
b89e7405
LC
175(test-assert "all-network-interface-names"
176 (match (all-network-interface-names)
4d54785c
LC
177 (((? string? names) ..1)
178 (member "lo" names))))
179
b89e7405
LC
180(test-assert "network-interface-names"
181 (match (network-interface-names)
7585016f 182 (((? string? names) ..1)
b89e7405 183 (lset<= string=? names (all-network-interface-names)))))
7585016f 184
973eea34 185(test-assert "network-interface-flags"
c9bf64d6 186 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
187 (flags (network-interface-flags sock "lo")))
188 (close-port sock)
189 (and (not (zero? (logand flags IFF_LOOPBACK)))
190 (not (zero? (logand flags IFF_UP))))))
191
192(test-equal "loopback-network-interface?"
193 ENODEV
194 (and (loopback-network-interface? "lo")
195 (catch 'system-error
196 (lambda ()
197 (loopback-network-interface? "nonexistent")
198 #f)
199 (lambda args
200 (system-error-errno args)))))
201
c9bf64d6 202(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 203(test-assert "set-network-interface-flags"
c9bf64d6
LC
204 (let ((sock (socket AF_INET SOCK_STREAM 0)))
205 (catch 'system-error
206 (lambda ()
207 (set-network-interface-flags sock "lo" IFF_UP))
208 (lambda args
209 (close-port sock)
d35c5e29
LC
210 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
211 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
212
213(test-equal "network-interface-address lo"
214 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
215 (let* ((sock (socket AF_INET SOCK_STREAM 0))
216 (addr (network-interface-address sock "lo")))
217 (close-port sock)
218 addr))
219
54e515eb 220(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 221(test-assert "set-network-interface-address"
c9bf64d6
LC
222 (let ((sock (socket AF_INET SOCK_STREAM 0)))
223 (catch 'system-error
224 (lambda ()
225 (set-network-interface-address sock "nonexistent"
226 (make-socket-address
227 AF_INET
228 (inet-pton AF_INET "127.12.14.15")
229 0)))
230 (lambda args
231 (close-port sock)
d35c5e29
LC
232 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
233 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 234
e7f5691d
LC
235(test-equal "network-interfaces returns one or more interfaces"
236 '(#t #t #t)
237 (match (network-interfaces)
238 ((interfaces ..1)
239 (list (every interface? interfaces)
240 (every string? (map interface-name interfaces))
7adbe85e
LC
241 (every (lambda (sockaddr)
242 ;; Sometimes interfaces have no associated address.
243 (or (vector? sockaddr)
244 (not sockaddr)))
245 (map interface-address interfaces))))))
e7f5691d
LC
246
247(test-equal "network-interfaces returns \"lo\""
248 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
249 (match (filter (lambda (interface)
250 (string=? "lo" (interface-name interface)))
251 (network-interfaces))
252 ((loopbacks ..1)
253 (list (every (lambda (lo)
254 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
255 loopbacks)
256 (match (find (lambda (lo)
257 (= AF_INET (sockaddr:fam (interface-address lo))))
258 loopbacks)
259 (#f #f)
260 (lo (interface-address lo)))))))
261
5cd25aad 262(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
263 (call-with-input-file "/dev/null"
264 (lambda (port)
265 (catch 'system-error
266 (lambda ()
267 (terminal-window-size port))
268 (lambda args
5cd25aad
LC
269 ;; Accept EINVAL, which some old Linux versions might return.
270 (memv (system-error-errno args)
271 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
272
273(test-assert "terminal-columns"
274 (> (terminal-columns) 0))
275
6d2b4391
LC
276(test-assert "terminal-columns non-file port"
277 (> (terminal-columns (open-input-string "Join us now, share the software!"))
278 0))
279
29fa45f4 280(test-end)