Repository navigation
Expand file tree
/
Copy pathtests.zig
More file actions
321 lines (262 loc) · 11.2 KB
/
Copy pathtests.zig
File metadata and controls
321 lines (262 loc) · 11.2 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const std = @import("std");
const bsdiff = @import("bsdiff.zig");
const bspatch = @import("bspatch.zig");
const FileSpec = struct {
path: []const u8,
contents: []const u8,
};
const originalSpecs = [_]FileSpec{
.{
.path = "co(lab)-canary/app/notes.txt",
.contents =
\\Alpha
\\Beta
\\Gamma
\\Delta
,
},
.{
.path = "co(lab)-canary/app/config/settings.json",
.contents =
\\{
\\ "theme": "dark",
\\ "autosave": true,
\\ "fontSize": 14
\\}
,
},
.{
.path = "co(lab)-canary/app/bin/runner",
.contents = "chunk-a\nchunk-b\nchunk-c\n",
},
};
const updatedSpecs = [_]FileSpec{
.{
.path = "co(lab)-canary/app/notes.txt",
.contents =
\\Alpha
\\Beta
\\Gamma
\\Echo
,
},
.{
.path = "co(lab)-canary/app/config/settings.json",
.contents =
\\{
\\ "theme": "light",
\\ "autosave": false,
\\ "fontSize": 16
\\}
,
},
.{
.path = "co(lab)-canary/app/bin/runner",
.contents = "chunk-a\nchunk-b\nchunk-d\n",
},
.{
.path = "co(lab)-canary/app/README.md",
.contents = "# Release Notes\nThis file was added in the update.\n",
},
};
test "bsdiff/bspatch roundtrip across tar archives" {
try runRoundTripPatch(true);
}
fn runRoundTripPatch(useZstd: bool) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const originalTar = try buildTarArchive(allocator, originalSpecs[0..]);
const updatedTar = try buildTarArchive(allocator, updatedSpecs[0..]);
var allocator_handle = allocator;
const patch = try bsdiff.calculateDifferences(&allocator_handle, std.testing.io, originalTar, updatedTar, useZstd, 0);
allocator_handle = allocator;
const patchedTar = try bspatch.applyPatch(&allocator_handle, originalTar, patch);
try std.testing.expectEqualSlices(u8, updatedTar, patchedTar);
}
fn buildTarArchive(allocator: std.mem.Allocator, specs: []const FileSpec) ![]u8 {
var allocating: std.Io.Writer.Allocating = .init(allocator);
defer allocating.deinit();
var tar_writer: std.tar.Writer = .{ .underlying_writer = &allocating.writer };
for (specs) |spec| {
try tar_writer.writeFileBytes(spec.path, spec.contents, .{ .mode = 0o644, .mtime = 0 });
}
// Writes the two trailing zero blocks that terminate a tar archive.
try tar_writer.finishPedantically();
return allocating.toOwnedSlice();
}
// Direct roundtrip on raw binary fixtures where the new file grows and
// shrinks relative to the old file, exercising patches that change the
// output size in both directions.
test "bsdiff/bspatch roundtrip with binary size changes" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var prng = std.Random.DefaultPrng.init(99);
var random = prng.random();
const oldFile = try allocator.alloc(u8, 256 * 1024);
random.bytes(oldFile);
// Grown file: original content with a small edit plus an appended section
const grown = try allocator.alloc(u8, oldFile.len + 32 * 1024);
@memcpy(grown[0..oldFile.len], oldFile);
@memcpy(grown[1024..][0..11], "hello world");
random.bytes(grown[oldFile.len..]);
// Shrunk file: the first half of the original with a small edit
const shrunk = try allocator.dupe(u8, oldFile[0 .. oldFile.len / 2]);
@memcpy(shrunk[2048..][0..9], "truncated");
for ([_][]const u8{ grown, shrunk }) |newFile| {
var allocator_handle = allocator;
const patchData = try bsdiff.calculateDifferences(&allocator_handle, std.testing.io, oldFile, newFile, true, 0);
allocator_handle = allocator;
const patchedFile = try bspatch.applyPatch(&allocator_handle, oldFile, patchData);
try std.testing.expectEqual(newFile.len, patchedFile.len);
try std.testing.expectEqualSlices(u8, newFile, patchedFile);
}
}
// Test with larger files to exercise chunk boundary handling
// This test creates files large enough to trigger multiple chunks (if parallel processing is enabled)
// and includes repeated content patterns that would cause match extension across chunk boundaries
test "bsdiff/bspatch roundtrip with large files" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Create 2MB files with repeated patterns
// This pattern is designed to trigger match extension: common header, different middle, common footer
const fileSize: usize = 2 * 1024 * 1024;
var oldFile = try allocator.alloc(u8, fileSize);
var newFile = try allocator.alloc(u8, fileSize);
// Fill with repeating pattern (this creates opportunities for long matches)
var prng = std.Random.DefaultPrng.init(12345);
var random = prng.random();
// Generate a base pattern
var pattern: [1024]u8 = undefined;
for (0..pattern.len) |i| {
pattern[i] = random.int(u8);
}
// Fill old file with repeated pattern
var pos: usize = 0;
while (pos < fileSize) {
const copyLen = @min(pattern.len, fileSize - pos);
@memcpy(oldFile[pos..][0..copyLen], pattern[0..copyLen]);
pos += copyLen;
}
// Copy to new file
@memcpy(newFile, oldFile);
// Make small changes scattered throughout
// This simulates a real update where most content is the same
const changePositions = [_]usize{ 100, 1000, 50000, 100000, 500000, 1000000, 1500000, 1900000 };
for (changePositions) |changePos| {
if (changePos + 10 < fileSize) {
@memcpy(newFile[changePos..][0..10], "UPDATED!!!");
}
}
var allocator_handle = allocator;
const patchData = try bsdiff.calculateDifferences(&allocator_handle, std.testing.io, oldFile, newFile, true, 0);
allocator_handle = allocator;
const patchedFile = try bspatch.applyPatch(&allocator_handle, oldFile, patchData);
try std.testing.expectEqual(newFile.len, patchedFile.len);
try std.testing.expectEqualSlices(u8, newFile, patchedFile);
}
// Test specifically for the parallel chunk merging bug where:
// 1. Chunk N extends further than chunk N+1 (requires maxActualEndSoFar tracking)
// 2. Kept entries at boundaries need correct oldpos (requires seek-only entry)
//
// The bug manifested when:
// - Chunk 0 extended to position X
// - Chunk 1 extended to position Y > X (further than chunk 0)
// - Chunks 2-5 extended to position Z < Y (less than chunk 1)
// - The code incorrectly used chunkResults[i-1].actualEndPos instead of max seen
// - The diff data was computed with chunk's internal oldpos, but seekBy adjustment
// happened AFTER reading (should be BEFORE via seek-only entry)
test "bsdiff/bspatch parallel chunk boundary handling" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Create 10MB files to trigger multiple chunks (minChunkSize is 1MB)
// With 10MB and up to 8 threads, we get chunks of ~1.25MB each
const fileSize: usize = 10 * 1024 * 1024;
var oldFile = try allocator.alloc(u8, fileSize);
var newFile = try allocator.alloc(u8, fileSize);
// Strategy: Create a pattern where chunks extend differently
// - Region A (0-2MB): identical - chunk 0 processes, may extend
// - Region B (2MB-3MB): DIFFERENT - creates boundary, forces extra data
// - Region C (3MB-7MB): identical - chunks can find matches and extend
// - Region D (7MB-8MB): DIFFERENT - another boundary
// - Region E (8MB-10MB): identical - final region
//
// This pattern causes:
// - Early chunks to extend through the identical regions
// - Later chunks starting in different regions to have different extension patterns
// - Boundaries where kept entries need correct oldpos alignment
var prng = std.Random.DefaultPrng.init(54321);
var random = prng.random();
// Fill oldFile with deterministic pattern
for (0..fileSize) |i| {
oldFile[i] = @truncate((i * 7 + 13) % 256);
}
// Copy to newFile
@memcpy(newFile, oldFile);
// Make regions B and D completely different
const regionB_start: usize = 2 * 1024 * 1024;
const regionB_end: usize = 3 * 1024 * 1024;
const regionD_start: usize = 7 * 1024 * 1024;
const regionD_end: usize = 8 * 1024 * 1024;
for (regionB_start..regionB_end) |i| {
newFile[i] = random.int(u8);
}
for (regionD_start..regionD_end) |i| {
newFile[i] = random.int(u8);
}
// Also make small scattered changes to create more interesting diff patterns
// These changes in the "identical" regions create entries with non-trivial seekBy values
const changePositions = [_]usize{
500 * 1024, // 500KB - in region A
1500 * 1024, // 1.5MB - in region A
4 * 1024 * 1024, // 4MB - in region C
5 * 1024 * 1024, // 5MB - in region C
6 * 1024 * 1024, // 6MB - in region C
9 * 1024 * 1024, // 9MB - in region E
};
for (changePositions) |pos| {
if (pos + 100 < fileSize) {
for (0..100) |j| {
newFile[pos + j] = random.int(u8);
}
}
}
var allocator_handle = allocator;
const patchData = try bsdiff.calculateDifferences(&allocator_handle, std.testing.io, oldFile, newFile, true, 0);
allocator_handle = allocator;
const patchedFile = try bspatch.applyPatch(&allocator_handle, oldFile, patchData);
try std.testing.expectEqual(newFile.len, patchedFile.len);
try std.testing.expectEqualSlices(u8, newFile, patchedFile);
}
// A tripped search-effort budget must still produce a valid patch: degenerate
// windows fall back to literal extra data and matching resumes afterwards, and
// bspatch must reconstruct the new file exactly. Uses multi-chunk sizes so the
// literal windows also exercise the merge path.
test "exhausted search budget degrades to a valid literal patch" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const fileSize: usize = 4 * 1024 * 1024;
var oldFile = try allocator.alloc(u8, fileSize);
var newFile = try allocator.alloc(u8, fileSize);
var prng = std.Random.DefaultPrng.init(97531);
var random = prng.random();
for (0..fileSize) |i| {
oldFile[i] = @truncate((i * 11 + 5) % 256);
}
@memcpy(newFile, oldFile);
for (1 * 1024 * 1024..2 * 1024 * 1024) |i| {
newFile[i] = random.int(u8);
}
var allocator_handle = allocator;
// Effort 1 trips on the random region (which charges ~19 units per byte)
// while the patterned regions still match normally.
const patchData = try bsdiff.calculateDifferences(&allocator_handle, std.testing.io, oldFile, newFile, true, 1);
allocator_handle = allocator;
const patchedFile = try bspatch.applyPatch(&allocator_handle, oldFile, patchData);
try std.testing.expectEqual(newFile.len, patchedFile.len);
try std.testing.expectEqualSlices(u8, newFile, patchedFile);
}