Files
zigp2p/tests/network/test_turn_integration.zig
2026-02-06 21:03:59 -03:00

1177 lines
37 KiB
Zig

//! TURN Integration Module Tests with Mocking
//!
//! Comprehensive tests for turn_integration.zig using mock responses
//! to achieve higher code coverage without actual network I/O.
const std = @import("std");
const testing = std.testing;
const Allocator = std.mem.Allocator;
const network = @import("network");
const turn_integration = network.turn_integration;
const nat = network.nat;
const ConnectionType = turn_integration.ConnectionType;
const ManagedConnection = turn_integration.ManagedConnection;
const TurnIntegration = turn_integration.TurnIntegration;
const TurnConfig = turn_integration.TurnConfig;
const TurnServer = nat.TurnServer;
const TurnClient = nat.TurnClient;
const TurnAllocation = nat.TurnAllocation;
const core = @import("core");
const Address = core.Address;
const Socket = core.Socket;
const Error = core.errors.Error;
// ============================================================================
// Mock Helper - Simulates TURN allocation state
// ============================================================================
/// Create a mock transaction ID
fn mockTransactionId() [12]u8 {
return [_]u8{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c };
}
/// Create a valid (non-expired) allocation
fn createValidAllocation() TurnAllocation {
return TurnAllocation{
.relayed_address = Address.ipv4(.{ 203, 0, 113, 100 }, 49152),
.lifetime = 600,
.created_at = std.time.timestamp(), // Just created
.transaction_id = mockTransactionId(),
};
}
/// Create an expired allocation
fn createExpiredAllocation() TurnAllocation {
return TurnAllocation{
.relayed_address = Address.ipv4(.{ 203, 0, 113, 100 }, 49152),
.lifetime = 600,
.created_at = std.time.timestamp() - 1000, // Created long ago, lifetime is 600s
.transaction_id = mockTransactionId(),
};
}
/// Helper to create a TurnClient with injected allocation state
fn createMockTurnClient(allocator: Allocator, socket_ptr: *Socket, server: TurnServer, with_allocation: bool) !*TurnClient {
const client_ptr = try allocator.create(TurnClient);
client_ptr.* = TurnClient.init(allocator, socket_ptr, server);
if (with_allocation) {
client_ptr.allocation = createValidAllocation();
}
return client_ptr;
}
/// Helper to create a UDP socket for testing
fn createTestSocket(allocator: Allocator) !struct { socket: Socket, ptr: *Socket } {
const socket = try Socket.init(.udp, allocator);
const ptr = try allocator.create(Socket);
ptr.* = socket;
return .{ .socket = socket, .ptr = ptr };
}
// ============================================================================
// ConnectionType Enum Tests
// ============================================================================
test "ConnectionType - direct value exists" {
const conn_type = ConnectionType.direct;
try testing.expectEqual(ConnectionType.direct, conn_type);
}
test "ConnectionType - hole_punched value exists" {
const conn_type = ConnectionType.hole_punched;
try testing.expectEqual(ConnectionType.hole_punched, conn_type);
}
test "ConnectionType - turn_relayed value exists" {
const conn_type = ConnectionType.turn_relayed;
try testing.expectEqual(ConnectionType.turn_relayed, conn_type);
}
test "ConnectionType - all values are distinct" {
try testing.expect(ConnectionType.direct != ConnectionType.hole_punched);
try testing.expect(ConnectionType.direct != ConnectionType.turn_relayed);
try testing.expect(ConnectionType.hole_punched != ConnectionType.turn_relayed);
}
test "ConnectionType - can be used in switch" {
const types = [_]ConnectionType{ .direct, .hole_punched, .turn_relayed };
for (types) |t| {
const desc: []const u8 = switch (t) {
.direct => "direct",
.hole_punched => "hole punched",
.turn_relayed => "TURN relayed",
};
try testing.expect(desc.len > 0);
}
}
test "ConnectionType - can be compared" {
const a = ConnectionType.direct;
const b = ConnectionType.direct;
const c = ConnectionType.turn_relayed;
try testing.expect(a == b);
try testing.expect(a != c);
}
// ============================================================================
// TurnConfig Tests
// ============================================================================
test "TurnConfig - default values" {
const config = TurnConfig{};
try testing.expectEqual(false, config.enabled);
try testing.expectEqual(@as(u32, 5000), config.fallback_timeout_ms);
try testing.expectEqual(@as(u32, 300000), config.refresh_interval_ms);
}
test "TurnConfig - enabled can be set to true" {
const config = TurnConfig{
.enabled = true,
};
try testing.expectEqual(true, config.enabled);
}
test "TurnConfig - custom fallback timeout" {
const config = TurnConfig{
.fallback_timeout_ms = 10000,
};
try testing.expectEqual(@as(u32, 10000), config.fallback_timeout_ms);
}
test "TurnConfig - custom refresh interval" {
const config = TurnConfig{
.refresh_interval_ms = 600000, // 10 minutes
};
try testing.expectEqual(@as(u32, 600000), config.refresh_interval_ms);
}
test "TurnConfig - default servers uses DEFAULT_TURN_SERVERS" {
const config = TurnConfig{};
try testing.expectEqual(&nat.DEFAULT_TURN_SERVERS, config.servers);
}
test "TurnConfig - custom servers can be set" {
const custom_servers = [_]TurnServer{
TurnServer{
.host = "turn.example.com",
.port = 3478,
.username = "testuser",
.credential = "testpass",
},
};
const config = TurnConfig{
.servers = &custom_servers,
};
try testing.expectEqual(@as(usize, 1), config.servers.len);
try testing.expectEqualStrings("testuser", config.servers[0].username.?);
}
test "TurnConfig - full custom configuration" {
const config = TurnConfig{
.enabled = true,
.fallback_timeout_ms = 3000,
.refresh_interval_ms = 120000,
};
try testing.expectEqual(true, config.enabled);
try testing.expectEqual(@as(u32, 3000), config.fallback_timeout_ms);
try testing.expectEqual(@as(u32, 120000), config.refresh_interval_ms);
}
// ============================================================================
// TurnIntegration Initialization Tests
// ============================================================================
test "TurnIntegration - init with default config" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(@as(?*TurnClient, null), integration.turn_client);
try testing.expectEqual(@as(?*Socket, null), integration.turn_socket);
try testing.expectEqual(@as(i64, 0), integration.last_refresh);
try testing.expectEqual(@as(u32, 0), integration.allocation_failures);
try testing.expectEqual(@as(u32, 0), integration.refresh_failures);
}
test "TurnIntegration - init with enabled config" {
const allocator = testing.allocator;
const config = TurnConfig{
.enabled = true,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(true, integration.config.enabled);
try testing.expectEqual(@as(?*TurnClient, null), integration.turn_client);
}
test "TurnIntegration - init preserves config" {
const allocator = testing.allocator;
const config = TurnConfig{
.enabled = true,
.fallback_timeout_ms = 8000,
.refresh_interval_ms = 240000,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(config.enabled, integration.config.enabled);
try testing.expectEqual(config.fallback_timeout_ms, integration.config.fallback_timeout_ms);
try testing.expectEqual(config.refresh_interval_ms, integration.config.refresh_interval_ms);
}
test "TurnIntegration - getRelayedAddress returns null when no client" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
const addr = integration.getRelayedAddress();
try testing.expectEqual(@as(?Address, null), addr);
}
test "TurnIntegration - isAvailable returns false when no client" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(false, integration.isAvailable());
}
test "TurnIntegration - deinit is safe when no resources allocated" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.deinit();
}
test "TurnIntegration - multiple deinit calls are safe" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.deinit();
integration.deinit();
}
test "TurnIntegration - start with disabled config does nothing" {
const allocator = testing.allocator;
const config = TurnConfig{
.enabled = false,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try integration.start();
try testing.expectEqual(@as(?*TurnClient, null), integration.turn_client);
try testing.expectEqual(@as(?*Socket, null), integration.turn_socket);
}
test "TurnIntegration - start with empty servers does nothing" {
const allocator = testing.allocator;
const empty_servers: []const TurnServer = &[_]TurnServer{};
const config = TurnConfig{
.enabled = true,
.servers = empty_servers,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try integration.start();
try testing.expectEqual(@as(?*TurnClient, null), integration.turn_client);
}
test "TurnIntegration - refreshIfNeeded with no client is no-op" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try integration.refreshIfNeeded();
}
test "TurnIntegration - connectWithFallback fails when no resources" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
const target = Address.ipv4(.{ 127, 0, 0, 1 }, 12345);
const result = integration.connectWithFallback(allocator, target);
try testing.expectError(Error.ConnectionFailed, result);
}
// ============================================================================
// ManagedConnection Tests - Basic
// ============================================================================
test "ManagedConnection - init creates direct connection type" {
const allocator = testing.allocator;
const socket = try Socket.init(.tcp, allocator);
const socket_ptr = try allocator.create(Socket);
socket_ptr.* = socket;
const conn = ManagedConnection.init(allocator, socket_ptr);
try testing.expectEqual(ConnectionType.direct, conn.conn_type);
try testing.expectEqual(socket_ptr, conn.socket.?);
try testing.expectEqual(@as(?*TurnClient, null), conn.turn_client);
try testing.expectEqual(@as(?Address, null), conn.peer_relay_addr);
socket_ptr.deinit();
allocator.destroy(socket_ptr);
}
test "ManagedConnection - send returns InvalidState with null socket for direct" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
const data = "test data";
const result = conn.send(data);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - send returns InvalidState with null socket for hole_punched" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .hole_punched,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
const data = "test data";
const result = conn.send(data);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - send returns InvalidState with null turn_client for relayed" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
const data = "test data";
const result = conn.send(data);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - receive returns InvalidState with null socket for direct" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
var buffer: [1024]u8 = undefined;
const result = conn.receive(&buffer);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - receive returns InvalidState with null socket for hole_punched" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .hole_punched,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
var buffer: [1024]u8 = undefined;
const result = conn.receive(&buffer);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - receive returns InvalidState with null turn_client for relayed" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
var buffer: [1024]u8 = undefined;
const result = conn.receive(&buffer);
try testing.expectError(Error.InvalidState, result);
}
test "ManagedConnection - deinit with null socket is safe" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
conn.deinit();
}
test "ManagedConnection - deinit cleans up socket for direct connection" {
const allocator = testing.allocator;
const socket = try Socket.init(.tcp, allocator);
const socket_ptr = try allocator.create(Socket);
socket_ptr.* = socket;
var conn = ManagedConnection.init(allocator, socket_ptr);
conn.deinit();
allocator.destroy(socket_ptr);
}
// ============================================================================
// ManagedConnection with Mock TurnClient Tests
// ============================================================================
test "ManagedConnection - initTurnRelayed creates turn_relayed connection type" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try createMockTurnClient(allocator, test_socket.ptr, server, false);
defer {
client_ptr.deinit();
allocator.destroy(client_ptr);
}
const peer_addr = Address.ipv4(.{ 10, 0, 0, 5 }, 5000);
const conn = ManagedConnection.initTurnRelayed(allocator, client_ptr, peer_addr);
try testing.expectEqual(ConnectionType.turn_relayed, conn.conn_type);
try testing.expectEqual(@as(?*Socket, null), conn.socket);
try testing.expectEqual(client_ptr, conn.turn_client.?);
try testing.expect(conn.peer_relay_addr != null);
try testing.expectEqual(@as(u16, 5000), conn.peer_relay_addr.?.port);
}
test "ManagedConnection - send returns InvalidState with null peer_relay_addr for relayed" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try createMockTurnClient(allocator, test_socket.ptr, server, false);
defer {
client_ptr.deinit();
allocator.destroy(client_ptr);
}
var conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = client_ptr,
.peer_relay_addr = null,
.allocator = allocator,
};
const data = "test data";
const result = conn.send(data);
try testing.expectError(Error.InvalidState, result);
}
// ============================================================================
// TurnIntegration with Injected Mock State Tests
// ============================================================================
test "TurnIntegration - isAvailable returns true with valid allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try createMockTurnClient(allocator, test_socket.ptr, server, true);
// Create integration and inject mock client
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
// Inject the mock client
integration.turn_client = client_ptr;
integration.turn_socket = test_socket.ptr;
// deinit handles cleanup of turn_client and turn_socket
defer integration.deinit();
// Test isAvailable with injected allocation
try testing.expectEqual(true, integration.isAvailable());
}
test "TurnIntegration - getRelayedAddress returns address with valid allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try createMockTurnClient(allocator, test_socket.ptr, server, true);
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.turn_client = client_ptr;
integration.turn_socket = test_socket.ptr;
// integration.deinit() handles cleanup of turn_client and turn_socket
defer integration.deinit();
const relay_addr = integration.getRelayedAddress();
try testing.expect(relay_addr != null);
try testing.expectEqual(@as(u16, 49152), relay_addr.?.port);
}
test "TurnIntegration - isAvailable returns false with expired allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try allocator.create(TurnClient);
client_ptr.* = TurnClient.init(allocator, test_socket.ptr, server);
// Set allocation with expired time
client_ptr.allocation = createExpiredAllocation();
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.turn_client = client_ptr;
integration.turn_socket = test_socket.ptr;
// integration.deinit() handles cleanup of turn_client and turn_socket
defer integration.deinit();
// Should return false because allocation is expired
try testing.expectEqual(false, integration.isAvailable());
}
test "TurnIntegration - connectWithFallback setup and state" {
// Note: Actually calling connectWithFallback would require a bound socket
// and network I/O. This test verifies the setup and state management.
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try createMockTurnClient(allocator, test_socket.ptr, server, true);
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.turn_client = client_ptr;
integration.turn_socket = test_socket.ptr;
// integration.deinit() handles cleanup of turn_client and turn_socket
defer integration.deinit();
// Verify the setup is correct
try testing.expectEqual(true, integration.isAvailable());
const relay_addr = integration.getRelayedAddress();
try testing.expect(relay_addr != null);
// ManagedConnection.initTurnRelayed can be used with the client
const target = Address.ipv4(.{ 192, 168, 1, 100 }, 8080);
const conn = ManagedConnection.initTurnRelayed(allocator, integration.turn_client.?, target);
try testing.expectEqual(ConnectionType.turn_relayed, conn.conn_type);
try testing.expect(conn.turn_client != null);
try testing.expect(conn.peer_relay_addr != null);
}
test "TurnIntegration - expired allocation is not available" {
// Note: connectWithFallback requires network I/O. Instead, we test that
// expired allocations are correctly detected as unavailable.
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
const client_ptr = try allocator.create(TurnClient);
client_ptr.* = TurnClient.init(allocator, test_socket.ptr, server);
// Set expired allocation
client_ptr.allocation = createExpiredAllocation();
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
integration.turn_client = client_ptr;
integration.turn_socket = test_socket.ptr;
// integration.deinit() handles cleanup of turn_client and turn_socket
defer integration.deinit();
// Verify expired allocation makes TURN unavailable
try testing.expectEqual(false, integration.isAvailable());
// Client should report invalid allocation
try testing.expectEqual(false, client_ptr.hasValidAllocation());
}
// ============================================================================
// TurnAllocation Tests
// ============================================================================
test "TurnAllocation - struct can be created" {
const alloc = createValidAllocation();
try testing.expectEqual(@as(u16, 49152), alloc.relayed_address.port);
try testing.expectEqual(@as(u32, 600), alloc.lifetime);
try testing.expect(alloc.transaction_id.len == 12);
}
test "TurnAllocation - isExpired determines validity" {
const valid_alloc = createValidAllocation();
const expired_alloc = createExpiredAllocation();
// Valid allocation should not be expired
try testing.expectEqual(false, valid_alloc.isExpired());
// Expired allocation should be expired
try testing.expectEqual(true, expired_alloc.isExpired());
}
// ============================================================================
// TurnClient Tests with Mock
// ============================================================================
test "TurnClient - init creates client without allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
try testing.expectEqual(@as(?TurnAllocation, null), client.allocation);
try testing.expectEqualStrings("turn.example.com", client.server.host);
}
test "TurnClient - hasValidAllocation returns false without allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
try testing.expectEqual(false, client.hasValidAllocation());
}
test "TurnClient - hasValidAllocation returns true with valid allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
// Inject valid allocation
client.allocation = createValidAllocation();
try testing.expectEqual(true, client.hasValidAllocation());
}
test "TurnClient - hasValidAllocation returns false with expired allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
// Inject expired allocation
client.allocation = createExpiredAllocation();
try testing.expectEqual(false, client.hasValidAllocation());
}
test "TurnClient - getRelayedAddress returns null without allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
try testing.expectEqual(@as(?Address, null), client.getRelayedAddress());
}
test "TurnClient - getRelayedAddress returns address with valid allocation" {
const allocator = testing.allocator;
const test_socket = try createTestSocket(allocator);
defer {
test_socket.ptr.deinit();
allocator.destroy(test_socket.ptr);
}
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
};
var client = TurnClient.init(allocator, test_socket.ptr, server);
defer client.deinit();
client.allocation = createValidAllocation();
const addr = client.getRelayedAddress();
try testing.expect(addr != null);
try testing.expectEqual(@as(u16, 49152), addr.?.port);
}
// ============================================================================
// Integration Scenarios
// ============================================================================
test "TurnIntegration - full lifecycle without network" {
const allocator = testing.allocator;
const config = TurnConfig{
.enabled = false,
.fallback_timeout_ms = 3000,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try integration.start();
try testing.expectEqual(false, integration.isAvailable());
try testing.expectEqual(@as(?Address, null), integration.getRelayedAddress());
try integration.refreshIfNeeded();
}
test "TurnIntegration - config persists through lifecycle" {
const allocator = testing.allocator;
const config = TurnConfig{
.enabled = true,
.fallback_timeout_ms = 8000,
.refresh_interval_ms = 240000,
};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(@as(u32, 8000), integration.config.fallback_timeout_ms);
try testing.expectEqual(@as(u32, 240000), integration.config.refresh_interval_ms);
try integration.start();
try testing.expectEqual(@as(u32, 8000), integration.config.fallback_timeout_ms);
}
test "ManagedConnection - connection type can be changed" {
const allocator = testing.allocator;
var conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
conn.conn_type = .hole_punched;
try testing.expectEqual(ConnectionType.hole_punched, conn.conn_type);
}
test "TurnIntegration - allocation_failures increments correctly" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(@as(u32, 0), integration.allocation_failures);
integration.allocation_failures += 1;
try testing.expectEqual(@as(u32, 1), integration.allocation_failures);
integration.allocation_failures += 1;
try testing.expectEqual(@as(u32, 2), integration.allocation_failures);
}
test "TurnIntegration - refresh_failures increments correctly" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(@as(u32, 0), integration.refresh_failures);
integration.refresh_failures += 1;
try testing.expectEqual(@as(u32, 1), integration.refresh_failures);
}
test "TurnIntegration - last_refresh starts at zero" {
const allocator = testing.allocator;
const config = TurnConfig{};
var integration = TurnIntegration.init(allocator, config);
defer integration.deinit();
try testing.expectEqual(@as(i64, 0), integration.last_refresh);
}
// ============================================================================
// Edge Cases and Boundary Tests
// ============================================================================
test "TurnConfig - minimum timeout values" {
const config = TurnConfig{
.fallback_timeout_ms = 0,
.refresh_interval_ms = 0,
};
try testing.expectEqual(@as(u32, 0), config.fallback_timeout_ms);
try testing.expectEqual(@as(u32, 0), config.refresh_interval_ms);
}
test "TurnConfig - maximum timeout values" {
const config = TurnConfig{
.fallback_timeout_ms = std.math.maxInt(u32),
.refresh_interval_ms = std.math.maxInt(u32),
};
try testing.expectEqual(std.math.maxInt(u32), config.fallback_timeout_ms);
try testing.expectEqual(std.math.maxInt(u32), config.refresh_interval_ms);
}
test "ManagedConnection - all fields can be set via struct literal" {
const allocator = testing.allocator;
const peer_addr = Address.ipv4(.{ 192, 168, 1, 100 }, 9999);
const conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = peer_addr,
.allocator = allocator,
};
try testing.expectEqual(ConnectionType.turn_relayed, conn.conn_type);
try testing.expect(conn.peer_relay_addr != null);
try testing.expectEqual(@as(u16, 9999), conn.peer_relay_addr.?.port);
}
test "TurnIntegration - init can use testing allocator" {
const config = TurnConfig{};
var integration = TurnIntegration.init(testing.allocator, config);
integration.deinit();
}
// ============================================================================
// Address Tests
// ============================================================================
test "ManagedConnection - peer_relay_addr can be IPv4" {
const allocator = testing.allocator;
const addr = Address.ipv4(.{ 10, 0, 0, 1 }, 3478);
const conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = addr,
.allocator = allocator,
};
try testing.expect(conn.peer_relay_addr != null);
try testing.expectEqual(@as(u16, 3478), conn.peer_relay_addr.?.port);
}
test "ManagedConnection - peer_relay_addr can be IPv6" {
const allocator = testing.allocator;
const addr = Address.ipv6(.{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 3478);
const conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = addr,
.allocator = allocator,
};
try testing.expect(conn.peer_relay_addr != null);
try testing.expectEqual(@as(u16, 3478), conn.peer_relay_addr.?.port);
}
// ============================================================================
// TurnServer Configuration Tests
// ============================================================================
test "TurnServer - can create custom server config" {
const server = TurnServer{
.host = "turn.example.com",
.port = 3478,
.username = "myuser",
.credential = "mypassword",
};
try testing.expectEqualStrings("turn.example.com", server.host);
try testing.expectEqual(@as(u16, 3478), server.port);
try testing.expectEqualStrings("myuser", server.username.?);
try testing.expectEqualStrings("mypassword", server.credential.?);
}
test "TurnServer - default port is 3478" {
const server = TurnServer{
.host = "turn.example.com",
};
try testing.expectEqual(@as(u16, 3478), server.port);
}
test "TurnServer - username and credential are optional" {
const server = TurnServer{
.host = "turn.example.com",
};
try testing.expectEqual(@as(?[]const u8, null), server.username);
try testing.expectEqual(@as(?[]const u8, null), server.credential);
}
test "TurnConfig - servers slice can be iterated" {
const custom_servers = [_]TurnServer{
TurnServer{ .host = "turn1.example.com" },
TurnServer{ .host = "turn2.example.com", .port = 5349 },
};
const config = TurnConfig{
.servers = &custom_servers,
};
var count: usize = 0;
for (config.servers) |_| {
count += 1;
}
try testing.expectEqual(@as(usize, 2), count);
}
// ============================================================================
// ChannelBinding Tests
// ============================================================================
test "ChannelBinding - struct can be created" {
const binding = nat.ChannelBinding{
.channel_number = 0x4000,
.peer_address = Address.ipv4(.{ 192, 168, 1, 50 }, 5000),
.created_at = std.time.timestamp(), // Just created
.lifetime = 600,
};
try testing.expectEqual(@as(u16, 0x4000), binding.channel_number);
try testing.expectEqual(@as(u16, 5000), binding.peer_address.port);
try testing.expectEqual(false, binding.isExpired());
}
test "ChannelBinding - channel numbers start at 0x4000" {
// Per RFC 5766, channel numbers are in range 0x4000-0x7FFF
const min_channel: u16 = 0x4000;
const max_channel: u16 = 0x7FFF;
try testing.expect(min_channel < max_channel);
try testing.expectEqual(@as(u16, 0x4000), min_channel);
}
// ============================================================================
// Error Handling Tests
// ============================================================================
test "ManagedConnection - all three send paths return InvalidState without resources" {
const allocator = testing.allocator;
// Test direct
var direct_conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, direct_conn.send("test"));
// Test hole_punched
var hole_conn = ManagedConnection{
.conn_type = .hole_punched,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, hole_conn.send("test"));
// Test turn_relayed
var turn_conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, turn_conn.send("test"));
}
test "ManagedConnection - all three receive paths return InvalidState without resources" {
const allocator = testing.allocator;
var buffer: [1024]u8 = undefined;
// Test direct
var direct_conn = ManagedConnection{
.conn_type = .direct,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, direct_conn.receive(&buffer));
// Test hole_punched
var hole_conn = ManagedConnection{
.conn_type = .hole_punched,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, hole_conn.receive(&buffer));
// Test turn_relayed
var turn_conn = ManagedConnection{
.conn_type = .turn_relayed,
.socket = null,
.turn_client = null,
.peer_relay_addr = null,
.allocator = allocator,
};
try testing.expectError(Error.InvalidState, turn_conn.receive(&buffer));
}