-
Notifications
You must be signed in to change notification settings - Fork 34
/
responder_test.go
103 lines (84 loc) · 1.95 KB
/
responder_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 dnssd
import (
"context"
"github.com/miekg/dns"
"net"
"testing"
"time"
)
func TestRemove(t *testing.T) {
cfg := Config{
Name: "Test",
Type: "_asdf._tcp",
Port: 1234,
}
si, err := NewService(cfg)
if err != nil {
t.Fatal(err)
}
msg := new(dns.Msg)
msg.Answer = []dns.RR{SRV(si), TXT(si)}
answers := []dns.RR{SRV(si), TXT(si), PTR(si)}
unknown := remove(msg.Answer, answers)
if x := len(unknown); x != 1 {
t.Fatal(x)
}
rr := unknown[0]
if _, ok := rr.(*dns.PTR); !ok {
t.Fatal("Invalid type", rr)
}
}
func TestRegisterServiceWithExplicitIP(t *testing.T) {
cfg := Config{
Host: "Computer",
Name: "Test",
Type: "_asdf._tcp",
Domain: "local",
Port: 12345,
Ifaces: []string{"lo0"},
}
sv, err := NewService(cfg)
if err != nil {
t.Fatal(err)
}
sv.ifaceIPs = map[string][]net.IP{
"lo0": []net.IP{net.IP{192, 168, 0, 123}},
}
conn := newTestConn()
otherConn := newTestConn()
conn.in = otherConn.out
conn.out = otherConn.in
ctx, cancel := context.WithCancel(context.Background())
t.Run("resolver", func(t *testing.T) {
t.Parallel()
lookupCtx, lookupCancel := context.WithTimeout(ctx, 5*time.Second)
defer lookupCancel()
defer cancel()
srv, err := lookupInstance(lookupCtx, "Test._asdf._tcp.local.", otherConn)
if err != nil {
t.Fatal(err)
}
if is, want := srv.Name, "Test"; is != want {
t.Fatalf("%v != %v", is, want)
}
if is, want := srv.Type, "_asdf._tcp"; is != want {
t.Fatalf("%v != %v", is, want)
}
if is, want := srv.Host, "Computer"; is != want {
t.Fatalf("%v != %v", is, want)
}
ips := srv.IPsAtInterface(&net.Interface{Name: "lo0"})
if is, want := len(ips), 1; is != want {
t.Fatalf("%v != %v", is, want)
}
if is, want := ips[0].String(), "192.168.0.123"; is != want {
t.Fatalf("%v != %v", is, want)
}
})
t.Run("responder", func(t *testing.T) {
t.Parallel()
r := newResponder(conn)
r.addManaged(sv) // don't probe
r.Respond(ctx)
})
}