849 lines
28 KiB
Zig
849 lines
28 KiB
Zig
//! Tests for file metadata, chunking, and verification
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const fs = std.fs;
|
|
|
|
const FileModule = @import("../src/files/file.zig");
|
|
const { FileInfo, FileChunker, FileWriter, FileUtils, PieceInfo, DEFAULT_PIECE_SIZE } = FileModule;
|
|
|
|
test "file info creation from path" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create a test file
|
|
const test_path = "test_info_file.txt";
|
|
const test_content = "This is test content for file info testing.";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Create file info
|
|
const file_info = try FileInfo.fromPath(test_path, allocator);
|
|
defer file_info.deinit(allocator);
|
|
|
|
try testing.expectEqualSlices(u8, "test_info_file.txt", file_info.name);
|
|
try testing.expect(file_info.size > 0);
|
|
try testing.expectEqualSlices(u8, ".txt", file_info.extension());
|
|
try testing.expect(!file_info.isEmpty());
|
|
|
|
// Test size string formatting
|
|
const size_str = try file_info.sizeString(allocator);
|
|
defer allocator.free(size_str);
|
|
try testing.expect(size_str.len > 0);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file info with empty file" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create empty test file
|
|
const test_path = "empty_test_file.txt";
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
file.close();
|
|
|
|
const file_info = try FileInfo.fromPath(test_path, allocator);
|
|
defer file_info.deinit(allocator);
|
|
|
|
try testing.expect(file_info.isEmpty());
|
|
try testing.expectEqual(@as(u64, 0), file_info.size);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file info extension detection" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test various file extensions
|
|
const test_cases = .{
|
|
.{ "file.txt", ".txt" },
|
|
.{ "document.pdf", ".pdf" },
|
|
.{ "image.png", ".png" },
|
|
.{ "archive.tar.gz", ".gz" },
|
|
.{ "no_extension", "" },
|
|
.{ ".hidden_file", "" },
|
|
};
|
|
|
|
for (test_cases) |test_case| {
|
|
const test_path = test_case[0];
|
|
const expected_ext = test_case[1];
|
|
|
|
// Create test file
|
|
const file = fs.cwd().createFile(test_path, .{}) catch continue;
|
|
file.close();
|
|
|
|
const file_info = try FileInfo.fromPath(test_path, allocator);
|
|
defer file_info.deinit(allocator);
|
|
|
|
try testing.expectEqualSlices(u8, expected_ext, file_info.extension());
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch continue;
|
|
}
|
|
}
|
|
|
|
test "file chunker metadata creation" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Create test file with known content
|
|
const test_path = "chunker_test.bin";
|
|
const test_size = 100 * 1024; // 100KB
|
|
const test_content = [_]u8{0} ** test_size;
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(&test_content) catch return;
|
|
file.close();
|
|
|
|
// Create metadata
|
|
const metadata = try chunker.createMetadata(test_path);
|
|
defer allocator.free(metadata.piece_hashes);
|
|
|
|
try testing.expectEqualSlices(u8, "chunker_test.bin", metadata.name);
|
|
try testing.expectEqual(@as(u64, test_size), metadata.size);
|
|
try testing.expect(metadata.piece_count > 0);
|
|
try testing.expect(metadata.piece_hashes.len == metadata.piece_count);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file chunker piece count calculation" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Test various file sizes
|
|
const test_cases = .{
|
|
.{ @as(u64, 0), @as(u32, 0) },
|
|
.{ @as(u64, 1), @as(u32, 1) },
|
|
.{ DEFAULT_PIECE_SIZE, @as(u32, 1) },
|
|
.{ DEFAULT_PIECE_SIZE + 1, @as(u32, 2) },
|
|
.{ 2 * DEFAULT_PIECE_SIZE, @as(u32, 2) },
|
|
.{ 3 * DEFAULT_PIECE_SIZE + 1, @as(u32, 4) },
|
|
};
|
|
|
|
for (test_cases) |test_case| {
|
|
const file_size = test_case[0];
|
|
const expected_count = test_case[1];
|
|
|
|
const piece_count = chunker.calculatePieceCount(file_size) orelse return error.CalculationFailed;
|
|
try testing.expectEqual(expected_count, piece_count);
|
|
}
|
|
}
|
|
|
|
test "file chunker piece info calculation" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
const file_size: u64 = 50 * 1024 * 1024; // 50MB
|
|
const piece_count = chunker.calculatePieceCount(file_size) orelse return error.CalculationFailed;
|
|
|
|
// Test first piece
|
|
const first_piece = chunker.getPieceInfo(file_size, 0);
|
|
try testing.expectEqual(@as(u32, 0), first_piece.index);
|
|
try testing.expectEqual(@as(u64, 0), first_piece.offset);
|
|
try testing.expectEqual(@as(usize, DEFAULT_PIECE_SIZE), first_piece.size);
|
|
|
|
// Test last piece
|
|
const last_index = piece_count - 1;
|
|
const last_piece = chunker.getPieceInfo(file_size, last_index);
|
|
try testing.expectEqual(last_index, last_piece.index);
|
|
try testing.expectEqual(last_index * DEFAULT_PIECE_SIZE, last_piece.offset);
|
|
|
|
// Last piece should be smaller or equal to piece size
|
|
try testing.expect(last_piece.size <= DEFAULT_PIECE_SIZE);
|
|
}
|
|
|
|
test "file chunker read piece" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Create test file with specific content
|
|
const test_path = "read_piece_test.bin";
|
|
const piece_data = "This is test piece data for reading!";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(piece_data) catch return;
|
|
file.close();
|
|
|
|
// Read the piece
|
|
const read_data = try chunker.readPiece(test_path, 0, piece_data.len);
|
|
defer allocator.free(read_data);
|
|
|
|
try testing.expectEqualSlices(u8, piece_data, read_data);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file chunker write piece" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Create test file
|
|
const test_path = "write_piece_test.bin";
|
|
const initial_size = 1024;
|
|
const initial_content = [_]u8{0} ** initial_size;
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(&initial_content) catch return;
|
|
file.close();
|
|
|
|
// Write piece data
|
|
const piece_data = "Written piece data content";
|
|
try chunker.writePiece(test_path, 0, piece_data);
|
|
|
|
// Read back and verify
|
|
const read_data = try chunker.readPiece(test_path, 0, piece_data.len);
|
|
defer allocator.free(read_data);
|
|
|
|
try testing.expectEqualSlices(u8, piece_data, read_data);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file chunker verify file integrity" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Create test file
|
|
const test_path = "verify_integrity_test.bin";
|
|
const test_content = "Data for integrity verification testing";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Create metadata
|
|
const metadata = try chunker.createMetadata(test_path);
|
|
defer allocator.free(metadata.piece_hashes);
|
|
|
|
// Verify file integrity
|
|
const is_valid = try chunker.verifyFile(test_path, metadata);
|
|
try testing.expect(is_valid);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file writer initialization" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test creating writer with new file
|
|
const test_path = "writer_init_test.bin";
|
|
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;
|
|
}
|
|
|
|
test "file writer piece writing" {
|
|
const allocator = testing.allocator;
|
|
|
|
const test_path = "writer_piece_test.bin";
|
|
var writer = try FileWriter.init(test_path, allocator);
|
|
defer writer.deinit();
|
|
|
|
// Write test pieces
|
|
const piece_data1 = "First piece of data";
|
|
const piece_data2 = "Second piece of data";
|
|
|
|
try writer.writePiece(0, piece_data1, piece_data1.len);
|
|
try writer.writePiece(1, piece_data2, piece_data2.len);
|
|
try writer.flush();
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file writer write at offset" {
|
|
const allocator = testing.allocator;
|
|
|
|
const test_path = "writer_offset_test.bin";
|
|
var writer = try FileWriter.init(test_path, allocator);
|
|
defer writer.deinit();
|
|
|
|
// Write data at specific offsets
|
|
const offset1: u64 = 1000;
|
|
const data1 = "Data at offset 1000";
|
|
|
|
const offset2: u64 = 2000;
|
|
const data2 = "Data at offset 2000";
|
|
|
|
try writer.writeAt(offset1, data1);
|
|
try writer.writeAt(offset2, data2);
|
|
try writer.flush();
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file utilities size formatting" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test various sizes
|
|
const test_cases = .{
|
|
.{ @as(u64, 0), "0B" },
|
|
.{ @as(u64, 1024), "1K" },
|
|
.{ @as(u64, 1024 * 1024), "1M" },
|
|
.{ @as(u64, 1024 * 1024 * 1024), "1G" },
|
|
.{ @as(u64, 1536), "1K" }, // Should round down
|
|
};
|
|
|
|
for (test_cases) |test_case| {
|
|
const size = test_case[0];
|
|
const expected_suffix = test_case[1];
|
|
|
|
const size_str = try FileUtils.formatSize(size, allocator);
|
|
defer allocator.free(size_str);
|
|
|
|
try testing.expect(std.mem.indexOf(u8, size_str, expected_suffix) != null);
|
|
}
|
|
}
|
|
|
|
test "file utilities file operations" {
|
|
// Test file existence checking
|
|
const existing_file = "test_files/existing.txt";
|
|
|
|
// Create test file first
|
|
const file = fs.cwd().createFile(existing_file, .{}) catch return;
|
|
file.close();
|
|
|
|
// Test file exists
|
|
try testing.expect(FileUtils.fileExists(existing_file));
|
|
|
|
// Test non-existent file
|
|
try testing.expect(!FileUtils.fileExists("test_files/nonexistent.txt"));
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(existing_file) catch return;
|
|
}
|
|
|
|
test "file utilities directory operations" {
|
|
const test_dir = "test_files/temp_dir";
|
|
|
|
// Ensure directory exists (should create it)
|
|
try FileUtils.ensureDir(test_dir);
|
|
try testing.expect(FileUtils.fileExists(test_dir)); // Directories should return true for exists
|
|
|
|
// Clean up directory
|
|
fs.cwd().deleteDir(test_dir) catch return;
|
|
}
|
|
|
|
test "file utilities temp path generation" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test various file types
|
|
const test_cases = .{
|
|
"file.txt",
|
|
"document.pdf",
|
|
"image.png.gz",
|
|
"no_extension",
|
|
};
|
|
|
|
for (test_cases) |original_path| {
|
|
const temp_path = try FileUtils.getTempPath(original_path, allocator);
|
|
defer allocator.free(temp_path);
|
|
|
|
// Should end with .tmp
|
|
try testing.expect(std.mem.endsWith(u8, temp_path, ".tmp"));
|
|
|
|
// Should contain original basename
|
|
const basename = std.fs.path.basename(original_path);
|
|
try testing.expect(std.mem.indexOf(u8, temp_path, basename) != null);
|
|
}
|
|
}
|
|
|
|
test "file utilities hash file" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file
|
|
const test_path = "hash_file_test.txt";
|
|
const test_content = "Content for hash file testing";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Hash the file
|
|
const hash = try FileUtils.hashFile(test_path, allocator);
|
|
try testing.expectEqual(@as(usize, 32), hash.len);
|
|
|
|
// Hash should be consistent
|
|
const hash2 = try FileUtils.hashFile(test_path, allocator);
|
|
try testing.expect(mem.eql(u8, &hash, &hash2));
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file utilities file stats" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file
|
|
const test_path = "stats_test.txt";
|
|
const test_content = "File for stats testing";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Get file stats
|
|
const file_info = try FileUtils.getFileStats(test_path);
|
|
defer file_info.deinit(allocator);
|
|
|
|
try testing.expectEqualSlices(u8, "stats_test.txt", file_info.name);
|
|
try testing.expect(file_info.size > 0);
|
|
try testing.expect(!file_info.isEmpty());
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "piece info calculation edge cases" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Test with empty file
|
|
const empty_piece = chunker.getPieceInfo(0, 0);
|
|
try testing.expectEqual(@as(u32, 0), empty_piece.index);
|
|
try testing.expectEqual(@as(u64, 0), empty_piece.offset);
|
|
try testing.expectEqual(@as(usize, 0), empty_piece.size);
|
|
|
|
// Test with file smaller than piece size
|
|
const small_file_size: u64 = 1024;
|
|
const small_piece = chunker.getPieceInfo(small_file_size, 0);
|
|
try testing.expectEqual(@as(usize, 1024), small_piece.size);
|
|
|
|
// Test with exact piece size
|
|
const exact_piece = chunker.getPieceInfo(DEFAULT_PIECE_SIZE, 0);
|
|
try testing.expectEqual(@as(usize, DEFAULT_PIECE_SIZE), exact_piece.size);
|
|
|
|
// Test piece index beyond file size
|
|
const out_of_bounds_piece = chunker.getPieceInfo(1024, 10);
|
|
try testing.expectEqual(@as(u64, 10 * DEFAULT_PIECE_SIZE), out_of_bounds_piece.offset);
|
|
try testing.expect(out_of_bounds_piece.size <= DEFAULT_PIECE_SIZE);
|
|
}
|
|
|
|
test "file chunker with different piece sizes" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test with smaller piece size
|
|
const small_piece_size: u64 = 1024; // 1KB pieces
|
|
const chunker = FileChunker.init(small_piece_size, allocator);
|
|
|
|
const test_path = "small_piece_test.bin";
|
|
const test_size: u64 = 10 * 1024; // 10KB
|
|
const test_content = [_]u8{0} ** test_size;
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(&test_content) catch return;
|
|
file.close();
|
|
|
|
// Create metadata
|
|
const metadata = try chunker.createMetadata(test_path);
|
|
defer allocator.free(metadata.piece_hashes);
|
|
|
|
// Should have 10 pieces of 1KB each
|
|
try testing.expectEqual(@as(u32, 10), metadata.piece_count);
|
|
|
|
// Verify piece sizes
|
|
for (0..metadata.piece_count) |i| {
|
|
const piece_size = metadata.pieceSize(@as(u32, @intCast(i)));
|
|
if (i < 9) {
|
|
try testing.expectEqual(@as(usize, small_piece_size), piece_size);
|
|
} else {
|
|
// Last piece
|
|
try testing.expect(piece_size <= small_piece_size);
|
|
}
|
|
}
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file chunker error handling" {
|
|
const allocator = testing.allocator;
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
|
|
// Test reading non-existent file
|
|
const non_existent = "non_existent_file.bin";
|
|
try testing.expectError(error.FileError, chunker.readPiece(non_existent, 0, 1024));
|
|
|
|
// Test writing to non-existent file (should fail)
|
|
const write_data = "Test write data";
|
|
try testing.expectError(error.FileError, chunker.writePiece(non_existent, 0, write_data));
|
|
|
|
// Test verifying non-existent file
|
|
const fake_metadata = FileMetadata{
|
|
.name = "fake",
|
|
.size = 1024,
|
|
.piece_count = 1,
|
|
.piece_hashes = &[_][32]u8{[_]u8{0} ** 32},
|
|
.created_at = 0,
|
|
.modified_at = 0,
|
|
};
|
|
try testing.expectError(error.FileError, chunker.verifyFile(non_existent, fake_metadata));
|
|
}
|
|
|
|
test "file reader streaming operations" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file
|
|
const test_path = "reader_stream_test.txt";
|
|
const test_content = "This is test content for streaming reader operations.";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Test file reader initialization
|
|
var reader = try FileReader.init(test_path, allocator, 1024);
|
|
defer reader.deinit();
|
|
|
|
// Test reading in chunks
|
|
var total_read: usize = 0;
|
|
try reader.readInChunks(fn(data: []const u8) Error!void {
|
|
total_read += data.len;
|
|
});
|
|
|
|
try testing.expectEqual(test_content.len, total_read);
|
|
|
|
// Test reading specific range
|
|
const range_data = try reader.readRange(0, 10);
|
|
defer allocator.free(range_data);
|
|
try testing.expectEqualSlices(u8, "This is tes", range_data);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file writer assembly operations" {
|
|
const allocator = testing.allocator;
|
|
|
|
const test_path = "writer_assembly_test.bin";
|
|
var writer = try FileWriter.init(test_path, allocator);
|
|
defer writer.deinit();
|
|
|
|
// Write multiple pieces out of order
|
|
const piece2_data = "Second piece";
|
|
const piece0_data = "First piece ";
|
|
const piece1_data = "and third";
|
|
|
|
try writer.writePiece(2, piece2_data, piece2_data.len);
|
|
try writer.writePiece(0, piece0_data, piece0_data.len);
|
|
try writer.writePiece(1, piece1_data, piece1_data.len);
|
|
try writer.flush();
|
|
|
|
// Verify the file was written correctly
|
|
var reader = try FileReader.init(test_path, allocator, 1024);
|
|
defer reader.deinit();
|
|
|
|
var content = std.ArrayList(u8).init(allocator);
|
|
defer content.deinit();
|
|
|
|
try reader.readInChunks(fn(data: []const u8) Error!void {
|
|
try content.appendSlice(data);
|
|
});
|
|
|
|
const expected_content = "First piece and thirdSecond piece";
|
|
try testing.expectEqualSlices(u8, expected_content, content.items);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file utils copy with progress" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create source file
|
|
const source_path = "copy_source_test.txt";
|
|
const test_content = "Content for copy testing with progress tracking.";
|
|
|
|
const file = fs.cwd().createFile(source_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
const dest_path = "copy_dest_test.txt";
|
|
|
|
var progress_called = false;
|
|
var total_size: u64 = 0;
|
|
var current_progress: u64 = 0;
|
|
|
|
try FileUtils.copyFileWithProgress(
|
|
source_path,
|
|
dest_path,
|
|
allocator,
|
|
fn(current: u64, total: u64) void {
|
|
progress_called = true;
|
|
current_progress = current;
|
|
total_size = total;
|
|
},
|
|
);
|
|
|
|
try testing.expect(progress_called);
|
|
try testing.expectEqual(@as(u64, test_content.len), total_size);
|
|
try testing.expectEqual(@as(u64, test_content.len), current_progress);
|
|
|
|
// Verify copied file
|
|
try testing.expect(FileUtils.fileExists(dest_path));
|
|
|
|
const dest_stats = try FileUtils.getFileStats(dest_path);
|
|
try testing.expectEqual(@as(u64, test_content.len), dest_stats.size);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(source_path) catch return;
|
|
fs.cwd().deleteFile(dest_path) catch return;
|
|
}
|
|
|
|
test "file utils backup creation" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create original file
|
|
const original_path = "backup_original_test.txt";
|
|
const test_content = "Content for backup testing.";
|
|
|
|
const file = fs.cwd().createFile(original_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Create backup
|
|
const backup_path = try FileUtils.createBackup(original_path, allocator);
|
|
defer allocator.free(backup_path);
|
|
|
|
try testing.expect(FileUtils.fileExists(backup_path));
|
|
try testing.expect(std.mem.endsWith(u8, backup_path, ".backup"));
|
|
|
|
// Verify backup content
|
|
const backup_stats = try FileUtils.getFileStats(backup_path);
|
|
const original_stats = try FileUtils.getFileStats(original_path);
|
|
try testing.expectEqual(original_stats.size, backup_stats.size);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(original_path) catch return;
|
|
fs.cwd().deleteFile(backup_path) catch return;
|
|
}
|
|
|
|
test "file utils checksum calculation" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file
|
|
const test_path = "checksum_test.txt";
|
|
const test_content = "Content for checksum verification.";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Calculate checksum
|
|
const checksum = try FileUtils.calculateChecksum(test_path, allocator);
|
|
try testing.expectEqual(@as(usize, 16), checksum.len);
|
|
|
|
// Checksum should be consistent
|
|
const checksum2 = try FileUtils.calculateChecksum(test_path, allocator);
|
|
try testing.expect(mem.eql(u8, &checksum, &checksum2));
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file utils file completion check" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file
|
|
const test_path = "completion_test.txt";
|
|
const test_content = "Content for completion testing.";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
// Create metadata
|
|
const chunker = FileChunker.init(DEFAULT_PIECE_SIZE, allocator);
|
|
const metadata = try chunker.createMetadata(test_path);
|
|
defer allocator.free(metadata.piece_hashes);
|
|
|
|
// File should be complete
|
|
const is_complete = try FileUtils.isFileComplete(test_path, metadata);
|
|
try testing.expect(is_complete);
|
|
|
|
// Test with wrong size
|
|
const wrong_metadata = FileMetadata{
|
|
.name = metadata.name,
|
|
.size = metadata.size + 1, // Wrong size
|
|
.piece_count = metadata.piece_count,
|
|
.piece_hashes = metadata.piece_hashes,
|
|
.created_at = metadata.created_at,
|
|
.modified_at = metadata.modified_at,
|
|
};
|
|
|
|
const is_not_complete = try FileUtils.isFileComplete(test_path, wrong_metadata);
|
|
try testing.expect(!is_not_complete);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "file chunker optimal piece count calculation" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test small file (should use fewer pieces)
|
|
const small_file_size: u64 = 1024 * 1024; // 1MB
|
|
const optimal_count = try FileChunker.calculateOptimalPieceCount(small_file_size, 10 * 1024 * 1024, allocator);
|
|
try testing.expect(optimal_count >= 1);
|
|
|
|
// Test large file (should adjust based on memory constraints)
|
|
const large_file_size: u64 = 100 * 1024 * 1024; // 100MB
|
|
const optimal_large_count = try FileChunker.calculateOptimalPieceCount(large_file_size, 1024 * 1024, allocator);
|
|
try testing.expect(optimal_large_count >= 1);
|
|
}
|
|
|
|
test "file type detection and classification" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test different file types
|
|
const test_cases = .{
|
|
.{ "document.txt", FileType.text },
|
|
.{ "image.png", FileType.image },
|
|
.{ "video.mp4", FileType.video },
|
|
.{ "audio.mp3", FileType.audio },
|
|
.{ "archive.zip", FileType.archive },
|
|
.{ "program.exe", FileType.binary },
|
|
.{ "readme.md", FileType.text },
|
|
};
|
|
|
|
for (test_cases) |test_case| {
|
|
const file_path = test_case[0];
|
|
const expected_type = test_case[1];
|
|
|
|
const detected_type = FileUtils.getFileType(file_path);
|
|
try testing.expectEqual(expected_type, detected_type);
|
|
try testing.expect(detected_type.description().len > 0);
|
|
|
|
// Test compressed property
|
|
if (detected_type == FileType.archive) {
|
|
try testing.expect(detected_type.isCompressed());
|
|
} else {
|
|
try testing.expect(!detected_type.isCompressed());
|
|
}
|
|
}
|
|
}
|
|
|
|
test "file compression ratio estimation" {
|
|
// Test compression ratio estimation for different file types
|
|
const text_ratio = FileUtils.estimateCompressionRatio("document.txt");
|
|
try testing.expect(text_ratio >= 0.0 and text_ratio <= 1.0);
|
|
try testing.expect(text_ratio < 0.5); // Text should compress well
|
|
|
|
const image_ratio = FileUtils.estimateCompressionRatio("image.jpg");
|
|
try testing.expect(image_ratio > 0.7); // Images don't compress much
|
|
|
|
const video_ratio = FileUtils.estimateCompressionRatio("video.mp4");
|
|
try testing.expect(video_ratio > 0.8); // Videos are already compressed
|
|
|
|
const archive_ratio = FileUtils.estimateCompressionRatio("archive.zip");
|
|
try testing.expect(archive_ratio == 1.0); // Archives are already compressed
|
|
}
|
|
|
|
test "file compression operations" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create source file
|
|
const source_path = "compress_source_test.txt";
|
|
const test_content = "This is content for compression testing. ".**100; // Repeat to see compression effect
|
|
|
|
const file = fs.cwd().createFile(source_path, .{}) catch return;
|
|
_ = file.write(test_content) catch return;
|
|
file.close();
|
|
|
|
const compressed_path = "compress_dest_test.txt";
|
|
|
|
// Test compression
|
|
try FileUtils.compressFile(source_path, compressed_path, allocator);
|
|
try testing.expect(FileUtils.fileExists(compressed_path));
|
|
|
|
// Test decompression
|
|
const decompressed_path = "decompress_dest_test.txt";
|
|
try FileUtils.decompressFile(compressed_path, decompressed_path, allocator);
|
|
try testing.expect(FileUtils.fileExists(decompressed_path));
|
|
|
|
// Verify decompressed content matches original
|
|
const source_stats = try FileUtils.getFileStats(source_path);
|
|
const decompressed_stats = try FileUtils.getFileStats(decompressed_path);
|
|
try testing.expectEqual(source_stats.size, decompressed_stats.size);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(source_path) catch return;
|
|
fs.cwd().deleteFile(compressed_path) catch return;
|
|
fs.cwd().deleteFile(decompressed_path) catch return;
|
|
}
|
|
|
|
test "file reader range operations" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Create test file with known content
|
|
const test_path = "range_test.txt";
|
|
const content = "0123456789ABCDEFGHIJ";
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(content) catch return;
|
|
file.close();
|
|
|
|
var reader = try FileReader.init(test_path, allocator, 1024);
|
|
defer reader.deinit();
|
|
|
|
// Test reading from beginning
|
|
const start_range = try reader.readRange(0, 5);
|
|
defer allocator.free(start_range);
|
|
try testing.expectEqualSlices(u8, "01234", start_range);
|
|
|
|
// Test reading from middle
|
|
const middle_range = try reader.readRange(5, 5);
|
|
defer allocator.free(middle_range);
|
|
try testing.expectEqualSlices(u8, "56789", middle_range);
|
|
|
|
// Test reading from end
|
|
const end_range = try reader.readRange(15, 5);
|
|
defer allocator.free(end_range);
|
|
try testing.expectEqualSlices(u8, "FGHIJ", end_range);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
}
|
|
|
|
test "variable chunk size file chunker" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Initialize variable size chunker
|
|
const variable_chunker = FileChunker.initVariable(allocator);
|
|
|
|
// Test that it initializes with a larger piece size
|
|
try testing.expect(variable_chunker.piece_size >= DEFAULT_PIECE_SIZE);
|
|
|
|
// Test metadata creation with variable sizes
|
|
const test_path = "variable_chunker_test.bin";
|
|
const test_size = 10 * 1024; // 10KB
|
|
const test_content = [_]u8{0} ** test_size;
|
|
|
|
const file = fs.cwd().createFile(test_path, .{}) catch return;
|
|
_ = file.write(&test_content) catch return;
|
|
file.close();
|
|
|
|
const metadata = try variable_chunker.createMetadata(test_path);
|
|
defer allocator.free(metadata.piece_hashes);
|
|
|
|
// Should work with variable chunking
|
|
try testing.expect(metadata.piece_count > 0);
|
|
try testing.expect(metadata.piece_hashes.len == metadata.piece_count);
|
|
|
|
// Clean up
|
|
fs.cwd().deleteFile(test_path) catch return;
|
|
} |