1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <tools/zcodec.hxx>
11 #include <tools/stream.hxx>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
20 // Sample text for compression
21 constexpr const char* DUMMY_TEXT
22 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n"
23 "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
24 "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
25 "commodo consequat.\n";
26 constexpr auto DUMMY_SIZE
= std::char_traits
<char>::length(DUMMY_TEXT
);
28 class ZCodecTest
: public CppUnit::TestFixture
31 void testGzCompressDecompress();
32 void testMakeDummyFile();
33 void testZlibCompressDecompress();
35 CPPUNIT_TEST_SUITE(ZCodecTest
);
36 CPPUNIT_TEST(testMakeDummyFile
);
37 CPPUNIT_TEST(testGzCompressDecompress
);
38 CPPUNIT_TEST(testZlibCompressDecompress
);
39 CPPUNIT_TEST_SUITE_END();
42 // Creates a stream from DUMMY_TEXT to compress and decompress
43 std::unique_ptr
<SvMemoryStream
> makeDummyFile()
45 SvMemoryStream
* pRet
= new SvMemoryStream();
46 pRet
->WriteBytes(DUMMY_TEXT
, DUMMY_SIZE
);
47 return std::unique_ptr
<SvMemoryStream
>(pRet
);
50 // Test that the stream generated from makeDummyFile is valid
51 void ZCodecTest::testMakeDummyFile()
53 auto pStream
= makeDummyFile();
55 CPPUNIT_ASSERT_MESSAGE("pStream is null", pStream
);
56 decltype(DUMMY_SIZE
) size
= pStream
->GetSize();
58 CPPUNIT_ASSERT_EQUAL(DUMMY_SIZE
, size
);
61 // Test that ZCodec::Compress and ZCodec::Decompress work for gzlib = true
62 // Compares the CRC32 checksum of the initial stream and the compressed->decompressed stream
63 void ZCodecTest::testGzCompressDecompress()
65 auto pInitialStream
= makeDummyFile();
66 SvMemoryStream pCompressedStream
;
67 SvMemoryStream pDecompressedStream
;
68 auto nInitialStreamCRC32
= rtl_crc32(0, pInitialStream
->GetData(), pInitialStream
->GetSize());
70 aCodec
.BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, true);
71 // Set compression metadata for compressing a GZ file
72 aCodec
.SetCompressionMetadata("dummy.txt", 0, nInitialStreamCRC32
);
73 aCodec
.Compress(*pInitialStream
, pCompressedStream
);
75 = aCodec
.EndCompression(); // returns compressed size or -1 if compression failed
76 // Check that the compression succeeded
77 CPPUNIT_ASSERT_GREATER(static_cast<tools::Long
>(0), nCompressedSize
);
78 // No need to Seek(0) here because ZCodec::InitDecompress does that already for gz
79 aCodec
.BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, true);
80 aCodec
.Decompress(pCompressedStream
, pDecompressedStream
);
81 auto nDecompressedSize
82 = aCodec
.EndCompression(); // returns decompressed size or -1 if decompression failed
83 // Check that the decompressed text size is equal to the original
84 CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long
>(DUMMY_SIZE
), nDecompressedSize
);
85 auto nCompressedDecompressedStreamCRC32
86 = rtl_crc32(0, pDecompressedStream
.GetData(), pDecompressedStream
.GetSize());
87 // Check that the initial and decompressed CRC32 checksums are the same -> gz (de)compression works
88 CPPUNIT_ASSERT_EQUAL(nInitialStreamCRC32
, nCompressedDecompressedStreamCRC32
);
91 // Test that ZCodec::Compress and ZCodec::Decompress work for gzlib = false
92 // Compares the CRC32 checksum of the initial stream and the compressed->decompressed stream
93 void ZCodecTest::testZlibCompressDecompress()
95 auto pInitialStream
= makeDummyFile();
96 SvMemoryStream pCompressedStream
;
97 SvMemoryStream pDecompressedStream
;
98 auto nInitialStreamCRC32
= rtl_crc32(0, pInitialStream
->GetData(), pInitialStream
->GetSize());
100 aCodec
.BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, false);
101 aCodec
.Compress(*pInitialStream
, pCompressedStream
);
103 = aCodec
.EndCompression(); // returns compressed size or -1 if compression failed
104 // Check that the compression succeeded
105 CPPUNIT_ASSERT_GREATER(static_cast<tools::Long
>(0), nCompressedSize
);
106 pCompressedStream
.Seek(0);
107 aCodec
.BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, false);
108 aCodec
.Decompress(pCompressedStream
, pDecompressedStream
);
109 auto nDecompressedSize
110 = aCodec
.EndCompression(); // returns decompressed size or -1 if decompression failed
111 // Check that the decompressed text size is equal to the original
112 CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long
>(DUMMY_SIZE
), nDecompressedSize
);
113 auto nCompressedDecompressedStreamCRC32
114 = rtl_crc32(0, pDecompressedStream
.GetData(), pDecompressedStream
.GetSize());
115 // Check that the initial and decompressed CRC32 checksums are the same -> zlib (de)compression works
116 CPPUNIT_ASSERT_EQUAL(nInitialStreamCRC32
, nCompressedDecompressedStreamCRC32
);
121 CPPUNIT_TEST_SUITE_REGISTRATION(ZCodecTest
);
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */