diff options
Diffstat (limited to 'internal/pkg/apiutil/attribute_test.go')
-rw-r--r-- | internal/pkg/apiutil/attribute_test.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/internal/pkg/apiutil/attribute_test.go b/internal/pkg/apiutil/attribute_test.go index 8b3cd1c2..a3eff1a7 100644 --- a/internal/pkg/apiutil/attribute_test.go +++ b/internal/pkg/apiutil/attribute_test.go @@ -16,6 +16,7 @@ package apiutil import ( + "bytes" "net" "testing" @@ -1579,3 +1580,105 @@ func Test_UnknownAttribute(t *testing.T) { assert.Equal(input.Type, output.Type) assert.Equal(input.Value, output.Value) } + +func TestFullCyclePrefixSID(t *testing.T) { + tests := []struct { + name string + input []byte + }{ + { + name: "srv6 prefix sid", + input: []byte{0xc0, 0x28, 0x25, 0x05, 0x00, 0x22, 0x00, 0x01, 0x00, 0x1e, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + attribute := bgp.PathAttributePrefixSID{} + if err := attribute.DecodeFromBytes(tt.input); err != nil { + t.Fatalf("test failed with error: %+v", err) + } + // Converting from Native to API + apiPrefixSID := NewPrefixSIDAttributeFromNative(&attribute) + // Converting back from API to Native + recoveredPrefixSID, err := bgp.NewPathAttributePrefixSID(apiPrefixSID) + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + recovered, err := recoveredPrefixSID.Serialize() + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + if !bytes.Equal(tt.input, recovered) { + t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) + } + }) + } +} + +func TestFullCycleSRv6SIDStructureSubSubTLV(t *testing.T) { + tests := []struct { + name string + input []byte + }{ + { + name: "srv6 prefix sid", + input: []byte{0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sstlv := bgp.SRv6SIDStructureSubSubTLV{} + if err := sstlv.DecodeFromBytes(tt.input); err != nil { + t.Fatalf("test failed with error: %+v", err) + } + // Converting from Native to API + apiPrefixSID := MarshalSRv6SubSubTLVs([]bgp.PrefixSIDTLVInterface{&sstlv}) + // Converting back from API to Native + _, recoveredPrefixSID, err := bgp.UnmarshalSubSubTLVs(apiPrefixSID) + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + recovered, err := recoveredPrefixSID[0].Serialize() + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + if !bytes.Equal(tt.input, recovered) { + t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) + } + }) + } +} + +func TestFullCycleSRv6InformationSubTLV(t *testing.T) { + tests := []struct { + name string + input []byte + }{ + { + name: "srv6 prefix sid informationw sub tlv", + input: []byte{0x01, 0x00, 0x1e, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stlv := bgp.SRv6InformationSubTLV{} + if err := stlv.DecodeFromBytes(tt.input); err != nil { + t.Fatalf("test failed with error: %+v", err) + } + // Converting from Native to API + apiPrefixSID := MarshalSRv6SubTLVs([]bgp.PrefixSIDTLVInterface{&stlv}) + // Converting back from API to Native + _, recoveredPrefixSID, err := bgp.UnmarshalSubTLVs(apiPrefixSID) + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + recovered, err := recoveredPrefixSID[0].Serialize() + if err != nil { + t.Fatalf("test failed with error: %+v", err) + } + if !bytes.Equal(tt.input, recovered) { + t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) + } + }) + } +} |