| ... | ... | @@ -13,7 +13,7 @@ fn decompress(data: []const u8) ![]u8 { |
| 13 | 13 | return xz_stream.reader.allocRemaining(gpa, .unlimited); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | | fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 16 | fn testReader(data: []const u8, expected: []const u8) !void { |
| 17 | 17 | const gpa = testing.allocator; |
| 18 | 18 | |
| 19 | 19 | const result = try decompress(data); |
| ... | ... | @@ -22,6 +22,17 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 22 | 22 | try testing.expectEqualSlices(u8, expected, result); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | fn 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 | |
| 25 | 36 | test "fixture good-0-empty.xz" { |
| 26 | 37 | try testReader(@embedFile("testdata/good-0-empty.xz"), ""); |
| 27 | 38 | } |
| ... | ... | @@ -98,21 +109,32 @@ test "fixture good-1-lzma2-5.xz" { |
| 98 | 109 | try testReader(@embedFile("testdata/good-1-lzma2-5.xz"), ""); |
| 99 | 110 | } |
| 100 | 111 | |
| 101 | | test "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 | | } |
| 112 | test "fixture good-1-delta-lzma2.tiff.xz" { |
| 113 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-delta-lzma2.tiff.xz")); |
| 114 | } |
| 115 | |
| 116 | test "fixture good-1-x86-lzma2.xz" { |
| 117 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-x86-lzma2.xz")); |
| 118 | } |
| 119 | |
| 120 | test "fixture good-1-sparc-lzma2.xz" { |
| 121 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-sparc-lzma2.xz")); |
| 122 | } |
| 123 | |
| 124 | test "fixture good-1-arm64-lzma2-1.xz" { |
| 125 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-1.xz")); |
| 126 | } |
| 127 | |
| 128 | test "fixture good-1-arm64-lzma2-2.xz" { |
| 129 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-2.xz")); |
| 130 | } |
| 131 | |
| 132 | test "fixture good-1-3delta-lzma2.xz" { |
| 133 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-3delta-lzma2.xz")); |
| 134 | } |
| 135 | |
| 136 | test "fixture good-1-empty-bcj-lzma2.xz" { |
| 137 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-empty-bcj-lzma2.xz")); |
| 116 | 138 | } |
| 117 | 139 | |
| 118 | 140 | fn testDontPanic(data: []const u8) !void { |
| ... | ... | @@ -127,6 +149,8 @@ test "size fields: integer overflow avoidance" { |
| 127 | 149 | // These cases were found via fuzz testing and each previously caused |
| 128 | 150 | // an integer overflow when decoding. We just want to ensure they no longer |
| 129 | 151 | // 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 | 154 | const header_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6z"; |
| 131 | 155 | try testDontPanic(header_size_overflow); |
| 132 | 156 | const lzma2_chunk_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x02\x00!\x01\x08\x00\x00\x00\xd8\x0f#\x13\x01\xff\xff"; |