forked from xiegeo/modbusone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_test.go
76 lines (74 loc) · 1.83 KB
/
tcp_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
package modbusone
import (
"bytes"
"io"
"testing"
)
func Test_readTCP(t *testing.T) {
type args struct {
r io.Reader
bs []byte
}
tests := []struct {
name string
args args
wantN int
wantErr bool
}{
{
name: "sample",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x11, 0x03, 0x00, 0x6B, 0x00, 0x03}),
bs: make([]byte, MaxPDUSize),
},
wantN: 12,
wantErr: false,
}, {
name: "early EOF",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x11, 0x03, 0x00, 0x6B, 0x00}),
bs: make([]byte, MaxPDUSize),
},
wantErr: true,
}, {
name: "length too long A",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x11, 0x03, 0x00, 0x6B, 0x00, 0x03}),
bs: make([]byte, MaxPDUSize),
},
wantErr: true,
}, {
name: "length too long B",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x11, 0x03, 0x00, 0x6B, 0x00, 0x03}),
bs: make([]byte, MaxPDUSize),
},
wantErr: true,
}, {
name: "unexpected Protocol Identifier A",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x01, 0x00, 0x00, 0x06, 0x11, 0x03, 0x00, 0x6B, 0x00, 0x03}),
bs: make([]byte, MaxPDUSize),
},
wantErr: true,
}, {
name: "unexpected Protocol Identifier B",
args: args{
r: bytes.NewBuffer([]byte{0x00, 0x01, 0x00, 0x01, 0x00, 0x06, 0x11, 0x03, 0x00, 0x6B, 0x00, 0x03}),
bs: make([]byte, MaxPDUSize),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotN, err := readTCP(tt.args.r, tt.args.bs)
if !tt.wantErr && gotN != tt.wantN {
t.Errorf("readTCP() = %v, want %v", gotN, tt.wantN)
}
if (err != nil) != tt.wantErr {
t.Errorf("readTCP() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}