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

     1  package bgp
     2  
     3  import (
     4  	"net/netip"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func Test_MUPExtended(t *testing.T) {
    12  	assert := assert.New(t)
    13  	exts := make([]ExtendedCommunityInterface, 0)
    14  	exts = append(exts, NewMUPExtended(100, 10000))
    15  	m1 := NewPathAttributeExtendedCommunities(exts)
    16  	buf1, err := m1.Serialize()
    17  	require.NoError(t, err)
    18  
    19  	m2 := NewPathAttributeExtendedCommunities(nil)
    20  	err = m2.DecodeFromBytes(buf1)
    21  	require.NoError(t, err)
    22  
    23  	_, err = m2.Serialize()
    24  	require.NoError(t, err)
    25  
    26  	assert.Equal(m1, m2)
    27  }
    28  
    29  func Test_MUPInterworkSegmentDiscoveryRouteIPv4(t *testing.T) {
    30  	assert := assert.New(t)
    31  	rd, _ := ParseRouteDistinguisher("100:100")
    32  	r := &MUPInterworkSegmentDiscoveryRoute{
    33  		RD:     rd,
    34  		Prefix: netip.MustParsePrefix("10.10.10.0/24"),
    35  	}
    36  	n1 := NewMUPNLRI(AFI_IP, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_INTERWORK_SEGMENT_DISCOVERY, r)
    37  	buf1, err := n1.Serialize()
    38  	assert.Nil(err)
    39  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv4))
    40  	assert.Nil(err)
    41  	err = n2.DecodeFromBytes(buf1)
    42  	assert.Nil(err)
    43  
    44  	t.Logf("%s", n1)
    45  	t.Logf("%s", n2)
    46  
    47  	assert.Equal(n1, n2)
    48  }
    49  
    50  func Test_MUPInterworkSegmentDiscoveryRouteIPv6(t *testing.T) {
    51  	assert := assert.New(t)
    52  	rd, _ := ParseRouteDistinguisher("100:100")
    53  	r := &MUPInterworkSegmentDiscoveryRoute{
    54  		RD:     rd,
    55  		Prefix: netip.MustParsePrefix("2001::/64"),
    56  	}
    57  	n1 := NewMUPNLRI(AFI_IP6, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_INTERWORK_SEGMENT_DISCOVERY, r)
    58  	buf1, err := n1.Serialize()
    59  	assert.Nil(err)
    60  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv6))
    61  	assert.Nil(err)
    62  	err = n2.DecodeFromBytes(buf1)
    63  	assert.Nil(err)
    64  
    65  	t.Logf("%s", n1)
    66  	t.Logf("%s", n2)
    67  
    68  	assert.Equal(n1, n2)
    69  }
    70  
    71  func Test_MUPDirectSegmentDiscoveryRouteIPv4(t *testing.T) {
    72  	assert := assert.New(t)
    73  	rd, _ := ParseRouteDistinguisher("100:100")
    74  	r := &MUPDirectSegmentDiscoveryRoute{
    75  		RD:      rd,
    76  		Address: netip.MustParseAddr("10.10.10.1"),
    77  	}
    78  	n1 := NewMUPNLRI(AFI_IP, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_DIRECT_SEGMENT_DISCOVERY, r)
    79  	buf1, err := n1.Serialize()
    80  	assert.Nil(err)
    81  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv4))
    82  	assert.Nil(err)
    83  	err = n2.DecodeFromBytes(buf1)
    84  	assert.Nil(err)
    85  
    86  	t.Logf("%s", n1)
    87  	t.Logf("%s", n2)
    88  
    89  	assert.Equal(n1, n2)
    90  }
    91  
    92  func Test_MUPDirectSegmentDiscoveryRouteIPv6(t *testing.T) {
    93  	assert := assert.New(t)
    94  	rd, _ := ParseRouteDistinguisher("100:100")
    95  	r := &MUPDirectSegmentDiscoveryRoute{
    96  		RD:      rd,
    97  		Address: netip.MustParseAddr("2001::1"),
    98  	}
    99  	n1 := NewMUPNLRI(AFI_IP6, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_DIRECT_SEGMENT_DISCOVERY, r)
   100  	buf1, err := n1.Serialize()
   101  	assert.Nil(err)
   102  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv6))
   103  	assert.Nil(err)
   104  	err = n2.DecodeFromBytes(buf1)
   105  	assert.Nil(err)
   106  
   107  	t.Logf("%s", n1)
   108  	t.Logf("%s", n2)
   109  
   110  	assert.Equal(n1, n2)
   111  }
   112  
   113  func Test_MUPType1SessionTransformedRoute(t *testing.T) {
   114  	assert := assert.New(t)
   115  	rd, _ := ParseRouteDistinguisher("100:100")
   116  	ipv4SA := netip.MustParseAddr("10.10.10.2")
   117  	ipv6SA := netip.MustParseAddr("2001::2")
   118  	tests := []struct {
   119  		name string
   120  		in   *MUPType1SessionTransformedRoute
   121  		afi  uint16
   122  		rf   RouteFamily
   123  	}{
   124  		{
   125  			name: "IPv4",
   126  			in: &MUPType1SessionTransformedRoute{
   127  				RD:                    rd,
   128  				Prefix:                netip.MustParsePrefix("192.100.0.0/24"),
   129  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   130  				QFI:                   9,
   131  				EndpointAddressLength: 32,
   132  				EndpointAddress:       netip.MustParseAddr("10.10.10.1"),
   133  			},
   134  			afi: AFI_IP,
   135  			rf:  RF_MUP_IPv4,
   136  		},
   137  		{
   138  			name: "IPv4_with_SourceAddress",
   139  			in: &MUPType1SessionTransformedRoute{
   140  				RD:                    rd,
   141  				Prefix:                netip.MustParsePrefix("192.100.0.0/24"),
   142  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   143  				QFI:                   9,
   144  				EndpointAddressLength: 32,
   145  				EndpointAddress:       netip.MustParseAddr("10.10.10.1"),
   146  				SourceAddressLength:   32,
   147  				SourceAddress:         &ipv4SA,
   148  			},
   149  			afi: AFI_IP,
   150  			rf:  RF_MUP_IPv4,
   151  		},
   152  		{
   153  			name: "IPv6",
   154  			in: &MUPType1SessionTransformedRoute{
   155  				RD:                    rd,
   156  				Prefix:                netip.MustParsePrefix("2001:db8:1::/48"),
   157  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   158  				QFI:                   9,
   159  				EndpointAddressLength: 128,
   160  				EndpointAddress:       netip.MustParseAddr("2001::1"),
   161  			},
   162  			afi: AFI_IP6,
   163  			rf:  RF_MUP_IPv6,
   164  		},
   165  		{
   166  			name: "IPv6_with_SourceAddress",
   167  			in: &MUPType1SessionTransformedRoute{
   168  				RD:                    rd,
   169  				Prefix:                netip.MustParsePrefix("2001:db8:1::/48"),
   170  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   171  				QFI:                   9,
   172  				EndpointAddressLength: 128,
   173  				EndpointAddress:       netip.MustParseAddr("2001::1"),
   174  				SourceAddressLength:   128,
   175  				SourceAddress:         &ipv6SA,
   176  			},
   177  			afi: AFI_IP6,
   178  			rf:  RF_MUP_IPv6,
   179  		},
   180  	}
   181  
   182  	for _, tt := range tests {
   183  		t.Run(tt.name, func(t *testing.T) {
   184  			n1 := NewMUPNLRI(tt.afi, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_TYPE_1_SESSION_TRANSFORMED, tt.in)
   185  			buf1, err := n1.Serialize()
   186  			assert.Nil(err)
   187  			n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(tt.rf))
   188  			assert.Nil(err)
   189  			err = n2.DecodeFromBytes(buf1)
   190  			assert.Nil(err)
   191  
   192  			t.Logf("%s", n1)
   193  			t.Logf("%s", n2)
   194  
   195  			assert.Equal(n1, n2)
   196  		})
   197  	}
   198  }
   199  
   200  func Test_MUPType2SessionTransformedRouteIPv4(t *testing.T) {
   201  	assert := assert.New(t)
   202  	rd, _ := ParseRouteDistinguisher("100:100")
   203  	tests := []struct {
   204  		name string
   205  		in   *MUPType2SessionTransformedRoute
   206  	}{
   207  		{
   208  			name: "teid length = 32",
   209  			in: &MUPType2SessionTransformedRoute{
   210  				RD:                    rd,
   211  				EndpointAddressLength: 64,
   212  				EndpointAddress:       netip.MustParseAddr("10.10.10.1"),
   213  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   214  			},
   215  		},
   216  		{
   217  			name: "teid length = 10",
   218  			in: &MUPType2SessionTransformedRoute{
   219  				RD:                    rd,
   220  				EndpointAddressLength: 42,
   221  				EndpointAddress:       netip.MustParseAddr("10.10.10.1"),
   222  				TEID:                  netip.MustParseAddr("100.64.0.0"), // /10
   223  			},
   224  		},
   225  		{
   226  			name: "teid length = 0",
   227  			in: &MUPType2SessionTransformedRoute{
   228  				RD:                    rd,
   229  				EndpointAddressLength: 32,
   230  				EndpointAddress:       netip.MustParseAddr("10.10.10.1"),
   231  				TEID:                  netip.MustParseAddr("0.0.0.0"),
   232  			},
   233  		},
   234  	}
   235  
   236  	for _, tt := range tests {
   237  		t.Run(tt.name, func(t *testing.T) {
   238  			n1 := NewMUPNLRI(AFI_IP, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_TYPE_2_SESSION_TRANSFORMED, tt.in)
   239  			buf1, err := n1.Serialize()
   240  			assert.Nil(err)
   241  			n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv4))
   242  			assert.Nil(err)
   243  			err = n2.DecodeFromBytes(buf1)
   244  			assert.Nil(err)
   245  
   246  			t.Logf("%s", n1)
   247  			t.Logf("%s", n2)
   248  
   249  			assert.Equal(n1, n2)
   250  		})
   251  	}
   252  }
   253  
   254  func Test_MUPType2SessionTransformedRouteIPv6(t *testing.T) {
   255  	assert := assert.New(t)
   256  	rd, _ := ParseRouteDistinguisher("100:100")
   257  	tests := []struct {
   258  		name string
   259  		in   *MUPType2SessionTransformedRoute
   260  	}{
   261  		{
   262  			name: "teid length = 32",
   263  			in: &MUPType2SessionTransformedRoute{
   264  				RD:                    rd,
   265  				EndpointAddressLength: 160,
   266  				EndpointAddress:       netip.MustParseAddr("2001::1"),
   267  				TEID:                  netip.MustParseAddr("0.0.0.100"),
   268  			},
   269  		},
   270  		{
   271  			name: "teid length = 10",
   272  			in: &MUPType2SessionTransformedRoute{
   273  				RD:                    rd,
   274  				EndpointAddressLength: 138,
   275  				EndpointAddress:       netip.MustParseAddr("2001::1"),
   276  				TEID:                  netip.MustParseAddr("100.64.0.0"), // /10
   277  			},
   278  		},
   279  		{
   280  			name: "teid length = 0",
   281  			in: &MUPType2SessionTransformedRoute{
   282  				RD:                    rd,
   283  				EndpointAddressLength: 128,
   284  				EndpointAddress:       netip.MustParseAddr("2001::1"),
   285  				TEID:                  netip.MustParseAddr("0.0.0.0"),
   286  			},
   287  		},
   288  	}
   289  	for _, tt := range tests {
   290  		t.Run(tt.name, func(t *testing.T) {
   291  			n1 := NewMUPNLRI(AFI_IP6, MUP_ARCH_TYPE_3GPP_5G, MUP_ROUTE_TYPE_TYPE_2_SESSION_TRANSFORMED, tt.in)
   292  			buf1, err := n1.Serialize()
   293  			assert.Nil(err)
   294  			n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_MUP_IPv6))
   295  			assert.Nil(err)
   296  			err = n2.DecodeFromBytes(buf1)
   297  			assert.Nil(err)
   298  
   299  			t.Logf("%s", n1)
   300  			t.Logf("%s", n2)
   301  
   302  			assert.Equal(n1, n2)
   303  		})
   304  	}
   305  }