diff options
author | Kevin Krakauer <krakauer@google.com> | 2021-02-17 09:59:54 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-02-17 10:02:02 -0800 |
commit | 2f35fa14abe546b58c72312fbd23ad3f0b7a193f (patch) | |
tree | 75c0a29d8dd6e62a59ef0ad96306440ac9ee4756 /test/iptables/iptables_util.go | |
parent | 3ef012944d32313cee4df244585f48e8d4fd8e9e (diff) |
Deflake //test/iptables:iptables_test
We were opening dual-stack sockets, but tests only set iptables rules for a
single network protocol. Thus, packets (probably sent by previous tests) could
sneak in and break tests.
Tested with:
bazel test ///test/iptables:iptables_test
--test_arg=--runtime=runsc --nocache_test_results --test_output=errors
--runs_per_test=1000 --test_filter=TestFilterInputDropUDP
PiperOrigin-RevId: 357972385
Diffstat (limited to 'test/iptables/iptables_util.go')
-rw-r--r-- | test/iptables/iptables_util.go | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/test/iptables/iptables_util.go b/test/iptables/iptables_util.go index 4cd770a65..bba17b894 100644 --- a/test/iptables/iptables_util.go +++ b/test/iptables/iptables_util.go @@ -71,11 +71,11 @@ func tableRules(ipv6 bool, table string, argsList [][]string) error { // listenUDP listens on a UDP port and returns the value of net.Conn.Read() for // the first read on that port. -func listenUDP(ctx context.Context, port int) error { +func listenUDP(ctx context.Context, port int, ipv6 bool) error { localAddr := net.UDPAddr{ Port: port, } - conn, err := net.ListenUDP("udp", &localAddr) + conn, err := net.ListenUDP(udpNetwork(ipv6), &localAddr) if err != nil { return err } @@ -97,12 +97,12 @@ func listenUDP(ctx context.Context, port int) error { // sendUDPLoop sends 1 byte UDP packets repeatedly to the IP and port specified // over a duration. -func sendUDPLoop(ctx context.Context, ip net.IP, port int) error { +func sendUDPLoop(ctx context.Context, ip net.IP, port int, ipv6 bool) error { remote := net.UDPAddr{ IP: ip, Port: port, } - conn, err := net.DialUDP("udp", nil, &remote) + conn, err := net.DialUDP(udpNetwork(ipv6), nil, &remote) if err != nil { return err } @@ -126,13 +126,13 @@ func sendUDPLoop(ctx context.Context, ip net.IP, port int) error { } // listenTCP listens for connections on a TCP port. -func listenTCP(ctx context.Context, port int) error { +func listenTCP(ctx context.Context, port int, ipv6 bool) error { localAddr := net.TCPAddr{ Port: port, } // Starts listening on port. - lConn, err := net.ListenTCP("tcp", &localAddr) + lConn, err := net.ListenTCP(tcpNetwork(ipv6), &localAddr) if err != nil { return err } @@ -155,7 +155,7 @@ func listenTCP(ctx context.Context, port int) error { } // connectTCP connects to the given IP and port from an ephemeral local address. -func connectTCP(ctx context.Context, ip net.IP, port int) error { +func connectTCP(ctx context.Context, ip net.IP, port int, ipv6 bool) error { contAddr := net.TCPAddr{ IP: ip, Port: port, @@ -164,7 +164,7 @@ func connectTCP(ctx context.Context, ip net.IP, port int) error { // upon error. callback := func() error { var d net.Dialer - conn, err := d.DialContext(ctx, "tcp", contAddr.String()) + conn, err := d.DialContext(ctx, tcpNetwork(ipv6), contAddr.String()) if conn != nil { conn.Close() } @@ -280,3 +280,19 @@ func nowhereIP(ipv6 bool) string { } return "192.0.2.1" } + +// udpNetwork returns an IPv6 or IPv6 UDP network argument to net.Dial. +func udpNetwork(ipv6 bool) string { + if ipv6 { + return "udp6" + } + return "udp4" +} + +// tcpNetwork returns an IPv6 or IPv6 TCP network argument to net.Dial. +func tcpNetwork(ipv6 bool) string { + if ipv6 { + return "tcp6" + } + return "tcp4" +} |