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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! additional CAR v1 codec tests ported from atmos (Go implementation).
//!
//! focuses on error paths, validation, and edge cases not covered
//! by the inline tests in car.zig.
const std = @import("std");
const car = @import("car.zig");
const cbor = @import("cbor.zig");
// === header validation ===
test "reject CAR with version 0" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// build header with version: 0
const root_cid = try cbor.Cid.forDagCbor(alloc, "data");
const header: cbor.Value = .{ .map = &.{
.{ .key = "roots", .value = .{ .array = &.{.{ .cid = root_cid }} } },
.{ .key = "version", .value = .{ .unsigned = 0 } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
try std.testing.expectError(error.InvalidHeader, car.read(alloc, car_aw.written()));
}
test "reject CAR with version 2" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const root_cid = try cbor.Cid.forDagCbor(alloc, "data");
const header: cbor.Value = .{ .map = &.{
.{ .key = "roots", .value = .{ .array = &.{.{ .cid = root_cid }} } },
.{ .key = "version", .value = .{ .unsigned = 2 } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
try std.testing.expectError(error.InvalidHeader, car.read(alloc, car_aw.written()));
}
test "reject CAR with missing version field" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const root_cid = try cbor.Cid.forDagCbor(alloc, "data");
// header with roots but no version
const header: cbor.Value = .{ .map = &.{
.{ .key = "roots", .value = .{ .array = &.{.{ .cid = root_cid }} } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
try std.testing.expectError(error.InvalidHeader, car.read(alloc, car_aw.written()));
}
test "reject CAR with missing roots field" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// header with version but no roots
const header: cbor.Value = .{ .map = &.{
.{ .key = "version", .value = .{ .unsigned = 1 } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
try std.testing.expectError(error.InvalidHeader, car.read(alloc, car_aw.written()));
}
test "reject CAR with empty roots array" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// header with empty roots: []
const header: cbor.Value = .{ .map = &.{
.{ .key = "roots", .value = .{ .array = &.{} } },
.{ .key = "version", .value = .{ .unsigned = 1 } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
try std.testing.expectError(error.InvalidHeader, car.read(alloc, car_aw.written()));
}
// === input edge cases ===
test "reject empty input" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
try std.testing.expectError(error.InvalidVarint, car.read(arena.allocator(), &.{}));
}
test "reject truncated header varint" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
// 0x80 = continuation byte, needs more data
try std.testing.expectError(error.InvalidVarint, car.read(arena.allocator(), &.{0x80}));
}
test "reject truncated header data" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
// header_len = 50 but only 3 bytes of data follow
try std.testing.expectError(error.UnexpectedEof, car.read(arena.allocator(), &.{ 0x32, 0xaa, 0xbb, 0xcc }));
}
// === block edge cases ===
test "reject block with truncated data" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// build a valid CAR with one block, then truncate the block data
const data = try cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "x", .value = .{ .unsigned = 1 } },
} });
const cid = try cbor.Cid.forDagCbor(alloc, data);
const car_bytes = try car.writeAlloc(alloc, .{
.roots = &.{cid},
.blocks = &.{.{ .cid_raw = cid.raw, .data = data }},
});
// truncate the last 5 bytes (removing part of block data)
const truncated = car_bytes[0 .. car_bytes.len - 5];
try std.testing.expectError(error.UnexpectedEof, car.read(alloc, truncated));
}
// === round-trip determinism ===
test "write then read then write produces identical bytes" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// create a multi-block CAR
const data1 = try cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "text", .value = .{ .text = "first block" } },
} });
const data2 = try cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "text", .value = .{ .text = "second block" } },
} });
const cid1 = try cbor.Cid.forDagCbor(alloc, data1);
const cid2 = try cbor.Cid.forDagCbor(alloc, data2);
const original = car.Car{
.roots = &.{cid1},
.blocks = &.{
.{ .cid_raw = cid1.raw, .data = data1 },
.{ .cid_raw = cid2.raw, .data = data2 },
},
};
// write → read → write
const first_write = try car.writeAlloc(alloc, original);
const parsed = try car.read(alloc, first_write);
const second_write = try car.writeAlloc(alloc, parsed);
try std.testing.expectEqualSlices(u8, first_write, second_write);
}
// === multiple roots ===
test "round-trip with multiple roots" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data1 = "block one";
const data2 = "block two";
const cid1 = try cbor.Cid.forDagCbor(alloc, data1);
const cid2 = try cbor.Cid.forDagCbor(alloc, data2);
const original = car.Car{
.roots = &.{ cid1, cid2 },
.blocks = &.{
.{ .cid_raw = cid1.raw, .data = data1 },
.{ .cid_raw = cid2.raw, .data = data2 },
},
};
const car_bytes = try car.writeAlloc(alloc, original);
const parsed = try car.read(alloc, car_bytes);
try std.testing.expectEqual(@as(usize, 2), parsed.roots.len);
try std.testing.expectEqual(@as(usize, 2), parsed.blocks.len);
try std.testing.expectEqualSlices(u8, cid1.digest().?, parsed.roots[0].digest().?);
try std.testing.expectEqualSlices(u8, cid2.digest().?, parsed.roots[1].digest().?);
}
// === CID integrity ===
test "reject single-bit corruption in block data" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = try cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "text", .value = .{ .text = "original data" } },
} });
const cid = try cbor.Cid.forDagCbor(alloc, data);
// write valid CAR, then flip one bit in block content
const car_bytes = try car.writeAlloc(alloc, .{
.roots = &.{cid},
.blocks = &.{.{ .cid_raw = cid.raw, .data = data }},
});
// find the block data in the CAR and corrupt it
var corrupted = try alloc.dupe(u8, car_bytes);
corrupted[corrupted.len - 1] ^= 0x01; // flip last bit
try std.testing.expectError(error.BadBlockHash, car.read(alloc, corrupted));
}
// full-CID verification: forge CID bytes and expect strict rejection.
// helper builds a CAR whose single block carries the given raw CID.
fn carWithCid(alloc: std.mem.Allocator, cid_raw: []const u8, data: []const u8) ![]u8 {
const root = try cbor.Cid.forDagCbor(alloc, data);
return car.writeAlloc(alloc, .{
.roots = &.{root},
.blocks = &.{.{ .cid_raw = cid_raw, .data = data }},
});
}
test "identity-hash CID must match content exactly" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = "identity block";
// CIDv1 identity: version=1, codec=raw(0x55), hash=identity(0x00), len, content
var good: std.Io.Writer.Allocating = .init(alloc);
try good.writer.writeAll(&.{ 0x01, 0x55, 0x00 });
try cbor.writeUvarint(&good.writer, data.len);
try good.writer.writeAll(data);
const parsed = try car.read(alloc, try carWithCid(alloc, good.writer.buffered(), data));
try std.testing.expectEqual(@as(usize, 1), parsed.blocks.len);
// same CID over different content: previously accepted unchecked
const bad_car = try carWithCid(alloc, good.writer.buffered(), "something else!");
try std.testing.expectError(error.BadBlockHash, car.read(alloc, bad_car));
}
test "reject CID with trailing bytes after the digest" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = "some block";
const cid = try cbor.Cid.forDagCbor(alloc, data);
var forged = try alloc.alloc(u8, cid.raw.len + 1);
@memcpy(forged[0..cid.raw.len], cid.raw);
forged[cid.raw.len] = 0xff;
// the reader derives the CID boundary from its varint structure, so
// the extra byte shifts into block content and the digest mismatches
const bad_car = try carWithCid(alloc, forged, data);
try std.testing.expectError(error.BadBlockHash, car.read(alloc, bad_car));
}
test "reject CID with unsupported version" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = "some block";
const cid = try cbor.Cid.forDagCbor(alloc, data);
var forged = try alloc.dupe(u8, cid.raw);
forged[0] = 0x02; // version 2
const bad_car = try carWithCid(alloc, forged, data);
try std.testing.expectError(error.InvalidCid, car.read(alloc, bad_car));
}
test "reject CID with unknown hash function" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = "some block";
const cid = try cbor.Cid.forDagCbor(alloc, data);
var forged = try alloc.dupe(u8, cid.raw);
forged[2] = 0x13; // sha2-512, not supported
const bad_car = try carWithCid(alloc, forged, data);
try std.testing.expectError(error.BadBlockHash, car.read(alloc, bad_car));
}
// === findBlock ===
test "findBlock via hash index" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const data = "test block";
const cid = try cbor.Cid.forDagCbor(alloc, data);
const car_bytes = try car.writeAlloc(alloc, .{
.roots = &.{cid},
.blocks = &.{.{ .cid_raw = cid.raw, .data = data }},
});
const parsed = try car.read(alloc, car_bytes);
// lookup by CID should return block data
const found = car.findBlock(parsed, cid.raw).?;
try std.testing.expectEqualSlices(u8, data, found);
// lookup with wrong CID should return null
const other_cid = try cbor.Cid.forDagCbor(alloc, "other");
try std.testing.expect(car.findBlock(parsed, other_cid.raw) == null);
}
// === size limits ===
test "reject CAR exceeding max size" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// tiny CAR, but set max_size very small
const data = "x";
const cid = try cbor.Cid.forDagCbor(alloc, data);
const car_bytes = try car.writeAlloc(alloc, .{
.roots = &.{cid},
.blocks = &.{.{ .cid_raw = cid.raw, .data = data }},
});
// set max_size smaller than the CAR
try std.testing.expectError(error.BlocksTooLarge, car.readWithOptions(alloc, car_bytes, .{
.max_size = 10,
}));
}
// === varint edge cases (via CAR reader) ===
test "readUvarint rejects varint longer than 10 bytes" {
// 10 continuation bytes + 1 terminator = 11 bytes total
const data = @as([10]u8, @splat(0x80)) ++ [_]u8{0x00};
var pos: usize = 0;
try std.testing.expect(cbor.readUvarint(&data, &pos) == null);
}
test "readUvarint accepts 10-byte varint" {
// max valid: 9 continuation bytes + 1 terminator with bit 0 set
const data = @as([9]u8, @splat(0x80)) ++ [_]u8{0x01};
var pos: usize = 0;
const val = cbor.readUvarint(&data, &pos);
try std.testing.expect(val != null);
try std.testing.expectEqual(@as(usize, 10), pos);
}
// === header-only CAR (roots, no blocks) ===
test "CAR with roots but no blocks" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const root_cid = try cbor.Cid.forDagCbor(alloc, "root");
const header: cbor.Value = .{ .map = &.{
.{ .key = "roots", .value = .{ .array = &.{.{ .cid = root_cid }} } },
.{ .key = "version", .value = .{ .unsigned = 1 } },
} };
const header_bytes = try cbor.encodeAlloc(alloc, header);
var car_aw: std.Io.Writer.Allocating = .init(alloc);
try cbor.writeUvarint(&car_aw.writer, header_bytes.len);
try car_aw.writer.writeAll(header_bytes);
const parsed = try car.read(alloc, car_aw.written());
try std.testing.expectEqual(@as(usize, 1), parsed.roots.len);
try std.testing.expectEqual(@as(usize, 0), parsed.blocks.len);
}
// note: checkAllAllocationFailures cannot be used for car.read because
// readWithOptions uses an internal ArenaAllocator for header CBOR parsing,
// which creates non-deterministic page-level allocations from the backing
// allocator. the errdefer cleanup on roots/blocks/block_index in
// readWithOptions ensures no leaks on OOM; the header arena's defer deinit
// ensures the header Value tree is always freed.