summaryrefslogtreecommitdiffstats
path: root/urn.t
blob: 734bb06db47bc0bf53d92302a389796b88d1ed69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Path qw(make_path remove_tree);
use Digest::SHA qw(sha1_hex);

# Setup testing environment
my $sandbox    = File::Spec->rel2abs("sandbox_env");
my $script_bin = File::Spec->catfile(File::Spec->rel2abs("."), "urn"); 
my $repo_meta  = ".urn";

remove_tree($sandbox);
make_path($sandbox);
chdir($sandbox) or die "Cannot chdir to $sandbox: $!";

sub run_cmd {
	my $cmd = shift;
	my $out = `perl $script_bin $cmd 2>&1`;
	if ($? != 0) {
		die "\nCommand '$cmd' failed with exit code $?:\n$out\n";
	}
	return $out;
}

sub get_head_id {
	my $head_path = File::Spec->catfile($repo_meta, "head");
	return undef unless -f $head_path;
	open(my $fh, '<', $head_path) or return undef;
	my $id = <$fh>;
	close($fh);
	chomp($id //= "");
	$id =~ s/^\s+|\s+$//g;
	return $id;
}

print "Starting tests...\n";
run_cmd("init");

# --- Subtest 1: All-In Integration ---
print "Subtest 1: Initial commit with symlink...\t";
{
	open(my $fh, '>', "file1.txt") or die $!;
	print $fh "Original content\n" x 10;
	close($fh);
	
	symlink("file1.txt", "link1.txt") or die "Symlink failed: $!";
	
	run_cmd("commit -am 'initial'");
	
	my $idx_path = File::Spec->catfile($repo_meta, "index");
	open(my $idx, '<', $idx_path) or die "Could not open $idx_path: $!";
	my %index_hashes;
	while (<$idx>) {
		chomp;
		# Updated parser: split on tabs and trim padding from hashes
		my ($shash, $chash, $bhash, $m, $s, $path) = split(/\t/, $_, 6);
		$shash =~ s/\s+$//; # Remove fixed-width padding
		$index_hashes{$path} = $shash;
	}
	close($idx);

	die "Missing file1" unless $index_hashes{"file1.txt"};
	# Symlink hash is the SHA of the target path string
	die "Symlink hash mismatch" unless $index_hashes{"link1.txt"} eq sha1_hex("file1.txt");
}
print "OK\n";

# --- Subtest 2: Large Source, Small Patch (Text Patching) ---
print "Subtest 2: Large source, small patch...\t";
{
	my $large_p = "large.txt";
	open(my $lf, '>', $large_p) or die $!;
	print $lf "Line $_\n" for 1..1000;
	close($lf);
	
	run_cmd("commit -am 'large file base'");
	
	open($lf, '>>', $large_p) or die $!;
	print $lf "One small change\n";
	close($lf);
	run_cmd("commit -am 'patch commit'");
	
	my $head_id = get_head_id();
	my $rev_path = File::Spec->catfile($repo_meta, "rev", $head_id);
	open(my $rf, '<', $rev_path) or die "Could not open $rev_path: $!";
	my $has_patch = 0;
	while (<$rf>) { $has_patch = 1 if /^patch:[a-f0-9]/; }
	close($rf);
	die "Expected patch in rev $head_id" unless $has_patch;
}
print "OK\n";

# --- Subtest 3: Small Source, Large Patch (Full Store) ---
print "Subtest 3: Small source, large patch...\t";
{
	my $small_p = "small.txt";
	open(my $sf, '>', $small_p) or die $!;
	print $sf "A";
	close($sf);
	run_cmd("commit -am 'small file base'");
	
	open($sf, '>', $small_p) or die $!;
	print $sf "Completely different content" x 200;
	close($sf);
	run_cmd("commit -am 'full store commit'");

	my $restored = run_cmd("show HEAD $small_p");
	my $actual = `cat $small_p`;
	die "Reconstruction mismatch" unless $restored eq $actual;
}
print "OK\n";

# --- Subtest 4: Binary Integrity ---
print "Subtest 4: Binary patch integrity...\t\t";
{
	my $bin_p = "data.bin";
	my $orig_data = pack("C*", map { int(rand(256)) } 1..2048);
	open(my $bf, '>:raw', $bin_p) or die $!;
	print $bf $orig_data;
	close($bf);
	run_cmd("commit -am 'initial bin'");
	
	my $old_rev = get_head_id();

	open($bf, '+<:raw', $bin_p) or die $!;
	seek($bf, 1024, 0);
	print $bf pack("C", 255);
	close($bf);
	run_cmd("commit -am 'mod bin'");

	my $v_old = run_cmd("show $old_rev $bin_p");
	my $v_new = run_cmd("show HEAD $bin_p");
	
	die "Old version corrupted" unless $v_old eq $orig_data;
	die "Binary restore error" unless length($v_new) == 2048;
}
print "OK\n";

# --- Subtest 5: CRUD (Deletion) ---
print "Subtest 5: File deletion...\t\t\t";
{
	unlink("file1.txt");
	run_cmd("commit -am 'delete file1'");
	
	my $head_id = get_head_id();
	my $rev_path = File::Spec->catfile($repo_meta, "rev", $head_id);
	my $rev_data = `cat $rev_path`;
	my ($tree_h) = $rev_data =~ /^tree:([a-f0-9]+)/m;
	
	my $t_obj = File::Spec->catfile($repo_meta, "obj", substr($tree_h, 0, 2), substr($tree_h, 2));
	my $tree_content = `cat $t_obj`;
	die "File1 still in tree after deletion" if $tree_content =~ /file1\.txt/;
}
print "OK\n";

# --- Subtest 6: Dirty Status ---
print "Subtest 6: Dirty state detection...\t\t";
{
	open(my $fh, '>', "dirty.txt") or die $!;
	print $fh "Clean state\n";
	close($fh);
	run_cmd("add dirty.txt");
	
	open($fh, '>>', "dirty.txt") or die $!;
	print $fh "Dirty edit\n";
	close($fh);
	
	my $status = run_cmd("status");
	die "Failed to detect dirty state" unless $status =~ /\[M\].*dirty\.txt.*\(dirty\)/;
}
print "OK\n";

print "\nAll tests passed successfully.\n";