forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
117 lines (92 loc) · 2.91 KB
/
server_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
package mbserver
import (
"testing"
"time"
"github.com/goburrow/modbus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAduRegisterAndNumber(t *testing.T) {
var frame TCPFrame
SetDataWithRegisterAndNumber(&frame, 0, 64)
expect := []byte{0, 0, 0, 64}
got := frame.Data
assert.Equal(t, expect, got)
}
func TestAduSetDataWithRegisterAndNumberAndValues(t *testing.T) {
var frame TCPFrame
SetDataWithRegisterAndNumberAndValues(&frame, 7, 2, []uint16{3, 4})
expect := []byte{0, 7, 0, 2, 4, 0, 3, 0, 4}
got := frame.Data
assert.Equal(t, expect, got)
}
func TestUnsupportedFunction(t *testing.T) {
s := NewServer()
var frame TCPFrame
frame.Function = 255
var req Request
req.frame = &frame
response := s.handle(&req)
exception := GetException(response)
assert.Equalf(t, exception, IllegalFunction, "expected IllegalFunction (%d), got (%v)", IllegalFunction, exception)
}
func TestModbus(t *testing.T) {
// Server
s := NewServer()
err := s.ListenTCP("127.0.0.1:3333")
require.NoError(t, err)
t.Cleanup(s.Shutdown)
go s.Serve()
// Allow the server to start and to avoid a connection refused on the client
time.Sleep(1 * time.Millisecond)
// Client
handler := modbus.NewTCPClientHandler("127.0.0.1:3333")
// Connect manually so that multiple requests are handled in one connection session
err = handler.Connect()
require.NoError(t, err)
t.Cleanup(func() { handler.Close() })
client := modbus.NewClient(handler)
t.Run("Coils", func(t *testing.T) {
results, err := client.WriteMultipleCoils(100, 9, []byte{255, 1})
require.NoError(t, err)
results, err = client.ReadCoils(100, 16)
require.NoError(t, err)
expect := []byte{255, 1}
got := results
assert.Equal(t, expect, got)
})
// Discrete inputs
t.Run("Discrete inputs", func(t *testing.T) {
results, err := client.ReadDiscreteInputs(0, 64)
require.NoError(t, err)
// test: 2017/05/14 21:09:53 modbus: sending 00 01 00 00 00 06 ff 02 00 00 00 40
// test: 2017/05/14 21:09:53 modbus: received 00 01 00 00 00 0b ff 02 08 00 00 00 00 00 00 00 00
expect := []byte{0, 0, 0, 0, 0, 0, 0, 0}
got := results
assert.Equal(t, expect, got)
})
t.Run("Holding registers", func(t *testing.T) {
// Holding registers
results, err := client.WriteMultipleRegisters(1, 2, []byte{0, 3, 0, 4})
require.NoError(t, err)
// received: 00 01 00 00 00 06 ff 10 00 01 00 02
expect := []byte{0, 2}
got := results
assert.Equal(t, expect, got)
results, err = client.ReadHoldingRegisters(1, 2)
require.NoError(t, err)
expect = []byte{0, 3, 0, 4}
got = results
assert.Equal(t, expect, got)
})
t.Run("Input registers", func(t *testing.T) {
// Input registers
s.InputRegisters[65530] = 1
s.InputRegisters[65535] = 65535
results, err := client.ReadInputRegisters(65530, 6)
require.NoError(t, err)
expect := []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255}
got := results
assert.Equal(t, expect, got)
})
}