Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package omci_test |
| 19 | |
| 20 | import ( |
| 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 | |
| 29 | func TestMibResetRequestDecode(t *testing.T) { |
| 30 | goodMessage := "01094f0a00020000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeMibResetRequest, omciMsg.NextLayerType()) |
| 46 | assert.Equal(t, MibResetRequestType, omciMsg.MessageType) |
| 47 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 48 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 49 | |
| 50 | msgLayer := packet.Layer(LayerTypeMibResetRequest) |
| 51 | assert.NotNil(t, msgLayer) |
| 52 | |
| 53 | request, ok2 := msgLayer.(*MibResetRequest) |
| 54 | assert.True(t, ok2) |
| 55 | assert.NotNil(t, request) |
| 56 | assert.Equal(t, LayerTypeMibResetRequest, request.LayerType()) |
| 57 | assert.Equal(t, LayerTypeMibResetRequest, request.CanDecode()) |
| 58 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 59 | assert.Equal(t, me.OnuDataClassID, request.EntityClass) |
| 60 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 61 | |
| 62 | // Verify string output for message |
| 63 | packetString := packet.String() |
| 64 | assert.NotZero(t, len(packetString)) |
| 65 | } |
| 66 | |
| 67 | func TestMibResetRequestSerialize(t *testing.T) { |
| 68 | goodMessage := "01094f0a00020000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 69 | |
| 70 | omciLayer := &OMCI{ |
| 71 | TransactionID: 0x0109, |
| 72 | MessageType: MibResetRequestType, |
| 73 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 74 | // Length: 0x28, // Optional, defaults to 40 octets |
| 75 | } |
| 76 | request := &MibResetRequest{ |
| 77 | MeBasePacket: MeBasePacket{ |
| 78 | EntityClass: me.OnuDataClassID, |
| 79 | // Default Instance ID is 0 |
| 80 | }, |
| 81 | } |
| 82 | // Test serialization back to former string |
| 83 | var options gopacket.SerializeOptions |
| 84 | options.FixLengths = true |
| 85 | |
| 86 | buffer := gopacket.NewSerializeBuffer() |
| 87 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 88 | assert.NoError(t, err) |
| 89 | |
| 90 | outgoingPacket := buffer.Bytes() |
| 91 | reconstituted := packetToString(outgoingPacket) |
| 92 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 93 | } |
| 94 | |
| 95 | func TestMibResetRequestZeroTICSerialize(t *testing.T) { |
| 96 | omciLayer := &OMCI{ |
| 97 | TransactionID: 0x0, |
| 98 | MessageType: MibResetRequestType, |
| 99 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 100 | // Length: 0x28, // Optional, defaults to 40 octets |
| 101 | } |
| 102 | request := &MibResetRequest{ |
| 103 | MeBasePacket: MeBasePacket{ |
| 104 | EntityClass: me.OnuDataClassID, |
| 105 | // Default Instance ID is 0 |
| 106 | }, |
| 107 | } |
| 108 | // Test serialization back to former string |
| 109 | var options gopacket.SerializeOptions |
| 110 | options.FixLengths = true |
| 111 | |
| 112 | buffer := gopacket.NewSerializeBuffer() |
| 113 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 114 | assert.Error(t, err) |
| 115 | } |
| 116 | |
| 117 | func TestMibResetResponseDecode(t *testing.T) { |
| 118 | goodMessage := "00012F0A00020000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 119 | data, err := stringToPacket(goodMessage) |
| 120 | assert.NoError(t, err) |
| 121 | |
| 122 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 123 | assert.NotNil(t, packet) |
| 124 | |
| 125 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 126 | assert.NotNil(t, omciLayer) |
| 127 | |
| 128 | omciMsg, ok := omciLayer.(*OMCI) |
| 129 | assert.True(t, ok) |
| 130 | assert.NotNil(t, omciMsg) |
| 131 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 132 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 133 | assert.Equal(t, LayerTypeMibResetResponse, omciMsg.NextLayerType()) |
| 134 | assert.Equal(t, MibResetResponseType, omciMsg.MessageType) |
| 135 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 136 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 137 | |
| 138 | msgLayer := packet.Layer(LayerTypeMibResetResponse) |
| 139 | |
| 140 | assert.NotNil(t, msgLayer) |
| 141 | |
| 142 | response, ok2 := msgLayer.(*MibResetResponse) |
| 143 | assert.True(t, ok2) |
| 144 | assert.NotNil(t, response) |
| 145 | assert.Equal(t, LayerTypeMibResetResponse, response.LayerType()) |
| 146 | assert.Equal(t, LayerTypeMibResetResponse, response.CanDecode()) |
| 147 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 148 | |
| 149 | // Verify string output for message |
| 150 | packetString := packet.String() |
| 151 | assert.NotZero(t, len(packetString)) |
| 152 | } |
| 153 | |
| 154 | func TestMibResetResponseSerialize(t *testing.T) { |
| 155 | goodMessage := "00012F0A00020000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 156 | |
| 157 | omciLayer := &OMCI{ |
| 158 | TransactionID: 0x01, |
| 159 | MessageType: MibResetResponseType, |
| 160 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 161 | // Length: 0x28, // Optional, defaults to 40 octets |
| 162 | } |
| 163 | request := &MibResetResponse{ |
| 164 | MeBasePacket: MeBasePacket{ |
| 165 | EntityClass: me.OnuDataClassID, |
| 166 | // Default Instance ID is 0 |
| 167 | }, |
| 168 | } |
| 169 | // Test serialization back to former string |
| 170 | var options gopacket.SerializeOptions |
| 171 | options.FixLengths = true |
| 172 | |
| 173 | buffer := gopacket.NewSerializeBuffer() |
| 174 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 175 | assert.NoError(t, err) |
| 176 | |
| 177 | outgoingPacket := buffer.Bytes() |
| 178 | reconstituted := packetToString(outgoingPacket) |
| 179 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 180 | } |
| 181 | |
| 182 | func TestMibResetResponseZeroTICSerialize(t *testing.T) { |
| 183 | omciLayer := &OMCI{ |
| 184 | TransactionID: 0x0, |
| 185 | MessageType: MibResetResponseType, |
| 186 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 187 | // Length: 0x28, // Optional, defaults to 40 octets |
| 188 | } |
| 189 | request := &MibResetResponse{ |
| 190 | MeBasePacket: MeBasePacket{ |
| 191 | EntityClass: me.OnuDataClassID, |
| 192 | // Default Instance ID is 0 |
| 193 | }, |
| 194 | } |
| 195 | // Test serialization back to former string |
| 196 | var options gopacket.SerializeOptions |
| 197 | options.FixLengths = true |
| 198 | |
| 199 | buffer := gopacket.NewSerializeBuffer() |
| 200 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 201 | assert.Error(t, err) |
| 202 | } |
| 203 | |
| 204 | func TestExtendedMibResetRequestDecode(t *testing.T) { |
| 205 | goodMessage := "01094f0b000200000000" |
| 206 | data, err := stringToPacket(goodMessage) |
| 207 | assert.NoError(t, err) |
| 208 | |
| 209 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 210 | assert.NotNil(t, packet) |
| 211 | |
| 212 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 213 | assert.NotNil(t, omciLayer) |
| 214 | |
| 215 | omciMsg, ok := omciLayer.(*OMCI) |
| 216 | assert.True(t, ok) |
| 217 | assert.NotNil(t, omciMsg) |
| 218 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 219 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 220 | assert.Equal(t, LayerTypeMibResetRequest, omciMsg.NextLayerType()) |
| 221 | assert.Equal(t, MibResetRequestType, omciMsg.MessageType) |
| 222 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 223 | assert.Equal(t, uint16(0), omciMsg.Length) |
| 224 | |
| 225 | msgLayer := packet.Layer(LayerTypeMibResetRequest) |
| 226 | assert.NotNil(t, msgLayer) |
| 227 | |
| 228 | request, ok2 := msgLayer.(*MibResetRequest) |
| 229 | assert.True(t, ok2) |
| 230 | assert.NotNil(t, request) |
| 231 | assert.Equal(t, LayerTypeMibResetRequest, request.LayerType()) |
| 232 | assert.Equal(t, LayerTypeMibResetRequest, request.CanDecode()) |
| 233 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 234 | assert.Equal(t, me.OnuDataClassID, request.EntityClass) |
| 235 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 236 | |
| 237 | // Verify string output for message |
| 238 | packetString := packet.String() |
| 239 | assert.NotZero(t, len(packetString)) |
| 240 | } |
| 241 | |
| 242 | func TestExtendedMibResetRequestSerialize(t *testing.T) { |
| 243 | goodMessage := "01094f0b000200000000" |
| 244 | |
| 245 | omciLayer := &OMCI{ |
| 246 | TransactionID: 0x0109, |
| 247 | MessageType: MibResetRequestType, |
| 248 | DeviceIdentifier: ExtendedIdent, |
| 249 | } |
| 250 | request := &MibResetRequest{ |
| 251 | MeBasePacket: MeBasePacket{ |
| 252 | EntityClass: me.OnuDataClassID, |
| 253 | Extended: true, |
| 254 | }, |
| 255 | } |
| 256 | // Test serialization back to former string |
| 257 | var options gopacket.SerializeOptions |
| 258 | options.FixLengths = true |
| 259 | |
| 260 | buffer := gopacket.NewSerializeBuffer() |
| 261 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 262 | assert.NoError(t, err) |
| 263 | |
| 264 | outgoingPacket := buffer.Bytes() |
| 265 | reconstituted := packetToString(outgoingPacket) |
| 266 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 267 | } |
| 268 | |
| 269 | func TestExtendedMibResetResponseDecode(t *testing.T) { |
| 270 | goodMessage := "00012F0B00020000000106" |
| 271 | data, err := stringToPacket(goodMessage) |
| 272 | assert.NoError(t, err) |
| 273 | |
| 274 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 275 | assert.NotNil(t, packet) |
| 276 | |
| 277 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 278 | assert.NotNil(t, omciLayer) |
| 279 | |
| 280 | omciMsg, ok := omciLayer.(*OMCI) |
| 281 | assert.True(t, ok) |
| 282 | assert.NotNil(t, omciMsg) |
| 283 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 284 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 285 | assert.Equal(t, LayerTypeMibResetResponse, omciMsg.NextLayerType()) |
| 286 | assert.Equal(t, MibResetResponseType, omciMsg.MessageType) |
| 287 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 288 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 289 | |
| 290 | msgLayer := packet.Layer(LayerTypeMibResetResponse) |
| 291 | |
| 292 | assert.NotNil(t, msgLayer) |
| 293 | |
| 294 | response, ok2 := msgLayer.(*MibResetResponse) |
| 295 | assert.True(t, ok2) |
| 296 | assert.NotNil(t, response) |
| 297 | assert.Equal(t, me.DeviceBusy, response.Result) |
| 298 | |
| 299 | // Verify string output for message |
| 300 | packetString := packet.String() |
| 301 | assert.NotZero(t, len(packetString)) |
| 302 | } |
| 303 | |
| 304 | func TestExtendedMibResetResponseSerialize(t *testing.T) { |
| 305 | goodMessage := "00012F0B00020000000106" |
| 306 | |
| 307 | omciLayer := &OMCI{ |
| 308 | TransactionID: 0x01, |
| 309 | MessageType: MibResetResponseType, |
| 310 | DeviceIdentifier: ExtendedIdent, |
| 311 | } |
| 312 | request := &MibResetResponse{ |
| 313 | MeBasePacket: MeBasePacket{ |
| 314 | EntityClass: me.OnuDataClassID, |
| 315 | Extended: true, |
| 316 | }, |
| 317 | Result: me.DeviceBusy, |
| 318 | } |
| 319 | // Test serialization back to former string |
| 320 | var options gopacket.SerializeOptions |
| 321 | options.FixLengths = true |
| 322 | |
| 323 | buffer := gopacket.NewSerializeBuffer() |
| 324 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 325 | assert.NoError(t, err) |
| 326 | |
| 327 | outgoingPacket := buffer.Bytes() |
| 328 | reconstituted := packetToString(outgoingPacket) |
| 329 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 330 | } |
| 331 | |
| 332 | // MibResetRequestTest tests decode/encode of a MIB Reset Request |
| 333 | func TestMibResetRequestMessage(t *testing.T) { |
| 334 | mibResetRequest := "00014F0A000200000000000000000000" + |
| 335 | "00000000000000000000000000000000" + |
| 336 | "000000000000000000000028" |
| 337 | |
| 338 | data, err := stringToPacket(mibResetRequest) |
| 339 | assert.NoError(t, err) |
| 340 | |
| 341 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 342 | |
| 343 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 344 | assert.NotNil(t, packet) |
| 345 | |
| 346 | omciMsg, ok := omciLayer.(*OMCI) |
| 347 | assert.True(t, ok) |
| 348 | assert.Equal(t, uint16(1), omciMsg.TransactionID) |
| 349 | assert.Equal(t, MibResetRequestType, omciMsg.MessageType) |
| 350 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 351 | |
| 352 | msgLayer := packet.Layer(LayerTypeMibResetRequest) |
| 353 | assert.NotNil(t, msgLayer) |
| 354 | |
| 355 | omciMsg2, ok2 := msgLayer.(*MibResetRequest) |
| 356 | assert.True(t, ok2) |
| 357 | assert.Equal(t, me.OnuDataClassID, omciMsg2.EntityClass) |
| 358 | assert.Equal(t, uint16(0), omciMsg2.EntityInstance) |
| 359 | |
| 360 | // Test serialization back to former string |
| 361 | var options gopacket.SerializeOptions |
| 362 | options.FixLengths = true |
| 363 | |
| 364 | buffer := gopacket.NewSerializeBuffer() |
| 365 | err = gopacket.SerializeLayers(buffer, options, omciMsg, omciMsg2) |
| 366 | assert.NoError(t, err) |
| 367 | |
| 368 | outgoingPacket := buffer.Bytes() |
| 369 | reconstituted := packetToString(outgoingPacket) |
| 370 | assert.Equal(t, strings.ToLower(mibResetRequest), reconstituted) |
| 371 | } |