-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdns_test.go
More file actions
128 lines (108 loc) · 2.95 KB
/
Copy pathdns_test.go
File metadata and controls
128 lines (108 loc) · 2.95 KB
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
package server_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/jcelliott/lumber"
"github.com/miekg/dns"
"github.com/nanopack/shaman/config"
"github.com/nanopack/shaman/core"
sham "github.com/nanopack/shaman/core/common"
"github.com/nanopack/shaman/server"
)
var nanopack = sham.Resource{Domain: "nanopack.io.", Records: []sham.Record{{Address: "127.0.0.1"}}}
var rootname = sham.Resource{Domain: "nanopack.com.", Records: []sham.Record{{Address: "nanopack.io", RType: "CNAME"}}}
func TestMain(m *testing.M) {
// manually configure
config.DnsListen = "127.0.0.1:8053"
config.Log = lumber.NewConsoleLogger(lumber.LvlInt("FATAL"))
// start dns server
go server.Start()
<-time.After(time.Second)
// run tests
rtn := m.Run()
os.Exit(rtn)
}
func TestDNS(t *testing.T) {
err := shaman.AddRecord(&nanopack)
if err != nil {
t.Errorf("Failed to add record - %v", err)
t.FailNow()
}
err = shaman.AddRecord(&rootname)
if err != nil {
t.Errorf("Failed to add root CNAME record - %v", err)
t.FailNow()
}
r, err := ResolveIt("nanopack.io", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) == 0 {
t.Error("No record found")
}
if len(r.Answer) > 0 && r.Answer[0].String() != "nanopack.io.\t60\tIN\tA\t127.0.0.1" {
t.Errorf("Response doesn't match expected - %+q", r.Answer[0].String())
}
r, err = ResolveIt("nanopack.com", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) == 0 {
t.Error("No record found")
}
if len(r.Answer) > 0 && r.Answer[0].String() != "nanopack.com.\t60\tIN\tA\t127.0.0.1" {
t.Errorf("Response doesn't match expected - %+q", r.Answer[0].String())
}
r, err = ResolveIt("a.b.nanobox.io", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) != 0 {
t.Error("Found non-existant record")
}
r, err = ResolveIt("nanopack.io", dns.TypeMX, true)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) != 0 {
t.Error("Found non-existant record")
}
// test fallback
config.DnsFallBack = "8.8.8.8:53"
r, err = ResolveIt("www.google.com", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) == 0 {
t.Error("No record found")
}
// reset fallback
config.DnsFallBack = ""
r, err = ResolveIt("www.google.com", dns.TypeA)
if len(r.Answer) != 0 {
t.Error("answer found for unregistered domain when fallback is off.")
}
}
func ResolveIt(domain string, rType uint16, badop ...bool) (*dns.Msg, error) {
// root domain if not already
root(&domain)
m := new(dns.Msg)
m.SetQuestion(domain, rType)
if len(badop) > 0 {
m.Opcode = dns.OpcodeStatus
}
// ask the dns server
r, err := dns.Exchange(m, config.DnsListen)
if err != nil {
return nil, fmt.Errorf("Failed to exchange - %v", err)
}
return r, nil
}
func root(domain *string) {
t := []byte(*domain)
if len(t) > 0 && t[len(t)-1] != '.' {
*domain = string(append(t, '.'))
}
}