services: openntpd: Add test for issue #3731.
[jackhill/guix/guix.git] / tests / networking.scm
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
19 (define-module (tests networking)
20 #:use-module (ice-9 regex)
21 #:use-module (gnu services networking)
22 #:use-module (srfi srfi-64))
23
24 ;;; Tests for the (gnu services networking) module.
25
26 (test-begin "networking")
27
28 \f
29 ;;;
30 ;;; NTP.
31 ;;;
32
33 (define ntp-server->string (@@ (gnu services networking) ntp-server->string))
34
35 (define %ntp-server-sample
36 (ntp-server
37 (type 'server)
38 (address "some.ntp.server.org")
39 (options `(iburst (version 3) (maxpoll 16) prefer))))
40
41 (test-equal "ntp-server->string"
42 (ntp-server->string %ntp-server-sample)
43 "server some.ntp.server.org iburst version 3 maxpoll 16 prefer")
44
45 (test-equal "ntp configuration servers deprecated form"
46 (ntp-configuration-servers
47 (ntp-configuration
48 (servers (list (ntp-server
49 (type 'server)
50 (address "example.pool.ntp.org")
51 (options '()))))))
52 (ntp-configuration-servers
53 (ntp-configuration
54 (servers (list "example.pool.ntp.org")))))
55
56 \f
57 ;;;
58 ;;; OpenNTPD
59 ;;;
60
61 (define openntpd-configuration->string (@@ (gnu services networking)
62 openntpd-configuration->string))
63
64 (define %openntpd-conf-sample
65 (openntpd-configuration
66 (server '("0.guix.pool.ntp.org" "1.guix.pool.ntp.org"))
67 (listen-on '("127.0.0.1" "::1"))
68 (sensor '("udcf0 correction 70000"))
69 (constraint-from '("www.gnu.org"))
70 (constraints-from '("https://www.google.com/"))
71 (allow-large-adjustment? #t)))
72
73 (test-assert "openntpd configuration generation sanity check"
74
75 (begin
76 (define (string-match/newline pattern text)
77 (regexp-exec (make-regexp pattern regexp/newline) text))
78
79 (define (match-count pattern text)
80 (fold-matches (make-regexp pattern regexp/newline) text 0
81 (lambda (match count)
82 (1+ count))))
83
84 (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
85 (if (not
86 (and (string-match/newline "^listen on 127.0.0.1$" config)
87 (string-match/newline "^listen on ::1$" config)
88 (string-match/newline "^sensor udcf0 correction 70000$" config)
89 (string-match/newline "^constraint from www.gnu.org$" config)
90 (string-match/newline "^server 0.guix.pool.ntp.org$" config)
91 (string-match/newline
92 "^constraints from \"https://www.google.com/\"$"
93 config)
94
95 ;; Check for issue #3731 (see:
96 ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=37318).
97 (= (match-count "^listen on " config) 2)
98 (= (match-count "^sensor " config) 1)
99 (= (match-count "^constraint from " config) 1)
100 (= (match-count "^server " config) 2)
101 (= (match-count "^constraints from " config) 1)))
102 (begin
103 (format #t "The configuration below failed \
104 the sanity check:\n~a~%" config)
105 #f)
106 #t))))
107
108 (test-equal "openntpd generated config string ends with a newline"
109 (let ((config (openntpd-configuration->string %openntpd-conf-sample)))
110 (string-take-right config 1))
111 "\n")
112
113 (test-end "networking")