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 ( |
| 20 | "fmt" |
| 21 | "github.com/google/gopacket" |
| 22 | "github.com/opencord/omci-lib-go" |
| 23 | me "github.com/opencord/omci-lib-go/generated" |
| 24 | "gotest.tools/assert" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | func TestCreateMibResetResponse(t *testing.T) { |
| 29 | data, _ := CreateMibResetResponse(1) |
| 30 | |
| 31 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 32 | |
| 33 | assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType) |
| 34 | |
| 35 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse) |
| 36 | msgObj, msgOk := msgLayer.(*omci.MibResetResponse) |
| 37 | if !msgOk { |
| 38 | t.Fail() |
| 39 | } |
| 40 | |
| 41 | assert.Equal(t, msgObj.Result, me.Success) |
| 42 | } |
| 43 | |
| 44 | // types for TestCreateMibUploadNextResponse test |
| 45 | type mibArgs struct { |
| 46 | omciPkt gopacket.Packet |
| 47 | omciMsg *omci.OMCI |
| 48 | } |
| 49 | |
| 50 | type mibExpected struct { |
| 51 | messageType omci.MessageType |
| 52 | transactionId uint16 |
| 53 | entityClass me.ClassID |
| 54 | attributes map[string]interface{} |
| 55 | } |
| 56 | |
| 57 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 58 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
| 59 | mibUploadNext = hexDecode(mibUploadNext) |
| 60 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 61 | |
| 62 | return mibArgs{ |
| 63 | omciPkt: *mibUploadNextPkt, |
| 64 | omciMsg: mibUploadNextMsg, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 69 | |
| 70 | tests := []struct { |
| 71 | name string |
| 72 | args mibArgs |
| 73 | want mibExpected |
| 74 | }{ |
| 75 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
| 76 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityClass: me.OnuDataClassID, attributes: map[string]interface{}{"MibDataSync": uint8(0)}}}, |
| 77 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
| 78 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 2, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"Type": uint8(47), "NumberOfPorts": uint8(4)}}}, |
| 79 | {"mibUploadNext-4", createTestMibUploadNextArgs(t, 3, 4), |
| 80 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"PowerShedOverride": uint32(0)}}}, |
| 81 | {"mibUploadNext-10", createTestMibUploadNextArgs(t, 4, 10), |
| 82 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"SensedType": uint8(47)}}}, |
| 83 | } |
| 84 | |
| 85 | for _, tt := range tests { |
| 86 | t.Run(tt.name, func(t *testing.T) { |
| 87 | |
| 88 | // create the packet starting from the mibUploadNextRequest |
| 89 | data, _ := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg) |
| 90 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 91 | |
| 92 | assert.Equal(t, omciMsg.MessageType, tt.want.messageType) |
| 93 | |
| 94 | msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse) |
| 95 | msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse) |
| 96 | if !msgOk { |
| 97 | t.Fail() |
| 98 | } |
| 99 | |
| 100 | assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid |
| 101 | // GetAttribute("ManagedEntityId") returns nil, |
| 102 | // msgObj.EntityClass is always OnuDataClassID |
| 103 | // how do we check this? |
| 104 | //meId, _ := msgObj.ReportedME.GetAttribute("ManagedEntityId") |
| 105 | //assert.Equal(t, meId, tt.want.entityClass) |
| 106 | //assert.Equal(t, msgObj.EntityClass, tt.want.entityClass) |
| 107 | |
| 108 | fmt.Println(msgObj.EntityInstance, msgObj.ReportedME.GetEntityID()) |
| 109 | |
| 110 | for k, v := range tt.want.attributes { |
| 111 | attr, _ := msgObj.ReportedME.GetAttribute(k) |
| 112 | assert.Equal(t, attr, v) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | } |