-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaddyfile_test.go
More file actions
225 lines (209 loc) · 3.65 KB
/
Copy pathcaddyfile_test.go
File metadata and controls
225 lines (209 loc) · 3.65 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package shadowtls
import (
"encoding/json"
"strings"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddytest"
_ "github.com/caddyserver/caddy/v2/modules/standard"
)
func TestCaddyfileAdaptV3(t *testing.T) {
caddytest.AssertAdapt(t, `
{
skip_install_trust
admin localhost:2999
http_port 9080
https_port 9443
shadowtls {
version 3
user alice secret1
user bob secret2
strict_mode
}
servers :9443 {
listener_wrappers {
shadowtls {
handshake_server www.example.com:443
handshake_for_server_name custom.example.com alt.example.com:443
}
}
}
}
:9443 {
respond "hello"
}
`, "caddyfile", `{
"admin": {
"listen": "localhost:2999"
},
"apps": {
"http": {
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9443"
],
"listener_wrappers": [
{
"handshake_for_server_name": {
"custom.example.com": "alt.example.com:443"
},
"handshake_server": "www.example.com:443",
"wrapper": "shadowtls"
}
],
"routes": [
{
"handle": [
{
"body": "hello",
"handler": "static_response"
}
]
}
]
}
}
},
"pki": {
"certificate_authorities": {
"local": {
"install_trust": false
}
}
},
"shadowtls": {
"version": 3,
"users": [
{
"name": "alice",
"password": "secret1"
},
{
"name": "bob",
"password": "secret2"
}
],
"strict_mode": true
}
}
}`)
}
func TestCaddyfileAdaptV2(t *testing.T) {
caddytest.AssertAdapt(t, `
{
skip_install_trust
admin localhost:2999
http_port 9080
https_port 9443
shadowtls {
version 2
password my-password
}
servers :9443 {
listener_wrappers {
shadowtls {
handshake_server www.example.com:443
}
}
}
}
:9443 {
respond "hello"
}
`, "caddyfile", `{
"admin": {
"listen": "localhost:2999"
},
"apps": {
"http": {
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9443"
],
"listener_wrappers": [
{
"handshake_server": "www.example.com:443",
"wrapper": "shadowtls"
}
],
"routes": [
{
"handle": [
{
"body": "hello",
"handler": "static_response"
}
]
}
]
}
}
},
"pki": {
"certificate_authorities": {
"local": {
"install_trust": false
}
}
},
"shadowtls": {
"version": 2,
"password": "my-password"
}
}
}`)
}
func TestLoadConfigV3NoUsers(t *testing.T) {
config := json.RawMessage(`{
"apps": {
"shadowtls": {
"version": 3
}
}
}`)
err := caddy.Load(config, true)
if err == nil {
t.Fatal("expected error for v3 with no users")
}
if got := err.Error(); !strings.Contains(got, "shadowtls v3 requires at least one user") {
t.Fatalf("unexpected error: %s", got)
}
}
func TestLoadConfigV2NoPassword(t *testing.T) {
config := json.RawMessage(`{
"apps": {
"shadowtls": {
"version": 2
}
}
}`)
err := caddy.Load(config, true)
if err == nil {
t.Fatal("expected error for v2 with no password")
}
if got := err.Error(); !strings.Contains(got, "shadowtls v2 requires a password") {
t.Fatalf("unexpected error: %s", got)
}
}
func TestLoadConfigInvalidVersion(t *testing.T) {
config := json.RawMessage(`{
"apps": {
"shadowtls": {
"version": 5
}
}
}`)
err := caddy.Load(config, true)
if err == nil {
t.Fatal("expected error for invalid version")
}
if got := err.Error(); !strings.Contains(got, "unknown shadowtls version") {
t.Fatalf("unexpected error: %s", got)
}
}