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 | "github.com/google/gopacket" |
| 21 | "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
| 23 | "gotest.tools/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) { |
| 28 | packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy) |
| 29 | if packet == nil { |
| 30 | t.Fatal("could not decode rxMsg as OMCI") |
| 31 | } |
| 32 | omciLayer := packet.Layer(omci.LayerTypeOMCI) |
| 33 | if omciLayer == nil { |
| 34 | t.Fatal("could not decode omci layer") |
| 35 | } |
| 36 | omciMsg, ok := omciLayer.(*omci.OMCI) |
| 37 | if !ok { |
| 38 | t.Fatal("could not assign omci layer") |
| 39 | } |
| 40 | return omciMsg, &packet |
| 41 | } |
| 42 | |
| 43 | func omciToGetResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.GetResponse { |
| 44 | msgLayer := (*omciPkt).Layer(omci.LayerTypeGetResponse) |
| 45 | if msgLayer == nil { |
| 46 | t.Fatal("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped") |
| 47 | } |
| 48 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 49 | if !msgOk { |
| 50 | t.Fatal("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped") |
| 51 | } |
| 52 | return msgObj |
| 53 | } |
| 54 | |
| 55 | func TestCreateOnu2gResponse(t *testing.T) { |
| 56 | response := createOnu2gResponse(40960, 1) |
| 57 | data, _ := serialize(omci.GetResponseType, response, 1) |
| 58 | |
| 59 | // emulate the openonu-go behavior: |
| 60 | // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback |
| 61 | // in the GetResponse case omci_cc.receiveOmciResponse |
| 62 | // then the OmciMessage (gopacket + OMIC layer) is is published on a channel |
| 63 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 64 | |
| 65 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 66 | |
| 67 | // that is read by myb_sync.processMibSyncMessages |
| 68 | // the myb_sync.handleOmciMessage is called and then |
| 69 | // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer |
| 70 | getResponseLayer := omciToGetResponse(t, omciPkt) |
| 71 | |
| 72 | assert.Equal(t, getResponseLayer.Result, me.Success) |
| 73 | } |
| 74 | |
| 75 | func TestCreateOnugResponse(t *testing.T) { |
| 76 | response := createOnugResponse(40960, 1) |
| 77 | data, _ := serialize(omci.GetResponseType, response, 1) |
| 78 | |
| 79 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 80 | |
| 81 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 82 | |
| 83 | getResponseLayer := omciToGetResponse(t, omciPkt) |
| 84 | |
| 85 | assert.Equal(t, getResponseLayer.Result, me.Success) |
| 86 | } |