ab ovo
This commit is contained in:
601
tests/integration/test_turn_coverage.zig
Normal file
601
tests/integration/test_turn_coverage.zig
Normal file
@@ -0,0 +1,601 @@
|
||||
//! TURN Integration Coverage Tests
|
||||
//!
|
||||
//! Targeted tests for network/turn_integration.zig to improve coverage.
|
||||
//! Tests ManagedConnection, TurnIntegration, and connection fallback logic.
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const mem = std.mem;
|
||||
|
||||
const network = @import("network");
|
||||
const TurnIntegration = network.turn_integration.TurnIntegration;
|
||||
const ManagedConnection = network.turn_integration.ManagedConnection;
|
||||
const ConnectionType = network.turn_integration.ConnectionType;
|
||||
const TurnConfig = network.turn_integration.TurnConfig;
|
||||
const TurnServer = network.nat.TurnServer;
|
||||
const TurnClient = network.nat.TurnClient;
|
||||
|
||||
const core = @import("core");
|
||||
const Address = core.Address;
|
||||
const Socket = core.Socket;
|
||||
const Error = core.errors.Error;
|
||||
|
||||
// ============================================================================
|
||||
// ConnectionType Tests
|
||||
// ============================================================================
|
||||
|
||||
test "ConnectionType: enum values" {
|
||||
const types = [_]ConnectionType{
|
||||
.direct,
|
||||
.hole_punched,
|
||||
.turn_relayed,
|
||||
};
|
||||
|
||||
for (types) |t| {
|
||||
try testing.expect(@intFromEnum(t) < 10);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ManagedConnection Tests
|
||||
// ============================================================================
|
||||
|
||||
test "ManagedConnection: init creates direct connection" {
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
defer testing.allocator.destroy(sock);
|
||||
sock.* = Socket.init(.tcp, testing.allocator) catch return error.SkipZigTest;
|
||||
defer sock.deinit();
|
||||
|
||||
const conn = ManagedConnection.init(testing.allocator, sock);
|
||||
try testing.expectEqual(ConnectionType.direct, conn.conn_type);
|
||||
try testing.expect(conn.socket != null);
|
||||
try testing.expect(conn.turn_client == null);
|
||||
try testing.expect(conn.peer_relay_addr == null);
|
||||
}
|
||||
|
||||
test "ManagedConnection: initTurnRelayed creates relayed connection" {
|
||||
// Create a mock TurnClient
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
defer testing.allocator.destroy(sock);
|
||||
sock.* = Socket.init(.udp, testing.allocator) catch return error.SkipZigTest;
|
||||
defer sock.deinit();
|
||||
|
||||
const turn_server = TurnServer{
|
||||
.host = "turn.example.com",
|
||||
.port = 3478,
|
||||
.username = "user",
|
||||
.credential = "pass",
|
||||
};
|
||||
|
||||
var turn_client = TurnClient.init(testing.allocator, sock, turn_server);
|
||||
defer turn_client.deinit();
|
||||
|
||||
const peer_addr = Address.ipv4(.{ 192, 168, 1, 100 }, 12345);
|
||||
|
||||
const conn = ManagedConnection.initTurnRelayed(testing.allocator, &turn_client, peer_addr);
|
||||
try testing.expectEqual(ConnectionType.turn_relayed, conn.conn_type);
|
||||
try testing.expect(conn.socket == null);
|
||||
try testing.expect(conn.turn_client != null);
|
||||
try testing.expect(conn.peer_relay_addr != null);
|
||||
}
|
||||
|
||||
test "ManagedConnection: send returns error for null socket in direct mode" {
|
||||
// Create connection with null socket to test error path
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .direct,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
const result = conn.send("test data");
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: send returns error for null socket in hole_punched mode" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .hole_punched,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
const result = conn.send("test data");
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: send returns error for null turn_client in relayed mode" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .turn_relayed,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
const result = conn.send("test data");
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: send returns error for null peer_relay_addr in relayed mode" {
|
||||
// Create a mock TurnClient
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
defer testing.allocator.destroy(sock);
|
||||
sock.* = Socket.init(.udp, testing.allocator) catch return error.SkipZigTest;
|
||||
defer sock.deinit();
|
||||
|
||||
const turn_server = TurnServer{
|
||||
.host = "turn.example.com",
|
||||
.port = 3478,
|
||||
.username = "user",
|
||||
.credential = "pass",
|
||||
};
|
||||
|
||||
var turn_client = TurnClient.init(testing.allocator, sock, turn_server);
|
||||
defer turn_client.deinit();
|
||||
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .turn_relayed,
|
||||
.socket = null,
|
||||
.turn_client = &turn_client,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
const result = conn.send("test data");
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: receive returns error for null socket in direct mode" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .direct,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
var buffer: [64]u8 = undefined;
|
||||
const result = conn.receive(&buffer);
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: receive returns error for null turn_client in relayed mode" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .turn_relayed,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
var buffer: [64]u8 = undefined;
|
||||
const result = conn.receive(&buffer);
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: deinit with null socket" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .direct,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
// Should not crash
|
||||
conn.deinit();
|
||||
}
|
||||
|
||||
test "ManagedConnection: deinit with socket" {
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
sock.* = Socket.init(.tcp, testing.allocator) catch {
|
||||
testing.allocator.destroy(sock);
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
var conn = ManagedConnection.init(testing.allocator, sock);
|
||||
conn.deinit();
|
||||
testing.allocator.destroy(sock);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// TurnIntegration Tests
|
||||
// ============================================================================
|
||||
|
||||
test "TurnIntegration: init with default config" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expect(!config.enabled);
|
||||
try testing.expect(integration.turn_client == null);
|
||||
try testing.expect(integration.turn_socket == null);
|
||||
}
|
||||
|
||||
test "TurnIntegration: init with enabled but no servers" {
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &[_]TurnServer{},
|
||||
};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Start should return without error when no servers configured
|
||||
integration.start() catch |err| {
|
||||
std.debug.print("Start error: {}\n", .{err});
|
||||
};
|
||||
}
|
||||
|
||||
test "TurnIntegration: start with disabled config" {
|
||||
const config = TurnConfig{
|
||||
.enabled = false,
|
||||
};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Should return without doing anything
|
||||
try integration.start();
|
||||
try testing.expect(integration.turn_client == null);
|
||||
}
|
||||
|
||||
test "TurnIntegration: getRelayedAddress returns null when not initialized" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expect(integration.getRelayedAddress() == null);
|
||||
}
|
||||
|
||||
test "TurnIntegration: isAvailable returns false when not initialized" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expect(!integration.isAvailable());
|
||||
}
|
||||
|
||||
test "TurnIntegration: refreshIfNeeded with no client" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Should return without error
|
||||
try integration.refreshIfNeeded();
|
||||
}
|
||||
|
||||
test "TurnIntegration: deinit with no resources" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
integration.deinit();
|
||||
// Should not crash
|
||||
}
|
||||
|
||||
test "TurnIntegration: connectWithFallback behavior" {
|
||||
const config = TurnConfig{
|
||||
.enabled = false,
|
||||
.fallback_timeout_ms = 100,
|
||||
};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Try to connect - result depends on network configuration
|
||||
// Use a non-routable address (RFC 5737 TEST-NET-3)
|
||||
const target = Address.ipv4(.{ 203, 0, 113, 1 }, 65534);
|
||||
const result = integration.connectWithFallback(testing.allocator, target);
|
||||
|
||||
// Either fails with error or succeeds (if somehow reachable)
|
||||
if (result) |conn| {
|
||||
// Clean up if connection succeeded
|
||||
var managed_conn = conn;
|
||||
managed_conn.deinit();
|
||||
// Also free the socket pointer that was allocated
|
||||
if (managed_conn.socket) |sock_ptr| {
|
||||
testing.allocator.destroy(sock_ptr);
|
||||
}
|
||||
} else |_| {
|
||||
// Connection failed as expected in most environments
|
||||
}
|
||||
}
|
||||
|
||||
test "TurnIntegration: TurnConfig defaults" {
|
||||
const config = TurnConfig{};
|
||||
|
||||
try testing.expect(!config.enabled);
|
||||
try testing.expectEqual(@as(u32, 5000), config.fallback_timeout_ms);
|
||||
try testing.expectEqual(@as(u32, 300000), config.refresh_interval_ms);
|
||||
}
|
||||
|
||||
test "TurnIntegration: allocation_failures tracking" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expectEqual(@as(u32, 0), integration.allocation_failures);
|
||||
try testing.expectEqual(@as(u32, 0), integration.refresh_failures);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// TurnConfig Validation Tests
|
||||
// ============================================================================
|
||||
|
||||
test "TurnConfig: custom values" {
|
||||
const servers = [_]TurnServer{
|
||||
.{ .host = "turn1.example.com", .port = 3478, .username = "user1", .credential = "pass1" },
|
||||
.{ .host = "turn2.example.com", .port = 3479, .username = "user2", .credential = "pass2" },
|
||||
};
|
||||
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &servers,
|
||||
.fallback_timeout_ms = 10000,
|
||||
.refresh_interval_ms = 600000,
|
||||
};
|
||||
|
||||
try testing.expect(config.enabled);
|
||||
try testing.expectEqual(@as(usize, 2), config.servers.len);
|
||||
try testing.expectEqual(@as(u32, 10000), config.fallback_timeout_ms);
|
||||
try testing.expectEqual(@as(u32, 600000), config.refresh_interval_ms);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Integration with Mock (using localhost)
|
||||
// ============================================================================
|
||||
|
||||
test "TurnIntegration: start with fake TURN server (connection fails gracefully)" {
|
||||
// Configure with a TURN server that doesn't exist
|
||||
const servers = [_]TurnServer{
|
||||
.{ .host = "127.0.0.1", .port = 65535, .username = "test", .credential = "test" },
|
||||
};
|
||||
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &servers,
|
||||
};
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Start should not crash even if TURN allocation fails
|
||||
integration.start() catch |err| {
|
||||
std.debug.print("Expected start error: {}\n", .{err});
|
||||
};
|
||||
|
||||
// TURN should not be available since allocation failed
|
||||
try testing.expect(!integration.isAvailable());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Additional TurnIntegration Coverage Tests
|
||||
// ============================================================================
|
||||
|
||||
test "TurnIntegration: getRelayedAddress returns null when not started" {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Without starting, should return null
|
||||
const addr = integration.getRelayedAddress();
|
||||
try testing.expect(addr == null);
|
||||
}
|
||||
|
||||
test "TurnIntegration: isAvailable returns false when disabled" {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expect(!integration.isAvailable());
|
||||
}
|
||||
|
||||
test "TurnIntegration: refreshIfNeeded when not available" {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Should not crash when not available
|
||||
integration.refreshIfNeeded() catch {};
|
||||
}
|
||||
|
||||
test "TurnIntegration: connectWithFallback when disabled" {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
const target = Address.ipv4(.{ 127, 0, 0, 1 }, 12345);
|
||||
|
||||
// Should fail since TURN is disabled and fallback won't work
|
||||
const result = integration.connectWithFallback(testing.allocator, target);
|
||||
if (result) |conn| {
|
||||
var managed_conn = conn;
|
||||
managed_conn.deinit();
|
||||
if (managed_conn.socket) |sock_ptr| {
|
||||
testing.allocator.destroy(sock_ptr);
|
||||
}
|
||||
} else |_| {
|
||||
// Expected - no connection available
|
||||
}
|
||||
}
|
||||
|
||||
test "TurnIntegration: init with enabled and no servers" {
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &.{}, // Empty server list
|
||||
};
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Should not crash
|
||||
try testing.expect(!integration.isAvailable());
|
||||
}
|
||||
|
||||
test "TurnIntegration: start when disabled" {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
// Start when disabled should be a no-op
|
||||
integration.start() catch {};
|
||||
|
||||
try testing.expect(!integration.isAvailable());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ManagedConnection Additional Tests
|
||||
// ============================================================================
|
||||
|
||||
test "ManagedConnection: receive with null socket returns InvalidState" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .direct,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const result = conn.receive(&buffer);
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: receive in hole_punched mode returns InvalidState" {
|
||||
var conn = ManagedConnection{
|
||||
.conn_type = .hole_punched,
|
||||
.socket = null,
|
||||
.turn_client = null,
|
||||
.peer_relay_addr = null,
|
||||
.allocator = testing.allocator,
|
||||
};
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const result = conn.receive(&buffer);
|
||||
try testing.expectError(Error.InvalidState, result);
|
||||
}
|
||||
|
||||
test "ManagedConnection: deinit with all null fields" {
|
||||
// Create with socket that we own to test deinit path
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
sock.* = Socket.init(.tcp, testing.allocator) catch {
|
||||
testing.allocator.destroy(sock);
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
var conn = ManagedConnection.init(testing.allocator, sock);
|
||||
// Should not crash
|
||||
conn.deinit();
|
||||
// Free the socket pointer that was allocated
|
||||
testing.allocator.destroy(sock);
|
||||
}
|
||||
|
||||
test "TurnConfig: default initialization" {
|
||||
const config = TurnConfig{};
|
||||
|
||||
try testing.expect(!config.enabled);
|
||||
try testing.expect(config.servers.len == 0);
|
||||
}
|
||||
|
||||
test "TurnConfig: with servers" {
|
||||
const servers = [_]TurnServer{
|
||||
.{ .host = "turn1.example.com", .port = 3478, .username = "user1", .credential = "pass1" },
|
||||
.{ .host = "turn2.example.com", .port = 3479, .username = "user2", .credential = "pass2" },
|
||||
};
|
||||
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &servers,
|
||||
};
|
||||
|
||||
try testing.expect(config.enabled);
|
||||
try testing.expectEqual(@as(usize, 2), config.servers.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Additional TurnIntegration Coverage
|
||||
// ============================================================================
|
||||
|
||||
test "TurnIntegration: multiple init/deinit cycles" {
|
||||
var i: usize = 0;
|
||||
while (i < 3) : (i += 1) {
|
||||
const config = TurnConfig{ .enabled = false };
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
integration.deinit();
|
||||
}
|
||||
}
|
||||
|
||||
test "TurnIntegration: config field accessors" {
|
||||
const servers = [_]TurnServer{
|
||||
.{ .host = "test.example.com", .port = 3478, .username = "u", .credential = "c" },
|
||||
};
|
||||
|
||||
const config = TurnConfig{
|
||||
.enabled = true,
|
||||
.servers = &servers,
|
||||
.fallback_timeout_ms = 3000,
|
||||
.refresh_interval_ms = 120000,
|
||||
};
|
||||
|
||||
try testing.expect(config.enabled);
|
||||
try testing.expectEqual(@as(usize, 1), config.servers.len);
|
||||
try testing.expectEqual(@as(u32, 3000), config.fallback_timeout_ms);
|
||||
try testing.expectEqual(@as(u32, 120000), config.refresh_interval_ms);
|
||||
}
|
||||
|
||||
test "ManagedConnection: init creates correct type" {
|
||||
const sock = testing.allocator.create(Socket) catch return error.SkipZigTest;
|
||||
defer testing.allocator.destroy(sock);
|
||||
sock.* = Socket.init(.tcp, testing.allocator) catch return error.SkipZigTest;
|
||||
|
||||
var conn = ManagedConnection.init(testing.allocator, sock);
|
||||
defer conn.deinit();
|
||||
|
||||
try testing.expectEqual(ConnectionType.direct, conn.conn_type);
|
||||
}
|
||||
|
||||
test "ConnectionType: all enum variants" {
|
||||
const types = [_]ConnectionType{
|
||||
.direct,
|
||||
.hole_punched,
|
||||
.turn_relayed,
|
||||
};
|
||||
|
||||
for (types) |t| {
|
||||
try testing.expect(@intFromEnum(t) < 10);
|
||||
}
|
||||
}
|
||||
|
||||
test "TurnServer: field initialization" {
|
||||
const server = TurnServer{
|
||||
.host = "stun.l.google.com",
|
||||
.port = 19302,
|
||||
.username = "user",
|
||||
.credential = "pass",
|
||||
};
|
||||
|
||||
try testing.expectEqualStrings("stun.l.google.com", server.host);
|
||||
try testing.expectEqual(@as(u16, 19302), server.port);
|
||||
try testing.expect(server.username != null);
|
||||
try testing.expect(server.credential != null);
|
||||
}
|
||||
|
||||
test "TurnIntegration: allocation_failures starts at zero" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expectEqual(@as(u32, 0), integration.allocation_failures);
|
||||
}
|
||||
|
||||
test "TurnIntegration: refresh_failures starts at zero" {
|
||||
const config = TurnConfig{};
|
||||
var integration = TurnIntegration.init(testing.allocator, config);
|
||||
defer integration.deinit();
|
||||
|
||||
try testing.expectEqual(@as(u32, 0), integration.refresh_failures);
|
||||
}
|
||||
Reference in New Issue
Block a user