-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler_test.go
121 lines (109 loc) · 3.87 KB
/
handler_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
package modbus
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadHandler(t *testing.T) {
h := NewReadHandler(func(unitID, start, quantity int) ([]Value, error) {
assert.Equal(t, 0, unitID)
assert.Equal(t, 5, start)
assert.Equal(t, 3, quantity)
return []Value{Value{0}, Value{1}, Value{1}}, nil
})
tests := []struct {
req Request
expected []byte
}{
{
Request{MBAP{}, ReadCoils, []byte{0x0, 0x5, 0x0, 0x3}},
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x1, 0x1, 0x6},
},
{
Request{MBAP{}, ReadHoldingRegisters, []byte{0x0, 0x5, 0x0, 0x3}},
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x9, 0x0, 0x3, 0x6, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1},
},
}
for _, test := range tests {
buf := new(bytes.Buffer)
h.ServeModbus(buf, test.req)
assert.Equal(t, test.expected, buf.Bytes())
}
}
func TestReduce(t *testing.T) {
tests := []struct {
input []Value
expected []byte
}{
{[]Value{Value{0}, Value{1}, Value{1}, Value{1}}, []byte{0xe}},
{[]Value{Value{1}, Value{0}, Value{1}, Value{0}, Value{1}, Value{0}, Value{1}, Value{0}, Value{1}}, []byte{0x1, 0x55}},
{[]Value{Value{1}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{1}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}, Value{0}}, []byte{0x0, 0x1, 0x1}},
}
for _, test := range tests {
assert.Equal(t, test.expected, reduce(test.input))
}
}
func newWriteHandler(t *testing.T, unitID, start int, values []Value, response error, s Signedness) *WriteHandler {
return NewWriteHandler(func(u, s int, v []Value) error {
assert.Equal(t, unitID, u)
assert.Equal(t, start, s)
assert.Equal(t, values, v)
return response
}, s)
}
func TestWriteHandler(t *testing.T) {
tests := []struct {
req Request
h *WriteHandler
expected []byte
}{
{
Request{MBAP{}, WriteSingleCoil, []byte{0x0, 0x1, 0x0, 0x0}},
newWriteHandler(t, 0, 1, []Value{Value{0}}, nil, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x5, 0x0, 0x01, 0x0, 0x0},
},
{
Request{MBAP{}, WriteSingleCoil, []byte{0x0, 0x1, 0xc, 0x1}},
newWriteHandler(t, 0, 1, []Value{Value{1}}, IllegalFunctionError, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x85, 0x01},
},
{
Request{MBAP{}, WriteSingleRegister, []byte{0x0, 0x1, 0xf3, 0x88}},
newWriteHandler(t, 0, 1, []Value{Value{-3192}}, nil, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x6, 0x0, 0x01, 0xf3, 0x88},
},
{
Request{MBAP{}, WriteSingleRegister, []byte{0x0, 0x1, 0xf3, 0x88}},
newWriteHandler(t, 0, 1, []Value{Value{62344}}, nil, Unsigned),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x6, 0x0, 0x01, 0xf3, 0x88},
},
{
Request{MBAP{}, WriteSingleRegister, []byte{0x0, 0x1, 0xc, 0x78}},
newWriteHandler(t, 0, 1, []Value{Value{3192}}, SlaveDeviceBusyError, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x86, 0x6},
},
{
// Valid write multiple registers request.
Request{MBAP{}, WriteMultipleRegisters, []byte{0x0, 0x1, 0x0, 0x2, 0x4, 0x3c, 0x13, 0xf3, 0x88}},
newWriteHandler(t, 0, 1, []Value{Value{0x3c13}, Value{-3192}}, nil, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x10, 0x0, 0x1, 0x0, 0x2},
},
{
// Valid write multiple registers request.
Request{MBAP{}, WriteMultipleRegisters, []byte{0x0, 0x1, 0x0, 0x2, 0x4, 0x3c, 0x13, 0xf3, 0x88}},
newWriteHandler(t, 0, 1, []Value{Value{0x3c13}, Value{62344}}, nil, Unsigned),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x10, 0x0, 0x1, 0x0, 0x2},
},
{
// Invalid write multiple registers request, the length doesn't match.
Request{MBAP{}, WriteMultipleRegisters, []byte{0x0, 0x1, 0x0, 0x2, 0x4, 0x3c, 0x13, 0x01}},
newWriteHandler(t, 0, 1, []Value{}, nil, Signed),
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x90, 0x3},
},
}
for _, test := range tests {
buf := new(bytes.Buffer)
test.h.ServeModbus(buf, test.req)
assert.Equal(t, test.expected, buf.Bytes())
}
}