blob: f205ab06cf42421d69bc0b76593d25df5009a83c [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 TestGetNextRequestDecode(t *testing.T) {
30 goodMessage := "285e5a0a00ab0202040000010000000000000000000000000000000000000000000000000000000000000028"
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, LayerTypeGetNextRequest, omciMsg.NextLayerType())
46 assert.Equal(t, uint16(0x285e), omciMsg.TransactionID)
47 assert.Equal(t, GetNextRequestType, omciMsg.MessageType)
48 assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier)
49 assert.Equal(t, uint16(40), omciMsg.Length)
50
51 msgLayer := packet.Layer(LayerTypeGetNextRequest)
52 assert.NotNil(t, msgLayer)
53
54 request, ok2 := msgLayer.(*GetNextRequest)
55 assert.True(t, ok2)
56 assert.NotNil(t, request)
57 assert.Equal(t, LayerTypeGetNextRequest, request.LayerType())
58 assert.Equal(t, LayerTypeGetNextRequest, request.CanDecode())
59 assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
60 assert.Equal(t, me.ExtendedVlanTaggingOperationConfigurationDataClassID, request.EntityClass)
61 assert.Equal(t, uint16(0x0202), request.EntityInstance)
62 assert.Equal(t, uint16(0x0400), request.AttributeMask)
63 assert.Equal(t, uint16(1), request.SequenceNumber)
64
65 // Verify string output for message
66 packetString := packet.String()
67 assert.NotZero(t, len(packetString))
68}
69
70func TestGetNextRequestDecodeExtended(t *testing.T) {
71 goodMessage := "285e5a0b00ab0202000404000001"
72 data, err := stringToPacket(goodMessage)
73 assert.NoError(t, err)
74
75 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
76 assert.NotNil(t, packet)
77
78 omciLayer := packet.Layer(LayerTypeOMCI)
79 assert.NotNil(t, omciLayer)
80
81 omciMsg, ok := omciLayer.(*OMCI)
82 assert.True(t, ok)
83 assert.NotNil(t, omciMsg)
84 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
85 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
86 assert.Equal(t, LayerTypeGetNextRequest, omciMsg.NextLayerType())
87 assert.Equal(t, uint16(0x285e), omciMsg.TransactionID)
88 assert.Equal(t, GetNextRequestType, omciMsg.MessageType)
89 assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier)
90 assert.Equal(t, uint16(4), omciMsg.Length)
91
92 msgLayer := packet.Layer(LayerTypeGetNextRequest)
93 assert.NotNil(t, msgLayer)
94
95 request, ok2 := msgLayer.(*GetNextRequest)
96 assert.True(t, ok2)
97 assert.NotNil(t, request)
98 assert.Equal(t, LayerTypeGetNextRequest, request.LayerType())
99 assert.Equal(t, LayerTypeGetNextRequest, request.CanDecode())
100 assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType())
101 assert.Equal(t, me.ExtendedVlanTaggingOperationConfigurationDataClassID, request.EntityClass)
102 assert.Equal(t, uint16(0x0202), request.EntityInstance)
103 assert.Equal(t, uint16(0x0400), request.AttributeMask)
104 assert.Equal(t, uint16(1), request.SequenceNumber)
105
106 // Verify string output for message
107 packetString := packet.String()
108 assert.NotZero(t, len(packetString))
109}
110
111func TestGetNextRequestSerialize(t *testing.T) {
112 goodMessage := "285e5a0a00ab0202040000010000000000000000000000000000000000000000000000000000000000000028"
113
114 omciLayer := &OMCI{
115 TransactionID: 0x285e,
116 MessageType: GetNextRequestType,
117 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
118 // Length: 0x28, // Optional, defaults to 40 octets
119 }
120 request := &GetNextRequest{
121 MeBasePacket: MeBasePacket{
122 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
123 EntityInstance: uint16(0x0202),
124 },
125 AttributeMask: uint16(0x0400),
126 SequenceNumber: uint16(1),
127 }
128 // Test serialization back to former string
129 var options gopacket.SerializeOptions
130 options.FixLengths = true
131
132 buffer := gopacket.NewSerializeBuffer()
133 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
134 assert.NoError(t, err)
135
136 outgoingPacket := buffer.Bytes()
137 reconstituted := packetToString(outgoingPacket)
138 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
139}
140
141func TestGetNextRequestZeroTICSerialize(t *testing.T) {
142 omciLayer := &OMCI{
143 TransactionID: 0x0,
144 MessageType: GetNextRequestType,
145 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
146 // Length: 0x28, // Optional, defaults to 40 octets
147 }
148 request := &GetNextRequest{
149 MeBasePacket: MeBasePacket{
150 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
151 EntityInstance: uint16(0x0202),
152 },
153 AttributeMask: uint16(0x0400),
154 SequenceNumber: uint16(1),
155 }
156 // Test serialization back to former string
157 var options gopacket.SerializeOptions
158 options.FixLengths = true
159
160 buffer := gopacket.NewSerializeBuffer()
161 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
162 assert.Error(t, err)
163}
164
165func TestGetNextRequestSerializeExtended(t *testing.T) {
166 goodMessage := "285e5a0b00ab0202000404000001"
167
168 omciLayer := &OMCI{
169 TransactionID: 0x285e,
170 MessageType: GetNextRequestType,
171 DeviceIdentifier: ExtendedIdent,
172 }
173 request := &GetNextRequest{
174 MeBasePacket: MeBasePacket{
175 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
176 EntityInstance: uint16(0x0202),
177 Extended: true,
178 },
179 AttributeMask: uint16(0x0400),
180 SequenceNumber: uint16(1),
181 }
182 // Test serialization back to former string
183 var options gopacket.SerializeOptions
184 options.FixLengths = true
185
186 buffer := gopacket.NewSerializeBuffer()
187 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
188 assert.NoError(t, err)
189
190 outgoingPacket := buffer.Bytes()
191 reconstituted := packetToString(outgoingPacket)
192 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
193}
194
195func TestGetNextResponseDecode(t *testing.T) {
196 goodMessage := "285e3a0a00ab0202000400080334000000000000000000000000000000000000000000000000000000000028"
197
198 data, err := stringToPacket(goodMessage)
199 assert.NoError(t, err)
200
201 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
202 assert.NotNil(t, packet)
203
204 omciLayer := packet.Layer(LayerTypeOMCI)
205 assert.NotNil(t, omciLayer)
206
207 omciMsg, ok := omciLayer.(*OMCI)
208 assert.True(t, ok)
209 assert.NotNil(t, omciMsg)
210 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
211 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
212 assert.Equal(t, LayerTypeGetNextResponse, omciMsg.NextLayerType())
213 assert.Equal(t, uint16(0x285e), omciMsg.TransactionID)
214 assert.Equal(t, GetNextResponseType, omciMsg.MessageType)
215 assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier)
216 assert.Equal(t, uint16(40), omciMsg.Length)
217
218 msgLayer := packet.Layer(LayerTypeGetNextResponse)
219 assert.NotNil(t, msgLayer)
220
221 vlanOpTable := []byte{0x08, 0x03, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
223
224 response, ok2 := msgLayer.(*GetNextResponse)
225 assert.True(t, ok2)
226 assert.NotNil(t, response)
227 assert.Equal(t, LayerTypeGetNextResponse, response.LayerType())
228 assert.Equal(t, LayerTypeGetNextResponse, response.CanDecode())
229 assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType())
230 assert.Equal(t, me.ExtendedVlanTaggingOperationConfigurationDataClassID, response.EntityClass)
231 assert.Equal(t, uint16(0x0202), response.EntityInstance)
232 assert.Equal(t, me.Success, response.Result)
233 assert.Equal(t, uint16(0x0400), response.AttributeMask)
234
235 // For GetNextResponse frames, caller is responsible for trimming last packet to remaining
236 // size
237 expectedOctets := 16
238 value := response.Attributes["ReceivedFrameVlanTaggingOperationTable"]
239 assert.Equal(t, value.([]byte)[:expectedOctets], vlanOpTable)
240
241 // Verify string output for message
242 packetString := packet.String()
243 assert.NotZero(t, len(packetString))
244}
245
246func TestGetNextResponseDecodeExtended(t *testing.T) {
247 goodMessage := "285e3a0b00ab0202001300040008033400000000000000000000000000"
248 data, err := stringToPacket(goodMessage)
249 assert.NoError(t, err)
250
251 packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
252 assert.NotNil(t, packet)
253
254 omciLayer := packet.Layer(LayerTypeOMCI)
255 assert.NotNil(t, omciLayer)
256
257 omciMsg, ok := omciLayer.(*OMCI)
258 assert.True(t, ok)
259 assert.NotNil(t, omciMsg)
260 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
261 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
262 assert.Equal(t, LayerTypeGetNextResponse, omciMsg.NextLayerType())
263 assert.Equal(t, uint16(0x285e), omciMsg.TransactionID)
264 assert.Equal(t, GetNextResponseType, omciMsg.MessageType)
265 assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier)
266 assert.Equal(t, uint16(19), omciMsg.Length)
267
268 msgLayer := packet.Layer(LayerTypeGetNextResponse)
269 assert.NotNil(t, msgLayer)
270
271 vlanOpTable := []byte{0x08, 0x03, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
273
274 response, ok2 := msgLayer.(*GetNextResponse)
275 assert.True(t, ok2)
276 assert.NotNil(t, response)
277 assert.Equal(t, LayerTypeGetNextResponse, response.LayerType())
278 assert.Equal(t, LayerTypeGetNextResponse, response.CanDecode())
279 assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType())
280 assert.Equal(t, me.ExtendedVlanTaggingOperationConfigurationDataClassID, response.EntityClass)
281 assert.Equal(t, uint16(0x0202), response.EntityInstance)
282 assert.Equal(t, me.Success, response.Result)
283 assert.Equal(t, uint16(0x0400), response.AttributeMask)
284
285 // For GetNextResponse frames, caller is responsible for trimming last packet to remaining
286 // size
287 expectedOctets := 16
288 value := response.Attributes["ReceivedFrameVlanTaggingOperationTable"]
289 assert.Equal(t, value.([]byte)[:expectedOctets], vlanOpTable)
290
291 // Verify string output for message
292 packetString := packet.String()
293 assert.NotZero(t, len(packetString))
294}
295
296func TestGetNextResponseSerialize(t *testing.T) {
297 goodMessage := "285e3a0a00ab0202000400080334000000000000000000000000000000000000000000000000000000000028"
298
299 omciLayer := &OMCI{
300 TransactionID: 0x285e,
301 MessageType: GetNextResponseType,
302 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
303 // Length: 0x28, // Optional, defaults to 40 octets
304 }
305 vlanOpTable := []byte{0x08, 0x03, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
307
308 request := &GetNextResponse{
309 MeBasePacket: MeBasePacket{
310 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
311 EntityInstance: uint16(0x0202),
312 },
313 Result: me.Success,
314 AttributeMask: uint16(0x0400),
315 Attributes: me.AttributeValueMap{"ReceivedFrameVlanTaggingOperationTable": vlanOpTable},
316 }
317 // Test serialization back to former string
318 var options gopacket.SerializeOptions
319 options.FixLengths = true
320
321 buffer := gopacket.NewSerializeBuffer()
322 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
323 assert.NoError(t, err)
324
325 outgoingPacket := buffer.Bytes()
326 reconstituted := packetToString(outgoingPacket)
327 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
328}
329
330func TestGetNextResponseZeroTICSerialize(t *testing.T) {
331 omciLayer := &OMCI{
332 TransactionID: 0x0,
333 MessageType: GetNextResponseType,
334 // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline
335 // Length: 0x28, // Optional, defaults to 40 octets
336 }
337 vlanOpTable := []byte{0x08, 0x03, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00,
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
339
340 request := &GetNextResponse{
341 MeBasePacket: MeBasePacket{
342 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
343 EntityInstance: uint16(0x0202),
344 },
345 Result: me.Success,
346 AttributeMask: uint16(0x0400),
347 Attributes: me.AttributeValueMap{"ReceivedFrameVlanTaggingOperationTable": vlanOpTable},
348 }
349 // Test serialization back to former string
350 var options gopacket.SerializeOptions
351 options.FixLengths = true
352
353 buffer := gopacket.NewSerializeBuffer()
354 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
355 assert.Error(t, err)
356}
357
358func TestGetNextResponseSerializeExtended(t *testing.T) {
359 goodMessage := "285e3a0b00ab0202001300040008033400000000000000000000000000"
360
361 omciLayer := &OMCI{
362 TransactionID: 0x285e,
363 MessageType: GetNextResponseType,
364 DeviceIdentifier: ExtendedIdent,
365 }
366 vlanOpTable := []byte{0x08, 0x03, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00,
367 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
368
369 request := &GetNextResponse{
370 MeBasePacket: MeBasePacket{
371 EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID,
372 EntityInstance: uint16(0x0202),
373 Extended: true,
374 },
375 Result: me.Success,
376 AttributeMask: uint16(0x0400),
377 Attributes: me.AttributeValueMap{"ReceivedFrameVlanTaggingOperationTable": vlanOpTable},
378 }
379 // Test serialization back to former string
380 var options gopacket.SerializeOptions
381 options.FixLengths = true
382
383 buffer := gopacket.NewSerializeBuffer()
384 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
385 assert.NoError(t, err)
386
387 outgoingPacket := buffer.Bytes()
388 reconstituted := packetToString(outgoingPacket)
389 assert.Equal(t, strings.ToLower(goodMessage), reconstituted)
390}