]>
Commit | Line | Data |
---|---|---|
340da6ae VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/streams/zlibstream.cpp | |
3 | // Purpose: Test wxZlibInputStream/wxZlibOutputStream | |
4 | // Author: Hans Van Leemputten | |
340da6ae | 5 | // Copyright: (c) 2004 Hans Van Leemputten |
526954c5 | 6 | // Licence: wxWindows licence |
340da6ae VS |
7 | /////////////////////////////////////////////////////////////////////////////// |
8 | ||
340da6ae | 9 | // For compilers that support precompilation, includes "wx/wx.h". |
8899b155 RN |
10 | // and "wx/cppunit.h" |
11 | #include "testprec.h" | |
340da6ae VS |
12 | |
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | // for all others, include the necessary headers | |
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
340da6ae VS |
22 | #include "wx/zstream.h" |
23 | #include "wx/wfstream.h" | |
24 | #include "wx/mstream.h" | |
7735998c | 25 | #include "wx/txtstrm.h" |
51acf83b | 26 | #include "wx/buffer.h" |
7735998c | 27 | |
340da6ae VS |
28 | #include "bstream.h" |
29 | ||
3e5f6c1c | 30 | using std::string; |
340da6ae | 31 | |
08776b09 VS |
32 | #define WXTEST_WITH_GZIP_CONDITION(testMethod) \ |
33 | WXTEST_WITH_CONDITION( COMPOSE_TEST_NAME(zlibStream), wxZlibInputStream::CanHandleGZip() && wxZlibOutputStream::CanHandleGZip(), testMethod ) | |
34 | ||
340da6ae VS |
35 | #define DATABUFFER_SIZE 1024 |
36 | ||
9a83f860 | 37 | static const wxString FILENAME_GZ = wxT("zlibtest.gz"); |
340da6ae VS |
38 | |
39 | /////////////////////////////////////////////////////////////////////////////// | |
40 | // The test case | |
41 | // | |
08776b09 | 42 | // Try to fully test wxZlibInputStream and wxZlibOutputStream |
340da6ae VS |
43 | |
44 | class zlibStream : public BaseStreamTestCase<wxZlibInputStream, wxZlibOutputStream> | |
45 | { | |
46 | public: | |
47 | zlibStream(); | |
48 | virtual ~zlibStream(); | |
49 | ||
50 | CPPUNIT_TEST_SUITE(zlibStream); | |
51 | // Base class stream tests the zlibstream supports. | |
52 | CPPUNIT_TEST_FAIL(Input_GetSize); | |
53 | CPPUNIT_TEST(Input_GetC); | |
54 | CPPUNIT_TEST(Input_Read); | |
55 | CPPUNIT_TEST(Input_Eof); | |
56 | CPPUNIT_TEST(Input_LastRead); | |
2d76b6d8 | 57 | CPPUNIT_TEST(Input_CanRead); |
340da6ae VS |
58 | CPPUNIT_TEST_FAIL(Input_SeekI); |
59 | CPPUNIT_TEST(Input_TellI); | |
60 | CPPUNIT_TEST(Input_Peek); | |
61 | CPPUNIT_TEST(Input_Ungetch); | |
62 | ||
63 | CPPUNIT_TEST(Output_PutC); | |
64 | CPPUNIT_TEST(Output_Write); | |
65 | CPPUNIT_TEST(Output_LastWrite); | |
66 | CPPUNIT_TEST_FAIL(Output_SeekO); | |
67 | CPPUNIT_TEST(Output_TellO); | |
68 | ||
69 | // Other test specific for zlib stream test case. | |
70 | CPPUNIT_TEST(TestStream_NoHeader_Default); | |
71 | CPPUNIT_TEST(TestStream_NoHeader_NoComp); | |
72 | CPPUNIT_TEST(TestStream_NoHeader_SpeedComp); | |
73 | CPPUNIT_TEST(TestStream_NoHeader_BestComp); | |
51acf83b | 74 | CPPUNIT_TEST(TestStream_NoHeader_Dictionary); |
340da6ae VS |
75 | CPPUNIT_TEST(TestStream_ZLib_Default); |
76 | CPPUNIT_TEST(TestStream_ZLib_NoComp); | |
77 | CPPUNIT_TEST(TestStream_ZLib_SpeedComp); | |
78 | CPPUNIT_TEST(TestStream_ZLib_BestComp); | |
08776b09 VS |
79 | WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Default); |
80 | WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_NoComp); | |
81 | WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_SpeedComp); | |
82 | WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_BestComp); | |
51acf83b | 83 | WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Dictionary); |
08776b09 | 84 | WXTEST_WITH_GZIP_CONDITION(TestStream_ZLibGZip); |
340da6ae | 85 | CPPUNIT_TEST(Decompress_BadData); |
7735998c VS |
86 | CPPUNIT_TEST(Decompress_wx251_zlib114_Data_NoHeader); |
87 | CPPUNIT_TEST(Decompress_wx251_zlib114_Data_ZLib); | |
88 | WXTEST_WITH_GZIP_CONDITION(Decompress_gzip135Data); | |
340da6ae VS |
89 | CPPUNIT_TEST_SUITE_END(); |
90 | ||
91 | protected: | |
92 | // Test different stream construct settings. | |
93 | void TestStream_NoHeader_Default(); | |
94 | void TestStream_NoHeader_NoComp(); | |
95 | void TestStream_NoHeader_SpeedComp(); | |
96 | void TestStream_NoHeader_BestComp(); | |
51acf83b | 97 | void TestStream_NoHeader_Dictionary(); |
340da6ae VS |
98 | void TestStream_ZLib_Default(); |
99 | void TestStream_ZLib_NoComp(); | |
100 | void TestStream_ZLib_SpeedComp(); | |
101 | void TestStream_ZLib_BestComp(); | |
102 | void TestStream_GZip_Default(); | |
103 | void TestStream_GZip_NoComp(); | |
104 | void TestStream_GZip_SpeedComp(); | |
105 | void TestStream_GZip_BestComp(); | |
51acf83b | 106 | void TestStream_GZip_Dictionary(); |
340da6ae VS |
107 | void TestStream_ZLibGZip(); |
108 | // Try to decompress bad data. | |
109 | void Decompress_BadData(); | |
3e5f6c1c | 110 | // Decompress data that was compress by an external app. |
7735998c | 111 | // (like test wx 2.4.2, 2.5.1 and gzip data) |
340da6ae | 112 | // Note: This test is limited in testing range! |
7735998c VS |
113 | void Decompress_wx251_zlib114_Data_NoHeader(); |
114 | void Decompress_wx251_zlib114_Data_ZLib(); | |
3e5f6c1c | 115 | void Decompress_gzip135Data(); |
340da6ae | 116 | |
3e5f6c1c | 117 | private: |
340da6ae VS |
118 | const char *GetDataBuffer(); |
119 | const unsigned char *GetCompressedData(); | |
51acf83b | 120 | void doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf = NULL); |
7735998c | 121 | void doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag = wxZLIB_AUTO); |
340da6ae VS |
122 | |
123 | private: | |
124 | // Implement base class functions. | |
3e5f6c1c | 125 | virtual wxZlibInputStream *DoCreateInStream(); |
340da6ae VS |
126 | virtual wxZlibOutputStream *DoCreateOutStream(); |
127 | virtual void DoDeleteInStream(); | |
128 | virtual void DoDeleteOutStream(); | |
129 | ||
3e5f6c1c | 130 | // Helper that can be used to create new wx compatibility tests... |
7735998c VS |
131 | // Otherwise not used by the tests. |
132 | void genExtTestData(wxTextOutputStream &out, const char *buf, int flag); | |
133 | ||
340da6ae VS |
134 | private: |
135 | char m_DataBuffer[DATABUFFER_SIZE]; | |
136 | size_t m_SizeCompressedData; | |
137 | unsigned char *m_pCompressedData; | |
51acf83b | 138 | wxMemoryBuffer m_Dictionary; |
340da6ae VS |
139 | |
140 | // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream. | |
141 | wxMemoryInputStream *m_pTmpMemInStream; | |
142 | wxMemoryOutputStream *m_pTmpMemOutStream; | |
143 | }; | |
144 | ||
145 | zlibStream::zlibStream() | |
146 | :m_SizeCompressedData(0), | |
147 | m_pCompressedData(NULL), | |
148 | m_pTmpMemInStream(NULL), | |
149 | m_pTmpMemOutStream(NULL) | |
150 | { | |
151 | // Init the data buffer. | |
152 | for (size_t i = 0; i < DATABUFFER_SIZE; i++) | |
153 | m_DataBuffer[i] = (i % 0xFF); | |
154 | ||
51acf83b VZ |
155 | m_Dictionary.AppendData(m_DataBuffer, sizeof(m_DataBuffer) / 2); |
156 | ||
340da6ae VS |
157 | // Set extra base config settings. |
158 | m_bSimpleTellITest = true; | |
159 | m_bSimpleTellOTest = true; | |
7735998c VS |
160 | |
161 | /* Example code on how to produce test data... | |
162 | { | |
9a83f860 | 163 | wxFFileOutputStream fstream_out(wxT("gentest.cpp")); |
7735998c VS |
164 | wxTextOutputStream out( fstream_out ); |
165 | ||
166 | genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4", wxZLIB_NO_HEADER); | |
167 | genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4", wxZLIB_ZLIB); | |
168 | } | |
169 | */ | |
340da6ae VS |
170 | } |
171 | ||
172 | zlibStream::~zlibStream() | |
173 | { | |
6b7d5d1c | 174 | delete[] m_pCompressedData; |
340da6ae VS |
175 | |
176 | delete m_pTmpMemInStream; | |
177 | delete m_pTmpMemOutStream; | |
178 | } | |
179 | ||
180 | void zlibStream::TestStream_NoHeader_Default() | |
181 | { | |
182 | doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION); | |
183 | } | |
184 | void zlibStream::TestStream_NoHeader_NoComp() | |
185 | { | |
186 | doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_NO_COMPRESSION); | |
187 | } | |
188 | void zlibStream::TestStream_NoHeader_SpeedComp() | |
189 | { | |
190 | doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_SPEED); | |
191 | } | |
192 | void zlibStream::TestStream_NoHeader_BestComp() | |
193 | { | |
194 | doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_COMPRESSION); | |
195 | } | |
51acf83b VZ |
196 | void zlibStream::TestStream_NoHeader_Dictionary() |
197 | { | |
198 | doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION, &m_Dictionary); | |
199 | } | |
340da6ae VS |
200 | |
201 | void zlibStream::TestStream_ZLib_Default() | |
202 | { | |
203 | doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION); | |
204 | } | |
205 | void zlibStream::TestStream_ZLib_NoComp() | |
206 | { | |
207 | doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_NO_COMPRESSION); | |
208 | } | |
209 | void zlibStream::TestStream_ZLib_SpeedComp() | |
210 | { | |
211 | doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_SPEED); | |
212 | } | |
213 | void zlibStream::TestStream_ZLib_BestComp() | |
214 | { | |
215 | doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_COMPRESSION); | |
216 | } | |
217 | ||
218 | void zlibStream::TestStream_GZip_Default() | |
219 | { | |
220 | doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION); | |
221 | } | |
222 | void zlibStream::TestStream_GZip_NoComp() | |
223 | { | |
224 | doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_NO_COMPRESSION); | |
225 | } | |
226 | void zlibStream::TestStream_GZip_SpeedComp() | |
227 | { | |
228 | doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_SPEED); | |
229 | } | |
230 | void zlibStream::TestStream_GZip_BestComp() | |
231 | { | |
232 | doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_COMPRESSION); | |
233 | } | |
51acf83b VZ |
234 | void zlibStream::TestStream_GZip_Dictionary() |
235 | { | |
236 | doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION, &m_Dictionary); | |
237 | } | |
340da6ae VS |
238 | |
239 | void zlibStream::TestStream_ZLibGZip() | |
240 | { | |
3e5f6c1c | 241 | // Only use default compression level, as this test is |
340da6ae | 242 | // for testing if the streams can determine the stream type info them self... |
7735998c VS |
243 | doTestStreamData(wxZLIB_AUTO, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION); |
244 | doTestStreamData(wxZLIB_AUTO, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION); | |
340da6ae VS |
245 | } |
246 | ||
247 | void zlibStream::Decompress_BadData() | |
248 | { | |
249 | // Setup the bad data stream and the zlib stream. | |
250 | wxMemoryInputStream memstream_in(GetDataBuffer(), DATABUFFER_SIZE); | |
251 | CPPUNIT_ASSERT(memstream_in.IsOk()); | |
252 | wxZlibInputStream zstream_in(memstream_in); | |
3e5f6c1c | 253 | CPPUNIT_ASSERT(zstream_in.IsOk()); // We did not yet read from the stream |
340da6ae VS |
254 | // so it should still be OK. |
255 | // Try to force the stream to go to bad status. | |
256 | CPPUNIT_ASSERT(!zstream_in.Eof()); | |
257 | if (zstream_in.IsOk()) | |
258 | zstream_in.GetC(); | |
259 | ||
260 | // Because of the bad data in the input stream the zlib | |
261 | // stream should be marked as NOT OK. | |
262 | CPPUNIT_ASSERT(!zstream_in.IsOk()); | |
263 | } | |
264 | ||
7735998c VS |
265 | void zlibStream::Decompress_wx251_zlib114_Data_NoHeader() |
266 | { | |
267 | const unsigned char data[] = {171,202,201,76,82,72,73,44,73,84,72,46,74,77,44,73,77,81,40,207,44,201,80,40,175,8,207,76,73,79,45,41,86,48,210,51,213,171,80,136,246,77,44,74,206,80,48,50,143,213,1,202,69,249,120,58,197,251,249,199,123,184,58,186,184,6,233,40,84,129,12,49,212,51,212,51,1,0,32}; | |
268 | const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4"; | |
269 | const size_t data_size = sizeof(data); | |
270 | const size_t value_size = strlen(value); | |
271 | // We need to specify wxZLIB_NO_HEADER because wxZLIB_AUTO can't find it his self. | |
272 | doDecompress_ExternalData(data, value, data_size, value_size, wxZLIB_NO_HEADER); | |
273 | } | |
340da6ae | 274 | |
7735998c VS |
275 | void zlibStream::Decompress_wx251_zlib114_Data_ZLib() |
276 | { | |
277 | const unsigned char data[] = {120,156,171,202,201,76,82,72,73,44,73,84,72,46,74,77,44,73,77,81,40,207,44,201,80,40,175,8,207,76,73,79,45,41,86,48,210,51,213,171,80,136,246,77,44,74,206,80,48,50,143,213,1,202,69,249,120,58,197,131,8,29,133,42,144,126,67,61,67,61,19,0,191,86,23,216}; | |
278 | const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4"; | |
279 | const size_t data_size = sizeof(data); | |
280 | const size_t value_size = strlen(value); | |
281 | doDecompress_ExternalData(data, value, data_size, value_size); | |
282 | } | |
340da6ae | 283 | |
7735998c VS |
284 | void zlibStream::Decompress_gzip135Data() |
285 | { | |
286 | // Compressed data was on the command line with gzip 1.3.5. | |
287 | const unsigned char gzip135_data[] = {31,139,8,0,177,248,112,64,4,3,115,206,207,45,40,74,45,46,78,77,81,72,73,44,73,84,72,46,74,77,44,1,114,202,51,75,50,20,220,253,66,21,210,171,50,11,20,12,245,140,245,76,185,0,1,107,16,80,44,0,0,0,0}; | |
288 | const char *gzip135_value = "Compressed data created with GNU gzip 1.3.5\n"; | |
289 | // Size of the value and date items. | |
290 | const size_t data_size = sizeof(gzip135_data); | |
291 | const size_t value_size = strlen(gzip135_value); | |
340da6ae | 292 | |
7735998c VS |
293 | // Perform a generic data test on the data. |
294 | doDecompress_ExternalData(gzip135_data, gzip135_value, data_size, value_size); | |
340da6ae VS |
295 | } |
296 | ||
297 | const char *zlibStream::GetDataBuffer() | |
298 | { | |
299 | return m_DataBuffer; | |
300 | } | |
301 | ||
302 | const unsigned char *zlibStream::GetCompressedData() | |
303 | { | |
304 | if (!m_pCompressedData) | |
305 | { | |
340da6ae VS |
306 | // Construct the compressed data live. |
307 | wxMemoryOutputStream memstream_out; | |
308 | { | |
309 | const char *buf = "01234567890123456789012345678901234567890123456789"; /* = 50 */ | |
310 | wxZlibOutputStream zstream_out(memstream_out); | |
311 | zstream_out.Write(buf, strlen(buf)); | |
312 | } | |
313 | ||
3e5f6c1c | 314 | // Copy the to the |
340da6ae VS |
315 | m_SizeCompressedData = memstream_out.GetSize(); |
316 | m_pCompressedData = new unsigned char[m_SizeCompressedData]; | |
317 | memstream_out.CopyTo(m_pCompressedData, m_SizeCompressedData); | |
340da6ae VS |
318 | } |
319 | ||
3e5f6c1c | 320 | CPPUNIT_ASSERT(m_pCompressedData != NULL); |
340da6ae VS |
321 | return m_pCompressedData; |
322 | } | |
323 | ||
51acf83b | 324 | void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf) |
340da6ae | 325 | { |
0e43839c | 326 | size_t fail_pos; |
340da6ae | 327 | char last_value = 0; |
0e43839c | 328 | bool bWasEOF; |
340da6ae VS |
329 | |
330 | { // Part one: Create a compressed file. | |
331 | wxFileOutputStream fstream_out(FILENAME_GZ); | |
332 | CPPUNIT_ASSERT(fstream_out.IsOk()); | |
340da6ae | 333 | { |
7735998c VS |
334 | wxZlibOutputStream zstream_out(fstream_out, compress_level, output_flag); |
335 | CPPUNIT_ASSERT_MESSAGE("Could not create the output stream", zstream_out.IsOk()); | |
336 | ||
51acf83b VZ |
337 | if (buf) |
338 | zstream_out.SetDictionary(*buf); | |
339 | ||
7735998c VS |
340 | // Next: Compress some data so the file is containing something. |
341 | zstream_out.Write(GetDataBuffer(), DATABUFFER_SIZE); | |
340da6ae VS |
342 | } |
343 | ||
7735998c VS |
344 | // Next thing is required by zlib versions pre 1.2.0. |
345 | if (input_flag == wxZLIB_NO_HEADER) | |
346 | fstream_out.PutC(' '); | |
340da6ae VS |
347 | } |
348 | ||
3e5f6c1c | 349 | { // Part two: Verify that the compressed data when uncompressed |
340da6ae VS |
350 | // matches the original data. |
351 | wxFileInputStream fstream_in(FILENAME_GZ); | |
352 | CPPUNIT_ASSERT(fstream_in.IsOk()); | |
353 | wxZlibInputStream zstream_in(fstream_in, input_flag); | |
354 | CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in.IsOk()); | |
355 | ||
51acf83b VZ |
356 | if (buf) |
357 | zstream_in.SetDictionary(*buf); | |
358 | ||
340da6ae VS |
359 | // Next: Check char per char if the returned data is valid. |
360 | const char *pbuf = GetDataBuffer(); | |
361 | for (fail_pos = 0; !zstream_in.Eof(); fail_pos++) | |
362 | { | |
363 | last_value = zstream_in.GetC(); | |
3e5f6c1c | 364 | if (zstream_in.LastRead() != 1 || |
7735998c | 365 | last_value != pbuf[fail_pos]) |
340da6ae VS |
366 | break; |
367 | } | |
368 | ||
369 | bWasEOF = zstream_in.Eof(); | |
370 | } | |
371 | ||
372 | // Remove the temp file... | |
373 | ::wxRemoveFile(FILENAME_GZ); | |
374 | ||
375 | // Check state of the verify action. | |
376 | if (fail_pos != DATABUFFER_SIZE || !bWasEOF) | |
377 | { | |
ea75e99e | 378 | wxString msg; |
9a83f860 VZ |
379 | msg << wxT("Wrong data item at pos ") << fail_pos |
380 | << wxT(" (Org_val ") << GetDataBuffer()[fail_pos] | |
381 | << wxT(" != Zlib_val ") << last_value | |
382 | << wxT("), with compression level ") << compress_level; | |
340da6ae VS |
383 | CPPUNIT_FAIL(string(msg.mb_str())); |
384 | } | |
385 | } | |
386 | ||
7735998c VS |
387 | void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag) |
388 | { | |
389 | // See that the input is ok. | |
390 | wxASSERT(data != NULL); | |
391 | wxASSERT(value != NULL); | |
392 | wxASSERT(data_size > 0); | |
393 | wxASSERT(value_size > 0); | |
394 | ||
395 | // Quickly try to see if the data is valid. | |
396 | switch (flag) | |
397 | { | |
398 | case wxZLIB_NO_HEADER: | |
399 | break; | |
7735998c VS |
400 | case wxZLIB_ZLIB: |
401 | if (!(data_size >= 1 && data[0] == 0x78)) | |
43b2d5e7 | 402 | { |
9a83f860 | 403 | wxLogError(wxT("zlib data seems to not be zlib data!")); |
43b2d5e7 | 404 | } |
7735998c VS |
405 | break; |
406 | case wxZLIB_GZIP: | |
407 | if (!(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B)) | |
43b2d5e7 | 408 | { |
9a83f860 | 409 | wxLogError(wxT("gzip data seems to not be gzip data!")); |
43b2d5e7 | 410 | } |
7735998c VS |
411 | break; |
412 | case wxZLIB_AUTO: | |
3e5f6c1c | 413 | if (!(data_size >= 1 && data[0] == 0x78) || |
7735998c | 414 | !(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B)) |
43b2d5e7 | 415 | { |
9a83f860 | 416 | wxLogError(wxT("Data seems to not be zlib or gzip data!")); |
43b2d5e7 | 417 | } |
7735998c | 418 | default: |
9a83f860 | 419 | wxLogError(wxT("Unknown flag, skipping quick test.")); |
7735998c VS |
420 | }; |
421 | ||
422 | // Creat the needed streams. | |
423 | wxMemoryInputStream memstream_in(data, data_size); | |
424 | CPPUNIT_ASSERT(memstream_in.IsOk()); | |
425 | wxZlibInputStream zstream_in(memstream_in, flag); | |
426 | CPPUNIT_ASSERT(zstream_in.IsOk()); | |
427 | ||
428 | bool bValueEq = true; | |
429 | size_t i; | |
430 | for (i = 0; !zstream_in.Eof(); i++) | |
431 | { | |
432 | char last_value = zstream_in.GetC(); | |
433 | ||
434 | // First check if it is a valid read. | |
435 | if (zstream_in.LastRead() == 1) | |
436 | { | |
437 | // Check the values | |
438 | if (last_value != value[i]) | |
439 | { | |
440 | bValueEq = false; | |
441 | break; | |
442 | } | |
443 | } | |
444 | else | |
445 | { | |
446 | // If the read failed and turned the stream to Eof we stop reading. | |
447 | if (zstream_in.Eof()) | |
448 | break; | |
449 | ||
450 | CPPUNIT_ASSERT_MESSAGE("Stream is no longer ok!", zstream_in.IsOk()); | |
451 | } | |
452 | ||
453 | // Don't go over the end of the value buffer... | |
454 | if (i == value_size) | |
455 | { | |
456 | // And if we do then try to see how long the stream actually is. | |
457 | while (!zstream_in.Eof()) | |
458 | { | |
459 | // Move one item along in the stream. | |
460 | (void)zstream_in.GetC(); | |
461 | i++; | |
3e5f6c1c | 462 | |
7735998c VS |
463 | // Check if we are in an infinite loop by multiplying value_size |
464 | // by 5 to have a *much* bigger range then the real range. | |
d13b34d3 DS |
465 | // Note: In case you ask yourself, why 5, the answer is no reason... |
466 | // it is not too big and not to small a size, nothing more | |
7735998c | 467 | // nothing less to it. |
3e5f6c1c | 468 | if (i > (value_size*5)) |
7735998c VS |
469 | { |
470 | // Note: Please make sure Input_Eof test passed. | |
471 | CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop"); | |
472 | return; | |
473 | } | |
474 | } | |
475 | } | |
476 | } | |
477 | ||
3e5f6c1c | 478 | CPPUNIT_ASSERT_MESSAGE("Could not decompress the compressed data, original and restored value did not match.", |
7735998c VS |
479 | i == value_size && bValueEq); |
480 | } | |
481 | ||
3e5f6c1c WS |
482 | wxZlibInputStream *zlibStream::DoCreateInStream() |
483 | { | |
340da6ae VS |
484 | const unsigned char *buf = GetCompressedData(); |
485 | m_pTmpMemInStream = new wxMemoryInputStream(buf, m_SizeCompressedData); | |
486 | CPPUNIT_ASSERT(m_pTmpMemInStream->IsOk()); | |
487 | wxZlibInputStream *pzstream_in = new wxZlibInputStream(*m_pTmpMemInStream); | |
488 | CPPUNIT_ASSERT(pzstream_in->IsOk()); | |
489 | return pzstream_in; | |
490 | } | |
491 | wxZlibOutputStream *zlibStream::DoCreateOutStream() | |
3e5f6c1c | 492 | { |
340da6ae VS |
493 | m_pTmpMemOutStream = new wxMemoryOutputStream(); |
494 | CPPUNIT_ASSERT(m_pTmpMemOutStream->IsOk()); | |
495 | wxZlibOutputStream *pzstream_out = new wxZlibOutputStream(*m_pTmpMemOutStream); | |
496 | CPPUNIT_ASSERT(pzstream_out->IsOk()); | |
497 | return pzstream_out; | |
498 | } | |
3e5f6c1c WS |
499 | void zlibStream::DoDeleteInStream() |
500 | { | |
340da6ae VS |
501 | delete m_pTmpMemInStream; |
502 | m_pTmpMemInStream = NULL; | |
503 | } | |
3e5f6c1c WS |
504 | void zlibStream::DoDeleteOutStream() |
505 | { | |
340da6ae VS |
506 | delete m_pTmpMemOutStream; |
507 | m_pTmpMemOutStream = NULL; | |
508 | } | |
509 | ||
7735998c VS |
510 | |
511 | void zlibStream::genExtTestData(wxTextOutputStream &out, const char *buf, int flag) | |
512 | { | |
0e43839c VS |
513 | unsigned char *data; |
514 | size_t size; | |
3e5f6c1c | 515 | |
7735998c VS |
516 | { // Gen data |
517 | wxMemoryOutputStream memstream_out; | |
518 | { | |
519 | wxZlibOutputStream zstream_out(memstream_out, wxZ_DEFAULT_COMPRESSION, flag); | |
520 | zstream_out.Write(buf, strlen(buf)); | |
521 | } | |
522 | if (flag == wxZLIB_NO_HEADER) | |
523 | memstream_out.PutC(' '); | |
3e5f6c1c | 524 | |
7735998c VS |
525 | size = memstream_out.GetSize(); |
526 | data = new unsigned char[size]; | |
527 | memstream_out.CopyTo(data, size); | |
528 | } | |
529 | ||
9a83f860 VZ |
530 | out << wxT("void zlibStream::Decompress_wxXXXData()") << wxT("\n"); |
531 | out << wxT("{") << wxT("\n") << wxT(" const unsigned char data[] = {"); | |
3e5f6c1c | 532 | |
7735998c VS |
533 | size_t i; |
534 | for (i = 0; i < size; i++) | |
535 | { | |
536 | if (i+1 != size) | |
9a83f860 | 537 | out << wxString::Format(wxT("%d,"), data[i]); |
7735998c | 538 | else |
9a83f860 | 539 | out << wxString::Format(wxT("%d"), data[i]); |
3e5f6c1c | 540 | } |
7735998c VS |
541 | delete [] data; |
542 | ||
9a83f860 VZ |
543 | out << wxT("};") << wxT("\n"); |
544 | out << wxT(" const char *value = \"") << wxString(buf, wxConvUTF8) << wxT("\";") << wxT("\n"); | |
545 | out << wxT(" const size_t data_size = sizeof(data);") << wxT("\n"); | |
546 | out << wxT(" const size_t value_size = strlen(value);") << wxT("\n"); | |
547 | out << wxT(" doDecompress_ExternalData(data, value, data_size, value_size);") << wxT("\n"); | |
548 | out << wxT("}") << wxT("\n"); | |
7735998c VS |
549 | } |
550 | ||
551 | ||
340da6ae VS |
552 | // Register the stream sub suite, by using some stream helper macro. |
553 | // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite()) | |
554 | STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(zlibStream) | |
555 |