forked from danomagnum/gologix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_test.go
More file actions
167 lines (153 loc) · 3.95 KB
/
Copy pathpath_test.go
File metadata and controls
167 lines (153 loc) · 3.95 KB
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
package gologix
import (
"fmt"
"testing"
)
func TestPath(t *testing.T) {
var tests = []struct {
path string
want []byte
}{
{
"1,0,2,172.25.58.11,1,1",
[]byte{0x01, 0x00, 0x12, 0x0C, 0x31, 0x37, 0x32, 0x2E, 0x32, 0x35, 0x2E, 0x35, 0x38, 0x2E, 0x31, 0x31, 0x01, 0x01},
},
{
"1,0,32,2,36,1",
[]byte{0x01, 0x00, 0x20, 0x02, 0x24, 0x01},
},
{
"1,0",
[]byte{0x01, 0x00},
},
}
for _, tt := range tests {
testname := fmt.Sprintf("path: %s", tt.path)
t.Run(testname, func(t *testing.T) {
res, err := ParsePath(tt.path)
if err != nil {
t.Errorf("Error in pathgen for %s. %v", tt.path, err)
}
if !check_bytes(res.Bytes(), tt.want) {
t.Errorf("Wrong Value for result. \nWanted %v. \nGot %v", tt.want, res)
}
})
}
}
func check_bytes(s0, s1 []byte) bool {
if len(s1) != len(s0) {
return false
}
for i := range s0 {
if s0[i] != s1[i] {
return false
}
}
return true
}
func TestPathBuild(t *testing.T) {
client := Client{}
client.SocketTimeout = 0
pmp_ioi, err := client.newIOI("Program:MainProgram", 16)
if err != nil {
t.Errorf("problem creating pmp ioi. %v", err)
}
tests := []struct {
name string
path []any
want []byte
}{
{
name: "connection manager only",
path: []any{CipObject_ConnectionManager},
want: []byte{0x20, 0x06},
},
{
name: "backplane to slot 0",
path: []any{CIPPort{PortNo: 1}, CIPAddress(0)},
want: []byte{0x01, 0x00},
},
{
name: "connection manager instance 1",
path: []any{CipObject_ConnectionManager, CIPInstance(1)},
want: []byte{0x20, 0x06, 0x24, 0x01},
},
{
name: "Symbol Object Instance 0",
path: []any{CipObject_Symbol, CIPInstance(0)},
want: []byte{0x20, 0x6B, 0x24, 0x00},
},
{
name: "Symbol Object Instance 0 of tag 'Program:MainProgram'",
path: []any{pmp_ioi, CipObject_Symbol, CIPInstance(0)},
want: []byte{0x91, 0x13, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x3a, 0x6d, 0x61, 0x69,
0x6e, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x00, 0x20, 0x6B, 0x24, 0x00},
},
{
name: "Template Attributes Instance 0x02E9",
path: []any{CipObject_Template, CIPInstance(0x02E9)},
want: []byte{0x20, 0x6C, 0x25, 0x00, 0xE9, 0x02},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
have, err := Serialize(tt.path...)
if err != nil {
t.Errorf("Problem building path. %v", err)
}
if !check_bytes(have.Bytes(), tt.want) {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", have.Bytes(), tt.want)
}
})
}
}
func TestPathBuilder(t *testing.T) {
tests := []struct {
name string
path *PathBuilder
want []byte
}{
{
name: "connection manager only",
path: (&PathBuilder{}).Class(CipObject_ConnectionManager),
want: []byte{0x20, 0x06},
},
{
name: "backplane to slot 0",
path: (&PathBuilder{}).Port(1).Slot(0),
want: []byte{0x01, 0x00},
},
{
name: "connection manager instance 1",
path: (&PathBuilder{}).Class(CipObject_ConnectionManager).Instance(1),
want: []byte{0x20, 0x06, 0x24, 0x01},
},
{
name: "Symbol Object Instance 0",
path: (&PathBuilder{}).Class(CipObject_Symbol).Instance(0),
want: []byte{0x20, 0x6B, 0x24, 0x00},
},
{
name: "Symbol Object Instance 0 of tag 'Program:MainProgram'",
path: (&PathBuilder{}).Symbolic("program:mainprogram").Class(CipObject_Symbol).Instance(0),
want: []byte{0x91, 0x13, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x3a, 0x6d, 0x61, 0x69,
0x6e, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x00, 0x20, 0x6B, 0x24, 0x00},
},
{
name: "Template Attributes Instance 0x02E9",
path: (&PathBuilder{}).Class(CipObject_Template).Instance(0x02E9),
want: []byte{0x20, 0x6C, 0x25, 0x00, 0xE9, 0x02},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pb := tt.path
if pb.Err != nil {
t.Errorf("Problem building path. %v", pb.Err)
}
if !check_bytes(pb.Bytes(), tt.want) {
t.Errorf("ResultMismatch.\n Have %v\n Want %v\n", pb.Bytes(), tt.want)
}
})
}
}