cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2022.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

config/configuration_test.go

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