clean up tests. remove placeholder assert-true tests, fix replay
protection and NAT test logic, fix MWP connection test peer ID registration, use atomic port counter for test parallelism
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user