github.com/osrg/gobgp/v3@v3.30.0/pkg/server/grpc_server_test.go (about) 1 package server 2 3 import ( 4 "net" 5 "testing" 6 "time" 7 8 api "github.com/osrg/gobgp/v3/api" 9 "github.com/osrg/gobgp/v3/internal/pkg/table" 10 "github.com/osrg/gobgp/v3/pkg/apiutil" 11 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 12 "github.com/stretchr/testify/assert" 13 "google.golang.org/protobuf/types/known/anypb" 14 ) 15 16 func TestParseHost(t *testing.T) { 17 tsts := []struct { 18 name string 19 host string 20 expectNetwork string 21 expectAddr string 22 }{ 23 { 24 name: "schemeless tcp host defaults to tcp", 25 host: "127.0.0.1:50051", 26 expectNetwork: "tcp", 27 expectAddr: "127.0.0.1:50051", 28 }, 29 { 30 name: "schemeless with only port defaults to tcp", 31 host: ":50051", 32 expectNetwork: "tcp", 33 expectAddr: ":50051", 34 }, 35 { 36 name: "unix socket", 37 host: "unix:///var/run/gobgp.socket", 38 expectNetwork: "unix", 39 expectAddr: "/var/run/gobgp.socket", 40 }, 41 } 42 43 for _, tst := range tsts { 44 t.Run(tst.name, func(t *testing.T) { 45 gotNetwork, gotAddr := parseHost(tst.host) 46 assert.Equal(t, tst.expectNetwork, gotNetwork) 47 assert.Equal(t, tst.expectAddr, gotAddr) 48 }) 49 } 50 } 51 52 func TestToPathApi(t *testing.T) { 53 type args struct { 54 path *table.Path 55 v *table.Validation 56 onlyBinary bool 57 nlriBinary bool 58 attributeBinary bool 59 } 60 tests := []struct { 61 name string 62 args args 63 want *api.Path 64 }{ 65 { 66 name: "ipv4 path", 67 args: args{ 68 path: table.NewPath(&table.PeerInfo{ 69 ID: net.IP{10, 10, 10, 10}, 70 LocalID: net.IP{10, 11, 11, 11}, 71 Address: net.IP{10, 12, 12, 12}, 72 LocalAddress: net.IP{10, 13, 13, 13}, 73 }, 74 bgp.NewIPAddrPrefix(8, "10.0.0.0"), 75 false, 76 []bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)}, 77 time.Time{}, 78 false), 79 }, 80 want: &api.Path{ 81 Nlri: anyNlri(bgp.NewIPAddrPrefix(8, "10.0.0.0")), 82 Pattrs: anyAttrs([]bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)}), 83 Family: &api.Family{ 84 Afi: api.Family_AFI_IP, 85 Safi: api.Family_SAFI_UNICAST, 86 }, 87 Validation: &api.Validation{}, 88 NeighborIp: "10.12.12.12", 89 SourceId: "10.10.10.10", 90 }, 91 }, 92 { 93 name: "eor ipv4 path", 94 args: args{ 95 path: eor(bgp.RF_IPv4_UC), 96 }, 97 want: &api.Path{ 98 Nlri: anyEorNlri(bgp.AFI_IP, bgp.SAFI_UNICAST), 99 Family: &api.Family{ 100 Afi: api.Family_AFI_IP, 101 Safi: api.Family_SAFI_UNICAST, 102 }, 103 Pattrs: []*anypb.Any{}, 104 Validation: &api.Validation{}, 105 NeighborIp: "10.12.12.12", 106 SourceId: "10.10.10.10", 107 }, 108 }, 109 { 110 name: "eor vpn path", 111 args: args{ 112 path: eor(bgp.RF_IPv4_VPN), 113 }, 114 want: &api.Path{ 115 Nlri: anyEorNlri(bgp.AFI_IP, bgp.SAFI_MPLS_VPN), 116 Family: &api.Family{ 117 Afi: api.Family_AFI_IP, 118 Safi: api.Family_SAFI_MPLS_VPN, 119 }, 120 Pattrs: []*anypb.Any{}, 121 Validation: &api.Validation{}, 122 NeighborIp: "10.12.12.12", 123 SourceId: "10.10.10.10", 124 }, 125 }, 126 } 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 apiPath := toPathApi(tt.args.path, tt.args.v, tt.args.onlyBinary, tt.args.nlriBinary, tt.args.attributeBinary) 130 assert.Equal(t, tt.want.Nlri, apiPath.Nlri, "not equal nlri") 131 assert.Equal(t, tt.want.Pattrs, apiPath.Pattrs, "not equal attrs") 132 assert.Equal(t, tt.want.Family, apiPath.Family, "not equal family") 133 assert.Equal(t, tt.want.NeighborIp, apiPath.NeighborIp, "not equal neighbor") 134 }) 135 } 136 } 137 138 func eor(f bgp.RouteFamily) *table.Path { 139 p := table.NewEOR(f) 140 p.SetSource(&table.PeerInfo{ 141 ID: net.IP{10, 10, 10, 10}, 142 LocalID: net.IP{10, 11, 11, 11}, 143 Address: net.IP{10, 12, 12, 12}, 144 LocalAddress: net.IP{10, 13, 13, 13}, 145 }) 146 return p 147 } 148 149 func anyEorNlri(afi uint16, safi uint8) *anypb.Any { 150 n, _ := bgp.NewPrefixFromRouteFamily(afi, safi) 151 return anyNlri(n) 152 } 153 154 func anyNlri(nlri bgp.AddrPrefixInterface) *anypb.Any { 155 anyNlri, _ := apiutil.MarshalNLRI(nlri) 156 return anyNlri 157 } 158 159 func anyAttrs(attrs []bgp.PathAttributeInterface) []*anypb.Any { 160 anyPattrs, _ := apiutil.MarshalPathAttributes(attrs) 161 return anyPattrs 162 }