github.com/osrg/gobgp@v2.0.0+incompatible/pkg/server/zclient_test.go (about)

     1  // Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package server
    17  
    18  import (
    19  	"net"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/osrg/gobgp/internal/pkg/table"
    26  	"github.com/osrg/gobgp/internal/pkg/zebra"
    27  )
    28  
    29  func Test_newPathFromIPRouteMessage(t *testing.T) {
    30  	assert := assert.New(t)
    31  
    32  	// IPv4 Route Add
    33  	m := &zebra.Message{}
    34  	h := &zebra.Header{
    35  		Len:     zebra.HeaderSize(2),
    36  		Marker:  zebra.HEADER_MARKER,
    37  		Version: 2,
    38  		Command: zebra.IPV4_ROUTE_ADD,
    39  	}
    40  	b := &zebra.IPRouteBody{
    41  		Type:    zebra.ROUTE_TYPE(zebra.ROUTE_STATIC),
    42  		Flags:   zebra.FLAG(zebra.FLAG_SELECTED),
    43  		Message: zebra.MESSAGE_NEXTHOP | zebra.MESSAGE_DISTANCE | zebra.MESSAGE_METRIC | zebra.MESSAGE_MTU,
    44  		SAFI:    zebra.SAFI(zebra.SAFI_UNICAST),
    45  		Prefix: zebra.Prefix{
    46  			Prefix:    net.ParseIP("192.168.100.0"),
    47  			PrefixLen: uint8(24),
    48  		},
    49  		Nexthops: []zebra.Nexthop{
    50  			{
    51  				Gate: net.ParseIP("0.0.0.0"),
    52  			},
    53  			{
    54  				Ifindex: uint32(1),
    55  			},
    56  		},
    57  		Distance: uint8(0),
    58  		Metric:   uint32(100),
    59  		Mtu:      uint32(0),
    60  		Api:      zebra.API_TYPE(zebra.IPV4_ROUTE_ADD),
    61  	}
    62  	m.Header = *h
    63  	m.Body = b
    64  
    65  	path := newPathFromIPRouteMessage(m, 2)
    66  	pp := table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false)
    67  	pp.SetIsFromExternal(path.IsFromExternal())
    68  	assert.Equal("0.0.0.0", pp.GetNexthop().String())
    69  	assert.Equal("192.168.100.0/24", pp.GetNlri().String())
    70  	assert.True(pp.IsFromExternal())
    71  	assert.False(pp.IsWithdraw)
    72  
    73  	// IPv4 Route Delete
    74  	h.Command = zebra.IPV4_ROUTE_DELETE
    75  	b.Api = zebra.IPV4_ROUTE_DELETE
    76  	m.Header = *h
    77  	m.Body = b
    78  
    79  	path = newPathFromIPRouteMessage(m, 2)
    80  	pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false)
    81  	pp.SetIsFromExternal(path.IsFromExternal())
    82  	assert.Equal("0.0.0.0", pp.GetNexthop().String())
    83  	assert.Equal("192.168.100.0/24", pp.GetNlri().String())
    84  	med, _ := pp.GetMed()
    85  	assert.Equal(uint32(100), med)
    86  	assert.True(pp.IsFromExternal())
    87  	assert.True(pp.IsWithdraw)
    88  
    89  	// IPv6 Route Add
    90  	h.Command = zebra.IPV6_ROUTE_ADD
    91  	b.Api = zebra.IPV6_ROUTE_ADD
    92  	b.Prefix.Prefix = net.ParseIP("2001:db8:0:f101::")
    93  	b.Prefix.PrefixLen = uint8(64)
    94  	b.Nexthops = []zebra.Nexthop{{Gate: net.ParseIP("::")}}
    95  	m.Header = *h
    96  	m.Body = b
    97  
    98  	path = newPathFromIPRouteMessage(m, 2)
    99  	pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false)
   100  	pp.SetIsFromExternal(path.IsFromExternal())
   101  	assert.Equal("::", pp.GetNexthop().String())
   102  	assert.Equal("2001:db8:0:f101::/64", pp.GetNlri().String())
   103  	med, _ = pp.GetMed()
   104  	assert.Equal(uint32(100), med)
   105  	assert.True(pp.IsFromExternal())
   106  	assert.False(pp.IsWithdraw)
   107  
   108  	// IPv6 Route Delete
   109  	h.Command = zebra.IPV6_ROUTE_DELETE
   110  	b.Api = zebra.IPV6_ROUTE_DELETE
   111  	m.Header = *h
   112  	m.Body = b
   113  
   114  	path = newPathFromIPRouteMessage(m, 2)
   115  	pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false)
   116  	pp.SetIsFromExternal(path.IsFromExternal())
   117  	assert.Equal("::", pp.GetNexthop().String())
   118  	assert.Equal("2001:db8:0:f101::/64", pp.GetNlri().String())
   119  	assert.True(pp.IsFromExternal())
   120  	assert.True(pp.IsWithdraw)
   121  }