fix protocol+tests: replay protection rework, wire envelope nonce auth,
flaky e2e test deadlines, build imports
This commit is contained in:
@@ -28,7 +28,6 @@ test "ReplayProtector: init with default values" {
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -36,7 +35,6 @@ 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);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -92,11 +90,11 @@ test "ReplayProtector: timestamp at exact TTL boundary" {
|
||||
// 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);
|
||||
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, 1001, 0, peer_id, 0);
|
||||
const result = protector.checkMessage(2, past_boundary, 0, peer_id, 0);
|
||||
try testing.expectError(ReplayError.TimestampTooOld, result);
|
||||
}
|
||||
|
||||
@@ -109,7 +107,7 @@ test "ReplayProtector: timestamp just within TTL" {
|
||||
|
||||
// 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);
|
||||
try protector.checkMessage(1, valid_timestamp, 0, peer_id, 0);
|
||||
|
||||
const stats = protector.getStats();
|
||||
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
||||
@@ -125,7 +123,7 @@ test "ReplayProtector: timestamp at exact clock skew boundary" {
|
||||
// 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);
|
||||
try protector.checkMessage(1, boundary_timestamp, 0, peer_id, 0);
|
||||
|
||||
const stats = protector.getStats();
|
||||
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
||||
@@ -140,31 +138,48 @@ test "ReplayProtector: timestamp just beyond clock skew" {
|
||||
|
||||
// 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);
|
||||
const result = protector.checkMessage(1, future_timestamp, 0, peer_id, 0);
|
||||
try testing.expectError(ReplayError.TimestampTooFarInFuture, result);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Nonce Edge Cases (isDuplicateNonce)
|
||||
// Per-Peer Message ID Dedup Edge Cases
|
||||
// ============================================================================
|
||||
|
||||
test "ReplayProtector: nonce with very future timestamp rejected" {
|
||||
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, 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
|
||||
// First message accepted
|
||||
try protector.checkMessage(42, current_time, 0, peer_id, 0);
|
||||
|
||||
// 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);
|
||||
// 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: nonce reuse after TTL expiry allowed" {
|
||||
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();
|
||||
|
||||
@@ -174,16 +189,15 @@ test "ReplayProtector: nonce reuse after TTL expiry allowed" {
|
||||
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);
|
||||
// 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 nonce
|
||||
// Force cleanup with future time to expire the entry
|
||||
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
|
||||
// Now msg_id 1 should be allowed again (with new timestamp)
|
||||
const new_time = std.time.timestamp();
|
||||
try protector.checkMessage(2, new_time, 3000, 0, peer_id, 0);
|
||||
try protector.checkMessage(1, new_time, 0, peer_id, 0);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -198,9 +212,9 @@ test "ReplayProtector: sequence 0 is always accepted" {
|
||||
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);
|
||||
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();
|
||||
@@ -215,13 +229,13 @@ test "ReplayProtector: sequence at window boundary" {
|
||||
const peer_id = [_]u8{5} ** 32;
|
||||
|
||||
// Send sequence 100
|
||||
try protector.checkMessage(1, current_time, 5000, 100, peer_id, 0);
|
||||
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, 5001, 37, peer_id, 0);
|
||||
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, 5002, 36, peer_id, 0);
|
||||
const result = protector.checkMessage(3, current_time + 2, 36, peer_id, 0);
|
||||
try testing.expectError(ReplayError.SequenceTooOld, result);
|
||||
}
|
||||
|
||||
@@ -233,12 +247,12 @@ test "ReplayProtector: sequence replay in sliding window" {
|
||||
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);
|
||||
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, 6003, 3, peer_id, 0);
|
||||
const result = protector.checkMessage(4, current_time + 3, 3, peer_id, 0);
|
||||
try testing.expectError(ReplayError.InvalidSequence, result);
|
||||
}
|
||||
|
||||
@@ -250,10 +264,10 @@ test "ReplayProtector: sequence exactly at last_sequence" {
|
||||
const peer_id = [_]u8{7} ** 32;
|
||||
|
||||
// Send sequence 10
|
||||
try protector.checkMessage(1, current_time, 7000, 10, peer_id, 0);
|
||||
try protector.checkMessage(1, current_time, 10, peer_id, 0);
|
||||
|
||||
// Send sequence 10 again (duplicate)
|
||||
const result = protector.checkMessage(2, current_time + 1, 7001, 10, peer_id, 0);
|
||||
const result = protector.checkMessage(2, current_time + 1, 10, peer_id, 0);
|
||||
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
||||
}
|
||||
|
||||
@@ -265,13 +279,13 @@ test "ReplayProtector: large sequence jump resets window" {
|
||||
const peer_id = [_]u8{8} ** 32;
|
||||
|
||||
// Send sequence 10
|
||||
try protector.checkMessage(1, current_time, 8000, 10, peer_id, 0);
|
||||
try protector.checkMessage(1, current_time, 10, peer_id, 0);
|
||||
|
||||
// Large jump to 1000 (> WINDOW_SIZE=64)
|
||||
try protector.checkMessage(2, current_time + 1, 8001, 1000, peer_id, 0);
|
||||
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, 8002, 5, peer_id, 0);
|
||||
const result = protector.checkMessage(3, current_time + 2, 5, peer_id, 0);
|
||||
try testing.expectError(ReplayError.SequenceTooOld, result);
|
||||
}
|
||||
|
||||
@@ -289,12 +303,11 @@ test "ReplayProtector: forceCleanup removes expired entries" {
|
||||
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);
|
||||
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);
|
||||
try testing.expectEqual(@as(usize, 2), stats.tracked_nonces);
|
||||
|
||||
// Wait briefly and force cleanup
|
||||
std.Thread.sleep(2 * std.time.ns_per_s);
|
||||
@@ -302,7 +315,6 @@ test "ReplayProtector: forceCleanup removes expired entries" {
|
||||
|
||||
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" {
|
||||
@@ -313,8 +325,8 @@ test "ReplayProtector: cleanup preserves recent entries" {
|
||||
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);
|
||||
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);
|
||||
@@ -336,7 +348,7 @@ test "ReplayProtector: peer cleanup after 24 hours" {
|
||||
const peer_id = [_]u8{11} ** 32;
|
||||
|
||||
// Add a peer
|
||||
try protector.checkMessage(1, base_time, 11000, 1, peer_id, 0);
|
||||
try protector.checkMessage(1, base_time, 1, peer_id, 0);
|
||||
|
||||
var stats = protector.getStats();
|
||||
try testing.expectEqual(@as(usize, 1), stats.tracked_peers);
|
||||
@@ -374,17 +386,17 @@ test "ReplayProtector: resetPeerSequence allows restarting sequence" {
|
||||
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);
|
||||
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, 13001, 1, peer_id, 0);
|
||||
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, 13002, 1, peer_id, 0);
|
||||
try protector.checkMessage(3, current_time + 2, 1, peer_id, 0);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -400,16 +412,16 @@ test "ReplayProtector: independent sequence tracking per peer" {
|
||||
const peer_b = [_]u8{0xBB} ** 32;
|
||||
|
||||
// Peer A at sequence 100
|
||||
try protector.checkMessage(1, current_time, 14000, 100, peer_a, 0);
|
||||
try protector.checkMessage(1, current_time, 100, peer_a, 0);
|
||||
|
||||
// Peer B can still use sequence 1
|
||||
try protector.checkMessage(2, current_time + 1, 14001, 1, peer_b, 0);
|
||||
try protector.checkMessage(2, current_time + 1, 1, peer_b, 0);
|
||||
|
||||
// Peer A can advance to 101
|
||||
try protector.checkMessage(3, current_time + 2, 14002, 101, peer_a, 0);
|
||||
try protector.checkMessage(3, current_time + 2, 101, peer_a, 0);
|
||||
|
||||
// Peer B can advance to 2
|
||||
try protector.checkMessage(4, current_time + 3, 14003, 2, peer_b, 0);
|
||||
try protector.checkMessage(4, current_time + 3, 2, peer_b, 0);
|
||||
|
||||
const stats = protector.getStats();
|
||||
try testing.expectEqual(@as(usize, 2), stats.tracked_peers);
|
||||
@@ -424,14 +436,14 @@ test "ReplayProtector: same message ID different peers" {
|
||||
const peer_b = [_]u8{0xDD} ** 32;
|
||||
|
||||
// Peer A sends message ID 42
|
||||
try protector.checkMessage(42, current_time, 15000, 1, peer_a, 0);
|
||||
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, 15001, 1, peer_b, 0);
|
||||
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, 15002, 2, peer_a, 0);
|
||||
const result = protector.checkMessage(42, current_time + 2, 2, peer_a, 0);
|
||||
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
||||
}
|
||||
|
||||
@@ -451,14 +463,12 @@ test "ReplayProtector: stats reflect correct counts" {
|
||||
@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);
|
||||
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_nonces);
|
||||
try testing.expectEqual(@as(usize, 3), stats.tracked_peers);
|
||||
}
|
||||
|
||||
@@ -473,38 +483,36 @@ test "ReplayError: all error types exist" {
|
||||
ReplayError.TimestampTooFarInFuture,
|
||||
ReplayError.DuplicateMessageId,
|
||||
ReplayError.InvalidSequence,
|
||||
ReplayError.DuplicateNonce,
|
||||
ReplayError.SequenceTooOld,
|
||||
};
|
||||
try testing.expectEqual(@as(usize, 6), errors.len);
|
||||
try testing.expectEqual(@as(usize, 5), 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);
|
||||
try testing.expectEqual(@as(usize, 2), errors.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Boundary and Edge Cases
|
||||
// ============================================================================
|
||||
|
||||
test "ReplayProtector: message with zero nonce" {
|
||||
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;
|
||||
|
||||
// Nonce 0 should work
|
||||
try protector.checkMessage(1, current_time, 0, 0, peer_id, 0);
|
||||
// msg_id 0 should work
|
||||
try protector.checkMessage(0, current_time, 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);
|
||||
// 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" {
|
||||
@@ -514,8 +522,8 @@ test "ReplayProtector: message with max u64 values" {
|
||||
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);
|
||||
// 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);
|
||||
@@ -531,15 +539,13 @@ test "ReplayProtector: rapid message processing" {
|
||||
// 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);
|
||||
try protector.checkMessage(msg_id, current_time, 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);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -557,7 +563,7 @@ test "ReplayProtector: periodic cleanup triggered by checkMessage" {
|
||||
const peer_id = [_]u8{20} ** 32;
|
||||
|
||||
// Add initial message
|
||||
try protector.checkMessage(1, base_time, 20000, 0, peer_id, 0);
|
||||
try protector.checkMessage(1, base_time, 0, peer_id, 0);
|
||||
|
||||
var stats = protector.getStats();
|
||||
try testing.expectEqual(@as(usize, 1), stats.tracked_messages);
|
||||
@@ -566,9 +572,74 @@ test "ReplayProtector: periodic cleanup triggered by checkMessage" {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user