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

     1  package bgp
     2  
     3  import (
     4  	"encoding/binary"
     5  	"net"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/go-test/deep"
    10  )
    11  
    12  func TestBindingSIDRoundTrip(t *testing.T) {
    13  	b := make([]byte, 4)
    14  	binary.BigEndian.PutUint32(b, 24321)
    15  	bsid, _ := NewBSID(b)
    16  	tests := []struct {
    17  		name  string
    18  		input *TunnelEncapSubTLVSRBSID
    19  		fail  bool
    20  	}{
    21  		{
    22  			name: "no bsid",
    23  			input: &TunnelEncapSubTLVSRBSID{
    24  				TunnelEncapSubTLV: TunnelEncapSubTLV{
    25  					Type:   1,
    26  					Length: 2,
    27  				},
    28  				Flags: 0x0,
    29  				BSID:  nil,
    30  			},
    31  			fail: false,
    32  		},
    33  		{
    34  			name: "v4 bsid",
    35  			input: &TunnelEncapSubTLVSRBSID{
    36  				TunnelEncapSubTLV: TunnelEncapSubTLV{
    37  					Type:   1,
    38  					Length: 6,
    39  				},
    40  				Flags: 0x0,
    41  				BSID:  bsid,
    42  			},
    43  			fail: false,
    44  		},
    45  		{
    46  			name: "srv6 bsid",
    47  			input: &TunnelEncapSubTLVSRBSID{
    48  				TunnelEncapSubTLV: TunnelEncapSubTLV{
    49  					Type:   1,
    50  					Length: 18,
    51  				},
    52  				Flags: 0x0,
    53  				BSID: &BSID{
    54  					Value: net.ParseIP("2001:1::1").To16(),
    55  				},
    56  			},
    57  			fail: false,
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			b, err := tt.input.Serialize()
    63  			if err != nil && !tt.fail {
    64  				t.Fatalf("expected to succeed but failed with error: %+v", err)
    65  			}
    66  			if err == nil && tt.fail {
    67  				t.Fatal("Expected to fail but succeeded")
    68  			}
    69  			if err != nil {
    70  				return
    71  			}
    72  			result := &TunnelEncapSubTLVSRBSID{}
    73  			err = result.DecodeFromBytes(b)
    74  			if err != nil && !tt.fail {
    75  				t.Fatalf("expected to succeed but failed with error: %+v", err)
    76  			}
    77  			if err == nil && tt.fail {
    78  				t.Fatal("Expected to fail but succeeded")
    79  			}
    80  			if err != nil {
    81  				return
    82  			}
    83  			if !reflect.DeepEqual(tt.input, result) {
    84  				t.Logf("Diffs: %+v", deep.Equal(tt.input, result))
    85  				t.Fatalf("expected: %+v does not match result: %+v", tt.input, result)
    86  			}
    87  		})
    88  	}
    89  }
    90  
    91  func TestSegmentListRoundTrip(t *testing.T) {
    92  	tests := []struct {
    93  		name  string
    94  		input *TunnelEncapSubTLVSRSegmentList
    95  		fail  bool
    96  	}{
    97  		{
    98  			name: "empty Segment List Sub TLV",
    99  			input: &TunnelEncapSubTLVSRSegmentList{
   100  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   101  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   102  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   103  				},
   104  			},
   105  			fail: false,
   106  		},
   107  		{
   108  			name: "only empty weight",
   109  			input: &TunnelEncapSubTLVSRSegmentList{
   110  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   111  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   112  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   113  				},
   114  				Weight: &SegmentListWeight{
   115  					TunnelEncapSubTLV: TunnelEncapSubTLV{
   116  						Type:   9,
   117  						Length: 6,
   118  					},
   119  					Flags:  0,
   120  					Weight: 100,
   121  				},
   122  			},
   123  			fail: false,
   124  		},
   125  		{
   126  			name: "weight and 1 type A segment",
   127  			input: &TunnelEncapSubTLVSRSegmentList{
   128  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   129  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   130  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   131  				},
   132  				Weight: &SegmentListWeight{
   133  					TunnelEncapSubTLV: TunnelEncapSubTLV{
   134  						Type:   SegmentListSubTLVWeight,
   135  						Length: 6,
   136  					},
   137  					Flags:  0,
   138  					Weight: 100,
   139  				},
   140  				Segments: []TunnelEncapSubTLVInterface{
   141  					&SegmentTypeA{
   142  						TunnelEncapSubTLV: TunnelEncapSubTLV{
   143  							Type:   EncapSubTLVType(TypeA),
   144  							Length: 6,
   145  						},
   146  						Flags: 0,
   147  						Label: (21431 << 12),
   148  					},
   149  				},
   150  			},
   151  			fail: false,
   152  		},
   153  		{
   154  			name: "weight and 2 type A segment",
   155  			input: &TunnelEncapSubTLVSRSegmentList{
   156  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   157  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   158  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   159  				},
   160  				Weight: &SegmentListWeight{
   161  					TunnelEncapSubTLV: TunnelEncapSubTLV{
   162  						Type:   SegmentListSubTLVWeight,
   163  						Length: 6,
   164  					},
   165  					Flags:  0,
   166  					Weight: 100,
   167  				},
   168  				Segments: []TunnelEncapSubTLVInterface{
   169  					&SegmentTypeA{
   170  						TunnelEncapSubTLV: TunnelEncapSubTLV{
   171  							Type:   EncapSubTLVType(TypeA),
   172  							Length: 6,
   173  						},
   174  						Flags: 0,
   175  						Label: (21431 << 12),
   176  					},
   177  					&SegmentTypeA{
   178  						TunnelEncapSubTLV: TunnelEncapSubTLV{
   179  							Type:   EncapSubTLVType(TypeA),
   180  							Length: 6,
   181  						},
   182  						Flags: 0,
   183  						Label: (100001 << 12),
   184  					},
   185  				},
   186  			},
   187  			fail: false,
   188  		},
   189  		{
   190  			name: "weight and 2 type B segment without SRv6 Endpoint Behavior and Structure",
   191  			input: &TunnelEncapSubTLVSRSegmentList{
   192  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   193  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   194  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   195  				},
   196  				Weight: &SegmentListWeight{
   197  					TunnelEncapSubTLV: TunnelEncapSubTLV{
   198  						Type:   SegmentListSubTLVWeight,
   199  						Length: 6,
   200  					},
   201  					Flags:  0,
   202  					Weight: 100,
   203  				},
   204  				Segments: []TunnelEncapSubTLVInterface{
   205  					&SegmentTypeB{
   206  						TunnelEncapSubTLV: TunnelEncapSubTLV{Type: EncapSubTLVType(TypeB), Length: 6},
   207  						Flags:             0,
   208  						SID:               net.ParseIP("2001:1::1").To16(),
   209  					},
   210  					&SegmentTypeB{
   211  						TunnelEncapSubTLV: TunnelEncapSubTLV{Type: EncapSubTLVType(TypeB), Length: 6},
   212  						Flags:             0,
   213  						SID:               net.ParseIP("2001:1::2").To16(),
   214  					},
   215  				},
   216  			},
   217  			fail: false,
   218  		},
   219  		{
   220  			name: "weight and 2 type B segment with SR Endpoint Behavior and Structure",
   221  			input: &TunnelEncapSubTLVSRSegmentList{
   222  				TunnelEncapSubTLV: TunnelEncapSubTLV{
   223  					Type:   ENCAP_SUBTLV_TYPE_SRSEGMENT_LIST,
   224  					Length: 6, // Weight (6 bytes) + Length of each segment + 2
   225  				},
   226  				Weight: &SegmentListWeight{
   227  					TunnelEncapSubTLV: TunnelEncapSubTLV{
   228  						Type:   SegmentListSubTLVWeight,
   229  						Length: 6,
   230  					},
   231  					Flags:  0,
   232  					Weight: 100,
   233  				},
   234  				Segments: []TunnelEncapSubTLVInterface{
   235  					&SegmentTypeB{
   236  						TunnelEncapSubTLV: TunnelEncapSubTLV{Type: EncapSubTLVType(TypeB), Length: 6},
   237  						Flags:             0,
   238  						SID:               net.ParseIP("2001:1::1").To16(),
   239  						SRv6EBS: &SRv6EndpointBehaviorStructure{
   240  							Behavior: 39,
   241  							BlockLen: 5,
   242  							NodeLen:  6,
   243  							FuncLen:  7,
   244  							ArgLen:   8,
   245  						},
   246  					},
   247  					&SegmentTypeB{
   248  						TunnelEncapSubTLV: TunnelEncapSubTLV{Type: EncapSubTLVType(TypeB), Length: 6},
   249  						Flags:             0,
   250  						SID:               net.ParseIP("2001:1::2").To16(),
   251  					},
   252  				},
   253  			},
   254  			fail: false,
   255  		},
   256  	}
   257  	for _, tt := range tests {
   258  		t.Run(tt.name, func(t *testing.T) {
   259  			b, err := tt.input.Serialize()
   260  			if err != nil && !tt.fail {
   261  				t.Fatalf("expected to succeed but failed with error: %+v", err)
   262  			}
   263  			if err == nil && tt.fail {
   264  				t.Fatal("Expected to fail but succeeded")
   265  			}
   266  			if err != nil {
   267  				return
   268  			}
   269  			result := &TunnelEncapSubTLVSRSegmentList{}
   270  			err = result.DecodeFromBytes(b)
   271  			if err != nil && !tt.fail {
   272  				t.Fatalf("expected to succeed but failed with error: %+v", err)
   273  			}
   274  			if err == nil && tt.fail {
   275  				t.Fatal("Expected to fail but succeeded")
   276  			}
   277  			if err != nil {
   278  				return
   279  			}
   280  			if !reflect.DeepEqual(tt.input, result) {
   281  				t.Logf("Diffs: %+v", deep.Equal(tt.input, result))
   282  				t.Fatalf("expected: %+v does not match result: %+v", tt.input, result)
   283  			}
   284  		})
   285  	}
   286  }