Update tests and documentation: - native/README.md: document C++ native library plans - restart_recovery_test.go: scheduler restart/recovery tests - scheduler_fixture.go: test fixtures for scheduler - hash_test.zig: SHA-NI hash tests (WIP) Improves test coverage and documentation.
58 lines
1.8 KiB
Zig
58 lines
1.8 KiB
Zig
const std = @import("std");
|
|
const hash = @import("../src/utils/hash.zig");
|
|
|
|
test "hash single file" {
|
|
const allocator = std.testing.allocator;
|
|
|
|
// Create a temporary file
|
|
const tmp_dir = std.testing.tmpDir(.{});
|
|
defer tmp_dir.cleanup();
|
|
|
|
const test_content = "hello world";
|
|
try tmp_dir.dir.writeFile("test.txt", test_content);
|
|
|
|
// Hash the file
|
|
var hasher = try hash.DatasetHash.init(allocator, 0);
|
|
defer hasher.deinit();
|
|
|
|
const file_hash = try hasher.hashFile("test.txt");
|
|
try std.testing.expectEqual(@as(usize, 64), file_hash.len);
|
|
}
|
|
|
|
test "hash empty file" {
|
|
const allocator = std.testing.allocator;
|
|
|
|
// Create a temporary empty file
|
|
const tmp_dir = std.testing.tmpDir(.{});
|
|
defer tmp_dir.cleanup();
|
|
|
|
try tmp_dir.dir.writeFile("empty.txt", "");
|
|
|
|
var hasher = try hash.DatasetHash.init(allocator, 0);
|
|
defer hasher.deinit();
|
|
|
|
const file_hash = try hasher.hashFile("empty.txt");
|
|
try std.testing.expectEqual(@as(usize, 64), file_hash.len);
|
|
}
|
|
|
|
test "path validation" {
|
|
// Valid paths
|
|
try hash.validatePath("/home/user/file.txt");
|
|
try hash.validatePath("./relative/path");
|
|
try hash.validatePath("normal_file");
|
|
|
|
// Path traversal should fail
|
|
try std.testing.expectError(error.PathTraversalAttempt, hash.validatePath("../etc/passwd"));
|
|
try std.testing.expectError(error.PathTraversalAttempt, hash.validatePath("foo/../../../etc/passwd"));
|
|
|
|
// Null bytes should fail
|
|
try std.testing.expectError(error.NullByteInPath, hash.validatePath("/path\x00with\x00null"));
|
|
}
|
|
|
|
test "bytes to hex conversion" {
|
|
var hex_buf: [64]u8 = undefined;
|
|
const bytes = [_]u8{ 0xAB, 0xCD, 0xEF, 0x12 };
|
|
hash.bytesToHex(&bytes, &hex_buf);
|
|
|
|
try std.testing.expectEqualStrings("abcdef12", hex_buf[0..8]);
|
|
}
|