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