TestZlib.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/Array.h>
  8. #include <LibCompress/Zlib.h>
  9. TEST_CASE(zlib_decompress_simple)
  10. {
  11. Array<u8, 40> const compressed {
  12. 0x78, 0x01, 0x01, 0x1D, 0x00, 0xE2, 0xFF, 0x54, 0x68, 0x69, 0x73, 0x20,
  13. 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x69, 0x6D, 0x70, 0x6C, 0x65, 0x20,
  14. 0x74, 0x65, 0x78, 0x74, 0x20, 0x66, 0x69, 0x6C, 0x65, 0x20, 0x3A, 0x29,
  15. 0x99, 0x5E, 0x09, 0xE8
  16. };
  17. const u8 uncompressed[] = "This is a simple text file :)";
  18. auto const decompressed = Compress::ZlibDecompressor::decompress_all(compressed);
  19. EXPECT(decompressed.value().bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));
  20. }
  21. TEST_CASE(zlib_compress_simple)
  22. {
  23. // Note: This is just the output of our compression function from an arbitrary point in time.
  24. // This test is intended to ensure that the decompression doesn't change unintentionally,
  25. // it does not make any guarantees for correctness.
  26. Array<u8, 37> const compressed {
  27. 0x78, 0x9C, 0x0B, 0xC9, 0xC8, 0x2C, 0x56, 0xC8, 0x2C, 0x56, 0x48, 0x54,
  28. 0x28, 0xCE, 0xCC, 0x2D, 0xC8, 0x49, 0x55, 0x28, 0x49, 0xAD, 0x28, 0x51,
  29. 0x48, 0xCB, 0xCC, 0x49, 0x55, 0xB0, 0xD2, 0x04, 0x00, 0x99, 0x5E, 0x09,
  30. 0xE8
  31. };
  32. const u8 uncompressed[] = "This is a simple text file :)";
  33. auto const freshly_pressed = Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 });
  34. EXPECT(freshly_pressed.value().bytes() == compressed.span());
  35. }
  36. TEST_CASE(zlib_decompress_with_missing_end_bits)
  37. {
  38. // This test case has been extracted from compressed PNG data of `/res/icons/16x16/app-masterword.png`.
  39. // The decompression results have been confirmed using the `zlib-flate` tool.
  40. // Note: It is unconfirmed whether there are actually bits missing.
  41. // However, our decompressor implementation ends up in a weird state nonetheless.
  42. Array<u8, 72> const compressed {
  43. 0x08, 0xD7, 0x63, 0x30, 0x86, 0x00, 0x01, 0x06, 0x23, 0x25, 0x30, 0x00,
  44. 0x32, 0x42, 0x95, 0x54, 0x83, 0xD0, 0x18, 0x41, 0xA1, 0x50, 0x46, 0x28,
  45. 0x8C, 0xA1, 0x8A, 0xA1, 0x46, 0xC5, 0x35, 0x48, 0xC9, 0x05, 0x99, 0xA1,
  46. 0xA4, 0xE2, 0x02, 0x44, 0x60, 0x93, 0x5D, 0x54, 0x54, 0x9C, 0x20, 0x0C,
  47. 0x17, 0x17, 0x08, 0x43, 0xC5, 0xC9, 0x05, 0xA8, 0x4B, 0x50, 0x50, 0x50,
  48. 0xC4, 0xD1, 0x45, 0x50, 0x80, 0x01, 0x06, 0x00, 0xB6, 0x1F, 0x15, 0xEF
  49. };
  50. Array<u8, 144> const decompressed {
  51. 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x10, 0x00, 0x32, 0x22,
  52. 0x22, 0x22, 0x22, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52,
  53. 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52, 0x22, 0x22, 0x10,
  54. 0x00, 0x32, 0x55, 0x52, 0x55, 0x52, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55,
  55. 0x55, 0x55, 0x52, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x25, 0x25, 0x52,
  56. 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52, 0x22, 0x22, 0x10,
  57. 0x00, 0x32, 0x55, 0x24, 0x45, 0x52, 0x22, 0x44, 0x10, 0x00, 0x32, 0x55,
  58. 0x24, 0x45, 0x52, 0x22, 0x44, 0x10, 0x00, 0x32, 0x22, 0x24, 0x44, 0x22,
  59. 0x24, 0x44, 0x10, 0x00, 0x32, 0x22, 0x22, 0x44, 0x24, 0x24, 0x42, 0x10,
  60. 0x00, 0x32, 0x22, 0x22, 0x44, 0x44, 0x44, 0x42, 0x10, 0x00, 0x32, 0x22,
  61. 0x22, 0x24, 0x42, 0x44, 0x22, 0x10, 0x00, 0x11, 0x11, 0x11, 0x14, 0x41,
  62. 0x44, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  63. };
  64. auto const maybe_decompressed = Compress::ZlibDecompressor::decompress_all(compressed);
  65. EXPECT(maybe_decompressed.has_value());
  66. EXPECT_EQ(maybe_decompressed.value().span(), decompressed.span());
  67. }