Newer
Older
bnet "github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert"
)
func TestDecodeNLRIs(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected *NLRI
}{
{
name: "Valid NRLI #1",
input: []byte{
24, 192, 168, 0,
8, 10,
17, 172, 16, 0,
},
wantFail: false,
expected: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 0), 24),
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(172, 16, 0, 0), 17),
},
},
},
},
{
name: "Invalid NRLI #1",
input: []byte{
24, 192, 168, 0,
8, 10,
17, 172, 16,
},
wantFail: true,
},
}
for _, test := range tests {
buf := bytes.NewBuffer(test.input)
res, err := decodeNLRIs(buf, uint16(len(test.input)), IPv4AFI, false)
if test.wantFail && err == nil {
t.Errorf("Expected error did not happen for test %q", test.name)
}
if !test.wantFail && err != nil {
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
}
assert.Equal(t, test.expected, res)
}
}
func TestDecodeNLRI(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected *NLRI
}{
{
name: "Valid NRLI #1",
input: []byte{
24, 192, 168, 0,
},
wantFail: false,
expected: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 0), 24),
},
},
{
name: "Valid NRLI #2",
input: []byte{
25, 192, 168, 0, 128,
},
wantFail: false,
expected: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 128), 25),
},
},
{
name: "Incomplete NLRI #1",
input: []byte{
25, 192, 168, 0,
},
wantFail: true,
},
{
name: "Incomplete NLRI #2",
input: []byte{
25,
},
wantFail: true,
},
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
{
name: "Valid NRLI #1 add path",
input: []byte{
0, 0, 0, 10, 24, 192, 168, 0,
},
addPath: true,
wantFail: false,
expected: &NLRI{
PathIdentifier: 10,
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 0), 24),
},
},
{
name: "Valid NRLI #2 add path",
input: []byte{
0, 0, 1, 0, 25, 192, 168, 0, 128,
},
addPath: true,
wantFail: false,
expected: &NLRI{
PathIdentifier: 256,
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 128), 25),
},
},
{
name: "Incomplete path Identifier",
input: []byte{
0, 0, 0,
},
addPath: true,
wantFail: true,
},
{
name: "Incomplete NLRI #1 add path",
input: []byte{
0, 0, 1, 0, 25, 192, 168, 0,
},
addPath: true,
wantFail: true,
},
{
name: "Incomplete NLRI #2 add path",
input: []byte{
0, 0, 1, 0, 25,
},
addPath: true,
wantFail: true,
},
{
name: "Empty input",
input: []byte{},
wantFail: true,
},
}
for _, test := range tests {
buf := bytes.NewBuffer(test.input)
res, _, err := decodeNLRI(buf, IPv4AFI, test.addPath)
if test.wantFail && err == nil {
t.Errorf("Expected error did not happen for test %q", test.name)
}
if !test.wantFail && err != nil {
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
}
assert.Equal(t, test.expected, res)
}
}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
func TestBytesInAddr(t *testing.T) {
tests := []struct {
name string
input uint8
expected uint8
}{
{
name: "Test #1",
input: 24,
expected: 3,
},
{
name: "Test #2",
input: 25,
expected: 4,
},
{
name: "Test #3",
input: 32,
expected: 4,
},
{
name: "Test #4",
input: 0,
expected: 0,
},
{
name: "Test #5",
input: 9,
expected: 2,
},
}
for _, test := range tests {
if res != test.expected {
t.Errorf("Unexpected result for test %q: %d", test.name, res)
}
}
}
func TestNLRISerialize(t *testing.T) {
tests := []struct {
name string
nlri *NLRI
expected []byte
}{
{
name: "Test #1",
nlri: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(1, 2, 3, 0), 25),
},
expected: []byte{25, 1, 2, 3, 0},
},
{
name: "Test #2",
nlri: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(1, 2, 3, 0), 24),
},
expected: []byte{24, 1, 2, 3},
},
{
name: "Test #3",
nlri: &NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(100, 200, 128, 0), 17),
},
expected: []byte{17, 100, 200, 128},
},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
test.nlri.serialize(buf)
res := buf.Bytes()
assert.Equal(t, test.expected, res)
}
}
func TestNLRIAddPathSerialize(t *testing.T) {
tests := []struct {
name string
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(1, 2, 3, 0), 25),
},
expected: []byte{0, 0, 0, 100, 25, 1, 2, 3, 0},
},
{
name: "Test #2",
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(1, 2, 3, 0), 24),
},
expected: []byte{0, 0, 0, 100, 24, 1, 2, 3},
},
{
name: "Test #3",
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(100, 200, 128, 0), 17),
},
expected: []byte{0, 0, 0, 100, 17, 100, 200, 128},
},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
res := buf.Bytes()
assert.Equal(t, test.expected, res)
}
}