//! 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_nonces); 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); try testing.expectEqual(@as(usize, 600_000), ReplayProtector.MAX_TRACKED_NONCES); } // ============================================================================ // 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, 1000, 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, 1001, 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, 1000, 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, 1000, 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, 1000, 0, peer_id, 0); try testing.expectError(ReplayError.TimestampTooFarInFuture, result); } // ============================================================================ // Nonce Edge Cases (isDuplicateNonce) // ============================================================================ test "ReplayProtector: nonce with very future timestamp rejected" { var protector = ReplayProtector.init(testing.allocator); defer protector.deinit(); const current_time = std.time.timestamp(); const peer_id = [_]u8{2} ** 32; // First, send a message with a timestamp far in the future (> 60s) // This tests the isDuplicateNonce future timestamp rejection path const far_future = current_time + 120; // 2 minutes in future // This will be rejected by validateTimestamp first (TimestampTooFarInFuture) const result = protector.checkMessage(1, far_future, 2000, 0, peer_id, 0); try testing.expectError(ReplayError.TimestampTooFarInFuture, result); } test "ReplayProtector: nonce 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 nonce 3000 try protector.checkMessage(1, base_time, 3000, 0, peer_id, 0); // Force cleanup with future time to expire the nonce protector.cleanup(base_time + 10); // Now nonce 3000 should be allowed again (with new message ID and timestamp) // Note: we need a valid timestamp relative to real current time const new_time = std.time.timestamp(); try protector.checkMessage(2, new_time, 3000, 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, 4000, 0, peer_id, 0); try protector.checkMessage(2, current_time + 1, 4001, 0, peer_id, 0); try protector.checkMessage(3, current_time + 2, 4002, 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, 5000, 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, 5001, 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, 5002, 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, 6000, 1, peer_id, 0); try protector.checkMessage(2, current_time + 1, 6001, 5, peer_id, 0); try protector.checkMessage(3, current_time + 2, 6002, 3, peer_id, 0); // Replay sequence 3 should fail (InvalidSequence - already in window) const result = protector.checkMessage(4, current_time + 3, 6003, 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, 7000, 10, peer_id, 0); // Send sequence 10 again (duplicate) const result = protector.checkMessage(2, current_time + 1, 7001, 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, 8000, 10, peer_id, 0); // Large jump to 1000 (> WINDOW_SIZE=64) try protector.checkMessage(2, current_time + 1, 8001, 1000, peer_id, 0); // Sequence 5 is now way too old (1000 - 5 = 995 > 64) const result = protector.checkMessage(3, current_time + 2, 8002, 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, 9000, 0, peer_id, 0); try protector.checkMessage(2, base_time, 9001, 0, peer_id, 0); var stats = protector.getStats(); try testing.expectEqual(@as(usize, 2), stats.tracked_messages); try testing.expectEqual(@as(usize, 2), stats.tracked_nonces); // 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); try testing.expectEqual(@as(usize, 0), stats.tracked_nonces); } 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, 10000, 1, peer_id, 0); try protector.checkMessage(2, current_time, 10001, 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, 11000, 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, 13000, 100, peer_id, 0); // Sequence 1 would normally be too old (100 - 1 = 99 > 64) const result = protector.checkMessage(2, current_time + 1, 13001, 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, 13002, 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, 14000, 100, peer_a, 0); // Peer B can still use sequence 1 try protector.checkMessage(2, current_time + 1, 14001, 1, peer_b, 0); // Peer A can advance to 101 try protector.checkMessage(3, current_time + 2, 14002, 101, peer_a, 0); // Peer B can advance to 2 try protector.checkMessage(4, current_time + 3, 14003, 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, 15000, 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, 15001, 1, peer_b, 0); // But Peer A replaying its own message ID should still be rejected const result = protector.checkMessage(42, current_time + 2, 15002, 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; const nonce: u64 = 16000 + @as(u64, @intCast(i)); try protector.checkMessage(msg_id, current_time, nonce, @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_nonces); 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.DuplicateNonce, ReplayError.SequenceTooOld, }; try testing.expectEqual(@as(usize, 6), errors.len); } test "MemoryError: all error types exist" { const errors = [_]MemoryError{ MemoryError.TooManyMessages, MemoryError.TooManyPeers, MemoryError.TooManyNonces, }; try testing.expectEqual(@as(usize, 3), errors.len); } // ============================================================================ // Boundary and Edge Cases // ============================================================================ test "ReplayProtector: message with zero nonce" { var protector = ReplayProtector.init(testing.allocator); defer protector.deinit(); const current_time = std.time.timestamp(); const peer_id = [_]u8{17} ** 32; // Nonce 0 should work try protector.checkMessage(1, current_time, 0, 0, peer_id, 0); // Duplicate nonce 0 should fail const result = protector.checkMessage(2, current_time + 1, 0, 0, peer_id, 0); try testing.expectError(ReplayError.DuplicateNonce, 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 and nonce try protector.checkMessage(std.math.maxInt(u64), current_time, std.math.maxInt(u64) - 1, 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 nonce: u64 = 190000 + @as(u64, @intCast(i)); const seq: u64 = @as(u64, @intCast(i + 1)); try protector.checkMessage(msg_id, current_time, nonce, seq, peer_id, 0); } const stats = protector.getStats(); try testing.expectEqual(@as(usize, 100), stats.tracked_messages); try testing.expectEqual(@as(usize, 100), stats.tracked_nonces); } // ============================================================================ // 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, 20000, 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(), 20001, 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); }