create Mutual Witness Protocol specs and implementation. #1
@@ -238,9 +238,6 @@ test "file writer initialization" {
|
||||
var writer = try FileWriter.init(test_path, allocator);
|
||||
defer writer.deinit();
|
||||
|
||||
// File should be created and ready for writing
|
||||
try testing.expect(true); // If we get here, test passed
|
||||
|
||||
// Clean up
|
||||
fs.cwd().deleteFile(test_path) catch return;
|
||||
}
|
||||
|
||||
@@ -663,9 +663,6 @@ test "memory cleanup and resource management" {
|
||||
// Manager will be cleaned up when it goes out of scope
|
||||
}
|
||||
|
||||
// If we get here without memory issues, cleanup is working
|
||||
try testing.expect(true);
|
||||
|
||||
// Clean up any remaining test files
|
||||
for (0..5) |i| {
|
||||
for (0..3) |j| {
|
||||
|
||||
@@ -840,9 +840,6 @@ test "DiscoveryManager: pruneStalePeers removes old peers" {
|
||||
|
||||
// Call prune (even with no peers)
|
||||
_ = manager.pruneStalePeers();
|
||||
|
||||
// Should not crash
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "DiscoveryManager: getDiscoveredPeers returns copy" {
|
||||
@@ -869,9 +866,6 @@ test "DiscoveryManager: cleanup respects max_peers" {
|
||||
try manager.start();
|
||||
|
||||
manager.cleanup();
|
||||
|
||||
// Should not crash
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "DiscoveryManager: maintain sends discovery request" {
|
||||
@@ -919,9 +913,6 @@ test "DiscoveryManager: sendGoodbye notifies peers" {
|
||||
|
||||
const local_peer_id = PeerId{ .bytes = [_]u8{0x55} ** 32 };
|
||||
try manager.sendGoodbye(local_peer_id, .shutdown);
|
||||
|
||||
// Even with no discovered peers, shouldn't crash
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "DiscoveryManager: broadcastKeepAlive to all peers" {
|
||||
@@ -934,9 +925,6 @@ test "DiscoveryManager: broadcastKeepAlive to all peers" {
|
||||
|
||||
const local_peer_id = PeerId{ .bytes = [_]u8{0x66} ** 32 };
|
||||
try manager.broadcastKeepAlive(local_peer_id);
|
||||
|
||||
// With no discovered peers, no packets sent but shouldn't crash
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "DiscoveryManager: multiple messages in sequence" {
|
||||
@@ -1043,8 +1031,6 @@ test "DiscoveryManager: handle truncated message gracefully" {
|
||||
|
||||
// Should handle gracefully without crashing
|
||||
_ = manager.listen(100) catch false;
|
||||
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "DiscoveryManager: handle invalid message type" {
|
||||
@@ -1065,6 +1051,4 @@ test "DiscoveryManager: handle invalid message type" {
|
||||
|
||||
// Should handle gracefully
|
||||
_ = manager.listen(100) catch false;
|
||||
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
@@ -260,16 +260,3 @@ test "broadcast result structure" {
|
||||
try testing.expectEqual(@as(usize, 7), result.total_peers);
|
||||
}
|
||||
|
||||
test "no silent error swallowing in critical paths" {
|
||||
// This is a meta-test that documents the fixes made
|
||||
|
||||
// 1. Router cleanup now pre-allocates and logs failures
|
||||
// 2. Replay protection cleanup now pre-allocates and logs failures
|
||||
// 3. Rate limiter cleanup now pre-allocates and logs failures
|
||||
// 4. Network broadcast now returns BroadcastResult with counts
|
||||
// 5. Handler errors now use std.log.err and record metrics
|
||||
// 6. Error helper functions now actually use their parameters
|
||||
// 7. Test cleanup now logs failures
|
||||
|
||||
try testing.expect(true); // All fixes verified by other tests
|
||||
}
|
||||
|
||||
@@ -152,9 +152,6 @@ test "health check handles allocation failures gracefully" {
|
||||
// Health checks should complete without crashing even if allocations fail internally
|
||||
// This is difficult to test directly, but we can verify the function doesn't crash
|
||||
try p2p.performHealthChecks();
|
||||
|
||||
// If we get here without crashing, the test passes
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test discovery manager initialization failure is logged
|
||||
@@ -174,9 +171,6 @@ test "discovery manager init failure logs warning" {
|
||||
// Start should succeed even if discovery manager fails to init
|
||||
try p2p.start();
|
||||
defer p2p.stop() catch {};
|
||||
|
||||
// Test passes if we get here - discovery failure didn't prevent startup
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test that route cleanup handles allocation failures
|
||||
@@ -204,9 +198,6 @@ test "route cleanup handles allocation failures" {
|
||||
|
||||
// Cleanup should handle any allocation failures gracefully
|
||||
router.cleanupExpiredRoutes(100); // Max age of 100 seconds
|
||||
|
||||
// If we get here without crashing, the test passes
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test that getAllRoutes handles allocation failures
|
||||
@@ -226,9 +217,6 @@ test "getAllRoutes handles allocation failures" {
|
||||
// Get all routes - should handle allocation failures gracefully
|
||||
var routes = router.getAllRoutes();
|
||||
defer routes.deinit(allocator);
|
||||
|
||||
// Should succeed even if some routes couldn't be added to the list
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Integration test: verify no silent failures in critical path
|
||||
@@ -259,9 +247,6 @@ test "integration: no silent failures in message routing" {
|
||||
// Verify errors were logged (counters increased)
|
||||
const final_errors = p2p.stats.errors_total.load(.monotonic);
|
||||
try testing.expect(final_errors >= initial_errors);
|
||||
|
||||
// No silent failures - test passes
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test that error statistics are properly tracked
|
||||
|
||||
@@ -17,13 +17,11 @@ const PieceHash = files.hash.PieceHash;
|
||||
const network = @import("network");
|
||||
const PeerId = network.peer.PeerId;
|
||||
|
||||
// Port allocation for tests
|
||||
var next_port: u16 = 19401;
|
||||
// Port allocation for tests (atomic for parallel test safety)
|
||||
var next_port = std.atomic.Value(u16).init(19401);
|
||||
|
||||
fn getNextPort() u16 {
|
||||
const port = next_port;
|
||||
next_port += 1;
|
||||
return port;
|
||||
return next_port.fetchAdd(1, .acq_rel);
|
||||
}
|
||||
|
||||
fn createTestConfig(port: u16) P2PConfig {
|
||||
|
||||
@@ -162,26 +162,6 @@ test "routing: maintain doesn't double-free peers" {
|
||||
_ = remaining; // Acknowledge counter value even if imprecise
|
||||
}
|
||||
|
||||
// Test socket cleanup on connection error in connectToPeer
|
||||
// This tests the socket leak fix in p2p.zig
|
||||
test "p2p: connectToPeer cleans up socket on error" {
|
||||
// This test would require mocking the Socket type and network operations
|
||||
// For now, we verify the fix is in place by reading the source
|
||||
// In a full implementation, this would use dependency injection
|
||||
// to test error paths
|
||||
|
||||
// Placeholder - would need Socket mock
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test socket cleanup in acceptConnections
|
||||
// This tests the double-free fix in p2p.zig acceptConnections
|
||||
test "p2p: acceptConnections doesn't double-free socket" {
|
||||
// This test would require mocking the Socket type and network operations
|
||||
// Placeholder - would need Socket mock
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test node removeNeighbor with duplicates
|
||||
// This tests the fix in node.zig removeNeighbor
|
||||
test "node: removeNeighbor removes all duplicate neighbors" {
|
||||
@@ -482,60 +462,6 @@ test "peer: getPeerByAddress returns safe copy" {
|
||||
try testing.expectEqual(address.port, peer_info.?.address.port);
|
||||
}
|
||||
|
||||
// Test NetworkManager callback data ownership
|
||||
test "network_manager: callback data is freed after callback" {
|
||||
const allocator = testing.allocator;
|
||||
const network_mod = @import("network");
|
||||
const NetworkManager = network_mod.network_manager.NetworkManager;
|
||||
const PeerConnection = network.PeerConnection;
|
||||
|
||||
var connections = std.AutoHashMap(PeerId, *PeerConnection).init(allocator);
|
||||
defer connections.deinit();
|
||||
|
||||
var manager = NetworkManager.init(allocator, &connections);
|
||||
defer manager.deinit();
|
||||
|
||||
// This test verifies the pattern is correct:
|
||||
// - extractMessage allocates data
|
||||
// - defer frees it after callback
|
||||
// - callback must copy if it wants to keep data
|
||||
|
||||
var callback_invoked = false;
|
||||
var data_was_valid = false;
|
||||
|
||||
const TestContext = struct {
|
||||
invoked: *bool,
|
||||
valid: *bool,
|
||||
};
|
||||
|
||||
const callback = struct {
|
||||
fn cb(peer_id: PeerId, data: []const u8, ctx: ?*anyopaque) void {
|
||||
_ = peer_id;
|
||||
const test_ctx = @as(*TestContext, @ptrCast(@alignCast(ctx.?)));
|
||||
test_ctx.invoked.* = true;
|
||||
|
||||
// Data should be valid during callback
|
||||
if (data.len > 0) {
|
||||
test_ctx.valid.* = true;
|
||||
}
|
||||
|
||||
// If callback needs data later, it MUST copy:
|
||||
// const my_copy = allocator.dupe(u8, data) catch return;
|
||||
// store_for_later(my_copy);
|
||||
}
|
||||
}.cb;
|
||||
|
||||
var test_ctx = TestContext{
|
||||
.invoked = &callback_invoked,
|
||||
.valid = &data_was_valid,
|
||||
};
|
||||
|
||||
manager.setMessageCallback(callback, &test_ctx);
|
||||
|
||||
// Verify the pattern works (callback data is temporary)
|
||||
try testing.expect(true); // Placeholder - actual test would need mock socket
|
||||
}
|
||||
|
||||
// Test concurrent access to connections (stress test)
|
||||
test "connection: concurrent access with guards is safe" {
|
||||
const allocator = testing.allocator;
|
||||
@@ -599,21 +525,3 @@ test "connection: safe removal with outstanding references" {
|
||||
// Now connection is freed
|
||||
}
|
||||
|
||||
// Test that getPeerSocket returns pointer only valid for immediate use
|
||||
test "p2p: getPeerSocket pointer lifetime" {
|
||||
// This test documents the usage pattern
|
||||
// In practice, the socket pointer should only be used immediately
|
||||
// and not stored for later use
|
||||
|
||||
// SAFE PATTERN (documented in getPeerSocket):
|
||||
// if (p2p.getPeerSocket(peer_id)) |socket| {
|
||||
// _ = socket.send(data) catch |err| { ... };
|
||||
// }
|
||||
|
||||
// UNSAFE PATTERN (will crash):
|
||||
// const socket = p2p.getPeerSocket(peer_id).?; // Store pointer
|
||||
// // ... other code ...
|
||||
// _ = socket.send(data); // CRASH: socket may be freed
|
||||
|
||||
try testing.expect(true); // Documentation test
|
||||
}
|
||||
|
||||
@@ -50,14 +50,17 @@ test "MWP: two P2P nodes complete hole punch on localhost" {
|
||||
};
|
||||
|
||||
// Register Bob's DHT ID on Alice's side (so Alice can route DHT messages to Bob)
|
||||
const server_peer_id = pair.getPeerId().?;
|
||||
// Get Bob's view of Alice's peer ID from the server's perspective
|
||||
// The server sees the client as server_peer_id, and the client sees the server as its connected peer
|
||||
pair.server.registerDhtNodeId(bob_dht_id, server_peer_id) catch {};
|
||||
// getPeerId() returns the client's view of the server — correct for Bob's side
|
||||
// getServerSidePeerId() returns the server's view of the client — correct for Alice's side
|
||||
const client_view_of_server = pair.getPeerId().?;
|
||||
const server_view_of_client = pair.getServerSidePeerId() orelse {
|
||||
std.debug.print("Server has no connected peer\n", .{});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
pair.server.registerDhtNodeId(bob_dht_id, server_view_of_client) catch {};
|
||||
|
||||
// Register Alice's DHT ID on Bob's side
|
||||
// The client's connected peer is the server
|
||||
pair.client.registerDhtNodeId(alice_dht_id, server_peer_id) catch {};
|
||||
pair.client.registerDhtNodeId(alice_dht_id, client_view_of_server) catch {};
|
||||
|
||||
// Add to DHT routing tables
|
||||
var alice_addr = Address.init();
|
||||
|
||||
@@ -293,8 +293,6 @@ test "FileSharingManager: concurrent session operations" {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
// If we got here without crashing, the iterator-while-modifying fix works
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
// Test mixed operations: Simulate realistic concurrent usage
|
||||
|
||||
@@ -288,8 +288,7 @@ test "NAT: XOR address encoding concept" {
|
||||
const xored_port = port ^ @as(u16, @truncate(magic_cookie >> 16));
|
||||
|
||||
// The XORed value should be different from original
|
||||
_ = xored_port;
|
||||
try testing.expect(true);
|
||||
try testing.expect(xored_port != port);
|
||||
}
|
||||
|
||||
test "NAT: IPv4 address XOR concept" {
|
||||
|
||||
@@ -426,8 +426,12 @@ test "ReplayProtector: same message ID different peers" {
|
||||
// Peer A sends message ID 42
|
||||
try protector.checkMessage(42, current_time, 15000, 1, peer_a);
|
||||
|
||||
// Peer B tries same message ID - should fail (message IDs are global)
|
||||
const result = protector.checkMessage(42, current_time + 1, 15001, 1, peer_b);
|
||||
// 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);
|
||||
|
||||
// But Peer A replaying its own message ID should still be rejected
|
||||
const result = protector.checkMessage(42, current_time + 2, 15002, 2, peer_a);
|
||||
try testing.expectError(ReplayError.DuplicateMessageId, result);
|
||||
}
|
||||
|
||||
|
||||
@@ -250,16 +250,6 @@ test "serializer: roundtrip app data message" {
|
||||
try testing.expectEqualStrings("custom application payload", full_msg_deserialized.app_data.data);
|
||||
}
|
||||
|
||||
// SEC-M9 FIX: Removed CRC32 checksum test.
|
||||
// CRC32 was redundant with HMAC-SHA256 which provides both integrity AND authentication.
|
||||
// HMAC integrity is tested in the HMAC-specific tests.
|
||||
test "serializer: hmac integrity note" {
|
||||
// This is a placeholder test documenting that integrity checking
|
||||
// is now exclusively handled by HMAC-SHA256, not CRC32.
|
||||
// HMAC provides cryptographic integrity guarantees that CRC32 cannot.
|
||||
try testing.expect(true);
|
||||
}
|
||||
|
||||
test "serializer: corrupted header handling" {
|
||||
const allocator = testing.allocator;
|
||||
var ser = serializer.MessageSerializer.initInsecureForTesting(allocator);
|
||||
|
||||
Reference in New Issue
Block a user