-
Notifications
You must be signed in to change notification settings - Fork 2
/
frame_test.go
122 lines (107 loc) · 2.45 KB
/
frame_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
package hdlc
import (
"bytes"
"crypto/rand"
"reflect"
"strings"
"testing"
)
func TestRound(t *testing.T) {
payload := []byte("1234")
t.Run("full round encapsulate/valid", func(t *testing.T) {
frame := Encapsulate(payload, false)
if !frame.Valid() {
t.Error("created frame isn't valid")
}
})
t.Run("full round encode/decode", func(t *testing.T) {
var buf bytes.Buffer
encoder := NewEncoder(&buf)
_, err := encoder.WriteFrame(Encapsulate(payload, true))
if err != nil {
t.Error(err)
}
decoder := NewDecoder(&buf)
frame, err := decoder.ReadFrame()
if err != nil {
t.Error(err)
}
if !frame.HasAddressCtrlPrefix {
t.Error("frame was not recognized as having the addressCtrlPrefix")
}
if !reflect.DeepEqual(payload, frame.Payload) {
t.Error("final payload doesn't match initial payload")
}
if !frame.Valid() {
t.Error("final frame is not valid")
}
})
}
func TestFrame_Valid(t *testing.T) {
tests := []struct {
name string
f Frame
want bool
}{
{
name: "test vector payload",
f: Frame{
Payload: []byte("123456789"),
FCS: []byte{0x6e, 0x90}, // 0x6f91 ^ 0xffff, little endian
HasAddressCtrlPrefix: false,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.f.Valid(); got != tt.want {
t.Errorf("Frame.Valid() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkEncodeDecode(b *testing.B) {
payloads := []struct {
Description string
PayloadSize int
}{
{
Description: "Small",
PayloadSize: 20,
},
{
Description: "Medium",
PayloadSize: 700,
},
{
Description: "Big",
PayloadSize: 1500,
},
}
hasAddressCtrlPrefixes := []bool{true, false}
for _, payloadDescr := range payloads {
payload := make([]byte, payloadDescr.PayloadSize)
for _, hasAddressCtrlPrefix := range hasAddressCtrlPrefixes {
var name strings.Builder
name.WriteString(payloadDescr.Description)
name.WriteString("Payload")
if hasAddressCtrlPrefix {
name.WriteString("WithAddressCtrlPrefix")
} else {
name.WriteString("WithoutAddressCtrlPrefix")
}
var buf bytes.Buffer
encoder := NewEncoder(&buf)
decoder := NewDecoder(&buf)
rand.Read(payload)
b.Run(name.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.SetBytes(int64(payloadDescr.PayloadSize))
encoder.WriteFrame(Encapsulate(payload, true))
decoder.ReadFrame()
}
})
}
}
}