networking.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. (define-module (gnu tests networking)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system vm)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services networking)
  24. #:use-module (guix gexp)
  25. #:use-module (guix store)
  26. #:use-module (guix monads)
  27. #:use-module (gnu packages bash)
  28. #:export (%test-inetd))
  29. (define %inetd-os
  30. ;; Operating system with 2 inetd services.
  31. (simple-operating-system
  32. (dhcp-client-service)
  33. (service inetd-service-type
  34. (inetd-configuration
  35. (entries (list
  36. (inetd-entry
  37. (name "echo")
  38. (socket-type 'stream)
  39. (protocol "tcp")
  40. (wait? #f)
  41. (user "root"))
  42. (inetd-entry
  43. (name "dict")
  44. (socket-type 'stream)
  45. (protocol "tcp")
  46. (wait? #f)
  47. (user "root")
  48. (program (file-append bash
  49. "/bin/bash"))
  50. (arguments
  51. (list "bash" (plain-file "my-dict.sh" "\
  52. while read line
  53. do
  54. if [[ $line =~ ^DEFINE\\ (.*)$ ]]
  55. then
  56. case ${BASH_REMATCH[1]} in
  57. Guix)
  58. echo GNU Guix is a package management tool for the GNU system.
  59. ;;
  60. G-expression)
  61. echo Like an S-expression but with a G.
  62. ;;
  63. *)
  64. echo NO DEFINITION FOUND
  65. ;;
  66. esac
  67. else
  68. echo ERROR
  69. fi
  70. done" ))))))))))
  71. (define* (run-inetd-test)
  72. "Run tests in %INETD-OS, where the inetd service provides an echo service on
  73. port 7, and a dict service on port 2628."
  74. (define os
  75. (marionette-operating-system %inetd-os))
  76. (define vm
  77. (virtual-machine
  78. (operating-system os)
  79. (port-forwardings `((8007 . 7)
  80. (8628 . 2628)))))
  81. (define test
  82. (with-imported-modules '((gnu build marionette))
  83. #~(begin
  84. (use-modules (ice-9 rdelim)
  85. (srfi srfi-64)
  86. (gnu build marionette))
  87. (define marionette
  88. (make-marionette (list #$vm)))
  89. (mkdir #$output)
  90. (chdir #$output)
  91. (test-begin "inetd")
  92. ;; Make sure the PID file is created.
  93. (test-assert "PID file"
  94. (marionette-eval
  95. '(file-exists? "/var/run/inetd.pid")
  96. marionette))
  97. ;; Test the echo service.
  98. (test-equal "echo response"
  99. "Hello, Guix!"
  100. (let ((echo (socket PF_INET SOCK_STREAM 0))
  101. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
  102. (connect echo addr)
  103. (display "Hello, Guix!\n" echo)
  104. (let ((response (read-line echo)))
  105. (close echo)
  106. response)))
  107. ;; Test the dict service
  108. (test-equal "dict response"
  109. "GNU Guix is a package management tool for the GNU system."
  110. (let ((dict (socket PF_INET SOCK_STREAM 0))
  111. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
  112. (connect dict addr)
  113. (display "DEFINE Guix\n" dict)
  114. (let ((response (read-line dict)))
  115. (close dict)
  116. response)))
  117. (test-end)
  118. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  119. (gexp->derivation "inetd-test" test))
  120. (define %test-inetd
  121. (system-test
  122. (name "inetd")
  123. (description "Connect to a host with an INETD server.")
  124. (value (run-inetd-test))))