Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 20 | "encoding/binary" |
| 21 | "reflect" |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 22 | "testing" |
| 23 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 24 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 25 | "github.com/opencord/omci-lib-go/v2" |
| 26 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 27 | "gotest.tools/assert" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func TestCreateMibResetResponse(t *testing.T) { |
| 31 | data, _ := CreateMibResetResponse(1) |
| 32 | |
| 33 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 34 | |
| 35 | assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType) |
| 36 | |
| 37 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse) |
| 38 | msgObj, msgOk := msgLayer.(*omci.MibResetResponse) |
| 39 | if !msgOk { |
| 40 | t.Fail() |
| 41 | } |
| 42 | |
| 43 | assert.Equal(t, msgObj.Result, me.Success) |
| 44 | } |
| 45 | |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 46 | func TestSetTxIdInEncodedPacket(t *testing.T) { |
| 47 | basePkt := []byte{129, 42, 46, 10, 0, 2, 0, 0, 0, 37, 0, 1, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 40, 40, 206, 0, 226} |
| 48 | res := SetTxIdInEncodedPacket(basePkt, uint16(292)) |
| 49 | assert.Equal(t, len(basePkt), len(res)) |
| 50 | |
| 51 | b := res[0:2] |
| 52 | txId := binary.BigEndian.Uint16(b) |
| 53 | assert.Equal(t, uint16(292), txId) |
| 54 | } |
| 55 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 56 | // types for TestCreateMibUploadNextResponse test |
| 57 | type mibArgs struct { |
| 58 | omciPkt gopacket.Packet |
| 59 | omciMsg *omci.OMCI |
| 60 | } |
| 61 | |
| 62 | type mibExpected struct { |
| 63 | messageType omci.MessageType |
| 64 | transactionId uint16 |
| 65 | entityClass me.ClassID |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 66 | entityID uint16 |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | attributes map[string]interface{} |
| 68 | } |
| 69 | |
| 70 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 71 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 72 | mibUploadNext = HexDecode(mibUploadNext) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 73 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 74 | |
| 75 | return mibArgs{ |
| 76 | omciPkt: *mibUploadNextPkt, |
| 77 | omciMsg: mibUploadNextMsg, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 82 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 83 | const uniPortCount = 4 |
| 84 | |
| 85 | var ( |
| 86 | onuDataEntityId = EntityID{0x00, 0x00} |
| 87 | onu2gEntityId = EntityID{0x00, 0x00} |
| 88 | anigEntityId = EntityID{tcontSlotId, aniGId} |
| 89 | ) |
| 90 | |
| 91 | // create a fake mibDb, we only need to test that given a CommandSequenceNumber |
| 92 | // we return the corresponding entry |
| 93 | // the only exception is for OnuData in which we need to replace the MibDataSync attribute with the current value |
| 94 | mibDb := MibDb{ |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 95 | NumberOfBaselineCommands: 4, |
| 96 | baselineItems: []MibDbEntry{}, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 99 | mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{ |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 100 | me.OnuDataClassID, |
| 101 | onuDataEntityId, |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 102 | me.AttributeValueMap{me.OnuData_MibDataSync: 0}, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 103 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 104 | }) |
| 105 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 106 | mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{ |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 107 | me.CircuitPackClassID, |
| 108 | circuitPackEntityID, |
| 109 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 110 | me.CircuitPack_Type: ethernetUnitType, |
| 111 | me.CircuitPack_NumberOfPorts: uniPortCount, |
| 112 | me.CircuitPack_SerialNumber: ToOctets("BBSM-Circuit-Pack", 20), |
| 113 | me.CircuitPack_Version: ToOctets("v0.0.1", 20), |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 114 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 115 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 116 | }) |
| 117 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 118 | mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{ |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 119 | me.AniGClassID, |
| 120 | anigEntityId, |
| 121 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 122 | me.AniG_Arc: 0, |
| 123 | me.AniG_ArcInterval: 0, |
| 124 | me.AniG_Deprecated: 0, |
| 125 | me.AniG_GemBlockLength: 48, |
| 126 | me.AniG_LowerOpticalThreshold: 255, |
| 127 | me.AniG_LowerTransmitPowerThreshold: 129, |
| 128 | me.AniG_OnuResponseTime: 0, |
| 129 | me.AniG_OpticalSignalLevel: 57428, |
| 130 | me.AniG_PiggybackDbaReporting: 0, |
| 131 | me.AniG_SignalDegradeThreshold: 9, |
| 132 | me.AniG_SignalFailThreshold: 5, |
| 133 | me.AniG_SrIndication: 1, |
| 134 | me.AniG_TotalTcontNumber: 8, |
| 135 | me.AniG_TransmitOpticalLevel: 3171, |
| 136 | me.AniG_UpperOpticalThreshold: 255, |
| 137 | me.AniG_UpperTransmitPowerThreshold: 129, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 138 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 139 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 140 | }) |
| 141 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 142 | mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{ |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 143 | me.Onu2GClassID, |
| 144 | onu2gEntityId, |
| 145 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 146 | me.Onu2G_ConnectivityCapability: 127, |
| 147 | me.Onu2G_CurrentConnectivityMode: 0, |
| 148 | me.Onu2G_Deprecated: 1, |
| 149 | me.Onu2G_PriorityQueueScaleFactor: 1, |
| 150 | me.Onu2G_QualityOfServiceQosConfigurationFlexibility: 63, |
| 151 | me.Onu2G_Sysuptime: 0, |
| 152 | me.Onu2G_TotalGemPortIdNumber: 8, |
| 153 | me.Onu2G_TotalPriorityQueueNumber: 64, |
| 154 | me.Onu2G_TotalTrafficSchedulerNumber: 8, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 155 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 156 | nil, |
| 157 | }) |
| 158 | |
| 159 | // create an entry with UnkownAttributes set |
| 160 | var customPktTxId uint16 = 5 |
| 161 | customPkt := []byte{0, byte(customPktTxId), 46, 10, 0, 2, 0, 0, 0, 37, 0, 1, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 40, 40, 206, 0, 226} |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 162 | mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{ |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 163 | me.ClassID(37), |
| 164 | nil, |
| 165 | me.AttributeValueMap{}, |
| 166 | customPkt, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 167 | }) |
| 168 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 169 | tests := []struct { |
| 170 | name string |
| 171 | args mibArgs |
| 172 | want mibExpected |
| 173 | }{ |
| 174 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 175 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{me.OnuData_MibDataSync: uint8(0)}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 176 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 177 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 2, entityID: circuitPackEntityID.ToUint16(), entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{me.CircuitPack_Type: uint8(47), me.CircuitPack_NumberOfPorts: uint8(4)}}}, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 178 | {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 179 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityID: anigEntityId.ToUint16(), entityClass: me.AniGClassID, attributes: map[string]interface{}{me.AniG_GemBlockLength: uint16(48)}}}, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 180 | {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 181 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{me.Onu2G_TotalPriorityQueueNumber: uint16(64)}}}, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 182 | {"mibUploadNext-unknown-attrs", createTestMibUploadNextArgs(t, 5, 4), |
| 183 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: customPktTxId, entityID: 1, entityClass: me.ClassID(37), attributes: map[string]interface{}{"UnknownAttr_1": []uint8{1}}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | for _, tt := range tests { |
| 187 | t.Run(tt.name, func(t *testing.T) { |
| 188 | |
| 189 | // create the packet starting from the mibUploadNextRequest |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 190 | data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, &mibDb) |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 191 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 192 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 193 | |
| 194 | assert.Equal(t, omciMsg.MessageType, tt.want.messageType) |
| 195 | |
| 196 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse) |
| 197 | msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse) |
| 198 | if !msgOk { |
| 199 | t.Fail() |
| 200 | } |
| 201 | |
| 202 | assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 203 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 204 | assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 205 | |
| 206 | for k, v := range tt.want.attributes { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 207 | attr, err := msgObj.ReportedME.GetAttribute(k) |
| 208 | assert.NilError(t, err) |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 209 | if isSlice(attr) { |
| 210 | expected := v.([]uint8) |
| 211 | data := attr.([]uint8) |
| 212 | for i, val := range data { |
| 213 | assert.Equal(t, expected[i], val) |
| 214 | } |
| 215 | } else { |
| 216 | assert.Equal(t, attr, v) |
| 217 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 218 | } |
| 219 | }) |
| 220 | } |
| 221 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 222 | // now try to get a non existing command from the DB anche expect an error |
| 223 | args := createTestMibUploadNextArgs(t, 1, 20) |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 224 | _, err := CreateMibUploadNextResponse(args.omciPkt, args.omciMsg, &mibDb) |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 225 | assert.Error(t, err, "mibdb-does-not-contain-item") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 226 | } |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 227 | |
| 228 | func isSlice(v interface{}) bool { |
| 229 | return reflect.TypeOf(v).Kind() == reflect.Slice |
| 230 | } |