-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathavc_test.go
215 lines (189 loc) · 7.15 KB
/
avc_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci_test
import (
. "github.com/cboling/omci/v2"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestAttributeValueChangeDecode(t *testing.T) {
goodMessage := "0000110a0007000080004d4c2d33363236000000000000000000000000000000000000000000000000000028"
data, err := stringToPacket(goodMessage)
assert.NoError(t, err)
packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
assert.NotNil(t, packet)
omciLayer := packet.Layer(LayerTypeOMCI)
assert.NotNil(t, omciLayer)
omciMsg, ok := omciLayer.(*OMCI)
assert.True(t, ok)
assert.NotNil(t, omciMsg)
assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
assert.Equal(t, LayerTypeAttributeValueChange, omciMsg.NextLayerType())
assert.Equal(t, uint16(0x0), omciMsg.TransactionID)
assert.Equal(t, AttributeValueChangeType, omciMsg.MessageType)
assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier)
assert.Equal(t, uint16(40), omciMsg.Length)
msgLayer := packet.Layer(LayerTypeAttributeValueChange)
assert.NotNil(t, msgLayer)
request, ok2 := msgLayer.(*AttributeValueChangeMsg)
assert.True(t, ok2)
assert.NotNil(t, request)
assert.Equal(t, LayerTypeAttributeValueChange, request.LayerType())
assert.Equal(t, LayerTypeAttributeValueChange, request.CanDecode())
assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
assert.Equal(t, uint16(0x8000), request.AttributeMask)
assert.Equal(t, me.SoftwareImageClassID, request.EntityClass)
assert.Equal(t, uint16(0), request.EntityInstance)
assert.Equal(t, []byte{
0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
request.Attributes["Version"])
// Verify string output for message
packetString := packet.String()
assert.NotZero(t, len(packetString))
}
func TestAttributeValueChangeSerialize(t *testing.T) {
goodMessage := "0000110a0007000080004d4c2d33363236000000000000000000000000000000000000000000000000000028"
omciLayer := &OMCI{
TransactionID: 0,
MessageType: AttributeValueChangeType,
// DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
// Length: 0x28, // Optional, defaults to 40 octets
}
request := &AttributeValueChangeMsg{
MeBasePacket: MeBasePacket{
EntityClass: me.SoftwareImageClassID,
EntityInstance: uint16(0),
},
AttributeMask: uint16(0x8000),
Attributes: me.AttributeValueMap{
"Version": []byte{
0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
}
// Test serialization back to former string
var options gopacket.SerializeOptions
options.FixLengths = true
buffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
assert.NoError(t, err)
outgoingPacket := buffer.Bytes()
reconstituted := packetToString(outgoingPacket)
assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
}
func TestAttributeValueChangeNonZeroTicSerialize(t *testing.T) {
omciLayer := &OMCI{
TransactionID: 1,
MessageType: AttributeValueChangeType,
}
request := &AttributeValueChangeMsg{
MeBasePacket: MeBasePacket{
EntityClass: me.SoftwareImageClassID,
EntityInstance: uint16(0),
},
AttributeMask: uint16(0x8000),
Attributes: me.AttributeValueMap{
"Version": []byte{
0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
}
// Test serialization back to former string
var options gopacket.SerializeOptions
options.FixLengths = true
buffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
assert.Error(t, err)
}
func TestExtendedAttributeValueChangeDecode(t *testing.T) {
// Software Image Version (14 bytes) AVC
goodMessage := "0000110b00070000001080004d4c2d3336323600000000000000"
data, err := stringToPacket(goodMessage)
assert.NoError(t, err)
packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
assert.NotNil(t, packet)
omciLayer := packet.Layer(LayerTypeOMCI)
assert.NotNil(t, omciLayer)
omciMsg, ok := omciLayer.(*OMCI)
assert.True(t, ok)
assert.NotNil(t, omciMsg)
assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
assert.Equal(t, LayerTypeAttributeValueChange, omciMsg.NextLayerType())
assert.Equal(t, uint16(0x0), omciMsg.TransactionID)
assert.Equal(t, AttributeValueChangeType, omciMsg.MessageType)
assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier)
assert.Equal(t, uint16(2+14), omciMsg.Length)
msgLayer := packet.Layer(LayerTypeAttributeValueChange)
assert.NotNil(t, msgLayer)
request, ok2 := msgLayer.(*AttributeValueChangeMsg)
assert.True(t, ok2)
assert.NotNil(t, request)
assert.Equal(t, LayerTypeAttributeValueChange, request.LayerType())
assert.Equal(t, LayerTypeAttributeValueChange, request.CanDecode())
assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
assert.Equal(t, uint16(0x8000), request.AttributeMask)
assert.Equal(t, me.SoftwareImageClassID, request.EntityClass)
assert.Equal(t, uint16(0), request.EntityInstance)
assert.Equal(t, []byte{
0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, request.Attributes["Version"])
// Verify string output for message
packetString := packet.String()
assert.NotZero(t, len(packetString))
}
func TestExtendedAttributeValueChangeSerialize(t *testing.T) {
goodMessage := "0000110b00070000001080004d4c2d3336323600000000000000"
omciLayer := &OMCI{
TransactionID: 0,
MessageType: AttributeValueChangeType,
DeviceIdentifier: ExtendedIdent,
// Length parameter is optional for Extended message format serialization
// and if present it will be overwritten during the serialization with the
// actual value.
}
request := &AttributeValueChangeMsg{
MeBasePacket: MeBasePacket{
EntityClass: me.SoftwareImageClassID,
EntityInstance: uint16(0),
Extended: true,
},
AttributeMask: uint16(0x8000),
Attributes: me.AttributeValueMap{
"Version": []byte{
0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
}
// Test serialization back to former string
var options gopacket.SerializeOptions
options.FixLengths = true
buffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
assert.NoError(t, err)
outgoingPacket := buffer.Bytes()
reconstituted := packetToString(outgoingPacket)
assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
}