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