646 lines
24 KiB
Zig
646 lines
24 KiB
Zig
//! Comprehensive tests for replay protection module
|
|
//!
|
|
//! Tests memory limits, edge cases, and code paths not covered
|
|
//! by the inline tests in replay_protection.zig.
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
|
|
const protocol = @import("protocol");
|
|
const ReplayProtector = protocol.replay_protection.ReplayProtector;
|
|
const ReplayError = protocol.replay_protection.ReplayError;
|
|
const MemoryError = protocol.replay_protection.MemoryError;
|
|
const ReplayStats = protocol.replay_protection.ReplayStats;
|
|
|
|
// ============================================================================
|
|
// Basic Initialization Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: init with default values" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
// Verify default configuration (HIGH-3 tighter defaults)
|
|
try testing.expectEqual(@as(i64, 5), protector.max_clock_skew);
|
|
try testing.expectEqual(@as(i64, 10), protector.message_ttl);
|
|
try testing.expectEqual(@as(i64, 30), protector.cleanup_interval);
|
|
|
|
// Verify empty state
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_messages);
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_peers);
|
|
}
|
|
|
|
test "ReplayProtector: constants are properly defined" {
|
|
// Verify security constants
|
|
try testing.expectEqual(@as(usize, 600_000), ReplayProtector.MAX_TRACKED_MESSAGES);
|
|
try testing.expectEqual(@as(usize, 10_000), ReplayProtector.MAX_TRACKED_PEERS);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Configuration Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: configure with partial values" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
// Configure only clock skew
|
|
protector.configure(10, null, null);
|
|
try testing.expectEqual(@as(i64, 10), protector.max_clock_skew);
|
|
try testing.expectEqual(@as(i64, 10), protector.message_ttl); // unchanged
|
|
|
|
// Configure only TTL
|
|
protector.configure(null, 60, null);
|
|
try testing.expectEqual(@as(i64, 10), protector.max_clock_skew); // unchanged
|
|
try testing.expectEqual(@as(i64, 60), protector.message_ttl);
|
|
|
|
// Configure only cleanup interval
|
|
protector.configure(null, null, 120);
|
|
try testing.expectEqual(@as(i64, 120), protector.cleanup_interval);
|
|
}
|
|
|
|
test "ReplayProtector: configure with all null values" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const orig_skew = protector.max_clock_skew;
|
|
const orig_ttl = protector.message_ttl;
|
|
const orig_interval = protector.cleanup_interval;
|
|
|
|
// Configure with all nulls should not change anything
|
|
protector.configure(null, null, null);
|
|
|
|
try testing.expectEqual(orig_skew, protector.max_clock_skew);
|
|
try testing.expectEqual(orig_ttl, protector.message_ttl);
|
|
try testing.expectEqual(orig_interval, protector.cleanup_interval);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Timestamp Validation Edge Cases
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: timestamp at exact TTL boundary" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{1} ** 32;
|
|
|
|
// Message at exactly TTL boundary (10 seconds old with 10s TTL)
|
|
// validateTimestamp uses (time_diff < -message_ttl), so exactly at boundary is accepted
|
|
const boundary_timestamp = current_time - protector.message_ttl;
|
|
try protector.checkMessage(1, boundary_timestamp, 0, peer_id, 0);
|
|
|
|
// Message just past TTL boundary should be rejected
|
|
const past_boundary = current_time - protector.message_ttl - 1;
|
|
const result = protector.checkMessage(2, past_boundary, 0, peer_id, 0);
|
|
try testing.expectError(ReplayError.TimestampTooOld, result);
|
|
}
|
|
|
|
test "ReplayProtector: timestamp just within TTL" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{1} ** 32;
|
|
|
|
// Message just within TTL boundary
|
|
const valid_timestamp = current_time - protector.message_ttl + 1;
|
|
try protector.checkMessage(1, valid_timestamp, 0, peer_id, 0);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: timestamp at exact clock skew boundary" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{1} ** 32;
|
|
|
|
// Message at exactly clock skew boundary (5 seconds in future with 5s skew)
|
|
// This should be accepted
|
|
const boundary_timestamp = current_time + protector.max_clock_skew;
|
|
try protector.checkMessage(1, boundary_timestamp, 0, peer_id, 0);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: timestamp just beyond clock skew" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{1} ** 32;
|
|
|
|
// Message just beyond clock skew boundary
|
|
const future_timestamp = current_time + protector.max_clock_skew + 1;
|
|
const result = protector.checkMessage(1, future_timestamp, 0, peer_id, 0);
|
|
try testing.expectError(ReplayError.TimestampTooFarInFuture, result);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Per-Peer Message ID Dedup Edge Cases
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: duplicate msg_id from same peer rejected" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{2} ** 32;
|
|
|
|
// First message accepted
|
|
try protector.checkMessage(42, current_time, 0, peer_id, 0);
|
|
|
|
// Same msg_id from same peer → DuplicateMessageId
|
|
const result = protector.checkMessage(42, current_time + 1, 0, peer_id, 0);
|
|
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
|
}
|
|
|
|
test "ReplayProtector: same msg_id from different peers accepted" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_a = [_]u8{3} ** 32;
|
|
const peer_b = [_]u8{4} ** 32;
|
|
|
|
// Peer A sends msg_id 42
|
|
try protector.checkMessage(42, current_time, 0, peer_a, 0);
|
|
|
|
// Peer B sends same msg_id 42 — accepted (per-peer dedup)
|
|
try protector.checkMessage(42, current_time + 1, 0, peer_b, 0);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: msg_id reuse after TTL expiry allowed" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
// Use short TTL for testing
|
|
protector.configure(null, 1, null);
|
|
|
|
const base_time = std.time.timestamp();
|
|
const peer_id = [_]u8{3} ** 32;
|
|
|
|
// Add a message with msg_id 1
|
|
try protector.checkMessage(1, base_time, 0, peer_id, 0);
|
|
|
|
// Force cleanup with future time to expire the entry
|
|
protector.cleanup(base_time + 10);
|
|
|
|
// Now msg_id 1 should be allowed again (with new timestamp)
|
|
const new_time = std.time.timestamp();
|
|
try protector.checkMessage(1, new_time, 0, peer_id, 0);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Sequence Number Edge Cases
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: sequence 0 is always accepted" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{4} ** 32;
|
|
|
|
// Sequence 0 is reserved for non-ordered messages
|
|
try protector.checkMessage(1, current_time, 0, peer_id, 0);
|
|
try protector.checkMessage(2, current_time + 1, 0, peer_id, 0);
|
|
try protector.checkMessage(3, current_time + 2, 0, peer_id, 0);
|
|
|
|
// No peer should be tracked for sequence 0 messages
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_peers);
|
|
}
|
|
|
|
test "ReplayProtector: sequence at window boundary" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{5} ** 32;
|
|
|
|
// Send sequence 100
|
|
try protector.checkMessage(1, current_time, 100, peer_id, 0);
|
|
|
|
// Sequence 37 is at position 63 (100 - 37 = 63), which is the last bit in the window
|
|
try protector.checkMessage(2, current_time + 1, 37, peer_id, 0);
|
|
|
|
// Sequence 36 is at position 64, which is outside the window (WINDOW_SIZE = 64)
|
|
const result = protector.checkMessage(3, current_time + 2, 36, peer_id, 0);
|
|
try testing.expectError(ReplayError.SequenceTooOld, result);
|
|
}
|
|
|
|
test "ReplayProtector: sequence replay in sliding window" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{6} ** 32;
|
|
|
|
// Send sequences 1, 5, then 3 (out of order)
|
|
try protector.checkMessage(1, current_time, 1, peer_id, 0);
|
|
try protector.checkMessage(2, current_time + 1, 5, peer_id, 0);
|
|
try protector.checkMessage(3, current_time + 2, 3, peer_id, 0);
|
|
|
|
// Replay sequence 3 should fail (InvalidSequence - already in window)
|
|
const result = protector.checkMessage(4, current_time + 3, 3, peer_id, 0);
|
|
try testing.expectError(ReplayError.InvalidSequence, result);
|
|
}
|
|
|
|
test "ReplayProtector: sequence exactly at last_sequence" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{7} ** 32;
|
|
|
|
// Send sequence 10
|
|
try protector.checkMessage(1, current_time, 10, peer_id, 0);
|
|
|
|
// Send sequence 10 again (duplicate)
|
|
const result = protector.checkMessage(2, current_time + 1, 10, peer_id, 0);
|
|
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
|
}
|
|
|
|
test "ReplayProtector: large sequence jump resets window" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{8} ** 32;
|
|
|
|
// Send sequence 10
|
|
try protector.checkMessage(1, current_time, 10, peer_id, 0);
|
|
|
|
// Large jump to 1000 (> WINDOW_SIZE=64)
|
|
try protector.checkMessage(2, current_time + 1, 1000, peer_id, 0);
|
|
|
|
// Sequence 5 is now way too old (1000 - 5 = 995 > 64)
|
|
const result = protector.checkMessage(3, current_time + 2, 5, peer_id, 0);
|
|
try testing.expectError(ReplayError.SequenceTooOld, result);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Cleanup Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: forceCleanup removes expired entries" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
protector.configure(null, 1, null); // 1 second TTL
|
|
|
|
const base_time = std.time.timestamp();
|
|
const peer_id = [_]u8{9} ** 32;
|
|
|
|
// Add messages
|
|
try protector.checkMessage(1, base_time, 0, peer_id, 0);
|
|
try protector.checkMessage(2, base_time, 0, peer_id, 0);
|
|
|
|
var stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
|
|
// Wait briefly and force cleanup
|
|
std.Thread.sleep(2 * std.time.ns_per_s);
|
|
protector.forceCleanup();
|
|
|
|
stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: cleanup preserves recent entries" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{10} ** 32;
|
|
|
|
// Add messages with current timestamp
|
|
try protector.checkMessage(1, current_time, 1, peer_id, 0);
|
|
try protector.checkMessage(2, current_time, 2, peer_id, 0);
|
|
|
|
var stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_peers);
|
|
|
|
// Cleanup with current time should preserve recent entries
|
|
protector.cleanup(current_time);
|
|
|
|
stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_peers);
|
|
}
|
|
|
|
test "ReplayProtector: peer cleanup after 24 hours" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const base_time = std.time.timestamp();
|
|
const peer_id = [_]u8{11} ** 32;
|
|
|
|
// Add a peer
|
|
try protector.checkMessage(1, base_time, 1, peer_id, 0);
|
|
|
|
var stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_peers);
|
|
|
|
// Cleanup with time 25 hours in future should remove the peer
|
|
const future_time = base_time + (25 * 3600);
|
|
protector.cleanup(future_time);
|
|
|
|
stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_peers);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Reset Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: resetPeerSequence for unknown peer" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const unknown_peer = [_]u8{12} ** 32;
|
|
|
|
// Reset for unknown peer should not error
|
|
try protector.resetPeerSequence(unknown_peer);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 0), stats.tracked_peers);
|
|
}
|
|
|
|
test "ReplayProtector: resetPeerSequence allows restarting sequence" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{13} ** 32;
|
|
|
|
// Build up sequence to 100 (so that 1 is outside WINDOW_SIZE of 64)
|
|
try protector.checkMessage(1, current_time, 100, peer_id, 0);
|
|
|
|
// Sequence 1 would normally be too old (100 - 1 = 99 > 64)
|
|
const result = protector.checkMessage(2, current_time + 1, 1, peer_id, 0);
|
|
try testing.expectError(ReplayError.SequenceTooOld, result);
|
|
|
|
// Reset the peer
|
|
try protector.resetPeerSequence(peer_id);
|
|
|
|
// Now sequence 1 should work
|
|
try protector.checkMessage(3, current_time + 2, 1, peer_id, 0);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Multiple Peer Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: independent sequence tracking per peer" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_a = [_]u8{0xAA} ** 32;
|
|
const peer_b = [_]u8{0xBB} ** 32;
|
|
|
|
// Peer A at sequence 100
|
|
try protector.checkMessage(1, current_time, 100, peer_a, 0);
|
|
|
|
// Peer B can still use sequence 1
|
|
try protector.checkMessage(2, current_time + 1, 1, peer_b, 0);
|
|
|
|
// Peer A can advance to 101
|
|
try protector.checkMessage(3, current_time + 2, 101, peer_a, 0);
|
|
|
|
// Peer B can advance to 2
|
|
try protector.checkMessage(4, current_time + 3, 2, peer_b, 0);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
|
|
}
|
|
|
|
test "ReplayProtector: same message ID different peers" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_a = [_]u8{0xCC} ** 32;
|
|
const peer_b = [_]u8{0xDD} ** 32;
|
|
|
|
// Peer A sends message ID 42
|
|
try protector.checkMessage(42, current_time, 1, peer_a, 0);
|
|
|
|
// Peer B sends same message ID — allowed because replay protection is per-peer.
|
|
// Each peer chooses IDs independently, so collisions between peers are expected.
|
|
try protector.checkMessage(42, current_time + 1, 1, peer_b, 0);
|
|
|
|
// But Peer A replaying its own message ID should still be rejected
|
|
const result = protector.checkMessage(42, current_time + 2, 2, peer_a, 0);
|
|
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Stats Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: stats reflect correct counts" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
|
|
// Add messages from 3 different peers
|
|
for (0..3) |i| {
|
|
var peer_id: [32]u8 = undefined;
|
|
@memset(&peer_id, @as(u8, @intCast(i + 1)));
|
|
|
|
const msg_id: u64 = @as(u64, @intCast(i)) * 100;
|
|
|
|
try protector.checkMessage(msg_id, current_time, @as(u64, @intCast(i + 1)), peer_id, 0);
|
|
}
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 3), stats.tracked_messages);
|
|
try testing.expectEqual(@as(usize, 3), stats.tracked_peers);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Error Type Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayError: all error types exist" {
|
|
// Verify error types can be used in switch statements
|
|
const errors = [_]ReplayError{
|
|
ReplayError.TimestampTooOld,
|
|
ReplayError.TimestampTooFarInFuture,
|
|
ReplayError.DuplicateMessageId,
|
|
ReplayError.InvalidSequence,
|
|
ReplayError.SequenceTooOld,
|
|
};
|
|
try testing.expectEqual(@as(usize, 5), errors.len);
|
|
}
|
|
|
|
test "MemoryError: all error types exist" {
|
|
const errors = [_]MemoryError{
|
|
MemoryError.TooManyMessages,
|
|
MemoryError.TooManyPeers,
|
|
};
|
|
try testing.expectEqual(@as(usize, 2), errors.len);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Boundary and Edge Cases
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: message with zero msg_id" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{17} ** 32;
|
|
|
|
// msg_id 0 should work
|
|
try protector.checkMessage(0, current_time, 0, peer_id, 0);
|
|
|
|
// Duplicate msg_id 0 from same peer should fail
|
|
const result = protector.checkMessage(0, current_time + 1, 0, peer_id, 0);
|
|
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
|
}
|
|
|
|
test "ReplayProtector: message with max u64 values" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{18} ** 32;
|
|
|
|
// Max u64 message ID
|
|
try protector.checkMessage(std.math.maxInt(u64), current_time, 1, peer_id, 0);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: rapid message processing" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{19} ** 32;
|
|
|
|
// Process 100 messages rapidly
|
|
for (0..100) |i| {
|
|
const msg_id: u64 = 19000 + @as(u64, @intCast(i));
|
|
const seq: u64 = @as(u64, @intCast(i + 1));
|
|
|
|
try protector.checkMessage(msg_id, current_time, seq, peer_id, 0);
|
|
}
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 100), stats.tracked_messages);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Cleanup Interval Tests
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: periodic cleanup triggered by checkMessage" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
// Set very short cleanup interval and TTL
|
|
protector.configure(null, 1, 1);
|
|
|
|
const base_time = std.time.timestamp();
|
|
const peer_id = [_]u8{20} ** 32;
|
|
|
|
// Add initial message
|
|
try protector.checkMessage(1, base_time, 0, peer_id, 0);
|
|
|
|
var stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
|
|
|
// Wait for cleanup interval to pass
|
|
std.Thread.sleep(2 * std.time.ns_per_s);
|
|
|
|
// New message should trigger periodic cleanup
|
|
try protector.checkMessage(2, std.time.timestamp(), 0, peer_id, 0);
|
|
|
|
stats = protector.getStats();
|
|
// Old message should be cleaned up, only new one remains
|
|
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Wire Nonce / Cross-Peer Tests (replay protection fix)
|
|
// ============================================================================
|
|
|
|
test "ReplayProtector: same request_id from two peers with different wire_nonce — both accepted" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_a = [_]u8{0xA1} ** 32;
|
|
const peer_b = [_]u8{0xB2} ** 32;
|
|
|
|
// Both peers send request_id=1, but with different random wire nonces
|
|
// (wire nonce is used to derive unique msg_id per session).
|
|
// Per-peer dedup means same msg_id from different peers is fine.
|
|
try protector.checkMessage(1, current_time, 0, peer_a, 0x01);
|
|
try protector.checkMessage(1, current_time, 0, peer_b, 0x01);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: same msg_id same peer different msg_type — both accepted" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{0xC3} ** 32;
|
|
|
|
// Same msg_id but different msg_type — per-peer dedup key includes msg_type
|
|
try protector.checkMessage(100, current_time, 0, peer_id, 0x01);
|
|
try protector.checkMessage(100, current_time + 1, 0, peer_id, 0x02);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
}
|
|
|
|
test "ReplayProtector: DHT message with stale wire_timestamp — rejected by validateTimestamp" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const peer_id = [_]u8{0xE5} ** 32;
|
|
|
|
// Simulate a DHT message with a very old wire_timestamp
|
|
const stale_timestamp = std.time.timestamp() - 3600; // 1 hour ago
|
|
const result = protector.checkMessage(300, stale_timestamp, 0, peer_id, 0x01);
|
|
try testing.expectError(ReplayError.TimestampTooOld, result);
|
|
}
|
|
|
|
test "ReplayProtector: same request_id different msg_type — both accepted (per-type tracking)" {
|
|
var protector = ReplayProtector.init(testing.allocator);
|
|
defer protector.deinit();
|
|
|
|
const current_time = std.time.timestamp();
|
|
const peer_id = [_]u8{0xF6} ** 32;
|
|
|
|
// Request/response pair sharing the same request_id but different msg_type
|
|
// msg_type 0x01 = find_node request, 0x11 = find_node response
|
|
try protector.checkMessage(42, current_time, 0, peer_id, 0x01);
|
|
try protector.checkMessage(42, current_time, 0, peer_id, 0x11);
|
|
|
|
const stats = protector.getStats();
|
|
try testing.expectEqual(@as(usize, 2), stats.tracked_messages);
|
|
}
|