fix protocol+tests: replay protection rework, wire envelope nonce auth,

flaky e2e test deadlines, build imports
This commit is contained in:
2026-02-13 16:37:30 -03:00
parent c94d189d42
commit 6ec1346cfd
22 changed files with 2275 additions and 786 deletions

View File

@@ -20,11 +20,13 @@ test "real e2e: two peers can exchange messages" {
// Create two P2P instances on different ports
var config1 = P2PConfig.default();
config1.network.listen_port = 19001;
config1.network.enable_noise_encryption = false;
config1.network.idle_timeout_ms = 120000; // 2 minutes for test
config1.network.health_check_interval_ms = 30000;
var config2 = P2PConfig.default();
config2.network.listen_port = 19002;
config2.network.enable_noise_encryption = false;
config2.network.idle_timeout_ms = 120000;
config2.network.health_check_interval_ms = 30000;
@@ -48,7 +50,7 @@ test "real e2e: two peers can exchange messages" {
try peer1.startListening(listen_addr);
// Give peer1 time to start listening
std.time.sleep(100 * std.time.ns_per_ms);
std.Thread.sleep(100 * std.time.ns_per_ms);
// Peer2 connects to Peer1
var connect_addr = Address.init();
@@ -62,16 +64,17 @@ test "real e2e: two peers can exchange messages" {
// Wait for connection to be established
var connected = false;
for (0..100) |_| {
const connect_deadline = std.time.milliTimestamp() + 3000;
while (std.time.milliTimestamp() < connect_deadline) {
try peer1.update();
try peer2.update();
const stats1 = peer1.getStats();
if (stats1.active_connections > 0) {
if (stats1.active_connections.load(.monotonic) > 0) {
connected = true;
break;
}
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
if (!connected) {
@@ -88,10 +91,11 @@ test "real e2e: two peers can exchange messages" {
};
// Update both peers to process the message
for (0..50) |_| {
const msg_deadline = std.time.milliTimestamp() + 2000;
while (std.time.milliTimestamp() < msg_deadline) {
try peer1.update();
try peer2.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
// Check if peer1 received the message
@@ -128,9 +132,11 @@ test "real e2e: peer connection and disconnection lifecycle" {
var config1 = P2PConfig.default();
config1.network.listen_port = 19011;
config1.network.enable_noise_encryption = false;
var config2 = P2PConfig.default();
config2.network.listen_port = 19012;
config2.network.enable_noise_encryption = false;
var peer1 = try P2P.init(allocator, config1);
defer peer1.deinit();
@@ -151,7 +157,7 @@ test "real e2e: peer connection and disconnection lifecycle" {
listen_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
try peer1.startListening(listen_addr);
std.time.sleep(100 * std.time.ns_per_ms);
std.Thread.sleep(100 * std.time.ns_per_ms);
// Connect
var connect_addr = Address.init();
@@ -165,15 +171,16 @@ test "real e2e: peer connection and disconnection lifecycle" {
// Wait for connection
var connected = false;
for (0..100) |_| {
const connect_deadline = std.time.milliTimestamp() + 3000;
while (std.time.milliTimestamp() < connect_deadline) {
try peer1.update();
try peer2.update();
if (peer1.getStats().active_connections > 0) {
if (peer1.getStats().active_connections.load(.monotonic) > 0) {
connected = true;
break;
}
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
if (!connected) {
@@ -181,22 +188,23 @@ test "real e2e: peer connection and disconnection lifecycle" {
return error.SkipZigTest;
}
const initial_connections = peer2.getStats().active_connections;
const initial_connections = peer2.getStats().active_connections.load(.monotonic);
try testing.expect(initial_connections > 0);
// Disconnect
try peer2.disconnectPeer(peer_id);
// Update to process disconnect
for (0..50) |_| {
const disconnect_deadline = std.time.milliTimestamp() + 2000;
while (std.time.milliTimestamp() < disconnect_deadline) {
try peer1.update();
try peer2.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
// Verify disconnection
const final_stats = peer2.getStats();
try testing.expect(final_stats.active_connections < initial_connections);
try testing.expect(final_stats.active_connections.load(.monotonic) < initial_connections);
}
test "real e2e: multiple sequential connections" {
@@ -204,6 +212,7 @@ test "real e2e: multiple sequential connections" {
var config_server = P2PConfig.default();
config_server.network.listen_port = 19021;
config_server.network.enable_noise_encryption = false;
config_server.network.max_connections = 10;
var server = try P2P.init(allocator, config_server);
@@ -217,7 +226,7 @@ test "real e2e: multiple sequential connections" {
listen_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
try server.startListening(listen_addr);
std.time.sleep(100 * std.time.ns_per_ms);
std.Thread.sleep(100 * std.time.ns_per_ms);
// Create 3 client peers that connect sequentially
const num_clients = 3;
@@ -228,6 +237,7 @@ test "real e2e: multiple sequential connections" {
for (&clients, 0..) |*client, i| {
var config = P2PConfig.default();
config.network.listen_port = @intCast(19022 + i);
config.network.enable_noise_encryption = false;
client.* = try P2P.init(allocator, config);
try client.start();
_ = client.processAllEvents();
@@ -250,27 +260,29 @@ test "real e2e: multiple sequential connections" {
};
// Wait for connection
for (0..50) |_| {
const client_deadline = std.time.milliTimestamp() + 2000;
while (std.time.milliTimestamp() < client_deadline) {
try server.update();
try client.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}
// Update all to process connections
for (0..100) |_| {
const all_deadline = std.time.milliTimestamp() + 3000;
while (std.time.milliTimestamp() < all_deadline) {
try server.update();
for (&clients) |*client| {
try client.update();
}
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
// Verify server has multiple connections (at least some should succeed)
const stats = server.getStats();
std.debug.print("Server has {} active connections\n", .{stats.active_connections});
std.debug.print("Server has {} active connections\n", .{stats.active_connections.load(.monotonic)});
// In test environment, at least verify we can attempt multiple connections
try testing.expect(stats.total_connections >= 1);
try testing.expect(stats.total_connections.load(.monotonic) >= 1);
}
test "real e2e: bidirectional message exchange" {
@@ -278,9 +290,11 @@ test "real e2e: bidirectional message exchange" {
var config1 = P2PConfig.default();
config1.network.listen_port = 19031;
config1.network.enable_noise_encryption = false;
var config2 = P2PConfig.default();
config2.network.listen_port = 19032;
config2.network.enable_noise_encryption = false;
var peer1 = try P2P.init(allocator, config1);
defer peer1.deinit();
@@ -304,7 +318,7 @@ test "real e2e: bidirectional message exchange" {
listen_addr2.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
try peer2.startListening(listen_addr2);
std.time.sleep(100 * std.time.ns_per_ms);
std.Thread.sleep(100 * std.time.ns_per_ms);
// Peer2 connects to peer1
var connect_addr = Address.init();
@@ -317,31 +331,33 @@ test "real e2e: bidirectional message exchange" {
};
// Wait for connection
for (0..100) |_| {
const connect_deadline = std.time.milliTimestamp() + 3000;
while (std.time.milliTimestamp() < connect_deadline) {
try peer1.update();
try peer2.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
// Send messages both ways
_ = peer2.sendMessage(peer1_id, 0x01, "Hello from peer2") catch {};
// Process messages
for (0..100) |_| {
const msg_deadline = std.time.milliTimestamp() + 2000;
while (std.time.milliTimestamp() < msg_deadline) {
try peer1.update();
try peer2.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
// Check stats show some activity
const stats1 = peer1.getStats();
const stats2 = peer2.getStats();
std.debug.print("Peer1: {} sent, {} received\n", .{ stats1.messages_sent, stats1.messages_received });
std.debug.print("Peer2: {} sent, {} received\n", .{ stats2.messages_sent, stats2.messages_received });
std.debug.print("Peer1: {} sent, {} received\n", .{ stats1.messages_sent.load(.monotonic), stats1.messages_received.load(.monotonic) });
std.debug.print("Peer2: {} sent, {} received\n", .{ stats2.messages_sent.load(.monotonic), stats2.messages_received.load(.monotonic) });
// Verify messaging was attempted (actual delivery may not complete in test environment)
try testing.expect(stats2.messages_sent > 0 or stats1.messages_received >= 0);
try testing.expect(stats2.messages_sent.load(.monotonic) > 0 or stats1.messages_received.load(.monotonic) >= 0);
}
test "real e2e: connection timeout handling" {
@@ -349,6 +365,7 @@ test "real e2e: connection timeout handling" {
var config = P2PConfig.default();
config.network.listen_port = 19041;
config.network.enable_noise_encryption = false;
config.network.connection_timeout = 1000; // 1 second timeout
var peer = try P2P.init(allocator, config);
@@ -374,7 +391,8 @@ test "real e2e: connection timeout handling" {
std.debug.print("Connection correctly failed: {}\n", .{err});
try testing.expect(err == core.Error.ConnectionRefused or
err == core.Error.ConnectionTimeout or
err == core.Error.SocketCreationFailed);
err == core.Error.SocketCreationFailed or
err == core.Error.SocketConnectFailed);
}
}
@@ -383,6 +401,7 @@ test "real e2e: stats tracking accuracy" {
var config = P2PConfig.default();
config.network.listen_port = 19051;
config.network.enable_noise_encryption = false;
config.library.enable_statistics = true;
var peer = try P2P.init(allocator, config);
@@ -393,14 +412,14 @@ test "real e2e: stats tracking accuracy" {
const initial_stats = peer.getStats();
try testing.expectEqual(p2p_lib.P2PState.Running, initial_stats.state);
try testing.expectEqual(@as(u32, 0), initial_stats.active_connections);
try testing.expectEqual(@as(u64, 0), initial_stats.messages_sent);
try testing.expectEqual(@as(u64, 0), initial_stats.messages_received);
try testing.expectEqual(@as(u32, 0), initial_stats.active_connections.load(.monotonic));
try testing.expectEqual(@as(u64, 0), initial_stats.messages_sent.load(.monotonic));
try testing.expectEqual(@as(u64, 0), initial_stats.messages_received.load(.monotonic));
// Run for a bit
for (0..10) |_| {
try peer.update();
std.time.sleep(10 * std.time.ns_per_ms);
std.Thread.sleep(10 * std.time.ns_per_ms);
}
const updated_stats = peer.getStats();