authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-26 20:56:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-26 21:00:58-07:00
log668299f0db3a258e45e28c68696e24cfbf8386a3
treead81d0e36688640cd563204b6dff3d96b8105eca
parent980445f08bfee496f4e784b05653e1698addaac8

std: update xz unit tests to new I/O API


1 files changed, 40 insertions(+), 16 deletions(-)

lib/std/compress/xz/test.zig+40-16
...@@ -13,7 +13,7 @@ fn decompress(data: []const u8) ![]u8 {...@@ -13,7 +13,7 @@ fn decompress(data: []const u8) ![]u8 {
13 return xz_stream.reader.allocRemaining(gpa, .unlimited);13 return xz_stream.reader.allocRemaining(gpa, .unlimited);
14}14}
1515
16fn testReader(data: []const u8, comptime expected: []const u8) !void {16fn testReader(data: []const u8, expected: []const u8) !void {
17 const gpa = testing.allocator;17 const gpa = testing.allocator;
1818
19 const result = try decompress(data);19 const result = try decompress(data);
...@@ -22,6 +22,17 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void {...@@ -22,6 +22,17 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void {
22 try testing.expectEqualSlices(u8, expected, result);22 try testing.expectEqualSlices(u8, expected, result);
23}23}
2424
25fn testDecompressError(expected: anyerror, compressed: []const u8) !void {
26 const gpa = std.testing.allocator;
27 var stream: std.Io.Reader = .fixed(compressed);
28
29 var decompressor = try xz.Decompress.init(&stream, gpa, &.{});
30 defer decompressor.deinit();
31
32 try std.testing.expectError(error.ReadFailed, decompressor.reader.allocRemaining(gpa, .unlimited));
33 try std.testing.expectEqual(expected, decompressor.err orelse return error.TestFailed);
34}
35
25test "fixture good-0-empty.xz" {36test "fixture good-0-empty.xz" {
26 try testReader(@embedFile("testdata/good-0-empty.xz"), "");37 try testReader(@embedFile("testdata/good-0-empty.xz"), "");
27}38}
...@@ -98,21 +109,32 @@ test "fixture good-1-lzma2-5.xz" {...@@ -98,21 +109,32 @@ test "fixture good-1-lzma2-5.xz" {
98 try testReader(@embedFile("testdata/good-1-lzma2-5.xz"), "");109 try testReader(@embedFile("testdata/good-1-lzma2-5.xz"), "");
99}110}
100111
101test "unsupported" {112test "fixture good-1-delta-lzma2.tiff.xz" {
102 inline for ([_][]const u8{113 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-delta-lzma2.tiff.xz"));
103 "good-1-delta-lzma2.tiff.xz",114}
104 "good-1-x86-lzma2.xz",115
105 "good-1-sparc-lzma2.xz",116test "fixture good-1-x86-lzma2.xz" {
106 "good-1-arm64-lzma2-1.xz",117 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-x86-lzma2.xz"));
107 "good-1-arm64-lzma2-2.xz",118}
108 "good-1-3delta-lzma2.xz",119
109 "good-1-empty-bcj-lzma2.xz",120test "fixture good-1-sparc-lzma2.xz" {
110 }) |filename| {121 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-sparc-lzma2.xz"));
111 try testing.expectError(122}
112 error.Unsupported,123
113 decompress(@embedFile("testdata/" ++ filename)),124test "fixture good-1-arm64-lzma2-1.xz" {
114 );125 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-1.xz"));
115 }126}
127
128test "fixture good-1-arm64-lzma2-2.xz" {
129 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-2.xz"));
130}
131
132test "fixture good-1-3delta-lzma2.xz" {
133 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-3delta-lzma2.xz"));
134}
135
136test "fixture good-1-empty-bcj-lzma2.xz" {
137 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-empty-bcj-lzma2.xz"));
116}138}
117139
118fn testDontPanic(data: []const u8) !void {140fn testDontPanic(data: []const u8) !void {
...@@ -127,6 +149,8 @@ test "size fields: integer overflow avoidance" {...@@ -127,6 +149,8 @@ test "size fields: integer overflow avoidance" {
127 // These cases were found via fuzz testing and each previously caused149 // These cases were found via fuzz testing and each previously caused
128 // an integer overflow when decoding. We just want to ensure they no longer150 // an integer overflow when decoding. We just want to ensure they no longer
129 // cause a panic151 // cause a panic
152 // TODO this not a sufficient way to test. tests should always check the result,
153 // not merely ensure that the code does not crash.
130 const header_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6z";154 const header_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6z";
131 try testDontPanic(header_size_overflow);155 try testDontPanic(header_size_overflow);
132 const lzma2_chunk_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x02\x00!\x01\x08\x00\x00\x00\xd8\x0f#\x13\x01\xff\xff";156 const lzma2_chunk_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x02\x00!\x01\x08\x00\x00\x00\xd8\x0f#\x13\x01\xff\xff";