-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sub_test.go
63 lines (53 loc) · 1.47 KB
/
sub_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
// Copyright © by Jeff Foley 2021-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"context"
"testing"
"github.com/miekg/dns"
)
func TestFirstProperSubdomain(t *testing.T) {
dns.HandleFunc("first.org.", firstHandler)
defer dns.HandleRemove("first.org.")
s, addrstr, _, err := RunLocalUDPServer("localhost:0")
if err != nil {
t.Fatalf("Unable to run test server: %v", err)
}
defer func() { _ = s.Shutdown() }()
r := NewResolvers()
_ = r.AddResolvers(10, addrstr)
defer r.Stop()
expected := "sub.first.org"
input := "one.two.sub.first.org"
if sub := FirstProperSubdomain(context.Background(), r, input); sub != expected {
t.Errorf("Failed to return the correct subdomain name from input %s: expected %s and got %s", input, expected, sub)
}
}
func firstHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
if req.Question[0].Qtype != dns.TypeNS {
m.Rcode = dns.RcodeRefused
_ = w.WriteMsg(m)
return
}
switch req.Question[0].Name {
case "first.org.", "sub.first.org.":
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 0,
},
Ns: "ns.first.org.",
}
case "two.sub.first.org.":
m.Rcode = dns.RcodeSuccess
default:
m.Rcode = dns.RcodeNameError
}
_ = w.WriteMsg(m)
}