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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
const std = @import("std");
const cbor = @import("cbor.zig");
const readArg = cbor.readArg;
const Arg = cbor.Arg;
const readText = cbor.readText;
const readBytes = cbor.readBytes;
const readUint = cbor.readUint;
const readInt = cbor.readInt;
const readBool = cbor.readBool;
const readNull = cbor.readNull;
const readMapHeader = cbor.readMapHeader;
const readArrayHeader = cbor.readArrayHeader;
const readCidLink = cbor.readCidLink;
// ---------------------------------------------------------------------------
// Inline values 0-23 (major type 0 = unsigned)
// ---------------------------------------------------------------------------
test "readArg: inline value 0" {
const data = [_]u8{0x00}; // major 0, additional 0
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 0), arg.val);
try std.testing.expectEqual(@as(usize, 1), arg.end);
}
test "readArg: inline value 1" {
const data = [_]u8{0x01};
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 1), arg.val);
try std.testing.expectEqual(@as(usize, 1), arg.end);
}
test "readArg: inline value 23" {
const data = [_]u8{0x17}; // major 0, additional 23
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 23), arg.val);
try std.testing.expectEqual(@as(usize, 1), arg.end);
}
// ---------------------------------------------------------------------------
// 1-byte value (additional info = 24)
// ---------------------------------------------------------------------------
test "readArg: 1-byte value 24" {
const data = [_]u8{ 0x18, 24 }; // major 0, additional 24, payload 24
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 24), arg.val);
try std.testing.expectEqual(@as(usize, 2), arg.end);
}
test "readArg: 1-byte value 255" {
const data = [_]u8{ 0x18, 0xff }; // major 0, additional 24, payload 255
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 255), arg.val);
try std.testing.expectEqual(@as(usize, 2), arg.end);
}
// ---------------------------------------------------------------------------
// 2-byte value (additional info = 25)
// ---------------------------------------------------------------------------
test "readArg: 2-byte value 256" {
const data = [_]u8{ 0x19, 0x01, 0x00 }; // major 0, additional 25, payload 256 big-endian
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 256), arg.val);
try std.testing.expectEqual(@as(usize, 3), arg.end);
}
// ---------------------------------------------------------------------------
// 4-byte value (additional info = 26)
// ---------------------------------------------------------------------------
test "readArg: 4-byte value 65536" {
const data = [_]u8{ 0x1a, 0x00, 0x01, 0x00, 0x00 }; // major 0, additional 26, payload 65536
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 65536), arg.val);
try std.testing.expectEqual(@as(usize, 5), arg.end);
}
// ---------------------------------------------------------------------------
// 8-byte value (additional info = 27)
// ---------------------------------------------------------------------------
test "readArg: 8-byte value 0x100000000" {
// major 0, additional 27, payload 0x00_00_00_01_00_00_00_00
const data = [_]u8{ 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
const arg = try readArg(&data, 0);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 0x100000000), arg.val);
try std.testing.expectEqual(@as(usize, 9), arg.end);
}
// ---------------------------------------------------------------------------
// Reject non-minimal encodings
// ---------------------------------------------------------------------------
test "readArg: reject non-minimal 0 encoded as 1-byte" {
// Value 0 encoded with additional=24 payload=0x00 (should be inline 0)
const data = [_]u8{ 0x18, 0x00 };
try std.testing.expectError(error.NonMinimalEncoding, readArg(&data, 0));
}
test "readArg: reject non-minimal 255 encoded as 2-byte" {
// Value 255 encoded with additional=25 payload=0x00ff (should be 1-byte)
const data = [_]u8{ 0x19, 0x00, 0xff };
try std.testing.expectError(error.NonMinimalEncoding, readArg(&data, 0));
}
// ---------------------------------------------------------------------------
// Reject truncated data
// ---------------------------------------------------------------------------
test "readArg: reject truncated 2-byte" {
// additional=25 needs 2 payload bytes, but only 1 provided
const data = [_]u8{ 0x19, 0x01 };
try std.testing.expectError(error.UnexpectedEof, readArg(&data, 0));
}
// ---------------------------------------------------------------------------
// Reject reserved additional info (28-30)
// ---------------------------------------------------------------------------
test "readArg: reject reserved additional info 28" {
const data = [_]u8{0x1c}; // major 0, additional 28
try std.testing.expectError(error.ReservedAdditionalInfo, readArg(&data, 0));
}
// ---------------------------------------------------------------------------
// Reject indefinite length (additional info = 31)
// ---------------------------------------------------------------------------
test "readArg: reject indefinite length 31" {
const data = [_]u8{0x5f}; // major 2 (byte string), additional 31
try std.testing.expectError(error.IndefiniteLength, readArg(&data, 0));
}
// ---------------------------------------------------------------------------
// Non-zero start position
// ---------------------------------------------------------------------------
test "readArg: non-zero start position" {
// prefix byte 0xAA, then a valid CBOR unsigned 24 at position 1
const data = [_]u8{ 0xaa, 0x18, 24 };
const arg = try readArg(&data, 1);
try std.testing.expectEqual(@as(u3, 0), arg.major);
try std.testing.expectEqual(@as(u64, 24), arg.val);
try std.testing.expectEqual(@as(usize, 3), arg.end);
}
test "readArg: non-zero start position with different major type" {
// At position 2: 0x63 = major 3 (text string), additional 3 (inline length 3)
const data = [_]u8{ 0x00, 0x00, 0x63, 0x66, 0x6f, 0x6f };
const arg = try readArg(&data, 2);
try std.testing.expectEqual(@as(u3, 3), arg.major);
try std.testing.expectEqual(@as(u64, 3), arg.val);
try std.testing.expectEqual(@as(usize, 3), arg.end);
}
// ---------------------------------------------------------------------------
// EOF at start position
// ---------------------------------------------------------------------------
test "readArg: empty data returns UnexpectedEof" {
const data = [_]u8{};
try std.testing.expectError(error.UnexpectedEof, readArg(&data, 0));
}
test "readArg: pos beyond data returns UnexpectedEof" {
const data = [_]u8{0x00};
try std.testing.expectError(error.UnexpectedEof, readArg(&data, 1));
}
// ===========================================================================
// readText
// ===========================================================================
test "readText: short string 'hello'" {
// 0x65 = major 3 (text), length 5
const data = [_]u8{ 0x65, 'h', 'e', 'l', 'l', 'o' };
const result = try readText(&data, 0);
try std.testing.expectEqualStrings("hello", result.val);
try std.testing.expectEqual(@as(usize, 6), result.end);
}
test "readText: empty string" {
const data = [_]u8{0x60}; // major 3, length 0
const result = try readText(&data, 0);
try std.testing.expectEqual(@as(usize, 0), result.val.len);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readText: reject non-text major" {
const data = [_]u8{ 0x45, 'h', 'e', 'l', 'l', 'o' }; // major 2 (bytes), length 5
try std.testing.expectError(error.WrongType, readText(&data, 0));
}
test "readText: reject invalid UTF-8" {
// 0x62 = major 3, length 2; 0xff 0xfe is invalid UTF-8
const data = [_]u8{ 0x62, 0xff, 0xfe };
try std.testing.expectError(error.InvalidUtf8, readText(&data, 0));
}
// ===========================================================================
// readBytes
// ===========================================================================
test "readBytes: 3-byte string" {
const data = [_]u8{ 0x43, 0x01, 0x02, 0x03 }; // major 2, length 3
const result = try readBytes(&data, 0);
try std.testing.expectEqual(@as(usize, 3), result.val.len);
try std.testing.expectEqual(@as(u8, 0x01), result.val[0]);
try std.testing.expectEqual(@as(u8, 0x02), result.val[1]);
try std.testing.expectEqual(@as(u8, 0x03), result.val[2]);
try std.testing.expectEqual(@as(usize, 4), result.end);
}
test "readBytes: empty bytes" {
const data = [_]u8{0x40}; // major 2, length 0
const result = try readBytes(&data, 0);
try std.testing.expectEqual(@as(usize, 0), result.val.len);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
// ===========================================================================
// readUint
// ===========================================================================
test "readUint: value 1000" {
// 0x19 = major 0, additional 25 (2-byte), 0x03e8 = 1000
const data = [_]u8{ 0x19, 0x03, 0xe8 };
const result = try readUint(&data, 0);
try std.testing.expectEqual(@as(u64, 1000), result.val);
try std.testing.expectEqual(@as(usize, 3), result.end);
}
test "readUint: reject negative" {
const data = [_]u8{0x20}; // major 1, value 0 => -1
try std.testing.expectError(error.WrongType, readUint(&data, 0));
}
// ===========================================================================
// readInt
// ===========================================================================
test "readInt: positive 42" {
// 0x18 = major 0, additional 24 (1-byte), 42
const data = [_]u8{ 0x18, 42 };
const result = try readInt(&data, 0);
try std.testing.expectEqual(@as(i64, 42), result.val);
try std.testing.expectEqual(@as(usize, 2), result.end);
}
test "readInt: negative -10" {
// major 1, value 9 => -1 - 9 = -10
// 0x29 = 0b001_01001 = major 1, additional 9
const data = [_]u8{0x29};
const result = try readInt(&data, 0);
try std.testing.expectEqual(@as(i64, -10), result.val);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readInt: reject non-integer" {
const data = [_]u8{0x60}; // major 3 (text), length 0
try std.testing.expectError(error.WrongType, readInt(&data, 0));
}
// ===========================================================================
// readBool
// ===========================================================================
test "readBool: true" {
const data = [_]u8{0xf5};
const result = try readBool(&data, 0);
try std.testing.expectEqual(true, result.val);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readBool: false" {
const data = [_]u8{0xf4};
const result = try readBool(&data, 0);
try std.testing.expectEqual(false, result.val);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readBool: reject non-bool" {
const data = [_]u8{0xf6}; // null
try std.testing.expectError(error.WrongType, readBool(&data, 0));
}
// ===========================================================================
// readNull
// ===========================================================================
test "readNull: null" {
const data = [_]u8{0xf6};
const result = try readNull(&data, 0);
try std.testing.expectEqual(@as(usize, 1), result);
}
test "readNull: reject non-null" {
const data = [_]u8{0xf5}; // true
try std.testing.expectError(error.WrongType, readNull(&data, 0));
}
// ===========================================================================
// readMapHeader
// ===========================================================================
test "readMapHeader: count 2" {
const data = [_]u8{0xa2}; // major 5, length 2
const result = try readMapHeader(&data, 0);
try std.testing.expectEqual(@as(u64, 2), result.val);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readMapHeader: reject non-map" {
const data = [_]u8{0x82}; // major 4 (array), length 2
try std.testing.expectError(error.WrongType, readMapHeader(&data, 0));
}
// ===========================================================================
// readArrayHeader
// ===========================================================================
test "readArrayHeader: count 3" {
const data = [_]u8{0x83}; // major 4, length 3
const result = try readArrayHeader(&data, 0);
try std.testing.expectEqual(@as(u64, 3), result.val);
try std.testing.expectEqual(@as(usize, 1), result.end);
}
test "readArrayHeader: reject non-array" {
const data = [_]u8{0xa3}; // major 5 (map), length 3
try std.testing.expectError(error.WrongType, readArrayHeader(&data, 0));
}
// ===========================================================================
// readCidLink
// ===========================================================================
test "readCidLink: valid CID (tag(42) + bytes with 0x00 prefix + 36-byte CIDv1)" {
// tag(42): 0xd8 0x2a
// bytes(37): 0x58 0x25 (37 = 1 prefix + 36 CID)
// 0x00 prefix
// 36-byte CID: 0x01 0x71 0x12 0x20 ++ 32 x 0xaa
const cid_raw = [_]u8{ 0x01, 0x71, 0x12, 0x20 } ++ @as([32]u8, @splat(0xaa));
const data = [_]u8{ 0xd8, 0x2a, 0x58, 0x25, 0x00 } ++ cid_raw;
const result = try readCidLink(&data, 0);
try std.testing.expectEqual(@as(usize, 36), result.val.len);
try std.testing.expectEqual(@as(u8, 0x01), result.val[0]);
try std.testing.expectEqual(@as(u8, 0x71), result.val[1]);
try std.testing.expectEqual(@as(u8, 0x12), result.val[2]);
try std.testing.expectEqual(@as(u8, 0x20), result.val[3]);
try std.testing.expectEqual(@as(u8, 0xaa), result.val[4]);
try std.testing.expectEqual(@as(usize, 4 + 37), result.end); // 4 header bytes (2 tag + 2 bytes hdr) + 37 payload bytes
}
test "readCidLink: reject non-tag" {
const data = [_]u8{ 0x43, 0x01, 0x02, 0x03 }; // major 2 (bytes), not a tag
try std.testing.expectError(error.WrongType, readCidLink(&data, 0));
}
// ===========================================================================
// skipValue
// ===========================================================================
const skipValue = cbor.skipValue;
const peekType = cbor.peekType;
const peekTypeAt = cbor.peekTypeAt;
const encodeAlloc = cbor.encodeAlloc;
const Value = cbor.Value;
test "skipValue: unsigned integer (1 byte)" {
// 0x05 = major 0, value 5
const data = [_]u8{0x05};
const end = try skipValue(&data, 0);
try std.testing.expectEqual(@as(usize, 1), end);
}
test "skipValue: text string (header + payload)" {
// 0x65 = major 3, length 5 + "hello"
const data = [_]u8{ 0x65, 'h', 'e', 'l', 'l', 'o' };
const end = try skipValue(&data, 0);
try std.testing.expectEqual(@as(usize, 6), end);
}
test "skipValue: nested map {\"a\": [1, 2]}" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// Build {"a": [1, 2]} using the encoder
const val = Value{ .map = &.{
.{ .key = "a", .value = .{ .array = &.{
.{ .unsigned = 1 },
.{ .unsigned = 2 },
} } },
} };
const encoded = try encodeAlloc(alloc, val);
const end = try skipValue(encoded, 0);
try std.testing.expectEqual(encoded.len, end);
}
test "skipValue: CID link (tag 42 + byte string)" {
// tag(42): 0xd8 0x2a
// bytes(37): 0x58 0x25 (37 = 1 prefix + 36 CID)
// 0x00 prefix + 36-byte CID
const cid_raw = [_]u8{ 0x01, 0x71, 0x12, 0x20 } ++ @as([32]u8, @splat(0xaa));
const data = [_]u8{ 0xd8, 0x2a, 0x58, 0x25, 0x00 } ++ cid_raw;
const end = try skipValue(&data, 0);
try std.testing.expectEqual(data.len, end);
}
test "skipValue: first of two concatenated values" {
// Two values: unsigned 5 (0x05) followed by unsigned 10 (0x0a)
const data = [_]u8{ 0x05, 0x0a };
const end = try skipValue(&data, 0);
try std.testing.expectEqual(@as(usize, 1), end);
// The second value starts at position 1
try std.testing.expectEqual(@as(u8, 0x0a), data[end]);
}
test "skipValue: complex record (encoded map)" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// Encode a realistic map with multiple types
const val = Value{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } },
.{ .key = "createdAt", .value = .{ .text = "2024-01-01T00:00:00Z" } },
.{ .key = "text", .value = .{ .text = "hello world" } },
} };
const encoded = try encodeAlloc(alloc, val);
const end = try skipValue(encoded, 0);
try std.testing.expectEqual(encoded.len, end);
}
// ===========================================================================
// peekType
// ===========================================================================
test "peekType: find $type when present" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const val = Value{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } },
.{ .key = "text", .value = .{ .text = "hello" } },
} };
const encoded = try encodeAlloc(alloc, val);
const result = try peekType(encoded);
try std.testing.expect(result != null);
try std.testing.expectEqualStrings("app.bsky.feed.post", result.?);
}
test "peekType: find $type when not first key (DAG-CBOR sort order)" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
// DAG-CBOR sorts by length then lex. Keys "ab" (2 bytes) sorts before
// "$type" (5 bytes), so "$type" won't be first.
const val = Value{ .map = &.{
.{ .key = "ab", .value = .{ .unsigned = 42 } },
.{ .key = "$type", .value = .{ .text = "app.bsky.graph.follow" } },
.{ .key = "zzzzzz", .value = .{ .boolean = true } },
} };
const encoded = try encodeAlloc(alloc, val);
const result = try peekType(encoded);
try std.testing.expect(result != null);
try std.testing.expectEqualStrings("app.bsky.graph.follow", result.?);
}
test "peekType: return null when no $type field" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const val = Value{ .map = &.{
.{ .key = "text", .value = .{ .text = "hello" } },
.{ .key = "count", .value = .{ .unsigned = 5 } },
} };
const encoded = try encodeAlloc(alloc, val);
const result = try peekType(encoded);
try std.testing.expect(result == null);
}
test "peekType: return null for non-map input" {
// An unsigned integer, not a map
const data = [_]u8{0x05};
const result = try peekType(&data);
try std.testing.expect(result == null);
}