1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
3 ;;; Copyright © 2017, 2020 Marius Bakke <marius@gnu.org>
4 ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
5 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
6 ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu tests networking)
24 #:use-module (gnu tests)
25 #:use-module (gnu system)
26 #:use-module (gnu system vm)
27 #:use-module (gnu services)
28 #:use-module (gnu services base)
29 #:use-module (gnu services networking)
30 #:use-module (guix gexp)
31 #:use-module (guix store)
32 #:use-module (guix monads)
33 #:use-module (guix modules)
34 #:use-module (gnu packages bash)
35 #:use-module (gnu packages linux)
36 #:use-module (gnu packages networking)
37 #:use-module (gnu packages guile)
38 #:use-module (gnu services shepherd)
39 #:use-module (ice-9 match)
40 #:export (%test-inetd %test-openvswitch %test-dhcpd %test-tor %test-iptables
44 ;; Operating system with 2 inetd services.
45 (simple-operating-system
46 (service dhcp-client-service-type)
47 (service inetd-service-type
62 (program (file-append bash
65 (list "bash" (plain-file "my-dict.sh" "\
68 if [[ $line =~ ^DEFINE\\ (.*)$ ]]
70 case ${BASH_REMATCH[1]} in
72 echo GNU Guix is a package management tool for the GNU system.
75 echo Like an S-expression but with a G.
78 echo NO DEFINITION FOUND
86 (define* (run-inetd-test)
87 "Run tests in %INETD-OS, where the inetd service provides an echo service on
88 port 7, and a dict service on port 2628."
90 (marionette-operating-system %inetd-os))
95 (port-forwardings `((8007 . 7)
99 (with-imported-modules '((gnu build marionette))
101 (use-modules (ice-9 rdelim)
103 (gnu build marionette))
105 (make-marionette (list #$vm)))
112 ;; Make sure the PID file is created.
113 (test-assert "PID file"
115 '(file-exists? "/var/run/inetd.pid")
118 ;; Test the echo service.
119 (test-equal "echo response"
121 (let ((echo (socket PF_INET SOCK_STREAM 0))
122 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
124 (display "Hello, Guix!\n" echo)
125 (let ((response (read-line echo)))
129 ;; Test the dict service
130 (test-equal "dict response"
131 "GNU Guix is a package management tool for the GNU system."
132 (let ((dict (socket PF_INET SOCK_STREAM 0))
133 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
135 (display "DEFINE Guix\n" dict)
136 (let ((response (read-line dict)))
141 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
143 (gexp->derivation "inetd-test" test))
148 (description "Connect to a host with an INETD server.")
149 (value (run-inetd-test))))
156 (define setup-openvswitch
157 #~(let ((ovs-vsctl (lambda (str)
158 (zero? (apply system*
159 #$(file-append openvswitch "/bin/ovs-vsctl")
160 (string-tokenize str)))))
161 (add-native-port (lambda (if)
162 (string-append "--may-exist add-port br0 " if
163 " vlan_mode=native-untagged"
164 " -- set Interface " if
166 (and (ovs-vsctl "--may-exist add-br br0")
167 ;; Connect eth0 as an "untagged" port (no VLANs).
168 (ovs-vsctl "--may-exist add-port br0 eth0 vlan_mode=native-untagged")
169 (ovs-vsctl (add-native-port "ovs0")))))
171 (define openvswitch-configuration-service
172 (simple-service 'openvswitch-configuration shepherd-root-service-type
173 (list (shepherd-service
174 (provision '(openvswitch-configuration))
175 (requirement '(vswitchd))
177 #$setup-openvswitch))
180 (define %openvswitch-os
182 (inherit (simple-operating-system
183 (static-networking-service "ovs0" "10.1.1.1"
184 #:netmask "255.255.255.252"
185 #:requirement '(openvswitch-configuration))
186 (service openvswitch-service-type)
187 openvswitch-configuration-service))
188 ;; Ensure the interface name does not change depending on the driver.
189 (kernel-arguments (cons "net.ifnames=0" %default-kernel-arguments))))
191 (define (run-openvswitch-test)
193 (marionette-operating-system %openvswitch-os
194 #:imported-modules '((gnu services herd))))
197 (with-imported-modules '((gnu build marionette))
199 (use-modules (gnu build marionette)
205 (make-marionette (list #$(virtual-machine os))))
210 (test-begin "openvswitch")
212 ;; Make sure the bridge is created.
213 (test-assert "br0 exists"
215 '(zero? (system* #$(file-append openvswitch "/bin/ovs-vsctl")
219 ;; Make sure eth0 is connected to the bridge.
220 (test-equal "eth0 is connected to br0"
224 (use-modules (ice-9 popen) (ice-9 rdelim))
225 (let* ((port (open-pipe*
227 (string-append #$openvswitch "/bin/ovs-vsctl")
228 "port-to-br" "eth0"))
229 (output (read-line port)))
234 ;; Make sure the virtual interface got a static IP.
235 (test-assert "networking has started on ovs0"
238 (use-modules (gnu services herd)
240 (live-service-running
242 (memq 'networking-ovs0
243 (live-service-provision live)))
244 (current-services))))
248 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
250 (gexp->derivation "openvswitch-test" test))
252 (define %test-openvswitch
255 (description "Test a running OpenvSwitch configuration.")
256 (value (run-openvswitch-test))))
263 (define minimal-dhcpd-v4-config-file
264 (plain-file "dhcpd.conf"
266 default-lease-time 600;
269 subnet 192.168.1.0 netmask 255.255.255.0 {
270 range 192.168.1.100 192.168.1.200;
271 option routers 192.168.1.1;
272 option domain-name-servers 192.168.1.2, 192.168.1.3;
273 option domain-name \"dummy.domain.name.abc123xyz\";
277 (define dhcpd-v4-configuration
279 (config-file minimal-dhcpd-v4-config-file)
281 (interfaces '("ens3"))))
284 (simple-operating-system
285 (static-networking-service "ens3" "192.168.1.4"
286 #:netmask "255.255.255.0"
287 #:gateway "192.168.1.1"
288 #:name-servers '("192.168.1.2" "192.168.1.3"))
289 (service dhcpd-service-type dhcpd-v4-configuration)))
291 (define (run-dhcpd-test)
293 (marionette-operating-system %dhcpd-os
294 #:imported-modules '((gnu services herd))))
297 (with-imported-modules '((gnu build marionette))
299 (use-modules (gnu build marionette)
305 (make-marionette (list #$(virtual-machine os))))
312 (test-assert "pid file exists"
315 #$(dhcpd-configuration-pid-file dhcpd-v4-configuration))
318 (test-assert "lease file exists"
321 #$(dhcpd-configuration-lease-file dhcpd-v4-configuration))
324 (test-assert "run directory exists"
327 #$(dhcpd-configuration-run-directory dhcpd-v4-configuration))
330 (test-assert "dhcpd is alive"
333 (use-modules (gnu services herd)
335 (live-service-running
338 (live-service-provision live)))
339 (current-services))))
343 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
345 (gexp->derivation "dhcpd-test" test))
350 (description "Test a running DHCP daemon configuration.")
351 (value (run-dhcpd-test))))
355 ;;; Services related to Tor
359 (simple-operating-system
360 (service tor-service-type)))
362 (define %tor-os/unix-socks-socket
363 (simple-operating-system
364 (service tor-service-type
366 (socks-socket-type 'unix)))))
368 (define (run-tor-test)
370 (marionette-operating-system %tor-os
371 #:imported-modules '((gnu services herd))
372 #:requirements '(tor)))
374 (define os/unix-socks-socket
375 (marionette-operating-system %tor-os/unix-socks-socket
376 #:imported-modules '((gnu services herd))
377 #:requirements '(tor)))
380 (with-imported-modules '((gnu build marionette))
382 (use-modules (gnu build marionette)
388 (make-marionette (list #$(virtual-machine os))))
390 (define (tor-is-alive? marionette)
393 (use-modules (gnu services herd)
395 (live-service-running
398 (live-service-provision live)))
399 (current-services))))
407 ;; Test the usual Tor service.
409 (test-assert "tor is alive"
410 (tor-is-alive? marionette))
412 (test-assert "tor is listening"
413 (let ((default-port 9050))
414 (wait-for-tcp-port default-port marionette)))
416 ;; Don't run two VMs at once.
417 (marionette-control "quit" marionette)
419 ;; Test the Tor service using a SOCKS socket.
421 (let* ((socket-directory "/tmp/more-sockets")
422 (_ (mkdir socket-directory))
423 (marionette/unix-socks-socket
425 (list #$(virtual-machine os/unix-socks-socket))
426 ;; We can't use the same socket directory as the first
428 #:socket-directory socket-directory)))
429 (test-assert "tor is alive, even when using a SOCKS socket"
430 (tor-is-alive? marionette/unix-socks-socket))
432 (test-assert "tor is listening, even when using a SOCKS socket"
433 (wait-for-unix-socket "/var/run/tor/socks-sock"
434 marionette/unix-socks-socket)))
437 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
439 (gexp->derivation "tor-test" test))
444 (description "Test a running Tor daemon configuration.")
445 (value (run-tor-test))))
447 (define* (run-iptables-test)
448 "Run tests of 'iptables-service-type'."
449 (define iptables-rules
454 -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp-port-unreachable
458 (define ip6tables-rules
463 -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp6-port-unreachable
467 (define inetd-echo-port 7)
470 (marionette-operating-system
471 (simple-operating-system
472 (service dhcp-client-service-type)
473 (service inetd-service-type
478 (socket-type 'stream)
482 (service iptables-service-type
483 (iptables-configuration
484 (ipv4-rules (plain-file "iptables.rules" iptables-rules))
485 (ipv6-rules (plain-file "ip6tables.rules" ip6tables-rules)))))
486 #:imported-modules '((gnu services herd))
487 #:requirements '(inetd iptables)))
490 (with-imported-modules '((gnu build marionette))
492 (use-modules (srfi srfi-64)
493 (gnu build marionette))
495 (make-marionette (list #$(virtual-machine os))))
497 (define (dump-iptables iptables-save marionette)
500 (use-modules (ice-9 popen)
503 (call-with-output-string
506 (open-pipe* OPEN_READ ,iptables-save)
508 (let loop ((line (read-line in)))
509 ;; iptables-save does not output rules in the exact
510 ;; same format we loaded using iptables-restore. It
511 ;; adds comments, packet counters, etc. We remove
513 (unless (eof-object? line)
516 ((string-match "^#" line) #t)
517 ;; Remove packet counters
518 ((string-match "^:([A-Z]*) ([A-Z]*) .*" line)
519 => (lambda (match-record)
520 (format out ":~a ~a~%"
521 (match:substring match-record 1)
522 (match:substring match-record 2))))
523 ;; Pass other lines without modification
524 (else (display line out)
526 (loop (read-line in)))))))))
532 (test-begin "iptables")
534 (test-equal "iptables-save dumps the same rules that were loaded"
535 (dump-iptables #$(file-append iptables "/sbin/iptables-save")
539 (test-equal "ip6tables-save dumps the same rules that were loaded"
540 (dump-iptables #$(file-append iptables "/sbin/ip6tables-save")
544 (test-error "iptables firewall blocks access to inetd echo service"
546 (wait-for-tcp-port inetd-echo-port marionette #:timeout 5))
548 ;; TODO: This test freezes up at the login prompt without any
549 ;; relevant messages on the console. Perhaps it is waiting for some
550 ;; timeout. Find and fix this issue.
551 ;; (test-assert "inetd echo service is accessible after iptables firewall is stopped"
555 ;; (use-modules (gnu services herd))
556 ;; (stop-service 'iptables))
558 ;; (wait-for-tcp-port inetd-echo-port marionette #:timeout 5)))
561 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
563 (gexp->derivation "iptables" test))
565 (define %test-iptables
568 (description "Test a running iptables daemon.")
569 (value (run-iptables-test))))
577 (simple-operating-system
578 (service ipfs-service-type)))
580 (define (run-ipfs-test)
582 (marionette-operating-system %ipfs-os
583 #:imported-modules (source-module-closure
584 '((gnu services herd)
586 #:extensions (list guile-json-4)
587 #:requirements '(ipfs)))
590 (with-imported-modules '((gnu build marionette))
592 (use-modules (gnu build marionette)
595 (ice-9 binary-ports))
598 (make-marionette (list #$(virtual-machine os))))
600 (define (ipfs-is-alive?)
603 (use-modules (gnu services herd)
605 (live-service-running
608 (live-service-provision live)))
609 (current-services))))
612 ;; The default API endpoint port 5001 is used,
613 ;; so there is no need to parameterize %ipfs-base-url.
614 (define (add-data data)
615 (marionette-eval `(content-name (add-data ,data)) marionette))
616 (define (read-contents object)
618 `(let* ((input (read-contents ,object))
619 (all-input (get-bytevector-all input)))
624 (marionette-eval '(use-modules (guix ipfs)) marionette)
630 ;; Test the IPFS service.
632 (test-assert "ipfs is alive" (ipfs-is-alive?))
634 (test-assert "ipfs is listening on the gateway"
635 (let ((default-port 8082))
636 (wait-for-tcp-port default-port marionette)))
638 (test-assert "ipfs is listening on the API endpoint"
639 (let ((default-port 5001))
640 (wait-for-tcp-port default-port marionette)))
642 (define test-bv (string->utf8 "hello ipfs!"))
643 (test-equal "can upload and download a file to/from ipfs"
645 (read-contents (add-data test-bv)))
648 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
649 (gexp->derivation "ipfs-test" test))
654 (description "Test a running IPFS daemon configuration.")
655 (value (run-ipfs-test))))