-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathioi_test.go
148 lines (136 loc) · 3.87 KB
/
ioi_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
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
package gologix
import (
"fmt"
"testing"
)
// these tests came from the tag names in 1756-PM020H-EN-P
// it only tests the request path portion of each tag addressing example
// it also only tests symbolic paths.
func TestIOI(t *testing.T) {
var tests = []struct {
path string
t CIPType
want []byte
}{
{
"profile[0,1,257]",
CIPTypeDINT,
[]byte{
0x91, 0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x00, // symbolic segment for "profile"
0x28, 0x00, // member segment for 0
0x28, 0x01, // member segment for 1
0x29, 0x00, 0x01, 0x01, // member segment for 257
},
},
{
"profile[1,2,258]",
CIPTypeDINT,
[]byte{
0x91, 0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x00, // symbolic segment for "profile"
0x28, 0x01, // member segment for 1
0x28, 0x02, // member segment for 2
0x29, 0x00, 0x02, 0x01, // member segment for 258
},
},
{
"profile[300,2,258]",
CIPTypeDINT,
[]byte{
0x91, 0x07, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x00, // symbolic segment for "profile"
0x29, 0x00, 0x2c, 0x01, // member segment for 300
0x28, 0x02, // member segment for 2
0x29, 0x00, 0x02, 0x01, // member segment for 258
},
},
{
"dwell3.acc",
CIPTypeDINT,
[]byte{
0x91, 0x06, 0x64, 0x77, 0x65, 0x6C, 0x6C, 0x33, // symbolic segment for "dwell3"
0x91, 0x03, 0x61, 0x63, 0x63, 0x00, // member segment for ACC
},
},
{
"struct3.today.rate",
CIPTypeStruct,
[]byte{
0x91, 0x07, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x33, 0x00, // symbolic segment for "struct3"
0x91, 0x05, 0x74, 0x6F, 0x64, 0x61, 0x79, 0x00, // symbolic segment for today
0x91, 0x04, 0x72, 0x61, 0x74, 0x65, // symbolic segment for rate
},
},
{
"my2dstruct4[1].today.hourlycount[3]",
CIPTypeINT,
[]byte{
0x91, 0x0B, 0x6d, 0x79, 0x32, 0x64, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x34, 0x00, // symbolic segment for my2dstruct4
0x28, 0x01, // index 1
0x91, 0x05, 0x74, 0x6F, 0x64, 0x61, 0x79, 0x00, //today
0x91, 0x0B, 0x68, 0x6F, 0x75, 0x72, 0x6C, 0x79, 0x63, 0x6F, 0x75, 0x6E, 0x74, 0x00, // hourlycount
0x28, 0x03, // index 3
},
},
{
"My2DstRucT4[1].ToDaY.hoURLycOuNt[3]",
CIPTypeINT,
[]byte{
0x91, 0x0B, 0x6d, 0x79, 0x32, 0x64, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x34, 0x00, // symbolic segment for my2dstruct4
0x28, 0x01, // index 1
0x91, 0x05, 0x74, 0x6F, 0x64, 0x61, 0x79, 0x00, //today
0x91, 0x0B, 0x68, 0x6F, 0x75, 0x72, 0x6C, 0x79, 0x63, 0x6F, 0x75, 0x6E, 0x74, 0x00, // hourlycount
0x28, 0x03, // index 3
},
},
}
client := Client{}
for _, tt := range tests {
testname := fmt.Sprintf("tag: %s", tt.path)
t.Run(testname, func(t *testing.T) {
res, err := client.newIOI(tt.path, tt.t)
if err != nil {
t.Errorf("IOI Generation error. %v", err)
}
if !check_bytes(res.Buffer, tt.want) {
t.Errorf("Wrong Value for result. \nWanted %v. \nGot %v", to_hex(tt.want), to_hex(res.Buffer))
}
})
}
}
func to_hex(b []byte) []string {
out := make([]string, len(b))
for i, v := range b {
out[i] = fmt.Sprintf("% X", v)
}
return out
}
func TestIOIToBytesAndBackAgain(t *testing.T) {
tests := []struct {
Tag string
Type CIPType
}{
{"test", CIPTypeDINT},
{"test[2]", CIPTypeDINT},
{"test[2,3]", CIPTypeDINT},
{"test[3000,3]", CIPTypeDINT},
{"test.tester", CIPTypeDINT},
{"test[2,3].tester", CIPTypeDINT},
}
client := Client{}
for _, tt := range tests {
testname := fmt.Sprintf("tag: %s", tt.Tag)
t.Run(testname, func(t *testing.T) {
res, err := client.newIOI(tt.Tag, tt.Type)
if err != nil {
t.Errorf("IOI Generation error. %v", err)
}
item := newItem(cipItem_Null, res)
path, err := getTagFromPath(&item)
if err != nil {
t.Errorf("problem parsing path from byte item")
}
if path != tt.Tag {
t.Errorf("Wrong Value for result. \nWanted %v. \nGot %v", tt.Tag, path)
}
})
}
}