| /* |
| * 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 TestSetRequestDecode(t *testing.T) { |
| goodMessage := "0107480a01000000020000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeSetRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetRequest) |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*SetRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeSetRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeSetRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestSetRequestSerialize(t *testing.T) { |
| goodMessage := "0107480a01000000020001000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0107, |
| MessageType: SetRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &SetRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| AttributeMask: uint16(0x200), |
| Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| } |
| // 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 TestSetRequestZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0, |
| MessageType: SetRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &SetRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| AttributeMask: uint16(0x200), |
| Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| } |
| // 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 TestSetResponseDecode(t *testing.T) { |
| goodMessage := "0107280a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeSetResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| assert.Equal(t, SetResponseType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetResponse) |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*SetResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeSetResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeSetResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestSetResponseSerialize(t *testing.T) { |
| goodMessage := "0107280a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0107, |
| MessageType: SetResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &SetResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| 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 TestSetResponseZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0, |
| MessageType: SetResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &SetResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| 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 TestExtendedSetRequestDecode(t *testing.T) { |
| goodMessage := "0107480b010000000003020001" |
| 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, LayerTypeSetRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(3), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetRequest) |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*SetRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeSetRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeSetRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| assert.Equal(t, uint16(0x0200), request.AttributeMask) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedSetRequestSerialize(t *testing.T) { |
| goodMessage := "0107480b010000000003020001" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0107, |
| MessageType: SetRequestType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &SetRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| Extended: true, |
| }, |
| AttributeMask: uint16(0x200), |
| Attributes: me.AttributeValueMap{"AdministrativeState": byte(1)}, |
| } |
| // 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 TestExtendedSetResponseDecode(t *testing.T) { |
| goodMessage := "0107280b01000000000100" |
| data, err := stringToPacket(goodMessage) |
| assert.NoError(t, err) |
| // TODO: Also test result == 9 decode |
| |
| 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, LayerTypeSetResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0107), omciMsg.TransactionID) |
| assert.Equal(t, SetResponseType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(1), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetResponse) |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*SetResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeSetResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeSetResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| assert.Equal(t, me.Success, response.Result) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedSetResponseSerialize(t *testing.T) { |
| goodMessage := "0107280b01000000000103" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0107, |
| MessageType: SetResponseType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &SetResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| 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) |
| |
| } |
| |
| func TestSetResponseTableFailedAttributesDecode(t *testing.T) { |
| // This is a SET Response with failed and unsupported attributes |
| // TODO:Implement (also implement for Extended message set) |
| } |
| |
| func TestSetResponseTableFailedAttributesSerialize(t *testing.T) { |
| // This is a SET Response with failed and unsupported attributes |
| // TODO:Implement (also implement for Extended message set) |
| } |
| |
| func TestSetTCont(t *testing.T) { |
| setTCont := "0003480A010680008000040000000000" + |
| "00000000000000000000000000000000" + |
| "000000000000000000000028" |
| |
| data, err := stringToPacket(setTCont) |
| assert.NoError(t, err) |
| |
| packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| assert.NotNil(t, packet) |
| |
| omciLayer := packet.Layer(LayerTypeOMCI) |
| assert.NotNil(t, packet) |
| |
| omciMsg, ok := omciLayer.(*OMCI) |
| assert.True(t, ok) |
| assert.Equal(t, uint16(3), omciMsg.TransactionID) |
| assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetRequest) |
| assert.NotNil(t, msgLayer) |
| |
| omciMsg2, ok2 := msgLayer.(*SetRequest) |
| assert.True(t, ok2) |
| assert.Equal(t, me.TContClassID, omciMsg2.EntityClass) |
| assert.Equal(t, uint16(0x8000), omciMsg2.EntityInstance) |
| |
| attributes := omciMsg2.Attributes |
| assert.Equal(t, 2, len(attributes)) |
| |
| // Here 1 is the index in the attribute definition map of a TCONT that points |
| // to the AllocID attribute. |
| value, ok3 := attributes["AllocId"] |
| assert.True(t, ok3) |
| assert.Equal(t, value, uint16(1024)) |
| |
| // Test serialization back to former string |
| var options gopacket.SerializeOptions |
| options.FixLengths = true |
| |
| buffer := gopacket.NewSerializeBuffer() |
| err = gopacket.SerializeLayers(buffer, options, omciMsg, omciMsg2) |
| assert.NoError(t, err) |
| |
| outgoingPacket := buffer.Bytes() |
| reconstituted := packetToString(outgoingPacket) |
| assert.Equal(t, strings.ToLower(setTCont), reconstituted) |
| } |
| |
| func TestSet8021pMapperServiceProfile(t *testing.T) { |
| set8021pMapperServiceProfile := "0016480A008280004000800100000000" + |
| "00000000000000000000000000000000" + |
| "000000000000000000000028" |
| |
| data, err := stringToPacket(set8021pMapperServiceProfile) |
| assert.NoError(t, err) |
| |
| packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| assert.NotNil(t, packet) |
| |
| omciLayer := packet.Layer(LayerTypeOMCI) |
| assert.NotNil(t, packet) |
| |
| omciMsg, ok := omciLayer.(*OMCI) |
| assert.True(t, ok) |
| assert.Equal(t, uint16(0x16), omciMsg.TransactionID) |
| assert.Equal(t, SetRequestType, omciMsg.MessageType) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeSetRequest) |
| assert.NotNil(t, msgLayer) |
| |
| setRequest, ok2 := msgLayer.(*SetRequest) |
| assert.True(t, ok2) |
| assert.Equal(t, me.Ieee8021PMapperServiceProfileClassID, setRequest.EntityClass) |
| assert.Equal(t, uint16(0x8000), setRequest.EntityInstance) |
| |
| attributes := setRequest.Attributes |
| assert.NotNil(t, attributes) |
| assert.Equal(t, 2, len(attributes)) |
| |
| // Test serialization back to former string |
| var options gopacket.SerializeOptions |
| options.FixLengths = true |
| |
| buffer := gopacket.NewSerializeBuffer() |
| err = gopacket.SerializeLayers(buffer, options, omciMsg, setRequest) |
| assert.NoError(t, err) |
| |
| outgoingPacket := buffer.Bytes() |
| reconstituted := packetToString(outgoingPacket) |
| assert.Equal(t, strings.ToLower(set8021pMapperServiceProfile), reconstituted) |
| } |