-
Notifications
You must be signed in to change notification settings - Fork 8
/
processing_test.go
141 lines (132 loc) · 3.94 KB
/
processing_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
package canlib
import (
"bytes"
"testing"
)
// TestByteArrayToCanFrame checks that ByteArrayToCanFrame accurately converts an Extended CAN frame into a RawCanFrame
func TestByteArrayToCanFrame(t *testing.T) {
frame := []byte{109, 237, 19, 137, 8, 0, 0, 0, 15, 234, 197, 79, 101, 147, 251, 118}
expected := RawCanFrame{
OID: 2299784557,
ID: 152300909,
Rtr: false,
Err: false,
Eff: true,
Dlc: 8,
Data: []byte{15, 234, 197, 79, 101, 147, 251, 118},
CaptureInterface: "test",
}
var result = new(RawCanFrame)
ByteArrayToCanFrame(frame, result, 0, "test")
if result.OID != expected.OID {
t.Error("OID mismatch")
} else if result.ID != expected.ID {
t.Error("ID mismatch")
} else if result.Rtr != expected.Rtr {
t.Error("RTR mismatch")
} else if result.Err != expected.Err {
t.Error("ERR mismatch")
} else if result.Eff != expected.Eff {
t.Error("EFF mismatch")
} else if result.Dlc != expected.Dlc {
t.Error("data length mismatch")
} else if bytes.Equal(result.Data, expected.Data) != true {
t.Error("data value mismatch")
} else if result.CaptureInterface != expected.CaptureInterface {
t.Errorf("capture interface mismatch")
}
}
// BenchmarkByteArrayToCanFrame runs benchmarks on ByteArrayToCanFrame
func BenchmarkByteArrayToCanFrame(b *testing.B) {
frame := []byte{109, 237, 19, 137, 8, 0, 0, 0, 15, 234, 197, 79, 101, 147, 251, 118}
var result = new(RawCanFrame)
b.ResetTimer()
ByteArrayToCanFrame(frame, result, 0, "test")
for i := 0; i < b.N; i++ {
ByteArrayToCanFrame(frame, result, 0, "test")
}
}
// TestProcessRawCan will verify that can messages can be processed
func TestProcessRawCan(t *testing.T) {
testFrame := RawCanFrame{
OID: 1,
ID: 1,
Rtr: false,
Eff: false,
Err: false,
Dlc: 1,
Data: []byte{1},
}
result := ProcessedCanFrame{}
ProcessRawCan(&result, testFrame)
expected := "1#01"
if result.PacketHash != expected {
t.Errorf("%s != %s", result.PacketHash, expected)
}
}
// BenchmarkProcessRawCan runs benchmarks on ProcessRawCan
func BenchmarkProcessRawCan(b *testing.B) {
testFrame := RawCanFrame{
OID: 1,
ID: 1,
Rtr: false,
Eff: false,
Err: false,
Dlc: 1,
Data: []byte{1},
}
result := ProcessedCanFrame{}
for i := 0; i < b.N; i++ {
ProcessRawCan(&result, testFrame)
}
}
// TestProcessCandump will verify that candump messages are appropriately converted to rawcanframe
func TestProcessCandump(t *testing.T) {
expected := RawCanFrame{
OID: 0,
ID: 1,
Rtr: false,
Eff: false,
Err: false,
Dlc: 1,
Data: []byte{1},
Timestamp: 1000000000,
CaptureInterface: "test",
}
result := RawCanFrame{}
ProcessCandump(&result, "(1) test 1#1")
if result.OID != expected.OID || expected.ID != result.ID {
t.Errorf("%v != %v", expected, result)
}
}
// BenchmarkProcessCandump runs benchmarks on ProcessCandump
func BenchmarkProcessCandump(b *testing.B) {
result := RawCanFrame{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ProcessCandump(&result, "(1) test 1#1")
}
}
// TestProcessCanalyze will verify that canalyze dump messages are appropriately converted to rawcanframe
func TestProcessCanalyze(t *testing.T) {
example := "vcan0 1513023350.178403 1 NOEFF NORTR NOERR 1 2 21 22"
expected := RawCanFrame{
OID: 1,
ID: 1,
Rtr: false,
Eff: false,
Err: false,
Dlc: 2,
Data: []byte{33, 34},
Timestamp: 1513023350178402816,
CaptureInterface: "vcan0",
}
result := RawCanFrame{}
ProcessCanalyzeLog(&result, example)
if result.OID != expected.OID || result.ID != expected.ID || result.Dlc != expected.Dlc {
t.Errorf("%v != %v", expected, result)
}
if bytes.Compare(expected.Data, result.Data) != 0 {
t.Errorf("%s != %s", expected.Data, result.Data)
}
}