| ... | @@ -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 | } |
| 15 | | 15 | |
| 16 | fn testReader(data: []const u8, comptime expected: []const u8) !void { | 16 | fn testReader(data: []const u8, expected: []const u8) !void { |
| 17 | const gpa = testing.allocator; | 17 | const gpa = testing.allocator; |
| 18 | | 18 | |
| 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 | } |
| 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 | test "fixture good-0-empty.xz" { | 36 | test "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 | } |
| 100 | | 111 | |
| 101 | test "unsupported" { | 112 | test "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", | 116 | test "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", | 120 | test "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)), | 124 | test "fixture good-1-arm64-lzma2-1.xz" { |
| 114 | ); | 125 | try testDecompressError(error.Unsupported, @embedFile("testdata/good-1-arm64-lzma2-1.xz")); |
| 115 | } | 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 | fn testDontPanic(data: []const u8) !void { | 140 | fn 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 caused | 149 | // These cases were found via fuzz testing and each previously caused |
| 128 | // an integer overflow when decoding. We just want to ensure they no longer | 150 | // an integer overflow when decoding. We just want to ensure they no longer |
| 129 | // cause a panic | 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 | 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"; |