-
Notifications
You must be signed in to change notification settings - Fork 5
/
message_test.go
127 lines (109 loc) · 3.15 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
package modbus
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewValue(t *testing.T) {
tests := []struct {
value int
expected []byte
}{
{19, []byte{0x0, 0x13}},
{-128, []byte{0xff, 0x80}},
{-2391, []byte{0xf6, 0xa9}},
{14459, []byte{0x38, 0x7b}},
}
for _, test := range tests {
rv, err := NewValue(test.value)
assert.Nil(t, err)
bytes, err := rv.MarshalBinary()
assert.Nil(t, err)
assert.Equal(t, test.expected, bytes)
}
}
func TestValueSet(t *testing.T) {
tests := []struct {
value int
expected error
}{
{-9, nil},
{-129431, errors.New("-129431 doesn't fit in 16 bytes")},
{12, nil},
{73892, errors.New("73892 doesn't fit in 16 bytes")},
}
for _, test := range tests {
var v Value
err := v.Set(test.value)
assert.Equal(t, test.expected, err)
}
}
func TestMBAP(t *testing.T) {
tests := []struct {
mbap MBAP
data []byte
}{
{MBAP{TransactionID: 18, ProtocolID: 1, Length: 18, UnitID: 2}, []byte{0x0, 0x12, 0x0, 0x1, 0x0, 0x12, 0x2}},
{MBAP{TransactionID: 18493, ProtocolID: 1, Length: 300, UnitID: 25}, []byte{0x48, 0x3d, 0x0, 0x1, 0x1, 0x2c, 0x19}},
{MBAP{TransactionID: 54602, ProtocolID: 1, Length: 20110, UnitID: 91}, []byte{0xd5, 0x4a, 0x0, 0x1, 0x4e, 0x8e, 0x5b}},
}
for _, test := range tests {
var m MBAP
assert.Nil(t, m.UnmarshalBinary(test.data))
assert.Equal(t, test.mbap, m)
data, err := m.MarshalBinary()
assert.Nil(t, err)
assert.Equal(t, test.data, data)
}
var m MBAP
// UnmarshalBinary should return error if length of given byte slice is not 7.
assert.NotNil(t, m.UnmarshalBinary([]byte{}))
}
func TestRequest(t *testing.T) {
mbap := MBAP{
TransactionID: 1,
ProtocolID: 1,
Length: 6,
UnitID: 3,
}
tests := []struct {
request Request
data []byte
}{
// Read 5 coils starting from address 2.
{Request{MBAP: mbap, FunctionCode: 1, Data: []byte{0x0, 0x2, 0x0, 0x5}}, []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x06, 0x3, 0x1, 0x0, 0x2, 0x0, 0x5}},
// Read 2 read discrete inputs starting from address 19.
{Request{MBAP: mbap, FunctionCode: 2, Data: []byte{0x0, 0x14, 0x0, 0x2}}, []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x06, 0x3, 0x2, 0x0, 0x14, 0x0, 0x2}},
}
for _, test := range tests {
var r Request
assert.Nil(t, r.UnmarshalBinary(test.data))
assert.Equal(t, test.request, r)
}
}
func TestResponse(t *testing.T) {
request := Request{
MBAP: MBAP{
TransactionID: 1,
ProtocolID: 1,
Length: 5,
UnitID: 3,
},
FunctionCode: 4,
Data: []byte{},
}
tests := []struct {
response *Response
data []byte
}{
{NewErrorResponse(request, IllegalFunctionError), []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x03, 0x3, 0x84, 0x1}},
{NewErrorResponse(request, AcknowledgeError), []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x03, 0x3, 0x84, 0x5}},
{NewResponse(request, []byte{0x24, 0x41}), []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x05, 0x3, 0x4, 0x2, 0x24, 0x41}},
{NewResponse(request, []byte{0x1, 0x9, 0x12, 0x3}), []byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x07, 0x3, 0x4, 0x4, 0x1, 0x9, 0x12, 0x3}},
}
for _, test := range tests {
data, err := test.response.MarshalBinary()
assert.Nil(t, err)
assert.Equal(t, test.data, data)
}
}