-
Notifications
You must be signed in to change notification settings - Fork 41
/
resolver_test.go
103 lines (78 loc) · 2.1 KB
/
resolver_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
package main
import (
"net"
"sync"
"testing"
"time"
"github.com/miekg/dns"
)
func S3DNSServer(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
target := "s3-1-w.amazonaws.com."
if m.Question[0].Name == "test.s3.amazonaws.com." {
target = "s3-us-west-2-w.amazonaws.com."
}
m.Answer = append(m.Answer, &dns.CNAME{Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: 0,
}, Target: target})
w.WriteMsg(m)
}
func RunLocalServer(pc net.PacketConn) (*dns.Server, string, chan error, error) {
server := &dns.Server{
PacketConn: pc,
ReadTimeout: time.Hour,
WriteTimeout: time.Hour,
}
waitLock := sync.Mutex{}
waitLock.Lock()
server.NotifyStartedFunc = waitLock.Unlock
addr := pc.LocalAddr().String()
closer := pc
fin := make(chan error, 1)
go func() {
fin <- server.ActivateAndServe()
closer.Close()
}()
waitLock.Lock()
return server, addr, fin, nil
}
func RunLocalUDPServer(laddr string) (*dns.Server, string, chan error, error) {
pc, err := net.ListenPacket("udp", laddr)
if err != nil {
return nil, "", nil, err
}
return RunLocalServer(pc)
}
func TestExistingBucket(t *testing.T) {
dns.HandleFunc("test.s3.amazonaws.com.", S3DNSServer)
defer dns.HandleRemove("test.s3.amazonnaws.com.")
s, addrstr, _, err := RunLocalUDPServer("127.0.0.1:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
resolver, err := NewDNSResolver(addrstr)
isBucket := resolver.IsBucket("test")
if !isBucket {
t.Fatal("'test' is an existing buckets")
}
}
func TestNonExistingBucket(t *testing.T) {
dns.HandleFunc("testnonexistingbucket.s3.amazonaws.com.", S3DNSServer)
defer dns.HandleRemove("testnonexistingbucket.s3.amazonnaws.com.")
s, addrstr, _, err := RunLocalUDPServer("127.0.0.1:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
resolver, err := NewDNSResolver(addrstr)
isBucket := resolver.IsBucket("testnonexistingbucket")
if isBucket != false {
t.Fatal("'testnonexistingbucket' is not a bucket")
}
}