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" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame^] | 22 | . "github.com/opencord/omci-lib-go/v2" |
| 23 | me "github.com/opencord/omci-lib-go/v2/generated" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
| 25 | "strings" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func TestAttributeValueChangeDecode(t *testing.T) { |
| 30 | goodMessage := "0000110a0007000080004d4c2d33363236000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeAttributeValueChange, omciMsg.NextLayerType()) |
| 46 | assert.Equal(t, uint16(0x0), omciMsg.TransactionID) |
| 47 | assert.Equal(t, AttributeValueChangeType, omciMsg.MessageType) |
| 48 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 49 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 50 | |
| 51 | msgLayer := packet.Layer(LayerTypeAttributeValueChange) |
| 52 | assert.NotNil(t, msgLayer) |
| 53 | |
| 54 | request, ok2 := msgLayer.(*AttributeValueChangeMsg) |
| 55 | assert.True(t, ok2) |
| 56 | assert.NotNil(t, request) |
| 57 | assert.Equal(t, LayerTypeAttributeValueChange, request.LayerType()) |
| 58 | assert.Equal(t, LayerTypeAttributeValueChange, request.CanDecode()) |
| 59 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 60 | assert.Equal(t, uint16(0x8000), request.AttributeMask) |
| 61 | assert.Equal(t, me.SoftwareImageClassID, request.EntityClass) |
| 62 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 63 | assert.Equal(t, []byte{ |
| 64 | 0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36, |
| 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 66 | request.Attributes["Version"]) |
| 67 | |
| 68 | // Verify string output for message |
| 69 | packetString := packet.String() |
| 70 | assert.NotZero(t, len(packetString)) |
| 71 | } |
| 72 | |
| 73 | func TestAttributeValueChangeSerialize(t *testing.T) { |
| 74 | goodMessage := "0000110a0007000080004d4c2d33363236000000000000000000000000000000000000000000000000000028" |
| 75 | |
| 76 | omciLayer := &OMCI{ |
| 77 | TransactionID: 0, |
| 78 | MessageType: AttributeValueChangeType, |
| 79 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 80 | // Length: 0x28, // Optional, defaults to 40 octets |
| 81 | } |
| 82 | request := &AttributeValueChangeMsg{ |
| 83 | MeBasePacket: MeBasePacket{ |
| 84 | EntityClass: me.SoftwareImageClassID, |
| 85 | EntityInstance: uint16(0), |
| 86 | }, |
| 87 | AttributeMask: uint16(0x8000), |
| 88 | Attributes: me.AttributeValueMap{ |
| 89 | "Version": []byte{ |
| 90 | 0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36, |
| 91 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 92 | }, |
| 93 | }, |
| 94 | } |
| 95 | // Test serialization back to former string |
| 96 | var options gopacket.SerializeOptions |
| 97 | options.FixLengths = true |
| 98 | |
| 99 | buffer := gopacket.NewSerializeBuffer() |
| 100 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 101 | assert.NoError(t, err) |
| 102 | |
| 103 | outgoingPacket := buffer.Bytes() |
| 104 | reconstituted := packetToString(outgoingPacket) |
| 105 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 106 | } |
| 107 | |
| 108 | func TestAttributeValueChangeNonZeroTicSerialize(t *testing.T) { |
| 109 | omciLayer := &OMCI{ |
| 110 | TransactionID: 1, |
| 111 | MessageType: AttributeValueChangeType, |
| 112 | } |
| 113 | request := &AttributeValueChangeMsg{ |
| 114 | MeBasePacket: MeBasePacket{ |
| 115 | EntityClass: me.SoftwareImageClassID, |
| 116 | EntityInstance: uint16(0), |
| 117 | }, |
| 118 | AttributeMask: uint16(0x8000), |
| 119 | Attributes: me.AttributeValueMap{ |
| 120 | "Version": []byte{ |
| 121 | 0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36, |
| 122 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 123 | }, |
| 124 | }, |
| 125 | } |
| 126 | // Test serialization back to former string |
| 127 | var options gopacket.SerializeOptions |
| 128 | options.FixLengths = true |
| 129 | |
| 130 | buffer := gopacket.NewSerializeBuffer() |
| 131 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 132 | assert.Error(t, err) |
| 133 | } |
| 134 | |
| 135 | func TestExtendedAttributeValueChangeDecode(t *testing.T) { |
| 136 | // Software Image Version (14 bytes) AVC |
| 137 | goodMessage := "0000110b00070000001080004d4c2d3336323600000000000000" |
| 138 | data, err := stringToPacket(goodMessage) |
| 139 | assert.NoError(t, err) |
| 140 | |
| 141 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 142 | assert.NotNil(t, packet) |
| 143 | |
| 144 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 145 | assert.NotNil(t, omciLayer) |
| 146 | |
| 147 | omciMsg, ok := omciLayer.(*OMCI) |
| 148 | assert.True(t, ok) |
| 149 | assert.NotNil(t, omciMsg) |
| 150 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 151 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 152 | assert.Equal(t, LayerTypeAttributeValueChange, omciMsg.NextLayerType()) |
| 153 | assert.Equal(t, uint16(0x0), omciMsg.TransactionID) |
| 154 | assert.Equal(t, AttributeValueChangeType, omciMsg.MessageType) |
| 155 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 156 | assert.Equal(t, uint16(2+14), omciMsg.Length) |
| 157 | |
| 158 | msgLayer := packet.Layer(LayerTypeAttributeValueChange) |
| 159 | assert.NotNil(t, msgLayer) |
| 160 | |
| 161 | request, ok2 := msgLayer.(*AttributeValueChangeMsg) |
| 162 | assert.True(t, ok2) |
| 163 | assert.NotNil(t, request) |
| 164 | assert.Equal(t, LayerTypeAttributeValueChange, request.LayerType()) |
| 165 | assert.Equal(t, LayerTypeAttributeValueChange, request.CanDecode()) |
| 166 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 167 | assert.Equal(t, uint16(0x8000), request.AttributeMask) |
| 168 | assert.Equal(t, me.SoftwareImageClassID, request.EntityClass) |
| 169 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 170 | assert.Equal(t, []byte{ |
| 171 | 0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36, |
| 172 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, request.Attributes["Version"]) |
| 173 | |
| 174 | // Verify string output for message |
| 175 | packetString := packet.String() |
| 176 | assert.NotZero(t, len(packetString)) |
| 177 | } |
| 178 | |
| 179 | func TestExtendedAttributeValueChangeSerialize(t *testing.T) { |
| 180 | goodMessage := "0000110b00070000001080004d4c2d3336323600000000000000" |
| 181 | |
| 182 | omciLayer := &OMCI{ |
| 183 | TransactionID: 0, |
| 184 | MessageType: AttributeValueChangeType, |
| 185 | DeviceIdentifier: ExtendedIdent, |
| 186 | // Length parameter is optional for Extended message format serialization |
| 187 | // and if present it will be overwritten during the serialization with the |
| 188 | // actual value. |
| 189 | } |
| 190 | request := &AttributeValueChangeMsg{ |
| 191 | MeBasePacket: MeBasePacket{ |
| 192 | EntityClass: me.SoftwareImageClassID, |
| 193 | EntityInstance: uint16(0), |
| 194 | Extended: true, |
| 195 | }, |
| 196 | AttributeMask: uint16(0x8000), |
| 197 | Attributes: me.AttributeValueMap{ |
| 198 | "Version": []byte{ |
| 199 | 0x4d, 0x4c, 0x2d, 0x33, 0x36, 0x32, 0x36, |
| 200 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 201 | }, |
| 202 | }, |
| 203 | } |
| 204 | // Test serialization back to former string |
| 205 | var options gopacket.SerializeOptions |
| 206 | options.FixLengths = true |
| 207 | |
| 208 | buffer := gopacket.NewSerializeBuffer() |
| 209 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 210 | assert.NoError(t, err) |
| 211 | |
| 212 | outgoingPacket := buffer.Bytes() |
| 213 | reconstituted := packetToString(outgoingPacket) |
| 214 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 215 | } |