-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
117 lines (113 loc) · 2.66 KB
/
message_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
package lcm
import (
"testing"
"gotest.tools/v3/assert"
)
func TestMessage_MarshalUnmarshal(t *testing.T) {
for _, tt := range []struct {
msg string
data []byte
message Message
}{
{
msg: "no payload",
data: []byte{
0x4c, 0x43, 0x30, 0x32, // short header magic
0x12, 0x34, 0x56, 0x78, // sequence number
'a', 0x00, // channel
},
message: Message{
SequenceNumber: 0x12345678,
Channel: "a",
Data: []byte{},
},
},
{
msg: "payload",
data: []byte{
0x4c, 0x43, 0x30, 0x32, // short header magic
0x12, 0x34, 0x56, 0x78, // sequence number
'a', 'b', 'c', 0x00, // channel
0x01, 0x02, 0x03, // payload
},
message: Message{
SequenceNumber: 0x12345678,
Channel: "abc",
Data: []byte{0x01, 0x02, 0x03},
},
},
{
msg: "payload channel with params",
data: []byte{
0x4c, 0x43, 0x30, 0x32, // short header magic
0x12, 0x34, 0x56, 0x78, // sequence number
'a', 'b', 'c', '?', 'z', '=', 'l', 'z', '4', 0x00, // channel
0x01, 0x02, 0x03, // payload
},
message: Message{
SequenceNumber: 0x12345678,
Channel: "abc",
Params: "z=lz4",
Data: []byte{0x01, 0x02, 0x03},
},
},
} {
tt := tt
t.Run(tt.msg, func(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
var data [lengthOfLargestUDPMessage]byte
n, err := tt.message.marshal(data[:])
assert.NilError(t, err)
assert.Equal(t, len(tt.data), n)
assert.DeepEqual(t, tt.data, data[:n])
})
t.Run("unmarshal", func(t *testing.T) {
var msg Message
assert.NilError(t, msg.unmarshal(tt.data))
assert.DeepEqual(t, tt.message, msg)
})
})
}
}
func TestMessage_Unmarshal_Errors(t *testing.T) {
for _, tt := range []struct {
msg string
data []byte
err string
}{
{
msg: "invalid size",
data: []byte{
0x4c, 0x43, 0x30, 0x32, // short header magic
0x12, 0x34, 0x56,
},
err: "insufficient data: 7 bytes",
},
{
msg: "invalid channel",
data: []byte{
0x4c, 0x43, 0x30, 0x32, // short header magic
0x12, 0x34, 0x56, 0x78, // sequence number
'a', 'b', 'c', 'd', // channel (missing null byte)
},
err: "invalid channel: not null-terminated",
},
{
msg: "invalid magic",
data: []byte{
0xde, 0xad, 0xbe, 0xef, // short header magic
0x12, 0x34, 0x56, 0x78, // sequence number
'a', 'b', 'c', 'd', // channel (missing null byte)
},
err: "wrong header magic: 0xdeadbeef",
},
} {
tt := tt
t.Run(tt.msg, func(t *testing.T) {
var msg Message
err := msg.unmarshal(tt.data)
assert.Assert(t, err != nil)
assert.Equal(t, tt.err, err.Error())
})
}
}