From edb40fd1793abd54c5273d2d803d4a1212041e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Momar=20TOUR=C3=89?= Date: Wed, 18 Dec 2024 14:06:41 +0100 Subject: [PATCH 1/4] test connect --- pkg/security/tests/connect_test.go | 39 ++++++------------------------ 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/pkg/security/tests/connect_test.go b/pkg/security/tests/connect_test.go index 1e64fe57e1989..2e7dda5893afa 100644 --- a/pkg/security/tests/connect_test.go +++ b/pkg/security/tests/connect_test.go @@ -12,7 +12,6 @@ import ( "context" "fmt" "net" - "sync" "testing" "golang.org/x/net/nettest" @@ -50,14 +49,10 @@ func TestConnectEvent(t *testing.T) { } t.Run("connect-af-inet-any-tcp-success", func(t *testing.T) { - var wg sync.WaitGroup - wg.Add(1) - defer wg.Wait() done := make(chan struct{}) defer close(done) go func() { - defer wg.Done() err := bindAndAcceptConnection("tcp", ":4242", done) if err != nil { t.Error(err) @@ -78,14 +73,10 @@ func TestConnectEvent(t *testing.T) { }) t.Run("connect-af-inet-any-udp-success", func(t *testing.T) { - var wg sync.WaitGroup - wg.Add(1) - defer wg.Wait() done := make(chan struct{}) defer close(done) go func() { - defer wg.Done() err := bindAndAcceptConnection("udp", ":4242", done) if err != nil { t.Error(err) @@ -110,14 +101,9 @@ func TestConnectEvent(t *testing.T) { t.Skip("IPv6 is not supported") } - var wg sync.WaitGroup - wg.Add(1) - defer wg.Wait() - done := make(chan struct{}) defer close(done) go func() { - defer wg.Done() err := bindAndAcceptConnection("tcp", ":4242", done) if err != nil { t.Error(err) @@ -142,14 +128,9 @@ func TestConnectEvent(t *testing.T) { t.Skip("IPv6 is not supported") } - var wg sync.WaitGroup - wg.Add(1) - defer wg.Wait() - done := make(chan struct{}) defer close(done) go func() { - defer wg.Done() err := bindAndAcceptConnection("udp", ":4242", done) if err != nil { t.Error(err) @@ -180,18 +161,14 @@ func bindAndAcceptConnection(proto, address string, done chan struct{}) error { } defer listener.Close() - // Start a goroutine to accept connections continuously - go func() { - for { - c, err := listener.Accept() - if err != nil { - fmt.Printf("accept error: %v\n", err) - return - } - fmt.Println("Connection accepted") - defer c.Close() - } - }() + c, err := listener.Accept() + defer c.Close() + + if err != nil { + fmt.Printf("accept error: %v\n", err) + return err + } + fmt.Println("Connection accepted") case "udp", "unixgram": conn, err := net.ListenPacket(proto, address) From 7485988b6d195916b16a83f708cb981d1931827b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Momar=20TOUR=C3=89?= Date: Wed, 18 Dec 2024 16:13:32 +0100 Subject: [PATCH 2/4] fix --- pkg/security/tests/connect_test.go | 73 ++++++++++++++++-------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/pkg/security/tests/connect_test.go b/pkg/security/tests/connect_test.go index 2e7dda5893afa..ddd197b9e2df7 100644 --- a/pkg/security/tests/connect_test.go +++ b/pkg/security/tests/connect_test.go @@ -50,17 +50,17 @@ func TestConnectEvent(t *testing.T) { t.Run("connect-af-inet-any-tcp-success", func(t *testing.T) { - done := make(chan struct{}) + done := make(chan error) defer close(done) go func() { - err := bindAndAcceptConnection("tcp", ":4242", done) - if err != nil { - t.Error(err) - } + bindAndAcceptConnection("tcp", ":4242", done) }() test.WaitSignal(t, func() error { - return runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "tcp") + if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "tcp"); err != nil { + return err + } + return <-done }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET), event.Connect.AddrFamily, "wrong address family") @@ -74,17 +74,17 @@ func TestConnectEvent(t *testing.T) { t.Run("connect-af-inet-any-udp-success", func(t *testing.T) { - done := make(chan struct{}) + done := make(chan error) defer close(done) go func() { - err := bindAndAcceptConnection("udp", ":4242", done) - if err != nil { - t.Error(err) - } + bindAndAcceptConnection("udp", ":4242", done) }() test.WaitSignal(t, func() error { - return runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "udp") + if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "udp"); err != nil { + return err + } + return <-done }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET), event.Connect.AddrFamily, "wrong address family") @@ -101,17 +101,17 @@ func TestConnectEvent(t *testing.T) { t.Skip("IPv6 is not supported") } - done := make(chan struct{}) + done := make(chan error) defer close(done) go func() { - err := bindAndAcceptConnection("tcp", ":4242", done) - if err != nil { - t.Error(err) - } + bindAndAcceptConnection("tcp", ":4242", done) }() test.WaitSignal(t, func() error { - return runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "tcp") + if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "tcp"); err != nil { + return err + } + return <-done }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET6), event.Connect.AddrFamily, "wrong address family") @@ -128,17 +128,17 @@ func TestConnectEvent(t *testing.T) { t.Skip("IPv6 is not supported") } - done := make(chan struct{}) + done := make(chan error) defer close(done) go func() { - err := bindAndAcceptConnection("udp", ":4242", done) - if err != nil { - t.Error(err) - } + bindAndAcceptConnection("udp", ":4242", done) }() test.WaitSignal(t, func() error { - return runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "udp") + if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "udp"); err != nil { + return err + } + return <-done }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET6), event.Connect.AddrFamily, "wrong address family") @@ -152,36 +152,41 @@ func TestConnectEvent(t *testing.T) { } -func bindAndAcceptConnection(proto, address string, done chan struct{}) error { +func bindAndAcceptConnection(proto, address string, done chan error) { switch proto { case "tcp": listener, err := net.Listen(proto, address) if err != nil { - return fmt.Errorf("failed to bind to %s:%s: %w", proto, address, err) + done <- fmt.Errorf("failed to bind to %s:%s: %w", proto, address, err) + return + } else { + defer listener.Close() } - defer listener.Close() c, err := listener.Accept() - defer c.Close() - if err != nil { fmt.Printf("accept error: %v\n", err) - return err + done <- err + return + } else { + defer c.Close() } + fmt.Println("Connection accepted") case "udp", "unixgram": conn, err := net.ListenPacket(proto, address) if err != nil { - return fmt.Errorf("failed to bind to %s:%s: %w", proto, address, err) + done <- err + return } defer conn.Close() default: - return fmt.Errorf("unsupported protocol: %s", proto) + done <- fmt.Errorf("unsupported protocol: %s", proto) + return } // Wait for the test to complete before returning - <-done - return nil + done <- nil } From b50b5445c7e914252466881b3f5344c441a6606b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Momar=20TOUR=C3=89?= Date: Thu, 19 Dec 2024 13:19:56 +0100 Subject: [PATCH 3/4] test --- pkg/security/tests/connect_test.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/security/tests/connect_test.go b/pkg/security/tests/connect_test.go index ddd197b9e2df7..cd5709075fd92 100644 --- a/pkg/security/tests/connect_test.go +++ b/pkg/security/tests/connect_test.go @@ -49,9 +49,7 @@ func TestConnectEvent(t *testing.T) { } t.Run("connect-af-inet-any-tcp-success", func(t *testing.T) { - done := make(chan error) - defer close(done) go func() { bindAndAcceptConnection("tcp", ":4242", done) }() @@ -60,7 +58,9 @@ func TestConnectEvent(t *testing.T) { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "tcp"); err != nil { return err } - return <-done + err := <-done + close(done) + return err }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET), event.Connect.AddrFamily, "wrong address family") @@ -75,7 +75,6 @@ func TestConnectEvent(t *testing.T) { t.Run("connect-af-inet-any-udp-success", func(t *testing.T) { done := make(chan error) - defer close(done) go func() { bindAndAcceptConnection("udp", ":4242", done) }() @@ -84,7 +83,9 @@ func TestConnectEvent(t *testing.T) { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "udp"); err != nil { return err } - return <-done + err := <-done + close(done) + return err }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET), event.Connect.AddrFamily, "wrong address family") @@ -102,7 +103,6 @@ func TestConnectEvent(t *testing.T) { } done := make(chan error) - defer close(done) go func() { bindAndAcceptConnection("tcp", ":4242", done) }() @@ -111,7 +111,9 @@ func TestConnectEvent(t *testing.T) { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "tcp"); err != nil { return err } - return <-done + err := <-done + close(done) + return err }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET6), event.Connect.AddrFamily, "wrong address family") @@ -129,7 +131,6 @@ func TestConnectEvent(t *testing.T) { } done := make(chan error) - defer close(done) go func() { bindAndAcceptConnection("udp", ":4242", done) }() @@ -138,7 +139,9 @@ func TestConnectEvent(t *testing.T) { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "udp"); err != nil { return err } - return <-done + err := <-done + close(done) + return err }, func(event *model.Event, _ *rules.Rule) { assert.Equal(t, "connect", event.GetType(), "wrong event type") assert.Equal(t, uint16(unix.AF_INET6), event.Connect.AddrFamily, "wrong address family") From b5b88496f9f1c6be86276787b46158c3b698b494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Momar=20TOUR=C3=89?= Date: Thu, 19 Dec 2024 15:26:56 +0100 Subject: [PATCH 4/4] test --- pkg/security/tests/connect_test.go | 45 +++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/pkg/security/tests/connect_test.go b/pkg/security/tests/connect_test.go index cd5709075fd92..7f56dcfb8956c 100644 --- a/pkg/security/tests/connect_test.go +++ b/pkg/security/tests/connect_test.go @@ -50,9 +50,11 @@ func TestConnectEvent(t *testing.T) { t.Run("connect-af-inet-any-tcp-success", func(t *testing.T) { done := make(chan error) - go func() { - bindAndAcceptConnection("tcp", ":4242", done) - }() + started := make(chan struct{}) + go bindAndAcceptConnection("tcp", ":4242", done, started) + + // Wait until the server is listening before attempting to connect + <-started test.WaitSignal(t, func() error { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "tcp"); err != nil { @@ -73,11 +75,11 @@ func TestConnectEvent(t *testing.T) { }) t.Run("connect-af-inet-any-udp-success", func(t *testing.T) { - done := make(chan error) - go func() { - bindAndAcceptConnection("udp", ":4242", done) - }() + started := make(chan struct{}) + go bindAndAcceptConnection("udp", ":4242", done, started) + + <-started test.WaitSignal(t, func() error { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET", "any", "udp"); err != nil { @@ -103,9 +105,10 @@ func TestConnectEvent(t *testing.T) { } done := make(chan error) - go func() { - bindAndAcceptConnection("tcp", ":4242", done) - }() + started := make(chan struct{}) + go bindAndAcceptConnection("tcp", ":4242", done, started) + + <-started test.WaitSignal(t, func() error { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "tcp"); err != nil { @@ -131,9 +134,10 @@ func TestConnectEvent(t *testing.T) { } done := make(chan error) - go func() { - bindAndAcceptConnection("udp", ":4242", done) - }() + started := make(chan struct{}) + go bindAndAcceptConnection("udp", ":4242", done, started) + + <-started test.WaitSignal(t, func() error { if err := runSyscallTesterFunc(context.Background(), t, syscallTester, "connect", "AF_INET6", "any", "udp"); err != nil { @@ -152,28 +156,28 @@ func TestConnectEvent(t *testing.T) { test.validateConnectSchema(t, event) }) }) - } -func bindAndAcceptConnection(proto, address string, done chan error) { +func bindAndAcceptConnection(proto, address string, done chan error, started chan struct{}) { switch proto { case "tcp": listener, err := net.Listen(proto, address) if err != nil { done <- fmt.Errorf("failed to bind to %s:%s: %w", proto, address, err) return - } else { - defer listener.Close() } + defer listener.Close() + + // Signal that the server is ready to accept connections + close(started) c, err := listener.Accept() if err != nil { fmt.Printf("accept error: %v\n", err) done <- err return - } else { - defer c.Close() } + defer c.Close() fmt.Println("Connection accepted") @@ -185,6 +189,9 @@ func bindAndAcceptConnection(proto, address string, done chan error) { } defer conn.Close() + // For packet-based connections, we can signal readiness immediately + close(started) + default: done <- fmt.Errorf("unsupported protocol: %s", proto) return