ab ovo
This commit is contained in:
403
tests/integration/test_memory_leaks.zig
Normal file
403
tests/integration/test_memory_leaks.zig
Normal file
@@ -0,0 +1,403 @@
|
||||
//! Comprehensive memory leak tests
|
||||
//!
|
||||
//! This test suite validates that all allocations are properly freed
|
||||
//! and there are no memory leaks in the codebase.
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
// Import modules to test
|
||||
const p2p_mod = @import("p2p");
|
||||
const P2P = p2p_mod.P2P;
|
||||
const P2PEvent = p2p_mod.P2PEvent;
|
||||
const P2PConfig = p2p_mod.P2PConfig;
|
||||
|
||||
const files = @import("files");
|
||||
const HashPool = files.hash.HashPool;
|
||||
const HashUtils = files.hash.HashUtils;
|
||||
const FileMetadata = files.file.FileMetadata;
|
||||
const PieceData = files.hash.PieceData;
|
||||
const PieceHash = files.hash.PieceHash;
|
||||
const PieceSelector = files.piece.PieceSelector;
|
||||
const SelectionStrategy = files.piece.SelectionStrategy;
|
||||
const PiecePool = files.piece.PiecePool;
|
||||
const PoolPieceData = files.piece.PieceData;
|
||||
|
||||
const protocol = @import("protocol");
|
||||
const CryptoManager = protocol.crypto.CryptoManager;
|
||||
|
||||
const network = @import("network");
|
||||
const NetworkTopology = network.topology.NetworkTopology;
|
||||
const NodePosition = network.topology.NodePosition;
|
||||
const TopologyType = network.topology.TopologyType;
|
||||
const KademliaRoutingTable = network.routing.KademliaRoutingTable;
|
||||
const PeerId = network.peer.PeerId;
|
||||
const Peer = network.peer.Peer;
|
||||
|
||||
const core = @import("core");
|
||||
const Address = core.address.Address;
|
||||
|
||||
// ============================================================================
|
||||
// P2P Event Tests
|
||||
// ============================================================================
|
||||
|
||||
test "P2P event with owned payload - cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create event with owned payload
|
||||
const payload = try allocator.dupe(u8, "test message data");
|
||||
|
||||
var event = P2PEvent.create(.message_received, .{
|
||||
.message_received = .{
|
||||
.message_type = 1,
|
||||
.source = PeerId.init(),
|
||||
.data = payload,
|
||||
},
|
||||
});
|
||||
event.owns_payload = true;
|
||||
event.allocator = allocator;
|
||||
|
||||
// Clean up - should not leak
|
||||
event.deinit();
|
||||
}
|
||||
|
||||
test "P2P event queue cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var p2p = try P2P.init(allocator, P2PConfig.default());
|
||||
defer p2p.deinit();
|
||||
|
||||
// Add events with owned payloads
|
||||
for (0..10) |i| {
|
||||
const payload = try allocator.dupe(u8, "test data");
|
||||
var event = P2PEvent.create(.message_received, .{
|
||||
.message_received = .{
|
||||
.message_type = @intCast(i),
|
||||
.source = PeerId.init(),
|
||||
.data = payload,
|
||||
},
|
||||
});
|
||||
event.owns_payload = true;
|
||||
event.allocator = allocator;
|
||||
try p2p.emitEvent(event);
|
||||
}
|
||||
|
||||
// Process all events - should clean up properly
|
||||
const processed = p2p.processAllEvents();
|
||||
try testing.expectEqual(@as(usize, 11), processed); // 10 + 1 init event
|
||||
}
|
||||
|
||||
test "P2P getNextEvent transfers ownership" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var p2p = try P2P.init(allocator, P2PConfig.default());
|
||||
defer p2p.deinit();
|
||||
|
||||
// Clear init event
|
||||
_ = p2p.processNextEvent();
|
||||
|
||||
// Add event with owned payload
|
||||
const payload = try allocator.dupe(u8, "test data");
|
||||
var event = P2PEvent.create(.message_received, .{
|
||||
.message_received = .{
|
||||
.message_type = 1,
|
||||
.source = PeerId.init(),
|
||||
.data = payload,
|
||||
},
|
||||
});
|
||||
event.owns_payload = true;
|
||||
event.allocator = allocator;
|
||||
try p2p.emitEvent(event);
|
||||
|
||||
// Get event - caller must clean up
|
||||
if (p2p.getNextEvent()) |mut_event| {
|
||||
var e = mut_event;
|
||||
defer e.deinit();
|
||||
try testing.expect(e.owns_payload);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Hash Module Tests
|
||||
// ============================================================================
|
||||
|
||||
test "HashPool verifyPieces cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
const pool = HashPool.init(allocator);
|
||||
|
||||
// Create test metadata
|
||||
const piece_hashes = try allocator.alloc(PieceHash, 2);
|
||||
defer allocator.free(piece_hashes);
|
||||
|
||||
piece_hashes[0] = try HashUtils.sha256("piece 0");
|
||||
piece_hashes[1] = try HashUtils.sha256("piece 1");
|
||||
|
||||
const metadata = try FileMetadata.create("test.bin", 32, piece_hashes, allocator);
|
||||
defer allocator.free(metadata.name);
|
||||
|
||||
// Create test pieces
|
||||
var pieces: std.ArrayList(PieceData) = .{};
|
||||
defer pieces.deinit(allocator);
|
||||
|
||||
try pieces.append(allocator, try PieceData.create(0, "piece 0"));
|
||||
try pieces.append(allocator, try PieceData.create(1, "piece 1"));
|
||||
|
||||
// Verify pieces - should clean up properly
|
||||
var batch = try pool.verifyPieces(metadata, pieces.items);
|
||||
defer batch.deinit();
|
||||
|
||||
try testing.expect(batch.isCompleteSuccess());
|
||||
}
|
||||
|
||||
test "VerificationBatch getFailedPieces cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
const pool = HashPool.init(allocator);
|
||||
|
||||
// Create test metadata with wrong hashes
|
||||
const piece_hashes = try allocator.alloc(PieceHash, 2);
|
||||
defer allocator.free(piece_hashes);
|
||||
|
||||
piece_hashes[0] = try HashUtils.sha256("wrong 0");
|
||||
piece_hashes[1] = try HashUtils.sha256("wrong 1");
|
||||
|
||||
const metadata = try FileMetadata.create("test.bin", 32, piece_hashes, allocator);
|
||||
defer allocator.free(metadata.name);
|
||||
|
||||
// Create test pieces with correct data
|
||||
var pieces: std.ArrayList(PieceData) = .{};
|
||||
defer pieces.deinit(allocator);
|
||||
|
||||
try pieces.append(allocator, try PieceData.create(0, "piece 0"));
|
||||
try pieces.append(allocator, try PieceData.create(1, "piece 1"));
|
||||
|
||||
// Verify pieces - should fail
|
||||
var batch = try pool.verifyPieces(metadata, pieces.items);
|
||||
defer batch.deinit();
|
||||
|
||||
// Get failed pieces - should clean up
|
||||
const failed = try batch.getFailedPieces();
|
||||
defer allocator.free(failed);
|
||||
|
||||
try testing.expectEqual(@as(usize, 2), failed.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Piece Selection Tests
|
||||
// ============================================================================
|
||||
|
||||
test "PieceSelector selectNext cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
var selector = PieceSelector.init(.rarest_first, 12345, allocator);
|
||||
defer selector.deinit();
|
||||
|
||||
// Add pieces
|
||||
for (0..5) |i| {
|
||||
try selector.addPiece(@intCast(i), 3);
|
||||
}
|
||||
|
||||
// Select pieces - should clean up
|
||||
const selected = try selector.selectNext(null);
|
||||
defer allocator.free(selected);
|
||||
|
||||
try testing.expectEqual(@as(usize, 5), selected.len);
|
||||
}
|
||||
|
||||
test "PiecePool getPieceIndices cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
var pool = PiecePool.init(allocator);
|
||||
defer pool.deinit();
|
||||
|
||||
// Add test pieces
|
||||
const test_data = "Test piece data";
|
||||
const hash = [_]u8{0} ** 32;
|
||||
|
||||
for (0..10) |i| {
|
||||
const piece = PoolPieceData{
|
||||
.index = @intCast(i),
|
||||
.data = @constCast(test_data),
|
||||
.hash = hash,
|
||||
};
|
||||
try pool.addPiece(piece);
|
||||
}
|
||||
|
||||
// Get indices - should clean up
|
||||
const indices = try pool.getPieceIndices();
|
||||
defer allocator.free(indices);
|
||||
|
||||
try testing.expectEqual(@as(usize, 10), indices.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Crypto Tests
|
||||
// ============================================================================
|
||||
|
||||
// REMOVED: CryptoManager.encrypt() has been removed for security
|
||||
// Use EncryptionManager from encryption.zig instead for counter-based nonces
|
||||
|
||||
// ============================================================================
|
||||
// Network Topology Tests
|
||||
// ============================================================================
|
||||
|
||||
test "NetworkTopology findShortestPath cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
var topology = NetworkTopology.init(allocator, .Mesh);
|
||||
defer topology.deinit();
|
||||
|
||||
const peer1 = PeerId.init();
|
||||
const peer2 = PeerId.init();
|
||||
const peer3 = PeerId.init();
|
||||
|
||||
_ = try topology.addNode(peer1, .Hub);
|
||||
_ = try topology.addNode(peer2, .Regular);
|
||||
_ = try topology.addNode(peer3, .Leaf);
|
||||
|
||||
try topology.addConnection(peer1, peer2, 80);
|
||||
try topology.addConnection(peer2, peer3, 70);
|
||||
|
||||
// Find path - must clean up ArrayList
|
||||
var path = try topology.findShortestPath(peer1, peer3);
|
||||
defer path.deinit(allocator);
|
||||
|
||||
try testing.expectEqual(@as(usize, 3), path.items.len);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Routing Tests
|
||||
// ============================================================================
|
||||
|
||||
test "KademliaRoutingTable getRefreshCandidates cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const local_id = PeerId.init();
|
||||
var routing_table = KademliaRoutingTable.init(allocator, local_id);
|
||||
defer routing_table.deinit();
|
||||
|
||||
// Add some peers
|
||||
for (0..5) |_| {
|
||||
const peer_id = PeerId.init();
|
||||
const address = Address.init();
|
||||
var peer = Peer.init(peer_id, address);
|
||||
|
||||
var topology_position = NodePosition.init(allocator, peer_id);
|
||||
defer topology_position.deinit();
|
||||
|
||||
try routing_table.addPeer(&peer, &topology_position);
|
||||
}
|
||||
|
||||
// Get refresh candidates - must free
|
||||
const candidates = try routing_table.getRefreshCandidates();
|
||||
defer allocator.free(candidates);
|
||||
|
||||
// May be empty if no buckets need refresh
|
||||
try testing.expect(candidates.len >= 0);
|
||||
}
|
||||
|
||||
test "KademliaRoutingTable findClosestPeers cleanup" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const local_id = PeerId.init();
|
||||
var routing_table = KademliaRoutingTable.init(allocator, local_id);
|
||||
defer routing_table.deinit();
|
||||
|
||||
const target_id = PeerId.init();
|
||||
|
||||
// Add multiple peers
|
||||
var peers: std.ArrayList(*Peer) = .{};
|
||||
defer {
|
||||
for (peers.items) |peer| {
|
||||
allocator.destroy(peer);
|
||||
}
|
||||
peers.deinit(allocator);
|
||||
}
|
||||
|
||||
var positions: std.ArrayList(*NodePosition) = .{};
|
||||
defer {
|
||||
for (positions.items) |pos| {
|
||||
pos.deinit();
|
||||
allocator.destroy(pos);
|
||||
}
|
||||
positions.deinit(allocator);
|
||||
}
|
||||
|
||||
for (0..10) |_| {
|
||||
const peer_id = PeerId.init();
|
||||
const address = Address.init();
|
||||
const peer = try allocator.create(Peer);
|
||||
peer.* = Peer.init(peer_id, address);
|
||||
try peers.append(allocator, peer);
|
||||
|
||||
const topology_position = try allocator.create(NodePosition);
|
||||
topology_position.* = NodePosition.init(allocator, peer_id);
|
||||
try positions.append(allocator, topology_position);
|
||||
|
||||
try routing_table.addPeer(peer, topology_position);
|
||||
}
|
||||
|
||||
// Find closest peers - must free
|
||||
const closest_peers = try routing_table.findClosestPeers(target_id, 5);
|
||||
defer allocator.free(closest_peers);
|
||||
|
||||
try testing.expect(closest_peers.len <= 5);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Integration Tests
|
||||
// ============================================================================
|
||||
|
||||
test "Full P2P lifecycle - no leaks" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Initialize P2P
|
||||
var p2p = try P2P.init(allocator, P2PConfig.default());
|
||||
defer p2p.deinit();
|
||||
|
||||
// Start P2P
|
||||
try p2p.start();
|
||||
|
||||
// Simulate some operations
|
||||
for (0..10) |_| {
|
||||
// Update will emit events
|
||||
try p2p.update();
|
||||
|
||||
// Process events
|
||||
_ = p2p.processAllEvents();
|
||||
|
||||
// Small sleep simulation
|
||||
std.Thread.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
// Stop P2P
|
||||
try p2p.stop();
|
||||
|
||||
// Everything should be cleaned up in deinit
|
||||
}
|
||||
|
||||
test "Multiple selector strategies - no leaks" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const strategies = [_]SelectionStrategy{
|
||||
.rarest_first,
|
||||
.random,
|
||||
.sequential,
|
||||
.priority_first,
|
||||
.balanced,
|
||||
.bandwidth_optimized,
|
||||
.network_aware,
|
||||
};
|
||||
|
||||
for (strategies) |strategy| {
|
||||
var selector = PieceSelector.init(strategy, 12345, allocator);
|
||||
defer selector.deinit();
|
||||
|
||||
// Add pieces
|
||||
for (0..20) |i| {
|
||||
try selector.addPiece(@intCast(i), @intCast(i % 5 + 1));
|
||||
}
|
||||
|
||||
// Select pieces
|
||||
const selected = try selector.selectNext(10);
|
||||
defer allocator.free(selected);
|
||||
|
||||
try testing.expect(selected.len <= 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user