github.com/osrg/gobgp/v3@v3.30.0/pkg/packet/bgp/prefix_sid_test.go (about)

     1  package bgp
     2  
     3  import (
     4  	"bytes"
     5  	"net/netip"
     6  	"testing"
     7  )
     8  
     9  func TestRoundTripSubSubTLV(t *testing.T) {
    10  	tests := []struct {
    11  		name  string
    12  		input []byte
    13  	}{
    14  		{
    15  			name:  "SRv6SIDStructureSubSubTLV",
    16  			input: []byte{0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40},
    17  		},
    18  	}
    19  	for _, tt := range tests {
    20  		t.Run(tt.name, func(t *testing.T) {
    21  			sstlv := &SRv6SIDStructureSubSubTLV{}
    22  			if err := sstlv.DecodeFromBytes(tt.input); err != nil {
    23  				t.Fatalf("test failed with error: %+v", err)
    24  			}
    25  			recovered, err := sstlv.Serialize()
    26  			if err != nil {
    27  				t.Fatalf("test failed with error: %+v", err)
    28  			}
    29  			if !bytes.Equal(tt.input, recovered) {
    30  				t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered)
    31  			}
    32  		})
    33  	}
    34  }
    35  
    36  func TestRoundTripSubTLV(t *testing.T) {
    37  	tests := []struct {
    38  		name  string
    39  		input []byte
    40  	}{
    41  		{
    42  			name:  "SRv6InformationSubTLV",
    43  			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},
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			stlv := &SRv6InformationSubTLV{}
    49  			if err := stlv.DecodeFromBytes(tt.input); err != nil {
    50  				t.Fatalf("test failed with error: %+v", err)
    51  			}
    52  			recovered, err := stlv.Serialize()
    53  			if err != nil {
    54  				t.Fatalf("test failed with error: %+v", err)
    55  			}
    56  			if !bytes.Equal(tt.input, recovered) {
    57  				t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered)
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  func TestRoundTripPrefixSID(t *testing.T) {
    64  	tests := []struct {
    65  		name  string
    66  		input []byte
    67  	}{
    68  		{
    69  			name:  "srv6 prefix sid",
    70  			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},
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			attribute, err := GetPathAttribute(tt.input)
    76  			if err != nil {
    77  				t.Fatalf("test failed with error: %+v", err)
    78  			}
    79  			if err := attribute.DecodeFromBytes(tt.input); err != nil {
    80  				t.Fatalf("test failed with error: %+v", err)
    81  			}
    82  			recovered, err := attribute.Serialize()
    83  			if err != nil {
    84  				t.Fatalf("test failed with error: %+v", err)
    85  			}
    86  			if !bytes.Equal(tt.input, recovered) {
    87  				t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestNewPathAttributePrefixSID(t *testing.T) {
    94  	prefix := netip.MustParsePrefix("2001:0:5:3::/64")
    95  	tests := []struct {
    96  		name string
    97  		psid *PathAttributePrefixSID
    98  		want []byte
    99  	}{
   100  		{
   101  			name: "srv6 prefix sid",
   102  			psid: NewPathAttributePrefixSID(
   103  				NewSRv6ServiceTLV(
   104  					TLVTypeSRv6L3Service,
   105  					NewSRv6InformationSubTLV(
   106  						prefix.Addr(),
   107  						END_DT4,
   108  						NewSRv6SIDStructureSubSubTLV(uint8(prefix.Bits()), 24, 16, 0, 16, 64),
   109  					),
   110  				),
   111  			),
   112  			want: []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, 0x40, 0x18, 0x10, 0x00, 0x10, 0x40},
   113  		},
   114  	}
   115  	for _, tt := range tests {
   116  		t.Run(tt.name, func(t *testing.T) {
   117  			got, err := tt.psid.Serialize()
   118  			if err != nil {
   119  				t.Fatalf("test failed with error: %+v", err)
   120  			}
   121  			if !bytes.Equal(got, tt.want) {
   122  				t.Logf("psid: %s", tt.psid)
   123  				t.Fatalf("got %x want %x", got, tt.want)
   124  			}
   125  		})
   126  	}
   127  }