forked from icedream/go-stagelinq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_connection_test.go
218 lines (203 loc) · 6.71 KB
/
message_connection_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package stagelinq
import (
"net"
"testing"
"github.com/stretchr/testify/require"
)
var testToken = Token{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
func Test_MessageConnection_Read(t *testing.T) {
testMessages := []struct {
Message message
Bytes []byte
}{
{
Message: &serviceAnnouncementMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0xf4, 0x05, 0xdc, 0x14, 0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c, 0x49, 0x33, 0x52, 0x76},
},
Service: "StateMap",
Port: 0xb1d7,
},
Bytes: []byte{
/*
from Prime4
00000014 00 00 00 00 f4 05 dc 14 02 23 47 f5 8b 79 2c 8c ........ .#G..y,.
00000024 49 33 52 76 00 00 00 10 00 53 00 74 00 61 00 74 I3Rv.... .S.t.a.t
00000034 00 65 00 4d 00 61 00 70 b1 d7 .e.M.a.p ..
*/
0x00, 0x00, 0x00, 0x00, 0xf4, 0x05, 0xdc, 0x14,
0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c,
0x49, 0x33, 0x52, 0x76, 0x00, 0x00, 0x00, 0x10,
0x00, 0x53, 0x00, 0x74, 0x00, 0x61, 0x00, 0x74,
0x00, 0x65, 0x00, 0x4d, 0x00, 0x61, 0x00, 0x70,
0xb1, 0xd7,
},
},
{
Message: &referenceMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0xf4, 0x05, 0xdc, 0x14, 0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c, 0x49, 0x33, 0x52, 0x76},
},
Reference: 0x000009ed4f310604,
},
Bytes: []byte{
/*
from Prime4
00000106 00 00 00 01 f4 05 dc 14 02 23 47 f5 8b 79 2c 8c ........ .#G..y,.
00000116 49 33 52 76 00 00 00 00 00 00 00 00 00 00 00 00 I3Rv.... ........
00000126 00 00 00 00 00 00 09 ed 4f 31 06 04 ........ O1..
*/
0x00, 0x00, 0x00, 0x01, 0xf4, 0x05, 0xdc, 0x14,
0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c,
0x49, 0x33, 0x52, 0x76, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0xed,
0x4f, 0x31, 0x06, 0x04,
},
},
{
Message: &servicesRequestMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0xf4, 0x05, 0xdc, 0x14, 0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c, 0x49, 0x33, 0x52, 0x76},
},
},
Bytes: []byte{
/* from Prime4 */
0x00, 0x00, 0x00, 0x02, 0xf4, 0x05, 0xdc, 0x14,
0x02, 0x23, 0x47, 0xf5, 0x8b, 0x79, 0x2c, 0x8c,
0x49, 0x33, 0x52, 0x76,
},
},
{
Message: &serviceAnnouncementMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0x52, 0x3e, 0x67, 0x9d, 0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52, 0xcf, 0xca, 0x8f, 0xf7},
},
Service: "DirectoryService",
Port: 0xe190,
},
Bytes: []byte{
/*
from Resolume
00000000 00 00 00 00 52 3e 67 9d a4 18 4d 1e 83 d0 c7 52 ....R>g. ..M....R
00000010 cf ca 8f f7 00 00 00 20 00 44 00 69 00 72 00 65 ....... .D.i.r.e
00000020 00 63 00 74 00 6f 00 72 00 79 00 53 00 65 00 72 .c.t.o.r .y.S.e.r
00000030 00 76 00 69 00 63 00 65 e1 90 .v.i.c.e ..
*/
0x00, 0x00, 0x00, 0x00, 0x52, 0x3e, 0x67, 0x9d,
0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52,
0xcf, 0xca, 0x8f, 0xf7, 0x00, 0x00, 0x00, 0x20,
0x00, 0x44, 0x00, 0x69, 0x00, 0x72, 0x00, 0x65,
0x00, 0x63, 0x00, 0x74, 0x00, 0x6f, 0x00, 0x72,
0x00, 0x79, 0x00, 0x53, 0x00, 0x65, 0x00, 0x72,
0x00, 0x76, 0x00, 0x69, 0x00, 0x63, 0x00, 0x65,
0xe1, 0x90,
},
},
{
Message: &servicesRequestMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0x52, 0x3e, 0x67, 0x9d, 0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52, 0xcf, 0xca, 0x8f, 0xf7},
},
},
Bytes: []byte{
/*
from Resolume
0000003A 00 00 00 02 52 3e 67 9d a4 18 4d 1e 83 d0 c7 52 ....R>g. ..M....R
0000004A cf ca 8f f7 ....
*/
0x00, 0x00, 0x00, 0x02, 0x52, 0x3e, 0x67, 0x9d,
0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52,
0xcf, 0xca, 0x8f, 0xf7,
},
},
{
Message: &referenceMessage{
Token2: Token{0x52, 0x3e, 0x67, 0x9d, 0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52, 0xcf, 0xca, 0x8f, 0xf7},
Reference: 0x000009ed4f310604,
},
Bytes: []byte{
/*
from Resolume
0000004E 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0000005E 00 00 00 00 52 3e 67 9d a4 18 4d 1e 83 d0 c7 52 ....R>g. ..M....R
0000006E cf ca 8f f7 00 00 09 ed 4f 31 06 04 ........ O1..
*/
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x52, 0x3e, 0x67, 0x9d,
0xa4, 0x18, 0x4d, 0x1e, 0x83, 0xd0, 0xc7, 0x52,
0xcf, 0xca, 0x8f, 0xf7, 0x00, 0x00, 0x09, 0xed,
0x4f, 0x31, 0x06, 0x04,
},
},
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Can't set up test listener: %s", err.Error())
return
}
go func() {
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatalf("Failed to set up test connection: %s", err.Error())
return
}
for _, testMessage := range testMessages {
_, err := conn.Write(testMessage.Bytes)
if err != nil {
t.Fatalf("Failed to write bytes: %s", err.Error())
}
}
}()
conn, err := listener.Accept()
if err != nil {
t.Fatalf("Failed to accept test connection: %s", err.Error())
}
messageObjects := []message{}
for _, testMessage := range testMessages {
messageObjects = append(messageObjects, testMessage.Message)
}
msgConn := newMessageConnection(conn, newDeviceConnMessageSet(messageObjects))
for _, expectedMessage := range testMessages {
message, err := msgConn.ReadMessage()
require.Nil(t, err)
require.Equal(t, expectedMessage.Message, message)
}
}
func Test_MessageConnection(t *testing.T) {
testMessages := []message{
&serviceAnnouncementMessage{
tokenPrefixedMessage: tokenPrefixedMessage{testToken},
Service: "test",
Port: 0x1234,
},
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Can't set up test listener: %s", err.Error())
return
}
go func() {
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatalf("Failed to set up test connection: %s", err.Error())
return
}
msgConn := newMessageConnection(conn, newDeviceConnMessageSet(testMessages))
for _, testMessage := range testMessages {
err := msgConn.WriteMessage(testMessage)
require.Nil(t, err)
}
}()
conn, err := listener.Accept()
if err != nil {
t.Fatalf("Failed to accept test connection: %s", err.Error())
}
msgConn := newMessageConnection(conn, newDeviceConnMessageSet(testMessages))
for _, expectedMessage := range testMessages {
message, err := msgConn.ReadMessage()
require.Nil(t, err)
require.Equal(t, expectedMessage, message)
}
}