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