blob: a22e7ef1e0cde5ea31332cac6e604aa0a9285a81 [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 */
17package omci_test
18
19import (
Chip Boling34ebcb62021-02-02 12:13:58 -060020 "github.com/google/gopacket"
Andrea Campanella7167ebb2020-02-24 09:56:38 +010021 . "github.com/opencord/omci-lib-go"
22 me "github.com/opencord/omci-lib-go/generated"
Chip Boling6e27b352020-02-14 09:10:01 -060023 "github.com/stretchr/testify/assert"
24 "strings"
25 "testing"
26)
27
28var 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
53var 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
100var 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
111func 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
123func 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
133func 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
142func 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
202func 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 Boling610117d2021-09-09 11:24:34 -0500255func TestGetSoftwareImageResponseDecode(t *testing.T) {
256 goodMessage := "035e290a00070000000400000102030405060708090a0b0c0d0e0f0000000000000000000000000000000028"
Chip Boling6e27b352020-02-14 09:10:01 -0600257 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 Boling610117d2021-09-09 11:24:34 -0500280 assert.Equal(t, response.AttributeMask, uint16(0x0400))
281 assert.Equal(t, response.Attributes["ImageHash"], toOctets("AAECAwQFBgcICQoLDA0ODw==")) // 0->f octets)
Chip Boling6e27b352020-02-14 09:10:01 -0600282
283 // Verify string output for message
284 packetString := packet.String()
285 assert.NotZero(t, len(packetString))
286}
287
Chip Boling610117d2021-09-09 11:24:34 -0500288func TestGetSoftwareImageResponseSerialize(t *testing.T) {
289 goodMessage := "035e290a00070000000400000102030405060708090a0b0c0d0e0f0000000000000000000000000000000028"
Chip Boling6e27b352020-02-14 09:10:01 -0600290 omciLayer := &OMCI{
291 TransactionID: 0x035e,
292 MessageType: GetResponseType,
Chip Boling6e27b352020-02-14 09:10:01 -0600293 }
294 request := &GetResponse{
295 MeBasePacket: MeBasePacket{
Chip Boling610117d2021-09-09 11:24:34 -0500296 EntityClass: me.SoftwareImageClassID,
Chip Boling6e27b352020-02-14 09:10:01 -0600297 EntityInstance: uint16(0),
298 },
299 Result: 0,
Chip Boling6e27b352020-02-14 09:10:01 -0600300 AttributeMask: uint16(0x0400),
Chip Boling6e27b352020-02-14 09:10:01 -0600301 Attributes: me.AttributeValueMap{
Chip Boling610117d2021-09-09 11:24:34 -0500302 "ImageHash": toOctets("AAECAwQFBgcICQoLDA0ODw==")}, // 0->f octets
Chip Boling6e27b352020-02-14 09:10:01 -0600303 }
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}