networking.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. (test-assert "openntpd configuration generation sanity check"
  60. (begin
  61. (define (string-match/newline pattern text)
  62. (regexp-exec (make-regexp pattern regexp/newline) text))
  63. (define (match-count pattern text)
  64. (fold-matches (make-regexp pattern regexp/newline) text 0
  65. (lambda (match count)
  66. (1+ count))))
  67. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  68. (if (not
  69. (and (string-match/newline "^listen on 127.0.0.1$" config)
  70. (string-match/newline "^listen on ::1$" config)
  71. (string-match/newline "^sensor udcf0 correction 70000$" config)
  72. (string-match/newline "^constraint from www.gnu.org$" config)
  73. (string-match/newline "^server 0.guix.pool.ntp.org$" config)
  74. (string-match/newline
  75. "^constraints from \"https://www.google.com/\"$"
  76. config)
  77. ;; Check for issue #3731 (see:
  78. ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=37318).
  79. (= (match-count "^listen on " config) 2)
  80. (= (match-count "^sensor " config) 1)
  81. (= (match-count "^constraint from " config) 1)
  82. (= (match-count "^server " config) 2)
  83. (= (match-count "^constraints from " config) 1)))
  84. (begin
  85. (format #t "The configuration below failed \
  86. the sanity check:\n~a~%" config)
  87. #f)
  88. #t))))
  89. (test-equal "openntpd generated config string ends with a newline"
  90. "\n"
  91. (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
  92. (string-take-right config 1)))
  93. (test-end "networking")