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 testDeleteRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 30 | // Generate the frame. Use a default Entity ID of zero, but for the |
| 31 | // OMCI library, we need to specify all supported Set-By-Create |
| 32 | params := me.ParamData{ |
| 33 | EntityID: uint16(0), |
| 34 | } |
| 35 | // Create the managed instance |
| 36 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 37 | assert.NotNil(t, err) |
| 38 | assert.Equal(t, err.StatusCode(), me.Success) |
| 39 | |
| 40 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 41 | |
| 42 | frame, omciErr := meframe.GenFrame(meInstance, DeleteRequestType, meframe.TransactionID(tid), meframe.FrameFormat(messageSet)) |
| 43 | assert.NotNil(t, frame) |
| 44 | assert.NotZero(t, len(frame)) |
| 45 | assert.Nil(t, omciErr) |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////// |
| 48 | // Now decode and compare |
| 49 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 50 | assert.NotNil(t, packet) |
| 51 | |
| 52 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 53 | assert.NotNil(t, omciLayer) |
| 54 | |
| 55 | omciObj, omciOk := omciLayer.(*OMCI) |
| 56 | assert.NotNil(t, omciObj) |
| 57 | assert.True(t, omciOk) |
| 58 | assert.Equal(t, tid, omciObj.TransactionID) |
| 59 | assert.Equal(t, DeleteRequestType, omciObj.MessageType) |
| 60 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 61 | |
| 62 | msgLayer := packet.Layer(LayerTypeDeleteRequest) |
| 63 | assert.NotNil(t, msgLayer) |
| 64 | |
| 65 | msgObj, msgOk := msgLayer.(*DeleteRequest) |
| 66 | assert.NotNil(t, msgObj) |
| 67 | assert.True(t, msgOk) |
| 68 | |
| 69 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 70 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 71 | } |
| 72 | |
| 73 | func testDeleteResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 74 | params := me.ParamData{ |
| 75 | EntityID: uint16(0), |
| 76 | } |
| 77 | // Create the managed instance |
| 78 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 79 | assert.NotNil(t, err) |
| 80 | assert.Equal(t, err.StatusCode(), me.Success) |
| 81 | |
| 82 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 83 | result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested |
| 84 | |
| 85 | frame, omciErr := meframe.GenFrame(meInstance, DeleteResponseType, meframe.TransactionID(tid), meframe.Result(result), |
| 86 | meframe.FrameFormat(messageSet)) |
| 87 | assert.NotNil(t, frame) |
| 88 | assert.NotZero(t, len(frame)) |
| 89 | assert.Nil(t, omciErr) |
| 90 | |
| 91 | /////////////////////////////////////////////////////////////////// |
| 92 | // Now decode and compare |
| 93 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 94 | assert.NotNil(t, packet) |
| 95 | |
| 96 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 97 | assert.NotNil(t, omciLayer) |
| 98 | |
| 99 | omciObj, omciOk := omciLayer.(*OMCI) |
| 100 | assert.NotNil(t, omciObj) |
| 101 | assert.True(t, omciOk) |
| 102 | assert.Equal(t, tid, omciObj.TransactionID) |
| 103 | assert.Equal(t, DeleteResponseType, omciObj.MessageType) |
| 104 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 105 | |
| 106 | msgLayer := packet.Layer(LayerTypeDeleteResponse) |
| 107 | assert.NotNil(t, msgLayer) |
| 108 | |
| 109 | msgObj, msgOk := msgLayer.(*DeleteResponse) |
| 110 | assert.NotNil(t, msgObj) |
| 111 | assert.True(t, msgOk) |
| 112 | |
| 113 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 114 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 115 | assert.Equal(t, result, msgObj.Result) |
| 116 | } |