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 TestDeleteRequestDecode(t *testing.T) { |
| 30 | goodMessage := "0211460a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeDeleteRequest, omciMsg.NextLayerType()) |
| 46 | assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| 47 | assert.Equal(t, DeleteRequestType, omciMsg.MessageType) |
| 48 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 49 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 50 | |
| 51 | msgLayer := packet.Layer(LayerTypeDeleteRequest) |
| 52 | |
| 53 | assert.NotNil(t, msgLayer) |
| 54 | |
| 55 | request, ok2 := msgLayer.(*DeleteRequest) |
| 56 | assert.True(t, ok2) |
| 57 | assert.NotNil(t, request) |
| 58 | assert.Equal(t, LayerTypeDeleteRequest, request.LayerType()) |
| 59 | assert.Equal(t, LayerTypeDeleteRequest, request.CanDecode()) |
| 60 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 61 | |
| 62 | // Verify string output for message |
| 63 | packetString := packet.String() |
| 64 | assert.NotZero(t, len(packetString)) |
| 65 | } |
| 66 | |
| 67 | func TestDeleteRequestSerialize(t *testing.T) { |
| 68 | goodMessage := "0211460a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| 69 | |
| 70 | omciLayer := &OMCI{ |
| 71 | TransactionID: 0x0211, |
| 72 | MessageType: DeleteRequestType, |
| 73 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 74 | // Length: 0x28, // Optional, defaults to 40 octets |
| 75 | } |
| 76 | request := &DeleteRequest{ |
| 77 | MeBasePacket: MeBasePacket{ |
| 78 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 79 | EntityInstance: uint16(0x202), |
| 80 | }, |
| 81 | } |
| 82 | // Test serialization back to former string |
| 83 | var options gopacket.SerializeOptions |
| 84 | options.FixLengths = true |
| 85 | |
| 86 | buffer := gopacket.NewSerializeBuffer() |
| 87 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 88 | assert.NoError(t, err) |
| 89 | |
| 90 | outgoingPacket := buffer.Bytes() |
| 91 | reconstituted := packetToString(outgoingPacket) |
| 92 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 93 | } |
| 94 | |
| 95 | func TestDeleteRequestZeroTICSerialize(t *testing.T) { |
| 96 | omciLayer := &OMCI{ |
| 97 | TransactionID: 0x0000, |
| 98 | MessageType: DeleteRequestType, |
| 99 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 100 | // Length: 0x28, // Optional, defaults to 40 octets |
| 101 | } |
| 102 | request := &DeleteRequest{ |
| 103 | MeBasePacket: MeBasePacket{ |
| 104 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 105 | EntityInstance: uint16(0x202), |
| 106 | }, |
| 107 | } |
| 108 | // Test serialization back to former string |
| 109 | var options gopacket.SerializeOptions |
| 110 | options.FixLengths = true |
| 111 | |
| 112 | buffer := gopacket.NewSerializeBuffer() |
| 113 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 114 | assert.Error(t, err) |
| 115 | } |
| 116 | |
| 117 | func TestDeleteResponseDecode(t *testing.T) { |
| 118 | goodMessage := "0211260a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028013437fb" |
| 119 | data, err := stringToPacket(goodMessage) |
| 120 | assert.NoError(t, err) |
| 121 | |
| 122 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 123 | assert.NotNil(t, packet) |
| 124 | |
| 125 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 126 | assert.NotNil(t, omciLayer) |
| 127 | |
| 128 | omciMsg, ok := omciLayer.(*OMCI) |
| 129 | assert.True(t, ok) |
| 130 | assert.NotNil(t, omciMsg) |
| 131 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 132 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 133 | assert.Equal(t, LayerTypeDeleteResponse, omciMsg.NextLayerType()) |
| 134 | assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| 135 | assert.Equal(t, DeleteResponseType, omciMsg.MessageType) |
| 136 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 137 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 138 | |
| 139 | msgLayer := packet.Layer(LayerTypeDeleteResponse) |
| 140 | |
| 141 | assert.NotNil(t, msgLayer) |
| 142 | |
| 143 | response, ok2 := msgLayer.(*DeleteResponse) |
| 144 | assert.True(t, ok2) |
| 145 | assert.NotNil(t, response) |
| 146 | assert.Equal(t, LayerTypeDeleteResponse, response.LayerType()) |
| 147 | assert.Equal(t, LayerTypeDeleteResponse, response.CanDecode()) |
| 148 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 149 | |
| 150 | // Verify string output for message |
| 151 | packetString := packet.String() |
| 152 | assert.NotZero(t, len(packetString)) |
| 153 | } |
| 154 | |
| 155 | func TestDeleteResponseSerialize(t *testing.T) { |
| 156 | goodMessage := "0211260a00ab0202000000000000000000000000000000000000000000000000000000000000000000000028" |
| 157 | |
| 158 | omciLayer := &OMCI{ |
| 159 | TransactionID: 0x0211, |
| 160 | MessageType: DeleteResponseType, |
| 161 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 162 | // Length: 0x28, // Optional, defaults to 40 octets |
| 163 | } |
| 164 | request := &DeleteResponse{ |
| 165 | MeBasePacket: MeBasePacket{ |
| 166 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 167 | EntityInstance: uint16(0x202), |
| 168 | }, |
| 169 | Result: me.Success, |
| 170 | } |
| 171 | // Test serialization back to former string |
| 172 | var options gopacket.SerializeOptions |
| 173 | options.FixLengths = true |
| 174 | |
| 175 | buffer := gopacket.NewSerializeBuffer() |
| 176 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 177 | assert.NoError(t, err) |
| 178 | |
| 179 | outgoingPacket := buffer.Bytes() |
| 180 | reconstituted := packetToString(outgoingPacket) |
| 181 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 182 | } |
| 183 | |
| 184 | func TestDeleteResponseZeroTICSerialize(t *testing.T) { |
| 185 | omciLayer := &OMCI{ |
| 186 | TransactionID: 0x0, |
| 187 | MessageType: DeleteResponseType, |
| 188 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 189 | // Length: 0x28, // Optional, defaults to 40 octets |
| 190 | } |
| 191 | request := &DeleteResponse{ |
| 192 | MeBasePacket: MeBasePacket{ |
| 193 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 194 | EntityInstance: uint16(0x202), |
| 195 | }, |
| 196 | Result: me.Success, |
| 197 | } |
| 198 | // Test serialization back to former string |
| 199 | var options gopacket.SerializeOptions |
| 200 | options.FixLengths = true |
| 201 | |
| 202 | buffer := gopacket.NewSerializeBuffer() |
| 203 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 204 | assert.Error(t, err) |
| 205 | } |
| 206 | |
| 207 | func TestExtendedDeleteRequestDecode(t *testing.T) { |
| 208 | goodMessage := "0211460b00ab02020000" |
| 209 | data, err := stringToPacket(goodMessage) |
| 210 | assert.NoError(t, err) |
| 211 | |
| 212 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 213 | assert.NotNil(t, packet) |
| 214 | |
| 215 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 216 | assert.NotNil(t, omciLayer) |
| 217 | |
| 218 | omciMsg, ok := omciLayer.(*OMCI) |
| 219 | assert.True(t, ok) |
| 220 | assert.NotNil(t, omciMsg) |
| 221 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 222 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 223 | assert.Equal(t, LayerTypeDeleteRequest, omciMsg.NextLayerType()) |
| 224 | assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| 225 | assert.Equal(t, DeleteRequestType, omciMsg.MessageType) |
| 226 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 227 | assert.Equal(t, uint16(0), omciMsg.Length) |
| 228 | |
| 229 | msgLayer := packet.Layer(LayerTypeDeleteRequest) |
| 230 | |
| 231 | assert.NotNil(t, msgLayer) |
| 232 | |
| 233 | request, ok2 := msgLayer.(*DeleteRequest) |
| 234 | assert.True(t, ok2) |
| 235 | assert.NotNil(t, request) |
| 236 | assert.Equal(t, LayerTypeDeleteRequest, request.LayerType()) |
| 237 | assert.Equal(t, LayerTypeDeleteRequest, request.CanDecode()) |
| 238 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 239 | |
| 240 | // Verify string output for message |
| 241 | packetString := packet.String() |
| 242 | assert.NotZero(t, len(packetString)) |
| 243 | } |
| 244 | |
| 245 | func TestExtendedDeleteRequestSerialize(t *testing.T) { |
| 246 | goodMessage := "0211460b00ab02020000" |
| 247 | |
| 248 | omciLayer := &OMCI{ |
| 249 | TransactionID: 0x0211, |
| 250 | MessageType: DeleteRequestType, |
| 251 | DeviceIdentifier: ExtendedIdent, |
| 252 | } |
| 253 | request := &DeleteRequest{ |
| 254 | MeBasePacket: MeBasePacket{ |
| 255 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 256 | EntityInstance: uint16(0x202), |
| 257 | Extended: true, |
| 258 | }, |
| 259 | } |
| 260 | // Test serialization back to former string |
| 261 | var options gopacket.SerializeOptions |
| 262 | options.FixLengths = true |
| 263 | |
| 264 | buffer := gopacket.NewSerializeBuffer() |
| 265 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 266 | assert.NoError(t, err) |
| 267 | |
| 268 | outgoingPacket := buffer.Bytes() |
| 269 | reconstituted := packetToString(outgoingPacket) |
| 270 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 271 | } |
| 272 | |
| 273 | func TestExtendedDeleteResponseDecode(t *testing.T) { |
| 274 | goodMessage := "0211260b00ab0202000103" |
| 275 | data, err := stringToPacket(goodMessage) |
| 276 | assert.NoError(t, err) |
| 277 | |
| 278 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 279 | assert.NotNil(t, packet) |
| 280 | |
| 281 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 282 | assert.NotNil(t, omciLayer) |
| 283 | |
| 284 | omciMsg, ok := omciLayer.(*OMCI) |
| 285 | assert.True(t, ok) |
| 286 | assert.NotNil(t, omciMsg) |
| 287 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 288 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 289 | assert.Equal(t, LayerTypeDeleteResponse, omciMsg.NextLayerType()) |
| 290 | assert.Equal(t, uint16(0x0211), omciMsg.TransactionID) |
| 291 | assert.Equal(t, DeleteResponseType, omciMsg.MessageType) |
| 292 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 293 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 294 | |
| 295 | msgLayer := packet.Layer(LayerTypeDeleteResponse) |
| 296 | |
| 297 | assert.NotNil(t, msgLayer) |
| 298 | |
| 299 | response, ok2 := msgLayer.(*DeleteResponse) |
| 300 | assert.True(t, ok2) |
| 301 | assert.NotNil(t, response) |
| 302 | assert.Equal(t, LayerTypeDeleteResponse, response.LayerType()) |
| 303 | assert.Equal(t, LayerTypeDeleteResponse, response.CanDecode()) |
| 304 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 305 | assert.Equal(t, me.ParameterError, response.Result) |
| 306 | |
| 307 | // Verify string output for message |
| 308 | packetString := packet.String() |
| 309 | assert.NotZero(t, len(packetString)) |
| 310 | } |
| 311 | |
| 312 | func TestExtendedDeleteResponseSerialize(t *testing.T) { |
| 313 | goodMessage := "0211260b00ab0202000103" |
| 314 | |
| 315 | omciLayer := &OMCI{ |
| 316 | TransactionID: 0x0211, |
| 317 | MessageType: DeleteResponseType, |
| 318 | DeviceIdentifier: ExtendedIdent, |
| 319 | } |
| 320 | request := &DeleteResponse{ |
| 321 | MeBasePacket: MeBasePacket{ |
| 322 | EntityClass: me.ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 323 | EntityInstance: uint16(0x202), |
| 324 | Extended: true, |
| 325 | }, |
| 326 | Result: me.ParameterError, |
| 327 | } |
| 328 | // Test serialization back to former string |
| 329 | var options gopacket.SerializeOptions |
| 330 | options.FixLengths = true |
| 331 | |
| 332 | buffer := gopacket.NewSerializeBuffer() |
| 333 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 334 | assert.NoError(t, err) |
| 335 | |
| 336 | outgoingPacket := buffer.Bytes() |
| 337 | reconstituted := packetToString(outgoingPacket) |
| 338 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 339 | } |