const std = @import("std"); const testing = std.testing; const core = @import("core"); const Socket = core.socket.Socket; const SocketConfig = core.socket.SocketConfig; const SocketProtocol = core.socket.SocketProtocol; const SocketState = core.socket.SocketState; const SocketOption = core.socket.SocketOption; const Address = core.address.Address; const Error = core.errors.Error; test "socket: create TCP socket succeeds" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try testing.expectEqual(SocketState.created, socket.state); try testing.expect(socket.fd == -1); } test "socket: create UDP socket succeeds" { const allocator = testing.allocator; var socket = try Socket.init(.udp, allocator); defer socket.deinit(); try testing.expectEqual(SocketState.created, socket.state); try testing.expectEqual(SocketProtocol.udp, socket.config.protocol); } test "socket: open TCP socket changes state" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); try testing.expectEqual(SocketState.opened, socket.state); try testing.expect(socket.fd >= 0); } test "socket: bind with valid address succeeds" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const addr = try Address.parse("127.0.0.1:0"); try socket.bind(addr); try testing.expectEqual(SocketState.bound, socket.state); try testing.expect(socket.local_address != null); } test "socket: bind without open auto-opens socket" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try testing.expectEqual(SocketState.created, socket.state); const addr = try Address.parse("127.0.0.1:0"); try socket.bind(addr); try testing.expectEqual(SocketState.bound, socket.state); try testing.expect(socket.fd >= 0); } test "socket: listen on bound socket succeeds" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const addr = try Address.parse("127.0.0.1:0"); try socket.bind(addr); try socket.listen(10); try testing.expectEqual(SocketState.listening, socket.state); } test "socket: listen on UDP socket fails" { const allocator = testing.allocator; var socket = try Socket.init(.udp, allocator); defer socket.deinit(); const result = socket.listen(10); try testing.expectError(Error.OperationNotSupported, result); } test "socket: send on closed socket returns error" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try socket.close(); const result = socket.send("test"); try testing.expectError(Error.InvalidState, result); } test "socket: receive on closed socket returns error" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try socket.close(); var buffer: [100]u8 = undefined; const result = socket.receive(&buffer); try testing.expectError(Error.InvalidState, result); } test "socket: state transitions created -> opened -> bound -> listening" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try testing.expectEqual(SocketState.created, socket.getState()); try socket.open(); try testing.expectEqual(SocketState.opened, socket.getState()); const addr = try Address.parse("127.0.0.1:0"); try socket.bind(addr); try testing.expectEqual(SocketState.bound, socket.getState()); try socket.listen(10); try testing.expectEqual(SocketState.listening, socket.getState()); } test "socket: invalid state transition fails" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); // Try to listen before binding const result = socket.listen(10); try testing.expectError(Error.InvalidState, result); } test "socket: set and get socket option" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); try socket.setOption(.reuse_address, 1); const value = try socket.getOption(.reuse_address); try testing.expect(value != 0); } test "socket: set option on closed socket fails" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try socket.close(); const result = socket.setOption(.reuse_address, 1); try testing.expectError(Error.InvalidState, result); } test "socket: socket statistics track send" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const stats = socket.getStatsMut(); stats.recordSend(100); try testing.expectEqual(@as(u64, 100), socket.getStats().bytes_sent); try testing.expectEqual(@as(u64, 1), socket.getStats().packets_sent); } test "socket: socket statistics track receive" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const stats = socket.getStatsMut(); stats.recordReceive(200); try testing.expectEqual(@as(u64, 200), socket.getStats().bytes_received); try testing.expectEqual(@as(u64, 1), socket.getStats().packets_received); } test "socket: socket statistics track errors" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const stats = socket.getStatsMut(); stats.recordError(); try testing.expectEqual(@as(u64, 1), socket.getStats().errors); } test "socket: close socket changes state" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try testing.expectEqual(SocketState.opened, socket.state); try socket.close(); try testing.expectEqual(SocketState.closed, socket.state); } test "socket: double close is safe" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try socket.close(); try socket.close(); // Should not error try testing.expectEqual(SocketState.closed, socket.state); } test "socket: isValid returns false for created socket" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try testing.expect(!socket.isValid()); } test "socket: isValid returns true for opened socket" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); try testing.expect(socket.isValid()); } test "socket: isValid returns false for closed socket" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); try socket.open(); try socket.close(); try testing.expect(!socket.isValid()); } test "socket: connect updates remote address" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); // Try to connect (will fail but that's OK, we're testing address storage) const addr = try Address.parse("127.0.0.1:9999"); _ = socket.connect(addr) catch {}; // Even if connection fails, remote address should be set if (socket.remote_address) |remote| { try testing.expectEqual(addr.port, remote.port); } } test "socket: setNonBlocking on created socket fails" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); const result = socket.setNonBlocking(true); try testing.expectError(Error.InvalidState, result); } test "socket: setNonBlocking on opened socket succeeds" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); try socket.setNonBlocking(true); try testing.expect(socket.config.non_blocking); } test "socket: canRead on new socket" { const allocator = testing.allocator; var socket = try Socket.init(.udp, allocator); defer socket.deinit(); try socket.open(); const addr = try Address.parse("127.0.0.1:0"); try socket.bind(addr); const can_read = try socket.canRead(); // New socket should not have data available try testing.expect(!can_read); } test "socket: canWrite on new socket" { const allocator = testing.allocator; var socket = try Socket.init(.tcp, allocator); defer socket.deinit(); try socket.open(); const can_write = try socket.canWrite(); // New socket should be writable try testing.expect(can_write); } test "socket: UDP sendTo requires UDP protocol" { const allocator = testing.allocator; var tcp_socket = try Socket.init(.tcp, allocator); defer tcp_socket.deinit(); try tcp_socket.open(); const addr = try Address.parse("127.0.0.1:9999"); const result = tcp_socket.sendTo("test", addr); try testing.expectError(Error.OperationNotSupported, result); } test "socket: UDP receiveFrom requires UDP protocol" { const allocator = testing.allocator; var tcp_socket = try Socket.init(.tcp, allocator); defer tcp_socket.deinit(); try tcp_socket.open(); var buffer: [100]u8 = undefined; const result = tcp_socket.receiveFrom(&buffer); try testing.expectError(Error.OperationNotSupported, result); } test "socket: config with custom buffer size" { const allocator = testing.allocator; const config = SocketConfig{ .protocol = .tcp, .buffer_size = 128 * 1024, }; var socket = try Socket.create(config, allocator); defer socket.deinit(); try testing.expectEqual(@as(usize, 128 * 1024), socket.config.buffer_size); } test "socket: config with custom timeout" { const allocator = testing.allocator; const config = SocketConfig{ .protocol = .tcp, .timeout_ms = 60000, }; var socket = try Socket.create(config, allocator); defer socket.deinit(); try testing.expectEqual(@as(u32, 60000), socket.config.timeout_ms); } test "socket: multiple sockets do not interfere" { const allocator = testing.allocator; var socket1 = try Socket.init(.tcp, allocator); defer socket1.deinit(); var socket2 = try Socket.init(.udp, allocator); defer socket2.deinit(); try socket1.open(); try socket2.open(); try testing.expect(socket1.fd != socket2.fd); try testing.expectEqual(SocketProtocol.tcp, socket1.config.protocol); try testing.expectEqual(SocketProtocol.udp, socket2.config.protocol); }