cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2018.10.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

validation/validation_test.go

243lines · modeblame

d06fc520Areg Harutyunyan8 years ago1package validation
2
3import (
4"fmt"
5"testing"
6
ca9902a8Areg Harutyunyan7 years ago7"context"
8"crypto/tls"
9"crypto/x509"
d06fc520Areg Harutyunyan8 years ago10"github.com/stretchr/testify/assert"
ca9902a8Areg Harutyunyan7 years ago11"net"
12"net/http"
13"net/http/httptest"
14"net/url"
15"strings"
d06fc520Areg Harutyunyan8 years ago16)
17
18func TestValidateHostname(t *testing.T) {
19var inputHostname string
20hostname, err := ValidateHostname(inputHostname)
ca9902a8Areg Harutyunyan7 years ago21assert.Equal(t, err, nil)
d06fc520Areg Harutyunyan8 years ago22assert.Empty(t, hostname)
23
24inputHostname = "hello.example.com"
25hostname, err = ValidateHostname(inputHostname)
26assert.Nil(t, err)
27assert.Equal(t, "hello.example.com", hostname)
28
29inputHostname = "http://hello.example.com"
30hostname, err = ValidateHostname(inputHostname)
31assert.Nil(t, err)
32assert.Equal(t, "hello.example.com", hostname)
33
34inputHostname = "bücher.example.com"
35hostname, err = ValidateHostname(inputHostname)
36assert.Nil(t, err)
37assert.Equal(t, "xn--bcher-kva.example.com", hostname)
38
39inputHostname = "http://bücher.example.com"
40hostname, err = ValidateHostname(inputHostname)
41assert.Nil(t, err)
42assert.Equal(t, "xn--bcher-kva.example.com", hostname)
43
44inputHostname = "http%3A%2F%2Fhello.example.com"
45hostname, err = ValidateHostname(inputHostname)
46assert.Nil(t, err)
47assert.Equal(t, "hello.example.com", hostname)
48
49}
50
51func TestValidateUrl(t *testing.T) {
52validUrl, err := ValidateUrl("")
ca9902a8Areg Harutyunyan7 years ago53assert.Equal(t, fmt.Errorf("URL should not be empty"), err)
d06fc520Areg Harutyunyan8 years ago54assert.Empty(t, validUrl)
55
56validUrl, err = ValidateUrl("https://localhost:8080")
57assert.Nil(t, err)
58assert.Equal(t, "https://localhost:8080", validUrl)
59
60validUrl, err = ValidateUrl("localhost:8080")
61assert.Nil(t, err)
62assert.Equal(t, "http://localhost:8080", validUrl)
63
64validUrl, err = ValidateUrl("http://localhost")
65assert.Nil(t, err)
66assert.Equal(t, "http://localhost", validUrl)
67
68validUrl, err = ValidateUrl("http://127.0.0.1:8080")
69assert.Nil(t, err)
70assert.Equal(t, "http://127.0.0.1:8080", validUrl)
71
72validUrl, err = ValidateUrl("127.0.0.1:8080")
73assert.Nil(t, err)
74assert.Equal(t, "http://127.0.0.1:8080", validUrl)
75
76validUrl, err = ValidateUrl("127.0.0.1")
77assert.Nil(t, err)
78assert.Equal(t, "http://127.0.0.1", validUrl)
79
80validUrl, err = ValidateUrl("https://127.0.0.1:8080")
81assert.Nil(t, err)
82assert.Equal(t, "https://127.0.0.1:8080", validUrl)
83
84validUrl, err = ValidateUrl("[::1]:8080")
85assert.Nil(t, err)
86assert.Equal(t, "http://[::1]:8080", validUrl)
87
88validUrl, err = ValidateUrl("http://[::1]")
89assert.Nil(t, err)
90assert.Equal(t, "http://[::1]", validUrl)
91
92validUrl, err = ValidateUrl("http://[::1]:8080")
93assert.Nil(t, err)
94assert.Equal(t, "http://[::1]:8080", validUrl)
95
96validUrl, err = ValidateUrl("[::1]")
97assert.Nil(t, err)
98assert.Equal(t, "http://[::1]", validUrl)
99
100validUrl, err = ValidateUrl("https://example.com")
101assert.Nil(t, err)
102assert.Equal(t, "https://example.com", validUrl)
103
104validUrl, err = ValidateUrl("example.com")
105assert.Nil(t, err)
106assert.Equal(t, "http://example.com", validUrl)
107
108validUrl, err = ValidateUrl("http://hello.example.com")
109assert.Nil(t, err)
110assert.Equal(t, "http://hello.example.com", validUrl)
111
112validUrl, err = ValidateUrl("hello.example.com")
113assert.Nil(t, err)
114assert.Equal(t, "http://hello.example.com", validUrl)
115
116validUrl, err = ValidateUrl("hello.example.com:8080")
117assert.Nil(t, err)
118assert.Equal(t, "http://hello.example.com:8080", validUrl)
119
120validUrl, err = ValidateUrl("https://hello.example.com:8080")
121assert.Nil(t, err)
122assert.Equal(t, "https://hello.example.com:8080", validUrl)
123
124validUrl, err = ValidateUrl("https://bücher.example.com")
125assert.Nil(t, err)
126assert.Equal(t, "https://xn--bcher-kva.example.com", validUrl)
127
128validUrl, err = ValidateUrl("bücher.example.com")
129assert.Nil(t, err)
130assert.Equal(t, "http://xn--bcher-kva.example.com", validUrl)
131
132validUrl, err = ValidateUrl("https%3A%2F%2Fhello.example.com")
133assert.Nil(t, err)
134assert.Equal(t, "https://hello.example.com", validUrl)
135
136validUrl, err = ValidateUrl("ftp://alex:12345@hello.example.com:8080/robot.txt")
137assert.Equal(t, "Currently Argo Tunnel does not support ftp protocol.", err.Error())
138assert.Empty(t, validUrl)
139
140validUrl, err = ValidateUrl("https://alex:12345@hello.example.com:8080")
141assert.Nil(t, err)
142assert.Equal(t, "https://hello.example.com:8080", validUrl)
143
144}
ca9902a8Areg Harutyunyan7 years ago145
146func TestToggleProtocol(t *testing.T) {
147assert.Equal(t, "https", toggleProtocol("http"))
148assert.Equal(t, "http", toggleProtocol("https"))
149assert.Equal(t, "random", toggleProtocol("random"))
150assert.Equal(t, "", toggleProtocol(""))
151}
152
153func TestValidateHTTPService_HTTP2HTTP(t *testing.T) {
154server, client, err := createMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155w.WriteHeader(200)
156}))
157assert.NoError(t, err)
158defer server.Close()
159
160assert.Equal(t, nil, ValidateHTTPService("http://example.com/", client.Transport))
161}
162
163func TestValidateHTTPService_ServerNonOKResponse(t *testing.T) {
164server, client, err := createMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
165w.WriteHeader(400)
166}))
167assert.NoError(t, err)
168defer server.Close()
169
170assert.Equal(t, nil, ValidateHTTPService("http://example.com/", client.Transport))
171}
172
173func TestValidateHTTPService_HTTPS2HTTP(t *testing.T) {
174server, client, err := createMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
175w.WriteHeader(200)
176}))
177assert.NoError(t, err)
178defer server.Close()
179
180assert.Equal(t,
181"example.com doesn't seem to work over https, but does seem to work over http. Consider changing the origin URL to http://example.com:1234/",
182ValidateHTTPService("https://example.com:1234/", client.Transport).Error())
183}
184
185func TestValidateHTTPService_HTTPS2HTTPS(t *testing.T) {
186server, client, err := createSecureMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
187w.WriteHeader(200)
188}))
189assert.NoError(t, err)
190defer server.Close()
191
192assert.Equal(t, nil, ValidateHTTPService("https://example.com/", client.Transport))
193}
194
195func TestValidateHTTPService_HTTP2HTTPS(t *testing.T) {
196server, client, err := createSecureMockServerAndClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
197w.WriteHeader(200)
198}))
199assert.NoError(t, err)
200defer server.Close()
201
202assert.Equal(t,
203"example.com doesn't seem to work over http, but does seem to work over https. Consider changing the origin URL to https://example.com:1234/",
204ValidateHTTPService("http://example.com:1234/", client.Transport).Error())
205}
206
207func createMockServerAndClient(handler http.Handler) (*httptest.Server, *http.Client, error) {
208client := http.DefaultClient
209server := httptest.NewServer(handler)
210
211client.Transport = &http.Transport{
212Proxy: func(req *http.Request) (*url.URL, error) {
213return url.Parse(server.URL)
214},
215}
216
217return server, client, nil
218}
219
220func createSecureMockServerAndClient(handler http.Handler) (*httptest.Server, *http.Client, error) {
221client := http.DefaultClient
222server := httptest.NewTLSServer(handler)
223
224cert, err := x509.ParseCertificate(server.TLS.Certificates[0].Certificate[0])
225if err != nil {
226server.Close()
227return nil, nil, err
228}
229
230certpool := x509.NewCertPool()
231certpool.AddCert(cert)
232
233client.Transport = &http.Transport{
234DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
235return net.Dial("tcp", server.URL[strings.LastIndex(server.URL, "/")+1:])
236},
237TLSClientConfig: &tls.Config{
238RootCAs: certpool,
239},
240}
241
242return server, client, nil
243}