ab ovo
This commit is contained in:
219
tests/protocol/test_allocation_limits.zig
Normal file
219
tests/protocol/test_allocation_limits.zig
Normal file
@@ -0,0 +1,219 @@
|
||||
//! Allocation Limits and Size Validation Tests
|
||||
//!
|
||||
//! This test suite validates that protocol message handling enforces
|
||||
//! strict size limits to prevent:
|
||||
//! - Memory exhaustion attacks
|
||||
//! - Integer overflow exploitation
|
||||
//! - Excessive resource consumption
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const protocol = @import("protocol");
|
||||
const serializer = protocol.serializer;
|
||||
const message = protocol.message;
|
||||
const protocol_constants = protocol.protocol;
|
||||
|
||||
// Import limits
|
||||
const MAX_MESSAGE_SIZE = protocol_constants.MAX_MESSAGE_SIZE;
|
||||
const Limits = serializer.Limits;
|
||||
|
||||
// ============================================================================
|
||||
// Limits Constants Tests
|
||||
// ============================================================================
|
||||
|
||||
test "limits constants are defined" {
|
||||
// Verify all limits are properly defined
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE > 0);
|
||||
try testing.expect(Limits.MAX_PAYLOAD_SIZE > 0);
|
||||
try testing.expect(Limits.MAX_DATA_ID_LENGTH > 0);
|
||||
try testing.expect(Limits.MAX_PEER_LIST_SIZE > 0);
|
||||
}
|
||||
|
||||
test "limits have reasonable values" {
|
||||
// MAX_MESSAGE_SIZE should be reasonable (64MB max)
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE <= 64 * 1024 * 1024);
|
||||
|
||||
// MAX_PAYLOAD_SIZE should be reasonable (16MB)
|
||||
try testing.expectEqual(@as(usize, 16 * 1024 * 1024), Limits.MAX_PAYLOAD_SIZE);
|
||||
|
||||
// MAX_PEER_LIST_SIZE should prevent excessive lists
|
||||
try testing.expectEqual(@as(usize, 1000), Limits.MAX_PEER_LIST_SIZE);
|
||||
|
||||
// Data ID length should be bounded
|
||||
try testing.expectEqual(@as(usize, 4096), Limits.MAX_DATA_ID_LENGTH);
|
||||
}
|
||||
|
||||
test "limits hierarchy is correct" {
|
||||
// MAX_DATA_ID_LENGTH < MAX_PAYLOAD_SIZE < MAX_MESSAGE_SIZE
|
||||
try testing.expect(Limits.MAX_DATA_ID_LENGTH < Limits.MAX_PAYLOAD_SIZE);
|
||||
try testing.expect(Limits.MAX_PAYLOAD_SIZE <= Limits.MAX_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Serializer Size Validation Tests
|
||||
// ============================================================================
|
||||
|
||||
test "serializer rejects buffer larger than MAX_MESSAGE_SIZE" {
|
||||
// Verify that the MAX_MESSAGE_SIZE limit is set correctly
|
||||
// The actual size enforcement happens at the message parsing level
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE > 0);
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE <= 64 * 1024 * 1024);
|
||||
|
||||
// Document that buffers larger than MAX_MESSAGE_SIZE should be rejected
|
||||
// before being passed to the deserializer
|
||||
}
|
||||
|
||||
test "MAX_MESSAGE_SIZE allows reasonable buffer sizes" {
|
||||
// Verify that MAX_MESSAGE_SIZE is large enough for practical use
|
||||
// but not excessively large
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE >= 1024); // At least 1KB
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE <= 64 * 1024 * 1024); // At most 64MB
|
||||
|
||||
// Verify the constant is what we expect (64MB)
|
||||
try testing.expectEqual(@as(usize, 64 * 1024 * 1024), Limits.MAX_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
test "serializer handles zero-length buffer" {
|
||||
const allocator = testing.allocator;
|
||||
// CRIT-NEW-9: Use test mode to allow null crypto_manager
|
||||
var s = serializer.MessageSerializer.initInsecureForTesting(allocator);
|
||||
|
||||
const empty_buffer: []const u8 = &[_]u8{};
|
||||
const result = s.deserialize(empty_buffer);
|
||||
|
||||
// Should fail with some error (buffer too small, not MessageTooLarge)
|
||||
try testing.expectError(serializer.SerializationError.BufferTooSmall, result);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Message Serialization Size Tests
|
||||
// ============================================================================
|
||||
|
||||
test "ping message serialization size is bounded" {
|
||||
const allocator = testing.allocator;
|
||||
// CRIT-NEW-9: Use test mode to allow null crypto_manager
|
||||
var s = serializer.MessageSerializer.initInsecureForTesting(allocator);
|
||||
|
||||
const ping = message.Ping.init(12345);
|
||||
const msg = message.Message{ .ping = ping };
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const size = try s.serialize(msg, &buffer);
|
||||
|
||||
// Ping should be small (header ~182 bytes + small payload)
|
||||
try testing.expect(size < 500);
|
||||
try testing.expect(size <= Limits.MAX_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
test "pong message serialization size is bounded" {
|
||||
const allocator = testing.allocator;
|
||||
// CRIT-NEW-9: Use test mode to allow null crypto_manager
|
||||
var s = serializer.MessageSerializer.initInsecureForTesting(allocator);
|
||||
|
||||
const pong = message.Pong.init(12345, 100);
|
||||
const msg = message.Message{ .pong = pong };
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const size = try s.serialize(msg, &buffer);
|
||||
|
||||
// Pong should be small (header ~182 bytes + small payload)
|
||||
try testing.expect(size < 500);
|
||||
try testing.expect(size <= Limits.MAX_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Security Tests - Overflow Prevention
|
||||
// ============================================================================
|
||||
|
||||
test "size fields prevent integer overflow" {
|
||||
// Test that u32 max values don't cause overflow
|
||||
const max_u32: u32 = std.math.maxInt(u32);
|
||||
|
||||
// These operations should not overflow
|
||||
const safe_check1 = @as(u64, max_u32) + 1;
|
||||
try testing.expect(safe_check1 > max_u32);
|
||||
|
||||
// The Limits are set below u32 max to prevent overflow
|
||||
try testing.expect(Limits.MAX_MESSAGE_SIZE < max_u32);
|
||||
try testing.expect(Limits.MAX_PAYLOAD_SIZE < max_u32);
|
||||
}
|
||||
|
||||
test "multiple allocations stay within limits" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Even multiple MAX_DATA_ID_LENGTH allocations should be safe
|
||||
const count = 10;
|
||||
const total = Limits.MAX_DATA_ID_LENGTH * count;
|
||||
|
||||
// This should not overflow or cause issues
|
||||
try testing.expect(total < Limits.MAX_MESSAGE_SIZE * count);
|
||||
|
||||
// We can safely allocate this much
|
||||
const buffer = try allocator.alloc(u8, Limits.MAX_DATA_ID_LENGTH);
|
||||
defer allocator.free(buffer);
|
||||
try testing.expectEqual(Limits.MAX_DATA_ID_LENGTH, buffer.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Handler Limits Tests
|
||||
// ============================================================================
|
||||
|
||||
test "handler limits are defined" {
|
||||
const HandlerLimits = protocol.handler.HandlerLimits;
|
||||
|
||||
try testing.expect(HandlerLimits.MAX_PENDING_MESSAGES > 0);
|
||||
try testing.expect(HandlerLimits.MAX_HANDLER_REGISTRY_SIZE > 0);
|
||||
}
|
||||
|
||||
test "handler limits have reasonable values" {
|
||||
const HandlerLimits = protocol.handler.HandlerLimits;
|
||||
|
||||
// MAX_PENDING_MESSAGES should prevent queue exhaustion
|
||||
try testing.expectEqual(@as(usize, 10000), HandlerLimits.MAX_PENDING_MESSAGES);
|
||||
|
||||
// MAX_HANDLER_REGISTRY_SIZE should be bounded
|
||||
try testing.expectEqual(@as(usize, 1000), HandlerLimits.MAX_HANDLER_REGISTRY_SIZE);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Documentation Test
|
||||
// ============================================================================
|
||||
|
||||
test "document all enforced limits" {
|
||||
// This test documents all the size limits enforced by the protocol
|
||||
|
||||
// Serialization limits
|
||||
_ = Limits.MAX_MESSAGE_SIZE; // Maximum total message size
|
||||
_ = Limits.MAX_PAYLOAD_SIZE; // Maximum payload data size
|
||||
_ = Limits.MAX_DATA_ID_LENGTH; // Maximum data identifier length
|
||||
_ = Limits.MAX_TOPOLOGY_DATA_SIZE; // Maximum topology data
|
||||
_ = Limits.MAX_ROUTE_INFO_SIZE; // Maximum route info size
|
||||
_ = Limits.MAX_ERROR_MESSAGE_LENGTH; // Maximum error message
|
||||
_ = Limits.MAX_APP_DATA_SIZE; // Maximum application data
|
||||
_ = Limits.MAX_PEER_LIST_SIZE; // Maximum peers in list
|
||||
_ = Limits.MAX_NODE_LIST_SIZE; // Maximum nodes in list
|
||||
|
||||
// Handler limits
|
||||
const HandlerLimits = protocol.handler.HandlerLimits;
|
||||
_ = HandlerLimits.MAX_PENDING_MESSAGES;
|
||||
_ = HandlerLimits.MAX_HANDLER_REGISTRY_SIZE;
|
||||
|
||||
// These limits prevent:
|
||||
// 1. Memory exhaustion attacks
|
||||
// 2. Integer overflow exploitation
|
||||
// 3. Queue exhaustion DoS
|
||||
// 4. Handler registry exhaustion
|
||||
}
|
||||
|
||||
test "limits are consistent across modules" {
|
||||
// Ensure limits are properly exported and accessible
|
||||
const s_limits = serializer.Limits;
|
||||
const h_limits = protocol.handler.HandlerLimits;
|
||||
|
||||
// All should be compile-time constants
|
||||
comptime {
|
||||
_ = s_limits.MAX_MESSAGE_SIZE;
|
||||
_ = s_limits.MAX_PAYLOAD_SIZE;
|
||||
_ = h_limits.MAX_PENDING_MESSAGES;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user