mirror of
https://github.com/moby/moby.git
synced 2026-08-03 14:41:03 +00:00
Follow-up to PR 52804, applying thaJeztah's review suggestion: check IsLoopback first for both address families (preserving any requested loopback address), and only fall back to the canonical loopback for the family otherwise. No behavior change; ::1 now returns through the loopback-preserving branch instead of the IPv6 fallback, with the same result. Signed-off-by: Andrew Liu <andrewjliu22@gmail.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package rlkclient
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestChildHostIP(t *testing.T) {
|
|
builtin := &PortDriverClient{
|
|
portDriverName: "builtin",
|
|
protos: map[string]struct{}{"tcp4": {}, "tcp6": {}, "udp4": {}, "udp6": {}},
|
|
}
|
|
slirp4netns := &PortDriverClient{
|
|
portDriverName: "slirp4netns",
|
|
protos: map[string]struct{}{"tcp4": {}, "udp4": {}},
|
|
childIP: netip.MustParseAddr("10.0.2.100"),
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
pdc *PortDriverClient
|
|
proto string
|
|
hostIP netip.Addr
|
|
want netip.Addr
|
|
}{
|
|
{
|
|
name: "nil client",
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("127.0.1.2"),
|
|
want: netip.MustParseAddr("127.0.1.2"),
|
|
},
|
|
{
|
|
name: "unsupported proto",
|
|
pdc: slirp4netns,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("::1"),
|
|
},
|
|
{
|
|
name: "forced child IP",
|
|
pdc: slirp4netns,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("127.0.1.2"),
|
|
want: netip.MustParseAddr("10.0.2.100"),
|
|
},
|
|
{
|
|
name: "v4 unspecified",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("0.0.0.0"),
|
|
want: netip.MustParseAddr("127.0.0.1"),
|
|
},
|
|
{
|
|
name: "v4 non-loopback",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("192.0.2.1"),
|
|
want: netip.MustParseAddr("127.0.0.1"),
|
|
},
|
|
{
|
|
name: "v4 default loopback",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("127.0.0.1"),
|
|
want: netip.MustParseAddr("127.0.0.1"),
|
|
},
|
|
{
|
|
// Distinct loopback addresses must not be collapsed to
|
|
// 127.0.0.1, or bindings on the same port collide in the
|
|
// child namespace.
|
|
// Regression test for https://github.com/moby/moby/issues/52783
|
|
name: "v4 non-default loopback",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("127.0.1.2"),
|
|
want: netip.MustParseAddr("127.0.1.2"),
|
|
},
|
|
{
|
|
name: "v6 loopback",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("::1"),
|
|
want: netip.IPv6Loopback(),
|
|
},
|
|
{
|
|
name: "v6 non-loopback",
|
|
pdc: builtin,
|
|
proto: "tcp",
|
|
hostIP: netip.MustParseAddr("2001:db8::1"),
|
|
want: netip.IPv6Loopback(),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := tc.pdc.ChildHostIP(tc.proto, tc.hostIP)
|
|
assert.Check(t, is.Equal(got, tc.want))
|
|
})
|
|
}
|
|
}
|