-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpath_test.go
114 lines (103 loc) · 2.49 KB
/
path_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
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)
}
})
}
}