Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / tests / syscalls.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (test-syscalls)
20 #:use-module (guix build syscalls)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-64)
23 #:use-module (ice-9 match))
24
25 ;; Test the (guix build syscalls) module, although there's not much that can
26 ;; actually be tested without being root.
27
28 (test-begin "syscalls")
29
30 (test-equal "mount, ENOENT"
31 ENOENT
32 (catch 'system-error
33 (lambda ()
34 (mount "/dev/null" "/does-not-exist" "ext2")
35 #f)
36 (compose system-error-errno list)))
37
38 (test-assert "umount, ENOENT/EPERM"
39 (catch 'system-error
40 (lambda ()
41 (umount "/does-not-exist")
42 #f)
43 (lambda args
44 ;; Both return values have been encountered in the wild.
45 (memv (system-error-errno args) (list EPERM ENOENT)))))
46
47 (test-assert "mount-points"
48 (member "/" (mount-points)))
49
50 (test-assert "swapon, ENOENT/EPERM"
51 (catch 'system-error
52 (lambda ()
53 (swapon "/does-not-exist")
54 #f)
55 (lambda args
56 (memv (system-error-errno args) (list EPERM ENOENT)))))
57
58 (test-assert "swapoff, ENOENT/EINVAL/EPERM"
59 (catch 'system-error
60 (lambda ()
61 (swapoff "/does-not-exist")
62 #f)
63 (lambda args
64 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
65
66 (test-assert "all-network-interfaces"
67 (match (all-network-interfaces)
68 (((? string? names) ..1)
69 (member "lo" names))))
70
71 (test-assert "network-interfaces"
72 (match (network-interfaces)
73 (((? string? names) ..1)
74 (lset<= string=? names (all-network-interfaces)))))
75
76 (test-assert "network-interface-flags"
77 (let* ((sock (socket AF_INET SOCK_STREAM 0))
78 (flags (network-interface-flags sock "lo")))
79 (close-port sock)
80 (and (not (zero? (logand flags IFF_LOOPBACK)))
81 (not (zero? (logand flags IFF_UP))))))
82
83 (test-equal "loopback-network-interface?"
84 ENODEV
85 (and (loopback-network-interface? "lo")
86 (catch 'system-error
87 (lambda ()
88 (loopback-network-interface? "nonexistent")
89 #f)
90 (lambda args
91 (system-error-errno args)))))
92
93 (test-skip (if (zero? (getuid)) 1 0))
94 (test-equal "set-network-interface-flags"
95 EPERM
96 (let ((sock (socket AF_INET SOCK_STREAM 0)))
97 (catch 'system-error
98 (lambda ()
99 (set-network-interface-flags sock "lo" IFF_UP))
100 (lambda args
101 (close-port sock)
102 (system-error-errno args)))))
103
104 (test-equal "network-interface-address lo"
105 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
106 (let* ((sock (socket AF_INET SOCK_STREAM 0))
107 (addr (network-interface-address sock "lo")))
108 (close-port sock)
109 addr))
110
111 (test-equal "set-network-interface-address"
112 EPERM
113 (let ((sock (socket AF_INET SOCK_STREAM 0)))
114 (catch 'system-error
115 (lambda ()
116 (set-network-interface-address sock "nonexistent"
117 (make-socket-address
118 AF_INET
119 (inet-pton AF_INET "127.12.14.15")
120 0)))
121 (lambda args
122 (close-port sock)
123 (system-error-errno args)))))
124
125 (test-end)
126
127 \f
128 (exit (= (test-runner-fail-count (test-runner-current)) 0))