ab ovo
This commit is contained in:
352
tests/files/test_main.zig
Normal file
352
tests/files/test_main.zig
Normal file
@@ -0,0 +1,352 @@
|
||||
//! Main test file for the file sharing module
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
// Import all file sharing modules
|
||||
const hash_module = @import("test_hash.zig");
|
||||
const file_module = @import("test_file.zig");
|
||||
const piece_module = @import("test_piece.zig");
|
||||
const transfer_module = @import("test_transfer.zig");
|
||||
const download_upload_integration = @import("test_download_upload_integration.zig");
|
||||
const workflow_scenarios = @import("test_workflow_scenarios.zig");
|
||||
const performance_scalability = @import("test_performance_scalability.zig");
|
||||
|
||||
// Re-export test modules for easy access
|
||||
pub usingnamespace hash_module;
|
||||
pub usingnamespace file_module;
|
||||
pub usingnamespace piece_module;
|
||||
pub usingnamespace transfer_module;
|
||||
pub usingnamespace download_upload_integration;
|
||||
pub usingnamespace workflow_scenarios;
|
||||
pub usingnamespace performance_scalability;
|
||||
|
||||
test "file sharing module integration test" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create a test file for integration testing
|
||||
const test_file_path = "integration_test.bin";
|
||||
const test_content = "Integration test file content for testing file sharing operations";
|
||||
|
||||
const file = std.fs.cwd().createFile(test_file_path, .{}) catch return;
|
||||
_ = file.write(test_content) catch return;
|
||||
file.close();
|
||||
|
||||
// Test the complete flow:
|
||||
// 1. Hash calculation
|
||||
const file_hash = try std.crypto.hash.Sha256.init();
|
||||
var hash_array: [32]u8 = undefined;
|
||||
file_hash.final(&hash_array);
|
||||
|
||||
// 2. File chunking and metadata creation
|
||||
const chunker = FileChunker.init(1024, allocator); // Small piece size for testing
|
||||
const metadata = try chunker.createMetadata(test_file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
try testing.expectEqualSlices(u8, "integration_test.bin", metadata.name);
|
||||
try testing.expect(metadata.piece_count > 0);
|
||||
|
||||
// 3. Piece selection
|
||||
const selector = PieceSelector.init(.rarest_first, 12345, allocator);
|
||||
defer selector.deinit();
|
||||
|
||||
for (0..metadata.piece_count) |i| {
|
||||
try selector.addPiece(@as(u32, @intCast(i)), 1);
|
||||
}
|
||||
|
||||
const selected_pieces = try selector.selectNext(1);
|
||||
defer allocator.free(selected_pieces);
|
||||
|
||||
try testing.expect(selected_pieces.len > 0);
|
||||
|
||||
// 4. Piece verification
|
||||
const piece_data = try chunker.readPiece(test_file_path, selected_pieces[0], metadata.pieceSize(selected_pieces[0]));
|
||||
defer allocator.free(piece_data);
|
||||
|
||||
const expected_hash = metadata.piece_hashes[selected_pieces[0]];
|
||||
const is_valid = try HashUtils.verify(piece_data, expected_hash);
|
||||
try testing.expect(is_valid);
|
||||
|
||||
// 5. File verification
|
||||
const file_integrity = try chunker.verifyFile(test_file_path, metadata);
|
||||
try testing.expect(file_integrity);
|
||||
|
||||
// Clean up
|
||||
std.fs.cwd().deleteFile(test_file_path) catch return;
|
||||
}
|
||||
|
||||
test "file sharing performance test" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create a larger test file for performance testing
|
||||
const test_file_path = "performance_test.bin";
|
||||
const test_size = 100 * 1024; // 100KB
|
||||
const test_content = [_]u8{0} ** test_size;
|
||||
|
||||
const file = std.fs.cwd().createFile(test_file_path, .{}) catch return;
|
||||
_ = file.write(&test_content) catch return;
|
||||
file.close();
|
||||
|
||||
// Test chunking performance
|
||||
const chunker = FileChunker.init(4096, allocator); // 4KB pieces
|
||||
const metadata = try chunker.createMetadata(test_file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
// Should create approximately 25 pieces (100KB / 4KB)
|
||||
const expected_pieces = @as(u32, @intCast(test_size / 4096));
|
||||
try testing.expectEqual(expected_pieces, metadata.piece_count);
|
||||
|
||||
// Test piece selection performance
|
||||
const selector = PieceSelector.init(.balanced, 54321, allocator);
|
||||
defer selector.deinit();
|
||||
|
||||
for (0..metadata.piece_count) |i| {
|
||||
try selector.addPiece(@as(u32, @intCast(i)), 2); // Each piece available from 2 peers
|
||||
}
|
||||
|
||||
const start_time = std.time.timestamp();
|
||||
const selected_pieces = try selector.selectNext(null);
|
||||
const end_time = std.time.timestamp();
|
||||
defer allocator.free(selected_pieces);
|
||||
|
||||
// Selection should be fast (less than 1 second)
|
||||
try testing.expect(end_time - start_time < 1);
|
||||
try testing.expectEqual(@as(usize, metadata.piece_count), selected_pieces.len);
|
||||
|
||||
// Test hash verification performance
|
||||
const verification_start = std.time.timestamp();
|
||||
for (selected_pieces) |piece_index| {
|
||||
const piece_size = metadata.pieceSize(piece_index);
|
||||
const piece_data = try chunker.readPiece(test_file_path, piece_index, piece_size);
|
||||
defer allocator.free(piece_data);
|
||||
|
||||
const expected_hash = metadata.piece_hashes[piece_index];
|
||||
const is_valid = try HashUtils.verify(piece_data, expected_hash);
|
||||
try testing.expect(is_valid);
|
||||
}
|
||||
const verification_end = std.time.timestamp();
|
||||
|
||||
// Verification should also be reasonably fast
|
||||
try testing.expect(verification_end - verification_start < 5);
|
||||
|
||||
// Clean up
|
||||
std.fs.cwd().deleteFile(test_file_path) catch return;
|
||||
}
|
||||
|
||||
test "file sharing stress test with multiple files" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create multiple test files
|
||||
const file_count = 5;
|
||||
var file_paths = std.ArrayList([]const u8).init(allocator);
|
||||
defer {
|
||||
for (file_paths.items) |path| {
|
||||
allocator.free(path);
|
||||
}
|
||||
file_paths.deinit();
|
||||
}
|
||||
|
||||
for (0..file_count) |i| {
|
||||
const file_path = try fmt.allocPrint(allocator, "stress_test_{d}.bin", .{i});
|
||||
try file_paths.append(file_path);
|
||||
|
||||
// Create files with different sizes
|
||||
const file_size = (i + 1) * 1024; // 1KB, 2KB, 3KB, 4KB, 5KB
|
||||
const content = [_]u8{ @as(u8, @intCast(i)) } ** file_size;
|
||||
|
||||
const file = std.fs.cwd().createFile(file_path, .{}) catch continue;
|
||||
_ = file.write(&content) catch continue;
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Process all files
|
||||
const chunker = FileChunker.init(512, allocator); // Small pieces
|
||||
var total_pieces: u32 = 0;
|
||||
|
||||
for (file_paths.items) |file_path| {
|
||||
const metadata = try chunker.createMetadata(file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
total_pieces += metadata.piece_count;
|
||||
|
||||
// Verify file integrity
|
||||
const is_valid = try chunker.verifyFile(file_path, metadata);
|
||||
try testing.expect(is_valid);
|
||||
}
|
||||
|
||||
try testing.expect(total_pieces > 0);
|
||||
|
||||
// Clean up all files
|
||||
for (file_paths.items) |file_path| {
|
||||
std.fs.cwd().deleteFile(file_path) catch continue;
|
||||
}
|
||||
}
|
||||
|
||||
test "file sharing error recovery test" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create test file
|
||||
const test_file_path = "recovery_test.bin";
|
||||
const original_content = "Original content for recovery testing";
|
||||
|
||||
const file = std.fs.cwd().createFile(test_file_path, .{}) catch return;
|
||||
_ = file.write(original_content) catch return;
|
||||
file.close();
|
||||
|
||||
// Create metadata
|
||||
const chunker = FileChunker.init(1024, allocator);
|
||||
const metadata = try chunker.createMetadata(test_file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
// Simulate corrupted piece by writing wrong data
|
||||
const wrong_data = "Corrupted piece data";
|
||||
try chunker.writePiece(test_file_path, 0, wrong_data);
|
||||
|
||||
// Verification should fail
|
||||
const is_valid = try chunker.verifyFile(test_file_path, metadata);
|
||||
try testing.expect(!is_valid);
|
||||
|
||||
// Restore original data
|
||||
try chunker.writePiece(test_file_path, 0, original_content);
|
||||
|
||||
// Verification should pass again
|
||||
const is_valid_after_recovery = try chunker.verifyFile(test_file_path, metadata);
|
||||
try testing.expect(is_valid_after_recovery);
|
||||
|
||||
// Clean up
|
||||
std.fs.cwd().deleteFile(test_file_path) catch return;
|
||||
}
|
||||
|
||||
test "file sharing concurrent access simulation" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create test file
|
||||
const test_file_path = "concurrent_test.bin";
|
||||
const content = "Content for concurrent access testing";
|
||||
|
||||
const file = std.fs.cwd().createFile(test_file_path, .{}) catch return;
|
||||
_ = file.write(content) catch return;
|
||||
file.close();
|
||||
|
||||
// Simulate multiple "threads" accessing the file
|
||||
const thread_count = 3;
|
||||
var selectors = std.ArrayList(PieceSelector).init(allocator);
|
||||
defer {
|
||||
for (selectors.items) |*selector| {
|
||||
selector.deinit();
|
||||
}
|
||||
selectors.deinit();
|
||||
}
|
||||
|
||||
// Each "thread" creates its own piece selector
|
||||
for (0..thread_count) |i| {
|
||||
const seed: u64 = 1000 + i;
|
||||
var selector = PieceSelector.init(.random, seed, allocator);
|
||||
try selectors.append(selector);
|
||||
}
|
||||
|
||||
// All selectors work with the same metadata
|
||||
const chunker = FileChunker.init(512, allocator);
|
||||
const metadata = try chunker.createMetadata(test_file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
// Each selector adds the pieces
|
||||
for (selectors.items) |*selector| {
|
||||
for (0..metadata.piece_count) |i| {
|
||||
try selector.addPiece(@as(u32, @intCast(i)), thread_count); // All threads can access
|
||||
}
|
||||
}
|
||||
|
||||
// Each selector selects pieces independently
|
||||
for (selectors.items, 0..) |*selector, thread_id| {
|
||||
const selected = try selector.selectNext(1);
|
||||
defer allocator.free(selected);
|
||||
|
||||
try testing.expect(selected.len > 0);
|
||||
|
||||
// Verify the selected piece
|
||||
const piece_index = selected[0];
|
||||
const piece_size = metadata.pieceSize(piece_index);
|
||||
const piece_data = try chunker.readPiece(test_file_path, piece_index, piece_size);
|
||||
defer allocator.free(piece_data);
|
||||
|
||||
const expected_hash = metadata.piece_hashes[piece_index];
|
||||
const is_valid = try HashUtils.verify(piece_data, expected_hash);
|
||||
try testing.expect(is_valid);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
std.fs.cwd().deleteFile(test_file_path) catch return;
|
||||
}
|
||||
|
||||
test "file sharing large file handling" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create a larger test file (1MB)
|
||||
const test_file_path = "large_file_test.bin";
|
||||
const file_size = 1024 * 1024; // 1MB
|
||||
const chunk_size = 1024; // 1KB chunks for the file content
|
||||
|
||||
// Create file in chunks to avoid memory issues
|
||||
const file = std.fs.cwd().createFile(test_file_path, .{}) catch return;
|
||||
|
||||
var bytes_written: usize = 0;
|
||||
while (bytes_written < file_size) {
|
||||
const to_write = @min(chunk_size, file_size - bytes_written);
|
||||
const chunk = [_]u8{ @as(u8, @intCast(bytes_written % 256)) } ** to_write;
|
||||
_ = file.write(&chunk) catch break;
|
||||
bytes_written += to_write;
|
||||
}
|
||||
file.close();
|
||||
|
||||
try testing.expectEqual(file_size, bytes_written);
|
||||
|
||||
// Test with larger piece size
|
||||
const large_piece_size: u64 = 64 * 1024; // 64KB pieces
|
||||
const chunker = FileChunker.init(large_piece_size, allocator);
|
||||
|
||||
const metadata = try chunker.createMetadata(test_file_path);
|
||||
defer allocator.free(metadata.piece_hashes);
|
||||
|
||||
// Should have approximately 16 pieces (1MB / 64KB)
|
||||
const expected_pieces = @as(u32, @intCast(@divCeil(file_size, large_piece_size)));
|
||||
try testing.expectEqual(expected_pieces, metadata.piece_count);
|
||||
|
||||
// Test piece selection on large file
|
||||
const selector = PieceSelector.init(.sequential, 99999, allocator);
|
||||
defer selector.deinit();
|
||||
|
||||
for (0..metadata.piece_count) |i| {
|
||||
try selector.addPiece(@as(u32, @intCast(i)), 1);
|
||||
}
|
||||
|
||||
const selected = try selector.selectNext(5);
|
||||
defer allocator.free(selected);
|
||||
|
||||
try testing.expectEqual(@as(usize, 5), selected.len);
|
||||
|
||||
// Test reading a few pieces from the large file
|
||||
for (selected) |piece_index| {
|
||||
const piece_size = metadata.pieceSize(piece_index);
|
||||
const piece_data = try chunker.readPiece(test_file_path, piece_index, piece_size);
|
||||
defer allocator.free(piece_data);
|
||||
|
||||
try testing.expect(piece_data.len > 0);
|
||||
try testing.expect(piece_data.len <= large_piece_size);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
std.fs.cwd().deleteFile(test_file_path) catch return;
|
||||
}
|
||||
|
||||
// Test utility functions
|
||||
fn compareByAvailability(context: *PieceSelector, a: u32, b: u32) bool {
|
||||
const piece_a = context.availability_map.get(a) orelse PieceAvailability.create(0, 0);
|
||||
const piece_b = context.availability_map.get(b) orelse PieceAvailability.create(0, 0);
|
||||
|
||||
if (piece_a.available_peers != piece_b.available_peers) {
|
||||
return piece_a.available_peers < piece_b.available_peers;
|
||||
}
|
||||
|
||||
return a < b;
|
||||
}
|
||||
Reference in New Issue
Block a user