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