| 1 | // |
| 2 | // ZLibTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "ZLibTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/InflatingStream.h" |
| 15 | #include "Poco/DeflatingStream.h" |
| 16 | #include "Poco/MemoryStream.h" |
| 17 | #include "Poco/StreamCopier.h" |
| 18 | #include "Poco/Buffer.h" |
| 19 | #include <sstream> |
| 20 | |
| 21 | |
| 22 | using Poco::InflatingInputStream; |
| 23 | using Poco::InflatingOutputStream; |
| 24 | using Poco::DeflatingOutputStream; |
| 25 | using Poco::DeflatingInputStream; |
| 26 | using Poco::InflatingStreamBuf; |
| 27 | using Poco::DeflatingStreamBuf; |
| 28 | using Poco::StreamCopier; |
| 29 | |
| 30 | |
| 31 | ZLibTest::ZLibTest(const std::string& rName): CppUnit::TestCase(rName) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | |
| 36 | ZLibTest::~ZLibTest() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void ZLibTest::testDeflate1() |
| 42 | { |
| 43 | std::stringstream buffer; |
| 44 | DeflatingOutputStream deflater(buffer); |
| 45 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 46 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 47 | deflater.close(); |
| 48 | InflatingInputStream inflater(buffer); |
| 49 | std::string data; |
| 50 | inflater >> data; |
| 51 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 52 | inflater >> data; |
| 53 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void ZLibTest::testDeflate2() |
| 58 | { |
| 59 | std::stringstream buffer; |
| 60 | DeflatingOutputStream deflater(buffer); |
| 61 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 62 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 63 | deflater.close(); |
| 64 | std::stringstream buffer2; |
| 65 | InflatingOutputStream inflater(buffer2); |
| 66 | StreamCopier::copyStream(buffer, inflater); |
| 67 | inflater.close(); |
| 68 | std::string data; |
| 69 | buffer2 >> data; |
| 70 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 71 | buffer2 >> data; |
| 72 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void ZLibTest::testDeflate3() |
| 77 | { |
| 78 | std::stringstream buffer; |
| 79 | buffer << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 80 | buffer << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 81 | DeflatingInputStream deflater(buffer); |
| 82 | std::stringstream buffer2; |
| 83 | StreamCopier::copyStream(deflater, buffer2); |
| 84 | std::stringstream buffer3; |
| 85 | InflatingOutputStream inflater(buffer3); |
| 86 | StreamCopier::copyStream(buffer2, inflater); |
| 87 | inflater.close(); |
| 88 | std::string data; |
| 89 | buffer3 >> data; |
| 90 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 91 | buffer3 >> data; |
| 92 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void ZLibTest::testDeflate4() |
| 97 | { |
| 98 | Poco::Buffer<char> buffer(1024); |
| 99 | Poco::MemoryOutputStream ostr(buffer.begin(), static_cast<std::streamsize>(buffer.size())); |
| 100 | DeflatingOutputStream deflater(ostr, -10, Z_BEST_SPEED); |
| 101 | std::string data(36828, 'x'); |
| 102 | deflater << data; |
| 103 | deflater.close(); |
| 104 | Poco::MemoryInputStream istr(buffer.begin(), ostr.charsWritten()); |
| 105 | InflatingInputStream inflater(istr, -10); |
| 106 | std::string data2; |
| 107 | inflater >> data2; |
| 108 | assertTrue (data2 == data); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void ZLibTest::testGzip1() |
| 113 | { |
| 114 | std::stringstream buffer; |
| 115 | DeflatingOutputStream deflater(buffer, DeflatingStreamBuf::STREAM_GZIP); |
| 116 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 117 | deflater << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 118 | deflater.close(); |
| 119 | InflatingInputStream inflater(buffer, InflatingStreamBuf::STREAM_GZIP); |
| 120 | std::string data; |
| 121 | inflater >> data; |
| 122 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 123 | inflater >> data; |
| 124 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void ZLibTest::testGzip2() |
| 129 | { |
| 130 | // created with gzip ("Hello, world!"): |
| 131 | const unsigned char gzdata[] = |
| 132 | { |
| 133 | 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x73, 0xd0, 0x41, 0x00, 0x03, 0x68, 0x77, 0x00, 0xf3, 0x48, 0xcd, |
| 134 | 0xc9, 0xc9, 0xd7, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0x51, 0xe4, 0x02, 0x00, 0x18, 0xa7, 0x55, |
| 135 | 0x7b, 0x0e, 0x00, 0x00, 0x00, 0x00 |
| 136 | }; |
| 137 | |
| 138 | std::string gzstr((char*) gzdata, sizeof(gzdata)); |
| 139 | std::istringstream istr(gzstr); |
| 140 | InflatingInputStream inflater(istr, InflatingStreamBuf::STREAM_GZIP); |
| 141 | std::string data; |
| 142 | inflater >> data; |
| 143 | assertTrue (data == "Hello," ); |
| 144 | inflater >> data; |
| 145 | assertTrue (data == "world!" ); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void ZLibTest::testGzip3() |
| 150 | { |
| 151 | std::stringstream buffer; |
| 152 | DeflatingOutputStream deflater1(buffer, DeflatingStreamBuf::STREAM_GZIP); |
| 153 | deflater1 << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 154 | deflater1 << "abcdefabcdefabcdefabcdefabcdefabcdef" << std::endl; |
| 155 | deflater1.close(); |
| 156 | DeflatingOutputStream deflater2(buffer, DeflatingStreamBuf::STREAM_GZIP); |
| 157 | deflater2 << "bcdefabcdefabcdefabcdefabcdefabcdefa" << std::endl; |
| 158 | deflater2 << "bcdefabcdefabcdefabcdefabcdefabcdefa" << std::endl; |
| 159 | deflater2.close(); |
| 160 | InflatingInputStream inflater(buffer, InflatingStreamBuf::STREAM_GZIP); |
| 161 | std::string data; |
| 162 | inflater >> data; |
| 163 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 164 | inflater >> data; |
| 165 | assertTrue (data == "abcdefabcdefabcdefabcdefabcdefabcdef" ); |
| 166 | data.clear(); |
| 167 | inflater >> data; |
| 168 | assertTrue (data.empty()); |
| 169 | assertTrue (inflater.eof()); |
| 170 | inflater.reset(); |
| 171 | inflater >> data; |
| 172 | assertTrue (data == "bcdefabcdefabcdefabcdefabcdefabcdefa" ); |
| 173 | inflater >> data; |
| 174 | assertTrue (data == "bcdefabcdefabcdefabcdefabcdefabcdefa" ); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | void ZLibTest::setUp() |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | |
| 183 | void ZLibTest::tearDown() |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | |
| 188 | CppUnit::Test* ZLibTest::suite() |
| 189 | { |
| 190 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ZLibTest" ); |
| 191 | |
| 192 | CppUnit_addTest(pSuite, ZLibTest, testDeflate1); |
| 193 | CppUnit_addTest(pSuite, ZLibTest, testDeflate2); |
| 194 | CppUnit_addTest(pSuite, ZLibTest, testDeflate3); |
| 195 | CppUnit_addTest(pSuite, ZLibTest, testDeflate4); |
| 196 | CppUnit_addTest(pSuite, ZLibTest, testGzip1); |
| 197 | CppUnit_addTest(pSuite, ZLibTest, testGzip2); |
| 198 | CppUnit_addTest(pSuite, ZLibTest, testGzip3); |
| 199 | |
| 200 | return pSuite; |
| 201 | } |
| 202 | |