Newer
Older
package packet
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecodePathAttrs(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected *PathAttribute
}{
{
name: "Valid attribute set",
input: []byte{
0, // Attr. Flags
1, // Attr. Type Code
1, // Attr. Length
1, // EGP
0, // Attr. Flags
3, // Next Hop
4, // Attr. Length
10, 20, 30, 40, // IP-Address
},
wantFail: false,
expected: &PathAttribute{
TypeCode: 1,
Length: 1,
Value: uint8(1),
Next: &PathAttribute{
TypeCode: 3,
Length: 4,
Value: strAddr("10.20.30.40"),
},
},
},
{
name: "Incomplete data",
input: []byte{
0, // Attr. Flags
1, // Attr. Type Code
1, // Attr. Length
},
wantFail: true,
},
}
for _, test := range tests {
res, err := decodePathAttrs(bytes.NewBuffer(test.input), uint16(len(test.input)), &DecodingOptions{})
if test.wantFail && err == nil {
t.Errorf("Expected error did not happen for test %q", test.name)
continue
}
if !test.wantFail && err != nil {
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue
}
assert.Equalf(t, test.expected, res, "%s", test.name)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
}
}
func TestDecodePathAttr(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected *PathAttribute
}{
{
name: "Valid origin",
input: []byte{
0, // Attr. Flags
1, // Attr. Type Code
1, // Attr. Length
1, // EGP
},
wantFail: false,
expected: &PathAttribute{
Length: 1,
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
TypeCode: OriginAttr,
Value: uint8(1),
},
},
{
name: "Missing TypeCode",
input: []byte{
0, // Attr. Flags
},
wantFail: true,
},
{
name: "Missing Length",
input: []byte{
0, // Attr. Flags
1, // Attr. Type Code
},
wantFail: true,
},
{
name: "Missing Value ORIGIN",
input: []byte{
0, // Attr. Flags
1, // Attr. Type Code
1, // Attr. Length
},
wantFail: true,
},
{
name: "Missing value AS_PATH",
input: []byte{
0, // Attr. Flags
2, // Attr. Type Code
8, // Attr. Length
},
wantFail: true,
},
{
name: "Missing value NextHop",
input: []byte{
0, // Attr. Flags
3, // Attr. Type Code
4, // Attr. Length
},
wantFail: true,
},
{
name: "Missing value MED",
input: []byte{
0, // Attr. Flags
4, // Attr. Type Code
4, // Attr. Length
},
wantFail: true,
},
{
name: "Missing value LocalPref",
input: []byte{
0, // Attr. Flags
5, // Attr. Type Code
4, // Attr. Length
},
wantFail: true,
},
{
name: "Missing value AGGREGATOR",
input: []byte{
0, // Attr. Flags
7, // Attr. Type Code
4, // Attr. Length
},
wantFail: true,
},
{
name: "Not supported attribute",
input: []byte{
0, // Attr. Flags
111, // Attr. Type Code
4, // Attr. Length
},
wantFail: true,
},
}
for _, test := range tests {
res, _, err := decodePathAttr(bytes.NewBuffer(test.input), &DecodingOptions{})
if test.wantFail && err == nil {
t.Errorf("Expected error did not happen for test %q", test.name)
continue
}
if !test.wantFail && err != nil {
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue
}
assert.Equal(t, test.expected, res)
}
}
func TestDecodeOrigin(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected *PathAttribute
}{
{
name: "Test #1",
Loading
Loading full blame...