392 lines
13 KiB
Zig
392 lines
13 KiB
Zig
//! Tests for socket management module
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
|
|
const Socket = @import("../../src/core/socket.zig").Socket;
|
|
const SocketConfig = @import("../../src/core/socket.zig").SocketConfig;
|
|
const SocketProtocol = @import("../../src/core/socket.zig").SocketProtocol;
|
|
const SocketOption = @import("../../src/core/socket.zig").SocketOption;
|
|
const SocketState = @import("../../src/core/socket.zig").SocketState;
|
|
const SocketStats = @import("../../src/core/socket.zig").SocketStats;
|
|
const SocketFactory = @import("../../src/core/socket.zig").SocketFactory;
|
|
const SocketManager = @import("../../src/core/socket.zig").SocketManager;
|
|
|
|
const Address = @import("../../src/core/address.zig").Address;
|
|
|
|
test "socket protocol types" {
|
|
try testing.expectEqual(@as(u8, 1), @intFromEnum(SocketProtocol.tcp));
|
|
try testing.expectEqual(@as(u8, 2), @intFromEnum(SocketProtocol.udp));
|
|
try testing.expectEqual(@as(u8, 3), @intFromEnum(SocketProtocol.unix_stream));
|
|
try testing.expectEqual(@as(u8, 4), @intFromEnum(SocketProtocol.unix_dgram));
|
|
}
|
|
|
|
test "socket configuration" {
|
|
const config = SocketConfig{
|
|
.protocol = .tcp,
|
|
.non_blocking = true,
|
|
.reuse_address = false,
|
|
.reuse_port = true,
|
|
.keep_alive = false,
|
|
.no_delay = false,
|
|
.buffer_size = 8192,
|
|
.timeout_ms = 5000,
|
|
.multicast_ttl = 2,
|
|
};
|
|
|
|
try testing.expect(config.protocol == .tcp);
|
|
try testing.expect(config.non_blocking == true);
|
|
try testing.expect(config.reuse_address == false);
|
|
try testing.expect(config.reuse_port == true);
|
|
try testing.expect(config.buffer_size == 8192);
|
|
try testing.expect(config.timeout_ms == 5000);
|
|
}
|
|
|
|
test "socket factory creation" {
|
|
var factory = SocketFactory.init(testing.allocator);
|
|
|
|
// Test TCP socket creation
|
|
const tcp_socket = try factory.createTcpSocket();
|
|
try testing.expect(tcp_socket.getState() == .created);
|
|
try testing.expect(tcp_socket.getStats().bytes_sent == 0);
|
|
|
|
tcp_socket.deinit();
|
|
|
|
// Test UDP socket creation
|
|
const udp_socket = try factory.createUdpSocket();
|
|
try testing.expect(udp_socket.getState() == .created);
|
|
|
|
udp_socket.deinit();
|
|
|
|
// Test Unix socket creation
|
|
const unix_stream = try factory.createUnixStreamSocket();
|
|
try testing.expect(unix_stream.getState() == .created);
|
|
|
|
unix_stream.deinit();
|
|
|
|
const unix_dgram = try factory.createUnixDgramSocket();
|
|
try testing.expect(unix_dgram.getState() == .created);
|
|
|
|
unix_dgram.deinit();
|
|
}
|
|
|
|
test "socket creation with config" {
|
|
const config = SocketConfig{
|
|
.protocol = .tcp,
|
|
.buffer_size = 4096,
|
|
.timeout_ms = 10000,
|
|
};
|
|
|
|
var factory = SocketFactory.init(testing.allocator);
|
|
const socket = try factory.createSocketWithConfig(config);
|
|
|
|
try testing.expect(socket.getState() == .created);
|
|
try testing.expect(socket.config.buffer_size == 4096);
|
|
|
|
socket.deinit();
|
|
}
|
|
|
|
test "socket state management" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Initial state
|
|
try testing.expect(socket.getState() == .created);
|
|
try testing.expect(socket.isValid() == false);
|
|
|
|
// Open socket
|
|
try socket.open();
|
|
try testing.expect(socket.getState() == .created); // Still created after open
|
|
try testing.expect(socket.isValid() == true);
|
|
|
|
// Close socket
|
|
try socket.close();
|
|
try testing.expect(socket.getState() == .closed);
|
|
try testing.expect(socket.isValid() == false);
|
|
}
|
|
|
|
test "socket binding" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Bind to localhost
|
|
const bind_addr = try Address.parse("127.0.0.1:0");
|
|
try socket.bind(bind_addr);
|
|
|
|
const local_addr = socket.getLocalAddress().?;
|
|
try testing.expect(local_addr.family == .ipv4);
|
|
|
|
// UDP socket binding should work differently
|
|
var udp_socket = try Socket.init(.udp, testing.allocator);
|
|
defer udp_socket.deinit();
|
|
|
|
try udp_socket.bind(bind_addr);
|
|
try testing.expect(udp_socket.getState() == .bound);
|
|
}
|
|
|
|
test "socket listening" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Bind first
|
|
const bind_addr = try Address.parse("127.0.0.1:0");
|
|
try socket.bind(bind_addr);
|
|
|
|
// Listen
|
|
try socket.listen(10);
|
|
try testing.expect(socket.getState() == .listening);
|
|
|
|
// UDP sockets can't listen
|
|
var udp_socket = try Socket.init(.udp, testing.allocator);
|
|
defer udp_socket.deinit();
|
|
|
|
try testing.expectError(Socket.Error.OperationNotSupported, udp_socket.listen(10));
|
|
}
|
|
|
|
test "socket options" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Test getting default options
|
|
const reuse_addr = try socket.getOption(.reuse_address);
|
|
try testing.expect(reuse_addr == 0 or reuse_addr == 1);
|
|
|
|
const keep_alive = try socket.getOption(.keep_alive);
|
|
try testing.expect(keep_alive == 0 or keep_alive == 1);
|
|
|
|
const buffer_size = try socket.getOption(.buffer_size);
|
|
try testing.expect(buffer_size > 0);
|
|
|
|
// Test setting options
|
|
try socket.setOption(.reuse_address, 1);
|
|
const new_reuse_addr = try socket.getOption(.reuse_address);
|
|
try testing.expect(new_reuse_addr == 1);
|
|
|
|
try socket.setOption(.keep_alive, 1);
|
|
const new_keep_alive = try socket.getOption(.keep_alive);
|
|
try testing.expect(new_keep_alive == 1);
|
|
|
|
// Test unsupported option
|
|
try testing.expectError(Socket.Error.OperationNotSupported, socket.getOption(.multicast_ttl));
|
|
}
|
|
|
|
test "socket statistics" {
|
|
var socket = try Socket.init(.udp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
const stats = socket.getStats();
|
|
|
|
// Test initial stats
|
|
try testing.expectEqual(@as(u64, 0), stats.bytes_sent);
|
|
try testing.expectEqual(@as(u64, 0), stats.bytes_received);
|
|
try testing.expectEqual(@as(u64, 0), stats.packets_sent);
|
|
try testing.expectEqual(@as(u64, 0), stats.packets_received);
|
|
try testing.expectEqual(@as(u64, 0), stats.errors);
|
|
|
|
// Test stat recording
|
|
stats.recordSend(100);
|
|
stats.recordReceive(200);
|
|
stats.recordError();
|
|
|
|
try testing.expectEqual(@as(u64, 100), stats.bytes_sent);
|
|
try testing.expectEqual(@as(u64, 200), stats.bytes_received);
|
|
try testing.expectEqual(@as(u64, 1), stats.packets_sent);
|
|
try testing.expectEqual(@as(u64, 1), stats.packets_received);
|
|
try testing.expectEqual(@as(u64, 1), stats.errors);
|
|
|
|
// Test timestamp update
|
|
const last_activity = stats.last_activity;
|
|
try testing.expect(last_activity > 0);
|
|
}
|
|
|
|
test "socket manager operations" {
|
|
var manager = SocketManager.init(testing.allocator);
|
|
defer manager.deinit();
|
|
|
|
// Create multiple sockets
|
|
const tcp_socket = try manager.createTcpSocket();
|
|
const udp_socket = try manager.createUdpSocket();
|
|
|
|
try testing.expectEqual(@as(usize, 2), manager.sockets.items.len);
|
|
|
|
// Test socket retrieval
|
|
try testing.expect(&manager.sockets.items[0] == tcp_socket);
|
|
try testing.expect(&manager.sockets.items[1] == udp_socket);
|
|
|
|
// Test removing socket
|
|
manager.removeSocket(tcp_socket);
|
|
try testing.expectEqual(@as(usize, 1), manager.sockets.items.len);
|
|
// Verify remaining socket is UDP by checking its protocol
|
|
try testing.expectEqual(SocketProtocol.udp, manager.sockets.items[0].config.protocol);
|
|
}
|
|
|
|
test "socket manager active sockets" {
|
|
var manager = SocketManager.init(testing.allocator);
|
|
defer manager.deinit();
|
|
|
|
// Create sockets
|
|
const socket1 = try manager.createTcpSocket();
|
|
const socket2 = try manager.createUdpSocket();
|
|
|
|
// Both should be valid initially
|
|
const active_sockets = manager.getActiveSockets();
|
|
try testing.expect(active_sockets.len >= 2);
|
|
}
|
|
|
|
test "socket protocol-specific operations" {
|
|
// Test TCP socket operations
|
|
var tcp_socket = try Socket.init(.tcp, testing.allocator);
|
|
defer tcp_socket.deinit();
|
|
|
|
try testing.expectError(Socket.Error.InvalidState, tcp_socket.listen(10));
|
|
|
|
// UDP socket operations
|
|
var udp_socket = try Socket.init(.udp, testing.allocator);
|
|
defer udp_socket.deinit();
|
|
|
|
try testing.expectError(Socket.Error.InvalidState, udp_socket.accept());
|
|
|
|
// Test unsupported operations
|
|
try testing.expectError(Socket.Error.OperationNotSupported, tcp_socket.sendTo("data", try Address.parse("127.0.0.1:0")));
|
|
try testing.expectError(Socket.Error.OperationNotSupported, tcp_socket.receiveFrom(&[_]u8{0} ** 100));
|
|
}
|
|
|
|
test "socket polling and readiness" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
try socket.open();
|
|
|
|
// Test canRead when not connected
|
|
const can_read = socket.canRead();
|
|
// This might return true or false depending on implementation
|
|
// The important thing is that it doesn't error
|
|
_ = can_read;
|
|
|
|
const can_write = socket.canWrite();
|
|
_ = can_write;
|
|
}
|
|
|
|
test "socket address management" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Test initial address state
|
|
try testing.expect(socket.getLocalAddress() == null);
|
|
try testing.expect(socket.getRemoteAddress() == null);
|
|
|
|
// Bind to get local address
|
|
const bind_addr = try Address.parse("127.0.0.1:0");
|
|
try socket.bind(bind_addr);
|
|
|
|
const local_addr = socket.getLocalAddress();
|
|
try testing.expect(local_addr != null);
|
|
|
|
// After binding, should have local address
|
|
const bound_addr = local_addr.?;
|
|
try testing.expect(bound_addr.family == .ipv4);
|
|
}
|
|
|
|
test "socket error handling" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// Test invalid state operations
|
|
try testing.expectError(Socket.Error.InvalidState, socket.accept());
|
|
try testing.expectError(Socket.Error.InvalidState, socket.connect(try Address.parse("127.0.0.1:0")));
|
|
|
|
// Test operations on closed socket
|
|
try socket.close();
|
|
try testing.expectError(Socket.Error.InvalidState, socket.send("data"));
|
|
try testing.expectError(Socket.Error.InvalidState, socket.receive(&[_]u8{0} ** 100));
|
|
}
|
|
|
|
test "socket configuration validation" {
|
|
// Test default configuration
|
|
const config = SocketConfig{ .protocol = .tcp };
|
|
try testing.expect(config.protocol == .tcp);
|
|
try testing.expect(config.non_blocking == false);
|
|
try testing.expect(config.buffer_size == 64 * 1024); // Default buffer size
|
|
try testing.expect(config.timeout_ms == 30000); // Default timeout
|
|
|
|
// Test custom configuration
|
|
const custom_config = SocketConfig{
|
|
.protocol = .udp,
|
|
.buffer_size = 8192,
|
|
.timeout_ms = 5000,
|
|
.multicast_ttl = 4,
|
|
};
|
|
try testing.expect(custom_config.protocol == .udp);
|
|
try testing.expect(custom_config.buffer_size == 8192);
|
|
try testing.expect(custom_config.timeout_ms == 5000);
|
|
try testing.expect(custom_config.multicast_ttl == 4);
|
|
}
|
|
|
|
test "socket creation errors" {
|
|
var factory = SocketFactory.init(testing.allocator);
|
|
|
|
// Test creating socket with invalid protocol (should not happen in current design)
|
|
// but test that factory methods work as expected
|
|
const tcp_socket = try factory.createTcpSocket();
|
|
try testing.expect(tcp_socket.fd >= 0 or tcp_socket.fd == -1); // Should be valid or invalid file descriptor
|
|
|
|
tcp_socket.deinit();
|
|
}
|
|
|
|
test "socket graceful cleanup" {
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
|
|
// Double close should be safe
|
|
try socket.close();
|
|
try socket.close(); // Should not error
|
|
}
|
|
|
|
test "socket manager cleanup" {
|
|
var manager = SocketManager.init(testing.allocator);
|
|
|
|
// Create some sockets
|
|
const socket1 = try manager.createTcpSocket();
|
|
const socket2 = try manager.createUdpSocket();
|
|
|
|
// Manager deinit should clean up all sockets
|
|
manager.deinit();
|
|
|
|
// After deinit, sockets should be closed
|
|
try testing.expect(socket1.getState() == .closed);
|
|
try testing.expect(socket2.getState() == .closed);
|
|
}
|
|
|
|
test "socket type consistency" {
|
|
var factory = SocketFactory.init(testing.allocator);
|
|
|
|
// Verify socket types are created correctly
|
|
const tcp_socket = try factory.createTcpSocket();
|
|
const udp_socket = try factory.createUdpSocket();
|
|
const unix_stream = try factory.createUnixStreamSocket();
|
|
const unix_dgram = try factory.createUnixDgramSocket();
|
|
|
|
// Check that protocol matches expected types
|
|
try testing.expect(tcp_socket.config.protocol == .tcp);
|
|
try testing.expect(udp_socket.config.protocol == .udp);
|
|
try testing.expect(unix_stream.config.protocol == .unix_stream);
|
|
try testing.expect(unix_dgram.config.protocol == .unix_dgram);
|
|
|
|
tcp_socket.deinit();
|
|
udp_socket.deinit();
|
|
unix_stream.deinit();
|
|
unix_dgram.deinit();
|
|
}
|
|
|
|
test "socket buffer operations" {
|
|
// This test would require actual buffer types from buffer.zig
|
|
// For now, just test that the methods exist and can be called
|
|
var socket = try Socket.init(.tcp, testing.allocator);
|
|
defer socket.deinit();
|
|
|
|
// These operations would normally require a buffer
|
|
// We're just testing the method signatures exist
|
|
_ = socket.canRead;
|
|
_ = socket.canWrite;
|
|
_ = socket.getStats;
|
|
_ = socket.getState;
|
|
} |