Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 20 | "github.com/google/gopacket" |
| 21 | "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
| 23 | "gotest.tools/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 27 | // used to verify that the first message in the MIB Sync (OnuData) |
| 28 | // reports the correct MDS |
| 29 | const MDS = uint8(128) |
| 30 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 31 | func TestCreateMibResetResponse(t *testing.T) { |
| 32 | data, _ := CreateMibResetResponse(1) |
| 33 | |
| 34 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 35 | |
| 36 | assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType) |
| 37 | |
| 38 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse) |
| 39 | msgObj, msgOk := msgLayer.(*omci.MibResetResponse) |
| 40 | if !msgOk { |
| 41 | t.Fail() |
| 42 | } |
| 43 | |
| 44 | assert.Equal(t, msgObj.Result, me.Success) |
| 45 | } |
| 46 | |
| 47 | // types for TestCreateMibUploadNextResponse test |
| 48 | type mibArgs struct { |
| 49 | omciPkt gopacket.Packet |
| 50 | omciMsg *omci.OMCI |
| 51 | } |
| 52 | |
| 53 | type mibExpected struct { |
| 54 | messageType omci.MessageType |
| 55 | transactionId uint16 |
| 56 | entityClass me.ClassID |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 57 | entityID uint16 |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 58 | attributes map[string]interface{} |
| 59 | } |
| 60 | |
| 61 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 62 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
| 63 | mibUploadNext = hexDecode(mibUploadNext) |
| 64 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 65 | |
| 66 | return mibArgs{ |
| 67 | omciPkt: *mibUploadNextPkt, |
| 68 | omciMsg: mibUploadNextMsg, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 73 | |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | args mibArgs |
| 77 | want mibExpected |
| 78 | }{ |
| 79 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 80 | mibExpected{messageType: omci.MibUploadNextResponseType, entityID: 0, transactionId: 1, entityClass: me.OnuDataClassID, attributes: map[string]interface{}{"MibDataSync": MDS}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 81 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 82 | mibExpected{messageType: omci.MibUploadNextResponseType, entityID: 0, transactionId: 2, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"Type": uint8(47), "NumberOfPorts": uint8(4)}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 83 | {"mibUploadNext-4", createTestMibUploadNextArgs(t, 3, 4), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 84 | mibExpected{messageType: omci.MibUploadNextResponseType, entityID: 0, transactionId: 3, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"PowerShedOverride": uint32(0)}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 85 | {"mibUploadNext-10", createTestMibUploadNextArgs(t, 4, 10), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 86 | mibExpected{messageType: omci.MibUploadNextResponseType, entityID: 258, transactionId: 4, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"SensedType": uint8(47)}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | for _, tt := range tests { |
| 90 | t.Run(tt.name, func(t *testing.T) { |
| 91 | |
| 92 | // create the packet starting from the mibUploadNextRequest |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 93 | data, _ := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 94 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 95 | |
| 96 | assert.Equal(t, omciMsg.MessageType, tt.want.messageType) |
| 97 | |
| 98 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse) |
| 99 | msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse) |
| 100 | if !msgOk { |
| 101 | t.Fail() |
| 102 | } |
| 103 | |
| 104 | assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 105 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame^] | 106 | assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 107 | |
| 108 | for k, v := range tt.want.attributes { |
| 109 | attr, _ := msgObj.ReportedME.GetAttribute(k) |
| 110 | assert.Equal(t, attr, v) |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | } |