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" |
| 22 | . "github.com/opencord/omci-lib-go" |
| 23 | me "github.com/opencord/omci-lib-go/generated" |
| 24 | "github.com/stretchr/testify/assert" |
| 25 | "strings" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func TestRebootRequestDecode(t *testing.T) { |
| 30 | goodMessage := "0001590a01000000010000000000000000000000000000000000000000000000000000000000000000000028" |
| 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, LayerTypeRebootRequest, omciMsg.NextLayerType()) |
| 46 | assert.Equal(t, uint16(0x0001), omciMsg.TransactionID) |
| 47 | assert.Equal(t, RebootRequestType, omciMsg.MessageType) |
| 48 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 49 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 50 | |
| 51 | msgLayer := packet.Layer(LayerTypeRebootRequest) |
| 52 | assert.NotNil(t, msgLayer) |
| 53 | |
| 54 | request, ok2 := msgLayer.(*RebootRequest) |
| 55 | assert.True(t, ok2) |
| 56 | assert.NotNil(t, request) |
| 57 | assert.Equal(t, LayerTypeRebootRequest, request.LayerType()) |
| 58 | assert.Equal(t, LayerTypeRebootRequest, request.CanDecode()) |
| 59 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 60 | assert.Equal(t, me.OnuGClassID, request.EntityClass) |
| 61 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 62 | assert.Equal(t, uint8(1), request.RebootCondition) |
| 63 | |
| 64 | // Verify string output for message |
| 65 | packetString := packet.String() |
| 66 | assert.NotZero(t, len(packetString)) |
| 67 | } |
| 68 | |
| 69 | func TestRebootRequestSerialize(t *testing.T) { |
| 70 | goodMessage := "0001590a01000000020000000000000000000000000000000000000000000000000000000000000000000028" |
| 71 | |
| 72 | omciLayer := &OMCI{ |
| 73 | TransactionID: 0x0001, |
| 74 | MessageType: RebootRequestType, |
| 75 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 76 | // Length: 0x28, // Optional, defaults to 40 octets |
| 77 | } |
| 78 | request := &RebootRequest{ |
| 79 | MeBasePacket: MeBasePacket{ |
| 80 | EntityClass: me.OnuGClassID, |
| 81 | // Default Instance ID is 0 |
| 82 | }, |
| 83 | RebootCondition: uint8(2), |
| 84 | } |
| 85 | // Test serialization back to former string |
| 86 | var options gopacket.SerializeOptions |
| 87 | options.FixLengths = true |
| 88 | |
| 89 | buffer := gopacket.NewSerializeBuffer() |
| 90 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 91 | assert.NoError(t, err) |
| 92 | |
| 93 | outgoingPacket := buffer.Bytes() |
| 94 | reconstituted := packetToString(outgoingPacket) |
| 95 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 96 | } |
| 97 | |
| 98 | func TestRebootRequestZeroTICSerialize(t *testing.T) { |
| 99 | omciLayer := &OMCI{ |
| 100 | TransactionID: 0x0, |
| 101 | MessageType: RebootRequestType, |
| 102 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 103 | // Length: 0x28, // Optional, defaults to 40 octets |
| 104 | } |
| 105 | request := &RebootRequest{ |
| 106 | MeBasePacket: MeBasePacket{ |
| 107 | EntityClass: me.OnuGClassID, |
| 108 | // Default Instance ID is 0 |
| 109 | }, |
| 110 | RebootCondition: uint8(2), |
| 111 | } |
| 112 | // Test serialization back to former string |
| 113 | var options gopacket.SerializeOptions |
| 114 | options.FixLengths = true |
| 115 | |
| 116 | buffer := gopacket.NewSerializeBuffer() |
| 117 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 118 | assert.Error(t, err) |
| 119 | } |
| 120 | |
| 121 | func TestRebootResponseDecode(t *testing.T) { |
| 122 | goodMessage := "023c390a01000000000000000000000000000000000000000000000000000000000000000000000000000028" |
| 123 | data, err := stringToPacket(goodMessage) |
| 124 | assert.NoError(t, err) |
| 125 | |
| 126 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 127 | assert.NotNil(t, packet) |
| 128 | |
| 129 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 130 | assert.NotNil(t, omciLayer) |
| 131 | |
| 132 | omciMsg, ok := omciLayer.(*OMCI) |
| 133 | assert.True(t, ok) |
| 134 | assert.NotNil(t, omciMsg) |
| 135 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 136 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 137 | assert.Equal(t, LayerTypeRebootResponse, omciMsg.NextLayerType()) |
| 138 | assert.Equal(t, uint16(0x023c), omciMsg.TransactionID) |
| 139 | assert.Equal(t, RebootResponseType, omciMsg.MessageType) |
| 140 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 141 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 142 | |
| 143 | msgLayer := packet.Layer(LayerTypeRebootResponse) |
| 144 | assert.NotNil(t, msgLayer) |
| 145 | |
| 146 | response, ok2 := msgLayer.(*RebootResponse) |
| 147 | assert.True(t, ok2) |
| 148 | assert.NotNil(t, response) |
| 149 | assert.Equal(t, LayerTypeRebootResponse, response.LayerType()) |
| 150 | assert.Equal(t, LayerTypeRebootResponse, response.CanDecode()) |
| 151 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 152 | assert.Equal(t, me.OnuGClassID, response.EntityClass) |
| 153 | assert.Equal(t, uint16(0), response.EntityInstance) |
| 154 | assert.Equal(t, me.Success, response.Result) |
| 155 | |
| 156 | // Verify string output for message |
| 157 | packetString := packet.String() |
| 158 | assert.NotZero(t, len(packetString)) |
| 159 | } |
| 160 | |
| 161 | func TestRebootResponseSerialize(t *testing.T) { |
| 162 | goodMessage := "023c390a01000000060000000000000000000000000000000000000000000000000000000000000000000028" |
| 163 | |
| 164 | omciLayer := &OMCI{ |
| 165 | TransactionID: 0x023c, |
| 166 | MessageType: RebootResponseType, |
| 167 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 168 | // Length: 0x28, // Optional, defaults to 40 octets |
| 169 | } |
| 170 | request := &RebootResponse{ |
| 171 | MeBasePacket: MeBasePacket{ |
| 172 | EntityClass: me.OnuGClassID, |
| 173 | EntityInstance: uint16(0), |
| 174 | }, |
| 175 | Result: me.DeviceBusy, |
| 176 | } |
| 177 | // Test serialization back to former string |
| 178 | var options gopacket.SerializeOptions |
| 179 | options.FixLengths = true |
| 180 | |
| 181 | buffer := gopacket.NewSerializeBuffer() |
| 182 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 183 | assert.NoError(t, err) |
| 184 | |
| 185 | outgoingPacket := buffer.Bytes() |
| 186 | reconstituted := packetToString(outgoingPacket) |
| 187 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 188 | } |
| 189 | |
| 190 | func TestRebootResponseZeroTICSerialize(t *testing.T) { |
| 191 | omciLayer := &OMCI{ |
| 192 | TransactionID: 0x0, |
| 193 | MessageType: RebootResponseType, |
| 194 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 195 | // Length: 0x28, // Optional, defaults to 40 octets |
| 196 | } |
| 197 | request := &RebootResponse{ |
| 198 | MeBasePacket: MeBasePacket{ |
| 199 | EntityClass: me.OnuGClassID, |
| 200 | EntityInstance: uint16(0), |
| 201 | }, |
| 202 | Result: me.DeviceBusy, |
| 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.Error(t, err) |
| 211 | } |
| 212 | |
| 213 | func TestExtendedRebootRequestDecode(t *testing.T) { |
| 214 | goodMessage := "0001590b01000000000101" |
| 215 | data, err := stringToPacket(goodMessage) |
| 216 | assert.NoError(t, err) |
| 217 | |
| 218 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 219 | assert.NotNil(t, packet) |
| 220 | |
| 221 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 222 | assert.NotNil(t, omciLayer) |
| 223 | |
| 224 | omciMsg, ok := omciLayer.(*OMCI) |
| 225 | assert.True(t, ok) |
| 226 | assert.NotNil(t, omciMsg) |
| 227 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 228 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 229 | assert.Equal(t, LayerTypeRebootRequest, omciMsg.NextLayerType()) |
| 230 | assert.Equal(t, uint16(0x0001), omciMsg.TransactionID) |
| 231 | assert.Equal(t, RebootRequestType, omciMsg.MessageType) |
| 232 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 233 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 234 | |
| 235 | msgLayer := packet.Layer(LayerTypeRebootRequest) |
| 236 | assert.NotNil(t, msgLayer) |
| 237 | |
| 238 | request, ok2 := msgLayer.(*RebootRequest) |
| 239 | assert.True(t, ok2) |
| 240 | assert.NotNil(t, request) |
| 241 | assert.Equal(t, LayerTypeRebootRequest, request.LayerType()) |
| 242 | assert.Equal(t, LayerTypeRebootRequest, request.CanDecode()) |
| 243 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 244 | assert.Equal(t, me.OnuGClassID, request.EntityClass) |
| 245 | assert.Equal(t, uint16(0), request.EntityInstance) |
| 246 | assert.Equal(t, uint8(1), request.RebootCondition) |
| 247 | |
| 248 | // Verify string output for message |
| 249 | packetString := packet.String() |
| 250 | assert.NotZero(t, len(packetString)) |
| 251 | } |
| 252 | |
| 253 | func TestExtendedRebootRequestSerialize(t *testing.T) { |
| 254 | goodMessage := "0001590b01000000000102" |
| 255 | |
| 256 | omciLayer := &OMCI{ |
| 257 | TransactionID: 0x0001, |
| 258 | MessageType: RebootRequestType, |
| 259 | DeviceIdentifier: ExtendedIdent, |
| 260 | } |
| 261 | request := &RebootRequest{ |
| 262 | MeBasePacket: MeBasePacket{ |
| 263 | EntityClass: me.OnuGClassID, |
| 264 | Extended: true, |
| 265 | }, |
| 266 | RebootCondition: uint8(2), |
| 267 | } |
| 268 | // Test serialization back to former string |
| 269 | var options gopacket.SerializeOptions |
| 270 | options.FixLengths = true |
| 271 | |
| 272 | buffer := gopacket.NewSerializeBuffer() |
| 273 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 274 | assert.NoError(t, err) |
| 275 | |
| 276 | outgoingPacket := buffer.Bytes() |
| 277 | reconstituted := packetToString(outgoingPacket) |
| 278 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 279 | } |
| 280 | |
| 281 | func TestExtendedRebootResponseDecode(t *testing.T) { |
| 282 | goodMessage := "023c390b01000000000100" |
| 283 | data, err := stringToPacket(goodMessage) |
| 284 | assert.NoError(t, err) |
| 285 | |
| 286 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 287 | assert.NotNil(t, packet) |
| 288 | |
| 289 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 290 | assert.NotNil(t, omciLayer) |
| 291 | |
| 292 | omciMsg, ok := omciLayer.(*OMCI) |
| 293 | assert.True(t, ok) |
| 294 | assert.NotNil(t, omciMsg) |
| 295 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 296 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 297 | assert.Equal(t, LayerTypeRebootResponse, omciMsg.NextLayerType()) |
| 298 | assert.Equal(t, uint16(0x023c), omciMsg.TransactionID) |
| 299 | assert.Equal(t, RebootResponseType, omciMsg.MessageType) |
| 300 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 301 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 302 | |
| 303 | msgLayer := packet.Layer(LayerTypeRebootResponse) |
| 304 | assert.NotNil(t, msgLayer) |
| 305 | |
| 306 | response, ok2 := msgLayer.(*RebootResponse) |
| 307 | assert.True(t, ok2) |
| 308 | assert.NotNil(t, response) |
| 309 | assert.Equal(t, LayerTypeRebootResponse, response.LayerType()) |
| 310 | assert.Equal(t, LayerTypeRebootResponse, response.CanDecode()) |
| 311 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 312 | assert.Equal(t, me.OnuGClassID, response.EntityClass) |
| 313 | assert.Equal(t, uint16(0), response.EntityInstance) |
| 314 | assert.Equal(t, me.Success, response.Result) |
| 315 | |
| 316 | // Verify string output for message |
| 317 | packetString := packet.String() |
| 318 | assert.NotZero(t, len(packetString)) |
| 319 | } |
| 320 | |
| 321 | func TestExtendedRebootResponseSerialize(t *testing.T) { |
| 322 | goodMessage := "023c390b01000000000106" |
| 323 | |
| 324 | omciLayer := &OMCI{ |
| 325 | TransactionID: 0x023c, |
| 326 | MessageType: RebootResponseType, |
| 327 | DeviceIdentifier: ExtendedIdent, |
| 328 | } |
| 329 | request := &RebootResponse{ |
| 330 | MeBasePacket: MeBasePacket{ |
| 331 | EntityClass: me.OnuGClassID, |
| 332 | EntityInstance: uint16(0), |
| 333 | Extended: true, |
| 334 | }, |
| 335 | Result: me.DeviceBusy, |
| 336 | } |
| 337 | // Test serialization back to former string |
| 338 | var options gopacket.SerializeOptions |
| 339 | options.FixLengths = true |
| 340 | |
| 341 | buffer := gopacket.NewSerializeBuffer() |
| 342 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 343 | assert.NoError(t, err) |
| 344 | |
| 345 | outgoingPacket := buffer.Bytes() |
| 346 | reconstituted := packetToString(outgoingPacket) |
| 347 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 348 | } |
| 349 | |
| 350 | func TestOnuRebootRequest(t *testing.T) { |
| 351 | onuRebootRequest := "0016590a01000000000000000000000000000" + |
| 352 | "0000000000000000000000000000000000000" + |
| 353 | "00000000000028" |
| 354 | |
| 355 | data, err := stringToPacket(onuRebootRequest) |
| 356 | assert.NoError(t, err) |
| 357 | |
| 358 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 359 | assert.NotNil(t, packet) |
| 360 | |
| 361 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 362 | assert.NotNil(t, packet) |
| 363 | |
| 364 | omciMsg, ok := omciLayer.(*OMCI) |
| 365 | assert.True(t, ok) |
| 366 | assert.Equal(t, uint16(0x16), omciMsg.TransactionID) |
| 367 | assert.Equal(t, RebootRequestType, omciMsg.MessageType) |
| 368 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 369 | |
| 370 | msgLayer := packet.Layer(LayerTypeRebootRequest) |
| 371 | assert.NotNil(t, msgLayer) |
| 372 | } |