Skip to content
Snippets Groups Projects
Commit 035b1223 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

unknown path serialization

parent da182cae
No related branches found
No related tags found
No related merge requests found
......@@ -390,6 +390,8 @@ func (pa *PathAttribute) serialize(buf *bytes.Buffer, opt *Options) uint8 {
pathAttrLen = pa.serializeCommunities(buf)
case LargeCommunitiesAttr:
pathAttrLen = pa.serializeLargeCommunities(buf)
default:
pathAttrLen = pa.serializeUnknownAttribute(buf)
}
return pathAttrLen
......@@ -546,6 +548,23 @@ func (pa *PathAttribute) serializeLargeCommunities(buf *bytes.Buffer) uint8 {
return length
}
func (pa *PathAttribute) serializeUnknownAttribute(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
if pa.Optional {
attrFlags = setOptional(attrFlags)
}
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(pa.TypeCode)
b := pa.Value.([]byte)
buf.WriteByte(uint8(len(b)))
buf.Write(b)
return uint8(len(b) + 2)
}
func fourBytesToUint32(address [4]byte) uint32 {
return uint32(address[0])<<24 + uint32(address[1])<<16 + uint32(address[2])<<8 + uint32(address[3])
}
......
......@@ -1345,6 +1345,41 @@ func TestSerializeCommunities(t *testing.T) {
}
}
func TestSerializeUnknownAttribute(t *testing.T) {
tests := []struct {
name string
input *PathAttribute
expected []byte
expectedLen uint8
}{
{
name: "Arbritary attribute",
input: &PathAttribute{
TypeCode: 200,
Value: []byte{1, 2, 3, 4},
Transitive: true,
},
expected: []byte{
64, // Attribute flags
200, // Type
4, // Length
1, 2, 3, 4, // Payload
},
expectedLen: 6,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
n := test.input.serializeUnknownAttribute(buf)
assert.Equal(t, test.expectedLen, n)
assert.Equal(t, test.expected, buf.Bytes())
})
}
}
func TestSerialize(t *testing.T) {
tests := []struct {
name string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment