diff options
Diffstat (limited to 'packet')
-rw-r--r-- | packet/bgp/bgp.go | 89 | ||||
-rw-r--r-- | packet/bgp/bgp_test.go | 188 | ||||
-rw-r--r-- | packet/bgp/validate.go | 2 | ||||
-rw-r--r-- | packet/bgp/validate_test.go | 6 | ||||
-rw-r--r-- | packet/bmp/bmp_test.go | 20 | ||||
-rw-r--r-- | packet/rtr/rtr_test.go | 22 |
6 files changed, 126 insertions, 201 deletions
diff --git a/packet/bgp/bgp.go b/packet/bgp/bgp.go index 361da392..b882f9a6 100644 --- a/packet/bgp/bgp.go +++ b/packet/bgp/bgp.go @@ -289,13 +289,13 @@ func (c BGPCapabilityCode) String() string { var ( // Used parsing RouteDistinguisher - _regexpRouteDistinguisher = regexp.MustCompile("^((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)|((\\d+)\\.)?(\\d+)|([\\w]+:[\\w:]*:[\\w]+)):(\\d+)$") + _regexpRouteDistinguisher = regexp.MustCompile(`^((\d+)\.(\d+)\.(\d+)\.(\d+)|((\d+)\.)?(\d+)|([\w]+:[\w:]*:[\w]+)):(\d+)$`) // Used for operator and value for the FlowSpec numeric type // Example: // re.FindStringSubmatch("&==80") // >>> ["&==80" "&" "==" "80"] - _regexpFlowSpecNumericType = regexp.MustCompile("(&?)(==|=|>|>=|<|<=|!|!=|=!)?(\\d+|-\\d|true|false)") + _regexpFlowSpecNumericType = regexp.MustCompile(`(&?)(==|=|>|>=|<|<=|!|!=|=!)?(\d+|-\d|true|false)`) // - "=!" is used in the old style format of "tcp-flags" and "fragment". // - The value field should be one of the followings: @@ -303,8 +303,8 @@ var ( // * Combination of the small letters, decimals, "-" and "+" // (e.g., tcp, ipv4, is-fragment+first-fragment) // * Capital letters (e.g., SA) - _regexpFlowSpecOperator = regexp.MustCompile("&|=|>|<|!|[\\w\\-+]+") - _regexpFlowSpecOperatorValue = regexp.MustCompile("[\\w\\-+]+") + _regexpFlowSpecOperator = regexp.MustCompile(`&|=|>|<|!|[\w\-+]+`) + _regexpFlowSpecOperatorValue = regexp.MustCompile(`[\w\-+]+`) // Note: "(-*)" and "(.*)" catch the invalid flags // Example: In this case, "Z" is unsupported flag type. @@ -315,13 +315,13 @@ var ( // Note: "(.*)" catches the invalid flags // re.FindStringSubmatch("&!=+first-fragment+last-fragment+invalid-fragment") // >>> ["&!=+first-fragment+last-fragment+invalid-fragment" "&" "!=" "+first-fragment+last-fragment" "+last-fragment" "+" "last" "+invalid-fragment"] - _regexpFlowSpecFragment = regexp.MustCompile("(&?)(==|=|!|!=|=!)?(((\\+)?(dont|is|first|last|not-a)-fragment)+)(.*)") + _regexpFlowSpecFragment = regexp.MustCompile(`(&?)(==|=|!|!=|=!)?(((\+)?(dont|is|first|last|not-a)-fragment)+)(.*)`) // re.FindStringSubmatch("192.168.0.0/24") // >>> ["192.168.0.0/24" "192.168.0.0" "/24" "24"] // re.FindStringSubmatch("192.168.0.1") // >>> ["192.168.0.1" "192.168.0.1" "" ""] - _regexpFindIPv4Prefix = regexp.MustCompile("^([\\d.]+)(/(\\d{1,2}))?") + _regexpFindIPv4Prefix = regexp.MustCompile(`^([\d.]+)(/(\d{1,2}))?`) // re.FindStringSubmatch("2001:dB8::/64") // >>> ["2001:dB8::/64" "2001:dB8::" "/64" "64" "" ""] @@ -329,7 +329,7 @@ var ( // >>> ["2001:dB8::/64/8" "2001:dB8::" "/64" "64" "/8" "8"] // re.FindStringSubmatch("2001:dB8::1") // >>> ["2001:dB8::1" "2001:dB8::1" "" "" "" ""] - _regexpFindIPv6Prefix = regexp.MustCompile("^([a-fA-F\\d:.]+)(/(\\d{1,3}))?(/(\\d{1,3}))?") + _regexpFindIPv6Prefix = regexp.MustCompile(`^([a-fA-F\d:.]+)(/(\d{1,3}))?(/(\d{1,3}))?`) ) type ParameterCapabilityInterface interface { @@ -1572,7 +1572,8 @@ func (l *MPLSLabelStack) DecodeFromBytes(data []byte) error { break } } - if foundBottom == false { + + if foundBottom { l.Labels = []uint32{} return nil } @@ -2161,7 +2162,7 @@ func ParseEthernetSegmentIdentifier(args []string) (EthernetSegmentIdentifier, e } invalidEsiValuesError := fmt.Errorf("invalid esi values for type %s: %s", esi.Type.String(), args[1:]) - esi.Value = make([]byte, 9, 9) + esi.Value = make([]byte, 9) switch esi.Type { case ESI_LACP: fallthrough @@ -2196,7 +2197,7 @@ func ParseEthernetSegmentIdentifier(args []string) (EthernetSegmentIdentifier, e if err != nil { return esi, invalidEsiValuesError } - iBuf := make([]byte, 4, 4) + iBuf := make([]byte, 4) binary.BigEndian.PutUint32(iBuf, uint32(i)) copy(esi.Value[6:9], iBuf[1:4]) case ESI_ROUTERID: @@ -2278,7 +2279,7 @@ func labelSerialize(label uint32) ([]byte, error) { if label > 0xffffff { return nil, NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Out of range Label: %d", label)) } - buf := make([]byte, 3, 3) + buf := make([]byte, 3) buf[0] = byte((label >> 16) & 0xff) buf[1] = byte((label >> 8) & 0xff) buf[2] = byte(label & 0xff) @@ -3951,13 +3952,10 @@ func (v *FlowSpecComponentItem) Len() int { } func (v *FlowSpecComponentItem) Serialize() ([]byte, error) { - if v.Value < 0 { - return nil, fmt.Errorf("invalid value size(too small): %d", v.Value) - } - if v.Op < 0 || v.Op > math.MaxUint8 { + if v.Op > math.MaxUint8 { return nil, fmt.Errorf("invalid op size: %d", v.Op) - } + order := uint32(math.Log2(float64(v.Len()))) buf := make([]byte, 1+(1<<order)) buf[0] = byte(uint32(v.Op) | order<<4) @@ -5488,7 +5486,7 @@ func (p *PathAttributeAsPath) DecodeFromBytes(data []byte, options ...*Marshalli } for len(value) > 0 { var tuple AsPathParamInterface - if isAs4 == true { + if isAs4 { tuple = &As4PathParam{} } else { tuple = &AsPathParam{} @@ -5498,9 +5496,6 @@ func (p *PathAttributeAsPath) DecodeFromBytes(data []byte, options ...*Marshalli return err } p.Value = append(p.Value, tuple) - if tuple.Len() > len(value) { - - } value = value[tuple.Len():] } return nil @@ -5877,19 +5872,19 @@ type WellKnownCommunity uint32 const ( COMMUNITY_INTERNET WellKnownCommunity = 0x00000000 - COMMUNITY_PLANNED_SHUT = 0xffff0000 - COMMUNITY_ACCEPT_OWN = 0xffff0001 - COMMUNITY_ROUTE_FILTER_TRANSLATED_v4 = 0xffff0002 - COMMUNITY_ROUTE_FILTER_v4 = 0xffff0003 - COMMUNITY_ROUTE_FILTER_TRANSLATED_v6 = 0xffff0004 - COMMUNITY_ROUTE_FILTER_v6 = 0xffff0005 - COMMUNITY_LLGR_STALE = 0xffff0006 - COMMUNITY_NO_LLGR = 0xffff0007 - COMMUNITY_BLACKHOLE = 0xffff029a - COMMUNITY_NO_EXPORT = 0xffffff01 - COMMUNITY_NO_ADVERTISE = 0xffffff02 - COMMUNITY_NO_EXPORT_SUBCONFED = 0xffffff03 - COMMUNITY_NO_PEER = 0xffffff04 + COMMUNITY_PLANNED_SHUT WellKnownCommunity = 0xffff0000 + COMMUNITY_ACCEPT_OWN WellKnownCommunity = 0xffff0001 + COMMUNITY_ROUTE_FILTER_TRANSLATED_v4 WellKnownCommunity = 0xffff0002 + COMMUNITY_ROUTE_FILTER_v4 WellKnownCommunity = 0xffff0003 + COMMUNITY_ROUTE_FILTER_TRANSLATED_v6 WellKnownCommunity = 0xffff0004 + COMMUNITY_ROUTE_FILTER_v6 WellKnownCommunity = 0xffff0005 + COMMUNITY_LLGR_STALE WellKnownCommunity = 0xffff0006 + COMMUNITY_NO_LLGR WellKnownCommunity = 0xffff0007 + COMMUNITY_BLACKHOLE WellKnownCommunity = 0xffff029a + COMMUNITY_NO_EXPORT WellKnownCommunity = 0xffffff01 + COMMUNITY_NO_ADVERTISE WellKnownCommunity = 0xffffff02 + COMMUNITY_NO_EXPORT_SUBCONFED WellKnownCommunity = 0xffffff03 + COMMUNITY_NO_PEER WellKnownCommunity = 0xffffff04 ) var WellKnownCommunityNameMap = map[WellKnownCommunity]string{ @@ -5996,7 +5991,7 @@ func (p *PathAttributeOriginatorId) MarshalJSON() ([]byte, error) { } func (p *PathAttributeOriginatorId) Serialize(options ...*MarshallingOption) ([]byte, error) { - buf := make([]byte, 4, 4) + buf := make([]byte, 4) copy(buf, p.Value) return p.PathAttribute.Serialize(buf, options...) } @@ -6710,7 +6705,7 @@ type ValidationExtended struct { } func (e *ValidationExtended) Serialize() ([]byte, error) { - buf := make([]byte, 8, 8) + buf := make([]byte, 8) typ, subType := e.GetTypes() buf[0] = byte(typ) buf[1] = byte(subType) @@ -6750,7 +6745,7 @@ type ColorExtended struct { } func (e *ColorExtended) Serialize() ([]byte, error) { - buf := make([]byte, 8, 8) + buf := make([]byte, 8) typ, subType := e.GetTypes() buf[0] = byte(typ) buf[1] = byte(subType) @@ -6790,7 +6785,7 @@ type EncapExtended struct { } func (e *EncapExtended) Serialize() ([]byte, error) { - buf := make([]byte, 8, 8) + buf := make([]byte, 8) typ, subType := e.GetTypes() buf[0] = byte(typ) buf[1] = byte(subType) @@ -6850,7 +6845,7 @@ type DefaultGatewayExtended struct { } func (e *DefaultGatewayExtended) Serialize() ([]byte, error) { - buf := make([]byte, 8, 8) + buf := make([]byte, 8) typ, subType := e.GetTypes() buf[0] = byte(typ) buf[1] = byte(subType) @@ -6889,7 +6884,7 @@ func (e *OpaqueExtended) Serialize() ([]byte, error) { if len(e.Value) != 7 { return nil, fmt.Errorf("invalid value length for opaque extended community: %d", len(e.Value)) } - buf := make([]byte, 8, 8) + buf := make([]byte, 8) if e.IsTransitive { buf[0] = byte(EC_TYPE_TRANSITIVE_OPAQUE) } else { @@ -6900,7 +6895,7 @@ func (e *OpaqueExtended) Serialize() ([]byte, error) { } func (e *OpaqueExtended) String() string { - buf := make([]byte, 8, 8) + buf := make([]byte, 8) copy(buf[1:], e.Value) return fmt.Sprintf("%d", binary.BigEndian.Uint64(buf)) } @@ -6931,7 +6926,7 @@ func (e *OpaqueExtended) MarshalJSON() ([]byte, error) { } func NewOpaqueExtended(isTransitive bool, value []byte) *OpaqueExtended { - v := make([]byte, 7, 7) + v := make([]byte, 7) copy(v, value) return &OpaqueExtended{ IsTransitive: isTransitive, @@ -7552,7 +7547,7 @@ func (e *UnknownExtended) Serialize() ([]byte, error) { if len(e.Value) != 7 { return nil, fmt.Errorf("invalid value length for unknown extended community: %d", len(e.Value)) } - buf := make([]byte, 8, 8) + buf := make([]byte, 8) buf[0] = uint8(e.Type) copy(buf[1:], e.Value) return buf, nil @@ -7587,7 +7582,7 @@ func (e *UnknownExtended) GetTypes() (ExtendedCommunityAttrType, ExtendedCommuni } func NewUnknownExtended(typ ExtendedCommunityAttrType, value []byte) *UnknownExtended { - v := make([]byte, 7, 7) + v := make([]byte, 7) copy(v, value) return &UnknownExtended{ Type: typ, @@ -7731,9 +7726,11 @@ func (p *PathAttributeAs4Path) DecodeFromBytes(data []byte, options ...*Marshall if err != nil { return err } - if isAs4 == false { + + if !isAs4 { return NewMessageError(eCode, eSubCode, nil, "AS4 PATH param is malformed") } + for len(value) > 0 { tuple := &As4PathParam{} tuple.DecodeFromBytes(value) @@ -8149,7 +8146,7 @@ func (p *TunnelEncapTLV) Serialize() ([]byte, error) { } func (p *TunnelEncapTLV) String() string { - tlvList := make([]string, len(p.Value), len(p.Value)) + tlvList := make([]string, len(p.Value)) for i, v := range p.Value { tlvList[i] = v.String() } @@ -8208,7 +8205,7 @@ func (p *PathAttributeTunnelEncap) Serialize(options ...*MarshallingOption) ([]b } func (p *PathAttributeTunnelEncap) String() string { - tlvList := make([]string, len(p.Value), len(p.Value)) + tlvList := make([]string, len(p.Value)) for i, v := range p.Value { tlvList[i] = v.String() } diff --git a/packet/bgp/bgp_test.go b/packet/bgp/bgp_test.go index 1d6f386f..555369db 100644 --- a/packet/bgp/bgp_test.go +++ b/packet/bgp/bgp_test.go @@ -19,11 +19,11 @@ import ( "bytes" "encoding/binary" "net" - "reflect" "strconv" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func keepalive() *BGPMessage { @@ -50,23 +50,18 @@ func BenchmarkNormalizeFlowSpecOpValues(b *testing.B) { func Test_Message(t *testing.T) { l := []*BGPMessage{keepalive(), notification(), refresh(), NewTestBGPOpenMessage(), NewTestBGPUpdateMessage()} + for _, m1 := range l { - buf1, _ := m1.Serialize() + buf1, err := m1.Serialize() + require.NoError(t, err) + t.Log("LEN =", len(buf1)) m2, err := ParseBGPMessage(buf1) - if err != nil { - t.Error(err) - } + require.NoError(t, err) + // FIXME: shouldn't but workaround for some structs. - buf2, _ := m2.Serialize() - - if reflect.DeepEqual(m1, m2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), m1, buf1) - t.Error(len(buf2), m2, buf2) - } + + assert.Equal(t, m1, m2) } } @@ -395,26 +390,24 @@ func Test_FlowSpecNlri(t *testing.T) { lastFragment := uint64(0x08) item5 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_MATCH, isFragment) item6 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND, lastFragment) + cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item5, item6})) item7 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK) item8 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND|BITMASK_FLAG_OP_NOT, TCP_FLAG_URGENT) + cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item7, item8})) n1 := NewFlowSpecIPv4Unicast(cmp) + buf1, err := n1.Serialize() assert.Nil(err) + n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_UC)) assert.Nil(err) + err = n2.DecodeFromBytes(buf1) assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + // should be equal + assert.Equal(n1, n2) } func Test_FlowSpecExtended(t *testing.T) { @@ -428,42 +421,36 @@ func Test_FlowSpecExtended(t *testing.T) { exts = append(exts, NewTrafficRemarkExtended(10)) m1 := NewPathAttributeExtendedCommunities(exts) buf1, err := m1.Serialize() - assert.Nil(err) + require.NoError(t, err) + m2 := NewPathAttributeExtendedCommunities(nil) err = m2.DecodeFromBytes(buf1) - assert.Nil(err) - buf2, _ := m2.Serialize() - if reflect.DeepEqual(m1, m2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), m1, buf1) - t.Error(len(buf2), m2, buf2) - } + require.NoError(t, err) + + _, err = m2.Serialize() + require.NoError(t, err) + + assert.Equal(m1, m2) } func Test_IP6FlowSpecExtended(t *testing.T) { - assert := assert.New(t) exts := make([]ExtendedCommunityInterface, 0) exts = append(exts, NewRedirectIPv6AddressSpecificExtended("2001:db8::68", 1000)) m1 := NewPathAttributeIP6ExtendedCommunities(exts) buf1, err := m1.Serialize() - assert.Nil(err) + require.NoError(t, err) + m2 := NewPathAttributeIP6ExtendedCommunities(nil) err = m2.DecodeFromBytes(buf1) - assert.Nil(err) - buf2, _ := m2.Serialize() - if reflect.DeepEqual(m1, m2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), m1, buf1) - t.Error(len(buf2), m2, buf2) - } + require.NoError(t, err) + + _, err = m2.Serialize() + require.NoError(t, err) + + assert.Equal(t, m1, m2) } func Test_FlowSpecNlriv6(t *testing.T) { - assert := assert.New(t) cmp := make([]FlowSpecComponentInterface, 0) cmp = append(cmp, NewFlowSpecDestinationPrefix6(NewIPv6AddrPrefix(64, "2001::"), 12)) cmp = append(cmp, NewFlowSpecSourcePrefix6(NewIPv6AddrPrefix(64, "2001::"), 12)) @@ -488,20 +475,18 @@ func Test_FlowSpecNlriv6(t *testing.T) { cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item6, item7})) n1 := NewFlowSpecIPv6Unicast(cmp) buf1, err := n1.Serialize() - assert.Nil(err) + require.NoError(t, err) + n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv6_UC)) - assert.Nil(err) + require.NoError(t, err) + err = n2.DecodeFromBytes(buf1) - assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + require.NoError(t, err) + + _, err = n2.Serialize() + require.NoError(t, err) + + assert.Equal(t, n1, n2) } func Test_Aigp(t *testing.T) { @@ -509,19 +494,13 @@ func Test_Aigp(t *testing.T) { m := NewAigpTLVIgpMetric(1000) a1 := NewPathAttributeAigp([]AigpTLVInterface{m}) buf1, err := a1.Serialize() - assert.Nil(err) + require.NoError(t, err) + a2 := NewPathAttributeAigp(nil) err = a2.DecodeFromBytes(buf1) - assert.Nil(err) - buf2, _ := a2.Serialize() - if reflect.DeepEqual(a1, a2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), a1, buf1) - t.Error(len(buf2), a2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + require.NoError(t, err) + + assert.Equal(a1, a2) } func Test_FlowSpecNlriL2(t *testing.T) { @@ -540,15 +519,8 @@ func Test_FlowSpecNlriL2(t *testing.T) { assert.Nil(err) err = n2.DecodeFromBytes(buf1) assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + + assert.Equal(n1, n2) } func Test_NotificationErrorCode(t *testing.T) { @@ -572,16 +544,9 @@ func Test_FlowSpecNlriVPN(t *testing.T) { n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_VPN)) assert.Nil(err) err = n2.DecodeFromBytes(buf1) - assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + require.NoError(t, err) + + assert.Equal(n1, n2) } func Test_EVPNIPPrefixRoute(t *testing.T) { @@ -606,17 +571,8 @@ func Test_EVPNIPPrefixRoute(t *testing.T) { assert.Nil(err) err = n2.DecodeFromBytes(buf1) assert.Nil(err) - buf2, _ := n2.Serialize() - t.Log(n1.RouteTypeData.(*EVPNIPPrefixRoute).ESI.Value, n2.(*EVPNNLRI).RouteTypeData.(*EVPNIPPrefixRoute).ESI.Value) - t.Log(reflect.DeepEqual(n1.RouteTypeData.(*EVPNIPPrefixRoute).ESI.Value, n2.(*EVPNNLRI).RouteTypeData.(*EVPNIPPrefixRoute).ESI.Value)) - if reflect.DeepEqual(n1, n2) { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + + assert.Equal(n1, n2) } func Test_CapExtendedNexthop(t *testing.T) { @@ -627,15 +583,8 @@ func Test_CapExtendedNexthop(t *testing.T) { assert.Nil(err) n2, err := DecodeCapability(buf1) assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + + assert.Equal(n1, n2) } func Test_AddPath(t *testing.T) { @@ -815,15 +764,8 @@ func Test_MpReachNLRIWithIPv4MappedIPv6Prefix(t *testing.T) { assert.Nil(err) err = n2.DecodeFromBytes(buf1) assert.Nil(err) - buf2, _ := n2.Serialize() - if reflect.DeepEqual(n1, n2) { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n1, buf1) - t.Error(len(buf2), n2, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + + assert.Equal(n1, n2) label := NewMPLSLabelStack(2) @@ -834,16 +776,8 @@ func Test_MpReachNLRIWithIPv4MappedIPv6Prefix(t *testing.T) { assert.Nil(err) err = n4.DecodeFromBytes(buf1) assert.Nil(err) - buf2, _ = n3.Serialize() - t.Log(n3, n4) - if reflect.DeepEqual(n3, n4) { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), n3, buf1) - t.Error(len(buf2), n4, buf2) - t.Log(bytes.Equal(buf1, buf2)) - } + + assert.Equal(n3, n4) } func Test_MpReachNLRIWithIPv6PrefixWithIPv4Peering(t *testing.T) { @@ -1133,7 +1067,7 @@ func Test_ParseEthernetSegmentIdentifier(t *testing.T) { // "single-homed" esiZero := EthernetSegmentIdentifier{} - args := make([]string, 0, 0) + args := make([]string, 0) esi, err := ParseEthernetSegmentIdentifier(args) assert.Nil(err) assert.Equal(esiZero, esi) diff --git a/packet/bgp/validate.go b/packet/bgp/validate.go index 1b8f27c9..60cf26e4 100644 --- a/packet/bgp/validate.go +++ b/packet/bgp/validate.go @@ -274,7 +274,7 @@ func validateAsPathValueBytes(data []byte) (bool, error) { return false, NewMessageError(eCode, eSubCode, nil, "AS PATH the number of AS is incorrect") } segLength := int(asNum) - if use4byte == true { + if use4byte { segLength *= 4 } else { segLength *= 2 diff --git a/packet/bgp/validate_test.go b/packet/bgp/validate_test.go index 4228c260..f780b5cc 100644 --- a/packet/bgp/validate_test.go +++ b/packet/bgp/validate_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func bgpupdate() *BGPMessage { @@ -47,6 +48,7 @@ func Test_Validate_CapV4(t *testing.T) { assert.Error(err) res, err = ValidateUpdateMsg(message, map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}, false, false) + require.NoError(t, err) assert.Equal(true, res) } @@ -58,6 +60,7 @@ func Test_Validate_CapV6(t *testing.T) { assert.NoError(err) res, err = ValidateUpdateMsg(message, map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}, false, false) + require.NoError(t, err) assert.Equal(false, res) } @@ -295,12 +298,12 @@ func Test_Validate_unrecognized_well_known(t *testing.T) { } func Test_Validate_aspath(t *testing.T) { - assert := assert.New(t) message := bgpupdate().Body.(*BGPUpdate) // VALID AS_PATH res, err := ValidateUpdateMsg(message, map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}, true, false) + require.NoError(t, err) assert.Equal(true, res) // CONFED_SET @@ -360,6 +363,7 @@ func Test_Validate_aspath(t *testing.T) { assert.Nil(e.Data) res, err = ValidateUpdateMsg(message, map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}, true, true) + require.NoError(t, err) assert.Equal(true, res) } diff --git a/packet/bmp/bmp_test.go b/packet/bmp/bmp_test.go index 266d6b99..fde4cd0f 100644 --- a/packet/bmp/bmp_test.go +++ b/packet/bmp/bmp_test.go @@ -16,27 +16,21 @@ package bmp import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/osrg/gobgp/packet/bgp" + "github.com/stretchr/testify/assert" - "reflect" - "testing" ) func verify(t *testing.T, m1 *BMPMessage) { buf1, _ := m1.Serialize() m2, err := ParseBMPMessage(buf1) - if err != nil { - t.Error(err) - } - buf2, _ := m2.Serialize() + require.NoError(t, err) - if reflect.DeepEqual(m1, m2) == true { - t.Log("OK") - } else { - t.Error("Something wrong") - t.Error(len(buf1), m1, buf1) - t.Error(len(buf2), m2, buf2) - } + assert.Equal(t, m1, m2) } func Test_Initiation(t *testing.T) { diff --git a/packet/rtr/rtr_test.go b/packet/rtr/rtr_test.go index 961805d5..08f930b2 100644 --- a/packet/rtr/rtr_test.go +++ b/packet/rtr/rtr_test.go @@ -19,26 +19,22 @@ import ( "encoding/hex" "math/rand" "net" - "reflect" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func verifyRTRMessage(t *testing.T, m1 RTRMessage) { buf1, _ := m1.Serialize() m2, err := ParseRTR(buf1) - if err != nil { - t.Error(err) - } - buf2, _ := m2.Serialize() - - if reflect.DeepEqual(buf1, buf2) == true { - t.Log("OK") - } else { - t.Errorf("Something wrong") - t.Error(len(buf1), m1, hex.EncodeToString(buf1)) - t.Error(len(buf2), m2, hex.EncodeToString(buf2)) - } + require.NoError(t, err) + + buf2, err := m2.Serialize() + require.NoError(t, err) + + assert.Equal(t, buf1, buf2, "buf1: %v buf2: %v", hex.EncodeToString(buf1), hex.EncodeToString(buf2)) } func randUint32() uint32 { |