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 {
1313 return xz_stream.reader.allocRemaining(gpa, .unlimited);
1414}
1515
16fn testReader(data: []const u8, comptime expected: []const u8) !void {
16fn testReader(data: []const u8, expected: []const u8) !void {
1717 const gpa = testing.allocator;
1818
1919 const result = try decompress(data);
......@@ -22,6 +22,17 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void {
2222 try testing.expectEqualSlices(u8, expected, result);
2323}
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
2536test "fixture good-0-empty.xz" {
2637 try testReader(@embedFile("testdata/good-0-empty.xz"), "");
2738}
......@@ -98,21 +109,32 @@ test "fixture good-1-lzma2-5.xz" {
98109 try testReader(@embedFile("testdata/good-1-lzma2-5.xz"), "");
99110}
100111
101test "unsupported" {
102 inline for ([_][]const u8{
103 "good-1-delta-lzma2.tiff.xz",
104 "good-1-x86-lzma2.xz",
105 "good-1-sparc-lzma2.xz",
106 "good-1-arm64-lzma2-1.xz",
107 "good-1-arm64-lzma2-2.xz",
108 "good-1-3delta-lzma2.xz",
109 "good-1-empty-bcj-lzma2.xz",
110 }) |filename| {
111 try testing.expectError(
112 error.Unsupported,
113 decompress(@embedFile("testdata/" ++ filename)),
114 );
115 }
112test "fixture good-1-delta-lzma2.tiff.xz" {
113 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-delta-lzma2.tiff.xz"));
114}
115
116test "fixture good-1-x86-lzma2.xz" {
117 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-x86-lzma2.xz"));
118}
119
120test "fixture good-1-sparc-lzma2.xz" {
121 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-sparc-lzma2.xz"));
122}
123
124test "fixture good-1-arm64-lzma2-1.xz" {
125 try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-1.xz"));
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"));
116138}
117139
118140fn testDontPanic(data: []const u8) !void {
......@@ -127,6 +149,8 @@ test "size fields: integer overflow avoidance" {
127149 // These cases were found via fuzz testing and each previously caused
128150 // an integer overflow when decoding. We just want to ensure they no longer
129151 // 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.
130154 const header_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6z";
131155 try testDontPanic(header_size_overflow);
132156 const lzma2_chunk_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x02\x00!\x01\x08\x00\x00\x00\xd8\x0f#\x13\x01\xff\xff";