Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address race condition in socks5 test #278

Merged
merged 3 commits into from
Sep 11, 2024
Merged

Conversation

amircybersec
Copy link
Contributor

This PR attempts to resolve issue #276

@amircybersec amircybersec requested a review from fortuna September 9, 2024 16:00
@@ -177,7 +177,6 @@ func TestConnectWithoutAuth(t *testing.T) {

go func() {
err := server.Serve(listener)
t.Log("server is listening...")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the race condition? This doesn't seem like it makes any difference.

@@ -177,7 +177,6 @@ func TestConnectWithoutAuth(t *testing.T) {

go func() {
err := server.Serve(listener)
t.Log("server is listening...")
if !errors.Is(err, net.ErrClosed) && err != nil {
require.NoError(t, err) // Assert no error if it's not the expected close error
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goroutine must signal when it's done, so that the parent can wait for it. You can use sync.WaitGroup or a channel. Same for TestConnectWithAuth.

@amircybersec
Copy link
Contributor Author

@fortuna I added sync.WaitGroup to wait for go routines to complete.

// Create a SOCKS5 server.
server := socks5.NewServer()

// Create SOCKS5 proxy on localhost with a random port.
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer listener.Close()
//defer listener.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//defer listener.Close()

@@ -167,17 +167,19 @@ func testExchange(tb testing.TB, listener *net.TCPListener, destAddr string, req
}

func TestConnectWithoutAuth(t *testing.T) {
var running sync.WaitGroup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to just before the go func(), so it's clearer you are adding that goroutine

@@ -192,9 +194,15 @@ func TestConnectWithoutAuth(t *testing.T) {

_, err = client.DialStream(context.Background(), address)
require.NoError(t, err)
// Forcefully close the listener to stop the server
// Otherwise defer listener.Close() will never be called
listener.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this won't close if the test fails earlier. Instead, defer a function that does both the close and wait.

}

func TestConnectWithAuth(t *testing.T) {
var running sync.WaitGroup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments

@amircybersec
Copy link
Contributor Author

@fortuna thanks for the feedback. I made the changes accordingly.

@fortuna fortuna merged commit 2c970d3 into main Sep 11, 2024
6 checks passed
@fortuna fortuna deleted the amir-socks5-test-fix branch September 11, 2024 22:27
@fortuna
Copy link
Contributor

fortuna commented Sep 11, 2024

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants