networking.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  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.
  14. ;;;
  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.
  19. ;;;
  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/>.
  22. (define-module (gnu tests networking)
  23. #:use-module (gnu tests)
  24. #:use-module (gnu system)
  25. #:use-module (gnu system vm)
  26. #:use-module (gnu services)
  27. #:use-module (gnu services base)
  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)
  33. #:use-module (gnu packages linux)
  34. #:use-module (gnu packages networking)
  35. #:use-module (gnu services shepherd)
  36. #:use-module (ice-9 match)
  37. #:export (%test-inetd %test-openvswitch %test-dhcpd %test-tor %test-iptables
  38. %test-ipfs))
  39. (define %inetd-os
  40. ;; Operating system with 2 inetd services.
  41. (simple-operating-system
  42. (service dhcp-client-service-type)
  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" "\
  62. while read line
  63. do
  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
  80. done" ))))))))))
  81. (define* (run-inetd-test)
  82. "Run tests in %INETD-OS, where the inetd service provides an echo service on
  83. port 7, and a dict service on port 2628."
  84. (define os
  85. (marionette-operating-system %inetd-os))
  86. (define vm
  87. (virtual-machine
  88. (operating-system os)
  89. (port-forwardings `((8007 . 7)
  90. (8628 . 2628)))))
  91. (define test
  92. (with-imported-modules '((gnu build marionette))
  93. #~(begin
  94. (use-modules (ice-9 rdelim)
  95. (srfi srfi-64)
  96. (gnu build marionette))
  97. (define marionette
  98. (make-marionette (list #$vm)))
  99. (mkdir #$output)
  100. (chdir #$output)
  101. (test-begin "inetd")
  102. ;; Make sure the PID file is created.
  103. (test-assert "PID file"
  104. (marionette-eval
  105. '(file-exists? "/var/run/inetd.pid")
  106. marionette))
  107. ;; Test the echo service.
  108. (test-equal "echo response"
  109. "Hello, Guix!"
  110. (let ((echo (socket PF_INET SOCK_STREAM 0))
  111. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
  112. (connect echo addr)
  113. (display "Hello, Guix!\n" echo)
  114. (let ((response (read-line echo)))
  115. (close echo)
  116. response)))
  117. ;; Test the dict service
  118. (test-equal "dict response"
  119. "GNU Guix is a package management tool for the GNU system."
  120. (let ((dict (socket PF_INET SOCK_STREAM 0))
  121. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
  122. (connect dict addr)
  123. (display "DEFINE Guix\n" dict)
  124. (let ((response (read-line dict)))
  125. (close dict)
  126. response)))
  127. (test-end)
  128. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  129. (gexp->derivation "inetd-test" test))
  130. (define %test-inetd
  131. (system-test
  132. (name "inetd")
  133. (description "Connect to a host with an INETD server.")
  134. (value (run-inetd-test))))
  135. ;;;
  136. ;;; Open vSwitch
  137. ;;;
  138. (define setup-openvswitch
  139. #~(let ((ovs-vsctl (lambda (str)
  140. (zero? (apply system*
  141. #$(file-append openvswitch "/bin/ovs-vsctl")
  142. (string-tokenize str)))))
  143. (add-native-port (lambda (if)
  144. (string-append "--may-exist add-port br0 " if
  145. " vlan_mode=native-untagged"
  146. " -- set Interface " if
  147. " type=internal"))))
  148. (and (ovs-vsctl "--may-exist add-br br0")
  149. ;; Connect eth0 as an "untagged" port (no VLANs).
  150. (ovs-vsctl "--may-exist add-port br0 eth0 vlan_mode=native-untagged")
  151. (ovs-vsctl (add-native-port "ovs0")))))
  152. (define openvswitch-configuration-service
  153. (simple-service 'openvswitch-configuration shepherd-root-service-type
  154. (list (shepherd-service
  155. (provision '(openvswitch-configuration))
  156. (requirement '(vswitchd))
  157. (start #~(lambda ()
  158. #$setup-openvswitch))
  159. (respawn? #f)))))
  160. (define %openvswitch-os
  161. (operating-system
  162. (inherit (simple-operating-system
  163. (static-networking-service "ovs0" "10.1.1.1"
  164. #:netmask "255.255.255.252"
  165. #:requirement '(openvswitch-configuration))
  166. (service openvswitch-service-type)
  167. openvswitch-configuration-service))
  168. ;; Ensure the interface name does not change depending on the driver.
  169. (kernel-arguments (cons "net.ifnames=0" %default-kernel-arguments))))
  170. (define (run-openvswitch-test)
  171. (define os
  172. (marionette-operating-system %openvswitch-os
  173. #:imported-modules '((gnu services herd))))
  174. (define test
  175. (with-imported-modules '((gnu build marionette))
  176. #~(begin
  177. (use-modules (gnu build marionette)
  178. (ice-9 popen)
  179. (ice-9 rdelim)
  180. (srfi srfi-64))
  181. (define marionette
  182. (make-marionette (list #$(virtual-machine os))))
  183. (mkdir #$output)
  184. (chdir #$output)
  185. (test-begin "openvswitch")
  186. ;; Make sure the bridge is created.
  187. (test-assert "br0 exists"
  188. (marionette-eval
  189. '(zero? (system* #$(file-append openvswitch "/bin/ovs-vsctl")
  190. "br-exists" "br0"))
  191. marionette))
  192. ;; Make sure eth0 is connected to the bridge.
  193. (test-equal "eth0 is connected to br0"
  194. "br0"
  195. (marionette-eval
  196. '(begin
  197. (use-modules (ice-9 popen) (ice-9 rdelim))
  198. (let* ((port (open-pipe*
  199. OPEN_READ
  200. (string-append #$openvswitch "/bin/ovs-vsctl")
  201. "port-to-br" "eth0"))
  202. (output (read-line port)))
  203. (close-pipe port)
  204. output))
  205. marionette))
  206. ;; Make sure the virtual interface got a static IP.
  207. (test-assert "networking has started on ovs0"
  208. (marionette-eval
  209. '(begin
  210. (use-modules (gnu services herd)
  211. (srfi srfi-1))
  212. (live-service-running
  213. (find (lambda (live)
  214. (memq 'networking-ovs0
  215. (live-service-provision live)))
  216. (current-services))))
  217. marionette))
  218. (test-end)
  219. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  220. (gexp->derivation "openvswitch-test" test))
  221. (define %test-openvswitch
  222. (system-test
  223. (name "openvswitch")
  224. (description "Test a running OpenvSwitch configuration.")
  225. (value (run-openvswitch-test))))
  226. ;;;
  227. ;;; DHCP Daemon
  228. ;;;
  229. (define minimal-dhcpd-v4-config-file
  230. (plain-file "dhcpd.conf"
  231. "\
  232. default-lease-time 600;
  233. max-lease-time 7200;
  234. subnet 192.168.1.0 netmask 255.255.255.0 {
  235. range 192.168.1.100 192.168.1.200;
  236. option routers 192.168.1.1;
  237. option domain-name-servers 192.168.1.2, 192.168.1.3;
  238. option domain-name \"dummy.domain.name.abc123xyz\";
  239. }
  240. "))
  241. (define dhcpd-v4-configuration
  242. (dhcpd-configuration
  243. (config-file minimal-dhcpd-v4-config-file)
  244. (version "4")
  245. (interfaces '("ens3"))))
  246. (define %dhcpd-os
  247. (simple-operating-system
  248. (static-networking-service "ens3" "192.168.1.4"
  249. #:netmask "255.255.255.0"
  250. #:gateway "192.168.1.1"
  251. #:name-servers '("192.168.1.2" "192.168.1.3"))
  252. (service dhcpd-service-type dhcpd-v4-configuration)))
  253. (define (run-dhcpd-test)
  254. (define os
  255. (marionette-operating-system %dhcpd-os
  256. #:imported-modules '((gnu services herd))))
  257. (define test
  258. (with-imported-modules '((gnu build marionette))
  259. #~(begin
  260. (use-modules (gnu build marionette)
  261. (ice-9 popen)
  262. (ice-9 rdelim)
  263. (srfi srfi-64))
  264. (define marionette
  265. (make-marionette (list #$(virtual-machine os))))
  266. (mkdir #$output)
  267. (chdir #$output)
  268. (test-begin "dhcpd")
  269. (test-assert "pid file exists"
  270. (marionette-eval
  271. '(file-exists?
  272. #$(dhcpd-configuration-pid-file dhcpd-v4-configuration))
  273. marionette))
  274. (test-assert "lease file exists"
  275. (marionette-eval
  276. '(file-exists?
  277. #$(dhcpd-configuration-lease-file dhcpd-v4-configuration))
  278. marionette))
  279. (test-assert "run directory exists"
  280. (marionette-eval
  281. '(file-exists?
  282. #$(dhcpd-configuration-run-directory dhcpd-v4-configuration))
  283. marionette))
  284. (test-assert "dhcpd is alive"
  285. (marionette-eval
  286. '(begin
  287. (use-modules (gnu services herd)
  288. (srfi srfi-1))
  289. (live-service-running
  290. (find (lambda (live)
  291. (memq 'dhcpv4-daemon
  292. (live-service-provision live)))
  293. (current-services))))
  294. marionette))
  295. (test-end)
  296. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  297. (gexp->derivation "dhcpd-test" test))
  298. (define %test-dhcpd
  299. (system-test
  300. (name "dhcpd")
  301. (description "Test a running DHCP daemon configuration.")
  302. (value (run-dhcpd-test))))
  303. ;;;
  304. ;;; Services related to Tor
  305. ;;;
  306. (define %tor-os
  307. (simple-operating-system
  308. (service tor-service-type)))
  309. (define %tor-os/unix-socks-socket
  310. (simple-operating-system
  311. (service tor-service-type
  312. (tor-configuration
  313. (socks-socket-type 'unix)))))
  314. (define (run-tor-test)
  315. (define os
  316. (marionette-operating-system %tor-os
  317. #:imported-modules '((gnu services herd))
  318. #:requirements '(tor)))
  319. (define os/unix-socks-socket
  320. (marionette-operating-system %tor-os/unix-socks-socket
  321. #:imported-modules '((gnu services herd))
  322. #:requirements '(tor)))
  323. (define test
  324. (with-imported-modules '((gnu build marionette))
  325. #~(begin
  326. (use-modules (gnu build marionette)
  327. (ice-9 popen)
  328. (ice-9 rdelim)
  329. (srfi srfi-64))
  330. (define marionette
  331. (make-marionette (list #$(virtual-machine os))))
  332. (define (tor-is-alive? marionette)
  333. (marionette-eval
  334. '(begin
  335. (use-modules (gnu services herd)
  336. (srfi srfi-1))
  337. (live-service-running
  338. (find (lambda (live)
  339. (memq 'tor
  340. (live-service-provision live)))
  341. (current-services))))
  342. marionette))
  343. (mkdir #$output)
  344. (chdir #$output)
  345. (test-begin "tor")
  346. ;; Test the usual Tor service.
  347. (test-assert "tor is alive"
  348. (tor-is-alive? marionette))
  349. (test-assert "tor is listening"
  350. (let ((default-port 9050))
  351. (wait-for-tcp-port default-port marionette)))
  352. ;; Don't run two VMs at once.
  353. (marionette-control "quit" marionette)
  354. ;; Test the Tor service using a SOCKS socket.
  355. (let* ((socket-directory "/tmp/more-sockets")
  356. (_ (mkdir socket-directory))
  357. (marionette/unix-socks-socket
  358. (make-marionette
  359. (list #$(virtual-machine os/unix-socks-socket))
  360. ;; We can't use the same socket directory as the first
  361. ;; marionette.
  362. #:socket-directory socket-directory)))
  363. (test-assert "tor is alive, even when using a SOCKS socket"
  364. (tor-is-alive? marionette/unix-socks-socket))
  365. (test-assert "tor is listening, even when using a SOCKS socket"
  366. (wait-for-unix-socket "/var/run/tor/socks-sock"
  367. marionette/unix-socks-socket)))
  368. (test-end)
  369. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  370. (gexp->derivation "tor-test" test))
  371. (define %test-tor
  372. (system-test
  373. (name "tor")
  374. (description "Test a running Tor daemon configuration.")
  375. (value (run-tor-test))))
  376. (define* (run-iptables-test)
  377. "Run tests of 'iptables-service-type'."
  378. (define iptables-rules
  379. "*filter
  380. :INPUT ACCEPT
  381. :FORWARD ACCEPT
  382. :OUTPUT ACCEPT
  383. -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp-port-unreachable
  384. COMMIT
  385. ")
  386. (define ip6tables-rules
  387. "*filter
  388. :INPUT ACCEPT
  389. :FORWARD ACCEPT
  390. :OUTPUT ACCEPT
  391. -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp6-port-unreachable
  392. COMMIT
  393. ")
  394. (define inetd-echo-port 7)
  395. (define os
  396. (marionette-operating-system
  397. (simple-operating-system
  398. (service dhcp-client-service-type)
  399. (service inetd-service-type
  400. (inetd-configuration
  401. (entries (list
  402. (inetd-entry
  403. (name "echo")
  404. (socket-type 'stream)
  405. (protocol "tcp")
  406. (wait? #f)
  407. (user "root"))))))
  408. (service iptables-service-type
  409. (iptables-configuration
  410. (ipv4-rules (plain-file "iptables.rules" iptables-rules))
  411. (ipv6-rules (plain-file "ip6tables.rules" ip6tables-rules)))))
  412. #:imported-modules '((gnu services herd))
  413. #:requirements '(inetd iptables)))
  414. (define test
  415. (with-imported-modules '((gnu build marionette))
  416. #~(begin
  417. (use-modules (srfi srfi-64)
  418. (gnu build marionette))
  419. (define marionette
  420. (make-marionette (list #$(virtual-machine os))))
  421. (define (dump-iptables iptables-save marionette)
  422. (marionette-eval
  423. `(begin
  424. (use-modules (ice-9 popen)
  425. (ice-9 rdelim)
  426. (ice-9 regex))
  427. (call-with-output-string
  428. (lambda (out)
  429. (call-with-port
  430. (open-pipe* OPEN_READ ,iptables-save)
  431. (lambda (in)
  432. (let loop ((line (read-line in)))
  433. ;; iptables-save does not output rules in the exact
  434. ;; same format we loaded using iptables-restore. It
  435. ;; adds comments, packet counters, etc. We remove
  436. ;; these additions.
  437. (unless (eof-object? line)
  438. (cond
  439. ;; Remove comments
  440. ((string-match "^#" line) #t)
  441. ;; Remove packet counters
  442. ((string-match "^:([A-Z]*) ([A-Z]*) .*" line)
  443. => (lambda (match-record)
  444. (format out ":~a ~a~%"
  445. (match:substring match-record 1)
  446. (match:substring match-record 2))))
  447. ;; Pass other lines without modification
  448. (else (display line out)
  449. (newline out)))
  450. (loop (read-line in)))))))))
  451. marionette))
  452. (mkdir #$output)
  453. (chdir #$output)
  454. (test-begin "iptables")
  455. (test-equal "iptables-save dumps the same rules that were loaded"
  456. (dump-iptables #$(file-append iptables "/sbin/iptables-save")
  457. marionette)
  458. #$iptables-rules)
  459. (test-equal "ip6tables-save dumps the same rules that were loaded"
  460. (dump-iptables #$(file-append iptables "/sbin/ip6tables-save")
  461. marionette)
  462. #$ip6tables-rules)
  463. (test-error "iptables firewall blocks access to inetd echo service"
  464. 'misc-error
  465. (wait-for-tcp-port inetd-echo-port marionette #:timeout 5))
  466. ;; TODO: This test freezes up at the login prompt without any
  467. ;; relevant messages on the console. Perhaps it is waiting for some
  468. ;; timeout. Find and fix this issue.
  469. ;; (test-assert "inetd echo service is accessible after iptables firewall is stopped"
  470. ;; (begin
  471. ;; (marionette-eval
  472. ;; '(begin
  473. ;; (use-modules (gnu services herd))
  474. ;; (stop-service 'iptables))
  475. ;; marionette)
  476. ;; (wait-for-tcp-port inetd-echo-port marionette #:timeout 5)))
  477. (test-end)
  478. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  479. (gexp->derivation "iptables" test))
  480. (define %test-iptables
  481. (system-test
  482. (name "iptables")
  483. (description "Test a running iptables daemon.")
  484. (value (run-iptables-test))))
  485. ;;;
  486. ;;; IPFS service
  487. ;;;
  488. (define %ipfs-os
  489. (simple-operating-system
  490. (service ipfs-service-type)))
  491. (define (run-ipfs-test)
  492. (define os
  493. (marionette-operating-system %ipfs-os
  494. #:imported-modules '((gnu services herd)
  495. (guix ipfs))
  496. #:requirements '(ipfs)))
  497. (define test
  498. (with-imported-modules '((gnu build marionette))
  499. #~(begin
  500. (use-modules (gnu build marionette)
  501. (srfi srfi-64))
  502. (define marionette
  503. (make-marionette (list #$(virtual-machine os))))
  504. (define (ipfs-is-alive?)
  505. (marionette-eval
  506. '(begin
  507. (use-modules (gnu services herd)
  508. (srfi srfi-1))
  509. (live-service-running
  510. (find (lambda (live)
  511. (memq 'ipfs
  512. (live-service-provision live)))
  513. (current-services))))
  514. marionette))
  515. ;; The default API endpoint port 5001 is used,
  516. ;; so there is no need to parameterize %ipfs-base-url.
  517. ;; By running this within the VM instead of outside the VM
  518. ;; this system test does not have to forward any ports.
  519. (define (add-data data)
  520. (marionette-eval
  521. `((@ (guix ipfs) add-contents) ,data)))
  522. (define (read-contents object)
  523. (marionette-eval
  524. `((@ (guix ipfs) read-contents) ,object)))
  525. (test-begin "ipfs")
  526. ;; Test the IPFS service.
  527. (test-assert "ipfs is alive" (ipfs-is-alive?))
  528. (test-assert "ipfs is listening on the gateway"
  529. (let ((default-port 8082))
  530. (wait-for-tcp-port default-port marionette)))
  531. (test-assert "ipfs is listening on the API endpoint"
  532. (let ((default-port 5001))
  533. (wait-for-tcp-port default-port marionette)))
  534. (define test-bv (string->utf8 "hello ipfs!"))
  535. (test-equal "can upload and download a file to/from ipfs"
  536. test-bv
  537. (read-contents (add-data test-bv)))
  538. (test-end)
  539. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  540. (gexp->derivation "ipfs-test" test))
  541. (define %test-ipfs
  542. (system-test
  543. (name "ipfs")
  544. (description "Test a running IPFS daemon configuration.")
  545. (value (run-ipfs-test))))