Newer
Older
Christoph Petrausch
committed
package server
import (
Christoph Petrausch
committed
"testing"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
Christoph Petrausch
committed
"github.com/stretchr/testify/assert"
)
func (f *failingUpdate) SerializeUpdate(opt *packet.EncodeOptions) ([]byte, error) {
return nil, errors.New("general error")
}
type WriterByter interface {
Bytes() []byte
io.Writer
}
type failingReadWriter struct {
}
func (f *failingReadWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("general error")
}
func (f *failingReadWriter) Bytes() []byte {
return []byte{}
}
Christoph Petrausch
committed
func TestSerializeAndSendUpdate(t *testing.T) {
tests := []struct {
name string
Christoph Petrausch
committed
err error
testUpdate serializeAbleUpdate
expected []byte
}{
{
name: "normal bgp update",
Christoph Petrausch
committed
err: nil,
testUpdate: &packet.BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &packet.NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
Christoph Petrausch
committed
Next: &packet.NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 0), 16),
Christoph Petrausch
committed
},
},
},
expected: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 28, // Length
2, // Type = Update
0, 5, 8, 10, 16, 192, 168, 0, 0, // 2 withdraws
},
},
{
name: "failed serialization",
buf: bytes.NewBuffer(nil),
err: nil,
testUpdate: &failingUpdate{},
expected: nil,
},
{
name: "failed connection",
buf: &failingReadWriter{},
err: errors.New("Failed sending Update: general error"),
testUpdate: &packet.BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &packet.NLRI{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(192, 168, 0, 0), 16),
Christoph Petrausch
committed
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opt := &packet.EncodeOptions{}
err := serializeAndSendUpdate(test.buf, test.testUpdate, opt)
Christoph Petrausch
committed
assert.Equal(t, test.err, err)
assert.Equal(t, test.expected, test.buf.Bytes())