github.com/osrg/gobgp/v3@v3.30.0/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/v3/internal/pkg/table" 26 "github.com/osrg/gobgp/v3/pkg/log" 27 "github.com/osrg/gobgp/v3/pkg/zebra" 28 ) 29 30 func Test_newPathFromIPRouteMessage(t *testing.T) { 31 assert := assert.New(t) 32 for v := zebra.MinZapiVer; v <= zebra.MaxZapiVer; v++ { 33 // IPv4 Route Add 34 m := &zebra.Message{} 35 software := zebra.NewSoftware(v, "") 36 flag := zebra.FlagSelected.ToEach(v, software) 37 message := zebra.MessageNexthop | zebra.MessageDistance.ToEach(v, software) | zebra.MessageMetric.ToEach(v, software) | zebra.MessageMTU.ToEach(v, software) 38 h := &zebra.Header{ 39 Len: zebra.HeaderSize(v), 40 Marker: zebra.HeaderMarker(v), 41 Version: v, 42 Command: zebra.RouteAdd.ToEach(v, software), 43 } 44 b := &zebra.IPRouteBody{ 45 Type: zebra.RouteType(zebra.RouteStatic), 46 Flags: flag, 47 Message: message, 48 Safi: zebra.Safi(zebra.SafiUnicast), // 1, FRR_ZAPI5_SAFI_UNICAST is same 49 Prefix: zebra.Prefix{ 50 Prefix: net.ParseIP("192.168.100.0"), 51 PrefixLen: uint8(24), 52 }, 53 Nexthops: []zebra.Nexthop{ 54 { 55 Gate: net.ParseIP("0.0.0.0"), 56 }, 57 { 58 Ifindex: uint32(1), 59 }, 60 }, 61 Distance: uint8(0), 62 Metric: uint32(100), 63 Mtu: uint32(0), 64 API: zebra.APIType(zebra.RouteAdd.ToEach(v, software)), 65 } 66 m.Header = *h 67 m.Body = b 68 logger := log.NewDefaultLogger() 69 zebra.BackwardIPv6RouteDelete.ToEach(v, software) 70 path := newPathFromIPRouteMessage(logger, m, v, software) 71 pp := table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false) 72 pp.SetIsFromExternal(path.IsFromExternal()) 73 assert.Equal("0.0.0.0", pp.GetNexthop().String()) 74 assert.Equal("192.168.100.0/24", pp.GetNlri().String()) 75 assert.True(pp.IsFromExternal()) 76 assert.False(pp.IsWithdraw) 77 78 // IPv4 Route Delete 79 h.Command = zebra.RouteDelete.ToEach(v, software) 80 b.API = zebra.RouteDelete.ToEach(v, software) 81 m.Header = *h 82 m.Body = b 83 84 path = newPathFromIPRouteMessage(logger, m, v, software) 85 pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false) 86 pp.SetIsFromExternal(path.IsFromExternal()) 87 assert.Equal("0.0.0.0", pp.GetNexthop().String()) 88 assert.Equal("192.168.100.0/24", pp.GetNlri().String()) 89 med, _ := pp.GetMed() 90 assert.Equal(uint32(100), med) 91 assert.True(pp.IsFromExternal()) 92 assert.True(pp.IsWithdraw) 93 94 // IPv6 Route Add 95 h.Command = zebra.RouteAdd.ToEach(v, software) 96 if v < 5 { 97 h.Command = zebra.BackwardIPv6RouteAdd.ToEach(v, software) 98 } 99 b.API = zebra.RouteAdd.ToEach(v, software) 100 if v < 5 { 101 b.API = zebra.BackwardIPv6RouteAdd.ToEach(v, software) 102 } 103 b.Prefix.Prefix = net.ParseIP("2001:db8:0:f101::") 104 b.Prefix.PrefixLen = uint8(64) 105 b.Nexthops = []zebra.Nexthop{{Gate: net.ParseIP("::")}} 106 m.Header = *h 107 m.Body = b 108 109 path = newPathFromIPRouteMessage(logger, m, v, software) 110 pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false) 111 pp.SetIsFromExternal(path.IsFromExternal()) 112 assert.Equal("::", pp.GetNexthop().String()) 113 assert.Equal("2001:db8:0:f101::/64", pp.GetNlri().String()) 114 med, _ = pp.GetMed() 115 assert.Equal(uint32(100), med) 116 assert.True(pp.IsFromExternal()) 117 assert.False(pp.IsWithdraw) 118 119 // IPv6 Route Delete 120 h.Command = zebra.RouteDelete.ToEach(v, software) 121 if v < 5 { 122 h.Command = zebra.BackwardIPv6RouteDelete.ToEach(v, software) 123 } 124 b.API = zebra.RouteDelete.ToEach(v, software) 125 if v < 5 { 126 b.API = zebra.BackwardIPv6RouteDelete.ToEach(v, software) 127 } 128 m.Header = *h 129 m.Body = b 130 131 path = newPathFromIPRouteMessage(logger, m, v, software) 132 pp = table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false) 133 pp.SetIsFromExternal(path.IsFromExternal()) 134 assert.Equal("::", pp.GetNexthop().String()) 135 assert.Equal("2001:db8:0:f101::/64", pp.GetNlri().String()) 136 assert.True(pp.IsFromExternal()) 137 assert.True(pp.IsWithdraw) 138 } 139 }