-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup_test.go
184 lines (179 loc) · 4.64 KB
/
setup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package https
import (
"crypto/tls"
"strings"
"testing"
"github.com/coredns/caddy"
"github.com/stretchr/testify/require"
)
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
input string
expectedConfig *httpsConfig
}{
{
name: "FromAndOneToURL",
input: "https . example.com/dns-query",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
},
},
{
name: "FromAndTwoToURLs",
input: "https . example.com/dns-query example.org/dns-query",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query", "https://example.org/dns-query"},
},
},
{
name: "ExceptProperty",
input: "https . example.com/dns-query {\nexcept domain.com\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
except: []string{"domain.com."},
},
},
{
name: "ExceptPropertyURL",
input: "https . example.com/dns-query {\nexcept https://domain.com\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
except: []string{"domain.com."},
},
},
{
name: "ExceptPropertyTwoDomains",
input: "https . example.com/dns-query {\nexcept domain1.com domain2.com\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
except: []string{"domain1.com.", "domain2.com."},
},
},
{
name: "TLSServerNameProperty",
input: "https . 10.1.1.1:853/dns-query {\ntls_servername internal.domain\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://10.1.1.1:853/dns-query"},
tlsConfig: &tls.Config{ServerName: "internal.domain"},
tlsServerName: "internal.domain",
},
},
{
name: "PolicyPropertyRandom",
input: "https . example.com/dns-query {\npolicy random\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
policy: newRandomPolicy(),
},
},
{
name: "PolicyPropertyRoundRobin",
input: "https . example.com/dns-query {\npolicy round_robin\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
policy: newRoundRobinPolicy(),
},
},
{
name: "PolicyPropertySequential",
input: "https . example.com/dns-query {\npolicy sequential\n}\n",
expectedConfig: &httpsConfig{
from: ".",
toURLs: []string{"https://example.com/dns-query"},
policy: newSequentialPolicy(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := caddy.NewTestController("https", tt.input)
conf, err := parseConfig(c)
require.NoError(t, err)
require.Equal(t, tt.expectedConfig, conf)
})
}
}
func TestParseConfigTLSProperty(t *testing.T) {
input := "https . example.com/dns-query {\ntls\n}\n"
c := caddy.NewTestController("https", input)
conf, err := parseConfig(c)
require.NoError(t, err)
require.Equal(t, ".", conf.from)
require.Equal(t, []string{"https://example.com/dns-query"}, conf.toURLs)
require.NotNil(t, conf.tlsConfig)
}
func TestParseConfigError(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "EmptyLine",
input: "",
},
{
name: "EmptyFrom",
input: "https",
},
{
name: "EmptyToURLs",
input: "https .",
},
{
name: "InvalidToURL",
input: "https . abc:&",
},
{
name: "TooManyToURLs",
input: "https . " + strings.Repeat("example.com/dns-query ", maxUpstreams+1),
},
{
name: "UnknownProperty",
input: "https . example.com/dns-query {\nabc\n}\n",
},
{
name: "ExceptPropertyZeroArgs",
input: "https . example.com/dns-query {\nexcept\n}\n",
},
{
name: "TLSPropertyTooManyArgs",
input: "https . example.com/dns-query {\ntls abc def ghi qwe\n}\n",
},
{
name: "TLSServerNamePropertyZeroArgs",
input: "https . example.com/dns-query {\ntls_servername\n}\n",
},
{
name: "TLSServerNamePropertyTooManyArgs",
input: "https . example.com/dns-query {\ntls_servername abc.com def.com\n}\n",
},
{
name: "PolicyPropertyZeroArgs",
input: "https . example.com/dns-query {\npolicy\n}\n",
},
{
name: "PolicyPropertyTooManyArgs",
input: "https . example.com/dns-query {\npolicy random round_robin\n}\n",
},
{
name: "PolicyPropertyUnknownArg",
input: "https . example.com/dns-query {\npolicy abc\n}\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := caddy.NewTestController("https", tt.input)
_, err := parseConfig(c)
require.Error(t, err)
})
}
}