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 TestSetRequestDecode(t *testing.T) { |
| 30 | goodMessage := "0107480a01000000020000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeSetRequest, omciMsg.NextLayerType()) |
| 46 | assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| 47 | assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| 48 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 49 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 50 | |
| 51 | msgLayer := packet.Layer(LayerTypeSetRequest) |
| 52 | assert.NotNil(t, msgLayer) |
| 53 | |
| 54 | request, ok2 := msgLayer.(*SetRequest) |
| 55 | assert.True(t, ok2) |
| 56 | assert.NotNil(t, request) |
| 57 | assert.Equal(t, LayerTypeSetRequest, request.LayerType()) |
| 58 | assert.Equal(t, LayerTypeSetRequest, request.CanDecode()) |
| 59 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 60 | |
| 61 | // Verify string output for message |
| 62 | packetString := packet.String() |
| 63 | assert.NotZero(t, len(packetString)) |
| 64 | } |
| 65 | |
| 66 | func TestSetRequestSerialize(t *testing.T) { |
| 67 | goodMessage := "0107480a01000000020001000000000000000000000000000000000000000000000000000000000000000028" |
| 68 | |
| 69 | omciLayer := &OMCI{ |
| 70 | TransactionID: 0x0107, |
| 71 | MessageType: SetRequestType, |
| 72 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 73 | // Length: 0x28, // Optional, defaults to 40 octets |
| 74 | } |
| 75 | request := &SetRequest{ |
| 76 | MeBasePacket: MeBasePacket{ |
| 77 | EntityClass: me.OnuGClassID, |
| 78 | EntityInstance: uint16(0), |
| 79 | }, |
| 80 | AttributeMask: uint16(0x200), |
| 81 | Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| 82 | } |
| 83 | // Test serialization back to former string |
| 84 | var options gopacket.SerializeOptions |
| 85 | options.FixLengths = true |
| 86 | |
| 87 | buffer := gopacket.NewSerializeBuffer() |
| 88 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 89 | assert.NoError(t, err) |
| 90 | |
| 91 | outgoingPacket := buffer.Bytes() |
| 92 | reconstituted := packetToString(outgoingPacket) |
| 93 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 94 | } |
| 95 | |
| 96 | func TestSetRequestZeroTICSerialize(t *testing.T) { |
| 97 | omciLayer := &OMCI{ |
| 98 | TransactionID: 0x0, |
| 99 | MessageType: SetRequestType, |
| 100 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 101 | // Length: 0x28, // Optional, defaults to 40 octets |
| 102 | } |
| 103 | request := &SetRequest{ |
| 104 | MeBasePacket: MeBasePacket{ |
| 105 | EntityClass: me.OnuGClassID, |
| 106 | EntityInstance: uint16(0), |
| 107 | }, |
| 108 | AttributeMask: uint16(0x200), |
| 109 | Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| 110 | } |
| 111 | // Test serialization back to former string |
| 112 | var options gopacket.SerializeOptions |
| 113 | options.FixLengths = true |
| 114 | |
| 115 | buffer := gopacket.NewSerializeBuffer() |
| 116 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 117 | assert.Error(t, err) |
| 118 | } |
| 119 | |
| 120 | func TestSetResponseDecode(t *testing.T) { |
| 121 | goodMessage := "0107280a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 122 | data, err := stringToPacket(goodMessage) |
| 123 | assert.NoError(t, err) |
| 124 | |
| 125 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 126 | assert.NotNil(t, packet) |
| 127 | |
| 128 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 129 | assert.NotNil(t, omciLayer) |
| 130 | |
| 131 | omciMsg, ok := omciLayer.(*OMCI) |
| 132 | assert.True(t, ok) |
| 133 | assert.NotNil(t, omciMsg) |
| 134 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 135 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 136 | assert.Equal(t, LayerTypeSetResponse, omciMsg.NextLayerType()) |
| 137 | assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| 138 | assert.Equal(t, SetResponseType, omciMsg.MessageType) |
| 139 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 140 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 141 | |
| 142 | msgLayer := packet.Layer(LayerTypeSetResponse) |
| 143 | assert.NotNil(t, msgLayer) |
| 144 | |
| 145 | response, ok2 := msgLayer.(*SetResponse) |
| 146 | assert.True(t, ok2) |
| 147 | assert.NotNil(t, response) |
| 148 | assert.Equal(t, LayerTypeSetResponse, response.LayerType()) |
| 149 | assert.Equal(t, LayerTypeSetResponse, response.CanDecode()) |
| 150 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 151 | |
| 152 | // Verify string output for message |
| 153 | packetString := packet.String() |
| 154 | assert.NotZero(t, len(packetString)) |
| 155 | } |
| 156 | |
| 157 | func TestSetResponseSerialize(t *testing.T) { |
| 158 | goodMessage := "0107280a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 159 | |
| 160 | omciLayer := &OMCI{ |
| 161 | TransactionID: 0x0107, |
| 162 | MessageType: SetResponseType, |
| 163 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 164 | // Length: 0x28, // Optional, defaults to 40 octets |
| 165 | } |
| 166 | request := &SetResponse{ |
| 167 | MeBasePacket: MeBasePacket{ |
| 168 | EntityClass: me.OnuGClassID, |
| 169 | EntityInstance: uint16(0), |
| 170 | }, |
| 171 | Result: me.Success, |
| 172 | } |
| 173 | // Test serialization back to former string |
| 174 | var options gopacket.SerializeOptions |
| 175 | options.FixLengths = true |
| 176 | |
| 177 | buffer := gopacket.NewSerializeBuffer() |
| 178 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 179 | assert.NoError(t, err) |
| 180 | |
| 181 | outgoingPacket := buffer.Bytes() |
| 182 | reconstituted := packetToString(outgoingPacket) |
| 183 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 184 | |
| 185 | } |
| 186 | |
| 187 | func TestSetResponseZeroTICSerialize(t *testing.T) { |
| 188 | omciLayer := &OMCI{ |
| 189 | TransactionID: 0x0, |
| 190 | MessageType: SetResponseType, |
| 191 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 192 | // Length: 0x28, // Optional, defaults to 40 octets |
| 193 | } |
| 194 | request := &SetResponse{ |
| 195 | MeBasePacket: MeBasePacket{ |
| 196 | EntityClass: me.OnuGClassID, |
| 197 | EntityInstance: uint16(0), |
| 198 | }, |
| 199 | Result: me.Success, |
| 200 | } |
| 201 | // Test serialization back to former string |
| 202 | var options gopacket.SerializeOptions |
| 203 | options.FixLengths = true |
| 204 | |
| 205 | buffer := gopacket.NewSerializeBuffer() |
| 206 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 207 | assert.Error(t, err) |
| 208 | |
| 209 | } |
| 210 | |
| 211 | func TestExtendedSetRequestDecode(t *testing.T) { |
| 212 | goodMessage := "0107480b010000000003020001" |
| 213 | data, err := stringToPacket(goodMessage) |
| 214 | assert.NoError(t, err) |
| 215 | |
| 216 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 217 | assert.NotNil(t, packet) |
| 218 | |
| 219 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 220 | assert.NotNil(t, omciLayer) |
| 221 | |
| 222 | omciMsg, ok := omciLayer.(*OMCI) |
| 223 | assert.True(t, ok) |
| 224 | assert.NotNil(t, omciMsg) |
| 225 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 226 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 227 | assert.Equal(t, LayerTypeSetRequest, omciMsg.NextLayerType()) |
| 228 | assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| 229 | assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| 230 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 231 | assert.Equal(t, uint16(3), omciMsg.Length) |
| 232 | |
| 233 | msgLayer := packet.Layer(LayerTypeSetRequest) |
| 234 | assert.NotNil(t, msgLayer) |
| 235 | |
| 236 | request, ok2 := msgLayer.(*SetRequest) |
| 237 | assert.True(t, ok2) |
| 238 | assert.NotNil(t, request) |
| 239 | assert.Equal(t, LayerTypeSetRequest, request.LayerType()) |
| 240 | assert.Equal(t, LayerTypeSetRequest, request.CanDecode()) |
| 241 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 242 | assert.Equal(t, uint16(0x0200), request.AttributeMask) |
| 243 | |
| 244 | // Verify string output for message |
| 245 | packetString := packet.String() |
| 246 | assert.NotZero(t, len(packetString)) |
| 247 | } |
| 248 | |
| 249 | func TestExtendedSetRequestSerialize(t *testing.T) { |
| 250 | goodMessage := "0107480b010000000003020001" |
| 251 | |
| 252 | omciLayer := &OMCI{ |
| 253 | TransactionID: 0x0107, |
| 254 | MessageType: SetRequestType, |
| 255 | DeviceIdentifier: ExtendedIdent, |
| 256 | } |
| 257 | request := &SetRequest{ |
| 258 | MeBasePacket: MeBasePacket{ |
| 259 | EntityClass: me.OnuGClassID, |
| 260 | EntityInstance: uint16(0), |
| 261 | Extended: true, |
| 262 | }, |
| 263 | AttributeMask: uint16(0x200), |
| 264 | Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| 265 | } |
| 266 | // Test serialization back to former string |
| 267 | var options gopacket.SerializeOptions |
| 268 | options.FixLengths = true |
| 269 | |
| 270 | buffer := gopacket.NewSerializeBuffer() |
| 271 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 272 | assert.NoError(t, err) |
| 273 | |
| 274 | outgoingPacket := buffer.Bytes() |
| 275 | reconstituted := packetToString(outgoingPacket) |
| 276 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 277 | } |
| 278 | |
| 279 | func TestExtendedSetResponseDecode(t *testing.T) { |
| 280 | goodMessage := "0107280b01000000000100" |
| 281 | data, err := stringToPacket(goodMessage) |
| 282 | assert.NoError(t, err) |
| 283 | // TODO: Also test result == 9 decode |
| 284 | |
| 285 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 286 | assert.NotNil(t, packet) |
| 287 | |
| 288 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 289 | assert.NotNil(t, omciLayer) |
| 290 | |
| 291 | omciMsg, ok := omciLayer.(*OMCI) |
| 292 | assert.True(t, ok) |
| 293 | assert.NotNil(t, omciMsg) |
| 294 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 295 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 296 | assert.Equal(t, LayerTypeSetResponse, omciMsg.NextLayerType()) |
| 297 | assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| 298 | assert.Equal(t, SetResponseType, omciMsg.MessageType) |
| 299 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 300 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 301 | |
| 302 | msgLayer := packet.Layer(LayerTypeSetResponse) |
| 303 | assert.NotNil(t, msgLayer) |
| 304 | |
| 305 | response, ok2 := msgLayer.(*SetResponse) |
| 306 | assert.True(t, ok2) |
| 307 | assert.NotNil(t, response) |
| 308 | assert.Equal(t, LayerTypeSetResponse, response.LayerType()) |
| 309 | assert.Equal(t, LayerTypeSetResponse, response.CanDecode()) |
| 310 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 311 | assert.Equal(t, me.Success, response.Result) |
| 312 | |
| 313 | // Verify string output for message |
| 314 | packetString := packet.String() |
| 315 | assert.NotZero(t, len(packetString)) |
| 316 | } |
| 317 | |
| 318 | func TestExtendedSetResponseSerialize(t *testing.T) { |
| 319 | goodMessage := "0107280b01000000000103" |
| 320 | |
| 321 | omciLayer := &OMCI{ |
| 322 | TransactionID: 0x0107, |
| 323 | MessageType: SetResponseType, |
| 324 | DeviceIdentifier: ExtendedIdent, |
| 325 | } |
| 326 | request := &SetResponse{ |
| 327 | MeBasePacket: MeBasePacket{ |
| 328 | EntityClass: me.OnuGClassID, |
| 329 | EntityInstance: uint16(0), |
| 330 | Extended: true, |
| 331 | }, |
| 332 | Result: me.ParameterError, |
| 333 | } |
| 334 | // Test serialization back to former string |
| 335 | var options gopacket.SerializeOptions |
| 336 | options.FixLengths = true |
| 337 | |
| 338 | buffer := gopacket.NewSerializeBuffer() |
| 339 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 340 | assert.NoError(t, err) |
| 341 | |
| 342 | outgoingPacket := buffer.Bytes() |
| 343 | reconstituted := packetToString(outgoingPacket) |
| 344 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 345 | |
| 346 | } |
| 347 | |
| 348 | func TestSetResponseTableFailedAttributesDecode(t *testing.T) { |
| 349 | // This is a SET Response with failed and unsupported attributes |
| 350 | // TODO:Implement (also implement for Extended message set) |
| 351 | } |
| 352 | |
| 353 | func TestSetResponseTableFailedAttributesSerialize(t *testing.T) { |
| 354 | // This is a SET Response with failed and unsupported attributes |
| 355 | // TODO:Implement (also implement for Extended message set) |
| 356 | } |
| 357 | |
| 358 | func TestSetTCont(t *testing.T) { |
| 359 | setTCont := "0003480A010680008000040000000000" + |
| 360 | "00000000000000000000000000000000" + |
| 361 | "000000000000000000000028" |
| 362 | |
| 363 | data, err := stringToPacket(setTCont) |
| 364 | assert.NoError(t, err) |
| 365 | |
| 366 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 367 | assert.NotNil(t, packet) |
| 368 | |
| 369 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 370 | assert.NotNil(t, packet) |
| 371 | |
| 372 | omciMsg, ok := omciLayer.(*OMCI) |
| 373 | assert.True(t, ok) |
| 374 | assert.Equal(t, uint16(3), omciMsg.TransactionID) |
| 375 | assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| 376 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 377 | |
| 378 | msgLayer := packet.Layer(LayerTypeSetRequest) |
| 379 | assert.NotNil(t, msgLayer) |
| 380 | |
| 381 | omciMsg2, ok2 := msgLayer.(*SetRequest) |
| 382 | assert.True(t, ok2) |
| 383 | assert.Equal(t, me.TContClassID, omciMsg2.EntityClass) |
| 384 | assert.Equal(t, uint16(0x8000), omciMsg2.EntityInstance) |
| 385 | |
| 386 | attributes := omciMsg2.Attributes |
| 387 | assert.Equal(t, 2, len(attributes)) |
| 388 | |
| 389 | // Here 1 is the index in the attribute definition map of a TCONT that points |
| 390 | // to the AllocID attribute. |
| 391 | value, ok3 := attributes["AllocId"] |
| 392 | assert.True(t, ok3) |
| 393 | assert.Equal(t, value, uint16(1024)) |
| 394 | |
| 395 | // Test serialization back to former string |
| 396 | var options gopacket.SerializeOptions |
| 397 | options.FixLengths = true |
| 398 | |
| 399 | buffer := gopacket.NewSerializeBuffer() |
| 400 | err = gopacket.SerializeLayers(buffer, options, omciMsg, omciMsg2) |
| 401 | assert.NoError(t, err) |
| 402 | |
| 403 | outgoingPacket := buffer.Bytes() |
| 404 | reconstituted := packetToString(outgoingPacket) |
| 405 | assert.Equal(t, strings.ToLower(setTCont), reconstituted) |
| 406 | } |
| 407 | |
| 408 | func TestSet8021pMapperServiceProfile(t *testing.T) { |
| 409 | set8021pMapperServiceProfile := "0016480A008280004000800100000000" + |
| 410 | "00000000000000000000000000000000" + |
| 411 | "000000000000000000000028" |
| 412 | |
| 413 | data, err := stringToPacket(set8021pMapperServiceProfile) |
| 414 | assert.NoError(t, err) |
| 415 | |
| 416 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 417 | assert.NotNil(t, packet) |
| 418 | |
| 419 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 420 | assert.NotNil(t, packet) |
| 421 | |
| 422 | omciMsg, ok := omciLayer.(*OMCI) |
| 423 | assert.True(t, ok) |
| 424 | assert.Equal(t, uint16(0x16), omciMsg.TransactionID) |
| 425 | assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| 426 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 427 | |
| 428 | msgLayer := packet.Layer(LayerTypeSetRequest) |
| 429 | assert.NotNil(t, msgLayer) |
| 430 | |
| 431 | setRequest, ok2 := msgLayer.(*SetRequest) |
| 432 | assert.True(t, ok2) |
| 433 | assert.Equal(t, me.Ieee8021PMapperServiceProfileClassID, setRequest.EntityClass) |
| 434 | assert.Equal(t, uint16(0x8000), setRequest.EntityInstance) |
| 435 | |
| 436 | attributes := setRequest.Attributes |
| 437 | assert.NotNil(t, attributes) |
| 438 | assert.Equal(t, 2, len(attributes)) |
| 439 | |
| 440 | // Test serialization back to former string |
| 441 | var options gopacket.SerializeOptions |
| 442 | options.FixLengths = true |
| 443 | |
| 444 | buffer := gopacket.NewSerializeBuffer() |
| 445 | err = gopacket.SerializeLayers(buffer, options, omciMsg, setRequest) |
| 446 | assert.NoError(t, err) |
| 447 | |
| 448 | outgoingPacket := buffer.Bytes() |
| 449 | reconstituted := packetToString(outgoingPacket) |
| 450 | assert.Equal(t, strings.ToLower(set8021pMapperServiceProfile), reconstituted) |
| 451 | } |