networking.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  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 (tests networking)
  19. #:use-module (ice-9 regex)
  20. #:use-module (gnu services networking)
  21. #:use-module (srfi srfi-64))
  22. ;;; Tests for the (gnu services networking) module.
  23. (test-begin "networking")
  24. ;;;
  25. ;;; NTP.
  26. ;;;
  27. (define ntp-server->string (@@ (gnu services networking) ntp-server->string))
  28. (define %ntp-server-sample
  29. (ntp-server
  30. (type 'server)
  31. (address "some.ntp.server.org")
  32. ;; Using either strings or symbols for option names is accepted.
  33. (options `("iburst" (version 3) (maxpoll 16) prefer))))
  34. (test-equal "ntp-server->string"
  35. "server some.ntp.server.org iburst version 3 maxpoll 16 prefer"
  36. (ntp-server->string %ntp-server-sample))
  37. ;;;
  38. ;;; OpenNTPD
  39. ;;;
  40. (define openntpd-configuration->string (@@ (gnu services networking)
  41. openntpd-configuration->string))
  42. (define %openntpd-conf-sample
  43. (openntpd-configuration
  44. (server '("0.guix.pool.ntp.org" "1.guix.pool.ntp.org"))
  45. (listen-on '("127.0.0.1" "::1"))
  46. (sensor '("udcf0 correction 70000"))
  47. (constraint-from '("www.gnu.org"))
  48. (constraints-from '("https://www.google.com/"))))
  49. (test-assert "openntpd configuration generation sanity check"
  50. (begin
  51. (define (string-match/newline pattern text)
  52. (regexp-exec (make-regexp pattern regexp/newline) text))
  53. (define (match-count pattern text)
  54. (fold-matches (make-regexp pattern regexp/newline) text 0
  55. (lambda (match count)
  56. (1+ count))))
  57. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  58. (if (not
  59. (and (string-match/newline "^listen on 127.0.0.1$" config)
  60. (string-match/newline "^listen on ::1$" config)
  61. (string-match/newline "^sensor udcf0 correction 70000$" config)
  62. (string-match/newline "^constraint from www.gnu.org$" config)
  63. (string-match/newline "^server 0.guix.pool.ntp.org$" config)
  64. (string-match/newline
  65. "^constraints from \"https://www.google.com/\"$"
  66. config)
  67. ;; Check for issue #3731 (see:
  68. ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=37318).
  69. (= (match-count "^listen on " config) 2)
  70. (= (match-count "^sensor " config) 1)
  71. (= (match-count "^constraint from " config) 1)
  72. (= (match-count "^server " config) 2)
  73. (= (match-count "^constraints from " config) 1)))
  74. (begin
  75. (format #t "The configuration below failed \
  76. the sanity check:\n~a~%" config)
  77. #f)
  78. #t))))
  79. (test-equal "openntpd generated config string ends with a newline"
  80. "\n"
  81. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  82. (string-take-right config 1)))
  83. (test-end "networking")