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 | |
| 18 | package omci_test |
| 19 | |
| 20 | import ( |
| 21 | "encoding/hex" |
| 22 | "fmt" |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 23 | "github.com/google/gopacket" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 24 | . "github.com/opencord/omci-lib-go/v2" |
| 25 | . "github.com/opencord/omci-lib-go/v2/generated" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
| 27 | "strings" |
| 28 | "testing" |
| 29 | ) |
| 30 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 31 | var allBaselineTypes = []MessageType{ |
| 32 | CreateRequestType, |
| 33 | CreateResponseType, |
| 34 | DeleteRequestType, |
| 35 | DeleteResponseType, |
| 36 | SetRequestType, |
| 37 | SetResponseType, |
| 38 | GetRequestType, |
| 39 | GetResponseType, |
| 40 | GetAllAlarmsRequestType, |
| 41 | GetAllAlarmsResponseType, |
| 42 | GetAllAlarmsNextRequestType, |
| 43 | GetAllAlarmsNextResponseType, |
| 44 | MibUploadRequestType, |
| 45 | MibUploadResponseType, |
| 46 | MibUploadNextRequestType, |
| 47 | MibUploadNextResponseType, |
| 48 | MibResetRequestType, |
| 49 | MibResetResponseType, |
| 50 | TestRequestType, |
| 51 | TestResponseType, |
| 52 | StartSoftwareDownloadRequestType, |
| 53 | StartSoftwareDownloadResponseType, |
| 54 | DownloadSectionRequestType, |
| 55 | DownloadSectionRequestWithResponseType, |
| 56 | DownloadSectionResponseType, |
| 57 | EndSoftwareDownloadRequestType, |
| 58 | EndSoftwareDownloadResponseType, |
| 59 | ActivateSoftwareRequestType, |
| 60 | ActivateSoftwareResponseType, |
| 61 | CommitSoftwareRequestType, |
| 62 | CommitSoftwareResponseType, |
| 63 | SynchronizeTimeRequestType, |
| 64 | SynchronizeTimeResponseType, |
| 65 | RebootRequestType, |
| 66 | RebootResponseType, |
| 67 | GetNextRequestType, |
| 68 | GetNextResponseType, |
| 69 | GetCurrentDataRequestType, |
| 70 | GetCurrentDataResponseType, |
| 71 | AlarmNotificationType, |
| 72 | AttributeValueChangeType, |
| 73 | TestResultType, |
| 74 | } |
| 75 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 76 | func stringToPacket(input string) ([]byte, error) { |
| 77 | var p []byte |
| 78 | |
| 79 | p, err := hex.DecodeString(input) |
| 80 | if err != nil { |
| 81 | fmt.Println(err) |
| 82 | return nil, err |
| 83 | } |
| 84 | return p, nil |
| 85 | } |
| 86 | |
| 87 | func packetToString(input []byte) string { |
| 88 | return strings.ToLower(hex.EncodeToString(input)) |
| 89 | } |
| 90 | |
| 91 | func getSbcMask(meDefinition IManagedEntityDefinition) uint16 { |
| 92 | var sbcMask uint16 |
| 93 | |
| 94 | for index, attr := range meDefinition.GetAttributeDefinitions() { |
| 95 | if SupportsAttributeAccess(attr, SetByCreate) { |
| 96 | if index == 0 { |
| 97 | continue // Skip Entity ID |
| 98 | } |
| 99 | sbcMask |= attr.Mask |
| 100 | } |
| 101 | } |
| 102 | return sbcMask |
| 103 | } |
| 104 | |
| 105 | func TestDeviceIdents(t *testing.T) { |
| 106 | |
| 107 | baselineString := BaselineIdent.String() |
| 108 | assert.NotZero(t, len(baselineString)) |
| 109 | |
| 110 | extendedString := ExtendedIdent.String() |
| 111 | assert.NotZero(t, len(extendedString)) |
| 112 | |
| 113 | assert.NotEqual(t, baselineString, extendedString) |
| 114 | |
| 115 | unknownString := DeviceIdent(0xff).String() |
| 116 | assert.NotZero(t, len(unknownString)) |
| 117 | assert.NotEqual(t, unknownString, baselineString) |
| 118 | assert.NotEqual(t, unknownString, extendedString) |
| 119 | } |
| 120 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 121 | func TestOmciCanDecodeAndNextLayer(t *testing.T) { |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 122 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 123 | baselineString := BaselineIdent.String() |
| 124 | assert.NotZero(t, len(baselineString)) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 125 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 126 | createGalEthernetProfile := "0002440A011000010030000000000000" + |
| 127 | "00000000000000000000000000000000" + |
| 128 | "000000000000000000000028" |
| 129 | |
| 130 | data, err := stringToPacket(createGalEthernetProfile) |
| 131 | assert.NoError(t, err) |
| 132 | |
| 133 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 134 | assert.NotNil(t, packet) |
| 135 | |
| 136 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 137 | assert.NotNil(t, packet) |
| 138 | |
| 139 | omciMsg, ok := omciLayer.(*OMCI) |
| 140 | assert.True(t, ok) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 141 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 142 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 143 | assert.Equal(t, LayerTypeCreateRequest, omciMsg.NextLayerType()) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 144 | |
| 145 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 146 | assert.NotNil(t, msgLayer) |
| 147 | |
| 148 | omciMsg2, ok2 := msgLayer.(*CreateRequest) |
| 149 | assert.True(t, ok2) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 150 | assert.Equal(t, LayerTypeCreateRequest, omciMsg2.LayerType()) |
| 151 | assert.Equal(t, LayerTypeCreateRequest, omciMsg2.CanDecode()) |
| 152 | assert.Equal(t, gopacket.LayerTypePayload, omciMsg2.NextLayerType()) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 155 | func TestOmciHeaderVeryShort(t *testing.T) { |
| 156 | // Need at least 6 octets in OMCI header to decode Message Type |
| 157 | message := "000159" |
| 158 | data, err := stringToPacket(message) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 159 | assert.NoError(t, err) |
| 160 | |
| 161 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 162 | assert.NotNil(t, packet) |
| 163 | |
| 164 | omciLayer := packet.Layer(LayerTypeOMCI) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 165 | assert.Nil(t, omciLayer) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 166 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 167 | badLayer := packet.Layer(gopacket.LayerTypeDecodeFailure) |
| 168 | assert.NotNil(t, badLayer) |
| 169 | assert.True(t, packet.Metadata().Truncated) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 172 | func TestOmciHeaderBaselineShort(t *testing.T) { |
| 173 | for _, msgType := range allBaselineTypes { |
| 174 | // Smallest message baseline is 40 bytes (length and MIC optional) |
| 175 | tid := 1 |
| 176 | if msgType == AlarmNotificationType || msgType == AttributeValueChangeType { |
| 177 | tid = 0 |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 178 | } |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 179 | msg39 := fmt.Sprintf("%04x%02x0a0002000000000000000000000000000000000000000000000000000000000000000000", |
| 180 | uint16(tid), uint8(msgType)) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 181 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 182 | data, err := stringToPacket(msg39) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 183 | assert.NoError(t, err) |
| 184 | |
| 185 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 186 | assert.NotNil(t, packet) |
| 187 | |
| 188 | omciLayer := packet.Layer(LayerTypeOMCI) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 189 | assert.Nil(t, omciLayer) |
| 190 | |
| 191 | badLayer := packet.Layer(gopacket.LayerTypeDecodeFailure) |
| 192 | assert.NotNil(t, badLayer) |
| 193 | truncated := packet.Metadata().Truncated |
| 194 | assert.True(t, truncated) |
| 195 | |
| 196 | // Let length be optional size baseline size is fixed and we can recover from that |
| 197 | msg40 := fmt.Sprintf("%04x%02x0a000200000000000000000000000000000000000000000000000000000000000000000000", |
| 198 | uint16(tid), uint8(msgType)) |
| 199 | data, err = stringToPacket(msg40) |
| 200 | assert.NoError(t, err) |
| 201 | |
| 202 | packet = gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 203 | assert.NotNil(t, packet) |
| 204 | |
| 205 | omciLayer = packet.Layer(LayerTypeOMCI) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 206 | assert.NotNil(t, omciLayer) |
| 207 | |
| 208 | omciMsg, ok := omciLayer.(*OMCI) |
| 209 | assert.True(t, ok) |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 210 | assert.Equal(t, uint16(40), omciMsg.Length) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 214 | func TestOmciHeaderExtendedShort(t *testing.T) { |
| 215 | // Smallest message possible is an Extended Set Delete request which |
| 216 | // is 10 octets. |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 217 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 218 | //mibResetRequest := "0001 4F 0A 0002 0000 0000000000000000" + |
| 219 | // "00000000000000000000000000000000" + |
| 220 | // "000000000000000000000028" |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 221 | |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 224 | func TestBad2017_G_988(t *testing.T) { |
| 225 | // ITU-G.988 11/2017 has a bad set of class IDs that map the Ethernet 64-bit PM counter |
| 226 | // to class ID 426 when it should be 425. Make sure that code-generation of the OMCI |
| 227 | // ME's does not let that bad value find it's way back into our library. |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 228 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 229 | assert.Equal(t, ClassID(425), EthernetFrameExtendedPm64BitClassID) |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 230 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 231 | instance, omciErr := NewEthernetFrameExtendedPm64Bit() |
| 232 | assert.NotNil(t, instance) |
| 233 | assert.NotNil(t, omciErr) |
| 234 | assert.Equal(t, omciErr.StatusCode(), Success) |
| 235 | assert.Equal(t, EthernetFrameExtendedPm64BitClassID, instance.GetClassID()) |
| 236 | } |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 237 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 238 | func TestBaselineBadLenSerialize(t *testing.T) { |
| 239 | omciLayer := &OMCI{ |
| 240 | TransactionID: 0x0211, |
| 241 | MessageType: DeleteResponseType, |
| 242 | DeviceIdentifier: BaselineIdent, |
| 243 | Length: 39, // Must be 0 or 40 |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 244 | } |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 245 | request := &DeleteResponse{ |
| 246 | MeBasePacket: MeBasePacket{ |
| 247 | EntityClass: ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 248 | EntityInstance: uint16(0x202), |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 249 | }, |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 250 | Result: Success, |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 251 | } |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 252 | // Test serialization back to former string |
| 253 | var options gopacket.SerializeOptions |
| 254 | options.FixLengths = true |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 255 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 256 | buffer := gopacket.NewSerializeBuffer() |
| 257 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 258 | assert.Error(t, err) |
| 259 | } |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 260 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 261 | func TestBadIdentSerialize(t *testing.T) { |
| 262 | omciLayer := &OMCI{ |
| 263 | TransactionID: 0x0211, |
| 264 | MessageType: DeleteResponseType, |
| 265 | DeviceIdentifier: DeviceIdent(1), // Default is Baseline (0xa), other is Extended (0xb) |
| 266 | } |
| 267 | request := &DeleteResponse{ |
| 268 | MeBasePacket: MeBasePacket{ |
| 269 | EntityClass: ExtendedVlanTaggingOperationConfigurationDataClassID, |
| 270 | EntityInstance: uint16(0x202), |
| 271 | }, |
| 272 | Result: Success, |
| 273 | } |
| 274 | // Test serialization back to former string |
| 275 | var options gopacket.SerializeOptions |
| 276 | options.FixLengths = true |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 277 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 278 | buffer := gopacket.NewSerializeBuffer() |
| 279 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 280 | assert.Error(t, err) |
Chip Boling | ff6bf17 | 2020-12-01 10:38:12 -0600 | [diff] [blame] | 281 | } |