-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket_test.go
93 lines (88 loc) · 2.27 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
package hacket
import (
"bytes"
"testing"
)
// testaPacketHandler - dummy function used for lookup tests
func testPacketHandler(packet *Packet, pw *PacketWriter) error {
// dummy
return nil
}
func TestMessageBuilderWithPacketType(t *testing.T) {
testcases := []struct {
name string
payload []byte
msgType PacketType
want PacketMessage
}{
{
name: "test-messagebuilder-1",
payload: []byte("test1"),
msgType: PacketType(uint8(1)),
want: PacketMessage(append([]byte{uint8(1)}, []byte("test1")...)),
},
{
name: "test-messagebuilder-2",
payload: []byte("test2"),
msgType: PacketType(uint8(2)),
want: PacketMessage(append([]byte{uint8(2)}, []byte("test2")...)),
},
{
name: "test-messagebuilder-3",
payload: []byte("!@#$%^&*()1234567890qwertyuiopasdfghjklzxcvbnm{}[]:;?"),
msgType: PacketType(uint8(3)),
want: PacketMessage(append([]byte{uint8(3)}, []byte("!@#$%^&*()1234567890qwertyuiopasdfghjklzxcvbnm{}[]:;?")...)),
},
}
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
msg, err := NewPacketMessageBuilder(tt.payload).WithPacketType(tt.msgType).Build()
if err != nil {
t.Error(err)
}
if value := bytes.Compare(msg, tt.want); value != 0 {
t.Error("Byte slices do not match")
}
})
}
}
func TestMessageBuilderNoPacketType(t *testing.T) {
testcases := []struct {
name string
payload []byte
want PacketMessage
}{
{
name: "test-messagebuilder-1",
payload: []byte("test1"),
want: PacketMessage([]byte("test1")),
},
{
name: "test-messagebuilder-2",
payload: []byte("test2"),
want: PacketMessage([]byte("test2")),
},
{
name: "test-messagebuilder-3",
payload: []byte("!@#$%^&*()1234567890qwertyuiopasdfghjklzxcvbnm{}[]:;?"),
want: PacketMessage([]byte("!@#$%^&*()1234567890qwertyuiopasdfghjklzxcvbnm{}[]:;?")),
},
}
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
msg, err := NewPacketMessageBuilder(tt.payload).Build()
if err != nil {
t.Error(err)
}
if value := bytes.Compare(msg, tt.want); value != 0 {
t.Error("Byte slices do not match")
}
})
}
}
func TestMessageBuilderNil(t *testing.T) {
_, err := NewPacketMessageBuilder(nil).Build()
if err == nil {
t.Error(err)
}
}