Skip to content
Commits on Source (1)
  • Eugene Burkov's avatar
    Pull request 276: Sort hotfix · 33a257f6
    Eugene Burkov authored
    Merge in GO/dnsproxy from hotfix-release to master
    
    Squashed commit of the following:
    
    commit 2dcd1bc7eafb0b5b44b7d1b42ff8cd5afd2da51f
    Author: Eugene Burkov <E.Burkov@AdGuard.COM>
    Date:   Wed Aug 9 14:14:19 2023 +0300
    
        workflows: upd golangci-lint
    
    commit 36e3419bd8c00d712f9e04bee5c22a67aa0858dc
    Author: Eugene Burkov <E.Burkov@AdGuard.COM>
    Date:   Wed Aug 9 14:00:25 2023 +0300
    
        netutil: imp tests, fix sort, fix lint
    33a257f6
......@@ -29,7 +29,7 @@ jobs:
with:
# This field is required. Dont set the patch version to always use
# the latest patch version.
version: v1.50.0
version: v1.52.0
notify:
needs:
- golangci
......
......@@ -74,7 +74,7 @@ func SortNetIPAddrs(addrs []netip.Addr, preferIPv6 bool) {
}
aIs4, bIs4 := addrA.Is4(), addrB.Is4()
if aIs4 != bIs4 {
if aIs4 == bIs4 {
return addrA.Compare(addrB)
}
......
package netutil_test
import (
"net/netip"
"testing"
"github.com/AdguardTeam/dnsproxy/internal/netutil"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)
func TestSortNetIPAddrs(t *testing.T) {
var (
aIPv4 = netip.MustParseAddr("1.2.3.4")
bIPv4 = netip.MustParseAddr("4.3.2.1")
aIPv6 = netip.MustParseAddr("2a00::1234")
bIPv6 = netip.MustParseAddr("2a00::4321")
badIP, _ = netip.ParseAddr("bad")
)
testCases := []struct {
name string
addrs []netip.Addr
want []netip.Addr
preferIPv6 bool
}{{
name: "v4_preferred",
addrs: []netip.Addr{aIPv6, bIPv6, badIP, aIPv4, bIPv4},
want: []netip.Addr{aIPv4, bIPv4, aIPv6, bIPv6, badIP},
preferIPv6: false,
}, {
name: "v6_preferred",
addrs: []netip.Addr{aIPv4, bIPv4, badIP, aIPv6, bIPv6},
want: []netip.Addr{aIPv6, bIPv6, aIPv4, bIPv4, badIP},
preferIPv6: true,
}, {
name: "shuffled_v4_preferred",
addrs: []netip.Addr{badIP, aIPv4, bIPv6, aIPv6, bIPv4},
want: []netip.Addr{aIPv4, bIPv4, aIPv6, bIPv6, badIP},
preferIPv6: false,
}, {
name: "shuffled_v6_preferred",
addrs: []netip.Addr{badIP, aIPv4, bIPv6, aIPv6, bIPv4},
want: []netip.Addr{aIPv6, bIPv6, aIPv4, bIPv4, badIP},
preferIPv6: true,
}, {
name: "empty",
addrs: []netip.Addr{},
want: []netip.Addr{},
preferIPv6: false,
}, {
name: "single",
addrs: []netip.Addr{aIPv4},
want: []netip.Addr{aIPv4},
preferIPv6: false,
}, {
name: "start_with_ipv4",
addrs: []netip.Addr{aIPv4, aIPv6, bIPv4, bIPv6},
want: []netip.Addr{aIPv6, bIPv6, aIPv4, bIPv4},
preferIPv6: true,
}, {
name: "start_with_ipv6",
addrs: []netip.Addr{aIPv6, aIPv4, bIPv6, bIPv4},
want: []netip.Addr{aIPv6, bIPv6, aIPv4, bIPv4},
preferIPv6: true,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ips := slices.Clone(tc.addrs)
netutil.SortNetIPAddrs(ips, tc.preferIPv6)
assert.Equal(t, tc.want, ips)
})
}
}