-
Notifications
You must be signed in to change notification settings - Fork 0
/
https_test.go
189 lines (162 loc) · 4.87 KB
/
https_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
185
186
187
188
189
/*
DNSProxy
Copyright (C) 2024 Ian Spence
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dnsproxy
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"io"
"net/http"
"testing"
)
func TestHTTPSGet(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://127.0.0.1:8443/dns-query?dns=" + base64.RawURLEncoding.EncodeToString(dnsMessage))
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 200 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
if contentType := resp.Header.Get("Content-Type"); contentType != "application/dns-message" {
t.Errorf("Unexpected HTTP response content type %s", contentType)
return
}
reply, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %s", err.Error())
return
}
assertExpectedReply(reply, t)
}
func TestHTTPSPost(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Post("https://127.0.0.1:8443/dns-query", "application/dns-message", bytes.NewBuffer(dnsMessage))
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 200 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
if contentType := resp.Header.Get("Content-Type"); contentType != "application/dns-message" {
t.Errorf("Unexpected HTTP response content type %s", contentType)
return
}
reply, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %s", err.Error())
return
}
assertExpectedReply(reply, t)
}
func TestHTTPSRedirect(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get("https://127.0.0.1:8443/")
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 302 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
if location := resp.Header.Get("Location"); location != "https://example.com" {
t.Errorf("Unexpected HTTP redirect: %s", location)
return
}
}
func TestHTTPSGetMissingQuery(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://127.0.0.1:8443/dns-query?foo=bar")
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 400 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
}
func TestHTTPSGetInvalidQuery(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://127.0.0.1:8443/dns-query?dns=^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 400 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
}
func TestHTTPSGetShortQuery(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://127.0.0.1:8443/dns-query?dns=baz")
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 400 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
}
func TestHTTPSPostExcessiveBody(t *testing.T) {
var body = make([]byte, 4097)
if _, err := rand.Read(body); err != nil {
panic(err)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Post("https://127.0.0.1:8443/dns-query", "application/dns-message", bytes.NewBuffer(body))
if err != nil {
t.Errorf("Error connecting to DOH: %s", err.Error())
return
}
if resp.StatusCode != 400 {
t.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
return
}
}