1package publictls
2
3import (
4 "testing"
5 "time"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/bnema/gordon/internal/domain"
11)
12
13func TestDefaultDNSConfig(t *testing.T) {
14 cfg := DefaultDNSConfig()
15
16 assert.Equal(t, []string{"1.1.1.1:53", "8.8.8.8:53"}, cfg.Resolvers)
17 assert.Equal(t, 5*time.Minute, cfg.PropagationTimeout)
18 assert.Equal(t, 5*time.Second, cfg.PollingInterval)
19}
20
21func TestDNSConfigValidateNormalizesResolvers(t *testing.T) {
22 cfg := DNSConfig{
23 Resolvers: []string{" 1.1.1.1:53 ", "\t8.8.8.8:53\n"},
24 PropagationTimeout: 5 * time.Minute,
25 PollingInterval: 5 * time.Second,
26 }
27
28 require.NoError(t, cfg.Validate())
29 assert.Equal(t, []string{"1.1.1.1:53", "8.8.8.8:53"}, cfg.Resolvers)
30}
31
32func TestDNSConfigValidate(t *testing.T) {
33 tests := []struct {
34 name string
35 cfg DNSConfig
36 wantErr string
37 }{
38 {
39 name: "valid",
40 cfg: DNSConfig{
41 Resolvers: []string{"1.1.1.1:53", "8.8.8.8:53"},
42 PropagationTimeout: 5 * time.Minute,
43 PollingInterval: 5 * time.Second,
44 },
45 },
46 {
47 name: "empty resolvers",
48 cfg: DNSConfig{
49 Resolvers: nil,
50 PropagationTimeout: 5 * time.Minute,
51 PollingInterval: 5 * time.Second,
52 },
53 wantErr: "dns.resolvers must contain at least one resolver",
54 },
55 {
56 name: "resolver missing port",
57 cfg: DNSConfig{
58 Resolvers: []string{"1.1.1.1"},
59 PropagationTimeout: 5 * time.Minute,
60 PollingInterval: 5 * time.Second,
61 },
62 wantErr: "invalid dns.resolvers[0]",
63 },
64 {
65 name: "whitespace-only resolver",
66 cfg: DNSConfig{
67 Resolvers: []string{" "},
68 PropagationTimeout: 5 * time.Minute,
69 PollingInterval: 5 * time.Second,
70 },
71 wantErr: "invalid dns.resolvers[0]",
72 },
73 {
74 name: "malformed resolver port",
75 cfg: DNSConfig{
76 Resolvers: []string{"1.1.1.1:abc"},
77 PropagationTimeout: 5 * time.Minute,
78 PollingInterval: 5 * time.Second,
79 },
80 wantErr: "invalid dns.resolvers[0]",
81 },
82 {
83 name: "zero timeout",
84 cfg: DNSConfig{
85 Resolvers: []string{"1.1.1.1:53"},
86 PropagationTimeout: 0,
87 PollingInterval: 5 * time.Second,
88 },
89 wantErr: "dns.propagation_timeout must be positive",
90 },
91 {
92 name: "zero polling interval",
93 cfg: DNSConfig{
94 Resolvers: []string{"1.1.1.1:53"},
95 PropagationTimeout: 5 * time.Minute,
96 PollingInterval: 0,
97 },
98 wantErr: "dns.polling_interval must be positive",
99 },
100 {
101 name: "polling interval equals timeout",
102 cfg: DNSConfig{
103 Resolvers: []string{"1.1.1.1:53"},
104 PropagationTimeout: 5 * time.Second,
105 PollingInterval: 5 * time.Second,
106 },
107 wantErr: "dns.polling_interval must be less than dns.propagation_timeout",
108 },
109 }
110
111 for _, tt := range tests {
112 t.Run(tt.name, func(t *testing.T) {
113 err := tt.cfg.Validate()
114 if tt.wantErr == "" {
115 require.NoError(t, err)
116 return
117 }
118 require.Error(t, err)
119 assert.ErrorIs(t, err, domain.ErrDNSConfigInvalid)
120 assert.Contains(t, err.Error(), tt.wantErr)
121 })
122 }
123}