blob: 86c9a59d4fd017bdd0195e859a32329283436a56 [file] [log] [blame]
Chip Boling6e27b352020-02-14 09:10:01 -06001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Andrea Campanella7167ebb2020-02-24 09:56:38 +01003 * Copyright 2020-present Open Networking Foundation
4
Chip Boling6e27b352020-02-14 09:10:01 -06005 * 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 Campanella7167ebb2020-02-24 09:56:38 +01008
Chip Boling6e27b352020-02-14 09:10:01 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Andrea Campanella7167ebb2020-02-24 09:56:38 +010010
Chip Boling6e27b352020-02-14 09:10:01 -060011 * 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 Boling6e27b352020-02-14 09:10:01 -060016 */
17
18package omci_test
19
20import (
21 "encoding/hex"
22 "fmt"
Chip Bolingff6bf172020-12-01 10:38:12 -060023 "github.com/google/gopacket"
Andrea Campanella7167ebb2020-02-24 09:56:38 +010024 . "github.com/opencord/omci-lib-go"
25 . "github.com/opencord/omci-lib-go/generated"
Chip Boling6e27b352020-02-14 09:10:01 -060026 "github.com/stretchr/testify/assert"
27 "strings"
28 "testing"
29)
30
Chip Boling610117d2021-09-09 11:24:34 -050031var 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 Boling6e27b352020-02-14 09:10:01 -060076func 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
87func packetToString(input []byte) string {
88 return strings.ToLower(hex.EncodeToString(input))
89}
90
91func 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
105func 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 Boling610117d2021-09-09 11:24:34 -0500121func TestOmciCanDecodeAndNextLayer(t *testing.T) {
Chip Boling6e27b352020-02-14 09:10:01 -0600122
Chip Boling610117d2021-09-09 11:24:34 -0500123 baselineString := BaselineIdent.String()
124 assert.NotZero(t, len(baselineString))
Chip Boling6e27b352020-02-14 09:10:01 -0600125
Chip Boling6e27b352020-02-14 09:10:01 -0600126 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 Boling610117d2021-09-09 11:24:34 -0500141 assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType())
142 assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode())
143 assert.Equal(t, LayerTypeCreateRequest, omciMsg.NextLayerType())
Chip Boling6e27b352020-02-14 09:10:01 -0600144
145 msgLayer := packet.Layer(LayerTypeCreateRequest)
146 assert.NotNil(t, msgLayer)
147
148 omciMsg2, ok2 := msgLayer.(*CreateRequest)
149 assert.True(t, ok2)
Chip Boling610117d2021-09-09 11:24:34 -0500150 assert.Equal(t, LayerTypeCreateRequest, omciMsg2.LayerType())
151 assert.Equal(t, LayerTypeCreateRequest, omciMsg2.CanDecode())
152 assert.Equal(t, gopacket.LayerTypePayload, omciMsg2.NextLayerType())
Chip Boling6e27b352020-02-14 09:10:01 -0600153}
154
Chip Boling610117d2021-09-09 11:24:34 -0500155func 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 Boling6e27b352020-02-14 09:10:01 -0600159 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 Boling610117d2021-09-09 11:24:34 -0500165 assert.Nil(t, omciLayer)
Chip Boling6e27b352020-02-14 09:10:01 -0600166
Chip Boling610117d2021-09-09 11:24:34 -0500167 badLayer := packet.Layer(gopacket.LayerTypeDecodeFailure)
168 assert.NotNil(t, badLayer)
169 assert.True(t, packet.Metadata().Truncated)
Chip Boling6e27b352020-02-14 09:10:01 -0600170}
171
Chip Boling610117d2021-09-09 11:24:34 -0500172func 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 Boling6e27b352020-02-14 09:10:01 -0600178 }
Chip Boling610117d2021-09-09 11:24:34 -0500179 msg39 := fmt.Sprintf("%04x%02x0a0002000000000000000000000000000000000000000000000000000000000000000000",
180 uint16(tid), uint8(msgType))
Chip Boling6e27b352020-02-14 09:10:01 -0600181
Chip Boling610117d2021-09-09 11:24:34 -0500182 data, err := stringToPacket(msg39)
Chip Boling6e27b352020-02-14 09:10:01 -0600183 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 Boling610117d2021-09-09 11:24:34 -0500189 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 Boling6e27b352020-02-14 09:10:01 -0600206 assert.NotNil(t, omciLayer)
207
208 omciMsg, ok := omciLayer.(*OMCI)
209 assert.True(t, ok)
Chip Boling610117d2021-09-09 11:24:34 -0500210 assert.Equal(t, uint16(40), omciMsg.Length)
Chip Boling6e27b352020-02-14 09:10:01 -0600211 }
212}
213
Chip Boling610117d2021-09-09 11:24:34 -0500214func TestOmciHeaderExtendedShort(t *testing.T) {
215 // Smallest message possible is an Extended Set Delete request which
216 // is 10 octets.
Chip Boling8a5379a2020-11-17 12:59:40 -0600217
Chip Boling610117d2021-09-09 11:24:34 -0500218 //mibResetRequest := "0001 4F 0A 0002 0000 0000000000000000" +
219 // "00000000000000000000000000000000" +
220 // "000000000000000000000028"
Chip Boling8a5379a2020-11-17 12:59:40 -0600221
Chip Boling8a5379a2020-11-17 12:59:40 -0600222}
223
Chip Boling610117d2021-09-09 11:24:34 -0500224func 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 Boling8a5379a2020-11-17 12:59:40 -0600228
Chip Boling610117d2021-09-09 11:24:34 -0500229 assert.Equal(t, ClassID(425), EthernetFrameExtendedPm64BitClassID)
Chip Boling8a5379a2020-11-17 12:59:40 -0600230
Chip Boling610117d2021-09-09 11:24:34 -0500231 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 Boling8a5379a2020-11-17 12:59:40 -0600237
Chip Boling610117d2021-09-09 11:24:34 -0500238func 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 Boling8a5379a2020-11-17 12:59:40 -0600244 }
Chip Boling610117d2021-09-09 11:24:34 -0500245 request := &DeleteResponse{
246 MeBasePacket: MeBasePacket{
247 EntityClass: ExtendedVlanTaggingOperationConfigurationDataClassID,
248 EntityInstance: uint16(0x202),
Chip Bolingff6bf172020-12-01 10:38:12 -0600249 },
Chip Boling610117d2021-09-09 11:24:34 -0500250 Result: Success,
Chip Bolingff6bf172020-12-01 10:38:12 -0600251 }
Chip Boling610117d2021-09-09 11:24:34 -0500252 // Test serialization back to former string
253 var options gopacket.SerializeOptions
254 options.FixLengths = true
Chip Bolingff6bf172020-12-01 10:38:12 -0600255
Chip Boling610117d2021-09-09 11:24:34 -0500256 buffer := gopacket.NewSerializeBuffer()
257 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
258 assert.Error(t, err)
259}
Chip Bolingff6bf172020-12-01 10:38:12 -0600260
Chip Boling610117d2021-09-09 11:24:34 -0500261func 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 Bolingff6bf172020-12-01 10:38:12 -0600277
Chip Boling610117d2021-09-09 11:24:34 -0500278 buffer := gopacket.NewSerializeBuffer()
279 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
280 assert.Error(t, err)
Chip Bolingff6bf172020-12-01 10:38:12 -0600281}