162 lines
4.4 KiB
Go
162 lines
4.4 KiB
Go
package integration
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"hash-of-wisdom/internal/protocol"
|
|
"hash-of-wisdom/internal/server"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTCPServer_TimeoutProtection_SlowReader(t *testing.T) {
|
|
// Setup server with very short read timeout for testing
|
|
serverConfig := &server.Config{
|
|
Address: ":0",
|
|
Timeouts: server.TimeoutConfig{
|
|
Read: 500 * time.Millisecond,
|
|
Write: 5 * time.Second,
|
|
Connection: 15 * time.Second,
|
|
},
|
|
}
|
|
srv := setupTestServerWithConfig(t, serverConfig)
|
|
defer srv.Stop()
|
|
|
|
// Connect to server
|
|
conn, err := net.Dial("tcp", srv.Address())
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
// Send partial message header (just type byte)
|
|
_, err = conn.Write([]byte{0x01}) // Challenge request type
|
|
require.NoError(t, err)
|
|
|
|
// Wait longer than read timeout before sending length
|
|
time.Sleep(700 * time.Millisecond)
|
|
|
|
// Try to send more data - connection should be timed out
|
|
_, err = conn.Write([]byte{0x00, 0x00, 0x00, 0x00}) // Payload length
|
|
|
|
// Verify connection is closed by reading
|
|
buffer := make([]byte, 1024)
|
|
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
|
|
_, err = conn.Read(buffer)
|
|
assert.Error(t, err, "Connection should be closed due to slow reading")
|
|
}
|
|
|
|
func TestTCPServer_TimeoutProtection_ConnectionTimeout(t *testing.T) {
|
|
// Setup server with very short connection timeout
|
|
serverConfig := &server.Config{
|
|
Address: ":0",
|
|
Timeouts: server.TimeoutConfig{
|
|
Read: 5 * time.Second,
|
|
Write: 5 * time.Second,
|
|
Connection: 1 * time.Second,
|
|
},
|
|
}
|
|
srv := setupTestServerWithConfig(t, serverConfig)
|
|
defer srv.Stop()
|
|
|
|
// Connect to server
|
|
conn, err := net.Dial("tcp", srv.Address())
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
// Wait longer than connection timeout
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
// Try to read from connection - should get EOF or connection reset
|
|
buffer := make([]byte, 1024)
|
|
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
_, err = conn.Read(buffer)
|
|
assert.Error(t, err, "Connection should be closed due to timeout")
|
|
}
|
|
|
|
func TestTCPServer_NormalOperation_WithinTimeouts(t *testing.T) {
|
|
srv := setupTestServer(t)
|
|
defer srv.Stop()
|
|
|
|
// Connect and complete normal flow quickly
|
|
conn, err := net.Dial("tcp", srv.Address())
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
// Request challenge using new protocol API
|
|
challengeReq := &protocol.ChallengeRequest{}
|
|
err = challengeReq.Encode(conn)
|
|
require.NoError(t, err)
|
|
|
|
// Should receive challenge response without timeout
|
|
decoder := protocol.NewMessageDecoder()
|
|
msg, err := decoder.Decode(conn)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, protocol.ChallengeResponseType, msg.Type)
|
|
assert.Greater(t, msg.PayloadLength, uint32(0), "Challenge payload should not be empty")
|
|
}
|
|
|
|
func TestTCPServer_MultipleConnections_IndependentTimeouts(t *testing.T) {
|
|
serverConfig := &server.Config{
|
|
Address: ":0",
|
|
Timeouts: server.TimeoutConfig{
|
|
Read: 1 * time.Second,
|
|
Write: 5 * time.Second,
|
|
Connection: 3 * time.Second,
|
|
},
|
|
}
|
|
srv := setupTestServerWithConfig(t, serverConfig)
|
|
defer srv.Stop()
|
|
|
|
// Start two connections
|
|
conn1, err := net.Dial("tcp", srv.Address())
|
|
require.NoError(t, err)
|
|
defer conn1.Close()
|
|
|
|
conn2, err := net.Dial("tcp", srv.Address())
|
|
require.NoError(t, err)
|
|
defer conn2.Close()
|
|
|
|
// Conn1: Send complete request quickly
|
|
go func() {
|
|
req := &protocol.ChallengeRequest{}
|
|
req.Encode(conn1)
|
|
}()
|
|
|
|
// Conn2: Send partial request and stall
|
|
conn2.Write([]byte{0x01}) // Just message type
|
|
|
|
// Wait for read timeout
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
// Conn1 should still work, Conn2 should be closed
|
|
buffer := make([]byte, 1024)
|
|
|
|
// Conn1 should receive response
|
|
conn1.SetReadDeadline(time.Now().Add(1 * time.Second))
|
|
n, err := conn1.Read(buffer)
|
|
assert.NoError(t, err)
|
|
assert.Greater(t, n, 0, "Conn1 should receive response")
|
|
|
|
// Conn2 should be closed
|
|
conn2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
_, err = conn2.Read(buffer)
|
|
assert.Error(t, err, "Conn2 should be closed due to timeout")
|
|
}
|
|
|
|
// Helper function to create test server with default config
|
|
func setupTestServer(t *testing.T) *server.TCPServer {
|
|
serverConfig := &server.Config{
|
|
Address: ":0",
|
|
Timeouts: server.TimeoutConfig{
|
|
Read: 5 * time.Second,
|
|
Write: 5 * time.Second,
|
|
Connection: 15 * time.Second,
|
|
},
|
|
}
|
|
return setupTestServerWithConfig(t, serverConfig)
|
|
}
|
|
|
|
// Helper function to create test server with custom config
|