Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / tests / networking.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu tests networking)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system vm)
23 #:use-module (gnu services)
24 #:use-module (gnu services networking)
25 #:use-module (guix gexp)
26 #:use-module (guix store)
27 #:use-module (guix monads)
28 #:use-module (gnu packages bash)
29 #:export (%test-inetd))
30
31 (define %inetd-os
32 ;; Operating system with 2 inetd services.
33 (simple-operating-system
34 (dhcp-client-service)
35 (service inetd-service-type
36 (inetd-configuration
37 (entries (list
38 (inetd-entry
39 (name "echo")
40 (socket-type 'stream)
41 (protocol "tcp")
42 (wait? #f)
43 (user "root"))
44 (inetd-entry
45 (name "dict")
46 (socket-type 'stream)
47 (protocol "tcp")
48 (wait? #f)
49 (user "root")
50 (program (file-append bash
51 "/bin/bash"))
52 (arguments
53 (list "bash" (plain-file "my-dict.sh" "\
54 while read line
55 do
56 if [[ $line =~ ^DEFINE\\ (.*)$ ]]
57 then
58 case ${BASH_REMATCH[1]} in
59 Guix)
60 echo GNU Guix is a package management tool for the GNU system.
61 ;;
62 G-expression)
63 echo Like an S-expression but with a G.
64 ;;
65 *)
66 echo NO DEFINITION FOUND
67 ;;
68 esac
69 else
70 echo ERROR
71 fi
72 done" ))))))))))
73
74 (define* (run-inetd-test)
75 "Run tests in %INETD-OS, where the inetd service provides an echo service on
76 port 7, and a dict service on port 2628."
77 (define os
78 (marionette-operating-system %inetd-os))
79
80 (define vm
81 (virtual-machine
82 (operating-system os)
83 (port-forwardings `((8007 . 7)
84 (8628 . 2628)))))
85
86 (define test
87 (with-imported-modules '((gnu build marionette))
88 #~(begin
89 (use-modules (ice-9 rdelim)
90 (srfi srfi-64)
91 (gnu build marionette))
92 (define marionette
93 (make-marionette (list #$vm)))
94
95 (mkdir #$output)
96 (chdir #$output)
97
98 (test-begin "inetd")
99
100 ;; Make sure the PID file is created.
101 (test-assert "PID file"
102 (marionette-eval
103 '(file-exists? "/var/run/inetd.pid")
104 marionette))
105
106 ;; Test the echo service.
107 (test-equal "echo response"
108 "Hello, Guix!"
109 (let ((echo (socket PF_INET SOCK_STREAM 0))
110 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
111 (connect echo addr)
112 (display "Hello, Guix!\n" echo)
113 (let ((response (read-line echo)))
114 (close echo)
115 response)))
116
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
128 (test-end)
129 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
130
131 (gexp->derivation "inetd-test" test))
132
133 (define %test-inetd
134 (system-test
135 (name "inetd")
136 (description "Connect to a host with an INETD server.")
137 (value (run-inetd-test))))