| /* |
| * 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/v2" |
| me "github.com/opencord/omci-lib-go/v2/generated" |
| "github.com/stretchr/testify/assert" |
| "strings" |
| "testing" |
| ) |
| |
| func TestRebootRequestDecode(t *testing.T) { |
| goodMessage := "0001590a01000000010000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeRebootRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0001), omciMsg.TransactionID) |
| assert.Equal(t, RebootRequestType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeRebootRequest) |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*RebootRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeRebootRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeRebootRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| assert.Equal(t, me.OnuGClassID, request.EntityClass) |
| assert.Equal(t, uint16(0), request.EntityInstance) |
| assert.Equal(t, uint8(1), request.RebootCondition) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestRebootRequestSerialize(t *testing.T) { |
| goodMessage := "0001590a01000000020000000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0001, |
| MessageType: RebootRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &RebootRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| // Default Instance ID is 0 |
| }, |
| RebootCondition: uint8(2), |
| } |
| // 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 TestRebootRequestZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0, |
| MessageType: RebootRequestType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &RebootRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| // Default Instance ID is 0 |
| }, |
| RebootCondition: uint8(2), |
| } |
| // 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 TestRebootResponseDecode(t *testing.T) { |
| goodMessage := "023c390a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeRebootResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x023c), omciMsg.TransactionID) |
| assert.Equal(t, RebootResponseType, omciMsg.MessageType) |
| assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeRebootResponse) |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*RebootResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeRebootResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeRebootResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| assert.Equal(t, me.OnuGClassID, response.EntityClass) |
| assert.Equal(t, uint16(0), response.EntityInstance) |
| assert.Equal(t, me.Success, response.Result) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestRebootResponseSerialize(t *testing.T) { |
| goodMessage := "023c390a01000000060000000000000000000000000000000000000000000000000000000000000000000028" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x023c, |
| MessageType: RebootResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &RebootResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| Result: me.DeviceBusy, |
| } |
| // 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 TestRebootResponseZeroTICSerialize(t *testing.T) { |
| omciLayer := &OMCI{ |
| TransactionID: 0x0, |
| MessageType: RebootResponseType, |
| // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| // Length: 0x28, // Optional, defaults to 40 octets |
| } |
| request := &RebootResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| }, |
| Result: me.DeviceBusy, |
| } |
| // 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 TestExtendedRebootRequestDecode(t *testing.T) { |
| goodMessage := "0001590b01000000000101" |
| 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, LayerTypeRebootRequest, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x0001), omciMsg.TransactionID) |
| assert.Equal(t, RebootRequestType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(1), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeRebootRequest) |
| assert.NotNil(t, msgLayer) |
| |
| request, ok2 := msgLayer.(*RebootRequest) |
| assert.True(t, ok2) |
| assert.NotNil(t, request) |
| assert.Equal(t, LayerTypeRebootRequest, request.LayerType()) |
| assert.Equal(t, LayerTypeRebootRequest, request.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| assert.Equal(t, me.OnuGClassID, request.EntityClass) |
| assert.Equal(t, uint16(0), request.EntityInstance) |
| assert.Equal(t, uint8(1), request.RebootCondition) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedRebootRequestSerialize(t *testing.T) { |
| goodMessage := "0001590b01000000000102" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x0001, |
| MessageType: RebootRequestType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &RebootRequest{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| Extended: true, |
| }, |
| RebootCondition: uint8(2), |
| } |
| // 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 TestExtendedRebootResponseDecode(t *testing.T) { |
| goodMessage := "023c390b01000000000100" |
| 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, LayerTypeRebootResponse, omciMsg.NextLayerType()) |
| assert.Equal(t, uint16(0x023c), omciMsg.TransactionID) |
| assert.Equal(t, RebootResponseType, omciMsg.MessageType) |
| assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| assert.Equal(t, uint16(1), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeRebootResponse) |
| assert.NotNil(t, msgLayer) |
| |
| response, ok2 := msgLayer.(*RebootResponse) |
| assert.True(t, ok2) |
| assert.NotNil(t, response) |
| assert.Equal(t, LayerTypeRebootResponse, response.LayerType()) |
| assert.Equal(t, LayerTypeRebootResponse, response.CanDecode()) |
| assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| assert.Equal(t, me.OnuGClassID, response.EntityClass) |
| assert.Equal(t, uint16(0), response.EntityInstance) |
| assert.Equal(t, me.Success, response.Result) |
| |
| // Verify string output for message |
| packetString := packet.String() |
| assert.NotZero(t, len(packetString)) |
| } |
| |
| func TestExtendedRebootResponseSerialize(t *testing.T) { |
| goodMessage := "023c390b01000000000106" |
| |
| omciLayer := &OMCI{ |
| TransactionID: 0x023c, |
| MessageType: RebootResponseType, |
| DeviceIdentifier: ExtendedIdent, |
| } |
| request := &RebootResponse{ |
| MeBasePacket: MeBasePacket{ |
| EntityClass: me.OnuGClassID, |
| EntityInstance: uint16(0), |
| Extended: true, |
| }, |
| Result: me.DeviceBusy, |
| } |
| // 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 TestOnuRebootRequest(t *testing.T) { |
| onuRebootRequest := "0016590a01000000000000000000000000000" + |
| "0000000000000000000000000000000000000" + |
| "00000000000028" |
| |
| data, err := stringToPacket(onuRebootRequest) |
| 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, RebootRequestType, omciMsg.MessageType) |
| assert.Equal(t, uint16(40), omciMsg.Length) |
| |
| msgLayer := packet.Layer(LayerTypeRebootRequest) |
| assert.NotNil(t, msgLayer) |
| } |