master
1const std = @import("../../std.zig");
2const lzma = std.compress.lzma;
3
4fn testDecompress(compressed: []const u8) ![]u8 {
5 const gpa = std.testing.allocator;
6 var stream: std.Io.Reader = .fixed(compressed);
7
8 var decompressor = try lzma.Decompress.initOptions(&stream, gpa, &.{}, .{}, std.math.maxInt(u32));
9 defer decompressor.deinit();
10 return decompressor.reader.allocRemaining(gpa, .unlimited);
11}
12
13fn testDecompressEqual(expected: []const u8, compressed: []const u8) !void {
14 const gpa = std.testing.allocator;
15 const decomp = try testDecompress(compressed);
16 defer gpa.free(decomp);
17 try std.testing.expectEqualSlices(u8, expected, decomp);
18}
19
20fn testDecompressError(expected: anyerror, compressed: []const u8) !void {
21 const gpa = std.testing.allocator;
22 var stream: std.Io.Reader = .fixed(compressed);
23
24 var decompressor = try lzma.Decompress.initOptions(&stream, gpa, &.{}, .{}, std.math.maxInt(u32));
25 defer decompressor.deinit();
26
27 try std.testing.expectError(error.ReadFailed, decompressor.reader.allocRemaining(gpa, .unlimited));
28 try std.testing.expectEqual(expected, decompressor.err orelse return error.TestFailed);
29}
30
31test "decompress empty world" {
32 try testDecompressEqual(
33 "",
34 &[_]u8{
35 0x5d, 0x00, 0x00, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x83, 0xff,
36 0xfb, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00,
37 },
38 );
39}
40
41test "decompress hello world" {
42 try testDecompressEqual(
43 "Hello world\n",
44 &[_]u8{
45 0x5d, 0x00, 0x00, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x24, 0x19,
46 0x49, 0x98, 0x6f, 0x10, 0x19, 0xc6, 0xd7, 0x31, 0xeb, 0x36, 0x50, 0xb2, 0x98, 0x48, 0xff, 0xfe,
47 0xa5, 0xb0, 0x00,
48 },
49 );
50}
51
52test "decompress huge dict" {
53 try testDecompressEqual(
54 "Hello world\n",
55 &[_]u8{
56 0x5d, 0x7f, 0x7f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x24, 0x19,
57 0x49, 0x98, 0x6f, 0x10, 0x19, 0xc6, 0xd7, 0x31, 0xeb, 0x36, 0x50, 0xb2, 0x98, 0x48, 0xff, 0xfe,
58 0xa5, 0xb0, 0x00,
59 },
60 );
61}
62
63test "unknown size with end of payload marker" {
64 try testDecompressEqual(
65 "Hello\nWorld!\n",
66 @embedFile("testdata/good-unknown_size-with_eopm.lzma"),
67 );
68}
69
70test "known size without end of payload marker" {
71 try testDecompressEqual(
72 "Hello\nWorld!\n",
73 @embedFile("testdata/good-known_size-without_eopm.lzma"),
74 );
75}
76
77test "known size with end of payload marker" {
78 try testDecompressEqual(
79 "Hello\nWorld!\n",
80 @embedFile("testdata/good-known_size-with_eopm.lzma"),
81 );
82}
83
84test "too big uncompressed size in header" {
85 try testDecompressError(
86 error.DecompressedSizeMismatch,
87 @embedFile("testdata/bad-too_big_size-with_eopm.lzma"),
88 );
89}
90
91test "too small uncompressed size in header" {
92 try testDecompressError(
93 error.DecompressedSizeMismatch,
94 @embedFile("testdata/bad-too_small_size-without_eopm-3.lzma"),
95 );
96}
97
98test "reading one byte" {
99 const gpa = std.testing.allocator;
100 const compressed = @embedFile("testdata/good-known_size-with_eopm.lzma");
101 var stream: std.Io.Reader = .fixed(compressed);
102 var decompressor = try lzma.Decompress.initOptions(&stream, gpa, &.{}, .{}, std.math.maxInt(u32));
103 defer decompressor.deinit();
104
105 var buffer: [1]u8 = undefined;
106 try decompressor.reader.readSliceAll(&buffer);
107 try std.testing.expectEqual(72, buffer[0]);
108}