From 8578b0445d703a59f1ca4c70e3118edd4127a345 Mon Sep 17 00:00:00 2001 From: Denton Gentry Date: Tue, 12 Jan 2021 15:13:45 -0800 Subject: [PATCH] tstun: add test to send a packet after Close() This test serves two purposes: + check that Write() returns an error if the tstun has been closed. + ensure that the close-related code in tstun is exercised in a test case. We were getting spurious code coverage adds/drops based on timing of when the test case finished. Signed-off-by: Denton Gentry --- wgengine/tstun/tun_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/wgengine/tstun/tun_test.go b/wgengine/tstun/tun_test.go index 9eeeeeef1..d6b3eb7be 100644 --- a/wgengine/tstun/tun_test.go +++ b/wgengine/tstun/tun_test.go @@ -341,6 +341,22 @@ func TestAllocs(t *testing.T) { } } +func TestClose(t *testing.T) { + ftun, tun := newFakeTUN(t.Logf, false) + + data := udp4("1.2.3.4", "5.6.7.8", 98, 98) + _, err := ftun.Write(data, 0) + if err != nil { + t.Error(err) + } + + tun.Close() + _, err = ftun.Write(data, 0) + if err == nil { + t.Error("Expected error from ftun.Write() after Close()") + } +} + func BenchmarkWrite(b *testing.B) { ftun, tun := newFakeTUN(b.Logf, true) defer tun.Close()