-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_test.go
188 lines (168 loc) · 4.12 KB
/
packet_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
package sap
import (
"reflect"
"testing"
)
// TestPacketMarshalSize checks the size of the marshaled packet.
func TestPacketMarshalSize(t *testing.T) {
testCases := []struct {
name string
mockPacket *Packet
want int
}{
{
name: "Test 1: Without Payload",
mockPacket: CreateMockPacket(Packet{}),
want: 8,
},
{
name: "Test 2: With Payload",
mockPacket: CreateMockPacket(Packet{
Payload: []byte{0x10, 0x04},
}),
want: 10,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := tc.mockPacket.MarshalSize()
if tc.want != got {
t.Errorf("expected %d, got %d", tc.want, got)
}
})
}
}
func TestPacketUnmarshalErrors(t *testing.T) {
testCases := []struct {
name string
input []byte
expectedError error
}{
{
name: "BufTooSmallForFlags",
input: make([]byte, 0), // Not enough bytes to unmarshal flags.
expectedError: errBufTooSmallForFlags,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockPacket := CreateMockPacket(Packet{})
err := mockPacket.Unmarshal(tc.input)
if err != tc.expectedError {
t.Errorf("Expected error %v, but got %v", tc.expectedError, err)
}
})
}
}
// TestPacketMarshalAndUnmarshal checks that a packet can be marshaled and then unmarshaled to its original state.
func TestPacketMarshalAndUnmarshal(t *testing.T) {
testCases := []struct {
name string
mockPacket *Packet
}{
{
name: "Test 1: Without Payload",
mockPacket: CreateMockPacket(Packet{}),
},
{
name: "Test 2: With Payload",
mockPacket: CreateMockPacket(Packet{
Payload: []byte{0x10, 0x04},
}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
data, err := tc.mockPacket.Marshal()
if err != nil {
t.Errorf("Marshal failed with error: %v", err)
}
p2 := CreateMockPacket(*tc.mockPacket)
err = p2.Unmarshal(data)
if err != nil {
t.Errorf("Unmarshal failed with error: %v", err)
}
if !reflect.DeepEqual(tc.mockPacket, p2) {
t.Error("original and unmarshalled packets do not match")
}
})
}
}
// TestPacketMarshalTo checks that the packet can be serialized and written to a byte slice.
func TestPacketMarshalTo(t *testing.T) {
testCases := []struct {
name string
mockPacket *Packet
}{
{
name: "Test 1: Without Payload",
mockPacket: CreateMockPacket(Packet{}),
},
{
name: "Test 2: With Payload",
mockPacket: CreateMockPacket(Packet{
Payload: []byte{0x10, 0x04},
}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
bufSize := tc.mockPacket.MarshalSize()
buf := make([]byte, bufSize)
n, err := tc.mockPacket.MarshalTo(buf)
if err != nil {
t.Errorf("MarshalTo failed with error: %v", err)
}
if n != len(buf) {
t.Errorf("expected to write %d bytes, wrote %d", len(buf), n)
}
})
}
}
func TestHeaderMarshalToErrors(t *testing.T) {
testCases := []struct {
name string
input []byte
expectedError error
}{
{
name: "BufTooSmallForHeader",
input: make([]byte, 4),
expectedError: errBufTooSmallForHeader,
},
{
name: "BufTooSmallForPayload",
input: make([]byte, 9),
expectedError: errBufTooSmallForPayload,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockPacket := CreateMockPacket(Packet{
Payload: []byte{0x10, 0x04},
})
_, err := mockPacket.MarshalTo(tc.input)
if err != tc.expectedError {
t.Errorf("Expected error %v, but got %v", tc.expectedError, err)
}
})
}
}
// TestPacketClone checks that cloning a packet returns a deep copy.
func TestPacketClone(t *testing.T) {
h1 := CreateMockPacket(Packet{})
h2 := h1.Clone()
if !reflect.DeepEqual(&h1, &h2) {
t.Error("Clone did not create a deep copy")
}
}
func CreateMockPacket(p Packet) *Packet {
var newPayload []byte
if p.Payload == nil {
newPayload = nil
} else {
newPayload = make([]byte, len(p.Payload))
copy(newPayload, p.Payload)
}
return &Packet{Header: CreateMockHeader(p.Header), Payload: newPayload}
}