-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_additional_test.go
105 lines (96 loc) · 2.2 KB
/
protocol_additional_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
package bridge
import (
"bytes"
"testing"
)
func TestHeader(t *testing.T) {
tests := []struct {
name string
header Header
}{
{
name: "basic header",
header: Header{
Type: MessageTypeData,
SequenceNumber: 1,
FragmentID: 1,
FragmentTotal: 1,
FragmentSeq: 0,
IsLastFragment: true,
},
},
{
name: "multi-fragment header",
header: Header{
Type: MessageTypeStreamInit,
SequenceNumber: 65535,
FragmentID: 2,
FragmentTotal: 3,
FragmentSeq: 1,
IsLastFragment: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := tt.header.marshal()
decoded, err := unmarshalHeader(data)
if err != nil {
t.Fatalf("Failed to unmarshal header: %v", err)
}
if decoded.Type != tt.header.Type ||
decoded.SequenceNumber != tt.header.SequenceNumber ||
decoded.FragmentID != tt.header.FragmentID ||
decoded.FragmentTotal != tt.header.FragmentTotal ||
decoded.FragmentSeq != tt.header.FragmentSeq ||
decoded.IsLastFragment != tt.header.IsLastFragment {
t.Errorf("Unmarshaled header doesn't match original")
}
})
}
}
func TestMessage(t *testing.T) {
tests := []struct {
name string
message message
}{
{
name: "small message",
message: message{
SequenceNumber: 1,
Data: []byte("Hello"),
},
},
{
name: "large message",
message: message{
SequenceNumber: 999999,
Data: bytes.Repeat([]byte("Large Message "), 100),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := tt.message.marshal()
decoded, err := unmarshalMessage(data)
if err != nil {
t.Fatalf("Failed to unmarshal message: %v", err)
}
if decoded.SequenceNumber != tt.message.SequenceNumber ||
!bytes.Equal(decoded.Data, tt.message.Data) {
t.Errorf("Unmarshaled message doesn't match original")
}
})
}
}
func TestSetMaxFragmentSize(t *testing.T) {
originalSize := MaxFragmentSize
defer func() {
MaxFragmentSize = originalSize
}()
newSize := 1024
SetMaxFragmentSize(newSize)
if MaxFragmentSize != newSize {
t.Errorf("MaxFragmentSize = %d; want %d", MaxFragmentSize, newSize)
}
}