networking.scm 22 KB

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