From 341e37708a2e413534b8440e0f92387aa3011a5f Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Tue, 19 Sep 2023 17:38:13 +0200 Subject: [PATCH] adding sort --- asn/asn_test.go | 33 ++++++++++++++++++++------------- ip.go | 3 +++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/asn/asn_test.go b/asn/asn_test.go index 2b6cf29..2ed7876 100644 --- a/asn/asn_test.go +++ b/asn/asn_test.go @@ -4,30 +4,31 @@ import ( "os" "testing" + sliceutil "github.com/projectdiscovery/utils/slice" stringsutil "github.com/projectdiscovery/utils/strings" "github.com/stretchr/testify/require" ) func Test_asnClient_GetCIDRsForASNNum(t *testing.T) { tests := []struct { - name string - asnNumber string - expected []string + name string + asnNumber string + potentiallyExpected [][]string }{ { - name: "ASN Number 1", - asnNumber: "AS14421", - expected: []string{"216.101.17.0/24"}, + name: "ASN Number 1", + asnNumber: "AS14421", + potentiallyExpected: [][]string{{"216.101.17.0/24"}}, }, { - name: "ASN Number 2", - asnNumber: "AS7712", - expected: []string{"118.67.200.0/23", "118.67.202.0/24", "118.67.203.0/24", "118.67.204.0/22"}, + name: "ASN Number 2", + asnNumber: "AS7712", + potentiallyExpected: [][]string{{"118.67.200.0/23", "118.67.202.0/24", "118.67.203.0/24", "118.67.204.0/22"}, {"118.67.200.0/21"}}, }, { - name: "Wrong ASN number", - asnNumber: "AS", - expected: []string{}, + name: "Wrong ASN number", + asnNumber: "AS", + potentiallyExpected: [][]string{{}}, }, } @@ -40,7 +41,13 @@ func Test_asnClient_GetCIDRsForASNNum(t *testing.T) { for _, cidr := range got { result = append(result, cidr.String()) } - require.ElementsMatch(t, tt.expected, result, "could not get correct cidrs") + var found bool + for _, expected := range tt.potentiallyExpected { + found = found || sliceutil.ElementsMatch(expected, result) + } + if !found { + t.Errorf("could not get correct cidrs: %v %v", tt.potentiallyExpected, result) + } } } diff --git a/ip.go b/ip.go index 11f29bc..fc04147 100644 --- a/ip.go +++ b/ip.go @@ -508,6 +508,9 @@ func CoalesceCIDRs(cidrs []*net.IPNet) (coalescedIPV4, coalescedIPV6 []*net.IPNe } func AggregateApproxIPV4s(ips []*net.IPNet) (approxIPs []*net.IPNet) { + sort.Slice(ips, func(i, j int) bool { + return bytes.Compare(ips[i].IP, ips[j].IP) < 0 + }) cidrs := make(map[string]*net.IPNet) for _, ip := range ips {