-
Notifications
You must be signed in to change notification settings - Fork 69
/
local_name_resolution_test.go
176 lines (145 loc) · 5.33 KB
/
local_name_resolution_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
package main
import (
"bytes"
"net"
"testing"
"time"
"github.com/miekg/dns"
)
func TestLLMNR(t *testing.T) {
testReply(t, "testdata/llmnr_request.bin", "testdata/llmnr_reply.bin")
}
func TestMDNS(t *testing.T) {
testReply(t, "testdata/mdns_request.bin", "testdata/mdns_reply.bin")
}
func TestNetBIOS(t *testing.T) {
relayIP := mustParseIP(t, "10.0.0.2")
mockRW := mockResonseWriter{Remote: &net.UDPAddr{IP: mustParseIP(t, "10.0.0.1")}}
request := readNameServiceMessage(t, "testdata/netbios_request.bin")
expectedReply := readFile(t, "testdata/netbios_reply.bin")
cfg := Config{RelayIPv4: relayIP, TTL: 60 * time.Second}
reply := createDNSReplyFromRequest(mockRW, request, nil, cfg, HandlerTypeNetBIOS, nil)
if reply == nil {
t.Fatalf("no message was created")
}
if !reply.Authoritative {
t.Fatalf("reply is not authoritative such that it will not be considered by Windows")
}
if len(reply.Answer) != 1 {
t.Fatalf("received %d answers instead of 1", len(reply.Answer))
}
answer, ok := reply.Answer[0].(*dns.NIMLOC)
if !ok {
t.Fatalf("wrong answer type: %T", reply.Answer[0])
}
expectedLocator := encodeNetBIOSLocator(relayIP)
if answer.Locator != expectedLocator {
t.Fatalf("unexpected locator: got %q instead of %q", answer.Locator, expectedLocator)
}
if answer.Hdr.Ttl != uint32(cfg.TTL.Seconds()) {
t.Fatalf("unexpected TTL: got %ds instead of %ds", answer.Hdr.Ttl, uint32(cfg.TTL.Seconds()))
}
if answer.Hdr.Name != request.Question[0].Name {
t.Fatalf("%q was echoed instead of question name %q", answer.Hdr.Name, reply.Question[0].Name)
}
if answer.Hdr.Rrtype != typeNetBios {
t.Fatalf("unexpected type: got %s instead of NIMLOC/NetBIOS", dnsQueryType(answer.Hdr.Rrtype))
}
if answer.Hdr.Class != dns.ClassINET {
t.Fatalf("unexpected class: got %d instead of %d", answer.Hdr.Class, dns.ClassINET)
}
if reply.CheckingDisabled {
t.Fatalf("checking disabled should be false in NetBIOS reply")
}
if reply.Question != nil {
t.Fatalf("NetBIOS reply cannot include question")
}
replyBuffer, err := reply.Pack()
if err != nil {
t.Fatalf("pack reply: %v", err)
}
if !bytes.Equal(replyBuffer, expectedReply) {
t.Fatalf("reply bytes do not match")
}
}
func TestSubnetBroadcastListenIP(t *testing.T) {
testCases := []struct {
Net string
BroadcastIP string
}{
{"192.168.0.4/24", "192.168.0.255"},
{"10.0.0.10/23", "10.0.1.255"},
{"10.0.0.0/8", "10.255.255.255"},
{"192.168.5.16/30", "192.168.5.19"},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Net, func(t *testing.T) {
_, ipNet, err := net.ParseCIDR(testCase.Net)
if err != nil {
t.Fatalf("invalid net %q: %v", testCase.Net, err)
}
expected := net.ParseIP(testCase.BroadcastIP)
if expected == nil {
t.Fatalf("invalid broadcast IP: %s", testCase.BroadcastIP)
}
broadcastIP, err := subnetBroadcastListenIP(ipNet)
if err != nil {
t.Fatalf("calculating broadcast listen IP: %v", err)
}
if !broadcastIP.Equal(expected) {
t.Fatalf("expected broadcast IP %s for net %s, got %s instead",
expected, ipNet, broadcastIP)
}
})
}
}
func TestDecodeNetBIOSHostname(t *testing.T) {
testCases := []struct {
NetBIOSName string
Expected string
}{
{NetBIOSName: "FEEFFDFEEIEPFDFECACACACACACACACA.", Expected: "TESTHOST"},
// different NetBIOS Suffixes
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-brws/0c773bdd-78e2-4d8b-8b3d-b7506849847b
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACAAA.", Expected: "WORKGROUP"}, // workstation name
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACAAB.", Expected: "WORKGROUP"}, // messenger service
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABL.", Expected: "WORKGROUP"}, // domain master browser
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABN.", Expected: "WORKGROUP"}, // master browser
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABO.", Expected: "WORKGROUP"}, // domain service elections
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACACA.", Expected: "WORKGROUP"}, // file service
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Expected, func(t *testing.T) {
decoded := decodeNetBIOSHostname(testCase.NetBIOSName)
if decoded != testCase.Expected {
t.Errorf("%s decoded to %s instead of %s",
testCase.NetBIOSName, decoded, testCase.Expected)
}
})
}
}
func TestDecodeNetBIOSSuffix(t *testing.T) {
testCases := []struct {
NetBIOSName string
Expected string
}{
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACAAA.", Expected: NetBIOSSuffixWorkstationService},
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACAAB.", Expected: NetBIOSSuffixWindowsMessengerService},
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABL.", Expected: NetBIOSSuffixDomainMasterBrowser},
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABN.", Expected: NetBIOSSuffixMasterBrowser},
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACABO.", Expected: NetBIOSSuffixBrowserServiceElections},
{NetBIOSName: "FHEPFCELEHFCEPFFFACACACACACACACA.", Expected: NetBIOSSuffixFileService},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Expected, func(t *testing.T) {
decoded := decodeNetBIOSSuffix(testCase.NetBIOSName)
if decoded != testCase.Expected {
t.Errorf("%s suffix decoded to %q instead of %q",
testCase.NetBIOSName, decoded, testCase.Expected)
}
})
}
}