-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmessage_test.go
More file actions
145 lines (115 loc) · 3.96 KB
/
Copy pathmessage_test.go
File metadata and controls
145 lines (115 loc) · 3.96 KB
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
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package datachannel
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChannelOpenMarshal(t *testing.T) {
msg := channelOpen{
ChannelType: ChannelTypeReliable,
Priority: 0,
ReliabilityParameter: 0,
Label: []byte("foo"),
Protocol: []byte("bar"),
}
rawMsg, err := msg.Marshal()
assert.NoError(t, err)
result := []byte{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
}
assert.Equal(t, len(result), len(rawMsg))
for i, v := range rawMsg {
assert.Equal(t, result[i], v)
}
}
func TestChannelAckMarshal(t *testing.T) {
msg := channelAck{}
rawMsg, err := msg.Marshal()
assert.NoError(t, err)
result := []byte{0x02, 0x00, 0x00, 0x00}
assert.Equal(t, len(result), len(rawMsg))
for i, v := range rawMsg {
assert.Equal(t, result[i], v)
}
}
func TestChannelOpenUnmarshal(t *testing.T) {
rawMsg := []byte{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
}
msgUncast, err := parse(rawMsg)
msg, ok := msgUncast.(*channelOpen)
assert.True(t, ok, "Failed to cast to ChannelOpen")
assert.NoError(t, err, "Unmarshal failed, ChannelOpen")
assert.Equal(t, msg.ChannelType, ChannelTypeReliable, "ChannelType should be 0")
assert.Equal(t, msg.Priority, uint16(0), "Priority should be 0")
assert.Equal(t, msg.ReliabilityParameter, uint32(0), "ReliabilityParameter should be 0")
assert.Equal(t, msg.Label, []uint8("foo"), "msg Label should be 'foo'")
assert.Equal(t, msg.Protocol, []uint8("bar"), "msg protocol should be 'bar'")
}
func TestChannelAckUnmarshal(t *testing.T) {
rawMsg := []byte{0x02}
msgUncast, err := parse(rawMsg)
assert.NoError(t, err)
_, ok := msgUncast.(*channelAck)
assert.True(t, ok, "Failed to cast to ChannelAck")
}
func TestChannelString(t *testing.T) {
channelString := channelOpen{
ChannelType: ChannelTypeReliable,
Priority: 0,
ReliabilityParameter: 0,
Label: []byte("foo"),
Protocol: []byte("bar"),
}.String()
assert.Equal(
t,
channelString,
"Open ChannelType(ReliableOrdered) Priority(0) ReliabilityParameter(0) Label(foo) Protocol(bar)",
)
assert.Equal(t, channelAck{}.String(), "ACK")
}
func TestChannelOpenUnmarshalVeryLongMessage(t *testing.T) {
label := strings.Repeat("a", maxLabelLength)
protocol := strings.Repeat("b", maxProtocolLength)
rawMsg := []byte{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff,
}
rawMsg = append(rawMsg, []byte(label)...)
rawMsg = append(rawMsg, []byte(protocol)...)
msgUncast, err := parse(rawMsg)
msg, ok := msgUncast.(*channelOpen)
assert.True(t, ok, "Failed to cast to ChannelOpen")
assert.NoError(t, err, "Unmarshal failed, ChannelOpen")
assert.Equal(t, msg.ChannelType, ChannelTypeReliable, "ChannelType should be 0")
assert.Equal(t, msg.Priority, uint16(0), "Priority should be 0")
assert.Equal(t, msg.ReliabilityParameter, uint32(0), "ReliabilityParameter should be 0")
assert.Equal(t, msg.Label, []uint8(label), "msg Label should be 'aaa...aaa'")
assert.Equal(t, msg.Protocol, []uint8(protocol), "msg protocol should be 'bbb...bbb'")
}
func TestChannelOpenMarshalTooLongLabel(t *testing.T) {
msg := channelOpen{
ChannelType: ChannelTypeReliable,
Priority: 0,
ReliabilityParameter: 0,
Label: []byte(strings.Repeat("a", maxLabelLength+1)),
Protocol: []byte("bar"),
}
_, err := msg.Marshal()
assert.ErrorIs(t, err, ErrTooLongLabel)
}
func TestChannelOpenMarshalTooLongProtocol(t *testing.T) {
msg := channelOpen{
ChannelType: ChannelTypeReliable,
Priority: 0,
ReliabilityParameter: 0,
Label: []byte("foo"),
Protocol: []byte(strings.Repeat("b", maxProtocolLength+1)),
}
_, err := msg.Marshal()
assert.ErrorIs(t, err, ErrTooLongProtocol)
}