Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sort #223

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions asn/asn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{{}},
},
}

Expand All @@ -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)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading