-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmebase_test.go
158 lines (133 loc) · 5.02 KB
/
mebase_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci_test
import (
. "github.com/cboling/omci/v2"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/stretchr/testify/assert"
"testing"
)
var buffer []byte
func simpleMock(t *testing.T) *MeBasePacket {
mibResetRequest := "00014F0A000200000000000000000000" +
"00000000000000000000000000000000" +
"000000000000000000000028"
data, err := stringToPacket(mibResetRequest)
assert.Nil(t, err)
assert.NotNil(t, data)
return &MeBasePacket{
EntityClass: 0x02,
EntityInstance: 0x00,
Layer: nil,
BaseLayer: layers.BaseLayer{},
MsgLayerType: LayerTypeMibResetRequest,
}
}
func TestNextIsNil(t *testing.T) {
mock := simpleMock(t)
assert.Equal(t, mock.NextLayerType(), gopacket.LayerTypeZero)
}
func TestPayloadAlwaysNil(t *testing.T) {
mock := simpleMock(t)
assert.Nil(t, mock.LayerPayload())
}
func TestMsgCanBeDecoded(t *testing.T) {
mock := simpleMock(t)
assert.Equal(t, mock.CanDecode(), mock.MsgLayerType)
}
func TestDecodesFrameNoTrailer(t *testing.T) {
// No baseline trailer. Which is okay. Earlier library release depended on at
// least the length field being present but we know baseline is always 40 bytes of payload
getAllAlarmsRequest := "04454b0a000200000000000000000000000000000000000000000000000000000000000000000000"
getAllAlarmsResponse := "04452b0a000200000003000000000000000000000000000000000000000000000000000000000000"
getAllAlarmsNextRequest := "02344c0a000200000003000000000000000000000000000000000000000000000000000000000000"
getAllAlarmsNextResponse := "02342c0a00020000000b010280000000000000000000000000000000000000000000000000000000"
alarmNotification := "0000100a000b01048000000000000000000000000000000000000000000000000000000000000005"
frames := []string{
getAllAlarmsRequest,
getAllAlarmsResponse,
getAllAlarmsNextRequest,
getAllAlarmsNextResponse,
alarmNotification,
}
for _, frame := range frames {
data, err := stringToPacket(frame)
assert.NoError(t, err)
packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
assert.NotNil(t, packet)
omciLayer := packet.Layer(LayerTypeOMCI)
assert.NotNil(t, omciLayer)
}
}
func TestDecodesFrameTooSmall(t *testing.T) {
// Less than 4 (so cannot determine message set)
veryShort := "04454b"
// Baseline message set checks (only 39 bytes)
// Extended message set checks (only 1 octet of 2 byte length field)
getAllAlarmsRequest := "04454b0a0002000000000000000000000000000000000000000000000000000000000000000000"
getAllAlarmsRequestExt := "04454b0b0002000000"
getAllAlarmsResponse := "04452b0a0002000000030000000000000000000000000000000000000000000000000000000000"
getAllAlarmsResponseExt := "04452b0b0002000000"
getAllAlarmsNextRequest := "02344c0a0002000000030000000000000000000000000000000000000000000000000000000000"
getAllAlarmsNextRequestExt := "02342c0b0002000000"
getAllAlarmsNextResponse := "02342c0a00020000000b0102800000000000000000000000000000000000000000000000000000"
getAllAlarmsNextResponseExt := "02342c0b0002000000"
alarmNotification := "0000100a000b010480000000000000000000000000000000000000000000000000000000000000"
alarmNotificationExt := "0000100b000b010400"
frames := []string{
veryShort,
getAllAlarmsRequest,
getAllAlarmsResponse,
getAllAlarmsNextRequest,
getAllAlarmsNextResponse,
alarmNotification,
getAllAlarmsRequestExt,
getAllAlarmsResponseExt,
getAllAlarmsNextRequestExt,
getAllAlarmsNextResponseExt,
alarmNotificationExt,
}
for _, frame := range frames {
data, err := stringToPacket(frame)
assert.NoError(t, err)
// Should get packet but with error layer
packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
assert.NotNil(t, packet)
errLayer := packet.ErrorLayer()
assert.NotNil(t, errLayer)
metaData := packet.Metadata()
assert.NotNil(t, metaData)
assert.True(t, metaData.Truncated)
omciLayer := packet.Layer(LayerTypeOMCI)
assert.Nil(t, omciLayer)
}
}
func TestFrameWithUnknownMessageType(t *testing.T) {
frame := "00010b0a000200000000000000000000000000000000000000000000000000000000000000000000"
data, err := stringToPacket(frame)
assert.NoError(t, err)
packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy)
assert.NotNil(t, packet)
errLayer := packet.ErrorLayer()
assert.NotNil(t, errLayer)
metaData := packet.Metadata()
assert.NotNil(t, metaData)
assert.False(t, metaData.Truncated)
omciLayer := packet.Layer(LayerTypeOMCI)
assert.NotNil(t, omciLayer)
}