-
Notifications
You must be signed in to change notification settings - Fork 27
/
advertise_test.go
149 lines (138 loc) · 3.89 KB
/
advertise_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
package ssdp
import (
"net"
"strings"
"sync"
"testing"
"time"
)
func TestAdvertise_Alive(t *testing.T) {
var mu sync.Mutex
var mm []*AliveMessage
m := newTestMonitor("test:advertise+alive", func(m *AliveMessage) {
mu.Lock()
mm = append(mm, m)
mu.Unlock()
}, nil, nil)
err := m.Start()
if err != nil {
t.Fatalf("failed to start Monitor: %s", err)
}
defer m.Close()
a, err := Advertise("test:advertise+alive", "usn:advertise+alive", "location:advertise+alive", "server:advertise+alive", 600)
if err != nil {
t.Fatalf("failed to Advertise: %s", err)
}
err = a.Alive()
if err != nil {
a.Close()
t.Fatalf("failed to send alive: %s", err)
}
a.Close()
time.Sleep(500 * time.Millisecond)
if len(mm) < 1 {
t.Fatal("no alives detected")
}
//t.Logf("found %d alives", len(mm))
_, port, err := net.SplitHostPort(mm[0].From.String())
if err != nil {
t.Fatalf("failed to split host and port: %s", err)
}
port = ":" + port
expHdr := map[string]string{
"Nts": "ssdp:alive",
"Nt": "test:advertise+alive",
"Usn": "usn:advertise+alive",
"Location": "location:advertise+alive",
"Server": "server:advertise+alive",
"Cache-Control": "max-age=600",
}
for i, m := range mm {
if strings.HasSuffix(port, m.From.String()) {
t.Errorf("unmatch port#%d:\nwant=%q\n got=%q", i, port, m.From.String())
}
if m.Type != "test:advertise+alive" {
t.Errorf("unexpected alive#%d type: want=%q got=%q", i, "test:advertise+alive", m.Type)
}
if m.USN != "usn:advertise+alive" {
t.Errorf("unexpected alive#%d usn: want=%q got=%q", i, "usn:advertise+alive", m.USN)
}
if m.Location != "location:advertise+alive" {
t.Errorf("unexpected alive#%d location: want=%q got=%q", i, "location:advertise+alive", m.Location)
}
if m.Server != "server:advertise+alive" {
t.Errorf("unexpected alive#%d server: want=%q got=%q", i, "server:advertise+alive", m.Server)
}
if m.MaxAge() != 600 {
t.Errorf("unexpected max-age: want=%d got=%d", 600, m.MaxAge())
}
h := m.Header()
for k := range h {
exp, ok := expHdr[k]
if !ok {
t.Errorf("unexpected header #%d %q=%q", i, k, h.Get(k))
} else if act := h.Get(k); act != exp {
t.Errorf("header #%d %q value mismatch:\nwant=%q\n got=%q", i, k, exp, act)
}
}
}
}
func TestAdvertise_Bye(t *testing.T) {
var mu sync.Mutex
var mm []*ByeMessage
m := newTestMonitor("test:advertise+bye", nil, func(m *ByeMessage) {
mu.Lock()
mm = append(mm, m)
mu.Unlock()
}, nil)
err := m.Start()
if err != nil {
t.Fatalf("failed to start Monitor: %s", err)
}
defer m.Close()
a, err := Advertise("test:advertise+bye", "usn:advertise+bye", "location:advertise+bye", "server:advertise+bye", 600)
if err != nil {
t.Fatalf("failed to Advertise: %s", err)
}
err = a.Bye()
if err != nil {
a.Close()
t.Fatalf("failed to send bye: %s", err)
}
a.Close()
time.Sleep(500 * time.Millisecond)
if len(mm) < 1 {
t.Fatal("no byes detected")
}
//t.Logf("found %d byes", len(mm))
_, port, err := net.SplitHostPort(mm[0].From.String())
if err != nil {
t.Fatalf("failed to split host and port: %s", err)
}
port = ":" + port
expHdr := map[string]string{
"Nts": "ssdp:byebye",
"Nt": "test:advertise+bye",
"Usn": "usn:advertise+bye",
}
for i, m := range mm {
if strings.HasSuffix(port, m.From.String()) {
t.Errorf("unmatch port#%d:\nwant=%q\n got=%q", i, port, m.From.String())
}
if m.Type != "test:advertise+bye" {
t.Errorf("unexpected bye#%d type: want=%q got=%q", i, "test:advertise+bye", m.Type)
}
if m.USN != "usn:advertise+bye" {
t.Errorf("unexpected bye#%d usn: want=%q got=%q", i, "usn:advertise+bye", m.USN)
}
h := m.Header()
for k := range h {
exp, ok := expHdr[k]
if !ok {
t.Errorf("unexpected header #%d %q=%q", i, k, h.Get(k))
} else if act := h.Get(k); act != exp {
t.Errorf("header #%d %q value mismatch:\nwant=%q\n got=%q", i, k, exp, act)
}
}
}
}