-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_test.go
194 lines (176 loc) · 4.89 KB
/
message_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
189
190
191
192
193
194
// Copyright 2017 Jacob Hesch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integra
import (
"errors"
"testing"
)
func TestNewEISCPPacket(t *testing.T) {
expected := []byte{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x30, 0x0a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}
result := newEISCPPacket()
_ = result.init("PWR00")
for i, b := range result {
if b != expected[i] {
t.Errorf("%v did not match expected %v at index %v", b, expected[i], i)
}
}
}
func TestNewEISCPPacketTooBig(t *testing.T) {
expected := "Message 'OVER THE HILLS' too long"
packet := newEISCPPacket()
result := packet.init("OVER THE HILLS")
if result == nil {
t.Error("expected non-nil error")
} else if result.Error() != expected {
t.Errorf("expected error %v but got %v", expected, result)
}
}
func TestEISCPPacketMessage(t *testing.T) {
expected := "PWR01"
packet := eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x0a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}
m, _ := packet.message()
result := m.String()
if result != expected {
t.Errorf("%v did not match %v", result, expected)
}
}
func TestCheckEISPPacket(t *testing.T) {
tests := []struct {
input eISCPPacket
expected error
}{
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, nil},
{make(eISCPPacket, 32),
errors.New("first 4 header bytes do not match ISCP")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x51,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("first 4 header bytes do not match ISCP")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x20, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("first 2 data bytes do not match !1")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("first 2 data bytes do not match !1")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x11,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("header size 0x11 is not expected size 0x10")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x11,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x1a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("data size 0x11 greater than max size 0x10")},
{eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x0a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}, errors.New("end of packet 0x0a did not match expected value 0x1a")},
}
for _, test := range tests {
result := test.input.check(endOfPacketRx)
if result == nil && test.expected == nil {
// ok
} else if result == nil || test.expected == nil {
t.Errorf("'%v' did not match expected '%v'", result, test.expected)
} else if result.Error() != test.expected.Error() {
t.Errorf("'%v' did not match expected '%v'", result, test.expected)
}
}
}
func TestDebugString(t *testing.T) {
packet := eISCPPacket{
0x49, 0x53, 0x43, 0x50,
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x08,
0x01, 0x00, 0x00, 0x00,
0x21, 0x31, 0x50, 0x57,
0x52, 0x30, 0x31, 0x0a,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}
expected := `
0x49 0x53 0x43 0x50
0x00 0x00 0x00 0x10
0x00 0x00 0x00 0x08
0x01 0x00 0x00 0x00
0x21 0x31 0x50 0x57
0x52 0x30 0x31 0x0a
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
`
if result := packet.debugString(); result != expected {
t.Errorf("%v did not match %v", result, expected)
}
}