This commit is contained in:
2026-02-06 21:03:59 -03:00
commit 2b7ab5f570
267 changed files with 176026 additions and 0 deletions

View File

@@ -0,0 +1,261 @@
//! Tests for transfer protocol and management
const std = @import("std");
const testing = std.testing;
const TransferModule = @import("../src/files/transfer.zig");
const {
TransferMessage, MessageType, TransferSession, TransferProtocol,
RateLimiter, ProgressTracker, TransferConfig, TransferStats
} = TransferModule;
test "rate limiter basic operations" {
var rate_limiter = RateLimiter.init();
// Initially should allow transfers
try testing.expect(rate_limiter.canStartTransfer(1024));
// Start a transfer
rate_limiter.startTransfer(1024);
try testing.expectEqual(@as(usize, 1), rate_limiter.active_transfers.load(.monotonic));
// Complete the transfer
rate_limiter.completeTransfer(1024);
try testing.expectEqual(@as(usize, 0), rate_limiter.active_transfers.load(.monotonic));
// Check bandwidth utilization
const utilization = rate_limiter.getBandwidthUtilization();
try testing.expect(utilization >= 0.0 and utilization <= 1.0);
}
test "rate limiter concurrent transfer limits" {
var rate_limiter = RateLimiter.init();
rate_limiter.max_concurrent_transfers = 2;
// Should allow up to 2 concurrent transfers
try testing.expect(rate_limiter.canStartTransfer(1024));
rate_limiter.startTransfer(1024);
try testing.expect(rate_limiter.canStartTransfer(1024));
rate_limiter.startTransfer(1024);
// Third transfer should be blocked
try testing.expect(!rate_limiter.canStartTransfer(1024));
// Complete one transfer
rate_limiter.completeTransfer(1024);
// Should allow new transfer now
try testing.expect(rate_limiter.canStartTransfer(1024));
}
test "progress tracker basic operations" {
const total_bytes: u64 = 1000;
var tracker = ProgressTracker.init(total_bytes);
// Initially no progress
try testing.expectEqual(@as(f32, 0.0), tracker.getDownloadProgress());
try testing.expectEqual(@as(f32, 0.0), tracker.getUploadProgress());
try testing.expectEqual(@as(f32, 0.0), tracker.getDownloadSpeed());
// Update download progress
tracker.updateDownload(250);
try testing.expectEqual(@as(f32, 0.25), tracker.getDownloadProgress());
tracker.updateDownload(250);
try testing.expectEqual(@as(f32, 0.5), tracker.getDownloadProgress());
// Update upload progress
tracker.updateUpload(100);
try testing.expectEqual(@as(f32, 0.1), tracker.getUploadProgress());
// Should have some speed now (non-zero time elapsed)
const speed = tracker.getDownloadSpeed();
try testing.expect(speed >= 0.0);
}
test "progress tracker speed calculation" {
var tracker = ProgressTracker.init(1000);
// Wait a bit and then update
std.time.sleep(100_000_000); // 100ms
tracker.updateDownload(500);
const speed = tracker.getDownloadSpeed();
try testing.expect(speed > 0.0);
// Speed should be approximately 500 bytes / elapsed_time
// (allowing for some variance due to timing)
try testing.expect(speed > 4000.0 and speed < 6000.0); // Roughly 5KB/s +/- 1KB
}
test "progress tracker time estimation" {
var tracker = ProgressTracker.init(1000);
// No progress yet
try testing.expectEqual(@as(i64, -1), tracker.getEstimatedDownloadTimeRemaining());
// Add some progress
tracker.updateDownload(250);
std.time.sleep(100_000_000); // 100ms
const time_remaining = tracker.getEstimatedDownloadTimeRemaining();
try testing.expect(time_remaining > 0); // Should estimate positive time remaining
try testing.expect(time_remaining < 10); // Should be reasonable (less than 10 seconds for 750 bytes)
// Complete download
tracker.updateDownload(750);
try testing.expectEqual(@as(i64, 0), tracker.getEstimatedDownloadTimeRemaining());
}
test "progress tracker stall detection" {
var tracker = ProgressTracker.init(1000);
// Should not be stalled initially
try testing.expect(!tracker.isStalled(60));
// Update progress to mark as active
tracker.updateDownload(100);
// Should not be stalled with recent update
try testing.expect(!tracker.isStalled(60));
}
test "transfer session with progress tracking" {
const allocator = testing.allocator;
// Create test metadata
const piece_hashes = try allocator.alloc([32]u8, 3);
for (piece_hashes) |*hash| {
hash.* = [_]u8{0} ** 32;
}
const metadata = try std.files.hash.FileMetadata.create("test.bin", 1024, piece_hashes, allocator);
defer allocator.free(piece_hashes);
const session = try TransferSession.create("peer123", metadata, allocator);
defer session.deinit();
// Progress should be initialized
try testing.expectEqual(@as(f32, 0.0), session.progress_tracker.getDownloadProgress());
try testing.expectEqual(@as(u64, 1024), session.progress_tracker.total_bytes);
// Update progress
session.progress_tracker.updateDownload(256);
try testing.expectEqual(@as(f32, 0.25), session.progress_tracker.getDownloadProgress());
// Rate limiter should be initialized
try testing.expect(session.rate_limiter.max_bandwidth_bps > 0);
}
test "transfer protocol rate limiting integration" {
const allocator = testing.allocator;
var protocol = TransferProtocol.init(allocator);
defer protocol.deinit();
// Configure protocol with custom rate limits
var config = TransferConfig.default();
config.rate_limiter.max_concurrent_transfers = 3;
config.rate_limiter.max_bandwidth_bps = 512 * 1024; // 512KB/s
// Test that protocol can handle rate-limited sessions
const stats = protocol.getStats();
try testing.expectEqual(@as(usize, 0), stats.total_sessions);
// Protocol should handle message rate limiting
const handshake_msg = TransferMessage.handshake("peer1", "client1");
try protocol.handleMessage(handshake_msg);
const stats = protocol.getStats();
try testing.expect(stats.pending_messages > 0);
protocol.clearSentMessages();
}
test "transfer stats with rate limiting" {
const allocator = testing.allocator;
var protocol = TransferProtocol.init(allocator);
defer protocol.deinit();
// Get initial stats
const initial_stats = protocol.getStats();
try testing.expectEqual(@as(u64, 0), initial_stats.total_downloaded);
try testing.expectEqual(@as(u64, 0), initial_stats.total_uploaded);
// Test bandwidth utilization calculation
const utilization = protocol.config.rate_limiter.getBandwidthUtilization();
try testing.expect(utilization >= 0.0 and utilization <= 1.0);
}
test "transfer message with enhanced metadata" {
const peer_id = "enhanced_peer";
const client_id = "enhanced_client";
// Test handshake with enhanced metadata
const handshake_msg = TransferMessage.handshake(peer_id, client_id);
try testing.expectEqual(MessageType.handshake, handshake_msg.message_type);
try testing.expect(handshake_msg.timestamp > 0);
// Test piece request with proper sizing
// New format: content_hash(32) + piece_index(4) + offset(4) = 40 bytes
const test_content_hash = [_]u8{0xEF} ** 32;
const piece_msg = TransferMessage.pieceRequest(peer_id, test_content_hash, 42, 2048, testing.allocator);
defer testing.allocator.free(piece_msg.data);
try testing.expectEqual(MessageType.piece_request, piece_msg.message_type);
try testing.expectEqual(@as(usize, 40), piece_msg.data.len);
// Test message expiration
try testing.expect(!handshake_msg.isExpired(60)); // Fresh message should not expire
}
test "transfer configuration validation" {
var config = TransferConfig.default();
// Test default values
try testing.expectEqual(@as(usize, 100), config.max_concurrent_sessions);
try testing.expectEqual(@as(i64, 60), config.message_timeout);
try testing.expectEqual(@as(i64, 300), config.session_timeout);
try testing.expect(config.enable_upload);
try testing.expect(config.enable_download);
// Test rate limiter configuration
try testing.expect(config.rate_limiter.max_bandwidth_bps > 0);
try testing.expect(config.rate_limiter.max_concurrent_transfers > 0);
}
test "progress tracker edge cases" {
// Test with zero total bytes
var tracker_zero = ProgressTracker.init(0);
try testing.expectEqual(@as(f32, 1.0), tracker_zero.getDownloadProgress());
try testing.expectEqual(@as(f32, 0.0), tracker_zero.getUploadProgress());
// Test with very large file
const large_bytes: u64 = 1024 * 1024 * 1024; // 1GB
var tracker_large = ProgressTracker.init(large_bytes);
tracker_large.updateDownload(1024 * 1024); // 1MB
const progress = tracker_large.getDownloadProgress();
try testing.expect(progress > 0.0 and progress < 1.0);
// Progress should be very small for large files
try testing.expect(progress < 0.01); // Less than 1%
}
test "rate limiter bandwidth calculations" {
var rate_limiter = RateLimiter.init();
rate_limiter.max_bandwidth_bps = 1024 * 1024; // 1MB/s
// Start a 1MB transfer
rate_limiter.startTransfer(1024 * 1024);
// Bandwidth utilization should reflect the transfer
const utilization = rate_limiter.getBandwidthUtilization();
try testing.expect(utilization > 0.0);
// Complete the transfer
rate_limiter.completeTransfer(1024 * 1024);
// Utilization should decrease
const new_utilization = rate_limiter.getBandwidthUtilization();
try testing.expect(new_utilization <= utilization);
}