networking.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. (test-equal "ntp configuration servers deprecated form"
  38. (ntp-configuration-servers
  39. (ntp-configuration
  40. (servers (list "example.pool.ntp.org"))))
  41. (ntp-configuration-servers
  42. (ntp-configuration
  43. (servers (list (ntp-server
  44. (type 'server)
  45. (address "example.pool.ntp.org")
  46. (options '())))))))
  47. ;;;
  48. ;;; OpenNTPD
  49. ;;;
  50. (define openntpd-configuration->string (@@ (gnu services networking)
  51. openntpd-configuration->string))
  52. (define %openntpd-conf-sample
  53. (openntpd-configuration
  54. (server '("0.guix.pool.ntp.org" "1.guix.pool.ntp.org"))
  55. (listen-on '("127.0.0.1" "::1"))
  56. (sensor '("udcf0 correction 70000"))
  57. (constraint-from '("www.gnu.org"))
  58. (constraints-from '("https://www.google.com/"))
  59. (allow-large-adjustment? #t)))
  60. (test-assert "openntpd configuration generation sanity check"
  61. (begin
  62. (define (string-match/newline pattern text)
  63. (regexp-exec (make-regexp pattern regexp/newline) text))
  64. (define (match-count pattern text)
  65. (fold-matches (make-regexp pattern regexp/newline) text 0
  66. (lambda (match count)
  67. (1+ count))))
  68. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  69. (if (not
  70. (and (string-match/newline "^listen on 127.0.0.1$" config)
  71. (string-match/newline "^listen on ::1$" config)
  72. (string-match/newline "^sensor udcf0 correction 70000$" config)
  73. (string-match/newline "^constraint from www.gnu.org$" config)
  74. (string-match/newline "^server 0.guix.pool.ntp.org$" config)
  75. (string-match/newline
  76. "^constraints from \"https://www.google.com/\"$"
  77. config)
  78. ;; Check for issue #3731 (see:
  79. ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=37318).
  80. (= (match-count "^listen on " config) 2)
  81. (= (match-count "^sensor " config) 1)
  82. (= (match-count "^constraint from " config) 1)
  83. (= (match-count "^server " config) 2)
  84. (= (match-count "^constraints from " config) 1)))
  85. (begin
  86. (format #t "The configuration below failed \
  87. the sanity check:\n~a~%" config)
  88. #f)
  89. #t))))
  90. (test-equal "openntpd generated config string ends with a newline"
  91. "\n"
  92. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  93. (string-take-right config 1)))
  94. (test-end "networking")