-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpacket_test.go
127 lines (113 loc) · 3.49 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
package ipod_test
import (
"bytes"
"reflect"
"testing"
"github.com/oandrew/ipod"
_ "github.com/oandrew/ipod/lingo-general"
)
func TestPacketWriter_WritePacket(t *testing.T) {
largeData := bytes.Repeat([]byte{0xee}, 255)
tests := []struct {
name string
data []byte
want []byte
wantErr bool
}{
{"no-data", []byte{}, nil, true},
{"with-data", []byte{0x01, 0x02, 0xfd}, []byte{0x55, 0x03, 0x01, 0x02, 0xfd, 0xfd}, false},
{"large-with-data", append([]byte{0x1, 0x02}, largeData...), append([]byte{0x55, 0x00, 0x01, 0x01, 0x1, 0x02}, append(largeData, 0xe9)...), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := ipod.NewPacketWriter()
err := w.WritePacket(tt.data)
got := w.Bytes()
if (err != nil) != tt.wantErr {
t.Errorf("PacketWriter.WritePacket() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !bytes.Equal(got, tt.want) {
t.Errorf("PacketWriter.WritePacket() = %v, want %v", got, tt.want)
}
})
}
}
func TestPacketReader_ReadPacket(t *testing.T) {
largeData := bytes.Repeat([]byte{0xee}, 255)
tests := []struct {
name string
data []byte
want []byte
wantErr bool
}{
{"no-data", []byte{0x55, 0x02, 0x01, 0x02, 256 - 0x05}, []byte{0x01, 0x02}, false},
{"with-data", []byte{0x55, 0x03, 0x01, 0x02, 0xfd, 0xfd}, []byte{0x01, 0x02, 0xfd}, false},
{"bad-crc", []byte{0x55, 0x03, 0x01, 0x02, 0xfd, 0x22}, nil, true},
{"wrong-start-byte", []byte{0xff, 0x03, 0x01, 0x02, 0xfd, 0xfd}, nil, true},
{"large-with-data", append([]byte{0x55, 0x00, 0x01, 0x01, 0x1, 0x02}, append(largeData, 0xe9)...), append([]byte{0x1, 0x02}, largeData...), false},
{"large-with-data-short", append([]byte{0x55, 0x00, 0x01, 0x02, 0x1, 0x02}, append(largeData, 0xe9)...), nil, true},
{"large-bad-crc", append([]byte{0x55, 0x00, 0x01, 0x01, 0x1, 0x02}, append(largeData, 0x22)...), nil, true},
{"short-packet", []byte{0x55}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := ipod.NewPacketReader(tt.data)
got, err := r.ReadPacket()
if (err != nil) != tt.wantErr {
t.Errorf("PacketReader.ReadPacket() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("PacketReader.ReadPacket() = %v, want %v", got, tt.want)
}
})
}
}
func TestPacketRoundtrip(t *testing.T) {
pw := [][]byte{
[]byte("packet1"),
[]byte("_packet2"),
}
w := ipod.NewPacketWriter()
for i := range pw {
w.WritePacket(pw[i])
}
r := ipod.NewPacketReader(w.Bytes())
for i := range pw {
pr, err := r.ReadPacket()
if err != nil {
t.Error(err)
}
if !bytes.Equal(pr, pw[i]) {
t.Fail()
}
}
}
func BenchmarkPacketReader(b *testing.B) {
frame := []byte{
0x55, 0x28, 0x0a, 0x03, 0x03, 0xe7, 0x00, 0x00,
0x1f, 0x40, 0x00, 0x00, 0x2b, 0x11, 0x00, 0x00,
0x2e, 0xe0, 0x00, 0x00, 0x3e, 0x80, 0x00, 0x00,
0x56, 0x22, 0x00, 0x00, 0x5d, 0xc0, 0x00, 0x00,
0x7d, 0x00, 0x00, 0x00, 0xac, 0x44, 0x00, 0x00,
0xbb, 0x80, 0x3d, 0x00, 0x00, 0x00, 0x00,
}
for i := 0; i < b.N; i++ {
r := ipod.NewPacketReader(frame)
r.ReadPacket()
}
}
func BenchmarkPacketWriter(b *testing.B) {
packet := []byte{
0x0a, 0x03, 0x03, 0xe7, 0x00, 0x00, 0x1f, 0x40,
0x00, 0x00, 0x2b, 0x11, 0x00, 0x00, 0x2e, 0xe0,
0x00, 0x00, 0x3e, 0x80, 0x00, 0x00, 0x56, 0x22,
0x00, 0x00, 0x5d, 0xc0, 0x00, 0x00, 0x7d, 0x00,
0x00, 0x00, 0xac, 0x44, 0x00, 0x00, 0xbb, 0x80,
}
for i := 0; i < b.N; i++ {
pw := ipod.NewPacketWriter()
pw.WritePacket(packet)
}
}