Skip to content

Commit ba034d9

Browse files
committed
Add bad data tests
1 parent b908dbc commit ba034d9

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/frame.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ const IDENTIFIER_FRAME: [10]u8 = [_]u8{ 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61
2323
/// Max allowed size for an uncompressed payload according to the spec.
2424
const UNCOMPRESSED_CHUNK_SIZE_LIMIT = 65536;
2525

26-
const UncompressError = error{
26+
pub const UncompressError = error{
2727
BadIdentifier,
2828
BadChecksum,
2929
IllegalChunkLength,
3030
} || snappy.Error || std.mem.Allocator.Error;
3131

32-
const CompressError = std.mem.Allocator.Error || snappy.Error;
32+
pub const CompressError = std.mem.Allocator.Error || snappy.Error;
3333

3434
/// Frame `bytes` into Snappy chunks, choosing compressed payloads only
3535
/// when they are smaller than their uncompressed counterparts.

src/snappy.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ test "round trip - raw" {
3939
}
4040
}
4141

42+
test "bad data" {
43+
const allocator = std.testing.allocator;
44+
45+
var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true });
46+
defer dir.close();
47+
48+
var it = dir.iterate();
49+
while (try it.next()) |entry| {
50+
if (entry.kind != .file) continue;
51+
if (!std.mem.startsWith(u8, entry.name, "baddata")) continue;
52+
53+
var file = try dir.openFile(entry.name, .{});
54+
defer file.close();
55+
56+
const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
57+
defer allocator.free(bytes);
58+
const got = try allocator.alloc(u8, try raw.uncompressedLength(bytes));
59+
defer allocator.free(got);
60+
try std.testing.expectError(raw.Error.invalid_input, raw.uncompress(bytes[0..], got));
61+
}
62+
}
63+
4264
test "round trip - framed" {
4365
const allocator = std.testing.allocator;
4466

0 commit comments

Comments
 (0)