networking.scm 22 KB

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