gnu: Rename rust-bincode-1.2 to rust-bincode-1.
[jackhill/guix/guix.git] / gnu / tests / networking.scm
CommitLineData
9260b9d1
TD
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
671dbdb9 3;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
b0f951e4 4;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
9926b8f8 5;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
9260b9d1
TD
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (gnu tests networking)
23 #:use-module (gnu tests)
24 #:use-module (gnu system)
9260b9d1
TD
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
d94e81db 27 #:use-module (gnu services base)
9260b9d1
TD
28 #:use-module (gnu services networking)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:use-module (guix monads)
32 #:use-module (gnu packages bash)
9926b8f8 33 #:use-module (gnu packages linux)
671dbdb9
MB
34 #:use-module (gnu packages networking)
35 #:use-module (gnu services shepherd)
9926b8f8
AI
36 #:use-module (ice-9 match)
37 #:export (%test-inetd %test-openvswitch %test-dhcpd %test-tor %test-iptables))
9260b9d1
TD
38
39(define %inetd-os
40 ;; Operating system with 2 inetd services.
892d9089 41 (simple-operating-system
39d7fdce 42 (service dhcp-client-service-type)
892d9089
LC
43 (service inetd-service-type
44 (inetd-configuration
45 (entries (list
46 (inetd-entry
47 (name "echo")
48 (socket-type 'stream)
49 (protocol "tcp")
50 (wait? #f)
51 (user "root"))
52 (inetd-entry
53 (name "dict")
54 (socket-type 'stream)
55 (protocol "tcp")
56 (wait? #f)
57 (user "root")
58 (program (file-append bash
59 "/bin/bash"))
60 (arguments
61 (list "bash" (plain-file "my-dict.sh" "\
9260b9d1
TD
62while read line
63do
64 if [[ $line =~ ^DEFINE\\ (.*)$ ]]
65 then
66 case ${BASH_REMATCH[1]} in
67 Guix)
68 echo GNU Guix is a package management tool for the GNU system.
69 ;;
70 G-expression)
71 echo Like an S-expression but with a G.
72 ;;
73 *)
74 echo NO DEFINITION FOUND
75 ;;
76 esac
77 else
78 echo ERROR
79 fi
892d9089 80done" ))))))))))
9260b9d1
TD
81
82(define* (run-inetd-test)
83 "Run tests in %INETD-OS, where the inetd service provides an echo service on
84port 7, and a dict service on port 2628."
8b113790
LC
85 (define os
86 (marionette-operating-system %inetd-os))
9260b9d1 87
8b113790
LC
88 (define vm
89 (virtual-machine
90 (operating-system os)
91 (port-forwardings `((8007 . 7)
92 (8628 . 2628)))))
9260b9d1 93
8b113790
LC
94 (define test
95 (with-imported-modules '((gnu build marionette))
96 #~(begin
97 (use-modules (ice-9 rdelim)
98 (srfi srfi-64)
99 (gnu build marionette))
100 (define marionette
101 (make-marionette (list #$vm)))
9260b9d1 102
8b113790
LC
103 (mkdir #$output)
104 (chdir #$output)
9260b9d1 105
8b113790 106 (test-begin "inetd")
9260b9d1 107
8b113790
LC
108 ;; Make sure the PID file is created.
109 (test-assert "PID file"
110 (marionette-eval
111 '(file-exists? "/var/run/inetd.pid")
112 marionette))
9260b9d1 113
8b113790
LC
114 ;; Test the echo service.
115 (test-equal "echo response"
116 "Hello, Guix!"
117 (let ((echo (socket PF_INET SOCK_STREAM 0))
118 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
119 (connect echo addr)
120 (display "Hello, Guix!\n" echo)
121 (let ((response (read-line echo)))
122 (close echo)
123 response)))
9260b9d1 124
8b113790
LC
125 ;; Test the dict service
126 (test-equal "dict response"
127 "GNU Guix is a package management tool for the GNU system."
128 (let ((dict (socket PF_INET SOCK_STREAM 0))
129 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
130 (connect dict addr)
131 (display "DEFINE Guix\n" dict)
132 (let ((response (read-line dict)))
133 (close dict)
134 response)))
135
136 (test-end)
137 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
138
139 (gexp->derivation "inetd-test" test))
9260b9d1
TD
140
141(define %test-inetd
142 (system-test
143 (name "inetd")
144 (description "Connect to a host with an INETD server.")
145 (value (run-inetd-test))))
671dbdb9
MB
146
147\f
148;;;
149;;; Open vSwitch
150;;;
151
152(define setup-openvswitch
153 #~(let ((ovs-vsctl (lambda (str)
154 (zero? (apply system*
155 #$(file-append openvswitch "/bin/ovs-vsctl")
156 (string-tokenize str)))))
157 (add-native-port (lambda (if)
158 (string-append "--may-exist add-port br0 " if
159 " vlan_mode=native-untagged"
160 " -- set Interface " if
161 " type=internal"))))
162 (and (ovs-vsctl "--may-exist add-br br0")
163 ;; Connect eth0 as an "untagged" port (no VLANs).
164 (ovs-vsctl "--may-exist add-port br0 eth0 vlan_mode=native-untagged")
165 (ovs-vsctl (add-native-port "ovs0")))))
166
167(define openvswitch-configuration-service
168 (simple-service 'openvswitch-configuration shepherd-root-service-type
169 (list (shepherd-service
170 (provision '(openvswitch-configuration))
171 (requirement '(vswitchd))
172 (start #~(lambda ()
173 #$setup-openvswitch))
174 (respawn? #f)))))
175
176(define %openvswitch-os
177 (simple-operating-system
178 (static-networking-service "ovs0" "10.1.1.1"
179 #:netmask "255.255.255.252"
180 #:requirement '(openvswitch-configuration))
e73ded3c 181 (service openvswitch-service-type)
671dbdb9
MB
182 openvswitch-configuration-service))
183
184(define (run-openvswitch-test)
185 (define os
186 (marionette-operating-system %openvswitch-os
187 #:imported-modules '((gnu services herd))))
188
189 (define test
190 (with-imported-modules '((gnu build marionette))
191 #~(begin
192 (use-modules (gnu build marionette)
193 (ice-9 popen)
194 (ice-9 rdelim)
195 (srfi srfi-64))
196
197 (define marionette
198 (make-marionette (list #$(virtual-machine os))))
199
200 (mkdir #$output)
201 (chdir #$output)
202
203 (test-begin "openvswitch")
204
205 ;; Make sure the bridge is created.
206 (test-assert "br0 exists"
207 (marionette-eval
6720616d
MB
208 '(zero? (system* #$(file-append openvswitch "/bin/ovs-vsctl")
209 "br-exists" "br0"))
671dbdb9
MB
210 marionette))
211
212 ;; Make sure eth0 is connected to the bridge.
213 (test-equal "eth0 is connected to br0"
214 "br0"
215 (marionette-eval
216 '(begin
217 (use-modules (ice-9 popen) (ice-9 rdelim))
218 (let* ((port (open-pipe*
219 OPEN_READ
220 (string-append #$openvswitch "/bin/ovs-vsctl")
221 "port-to-br" "eth0"))
222 (output (read-line port)))
223 (close-pipe port)
224 output))
225 marionette))
226
227 ;; Make sure the virtual interface got a static IP.
228 (test-assert "networking has started on ovs0"
229 (marionette-eval
230 '(begin
231 (use-modules (gnu services herd)
232 (srfi srfi-1))
233 (live-service-running
234 (find (lambda (live)
235 (memq 'networking-ovs0
236 (live-service-provision live)))
237 (current-services))))
238 marionette))
239
240 (test-end)
241 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
242
243 (gexp->derivation "openvswitch-test" test))
244
245(define %test-openvswitch
246 (system-test
247 (name "openvswitch")
248 (description "Test a running OpenvSwitch configuration.")
249 (value (run-openvswitch-test))))
f1104d90
CM
250
251\f
252;;;
253;;; DHCP Daemon
254;;;
255
256(define minimal-dhcpd-v4-config-file
257 (plain-file "dhcpd.conf"
258 "\
259default-lease-time 600;
260max-lease-time 7200;
261
262subnet 192.168.1.0 netmask 255.255.255.0 {
263 range 192.168.1.100 192.168.1.200;
264 option routers 192.168.1.1;
265 option domain-name-servers 192.168.1.2, 192.168.1.3;
266 option domain-name \"dummy.domain.name.abc123xyz\";
267}
268"))
269
270(define dhcpd-v4-configuration
271 (dhcpd-configuration
272 (config-file minimal-dhcpd-v4-config-file)
273 (version "4")
ef0f5ff2 274 (interfaces '("ens3"))))
f1104d90
CM
275
276(define %dhcpd-os
277 (simple-operating-system
ef0f5ff2 278 (static-networking-service "ens3" "192.168.1.4"
f1104d90
CM
279 #:netmask "255.255.255.0"
280 #:gateway "192.168.1.1"
281 #:name-servers '("192.168.1.2" "192.168.1.3"))
282 (service dhcpd-service-type dhcpd-v4-configuration)))
283
284(define (run-dhcpd-test)
285 (define os
286 (marionette-operating-system %dhcpd-os
287 #:imported-modules '((gnu services herd))))
288
289 (define test
290 (with-imported-modules '((gnu build marionette))
291 #~(begin
292 (use-modules (gnu build marionette)
293 (ice-9 popen)
294 (ice-9 rdelim)
295 (srfi srfi-64))
296
297 (define marionette
298 (make-marionette (list #$(virtual-machine os))))
299
300 (mkdir #$output)
301 (chdir #$output)
302
303 (test-begin "dhcpd")
304
305 (test-assert "pid file exists"
306 (marionette-eval
307 '(file-exists?
308 #$(dhcpd-configuration-pid-file dhcpd-v4-configuration))
309 marionette))
310
311 (test-assert "lease file exists"
312 (marionette-eval
313 '(file-exists?
314 #$(dhcpd-configuration-lease-file dhcpd-v4-configuration))
315 marionette))
316
317 (test-assert "run directory exists"
318 (marionette-eval
319 '(file-exists?
320 #$(dhcpd-configuration-run-directory dhcpd-v4-configuration))
321 marionette))
322
323 (test-assert "dhcpd is alive"
324 (marionette-eval
325 '(begin
326 (use-modules (gnu services herd)
327 (srfi srfi-1))
328 (live-service-running
329 (find (lambda (live)
330 (memq 'dhcpv4-daemon
331 (live-service-provision live)))
332 (current-services))))
333 marionette))
334
335 (test-end)
336 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
337
338 (gexp->derivation "dhcpd-test" test))
339
340(define %test-dhcpd
341 (system-test
342 (name "dhcpd")
343 (description "Test a running DHCP daemon configuration.")
344 (value (run-dhcpd-test))))
5dfd80e1
CM
345
346\f
347;;;
348;;; Services related to Tor
349;;;
350
351(define %tor-os
352 (simple-operating-system
84a2de36 353 (service tor-service-type)))
5dfd80e1 354
b0f951e4
CM
355(define %tor-os/unix-socks-socket
356 (simple-operating-system
357 (service tor-service-type
358 (tor-configuration
3bcb305b 359 (socks-socket-type 'unix)))))
b0f951e4 360
5dfd80e1
CM
361(define (run-tor-test)
362 (define os
363 (marionette-operating-system %tor-os
364 #:imported-modules '((gnu services herd))
365 #:requirements '(tor)))
366
b0f951e4
CM
367 (define os/unix-socks-socket
368 (marionette-operating-system %tor-os/unix-socks-socket
369 #:imported-modules '((gnu services herd))
370 #:requirements '(tor)))
371
5dfd80e1
CM
372 (define test
373 (with-imported-modules '((gnu build marionette))
374 #~(begin
375 (use-modules (gnu build marionette)
376 (ice-9 popen)
377 (ice-9 rdelim)
378 (srfi srfi-64))
379
380 (define marionette
381 (make-marionette (list #$(virtual-machine os))))
382
b0f951e4 383 (define (tor-is-alive? marionette)
5dfd80e1
CM
384 (marionette-eval
385 '(begin
386 (use-modules (gnu services herd)
387 (srfi srfi-1))
388 (live-service-running
389 (find (lambda (live)
390 (memq 'tor
391 (live-service-provision live)))
392 (current-services))))
393 marionette))
394
b0f951e4
CM
395 (mkdir #$output)
396 (chdir #$output)
397
398 (test-begin "tor")
399
400 ;; Test the usual Tor service.
401
402 (test-assert "tor is alive"
403 (tor-is-alive? marionette))
404
405 (test-assert "tor is listening"
406 (let ((default-port 9050))
407 (wait-for-tcp-port default-port marionette)))
408
409 ;; Don't run two VMs at once.
410 (marionette-control "quit" marionette)
411
412 ;; Test the Tor service using a SOCKS socket.
413
414 (let* ((socket-directory "/tmp/more-sockets")
415 (_ (mkdir socket-directory))
416 (marionette/unix-socks-socket
417 (make-marionette
418 (list #$(virtual-machine os/unix-socks-socket))
419 ;; We can't use the same socket directory as the first
420 ;; marionette.
421 #:socket-directory socket-directory)))
422 (test-assert "tor is alive, even when using a SOCKS socket"
423 (tor-is-alive? marionette/unix-socks-socket))
424
425 (test-assert "tor is listening, even when using a SOCKS socket"
426 (wait-for-unix-socket "/var/run/tor/socks-sock"
427 marionette/unix-socks-socket)))
428
5dfd80e1
CM
429 (test-end)
430 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
431
432 (gexp->derivation "tor-test" test))
433
434(define %test-tor
435 (system-test
436 (name "tor")
437 (description "Test a running Tor daemon configuration.")
438 (value (run-tor-test))))
9926b8f8
AI
439
440(define* (run-iptables-test)
441 "Run tests of 'iptables-service-type'."
442 (define iptables-rules
443 "*filter
444:INPUT ACCEPT
445:FORWARD ACCEPT
446:OUTPUT ACCEPT
447-A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp-port-unreachable
448COMMIT
449")
450
451 (define ip6tables-rules
452 "*filter
453:INPUT ACCEPT
454:FORWARD ACCEPT
455:OUTPUT ACCEPT
456-A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp6-port-unreachable
457COMMIT
458")
459
460 (define inetd-echo-port 7)
461
462 (define os
463 (marionette-operating-system
464 (simple-operating-system
39d7fdce 465 (service dhcp-client-service-type)
9926b8f8
AI
466 (service inetd-service-type
467 (inetd-configuration
468 (entries (list
469 (inetd-entry
470 (name "echo")
471 (socket-type 'stream)
472 (protocol "tcp")
473 (wait? #f)
474 (user "root"))))))
475 (service iptables-service-type
476 (iptables-configuration
477 (ipv4-rules (plain-file "iptables.rules" iptables-rules))
478 (ipv6-rules (plain-file "ip6tables.rules" ip6tables-rules)))))
479 #:imported-modules '((gnu services herd))
480 #:requirements '(inetd iptables)))
481
482 (define test
483 (with-imported-modules '((gnu build marionette))
484 #~(begin
485 (use-modules (srfi srfi-64)
486 (gnu build marionette))
487 (define marionette
488 (make-marionette (list #$(virtual-machine os))))
489
490 (define (dump-iptables iptables-save marionette)
491 (marionette-eval
492 `(begin
493 (use-modules (ice-9 popen)
494 (ice-9 rdelim)
495 (ice-9 regex))
496 (call-with-output-string
497 (lambda (out)
498 (call-with-port
499 (open-pipe* OPEN_READ ,iptables-save)
500 (lambda (in)
501 (let loop ((line (read-line in)))
502 ;; iptables-save does not output rules in the exact
503 ;; same format we loaded using iptables-restore. It
504 ;; adds comments, packet counters, etc. We remove
505 ;; these additions.
506 (unless (eof-object? line)
507 (cond
508 ;; Remove comments
509 ((string-match "^#" line) #t)
510 ;; Remove packet counters
511 ((string-match "^:([A-Z]*) ([A-Z]*) .*" line)
512 => (lambda (match-record)
513 (format out ":~a ~a~%"
514 (match:substring match-record 1)
515 (match:substring match-record 2))))
516 ;; Pass other lines without modification
517 (else (display line out)
518 (newline out)))
519 (loop (read-line in)))))))))
520 marionette))
521
522 (mkdir #$output)
523 (chdir #$output)
524
525 (test-begin "iptables")
526
527 (test-equal "iptables-save dumps the same rules that were loaded"
528 (dump-iptables #$(file-append iptables "/sbin/iptables-save")
529 marionette)
530 #$iptables-rules)
531
532 (test-equal "ip6tables-save dumps the same rules that were loaded"
533 (dump-iptables #$(file-append iptables "/sbin/ip6tables-save")
534 marionette)
535 #$ip6tables-rules)
536
537 (test-error "iptables firewall blocks access to inetd echo service"
538 'misc-error
539 (wait-for-tcp-port inetd-echo-port marionette #:timeout 5))
540
541 ;; TODO: This test freezes up at the login prompt without any
542 ;; relevant messages on the console. Perhaps it is waiting for some
543 ;; timeout. Find and fix this issue.
544 ;; (test-assert "inetd echo service is accessible after iptables firewall is stopped"
545 ;; (begin
546 ;; (marionette-eval
547 ;; '(begin
548 ;; (use-modules (gnu services herd))
549 ;; (stop-service 'iptables))
550 ;; marionette)
551 ;; (wait-for-tcp-port inetd-echo-port marionette #:timeout 5)))
552
553 (test-end)
554 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
555
556 (gexp->derivation "iptables" test))
557
558(define %test-iptables
559 (system-test
560 (name "iptables")
561 (description "Test a running iptables daemon.")
562 (value (run-iptables-test))))