Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 8 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 10 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 16 | */ |
| 17 | package omci_test |
| 18 | |
| 19 | import ( |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 20 | "github.com/google/gopacket" |
| 21 | "github.com/google/gopacket/layers" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 22 | . "github.com/opencord/omci-lib-go/v2" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | var buffer []byte |
| 28 | |
| 29 | func simpleMock(t *testing.T) *MeBasePacket { |
| 30 | mibResetRequest := "00014F0A000200000000000000000000" + |
| 31 | "00000000000000000000000000000000" + |
| 32 | "000000000000000000000028" |
| 33 | data, err := stringToPacket(mibResetRequest) |
| 34 | assert.Nil(t, err) |
| 35 | assert.NotNil(t, data) |
| 36 | |
| 37 | return &MeBasePacket{ |
| 38 | EntityClass: 0x02, |
| 39 | EntityInstance: 0x00, |
| 40 | Layer: nil, |
| 41 | BaseLayer: layers.BaseLayer{}, |
| 42 | MsgLayerType: LayerTypeMibResetRequest, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestNextIsNil(t *testing.T) { |
| 47 | mock := simpleMock(t) |
| 48 | assert.Equal(t, mock.NextLayerType(), gopacket.LayerTypeZero) |
| 49 | } |
| 50 | |
| 51 | func TestPayloadAlwaysNil(t *testing.T) { |
| 52 | mock := simpleMock(t) |
| 53 | assert.Nil(t, mock.LayerPayload()) |
| 54 | } |
| 55 | |
| 56 | func TestMsgCanBeDecoded(t *testing.T) { |
| 57 | mock := simpleMock(t) |
| 58 | assert.Equal(t, mock.CanDecode(), mock.MsgLayerType) |
| 59 | } |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 60 | |
| 61 | func TestDecodesFrameNoTrailer(t *testing.T) { |
| 62 | // No baseline trailer. Which is okay. Earlier library release depended on at |
| 63 | // least the length field being present but we know baseline is always 40 bytes of payload |
| 64 | |
| 65 | getAllAlarmsRequest := "04454b0a000200000000000000000000000000000000000000000000000000000000000000000000" |
| 66 | getAllAlarmsResponse := "04452b0a000200000003000000000000000000000000000000000000000000000000000000000000" |
| 67 | |
| 68 | getAllAlarmsNextRequest := "02344c0a000200000003000000000000000000000000000000000000000000000000000000000000" |
| 69 | getAllAlarmsNextResponse := "02342c0a00020000000b010280000000000000000000000000000000000000000000000000000000" |
| 70 | |
| 71 | alarmNotification := "0000100a000b01048000000000000000000000000000000000000000000000000000000000000005" |
| 72 | |
| 73 | frames := []string{ |
| 74 | getAllAlarmsRequest, |
| 75 | getAllAlarmsResponse, |
| 76 | getAllAlarmsNextRequest, |
| 77 | getAllAlarmsNextResponse, |
| 78 | alarmNotification, |
| 79 | } |
| 80 | for _, frame := range frames { |
| 81 | data, err := stringToPacket(frame) |
| 82 | assert.NoError(t, err) |
| 83 | |
| 84 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 85 | assert.NotNil(t, packet) |
| 86 | |
| 87 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 88 | assert.NotNil(t, omciLayer) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestDecodesFrameTooSmall(t *testing.T) { |
| 93 | // Less than 4 (so cannot determine message set) |
| 94 | veryShort := "04454b" |
| 95 | |
| 96 | // Baseline message set checks (only 39 bytes) |
| 97 | // Extended message set checks (only 1 octet of 2 byte length field) |
| 98 | getAllAlarmsRequest := "04454b0a0002000000000000000000000000000000000000000000000000000000000000000000" |
| 99 | getAllAlarmsRequestExt := "04454b0b0002000000" |
| 100 | getAllAlarmsResponse := "04452b0a0002000000030000000000000000000000000000000000000000000000000000000000" |
| 101 | getAllAlarmsResponseExt := "04452b0b0002000000" |
| 102 | |
| 103 | getAllAlarmsNextRequest := "02344c0a0002000000030000000000000000000000000000000000000000000000000000000000" |
| 104 | getAllAlarmsNextRequestExt := "02342c0b0002000000" |
| 105 | getAllAlarmsNextResponse := "02342c0a00020000000b0102800000000000000000000000000000000000000000000000000000" |
| 106 | getAllAlarmsNextResponseExt := "02342c0b0002000000" |
| 107 | |
| 108 | alarmNotification := "0000100a000b010480000000000000000000000000000000000000000000000000000000000000" |
| 109 | alarmNotificationExt := "0000100b000b010400" |
| 110 | |
| 111 | frames := []string{ |
| 112 | veryShort, |
| 113 | getAllAlarmsRequest, |
| 114 | getAllAlarmsResponse, |
| 115 | getAllAlarmsNextRequest, |
| 116 | getAllAlarmsNextResponse, |
| 117 | alarmNotification, |
| 118 | getAllAlarmsRequestExt, |
| 119 | getAllAlarmsResponseExt, |
| 120 | getAllAlarmsNextRequestExt, |
| 121 | getAllAlarmsNextResponseExt, |
| 122 | alarmNotificationExt, |
| 123 | } |
| 124 | for _, frame := range frames { |
| 125 | data, err := stringToPacket(frame) |
| 126 | assert.NoError(t, err) |
| 127 | |
| 128 | // Should get packet but with error layer |
| 129 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 130 | assert.NotNil(t, packet) |
| 131 | errLayer := packet.ErrorLayer() |
| 132 | assert.NotNil(t, errLayer) |
| 133 | metaData := packet.Metadata() |
| 134 | assert.NotNil(t, metaData) |
| 135 | assert.True(t, metaData.Truncated) |
| 136 | |
| 137 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 138 | assert.Nil(t, omciLayer) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestFrameWithUnknownMessageType(t *testing.T) { |
| 143 | frame := "00010b0a000200000000000000000000000000000000000000000000000000000000000000000000" |
| 144 | |
| 145 | data, err := stringToPacket(frame) |
| 146 | assert.NoError(t, err) |
| 147 | |
| 148 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 149 | assert.NotNil(t, packet) |
| 150 | errLayer := packet.ErrorLayer() |
| 151 | assert.NotNil(t, errLayer) |
| 152 | metaData := packet.Metadata() |
| 153 | assert.NotNil(t, metaData) |
| 154 | assert.False(t, metaData.Truncated) |
| 155 | |
| 156 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 157 | assert.NotNil(t, omciLayer) |
| 158 | } |