Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 8 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 10 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 16 | */ |
| 17 | package omci_test |
| 18 | |
| 19 | import ( |
Chip Boling | 34ebcb6 | 2021-02-02 12:13:58 -0600 | [diff] [blame] | 20 | "github.com/google/gopacket" |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 21 | . "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
| 24 | "strings" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | var allMsgTypes = [...]me.MsgType{ |
| 29 | me.Create, |
| 30 | me.Delete, |
| 31 | me.Set, |
| 32 | me.Get, |
| 33 | me.GetAllAlarms, |
| 34 | me.GetAllAlarmsNext, |
| 35 | me.MibUpload, |
| 36 | me.MibUploadNext, |
| 37 | me.MibReset, |
| 38 | me.AlarmNotification, |
| 39 | me.AttributeValueChange, |
| 40 | me.Test, |
| 41 | me.StartSoftwareDownload, |
| 42 | me.DownloadSection, |
| 43 | me.EndSoftwareDownload, |
| 44 | me.ActivateSoftware, |
| 45 | me.CommitSoftware, |
| 46 | me.SynchronizeTime, |
| 47 | me.Reboot, |
| 48 | me.GetNext, |
| 49 | me.TestResult, |
| 50 | me.GetCurrentData, |
| 51 | me.SetTable} |
| 52 | |
| 53 | var allMessageTypes = [...]MessageType{ |
| 54 | CreateRequestType, |
| 55 | CreateResponseType, |
| 56 | DeleteRequestType, |
| 57 | DeleteResponseType, |
| 58 | SetRequestType, |
| 59 | SetResponseType, |
| 60 | GetRequestType, |
| 61 | GetResponseType, |
| 62 | GetAllAlarmsRequestType, |
| 63 | GetAllAlarmsResponseType, |
| 64 | GetAllAlarmsNextRequestType, |
| 65 | GetAllAlarmsNextResponseType, |
| 66 | MibUploadRequestType, |
| 67 | MibUploadResponseType, |
| 68 | MibUploadNextRequestType, |
| 69 | MibUploadNextResponseType, |
| 70 | MibResetRequestType, |
| 71 | MibResetResponseType, |
| 72 | TestRequestType, |
| 73 | TestResponseType, |
| 74 | StartSoftwareDownloadRequestType, |
| 75 | StartSoftwareDownloadResponseType, |
| 76 | DownloadSectionRequestType, |
| 77 | DownloadSectionResponseType, |
| 78 | EndSoftwareDownloadRequestType, |
| 79 | EndSoftwareDownloadResponseType, |
| 80 | ActivateSoftwareRequestType, |
| 81 | ActivateSoftwareResponseType, |
| 82 | CommitSoftwareRequestType, |
| 83 | CommitSoftwareResponseType, |
| 84 | SynchronizeTimeRequestType, |
| 85 | SynchronizeTimeResponseType, |
| 86 | RebootRequestType, |
| 87 | RebootResponseType, |
| 88 | GetNextRequestType, |
| 89 | GetNextResponseType, |
| 90 | GetCurrentDataRequestType, |
| 91 | GetCurrentDataResponseType, |
| 92 | SetTableRequestType, |
| 93 | SetTableResponseType, |
| 94 | // Autonomous ONU messages |
| 95 | AlarmNotificationType, |
| 96 | AttributeValueChangeType, |
| 97 | TestResultType, |
| 98 | } |
| 99 | |
| 100 | var allResults = [...]me.Results{ |
| 101 | me.Success, |
| 102 | me.ProcessingError, |
| 103 | me.NotSupported, |
| 104 | me.ParameterError, |
| 105 | me.UnknownEntity, |
| 106 | me.UnknownInstance, |
| 107 | me.DeviceBusy, |
| 108 | me.InstanceExists} |
| 109 | |
| 110 | // TestMsgTypeStrings tests that base message types can be printed |
| 111 | func TestMsgTypeStrings(t *testing.T) { |
| 112 | for _, msg := range allMsgTypes { |
| 113 | strMsg := msg.String() |
| 114 | assert.NotEqual(t, len(strMsg), 0) |
| 115 | } |
| 116 | unknown := me.MsgType(0xFF) |
| 117 | strMsg := unknown.String() |
| 118 | assert.NotEqual(t, len(strMsg), 0) |
| 119 | } |
| 120 | |
| 121 | // TestMessageTypeStrings tests that request/response/notification |
| 122 | // message types can be printed |
| 123 | func TestMessageTypeStrings(t *testing.T) { |
| 124 | for _, msg := range allMessageTypes { |
| 125 | strMsg := msg.String() |
| 126 | assert.NotEqual(t, len(strMsg), 0) |
| 127 | } |
| 128 | unknown := MessageType(0xFF) |
| 129 | strMsg := unknown.String() |
| 130 | assert.NotEqual(t, len(strMsg), 0) |
| 131 | } |
| 132 | |
| 133 | func TestResultsStrings(t *testing.T) { |
| 134 | for _, code := range allResults { |
| 135 | strMsg := code.String() |
| 136 | assert.NotEqual(t, len(strMsg), 0) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // TestOmciDecode will test for proper error checking of things that |
| 141 | // are invalid at the OMCI decode layer |
| 142 | func TestOmciDecode(t *testing.T) { |
| 143 | // TID = 0 on autonomous ONU notifications only. Get packet back but ErrorLayer() |
| 144 | // returns non-nil |
| 145 | tidZeroOnNonNotification := "0000440A010C01000400800003010000" + |
| 146 | "00000000000000000000000000000000" + |
| 147 | "000000000000000000000028" |
| 148 | |
| 149 | data, err := stringToPacket(tidZeroOnNonNotification) |
| 150 | assert.NoError(t, err) |
| 151 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 152 | assert.NotNil(t, packet) |
| 153 | assert.NotNil(t, packet.ErrorLayer()) |
| 154 | |
| 155 | // Only Baseline and Extended Message types allowed |
| 156 | invalidMessageType := "000C440F010C01000400800003010000" + |
| 157 | "00000000000000000000000000000000" + |
| 158 | "000000000000000000000028" |
| 159 | |
| 160 | data, err = stringToPacket(invalidMessageType) |
| 161 | assert.NoError(t, err) |
| 162 | packet = gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 163 | assert.NotNil(t, packet) |
| 164 | assert.NotNil(t, packet.ErrorLayer()) |
| 165 | |
| 166 | // Bad baseline message length |
| 167 | badBaselineMsgLength := "000C440A010C01000400800003010000" + |
| 168 | "00000000000000000000000000000000" + |
| 169 | "000000000000000000000029" |
| 170 | |
| 171 | data, err = stringToPacket(badBaselineMsgLength) |
| 172 | assert.NoError(t, err) |
| 173 | packet = gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 174 | assert.NotNil(t, packet) |
| 175 | assert.NotNil(t, packet.ErrorLayer()) |
| 176 | |
| 177 | // Bad extended message length |
| 178 | badExtendedMsgLength := "000C440B010C010000290400800003010000" + |
| 179 | "00000000000000000000000000000000" + |
| 180 | "00000000000000000000" |
| 181 | |
| 182 | data, err = stringToPacket(badExtendedMsgLength) |
| 183 | assert.NoError(t, err) |
| 184 | packet = gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 185 | assert.NotNil(t, packet) |
| 186 | assert.NotNil(t, packet.ErrorLayer()) |
| 187 | |
| 188 | // Huge extended message length |
| 189 | hugeExtendedMsgLength := "000C440B010C010007BD0400800003010000" + |
| 190 | "00000000000000000000000000000000" + |
| 191 | "00000000000000000000" |
| 192 | |
| 193 | data, err = stringToPacket(hugeExtendedMsgLength) |
| 194 | assert.NoError(t, err) |
| 195 | packet = gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 196 | assert.NotNil(t, packet) |
| 197 | assert.NotNil(t, packet.ErrorLayer()) |
| 198 | } |
| 199 | |
| 200 | // TestOmciSerialization will test for proper error checking of things that |
| 201 | // are invalid at the OMCI layer |
| 202 | func TestOmciSerialization(t *testing.T) { |
| 203 | goodMessage := "000C440A010C0100040080000301000000000000000000000000000000000000000000000000000000000028" |
| 204 | |
| 205 | omciLayerDefaults := &OMCI{ |
| 206 | TransactionID: 0x0c, |
| 207 | MessageType: CreateRequestType, |
| 208 | // DeviceIdentifier: BaselineIdent, // Optional, defaults to Baseline |
| 209 | // Length: 0x28, // Optional, defaults to 40 octets |
| 210 | } |
| 211 | omciLayerFixed := &OMCI{ |
| 212 | TransactionID: 0x0c, |
| 213 | MessageType: CreateRequestType, |
| 214 | DeviceIdentifier: BaselineIdent, |
| 215 | Length: 0x28, |
| 216 | } |
| 217 | request := &CreateRequest{ |
| 218 | MeBasePacket: MeBasePacket{ |
| 219 | EntityClass: me.GemPortNetworkCtpClassID, |
| 220 | EntityInstance: uint16(0x100), |
| 221 | }, |
| 222 | Attributes: me.AttributeValueMap{ |
| 223 | "PortId": 0x400, |
| 224 | "TContPointer": 0x8000, |
| 225 | "Direction": 3, |
| 226 | "TrafficManagementPointerForUpstream": 0x100, |
| 227 | "TrafficDescriptorProfilePointerForUpstream": 0, |
| 228 | "PriorityQueuePointerForDownStream": 0, |
| 229 | "TrafficDescriptorProfilePointerForDownstream": 0, |
| 230 | "EncryptionKeyRing": 0, |
| 231 | }, |
| 232 | } |
| 233 | // Test serialization back to former string (using defaults in the message parts) |
| 234 | var options gopacket.SerializeOptions |
| 235 | options.FixLengths = true |
| 236 | |
| 237 | buffer := gopacket.NewSerializeBuffer() |
| 238 | err := gopacket.SerializeLayers(buffer, options, omciLayerDefaults, request) |
| 239 | assert.NoError(t, err) |
| 240 | |
| 241 | outgoingPacket := buffer.Bytes() |
| 242 | reconstituted := packetToString(outgoingPacket) |
| 243 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 244 | |
| 245 | // Test serialization back to former string (using explicit values in the message parts) |
| 246 | buffer = gopacket.NewSerializeBuffer() |
| 247 | err = gopacket.SerializeLayers(buffer, options, omciLayerFixed, request) |
| 248 | assert.NoError(t, err) |
| 249 | |
| 250 | outgoingPacket = buffer.Bytes() |
| 251 | reconstituted = packetToString(outgoingPacket) |
| 252 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 253 | } |
| 254 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 255 | func TestGetSoftwareImageResponseDecode(t *testing.T) { |
| 256 | goodMessage := "035e290a00070000000400000102030405060708090a0b0c0d0e0f0000000000000000000000000000000028" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 257 | data, err := stringToPacket(goodMessage) |
| 258 | assert.NoError(t, err) |
| 259 | |
| 260 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 261 | assert.NotNil(t, packet) |
| 262 | |
| 263 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 264 | assert.NotNil(t, omciLayer) |
| 265 | |
| 266 | omciMsg, ok := omciLayer.(*OMCI) |
| 267 | assert.True(t, ok) |
| 268 | assert.Equal(t, omciMsg.TransactionID, uint16(0x035e)) |
| 269 | assert.Equal(t, omciMsg.MessageType, GetResponseType) |
| 270 | assert.Equal(t, omciMsg.DeviceIdentifier, BaselineIdent) |
| 271 | assert.Equal(t, omciMsg.Length, uint16(40)) |
| 272 | |
| 273 | msgLayer := packet.Layer(LayerTypeGetResponse) |
| 274 | assert.NotNil(t, msgLayer) |
| 275 | |
| 276 | response, ok2 := msgLayer.(*GetResponse) |
| 277 | assert.True(t, ok2) |
| 278 | assert.NotNil(t, response) |
| 279 | assert.Equal(t, response.Result, me.Success) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 280 | assert.Equal(t, response.AttributeMask, uint16(0x0400)) |
| 281 | assert.Equal(t, response.Attributes["ImageHash"], toOctets("AAECAwQFBgcICQoLDA0ODw==")) // 0->f octets) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 282 | |
| 283 | // Verify string output for message |
| 284 | packetString := packet.String() |
| 285 | assert.NotZero(t, len(packetString)) |
| 286 | } |
| 287 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 288 | func TestGetSoftwareImageResponseSerialize(t *testing.T) { |
| 289 | goodMessage := "035e290a00070000000400000102030405060708090a0b0c0d0e0f0000000000000000000000000000000028" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 290 | omciLayer := &OMCI{ |
| 291 | TransactionID: 0x035e, |
| 292 | MessageType: GetResponseType, |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 293 | } |
| 294 | request := &GetResponse{ |
| 295 | MeBasePacket: MeBasePacket{ |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 296 | EntityClass: me.SoftwareImageClassID, |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 297 | EntityInstance: uint16(0), |
| 298 | }, |
| 299 | Result: 0, |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 300 | AttributeMask: uint16(0x0400), |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 301 | Attributes: me.AttributeValueMap{ |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 302 | "ImageHash": toOctets("AAECAwQFBgcICQoLDA0ODw==")}, // 0->f octets |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 303 | } |
| 304 | // Test serialization back to former string |
| 305 | var options gopacket.SerializeOptions |
| 306 | options.FixLengths = true |
| 307 | |
| 308 | buffer := gopacket.NewSerializeBuffer() |
| 309 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 310 | assert.NoError(t, err) |
| 311 | |
| 312 | outgoingPacket := buffer.Bytes() |
| 313 | reconstituted := packetToString(outgoingPacket) |
| 314 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 315 | } |