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,307 @@
//! Comprehensive tests for hash operations
//! Tests hash calculation for various sizes, verification, merkle tree operations,
//! concurrent calculations, and corrupted data handling
const std = @import("std");
const testing = std.testing;
const files = @import("files");
const core = @import("core");
test "hash: sha256 calculation for zero bytes" {
const data = "";
const hash = try files.hash.HashUtils.sha256(data);
try testing.expect(hash.len == 32);
}
test "hash: sha256 calculation for 1 byte" {
const data = "X";
const hash = try files.hash.HashUtils.sha256(data);
try testing.expect(hash.len == 32);
}
test "hash: sha256 calculation for 16 bytes" {
const data = "0123456789ABCDEF";
const hash = try files.hash.HashUtils.sha256(data);
try testing.expect(hash.len == 32);
}
test "hash: sha256 calculation for 1KB" {
const data = [_]u8{'A'} ** 1024;
const hash = try files.hash.HashUtils.sha256(&data);
try testing.expect(hash.len == 32);
}
test "hash: sha256 calculation for 1MB" {
const allocator = testing.allocator;
const data = try allocator.alloc(u8, 1024 * 1024);
defer allocator.free(data);
@memset(data, 'B');
const hash = try files.hash.HashUtils.sha256(data);
try testing.expect(hash.len == 32);
}
test "hash: verification with correct hash" {
const data = "Test data for verification";
const hash = try files.hash.HashUtils.sha256(data);
const is_valid = try files.hash.HashUtils.verify(data, hash);
try testing.expect(is_valid);
}
test "hash: verification with incorrect hash" {
const data = "Test data";
const hash = try files.hash.HashUtils.sha256(data);
const different_data = "Different data";
const is_invalid = try files.hash.HashUtils.verify(different_data, hash);
try testing.expect(!is_invalid);
}
test "hash: hash consistency - same input produces same hash" {
const data = "Consistent data";
const hash1 = try files.hash.HashUtils.sha256(data);
const hash2 = try files.hash.HashUtils.sha256(data);
try testing.expect(files.hash.HashUtils.equal(hash1, hash2));
}
test "hash: hash uniqueness - different inputs produce different hashes" {
const data1 = "Data 1";
const data2 = "Data 2";
const hash1 = try files.hash.HashUtils.sha256(data1);
const hash2 = try files.hash.HashUtils.sha256(data2);
try testing.expect(!files.hash.HashUtils.equal(hash1, hash2));
}
test "hash: content hash from multiple pieces" {
const allocator = testing.allocator;
const piece_hashes = try allocator.alloc(files.hash.PieceHash, 3);
defer allocator.free(piece_hashes);
piece_hashes[0] = try files.hash.HashUtils.sha256("piece 0");
piece_hashes[1] = try files.hash.HashUtils.sha256("piece 1");
piece_hashes[2] = try files.hash.HashUtils.sha256("piece 2");
const content_hash = try files.hash.HashUtils.contentHash(piece_hashes);
try testing.expect(content_hash.len == 32);
}
test "hash: hex conversion roundtrip" {
const allocator = testing.allocator;
const data = "Hex conversion test";
const hash = try files.hash.HashUtils.sha256(data);
const hex = try files.hash.HashUtils.toHex(hash, allocator);
defer allocator.free(hex);
try testing.expectEqual(@as(usize, 64), hex.len); // 32 bytes * 2 hex chars
const parsed = try files.hash.HashUtils.fromHex(hex);
try testing.expect(files.hash.HashUtils.equal(hash, parsed));
}
test "hash: invalid hex string rejection" {
// Too short
const result1 = files.hash.HashUtils.fromHex("abc123");
try testing.expectError(core.errors.Error.InvalidData, result1);
// Invalid characters
const result2 = files.hash.HashUtils.fromHex("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
try testing.expectError(core.errors.Error.InvalidData, result2);
}
test "hash: metadata hash calculation" {
const allocator = testing.allocator;
const piece_hashes = try allocator.alloc(files.hash.PieceHash, 2);
defer allocator.free(piece_hashes);
piece_hashes[0] = [_]u8{0x11} ** 32;
piece_hashes[1] = [_]u8{0x22} ** 32;
const metadata = try files.hash.FileMetadata.create(
"metadata.bin",
2048,
piece_hashes,
allocator,
);
const meta_hash = try files.hash.HashUtils.metadataHash(metadata);
try testing.expect(meta_hash.len == 32);
}
test "hash: piece data creation and verification" {
const data = "Piece data content";
const piece = try files.hash.PieceData.create(5, data);
try testing.expectEqual(@as(u32, 5), piece.index);
try testing.expectEqualStrings(data, piece.data);
const is_valid = try piece.verify();
try testing.expect(is_valid);
}
test "hash: incremental verifier" {
const allocator = testing.allocator;
const data = "Incremental verification test data";
const expected_hash = try files.hash.HashUtils.sha256(data);
var verifier = files.hash.IncrementalVerifier.init(
expected_hash,
0,
data.len,
allocator,
);
// Update in chunks
try verifier.update(data[0..10]);
try verifier.update(data[10..20]);
try verifier.update(data[20..]);
const result = try verifier.finalize();
try testing.expect(result.is_valid);
}
test "hash: incremental verifier with wrong data" {
const allocator = testing.allocator;
const expected_data = "Expected data";
const expected_hash = try files.hash.HashUtils.sha256(expected_data);
var verifier = files.hash.IncrementalVerifier.init(
expected_hash,
0,
expected_data.len,
allocator,
);
// Update with wrong data
const wrong_data = "Wrong data!!!";
try verifier.update(wrong_data);
const result = try verifier.finalize();
try testing.expect(!result.is_valid);
}
test "hash: hash pool batch verification" {
const allocator = testing.allocator;
const pool = files.hash.HashPool.init(allocator);
const piece_hashes = try allocator.alloc(files.hash.PieceHash, 3);
defer allocator.free(piece_hashes);
piece_hashes[0] = try files.hash.HashUtils.sha256("piece 0");
piece_hashes[1] = try files.hash.HashUtils.sha256("piece 1");
piece_hashes[2] = try files.hash.HashUtils.sha256("piece 2");
const metadata = try files.hash.FileMetadata.create("batch.bin", 300, piece_hashes, allocator);
var pieces = std.ArrayList(files.hash.PieceData).init(allocator);
defer pieces.deinit();
try pieces.append(try files.hash.PieceData.create(0, "piece 0"));
try pieces.append(try files.hash.PieceData.create(1, "piece 1"));
try pieces.append(try files.hash.PieceData.create(2, "piece 2"));
var batch = try pool.verifyPieces(metadata, pieces.items);
defer batch.deinit();
try testing.expect(batch.isCompleteSuccess());
try testing.expectEqual(@as(f32, 1.0), batch.successRate());
}
test "hash: hash pool batch verification with failures" {
const allocator = testing.allocator;
const pool = files.hash.HashPool.init(allocator);
const piece_hashes = try allocator.alloc(files.hash.PieceHash, 3);
defer allocator.free(piece_hashes);
piece_hashes[0] = try files.hash.HashUtils.sha256("piece 0");
piece_hashes[1] = try files.hash.HashUtils.sha256("piece 1");
piece_hashes[2] = try files.hash.HashUtils.sha256("piece 2");
const metadata = try files.hash.FileMetadata.create("fail_batch.bin", 300, piece_hashes, allocator);
var pieces = std.ArrayList(files.hash.PieceData).init(allocator);
defer pieces.deinit();
try pieces.append(try files.hash.PieceData.create(0, "piece 0")); // Valid
try pieces.append(try files.hash.PieceData.create(1, "CORRUPTED")); // Invalid
try pieces.append(try files.hash.PieceData.create(2, "piece 2")); // Valid
var batch = try pool.verifyPieces(metadata, pieces.items);
defer batch.deinit();
try testing.expect(!batch.isCompleteSuccess());
const failed = try batch.getFailedPieces();
defer allocator.free(failed);
try testing.expectEqual(@as(usize, 1), failed.len);
try testing.expectEqual(@as(u32, 1), failed[0]);
}
test "hash: concurrent hash calculations" {
const allocator = testing.allocator;
// Simulate concurrent calculations by doing multiple in sequence
var hashes = std.ArrayList(files.hash.PieceHash).init(allocator);
defer hashes.deinit();
var i: usize = 0;
while (i < 100) : (i += 1) {
const data = try std.fmt.allocPrint(allocator, "Data {d}", .{i});
defer allocator.free(data);
const hash = try files.hash.HashUtils.sha256(data);
try hashes.append(hash);
}
try testing.expectEqual(@as(usize, 100), hashes.items.len);
// All hashes should be unique
var i_outer: usize = 0;
while (i_outer < hashes.items.len) : (i_outer += 1) {
var j: usize = i_outer + 1;
while (j < hashes.items.len) : (j += 1) {
try testing.expect(!files.hash.HashUtils.equal(hashes.items[i_outer], hashes.items[j]));
}
}
}
test "hash: corrupted data detection" {
const allocator = testing.allocator;
const original_data = "Original uncorrupted data";
const hash = try files.hash.HashUtils.sha256(original_data);
// Corrupt one byte
var corrupted_data = try allocator.dupe(u8, original_data);
defer allocator.free(corrupted_data);
corrupted_data[10] ^= 0xFF;
const is_valid = try files.hash.HashUtils.verify(corrupted_data, hash);
try testing.expect(!is_valid);
}
test "hash: random hash generation for testing" {
const allocator = testing.allocator;
const hash1 = try files.hash.HashUtils.random(allocator);
const hash2 = try files.hash.HashUtils.random(allocator);
// Should be different (with overwhelming probability)
try testing.expect(!files.hash.HashUtils.equal(hash1, hash2));
}