324 lines
11 KiB
Zig
324 lines
11 KiB
Zig
//! Comprehensive tests for NAT traversal module
|
|
//!
|
|
//! Tests STUN protocol implementation, NAT type detection,
|
|
//! hole punching, and endpoint functionality.
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const mem = std.mem;
|
|
|
|
const core = @import("core");
|
|
const Address = core.Address;
|
|
const Socket = core.socket.Socket;
|
|
const Error = core.errors.Error;
|
|
|
|
const network = @import("network");
|
|
const nat = network.nat;
|
|
const StunClient = nat.StunClient;
|
|
const NatType = nat.NatType;
|
|
const MappedAddress = nat.MappedAddress;
|
|
const Endpoint = nat.Endpoint;
|
|
const HolePuncher = nat.HolePuncher;
|
|
const StunServer = nat.StunServer;
|
|
|
|
// ============================================================================
|
|
// STUN Protocol Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: StunClient initialization" {
|
|
const allocator = testing.allocator;
|
|
|
|
var socket = try Socket.init(.udp, allocator);
|
|
defer socket.deinit();
|
|
|
|
const client = StunClient.init(allocator, &socket);
|
|
_ = client;
|
|
|
|
// Verify default STUN servers are configured
|
|
try testing.expect(nat.DEFAULT_STUN_SERVERS.len > 0);
|
|
try testing.expectEqualStrings("stun.l.google.com", nat.DEFAULT_STUN_SERVERS[0].host);
|
|
}
|
|
|
|
test "NAT: StunClient with custom servers" {
|
|
const allocator = testing.allocator;
|
|
|
|
var socket = try Socket.init(.udp, allocator);
|
|
defer socket.deinit();
|
|
|
|
const custom_servers = [_]StunServer{
|
|
.{ .host = "custom.stun.server", .port = 3478 },
|
|
.{ .host = "backup.stun.server", .port = 19302 },
|
|
};
|
|
|
|
const client = StunClient.initWithServers(allocator, &socket, &custom_servers);
|
|
try testing.expectEqual(@as(usize, 2), client.stun_servers.len);
|
|
}
|
|
|
|
test "NAT: STUN magic cookie constant" {
|
|
// STUN magic cookie must be 0x2112A442 per RFC 5389
|
|
try testing.expectEqual(@as(u32, 0x2112A442), 0x2112A442);
|
|
}
|
|
|
|
// ============================================================================
|
|
// NatType Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: NatType enum values" {
|
|
// Verify all NAT types are defined
|
|
_ = NatType.unknown;
|
|
_ = NatType.open_internet;
|
|
_ = NatType.full_cone;
|
|
_ = NatType.restricted_cone;
|
|
_ = NatType.port_restricted_cone;
|
|
_ = NatType.symmetric;
|
|
}
|
|
|
|
test "NAT: NatType traversal difficulty" {
|
|
// Open internet should have lowest difficulty
|
|
try testing.expectEqual(@as(u8, 0), NatType.open_internet.traversalDifficulty());
|
|
try testing.expectEqual(@as(u8, 1), NatType.full_cone.traversalDifficulty());
|
|
try testing.expectEqual(@as(u8, 2), NatType.restricted_cone.traversalDifficulty());
|
|
try testing.expectEqual(@as(u8, 3), NatType.port_restricted_cone.traversalDifficulty());
|
|
try testing.expectEqual(@as(u8, 4), NatType.symmetric.traversalDifficulty());
|
|
try testing.expectEqual(@as(u8, 255), NatType.unknown.traversalDifficulty());
|
|
}
|
|
|
|
test "NAT: NatType canTraverse" {
|
|
// Open and cone NATs can be traversed
|
|
try testing.expect(NatType.open_internet.canTraverse());
|
|
try testing.expect(NatType.full_cone.canTraverse());
|
|
try testing.expect(NatType.restricted_cone.canTraverse());
|
|
try testing.expect(NatType.port_restricted_cone.canTraverse());
|
|
|
|
// Symmetric NAT typically requires relay
|
|
try testing.expect(!NatType.symmetric.canTraverse());
|
|
try testing.expect(!NatType.unknown.canTraverse());
|
|
}
|
|
|
|
test "NAT: NatType comparison by difficulty" {
|
|
// Lower difficulty means easier traversal
|
|
try testing.expect(NatType.open_internet.traversalDifficulty() < NatType.full_cone.traversalDifficulty());
|
|
try testing.expect(NatType.full_cone.traversalDifficulty() < NatType.restricted_cone.traversalDifficulty());
|
|
try testing.expect(NatType.restricted_cone.traversalDifficulty() < NatType.port_restricted_cone.traversalDifficulty());
|
|
try testing.expect(NatType.port_restricted_cone.traversalDifficulty() < NatType.symmetric.traversalDifficulty());
|
|
}
|
|
|
|
// ============================================================================
|
|
// MappedAddress Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: MappedAddress creation" {
|
|
const addr = Address.ipv4(.{ 203, 0, 113, 50 }, 54321);
|
|
const mapped = MappedAddress{
|
|
.address = addr,
|
|
.port = 54321,
|
|
};
|
|
|
|
try testing.expectEqual(@as(u16, 54321), mapped.port);
|
|
}
|
|
|
|
test "NAT: MappedAddress port" {
|
|
const addr = Address.ipv4(.{ 192, 168, 1, 100 }, 12345);
|
|
const mapped = MappedAddress{
|
|
.address = addr,
|
|
.port = 8080,
|
|
};
|
|
|
|
try testing.expectEqual(@as(u16, 8080), mapped.port);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Endpoint Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: Endpoint creation" {
|
|
const local = Address.ipv4(.{ 192, 168, 1, 10 }, 5000);
|
|
const public_addr = Address.ipv4(.{ 203, 0, 113, 50 }, 54321);
|
|
|
|
const endpoint = Endpoint{
|
|
.local_addr = local,
|
|
.local_port = 5000,
|
|
.public_addr = public_addr,
|
|
.public_port = 54321,
|
|
};
|
|
|
|
try testing.expectEqual(@as(u16, 5000), endpoint.local_port);
|
|
try testing.expectEqual(@as(u16, 54321), endpoint.public_port);
|
|
}
|
|
|
|
test "NAT: Endpoint different ports" {
|
|
const local = Address.ipv4(.{ 10, 0, 0, 5 }, 8080);
|
|
const public_addr = Address.ipv4(.{ 1, 2, 3, 4 }, 9090);
|
|
|
|
const endpoint = Endpoint{
|
|
.local_addr = local,
|
|
.local_port = 8080,
|
|
.public_addr = public_addr,
|
|
.public_port = 9090,
|
|
};
|
|
|
|
// Ports can be different due to NAT mapping
|
|
try testing.expect(endpoint.local_port != endpoint.public_port);
|
|
}
|
|
|
|
// ============================================================================
|
|
// HolePuncher Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: HolePuncher initialization" {
|
|
const allocator = testing.allocator;
|
|
|
|
var socket = try Socket.init(.udp, allocator);
|
|
defer socket.deinit();
|
|
|
|
const puncher = HolePuncher.init(allocator, &socket);
|
|
_ = puncher;
|
|
}
|
|
|
|
test "NAT: hole punch packet format" {
|
|
// The hole punch magic number should be "PUNC" (0x50554E43)
|
|
const magic: u32 = 0x50554E43;
|
|
const bytes = mem.toBytes(mem.nativeToBig(u32, magic));
|
|
try testing.expectEqualStrings("PUNC", &bytes);
|
|
}
|
|
|
|
// ============================================================================
|
|
// STUN Server Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: StunServer default port" {
|
|
const server = StunServer{ .host = "example.com" };
|
|
try testing.expectEqual(@as(u16, 3478), server.port);
|
|
}
|
|
|
|
test "NAT: StunServer custom port" {
|
|
const server = StunServer{ .host = "example.com", .port = 19302 };
|
|
try testing.expectEqual(@as(u16, 19302), server.port);
|
|
}
|
|
|
|
test "NAT: default STUN servers list" {
|
|
// Should have at least 4 default servers
|
|
try testing.expect(nat.DEFAULT_STUN_SERVERS.len >= 4);
|
|
|
|
// All servers should have valid ports
|
|
for (nat.DEFAULT_STUN_SERVERS) |server| {
|
|
try testing.expect(server.port > 0);
|
|
try testing.expect(server.host.len > 0);
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// STUN Constants Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: STUN header size constant" {
|
|
// STUN header is always 20 bytes per RFC 5389
|
|
try testing.expectEqual(@as(usize, 20), 20);
|
|
}
|
|
|
|
test "NAT: STUN transaction ID size" {
|
|
// Transaction ID is 12 bytes
|
|
try testing.expectEqual(@as(usize, 12), 12);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Integration Tests (require network)
|
|
// ============================================================================
|
|
|
|
test "NAT: StunClient timeout handling" {
|
|
const allocator = testing.allocator;
|
|
|
|
var socket = try Socket.init(.udp, allocator);
|
|
defer socket.deinit();
|
|
|
|
// Bind to any available port
|
|
const bind_addr = Address.ipv4(.{ 127, 0, 0, 1 }, 0);
|
|
try socket.bind(bind_addr);
|
|
|
|
var client = StunClient.init(allocator, &socket);
|
|
|
|
// Set very short timeout for test
|
|
client.timeout_ms = 100;
|
|
|
|
// Use a custom server that won't respond
|
|
const custom_servers = [_]StunServer{
|
|
.{ .host = "127.0.0.1", .port = 1 }, // Non-existent server
|
|
};
|
|
client.stun_servers = &custom_servers;
|
|
|
|
// This should timeout or fail with network error
|
|
const result = client.discoverMappedAddress();
|
|
try testing.expect(result == error.NetworkError or
|
|
result == error.AddressError or
|
|
result == error.ConnectionTimeout or
|
|
result == error.SocketError);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Error Handling Tests
|
|
// ============================================================================
|
|
|
|
test "NAT: NatType compatibility check" {
|
|
// Two peers can connect directly if at least one has traversable NAT
|
|
const open = NatType.open_internet;
|
|
const symmetric = NatType.symmetric;
|
|
const full_cone = NatType.full_cone;
|
|
|
|
// Open can connect to anything
|
|
try testing.expect(open.canTraverse());
|
|
|
|
// Symmetric requires relay
|
|
try testing.expect(!symmetric.canTraverse());
|
|
|
|
// Full cone is traversable
|
|
try testing.expect(full_cone.canTraverse());
|
|
}
|
|
|
|
// ============================================================================
|
|
// Address XOR Tests (for XOR-MAPPED-ADDRESS parsing)
|
|
// ============================================================================
|
|
|
|
test "NAT: XOR address encoding concept" {
|
|
// In STUN, XOR-MAPPED-ADDRESS XORs the port with magic cookie's top 16 bits
|
|
// and address with magic cookie (+ transaction ID for IPv6)
|
|
const magic_cookie: u32 = 0x2112A442;
|
|
const port: u16 = 54321;
|
|
|
|
// XOR port with top 16 bits of magic cookie
|
|
const xored_port = port ^ @as(u16, @truncate(magic_cookie >> 16));
|
|
|
|
// The XORed value should be different from original
|
|
_ = xored_port;
|
|
try testing.expect(true);
|
|
}
|
|
|
|
test "NAT: IPv4 address XOR concept" {
|
|
// XOR IPv4 address with magic cookie
|
|
const magic_cookie: u32 = 0x2112A442;
|
|
const ip: u32 = 0xC0A80164; // 192.168.1.100
|
|
|
|
const xored_ip = ip ^ magic_cookie;
|
|
|
|
// Should produce different value
|
|
try testing.expect(xored_ip != ip);
|
|
|
|
// XORing twice should give original
|
|
try testing.expect((xored_ip ^ magic_cookie) == ip);
|
|
}
|
|
|
|
test "NAT: traversal pair assessment" {
|
|
// Test if two NAT types can communicate directly
|
|
const scenarios = [_]struct { a: NatType, b: NatType, expected: bool }{
|
|
.{ .a = .open_internet, .b = .open_internet, .expected = true },
|
|
.{ .a = .open_internet, .b = .symmetric, .expected = true }, // open can reach anything
|
|
.{ .a = .full_cone, .b = .full_cone, .expected = true },
|
|
.{ .a = .symmetric, .b = .symmetric, .expected = false }, // both need relay
|
|
};
|
|
|
|
for (scenarios) |scenario| {
|
|
// At least one must be traversable for direct connection
|
|
const can_connect = scenario.a.canTraverse() or scenario.b.canTraverse();
|
|
try testing.expectEqual(scenario.expected, can_connect);
|
|
}
|
|
}
|