From b4f8a925283eaca32820e7977706768343bf04b8 Mon Sep 17 00:00:00 2001 From: Ghanan Gowripalan Date: Sun, 31 Jan 2021 12:25:21 -0800 Subject: Remove NICs before closing their link endpoints ...in IPv6 ICMP tests. A channel link endpoint's channel is closed when the link endpoint is closed. When the stack tries to send packets through a NIC with a closed channel endpoint, a panic will occur when attempting to write to a closed channel (https://golang.org/ref/spec#Close). To make sure the stack does not try to send packets through a NIC, we remove it. PiperOrigin-RevId: 354822085 --- pkg/tcpip/network/ipv6/icmp_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'pkg/tcpip/network') diff --git a/pkg/tcpip/network/ipv6/icmp_test.go b/pkg/tcpip/network/ipv6/icmp_test.go index 4374d0198..60404c78b 100644 --- a/pkg/tcpip/network/ipv6/icmp_test.go +++ b/pkg/tcpip/network/ipv6/icmp_test.go @@ -492,6 +492,8 @@ func visitStats(v reflect.Value, f func(string, *tcpip.StatCounter)) { } type testContext struct { + t *testing.T + s0 *stack.Stack s1 *stack.Stack @@ -509,6 +511,8 @@ func (e endpointWithResolutionCapability) Capabilities() stack.LinkEndpointCapab func newTestContext(t *testing.T) *testContext { c := &testContext{ + t: t, + s0: stack.New(stack.Options{ NetworkProtocols: []stack.NetworkProtocolFactory{NewProtocol}, TransportProtocols: []stack.TransportProtocolFactory{icmp.NewProtocol6}, @@ -562,10 +566,19 @@ func newTestContext(t *testing.T) *testContext { }}, ) + t.Cleanup(c.cleanup) + return c } func (c *testContext) cleanup() { + if err := c.s0.RemoveNIC(nicID); err != nil { + c.t.Errorf("c.s0.RemoveNIC(%d): %s", nicID, err) + } + if err := c.s1.RemoveNIC(nicID); err != nil { + c.t.Errorf("c.s1.RemoveNIC(%d): %s", nicID, err) + } + c.linkEP0.Close() c.linkEP1.Close() } @@ -617,7 +630,6 @@ func routeICMPv6Packet(t *testing.T, args routeArgs, fn func(*testing.T, header. func TestLinkResolution(t *testing.T) { c := newTestContext(t) - defer c.cleanup() r, err := c.s0.FindRoute(nicID, lladdr0, lladdr1, ProtocolNumber, false /* multicastLoop */) if err != nil { -- cgit v1.2.3