cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
98deb95eaec1a2e2e43320e526e6a192a4eba249

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

config/configuration_test.go

215lines · modecode

1package config
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 yaml "gopkg.in/yaml.v2"
11)
12
13func TestConfigFileSettings(t *testing.T) {
14 var (
15 firstIngress = UnvalidatedIngressRule{
16 Hostname: "tunnel1.example.com",
17 Path: "/id",
18 Service: "https://localhost:8000",
19 }
20 secondIngress = UnvalidatedIngressRule{
21 Hostname: "*",
22 Path: "",
23 Service: "https://localhost:8001",
24 }
25 warpRouting = WarpRoutingConfig{
26 Enabled: true,
27 }
28 )
29 rawYAML := `
30tunnel: config-file-test
31originRequest:
32 ipRules:
33 - prefix: "10.0.0.0/8"
34 ports:
35 - 80
36 - 8080
37 allow: false
38 - prefix: "fc00::/7"
39 ports:
40 - 443
41 - 4443
42 allow: true
43ingress:
44 - hostname: tunnel1.example.com
45 path: /id
46 service: https://localhost:8000
47 - hostname: "*"
48 service: https://localhost:8001
49warp-routing:
50 enabled: true
51retries: 5
52grace-period: 30s
53percentage: 3.14
54hostname: example.com
55tag:
56 - test
57 - central-1
58counters:
59 - 123
60 - 456
61`
62 var config configFileSettings
63 err := yaml.Unmarshal([]byte(rawYAML), &config)
64 assert.NoError(t, err)
65
66 assert.Equal(t, "config-file-test", config.TunnelID)
67 assert.Equal(t, firstIngress, config.Ingress[0])
68 assert.Equal(t, secondIngress, config.Ingress[1])
69 assert.Equal(t, warpRouting, config.WarpRouting)
70 privateV4 := "10.0.0.0/8"
71 privateV6 := "fc00::/7"
72 ipRules := []IngressIPRule{
73 {
74 Prefix: &privateV4,
75 Ports: []int{80, 8080},
76 Allow: false,
77 },
78 {
79 Prefix: &privateV6,
80 Ports: []int{443, 4443},
81 Allow: true,
82 },
83 }
84 assert.Equal(t, ipRules, config.OriginRequest.IPRules)
85
86 retries, err := config.Int("retries")
87 assert.NoError(t, err)
88 assert.Equal(t, 5, retries)
89
90 gracePeriod, err := config.Duration("grace-period")
91 assert.NoError(t, err)
92 assert.Equal(t, time.Second*30, gracePeriod)
93
94 percentage, err := config.Float64("percentage")
95 assert.NoError(t, err)
96 assert.Equal(t, 3.14, percentage)
97
98 hostname, err := config.String("hostname")
99 assert.NoError(t, err)
100 assert.Equal(t, "example.com", hostname)
101
102 tags, err := config.StringSlice("tag")
103 assert.NoError(t, err)
104 assert.Equal(t, "test", tags[0])
105 assert.Equal(t, "central-1", tags[1])
106
107 counters, err := config.IntSlice("counters")
108 assert.NoError(t, err)
109 assert.Equal(t, 123, counters[0])
110 assert.Equal(t, 456, counters[1])
111
112}
113
114var rawConfig = []byte(`
115{
116 "connectTimeout": 10,
117 "tlsTimeout": 30,
118 "tcpKeepAlive": 30,
119 "noHappyEyeballs": true,
120 "keepAliveTimeout": 60,
121 "keepAliveConnections": 10,
122 "httpHostHeader": "app.tunnel.com",
123 "originServerName": "app.tunnel.com",
124 "caPool": "/etc/capool",
125 "noTLSVerify": true,
126 "disableChunkedEncoding": true,
127 "bastionMode": true,
128 "proxyAddress": "127.0.0.3",
129 "proxyPort": 9000,
130 "proxyType": "socks",
131 "ipRules": [
132 {
133 "prefix": "10.0.0.0/8",
134 "ports": [80, 8080],
135 "allow": false
136 },
137 {
138 "prefix": "fc00::/7",
139 "ports": [443, 4443],
140 "allow": true
141 }
142 ]
143}
144`)
145
146func TestMarshalUnmarshalOriginRequest(t *testing.T) {
147 testCases := []struct {
148 name string
149 marshalFunc func(in interface{}) (out []byte, err error)
150 unMarshalFunc func(in []byte, out interface{}) (err error)
151 baseUnit time.Duration
152 }{
153 {"json", json.Marshal, json.Unmarshal, time.Second},
154 {"yaml", yaml.Marshal, yaml.Unmarshal, time.Nanosecond},
155 }
156
157 for _, tc := range testCases {
158 t.Run(tc.name, func(t *testing.T) {
159 assertConfig(t, tc.marshalFunc, tc.unMarshalFunc, tc.baseUnit)
160 })
161 }
162}
163
164func assertConfig(
165 t *testing.T,
166 marshalFunc func(in interface{}) (out []byte, err error),
167 unMarshalFunc func(in []byte, out interface{}) (err error),
168 baseUnit time.Duration,
169) {
170 var config OriginRequestConfig
171 var config2 OriginRequestConfig
172
173 assert.NoError(t, unMarshalFunc(rawConfig, &config))
174
175 assert.Equal(t, baseUnit*10, config.ConnectTimeout.Duration)
176 assert.Equal(t, baseUnit*30, config.TLSTimeout.Duration)
177 assert.Equal(t, baseUnit*30, config.TCPKeepAlive.Duration)
178 assert.Equal(t, true, *config.NoHappyEyeballs)
179 assert.Equal(t, baseUnit*60, config.KeepAliveTimeout.Duration)
180 assert.Equal(t, 10, *config.KeepAliveConnections)
181 assert.Equal(t, "app.tunnel.com", *config.HTTPHostHeader)
182 assert.Equal(t, "app.tunnel.com", *config.OriginServerName)
183 assert.Equal(t, "/etc/capool", *config.CAPool)
184 assert.Equal(t, true, *config.NoTLSVerify)
185 assert.Equal(t, true, *config.DisableChunkedEncoding)
186 assert.Equal(t, true, *config.BastionMode)
187 assert.Equal(t, "127.0.0.3", *config.ProxyAddress)
188 assert.Equal(t, true, *config.NoTLSVerify)
189 assert.Equal(t, uint(9000), *config.ProxyPort)
190 assert.Equal(t, "socks", *config.ProxyType)
191
192 privateV4 := "10.0.0.0/8"
193 privateV6 := "fc00::/7"
194 ipRules := []IngressIPRule{
195 {
196 Prefix: &privateV4,
197 Ports: []int{80, 8080},
198 Allow: false,
199 },
200 {
201 Prefix: &privateV6,
202 Ports: []int{443, 4443},
203 Allow: true,
204 },
205 }
206 assert.Equal(t, ipRules, config.IPRules)
207
208 // validate that serializing and deserializing again matches the deserialization from raw string
209 result, err := marshalFunc(config)
210 require.NoError(t, err)
211 err = unMarshalFunc(result, &config2)
212 require.NoError(t, err)
213
214 require.Equal(t, config2, config)
215}
216