Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
| 5 | * 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 |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * 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. |
| 16 | */ |
| 17 | package meframe_test |
| 18 | |
| 19 | import ( |
| 20 | "github.com/google/gopacket" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 21 | . "github.com/opencord/omci-lib-go/v2" |
| 22 | me "github.com/opencord/omci-lib-go/v2/generated" |
| 23 | "github.com/opencord/omci-lib-go/v2/meframe" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
| 25 | "math/rand" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func testMibResetRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 30 | params := me.ParamData{ |
| 31 | EntityID: uint16(0), |
| 32 | } |
| 33 | // Create the managed instance |
| 34 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 35 | assert.NotNil(t, err) |
| 36 | assert.Equal(t, err.StatusCode(), me.Success) |
| 37 | |
| 38 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 39 | |
| 40 | frame, omciErr := meframe.GenFrame(meInstance, MibResetRequestType, meframe.TransactionID(tid), meframe.FrameFormat(messageSet)) |
| 41 | assert.NotNil(t, frame) |
| 42 | assert.NotZero(t, len(frame)) |
| 43 | assert.Nil(t, omciErr) |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////// |
| 46 | // Now decode and compare |
| 47 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 48 | assert.NotNil(t, packet) |
| 49 | |
| 50 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 51 | assert.NotNil(t, omciLayer) |
| 52 | |
| 53 | omciObj, omciOk := omciLayer.(*OMCI) |
| 54 | assert.NotNil(t, omciObj) |
| 55 | assert.True(t, omciOk) |
| 56 | assert.Equal(t, tid, omciObj.TransactionID) |
| 57 | assert.Equal(t, MibResetRequestType, omciObj.MessageType) |
| 58 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 59 | |
| 60 | msgLayer := packet.Layer(LayerTypeMibResetRequest) |
| 61 | assert.NotNil(t, msgLayer) |
| 62 | |
| 63 | msgObj, msgOk := msgLayer.(*MibResetRequest) |
| 64 | assert.NotNil(t, msgObj) |
| 65 | assert.True(t, msgOk) |
| 66 | |
| 67 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 68 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 69 | } |
| 70 | |
| 71 | func testMibResetResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 72 | params := me.ParamData{ |
| 73 | EntityID: uint16(0), |
| 74 | } |
| 75 | // Create the managed instance |
| 76 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 77 | assert.NotNil(t, err) |
| 78 | assert.Equal(t, err.StatusCode(), me.Success) |
| 79 | |
| 80 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 81 | result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested |
| 82 | |
| 83 | frame, omciErr := meframe.GenFrame(meInstance, MibResetResponseType, meframe.TransactionID(tid), |
| 84 | meframe.Result(result), meframe.FrameFormat(messageSet)) |
| 85 | assert.NotNil(t, frame) |
| 86 | assert.NotZero(t, len(frame)) |
| 87 | assert.Nil(t, omciErr) |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////// |
| 90 | // Now decode and compare |
| 91 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 92 | assert.NotNil(t, packet) |
| 93 | |
| 94 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 95 | assert.NotNil(t, omciLayer) |
| 96 | |
| 97 | omciObj, omciOk := omciLayer.(*OMCI) |
| 98 | assert.NotNil(t, omciObj) |
| 99 | assert.True(t, omciOk) |
| 100 | assert.Equal(t, tid, omciObj.TransactionID) |
| 101 | assert.Equal(t, MibResetResponseType, omciObj.MessageType) |
| 102 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 103 | |
| 104 | msgLayer := packet.Layer(LayerTypeMibResetResponse) |
| 105 | assert.NotNil(t, msgLayer) |
| 106 | |
| 107 | msgObj, msgOk := msgLayer.(*MibResetResponse) |
| 108 | assert.NotNil(t, msgObj) |
| 109 | assert.True(t, msgOk) |
| 110 | |
| 111 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 112 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 113 | assert.Equal(t, result, msgObj.Result) |
| 114 | } |