ab ovo
This commit is contained in:
410
tests/protocol/test_replay_integration.zig
Normal file
410
tests/protocol/test_replay_integration.zig
Normal file
@@ -0,0 +1,410 @@
|
||||
//! Comprehensive integration tests for replay protection in message handlers
|
||||
//! This file tests that replay protection is properly wired into the message handling pipeline
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const protocol = @import("protocol");
|
||||
const handler_mod = protocol.handler;
|
||||
const message_mod = protocol.message;
|
||||
const replay_protection = protocol.replay_protection;
|
||||
|
||||
/// Test that duplicate message IDs are rejected
|
||||
test "replay protection: duplicate message_id is rejected" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// Create a simple ping message
|
||||
const ping = message_mod.Ping.init(12345);
|
||||
const msg = message_mod.Message{ .ping = ping };
|
||||
|
||||
// Set source peer for context
|
||||
const peer_id: message_mod.NodeId = [_]u8{1} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// First message should succeed
|
||||
try registry.processMessage(msg);
|
||||
|
||||
// Immediate replay should fail (same message ID, timestamp, nonce)
|
||||
const result = registry.processMessage(msg);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, result);
|
||||
}
|
||||
|
||||
/// Test that old timestamps are rejected
|
||||
test "replay protection: old timestamp is rejected" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// Configure replay protector with short TTL for testing
|
||||
registry.replay_protector.configure(null, 1, null); // 1 second TTL
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{2} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Create message with very old timestamp
|
||||
const old_timestamp = std.time.timestamp() - 7200; // 2 hours ago
|
||||
|
||||
// Directly test the replay protector with old timestamp
|
||||
const result = registry.replay_protector.checkMessage(
|
||||
12345,
|
||||
old_timestamp,
|
||||
1000,
|
||||
0,
|
||||
peer_id,
|
||||
);
|
||||
|
||||
try testing.expectError(replay_protection.ReplayError.TimestampTooOld, result);
|
||||
}
|
||||
|
||||
/// Test that future timestamps are rejected
|
||||
test "replay protection: future timestamp is rejected" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{3} ** 32;
|
||||
|
||||
// Create message with future timestamp (beyond clock skew tolerance)
|
||||
const future_timestamp = std.time.timestamp() + 600; // 10 minutes in future
|
||||
|
||||
// Test directly via replay protector
|
||||
const result = registry.replay_protector.checkMessage(
|
||||
12345,
|
||||
future_timestamp,
|
||||
1000,
|
||||
0,
|
||||
peer_id,
|
||||
);
|
||||
|
||||
try testing.expectError(replay_protection.ReplayError.TimestampTooFarInFuture, result);
|
||||
}
|
||||
|
||||
/// Test that valid messages pass through replay protection
|
||||
test "replay protection: valid messages pass through" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{4} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Create different ping messages with different IDs
|
||||
const ping1 = message_mod.Ping.init(100);
|
||||
const ping2 = message_mod.Ping.init(200);
|
||||
const ping3 = message_mod.Ping.init(300);
|
||||
|
||||
const msg1 = message_mod.Message{ .ping = ping1 };
|
||||
const msg2 = message_mod.Message{ .ping = ping2 };
|
||||
const msg3 = message_mod.Message{ .ping = ping3 };
|
||||
|
||||
// All messages should succeed
|
||||
try registry.processMessage(msg1);
|
||||
try registry.processMessage(msg2);
|
||||
try registry.processMessage(msg3);
|
||||
|
||||
// Verify metrics
|
||||
try testing.expectEqual(@as(u64, 3), registry.context.metrics.messages_processed);
|
||||
try testing.expectEqual(@as(u64, 0), registry.context.metrics.messages_failed);
|
||||
}
|
||||
|
||||
/// Test cleanup removes old entries
|
||||
test "replay protection: cleanup removes old entries" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// Configure with very short TTL for testing
|
||||
registry.replay_protector.configure(null, 1, null); // 1 second TTL
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{5} ** 32;
|
||||
|
||||
// Add some messages with old timestamps
|
||||
const old_time = std.time.timestamp() - 2;
|
||||
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id);
|
||||
_ = try registry.replay_protector.checkMessage(2, old_time, 1001, 2, peer_id);
|
||||
|
||||
var stats = registry.getReplayStats();
|
||||
try testing.expect(stats.tracked_messages > 0);
|
||||
|
||||
// Force cleanup
|
||||
registry.cleanupReplayProtection();
|
||||
|
||||
// Old messages should be cleaned up
|
||||
stats = registry.getReplayStats();
|
||||
try testing.expectEqual(@as(usize, 0), stats.tracked_messages);
|
||||
}
|
||||
|
||||
/// Test per-peer sequence tracking works
|
||||
test "replay protection: per-peer sequence tracking" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer1: message_mod.NodeId = [_]u8{6} ** 32;
|
||||
const peer2: message_mod.NodeId = [_]u8{7} ** 32;
|
||||
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// Peer 1 sends messages in order
|
||||
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1);
|
||||
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer1);
|
||||
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 3, peer1);
|
||||
|
||||
// Peer 2 can use same sequences independently
|
||||
try registry.replay_protector.checkMessage(10, current_time, 2000, 1, peer2);
|
||||
try registry.replay_protector.checkMessage(11, current_time + 1, 2001, 2, peer2);
|
||||
|
||||
// Both peers tracked
|
||||
const stats = registry.getReplayStats();
|
||||
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
|
||||
|
||||
// Replay from peer1 should fail
|
||||
const result = registry.replay_protector.checkMessage(4, current_time + 3, 1003, 2, peer1);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, result);
|
||||
}
|
||||
|
||||
/// Test nonce uniqueness enforcement
|
||||
test "replay protection: nonce uniqueness" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{8} ** 32;
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// First message with nonce 5000
|
||||
try registry.replay_protector.checkMessage(100, current_time, 5000, 0, peer_id);
|
||||
|
||||
// Different message ID but same nonce should fail
|
||||
const result = registry.replay_protector.checkMessage(101, current_time + 1, 5000, 0, peer_id);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateNonce, result);
|
||||
}
|
||||
|
||||
/// Test out-of-order messages within sliding window
|
||||
test "replay protection: out-of-order messages within window" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{9} ** 32;
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// Send sequence 1, 2, 5, then 3 (out of order but within window)
|
||||
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer_id);
|
||||
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 2, peer_id);
|
||||
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 5, peer_id);
|
||||
|
||||
// Message 3 (out of order) should be accepted
|
||||
try registry.replay_protector.checkMessage(4, current_time + 3, 1003, 3, peer_id);
|
||||
}
|
||||
|
||||
/// Test sequences too old (outside window) are rejected
|
||||
test "replay protection: sequences too old are rejected" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{10} ** 32;
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// Send sequence 100 (establishes baseline)
|
||||
try registry.replay_protector.checkMessage(1, current_time, 1000, 100, peer_id);
|
||||
|
||||
// Try to send sequence 1 (too old, outside 64-message window)
|
||||
const result = registry.replay_protector.checkMessage(2, current_time + 1, 1001, 1, peer_id);
|
||||
try testing.expectError(replay_protection.ReplayError.SequenceTooOld, result);
|
||||
}
|
||||
|
||||
/// Test peer sequence reset functionality
|
||||
test "replay protection: peer sequence reset" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{11} ** 32;
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// Send some messages
|
||||
try registry.replay_protector.checkMessage(1, current_time, 1000, 5, peer_id);
|
||||
try registry.replay_protector.checkMessage(2, current_time + 1, 1001, 6, peer_id);
|
||||
|
||||
// Reset peer sequence (simulating reconnection)
|
||||
try registry.resetPeerReplayState(peer_id);
|
||||
|
||||
// Should be able to start from sequence 1 again
|
||||
try registry.replay_protector.checkMessage(3, current_time + 2, 1002, 1, peer_id);
|
||||
}
|
||||
|
||||
/// Test integration: message processing with replay protection enabled
|
||||
test "replay protection: full integration test" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{12} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Register a simple handler that counts messages
|
||||
var message_count: u32 = 0;
|
||||
const TestContext = struct {
|
||||
count: *u32,
|
||||
};
|
||||
var test_ctx = TestContext{ .count = &message_count };
|
||||
|
||||
const test_handler = handler_mod.MessageHandler.init(
|
||||
struct {
|
||||
fn handler(msg: message_mod.Message, ctx: *anyopaque) !void {
|
||||
_ = msg;
|
||||
const test_context = @as(*TestContext, @ptrCast(@alignCast(ctx)));
|
||||
test_context.count.* += 1;
|
||||
}
|
||||
}.handler,
|
||||
&test_ctx,
|
||||
.ping,
|
||||
);
|
||||
|
||||
try registry.registerHandler(.ping, test_handler);
|
||||
|
||||
// Send valid messages
|
||||
const ping1 = message_mod.Ping.init(1);
|
||||
const ping2 = message_mod.Ping.init(2);
|
||||
const msg1 = message_mod.Message{ .ping = ping1 };
|
||||
const msg2 = message_mod.Message{ .ping = ping2 };
|
||||
|
||||
try registry.processMessage(msg1);
|
||||
try registry.processMessage(msg2);
|
||||
|
||||
// Handler should have been called twice
|
||||
try testing.expectEqual(@as(u32, 2), message_count);
|
||||
|
||||
// Try to replay msg1 - should be rejected and handler should NOT be called
|
||||
const replay_result = registry.processMessage(msg1);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, replay_result);
|
||||
|
||||
// Message count should still be 2 (handler not called for replay)
|
||||
try testing.expectEqual(@as(u32, 2), message_count);
|
||||
}
|
||||
|
||||
/// Test statistics reporting
|
||||
test "replay protection: statistics reporting" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer1: message_mod.NodeId = [_]u8{13} ** 32;
|
||||
const peer2: message_mod.NodeId = [_]u8{14} ** 32;
|
||||
const current_time = std.time.timestamp();
|
||||
|
||||
// Add some messages
|
||||
try registry.replay_protector.checkMessage(1, current_time, 1000, 1, peer1);
|
||||
try registry.replay_protector.checkMessage(2, current_time, 2000, 1, peer2);
|
||||
|
||||
const stats = registry.getReplayStats();
|
||||
|
||||
// Should have tracked messages, nonces, and peers
|
||||
try testing.expect(stats.tracked_messages >= 2);
|
||||
try testing.expect(stats.tracked_nonces >= 2);
|
||||
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
|
||||
}
|
||||
|
||||
/// Test automatic cleanup during checkMessage
|
||||
test "replay protection: automatic cleanup during check" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// Set very short cleanup interval (0 = immediate)
|
||||
registry.replay_protector.configure(null, 0, 0); // 0 second TTL, 0 second cleanup interval
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{15} ** 32;
|
||||
|
||||
// Add old message (already stale with 0 TTL)
|
||||
const old_time = std.time.timestamp() - 1;
|
||||
_ = try registry.replay_protector.checkMessage(1, old_time, 1000, 1, peer_id);
|
||||
|
||||
// Brief wait to ensure cleanup can trigger
|
||||
std.time.sleep(50 * std.time.ns_per_ms);
|
||||
|
||||
// Add new message (should trigger automatic cleanup)
|
||||
const current_time = std.time.timestamp();
|
||||
try registry.replay_protector.checkMessage(2, current_time, 2000, 2, peer_id);
|
||||
|
||||
// Old message should have been cleaned up automatically
|
||||
const stats = registry.getReplayStats();
|
||||
try testing.expect(stats.tracked_messages <= 1);
|
||||
}
|
||||
|
||||
/// Test multiple message types with replay protection
|
||||
test "replay protection: multiple message types" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{16} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Create different message types
|
||||
const ping = message_mod.Ping.init(100);
|
||||
const pong = message_mod.Pong{ .sequence = 200, .rtt = 10 };
|
||||
|
||||
const ping_msg = message_mod.Message{ .ping = ping };
|
||||
const pong_msg = message_mod.Message{ .pong = pong };
|
||||
|
||||
// All different message types should be processed
|
||||
try registry.processMessage(ping_msg);
|
||||
try registry.processMessage(pong_msg);
|
||||
|
||||
// Same message replayed should fail
|
||||
const result1 = registry.processMessage(ping_msg);
|
||||
const result2 = registry.processMessage(pong_msg);
|
||||
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, result1);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, result2);
|
||||
}
|
||||
|
||||
/// Test that replay protection is mandatory by default
|
||||
test "replay protection: enabled by default (security requirement)" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// SECURITY: Replay protection MUST be enabled by default
|
||||
try testing.expect(registry.require_replay_protection);
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{17} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Create and send a message
|
||||
const ping = message_mod.Ping.init(500);
|
||||
const msg = message_mod.Message{ .ping = ping };
|
||||
try registry.processMessage(msg);
|
||||
|
||||
// Replay should be rejected when protection is enabled (default)
|
||||
const replay_result = registry.processMessage(msg);
|
||||
try testing.expectError(replay_protection.ReplayError.DuplicateMessageId, replay_result);
|
||||
}
|
||||
|
||||
/// Test that replay protection can be explicitly disabled for testing only
|
||||
test "replay protection: can be disabled for testing (SECURITY WARNING)" {
|
||||
const allocator = testing.allocator;
|
||||
var registry = try handler_mod.HandlerRegistry.init(allocator);
|
||||
defer registry.deinit();
|
||||
|
||||
// SECURITY WARNING: Only disable for testing purposes - production MUST have this enabled
|
||||
registry.require_replay_protection = false;
|
||||
|
||||
const peer_id: message_mod.NodeId = [_]u8{18} ** 32;
|
||||
registry.context.source_peer_id = peer_id;
|
||||
|
||||
// Create and send a message
|
||||
const ping = message_mod.Ping.init(600);
|
||||
const msg = message_mod.Message{ .ping = ping };
|
||||
try registry.processMessage(msg);
|
||||
|
||||
// Replay should be ACCEPTED when protection is disabled (only for testing)
|
||||
try registry.processMessage(msg);
|
||||
|
||||
// Verify both messages were processed (replay not blocked)
|
||||
try testing.expectEqual(@as(u64, 2), registry.context.metrics.messages_processed);
|
||||
}
|
||||
Reference in New Issue
Block a user