| /* |
| * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| * Copyright 2020-present Open Networking Foundation |
| |
| * 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/google/gopacket" |
| . "github.com/opencord/omci-lib-go" |
| me "github.com/opencord/omci-lib-go/generated" |
| "github.com/stretchr/testify/assert" |
| "strings" |
| "testing" |
| ) |
| |
| func TestDeleteRequestDecode(t *testing.T) { |
| goodMessage := "0211460a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeDeleteRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| assert.Equal(t, DeleteRequestType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeDeleteRequest) |
| |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*DeleteRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeDeleteRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeDeleteRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestDeleteRequestSerialize(t *testing.T) { |
| goodMessage := "0211460a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0211, |
| MessageType: DeleteRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &DeleteRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| }, |
| } |
| // 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 TestDeleteRequestZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0000, |
| MessageType: DeleteRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &DeleteRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| }, |
| } |
| // 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 TestDeleteResponseDecode(t *testing.T) { |
| goodMessage := "0211260a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028013437fb" |
| 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, LayerTypeDeleteResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| assert.Equal(t, DeleteResponseType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeDeleteResponse) |
| |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*DeleteResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeDeleteResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeDeleteResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestDeleteResponseSerialize(t *testing.T) { |
| goodMessage := "0211260a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0211, |
| MessageType: DeleteResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &DeleteResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| }, |
| Result: me.Success, |
| } |
| // 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 TestDeleteResponseZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0, |
| MessageType: DeleteResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &DeleteResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| }, |
| Result: me.Success, |
| } |
| // 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 TestExtendedDeleteRequestDecode(t *testing.T) { |
| goodMessage := "0211460b00ab02020000" |
| 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, LayerTypeDeleteRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| assert.Equal(t, DeleteRequestType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(0), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeDeleteRequest) |
| |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*DeleteRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeDeleteRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeDeleteRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedDeleteRequestSerialize(t *testing.T) { |
| goodMessage := "0211460b00ab02020000" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0211, |
| MessageType: DeleteRequestType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &DeleteRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| Extended: true, |
| }, |
| } |
| // 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 TestExtendedDeleteResponseDecode(t *testing.T) { |
| goodMessage := "0211260b00ab0202000103" |
| 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, LayerTypeDeleteResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| assert.Equal(t, DeleteResponseType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(1), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeDeleteResponse) |
| |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*DeleteResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeDeleteResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeDeleteResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| assert.Equal(t, me.ParameterError, response.Result) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedDeleteResponseSerialize(t *testing.T) { |
| goodMessage := "0211260b00ab0202000103" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0211, |
| MessageType: DeleteResponseType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &DeleteResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| EntityInstance: uint16(0x202), |
| Extended: true, |
| }, |
| Result: me.ParameterError, |
| } |
| // 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) |
| } |