This commit is contained in:
2026-02-06 21:03:59 -03:00
commit 2b7ab5f570
267 changed files with 176026 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
//! Test suite for security fixes
//! This test verifies all 4 critical security issues have been fixed
const std = @import("std");
const testing = std.testing;
const core = @import("core");
const Error = core.Error;
const Address = core.address.Address;
const AddressResolver = core.address.AddressResolver;
const Connection = core.connection.Connection;
const ConnectionConfig = core.connection.ConnectionConfig;
const ConnectionPool = core.connection.ConnectionPool;
const ConnectionStats = core.connection.ConnectionStats;
// Issue 1: Discovery Buffer Bounds
test "Issue 1: Discovery buffer size validation" {
const network = @import("network");
const discovery = network.discovery;
const DiscoveryConfig = discovery.DiscoveryConfig;
const MAX_RECEIVE_BUFFER_SIZE = discovery.MAX_RECEIVE_BUFFER_SIZE;
// Valid configuration should pass
var valid_config = DiscoveryConfig{
.broadcast_address = try Address.parse("255.255.255.255:0"),
.receive_buffer_size = 4096,
};
try valid_config.validate();
// Configuration with buffer size exceeding maximum should fail
var invalid_config = DiscoveryConfig{
.broadcast_address = try Address.parse("255.255.255.255:0"),
.receive_buffer_size = MAX_RECEIVE_BUFFER_SIZE + 1,
};
try testing.expectError(Error.InvalidConfiguration, invalid_config.validate());
// Configuration with zero buffer size should fail
var zero_config = DiscoveryConfig{
.broadcast_address = try Address.parse("255.255.255.255:0"),
.receive_buffer_size = 0,
};
try testing.expectError(Error.InvalidConfiguration, zero_config.validate());
}
// Issue 2: DNS Amplification - Rate Limiting
test "Issue 2: DNS rate limiting" {
var resolver = AddressResolver.init(testing.allocator);
defer resolver.deinit();
// First lookup - will fail with AddressParseError (DNS not implemented)
// but should update rate limiter
_ = resolver.resolve("test.example.com") catch |err| {
try testing.expectEqual(Error.AddressParseError, err);
};
// Immediate second lookup to same hostname should be rate limited
// before even attempting DNS resolution
const result2 = resolver.resolve("test.example.com");
try testing.expectError(Error.ResourceExhausted, result2);
// Different hostname should be allowed (not rate limited)
_ = resolver.resolve("other.example.com") catch |err| {
// Should get AddressParseError (DNS not implemented), not ResourceExhausted
try testing.expectEqual(Error.AddressParseError, err);
};
}
// Issue 3: Connection Pool Unbounded Growth
test "Issue 3: Connection pool size limit" {
const config = ConnectionConfig{
.connection_type = .udp,
};
// Create pool with small max size for testing
var pool = ConnectionPool.initWithMaxSize(config, testing.allocator, 3);
defer pool.deinit();
// Should be able to create up to max_pool_size connections
_ = try pool.createConnection(.udp);
_ = try pool.createConnection(.udp);
_ = try pool.createConnection(.udp);
// 4th connection should fail with PoolExhausted
const result = pool.createConnection(.udp);
try testing.expectError(Error.PoolExhausted, result);
// Verify pool size
try testing.expectEqual(@as(usize, 3), pool.getMaxPoolSize());
}
// Issue 4: Integer Overflow in Connection Stats
test "Issue 4: Connection stats overflow handling" {
// Note: This test intentionally logs warnings about counter saturation
// This is the expected security behavior - logging when counters reach max value
var stats = ConnectionStats.init();
// Test saturating arithmetic for byte counters
// Set bytes_sent to near maximum
const near_max = std.math.maxInt(u64) - 10;
stats.bytes_sent.store(near_max, .monotonic);
// Record send that would overflow - should log warning and saturate
stats.recordSend(100, 1);
// Should saturate at max value, not wrap around
const bytes_sent = stats.getBytesSent();
try testing.expectEqual(std.math.maxInt(u64), bytes_sent);
// Same for bytes_received
stats.bytes_received.store(near_max, .monotonic);
stats.recordReceive(100, 1);
try testing.expectEqual(std.math.maxInt(u64), stats.getBytesReceived());
// Test counter saturation
stats.connection_attempts.store(std.math.maxInt(u64), .monotonic);
stats.recordConnectionAttempt(); // Logs warning - expected
// Should stay at max, not overflow
try testing.expectEqual(std.math.maxInt(u64), stats.getConnectionAttempts());
// Verify other counters work similarly
stats.errors.store(std.math.maxInt(u64), .monotonic);
stats.recordError(); // Logs warning - expected
try testing.expectEqual(std.math.maxInt(u64), stats.getErrors());
}
test "Issue 4: Large usize to u64 conversion protection" {
// Note: This test intentionally logs warnings about overflow detection
// This is the expected security behavior
var stats = ConnectionStats.init();
// On 64-bit systems, this would be a very large value
// The code should handle this gracefully
const huge_value: usize = std.math.maxInt(usize);
// This should be checked and handled safely - will log warning if overflow
stats.recordSend(huge_value, 1);
// The result should be capped at u64 max
const result = stats.getBytesSent();
try testing.expect(result <= std.math.maxInt(u64));
}