forked from brutella/dnssd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
123 lines (97 loc) · 2.45 KB
/
service_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
package dnssd
import (
"net"
"testing"
)
func TestParseServiceInstanceName(t *testing.T) {
instance, service, domain := parseServiceInstanceName("Test._hap._tcp.local.")
if is, want := instance, "Test"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if is, want := service, "_hap._tcp"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if is, want := domain, "local"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestParseHostname(t *testing.T) {
name, domain := parseHostname("Computer.local.")
if name != "Computer" {
t.Fatalf("%s != Computer", name)
}
if is, want := domain, "local"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestParseHostnameTrailingDomain(t *testing.T) {
name, domain := parseHostname("Computer.local")
if name != "Computer" {
t.Fatalf("%s != Computer", name)
}
if is, want := domain, "local"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestParseHostnameWithoutDomain(t *testing.T) {
name, domain := parseHostname("Computer.")
if is, want := name, "Computer"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if is, want := domain, ""; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestParseHostnameWithoutTrailingDot(t *testing.T) {
name, domain := parseHostname("Computer")
if is, want := name, "Computer"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if is, want := domain, ""; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestNewServiceWithMinimalConfig(t *testing.T) {
cfg := Config{
Name: "Test",
Type: "_asdf._tcp",
Port: 1234,
}
sv, err := NewService(cfg)
if err != nil {
t.Fatal(err)
}
if len(sv.Host) == 0 {
t.Fatal("Expected hostname")
}
if is, want := sv.Domain, "local"; is != want {
t.Fatalf("%v != %v", is, want)
}
if is, want := len(sv.IPs), 0; is != want {
t.Fatalf("%v != %v", is, want)
}
}
func TestNewServiceWithExplicitIP(t *testing.T) {
cfg := Config{
Name: "Test",
Type: "_asdf._tcp",
IPs: []net.IP{net.ParseIP("127.0.0.1")},
Port: 1234,
}
sv, err := NewService(cfg)
if err != nil {
t.Fatal(err)
}
if is, want := len(sv.IPs), 1; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if is, want := sv.IPs[0].String(), "127.0.0.1"; is != want {
t.Fatalf("%v != %v", is, want)
}
}
func TestSanitizeHostname(t *testing.T) {
host := sanitizeHostname("My Computer")
if is, want := host, "My-Computer"; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}