-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathdoh_test.go
77 lines (61 loc) · 1.86 KB
/
doh_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
package main
import (
"github.com/miekg/dns"
"net/http"
"strings"
"testing"
"time"
)
func dnsAQuestion(question string) (msg *dns.Msg) {
msg = new(dns.Msg)
msg.SetQuestion(question, dns.TypeA)
return msg
}
func TestDohHappyPath(t *testing.T) {
handler := dns.NewServeMux()
custom := NewCustomDNSRecordsFromText([]string{"example.com. IN A 10.0.0.0 "})
handler.HandleFunc("example.com", custom[0].asHandler())
dohTest(t, handler, func(r Resolver, bind string) {
response, err := r.DoHLookup("http://"+bind+"/dns-query", 1, dnsAQuestion("example.com."))
if err != nil {
t.Fatalf("unexpected error during lookup %v", err)
}
if !strings.Contains(response.Answer[0].String(), "10.0.0.0") {
t.Fatalf("failed to answer dns query for example.org - expected 10.0.0.0 but got %v", response.Answer)
}
})
}
func TestDoh404(t *testing.T) {
handler := dns.NewServeMux()
custom := NewCustomDNSRecordsFromText([]string{"example.com A 10.0.0.0"})
handler.HandleFunc("example.com", custom[0].asHandler())
dohTest(t, handler, func(r Resolver, bind string) {
resp, err := http.Get("http://" + bind + "/unknown-path")
if resp.StatusCode != 404 {
t.Fatalf("expected 404 but got %v", resp.StatusCode)
}
if err != nil {
t.Fatalf("unexpected error during lookup %v", err)
}
})
}
func dohTest(t *testing.T, handler dns.Handler, doTest func(r Resolver, bind string)) {
bind := "localhost:7698"
config := parseDefaultConfig()
loggingState, _ := loggerInit(config.LogConfig)
config.DnsOverHttpServer.Bind = bind
defer func() {
loggingState.cleanUp()
}()
doh, err := NewServerHTTPS(handler, bind, time.Second*5, time.Second*5, nil)
defer doh.Shutdown()
if err != nil {
t.Fatalf("error when tarting server %v", err)
}
go func() {
_ = doh.httpsServer.ListenAndServe()
}()
time.Sleep(100 * time.Millisecond)
r := Resolver{}
doTest(r, bind)
}