1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
32 ;; Operating system with 2 inetd services.
33 (simple-operating-system
35 (service inetd-service-type
50 (program (file-append bash
53 (list "bash" (plain-file "my-dict.sh" "\
56 if [[ $line =~ ^DEFINE\\ (.*)$ ]]
58 case ${BASH_REMATCH[1]} in
60 echo GNU Guix is a package management tool for the GNU system.
63 echo Like an S-expression but with a G.
66 echo NO DEFINITION FOUND
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 (mlet* %store-monad ((os -> (marionette-operating-system %inetd-os))
78 (command (system-qemu-image/shared-store-script
81 (with-imported-modules '((gnu build marionette))
83 (use-modules (ice-9 rdelim)
85 (gnu build marionette))
87 ;; Forward guest ports 7 and 2628 to host ports 8007 and 8628.
88 (make-marionette (list #$command "-net"
91 ",hostfwd=tcp::8007-:7"
92 ",hostfwd=tcp::8628-:2628"))))
99 ;; Make sure the PID file is created.
100 (test-assert "PID file"
102 '(file-exists? "/var/run/inetd.pid")
105 ;; Test the echo service.
106 (test-equal "echo response"
108 (let ((echo (socket PF_INET SOCK_STREAM 0))
109 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
111 (display "Hello, Guix!\n" echo)
112 (let ((response (read-line echo)))
116 ;; Test the dict service
117 (test-equal "dict response"
118 "GNU Guix is a package management tool for the GNU system."
119 (let ((dict (socket PF_INET SOCK_STREAM 0))
120 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
122 (display "DEFINE Guix\n" dict)
123 (let ((response (read-line dict)))
128 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
130 (gexp->derivation "inetd-test" test)))
135 (description "Connect to a host with an INETD server.")
136 (value (run-inetd-test))))