blob: 477f70416376a50bf84e91d4fd7843e0eacbb651 [file] [log] [blame]
Chip Boling610117d2021-09-09 11:24:34 -05001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
3 * Copyright 2020-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package omci_test
19
20import (
21 "github.com/google/gopacket"
22 . "github.com/opencord/omci-lib-go"
23 me "github.com/opencord/omci-lib-go/generated"
24 "github.com/stretchr/testify/assert"
25 "strings"
26 "testing"
27)
28
29func TestGetCurrentDataRequestDecode(t *testing.T) {
30 goodMessage := "035e5c0a01a90000004400000000000000000000000000000000000000000000000000000000000000000028"
31 data, err := stringToPacket(goodMessage)
32 assert.NoError(t, err)
33
34 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
35 assert.NotNil(t, packet)
36
37 omciLayer := packet.Layer(LayerTypeOMCI)
38 assert.NotNil(t, omciLayer)
39
40 omciMsg, ok := omciLayer.(*OMCI)
41 assert.True(t, ok)
42 assert.NotNil(t, omciMsg)
43 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
44 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
45 assert.Equal(t, LayerTypeGetCurrentDataRequest, omciMsg.NextLayerType())
46 assert.Equal(t, uint16(0x035e), omciMsg.TransactionID)
47 assert.Equal(t, GetCurrentDataRequestType, omciMsg.MessageType)
48 assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier)
49 assert.Equal(t, uint16(40), omciMsg.Length)
50
51 msgLayer := packet.Layer(LayerTypeGetCurrentDataRequest)
52 assert.NotNil(t, msgLayer)
53
54 request, ok2 := msgLayer.(*GetCurrentDataRequest)
55 assert.True(t, ok2)
56 assert.NotNil(t, request)
57 assert.Equal(t, LayerTypeGetCurrentDataRequest, request.LayerType())
58 assert.Equal(t, LayerTypeGetCurrentDataRequest, request.CanDecode())
59 assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
60 assert.Equal(t, me.EthernetFrameExtendedPm64BitClassID, request.EntityClass)
61 assert.Equal(t, uint16(0), request.EntityInstance)
62 assert.Equal(t, uint16(0x0044), request.AttributeMask)
63
64 // Verify string output for message
65 packetString := packet.String()
66 assert.NotZero(t, len(packetString))
67}
68
69func TestGetCurrentDataRequestSerialize(t *testing.T) {
70 goodMessage := "035e5c0a01a90000004400000000000000000000000000000000000000000000000000000000000000000028"
71
72 omciLayer := &OMCI{
73 TransactionID: 0x035e,
74 MessageType: GetCurrentDataRequestType,
75 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
76 // Length: 0x28, // Optional, defaults to 40 octets
77 }
78 request := &GetCurrentDataRequest{
79 MeBasePacket: MeBasePacket{
80 EntityClass: me.EthernetFrameExtendedPm64BitClassID,
81 EntityInstance: uint16(0),
82 },
83 AttributeMask: uint16(0x0044),
84 }
85 // Test serialization back to former string
86 var options gopacket.SerializeOptions
87 options.FixLengths = true
88
89 buffer := gopacket.NewSerializeBuffer()
90 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
91 assert.NoError(t, err)
92
93 outgoingPacket := buffer.Bytes()
94 reconstituted := packetToString(outgoingPacket)
95 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
96}
97
98func TestGetCurrentDataRequestZeroTICSerialize(t *testing.T) {
99 omciLayer := &OMCI{
100 TransactionID: 0x0,
101 MessageType: GetCurrentDataRequestType,
102 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
103 // Length: 0x28, // Optional, defaults to 40 octets
104 }
105 request := &GetCurrentDataRequest{
106 MeBasePacket: MeBasePacket{
107 EntityClass: me.EthernetFrameExtendedPm64BitClassID,
108 EntityInstance: uint16(0),
109 },
110 AttributeMask: uint16(0x0044),
111 }
112 // Test serialization back to former string
113 var options gopacket.SerializeOptions
114 options.FixLengths = true
115
116 buffer := gopacket.NewSerializeBuffer()
117 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
118 assert.Error(t, err)
119}
120
121func TestGetCurrentDataResponseDecode(t *testing.T) {
122 goodMessage := "035e3c0a01a90000000044123456781234dbcb432187654321dac1000000000000000000000000000028"
123 data, err := stringToPacket(goodMessage)
124 assert.NoError(t, err)
125
126 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
127 assert.NotNil(t, packet)
128
129 omciLayer := packet.Layer(LayerTypeOMCI)
130 assert.NotNil(t, omciLayer)
131
132 omciMsg, ok := omciLayer.(*OMCI)
133 assert.True(t, ok)
134 assert.NotNil(t, omciMsg)
135 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
136 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
137 assert.Equal(t, LayerTypeGetCurrentDataResponse, omciMsg.NextLayerType())
138 assert.Equal(t, uint16(0x035e), omciMsg.TransactionID)
139 assert.Equal(t, GetCurrentDataResponseType, omciMsg.MessageType)
140 assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier)
141 assert.Equal(t, uint16(40), omciMsg.Length)
142
143 msgLayer := packet.Layer(LayerTypeGetCurrentDataResponse)
144 assert.NotNil(t, msgLayer)
145
146 response, ok2 := msgLayer.(*GetCurrentDataResponse)
147 assert.True(t, ok2)
148 assert.NotNil(t, response)
149 assert.Equal(t, LayerTypeGetCurrentDataResponse, response.LayerType())
150 assert.Equal(t, LayerTypeGetCurrentDataResponse, response.CanDecode())
151 assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType())
152 assert.Equal(t, me.EthernetFrameExtendedPm64BitClassID, response.EntityClass)
153 assert.Equal(t, uint16(0), response.EntityInstance)
154 assert.Equal(t, me.Success, response.Result)
155 assert.Equal(t, uint16(0x0044), response.AttributeMask)
156 assert.Equal(t, uint64(0x123456781234dbcb), response.Attributes["OversizeFrames"])
157 assert.Equal(t, uint64(0x432187654321dac1), response.Attributes["Frames256To511Octets"])
158
159 // Verify string output for message
160 packetString := packet.String()
161 assert.NotZero(t, len(packetString))
162}
163
164func TestGetCurrentDataResponseSerialize(t *testing.T) {
165 goodMessage := "035e3c0a01a90000000044123456781234dbcb432187654321dac10000000000000000000000000000000028"
166
167 omciLayer := &OMCI{
168 TransactionID: 0x035e,
169 MessageType: GetCurrentDataResponseType,
170 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
171 // Length: 0x28, // Optional, defaults to 40 octets
172 }
173 request := &GetCurrentDataResponse{
174 MeBasePacket: MeBasePacket{
175 EntityClass: me.EthernetFrameExtendedPm64BitClassID,
176 EntityInstance: uint16(0),
177 },
178 Result: 0,
179 AttributeMask: uint16(0x0044),
180 Attributes: me.AttributeValueMap{
181 "OversizeFrames": uint64(0x123456781234dbcb),
182 "Frames256To511Octets": uint64(0x432187654321dac1),
183 // BroadcastFrames can be supplied but will not be encoded since not in attribute mask.
184 "BroadcastFrames": uint64(0x0123456789abcdef)},
185 }
186 // Test serialization back to former string
187 var options gopacket.SerializeOptions
188 options.FixLengths = true
189
190 buffer := gopacket.NewSerializeBuffer()
191 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
192 assert.NoError(t, err)
193
194 outgoingPacket := buffer.Bytes()
195 reconstituted := packetToString(outgoingPacket)
196 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
197}
198
199func TestGetCurrentDataResponseZeroTICSerialize(t *testing.T) {
200 omciLayer := &OMCI{
201 TransactionID: 0x0,
202 MessageType: GetCurrentDataResponseType,
203 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
204 // Length: 0x28, // Optional, defaults to 40 octets
205 }
206 request := &GetCurrentDataResponse{
207 MeBasePacket: MeBasePacket{
208 EntityClass: me.EthernetFrameExtendedPm64BitClassID,
209 EntityInstance: uint16(0),
210 },
211 Result: 0,
212 AttributeMask: uint16(0x0044),
213 Attributes: me.AttributeValueMap{
214 "OversizeFrames": uint64(0x123456781234dbcb),
215 "Frames256To511Octets": uint64(0x432187654321dac1),
216 // BroadcastFrames can be supplied but will not be encoded since not in attribute mask.
217 "BroadcastFrames": uint64(0x0123456789abcdef)},
218 }
219 // Test serialization back to former string
220 var options gopacket.SerializeOptions
221 options.FixLengths = true
222
223 buffer := gopacket.NewSerializeBuffer()
224 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
225 assert.Error(t, err)
226}
227
228func TestExtendedGetCurrentDataRequestDecode(t *testing.T) {
229 goodMessage := "035e5c0b0034000100028000"
230 data, err := stringToPacket(goodMessage)
231 assert.NoError(t, err)
232
233 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
234 assert.NotNil(t, packet)
235
236 omciLayer := packet.Layer(LayerTypeOMCI)
237 assert.NotNil(t, omciLayer)
238
239 omciMsg, ok := omciLayer.(*OMCI)
240 assert.True(t, ok)
241 assert.NotNil(t, omciMsg)
242 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
243 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
244 assert.Equal(t, LayerTypeGetCurrentDataRequest, omciMsg.NextLayerType())
245 assert.Equal(t, uint16(0x035e), omciMsg.TransactionID)
246 assert.Equal(t, GetCurrentDataRequestType, omciMsg.MessageType)
247 assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier)
248 assert.Equal(t, uint16(2), omciMsg.Length)
249
250 msgLayer := packet.Layer(LayerTypeGetCurrentDataRequest)
251 assert.NotNil(t, msgLayer)
252
253 request, ok2 := msgLayer.(*GetCurrentDataRequest)
254 assert.True(t, ok2)
255 assert.NotNil(t, request)
256 assert.Equal(t, LayerTypeGetCurrentDataRequest, request.LayerType())
257 assert.Equal(t, LayerTypeGetCurrentDataRequest, request.CanDecode())
258 assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
259 assert.Equal(t, me.MacBridgePortPerformanceMonitoringHistoryDataClassID, request.EntityClass)
260 assert.Equal(t, uint16(0x8000), request.AttributeMask)
261
262 // Verify string output for message
263 packetString := packet.String()
264 assert.NotZero(t, len(packetString))
265}
266
267func TestExtendedGetCurrentDataRequestSerialize(t *testing.T) {
268 goodMessage := "035e5c0b0034000100028000"
269
270 omciLayer := &OMCI{
271 TransactionID: 0x035e,
272 MessageType: GetCurrentDataRequestType,
273 DeviceIdentifier: ExtendedIdent,
274 }
275 request := &GetRequest{
276 MeBasePacket: MeBasePacket{
277 EntityClass: me.MacBridgePortPerformanceMonitoringHistoryDataClassID,
278 EntityInstance: uint16(1),
279 Extended: true,
280 },
281 AttributeMask: uint16(0x8000),
282 }
283 // Test serialization back to former string
284 var options gopacket.SerializeOptions
285 options.FixLengths = true
286
287 buffer := gopacket.NewSerializeBuffer()
288 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
289 assert.NoError(t, err)
290
291 outgoingPacket := buffer.Bytes()
292 reconstituted := packetToString(outgoingPacket)
293 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
294}
295
296func TestExtendedGetCurrentDataResponseDecode(t *testing.T) {
297 goodMessage := "035e3c0b0034000100080080000000000010"
298 data, err := stringToPacket(goodMessage)
299 assert.NoError(t, err)
300
301 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
302 assert.NotNil(t, packet)
303
304 omciLayer := packet.Layer(LayerTypeOMCI)
305 assert.NotNil(t, omciLayer)
306
307 omciMsg, ok := omciLayer.(*OMCI)
308 assert.True(t, ok)
309 assert.NotNil(t, omciMsg)
310 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
311 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
312 assert.Equal(t, LayerTypeGetCurrentDataResponse, omciMsg.NextLayerType())
313 assert.Equal(t, uint16(0x035e), omciMsg.TransactionID)
314 assert.Equal(t, GetCurrentDataResponseType, omciMsg.MessageType)
315 assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier)
316 assert.Equal(t, uint16(8), omciMsg.Length)
317
318 msgLayer := packet.Layer(LayerTypeGetCurrentDataResponse)
319 assert.NotNil(t, msgLayer)
320
321 response, ok2 := msgLayer.(*GetCurrentDataResponse)
322 assert.True(t, ok2)
323 assert.NotNil(t, response)
324 assert.Equal(t, LayerTypeGetCurrentDataResponse, response.LayerType())
325 assert.Equal(t, LayerTypeGetCurrentDataResponse, response.CanDecode())
326 assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType())
327 assert.Equal(t, me.MacBridgePortPerformanceMonitoringHistoryDataClassID, response.EntityClass)
328 assert.Equal(t, uint16(1), response.EntityInstance)
329 assert.Equal(t, me.Success, response.Result)
330 assert.Equal(t, uint16(0x8000), response.AttributeMask)
331 assert.Equal(t, uint8(0x10), response.Attributes["IntervalEndTime"])
332
333 // Verify string output for message
334 packetString := packet.String()
335 assert.NotZero(t, len(packetString))
336}
337
338func TestExtendedGetCurrentDataResponseSerialize(t *testing.T) {
339 goodMessage := "035e3c0b0034000100080080000000000010"
340
341 omciLayer := &OMCI{
342 TransactionID: 0x035e,
343 MessageType: GetCurrentDataResponseType,
344 DeviceIdentifier: ExtendedIdent,
345 }
346 request := &GetResponse{
347 MeBasePacket: MeBasePacket{
348 EntityClass: me.MacBridgePortPerformanceMonitoringHistoryDataClassID,
349 EntityInstance: uint16(1),
350 Extended: true,
351 },
352 Result: me.Success,
353 AttributeMask: uint16(0x8000),
354 Attributes: me.AttributeValueMap{
355 "IntervalEndTime": uint8(0x10),
356 },
357 }
358 // Test serialization back to former string
359 var options gopacket.SerializeOptions
360 options.FixLengths = true
361
362 buffer := gopacket.NewSerializeBuffer()
363 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
364 assert.NoError(t, err)
365
366 outgoingPacket := buffer.Bytes()
367 reconstituted := packetToString(outgoingPacket)
368 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
369}