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 | ) |
| 28 | |
| 29 | func testStartSoftwareDownloadRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 30 | instance := uint16(0) // ONU-G |
| 31 | image := uint16(1) |
| 32 | params := me.ParamData{ |
| 33 | EntityID: uint16((instance << 8) + image), |
| 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 | options := meframe.SoftwareOptions{ |
| 42 | WindowSize: uint8(rand.Int31n(255)), // [0, 255] |
| 43 | ImageSize: uint32(rand.Int31n(0x100000) + 0x100000), // [1 Meg, 2M-1] |
| 44 | CircuitPacks: []uint16{0}, // [1 Meg, 2M-1] |
| 45 | } |
| 46 | frame, omciErr := meframe.GenFrame(meInstance, StartSoftwareDownloadRequestType, |
| 47 | meframe.TransactionID(tid), meframe.Software(options), meframe.FrameFormat(messageSet)) |
| 48 | assert.NotNil(t, frame) |
| 49 | assert.NotZero(t, len(frame)) |
| 50 | assert.Nil(t, omciErr) |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////// |
| 53 | // Now decode and compare |
| 54 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 55 | assert.NotNil(t, packet) |
| 56 | |
| 57 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 58 | assert.NotNil(t, omciLayer) |
| 59 | |
| 60 | omciObj, omciOk := omciLayer.(*OMCI) |
| 61 | assert.NotNil(t, omciObj) |
| 62 | assert.True(t, omciOk) |
| 63 | assert.Equal(t, tid, omciObj.TransactionID) |
| 64 | assert.Equal(t, StartSoftwareDownloadRequestType, omciObj.MessageType) |
| 65 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 66 | |
| 67 | msgLayer := packet.Layer(LayerTypeStartSoftwareDownloadRequest) |
| 68 | assert.NotNil(t, msgLayer) |
| 69 | |
| 70 | msgObj, msgOk := msgLayer.(*StartSoftwareDownloadRequest) |
| 71 | assert.NotNil(t, msgObj) |
| 72 | assert.True(t, msgOk) |
| 73 | |
| 74 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 75 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 76 | assert.Equal(t, options.ImageSize, msgObj.ImageSize) |
| 77 | assert.Equal(t, len(options.CircuitPacks), int(msgObj.NumberOfCircuitPacks)) |
| 78 | |
| 79 | for index, circuitPack := range options.CircuitPacks { |
| 80 | assert.Equal(t, circuitPack, msgObj.CircuitPacks[index]) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func testStartSoftwareDownloadResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 85 | // TODO: Implement |
| 86 | } |
| 87 | |
| 88 | func testDownloadSectionRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 89 | // TODO: In future, also support slot/multiple download formats |
| 90 | instance := uint16(0) |
| 91 | image := uint16(rand.Intn(1)) // Image 0 or 1 for this test |
| 92 | params := me.ParamData{ |
| 93 | EntityID: (instance << 8) + image, |
| 94 | } |
| 95 | // Create the managed instance |
| 96 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 97 | assert.NotNil(t, err) |
| 98 | assert.Equal(t, err.StatusCode(), me.Success) |
| 99 | |
| 100 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 101 | var data []byte |
| 102 | |
| 103 | if messageSet == ExtendedIdent { |
| 104 | data = make([]byte, MaxDownloadSectionExtendedLength) |
| 105 | } else { |
| 106 | data = make([]byte, MaxDownloadSectionLength) |
| 107 | } |
| 108 | for index := range data { |
| 109 | data[index] = byte(index & 0xFF) |
| 110 | } |
| 111 | options := meframe.SoftwareOptions{ |
| 112 | SectionNumber: uint8(rand.Int31n(255)), // [0, 255] |
| 113 | Data: data, |
| 114 | } |
| 115 | frame, omciErr := meframe.GenFrame(meInstance, DownloadSectionRequestType, |
| 116 | meframe.TransactionID(tid), meframe.Software(options), meframe.FrameFormat(messageSet)) |
| 117 | assert.NotNil(t, frame) |
| 118 | assert.NotZero(t, len(frame)) |
| 119 | assert.Nil(t, omciErr) |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////// |
| 122 | // Now decode and compare |
| 123 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 124 | assert.NotNil(t, packet) |
| 125 | |
| 126 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 127 | assert.NotNil(t, omciLayer) |
| 128 | |
| 129 | omciObj, omciOk := omciLayer.(*OMCI) |
| 130 | assert.NotNil(t, omciObj) |
| 131 | assert.True(t, omciOk) |
| 132 | assert.Equal(t, tid, omciObj.TransactionID) |
| 133 | assert.Equal(t, DownloadSectionRequestType, omciObj.MessageType) |
| 134 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 135 | |
| 136 | msgLayer := packet.Layer(LayerTypeDownloadSectionRequest) |
| 137 | assert.NotNil(t, msgLayer) |
| 138 | |
| 139 | msgObj, msgOk := msgLayer.(*DownloadSectionRequest) |
| 140 | assert.NotNil(t, msgObj) |
| 141 | assert.True(t, msgOk) |
| 142 | |
| 143 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 144 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 145 | assert.Equal(t, options.SectionNumber, msgObj.SectionNumber) |
| 146 | assert.NotNil(t, msgObj.SectionData) |
| 147 | assert.Equal(t, options.Data, msgObj.SectionData) |
| 148 | } |
| 149 | |
| 150 | func testDownloadSectionResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 151 | instance := uint16(0) |
| 152 | image := uint16(rand.Intn(1)) // Image 0 or 1 for this test |
| 153 | params := me.ParamData{ |
| 154 | EntityID: (instance << 8) + image, |
| 155 | } |
| 156 | // Create the managed instance |
| 157 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 158 | assert.NotNil(t, err) |
| 159 | assert.Equal(t, err.StatusCode(), me.Success) |
| 160 | |
| 161 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 162 | result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested |
| 163 | swOptions := meframe.SoftwareOptions{ |
| 164 | SectionNumber: uint8(rand.Int31n(255)), // [0, 255] |
| 165 | } |
| 166 | var frame []byte |
| 167 | frame, omciErr := meframe.GenFrame(meInstance, DownloadSectionResponseType, meframe.TransactionID(tid), |
| 168 | meframe.Result(result), meframe.FrameFormat(messageSet), meframe.Software(swOptions)) |
| 169 | |
| 170 | assert.NotNil(t, frame) |
| 171 | assert.NotZero(t, len(frame)) |
| 172 | assert.Nil(t, omciErr) |
| 173 | |
| 174 | /////////////////////////////////////////////////////////////////// |
| 175 | // Now decode and compare |
| 176 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 177 | assert.NotNil(t, packet) |
| 178 | |
| 179 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 180 | assert.NotNil(t, omciLayer) |
| 181 | |
| 182 | omciObj, omciOk := omciLayer.(*OMCI) |
| 183 | assert.NotNil(t, omciObj) |
| 184 | assert.True(t, omciOk) |
| 185 | assert.Equal(t, tid, omciObj.TransactionID) |
| 186 | assert.Equal(t, DownloadSectionResponseType, omciObj.MessageType) |
| 187 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 188 | |
| 189 | msgLayer := packet.Layer(LayerTypeDownloadSectionResponse) |
| 190 | assert.NotNil(t, msgLayer) |
| 191 | |
| 192 | msgObj, msgOk := msgLayer.(*DownloadSectionResponse) |
| 193 | assert.NotNil(t, msgObj) |
| 194 | assert.True(t, msgOk) |
| 195 | |
| 196 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 197 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 198 | assert.Equal(t, result, msgObj.Result) |
| 199 | assert.Equal(t, swOptions.SectionNumber, msgObj.SectionNumber) |
| 200 | } |
| 201 | |
| 202 | func testEndSoftwareDownloadRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 203 | // TODO: Implement |
| 204 | } |
| 205 | |
| 206 | func testEndSoftwareDownloadResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 207 | // TODO: Implement |
| 208 | } |
| 209 | |
| 210 | func testActivateSoftwareRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 211 | // TODO: Implement |
| 212 | } |
| 213 | |
| 214 | func testActivateSoftwareResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 215 | // TODO: Implement |
| 216 | } |
| 217 | |
| 218 | func testCommitSoftwareRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 219 | // TODO: Implement |
| 220 | } |
| 221 | |
| 222 | func testCommitSoftwareResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 223 | // TODO: Implement |
| 224 | } |