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" |
| 21 | . "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
| 23 | "github.com/opencord/omci-lib-go/meframe" |
| 24 | "github.com/stretchr/testify/assert" |
| 25 | "math/rand" |
| 26 | "testing" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | func testSynchronizeTimeRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 31 | params := me.ParamData{ |
| 32 | EntityID: uint16(0), |
| 33 | } |
| 34 | // Create the managed instance |
| 35 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 36 | assert.NotNil(t, err) |
| 37 | assert.Equal(t, err.StatusCode(), me.Success) |
| 38 | |
| 39 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 40 | tm := time.Now().UTC() |
| 41 | tmUnix := tm.Unix() |
| 42 | |
| 43 | frame, omciErr := meframe.GenFrame(meInstance, SynchronizeTimeRequestType, meframe.TransactionID(tid), |
| 44 | meframe.Payload(tmUnix), meframe.FrameFormat(messageSet)) |
| 45 | assert.NotNil(t, frame) |
| 46 | assert.NotZero(t, len(frame)) |
| 47 | assert.Nil(t, omciErr) |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////// |
| 50 | // Now decode and compare |
| 51 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 52 | assert.NotNil(t, packet) |
| 53 | |
| 54 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 55 | assert.NotNil(t, omciLayer) |
| 56 | |
| 57 | omciObj, omciOk := omciLayer.(*OMCI) |
| 58 | assert.NotNil(t, omciObj) |
| 59 | assert.True(t, omciOk) |
| 60 | assert.Equal(t, tid, omciObj.TransactionID) |
| 61 | assert.Equal(t, SynchronizeTimeRequestType, omciObj.MessageType) |
| 62 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 63 | |
| 64 | msgLayer := packet.Layer(LayerTypeSynchronizeTimeRequest) |
| 65 | assert.NotNil(t, msgLayer) |
| 66 | |
| 67 | msgObj, msgOk := msgLayer.(*SynchronizeTimeRequest) |
| 68 | assert.NotNil(t, msgObj) |
| 69 | assert.True(t, msgOk) |
| 70 | |
| 71 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 72 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 73 | |
| 74 | assert.Equal(t, uint16(tm.Year()), msgObj.Year) |
| 75 | assert.Equal(t, uint8(tm.Month()), msgObj.Month) |
| 76 | assert.Equal(t, uint8(tm.Day()), msgObj.Day) |
| 77 | assert.Equal(t, uint8(tm.Hour()), msgObj.Hour) |
| 78 | assert.Equal(t, uint8(tm.Minute()), msgObj.Minute) |
| 79 | assert.Equal(t, uint8(tm.Second()), msgObj.Second) |
| 80 | } |
| 81 | |
| 82 | func testSynchronizeTimeResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 83 | params := me.ParamData{ |
| 84 | EntityID: uint16(0), |
| 85 | } |
| 86 | // Create the managed instance |
| 87 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 88 | assert.NotNil(t, err) |
| 89 | assert.Equal(t, err.StatusCode(), me.Success) |
| 90 | |
| 91 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 92 | result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested |
| 93 | successResult := uint8(rand.Int31n(2)) // [0, 1] |
| 94 | |
| 95 | var frame []byte |
| 96 | frame, omciErr := meframe.GenFrame(meInstance, SynchronizeTimeResponseType, meframe.TransactionID(tid), |
| 97 | meframe.Result(result), meframe.SuccessResult(successResult), meframe.FrameFormat(messageSet)) |
| 98 | assert.NotNil(t, frame) |
| 99 | assert.NotZero(t, len(frame)) |
| 100 | assert.Nil(t, omciErr) |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////// |
| 103 | // Now decode and compare |
| 104 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 105 | assert.NotNil(t, packet) |
| 106 | |
| 107 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 108 | assert.NotNil(t, omciLayer) |
| 109 | |
| 110 | omciObj, omciOk := omciLayer.(*OMCI) |
| 111 | assert.NotNil(t, omciObj) |
| 112 | assert.True(t, omciOk) |
| 113 | assert.Equal(t, tid, omciObj.TransactionID) |
| 114 | assert.Equal(t, SynchronizeTimeResponseType, omciObj.MessageType) |
| 115 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 116 | |
| 117 | msgLayer := packet.Layer(LayerTypeSynchronizeTimeResponse) |
| 118 | assert.NotNil(t, msgLayer) |
| 119 | |
| 120 | msgObj, msgOk := msgLayer.(*SynchronizeTimeResponse) |
| 121 | assert.NotNil(t, msgObj) |
| 122 | assert.True(t, msgOk) |
| 123 | |
| 124 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 125 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 126 | assert.Equal(t, result, msgObj.Result) |
| 127 | if result == me.Success { |
| 128 | assert.Equal(t, successResult, msgObj.SuccessResults) |
| 129 | } else { |
| 130 | assert.Zero(t, msgObj.SuccessResults) |
| 131 | } |
| 132 | } |