1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/zlibstream.cpp
3 // Purpose: Test wxZlibInputStream/wxZlibOutputStream
4 // Author: Hans Van Leemputten
6 // Copyright: (c) 2004 Hans Van Leemputten
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(__APPLE__)
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers
27 #include "wx/cppunit.h"
28 #include "wx/zstream.h"
29 #include "wx/wfstream.h"
30 #include "wx/mstream.h"
32 #include "wx/txtstrm.h"
38 #define WXTEST_WITH_GZIP_CONDITION(testMethod) \
39 WXTEST_WITH_CONDITION( COMPOSE_TEST_NAME(zlibStream), wxZlibInputStream::CanHandleGZip() && wxZlibOutputStream::CanHandleGZip(), testMethod )
41 #define DATABUFFER_SIZE 1024
43 static const wxString FILENAME_GZ
= _T("zlibtest.gz");
45 ///////////////////////////////////////////////////////////////////////////////
48 // Try to fully test wxZlibInputStream and wxZlibOutputStream
50 class zlibStream
: public BaseStreamTestCase
<wxZlibInputStream
, wxZlibOutputStream
>
54 virtual ~zlibStream();
56 CPPUNIT_TEST_SUITE(zlibStream
);
57 // Base class stream tests the zlibstream supports.
58 CPPUNIT_TEST_FAIL(Input_GetSize
);
59 CPPUNIT_TEST(Input_GetC
);
60 CPPUNIT_TEST(Input_Read
);
61 CPPUNIT_TEST(Input_Eof
);
62 CPPUNIT_TEST(Input_LastRead
);
63 CPPUNIT_TEST_FAIL(Input_SeekI
);
64 CPPUNIT_TEST(Input_TellI
);
65 CPPUNIT_TEST(Input_Peek
);
66 CPPUNIT_TEST(Input_Ungetch
);
68 CPPUNIT_TEST(Output_PutC
);
69 CPPUNIT_TEST(Output_Write
);
70 CPPUNIT_TEST(Output_LastWrite
);
71 CPPUNIT_TEST_FAIL(Output_SeekO
);
72 CPPUNIT_TEST(Output_TellO
);
74 // Other test specific for zlib stream test case.
75 CPPUNIT_TEST(TestStream_NoHeader_Default
);
76 CPPUNIT_TEST(TestStream_NoHeader_NoComp
);
77 CPPUNIT_TEST(TestStream_NoHeader_SpeedComp
);
78 CPPUNIT_TEST(TestStream_NoHeader_BestComp
);
79 CPPUNIT_TEST(TestStream_ZLib_Default
);
80 CPPUNIT_TEST(TestStream_ZLib_NoComp
);
81 CPPUNIT_TEST(TestStream_ZLib_SpeedComp
);
82 CPPUNIT_TEST(TestStream_ZLib_BestComp
);
83 WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Default
);
84 WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_NoComp
);
85 WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_SpeedComp
);
86 WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_BestComp
);
87 WXTEST_WITH_GZIP_CONDITION(TestStream_ZLibGZip
);
88 CPPUNIT_TEST(Decompress_BadData
);
89 #if WXWIN_COMPATIBILITY_2_4
90 CPPUNIT_TEST(Decompress_wx24Data
);
92 CPPUNIT_TEST(Decompress_wx251_zlib114_Data_NoHeader
);
93 CPPUNIT_TEST(Decompress_wx251_zlib114_Data_ZLib
);
94 WXTEST_WITH_GZIP_CONDITION(Decompress_gzip135Data
);
95 CPPUNIT_TEST_SUITE_END();
98 // Test different stream construct settings.
99 void TestStream_NoHeader_Default();
100 void TestStream_NoHeader_NoComp();
101 void TestStream_NoHeader_SpeedComp();
102 void TestStream_NoHeader_BestComp();
103 void TestStream_ZLib_Default();
104 void TestStream_ZLib_NoComp();
105 void TestStream_ZLib_SpeedComp();
106 void TestStream_ZLib_BestComp();
107 void TestStream_GZip_Default();
108 void TestStream_GZip_NoComp();
109 void TestStream_GZip_SpeedComp();
110 void TestStream_GZip_BestComp();
111 void TestStream_ZLibGZip();
112 // Try to decompress bad data.
113 void Decompress_BadData();
114 // Decompress data that was compress by an external app.
115 // (like test wx 2.4.2, 2.5.1 and gzip data)
116 // Note: This test is limited in testing range!
117 #if WXWIN_COMPATIBILITY_2_4
118 void Decompress_wx24Data();
120 void Decompress_wx251_zlib114_Data_NoHeader();
121 void Decompress_wx251_zlib114_Data_ZLib();
122 void Decompress_gzip135Data();
125 const char *GetDataBuffer();
126 const unsigned char *GetCompressedData();
127 void doTestStreamData(int input_flag
, int output_flag
, int compress_level
);
128 void doDecompress_ExternalData(const unsigned char *data
, const char *value
, size_t data_size
, size_t value_size
, int flag
= wxZLIB_AUTO
);
131 // Implement base class functions.
132 virtual wxZlibInputStream
*DoCreateInStream();
133 virtual wxZlibOutputStream
*DoCreateOutStream();
134 virtual void DoDeleteInStream();
135 virtual void DoDeleteOutStream();
137 // Helper that can be used to create new wx compatibility tests...
138 // Otherwise not used by the tests.
139 void genExtTestData(wxTextOutputStream
&out
, const char *buf
, int flag
);
142 char m_DataBuffer
[DATABUFFER_SIZE
];
143 size_t m_SizeCompressedData
;
144 unsigned char *m_pCompressedData
;
146 // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream.
147 wxMemoryInputStream
*m_pTmpMemInStream
;
148 wxMemoryOutputStream
*m_pTmpMemOutStream
;
151 zlibStream::zlibStream()
152 :m_SizeCompressedData(0),
153 m_pCompressedData(NULL
),
154 m_pTmpMemInStream(NULL
),
155 m_pTmpMemOutStream(NULL
)
157 // Init the data buffer.
158 for (size_t i
= 0; i
< DATABUFFER_SIZE
; i
++)
159 m_DataBuffer
[i
] = (i
% 0xFF);
161 // Set extra base config settings.
162 m_bSimpleTellITest
= true;
163 m_bSimpleTellOTest
= true;
165 /* Example code on how to produce test data...
167 wxFFileOutputStream fstream_out(_T("gentest.cpp"));
168 wxTextOutputStream out( fstream_out );
170 genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4", wxZLIB_NO_HEADER);
171 genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4", wxZLIB_ZLIB);
176 zlibStream::~zlibStream()
178 delete[] m_pCompressedData
;
180 delete m_pTmpMemInStream
;
181 delete m_pTmpMemOutStream
;
184 void zlibStream::TestStream_NoHeader_Default()
186 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_DEFAULT_COMPRESSION
);
188 void zlibStream::TestStream_NoHeader_NoComp()
190 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_NO_COMPRESSION
);
192 void zlibStream::TestStream_NoHeader_SpeedComp()
194 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_BEST_SPEED
);
196 void zlibStream::TestStream_NoHeader_BestComp()
198 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_BEST_COMPRESSION
);
201 void zlibStream::TestStream_ZLib_Default()
203 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_DEFAULT_COMPRESSION
);
205 void zlibStream::TestStream_ZLib_NoComp()
207 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_NO_COMPRESSION
);
209 void zlibStream::TestStream_ZLib_SpeedComp()
211 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_BEST_SPEED
);
213 void zlibStream::TestStream_ZLib_BestComp()
215 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_BEST_COMPRESSION
);
218 void zlibStream::TestStream_GZip_Default()
220 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_DEFAULT_COMPRESSION
);
222 void zlibStream::TestStream_GZip_NoComp()
224 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_NO_COMPRESSION
);
226 void zlibStream::TestStream_GZip_SpeedComp()
228 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_BEST_SPEED
);
230 void zlibStream::TestStream_GZip_BestComp()
232 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_BEST_COMPRESSION
);
235 void zlibStream::TestStream_ZLibGZip()
237 // Only use default compression level, as this test is
238 // for testing if the streams can determine the stream type info them self...
239 doTestStreamData(wxZLIB_AUTO
, wxZLIB_ZLIB
, wxZ_DEFAULT_COMPRESSION
);
240 doTestStreamData(wxZLIB_AUTO
, wxZLIB_GZIP
, wxZ_DEFAULT_COMPRESSION
);
243 void zlibStream::Decompress_BadData()
245 // Setup the bad data stream and the zlib stream.
246 wxMemoryInputStream
memstream_in(GetDataBuffer(), DATABUFFER_SIZE
);
247 CPPUNIT_ASSERT(memstream_in
.IsOk());
248 wxZlibInputStream
zstream_in(memstream_in
);
249 CPPUNIT_ASSERT(zstream_in
.IsOk()); // We did not yet read from the stream
250 // so it should still be OK.
251 // Try to force the stream to go to bad status.
252 CPPUNIT_ASSERT(!zstream_in
.Eof());
253 if (zstream_in
.IsOk())
256 // Because of the bad data in the input stream the zlib
257 // stream should be marked as NOT OK.
258 CPPUNIT_ASSERT(!zstream_in
.IsOk());
261 #if WXWIN_COMPATIBILITY_2_4
262 void zlibStream::Decompress_wx24Data()
264 // The wx24_value was used in a wxWidgets 2.4(.2)
265 // application to produce wx24_data, using wxZlibOutputStream.
266 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};
267 const char *wx24_value
= "Hello world";
268 // Size of the value and date items.
269 const size_t data_size
= sizeof(wx24_data
);
270 const size_t value_size
= strlen(wx24_value
) + 1; // +1 because the wx24 app also did this.
272 // Perform a generic data test on the data.
273 doDecompress_ExternalData(wx24_data
, wx24_value
, data_size
, value_size
, wxZLIB_24COMPATIBLE
);
277 void zlibStream::Decompress_wx251_zlib114_Data_NoHeader()
279 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};
280 const char *value
= "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4";
281 const size_t data_size
= sizeof(data
);
282 const size_t value_size
= strlen(value
);
283 // We need to specify wxZLIB_NO_HEADER because wxZLIB_AUTO can't find it his self.
284 doDecompress_ExternalData(data
, value
, data_size
, value_size
, wxZLIB_NO_HEADER
);
287 void zlibStream::Decompress_wx251_zlib114_Data_ZLib()
289 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};
290 const char *value
= "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4";
291 const size_t data_size
= sizeof(data
);
292 const size_t value_size
= strlen(value
);
293 doDecompress_ExternalData(data
, value
, data_size
, value_size
);
296 void zlibStream::Decompress_gzip135Data()
298 // Compressed data was on the command line with gzip 1.3.5.
299 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};
300 const char *gzip135_value
= "Compressed data created with GNU gzip 1.3.5\n";
301 // Size of the value and date items.
302 const size_t data_size
= sizeof(gzip135_data
);
303 const size_t value_size
= strlen(gzip135_value
);
305 // Perform a generic data test on the data.
306 doDecompress_ExternalData(gzip135_data
, gzip135_value
, data_size
, value_size
);
309 const char *zlibStream::GetDataBuffer()
314 const unsigned char *zlibStream::GetCompressedData()
316 if (!m_pCompressedData
)
318 // Construct the compressed data live.
319 wxMemoryOutputStream memstream_out
;
321 const char *buf
= "01234567890123456789012345678901234567890123456789"; /* = 50 */
322 wxZlibOutputStream
zstream_out(memstream_out
);
323 zstream_out
.Write(buf
, strlen(buf
));
327 m_SizeCompressedData
= memstream_out
.GetSize();
328 m_pCompressedData
= new unsigned char[m_SizeCompressedData
];
329 memstream_out
.CopyTo(m_pCompressedData
, m_SizeCompressedData
);
332 CPPUNIT_ASSERT(m_pCompressedData
!= NULL
);
333 return m_pCompressedData
;
336 void zlibStream::doTestStreamData(int input_flag
, int output_flag
, int compress_level
)
342 { // Part one: Create a compressed file.
343 wxFileOutputStream
fstream_out(FILENAME_GZ
);
344 CPPUNIT_ASSERT(fstream_out
.IsOk());
346 wxZlibOutputStream
zstream_out(fstream_out
, compress_level
, output_flag
);
347 CPPUNIT_ASSERT_MESSAGE("Could not create the output stream", zstream_out
.IsOk());
349 // Next: Compress some data so the file is containing something.
350 zstream_out
.Write(GetDataBuffer(), DATABUFFER_SIZE
);
353 // Next thing is required by zlib versions pre 1.2.0.
354 if (input_flag
== wxZLIB_NO_HEADER
)
355 fstream_out
.PutC(' ');
358 { // Part two: Verify that the compressed data when uncompressed
359 // matches the original data.
360 wxFileInputStream
fstream_in(FILENAME_GZ
);
361 CPPUNIT_ASSERT(fstream_in
.IsOk());
362 wxZlibInputStream
zstream_in(fstream_in
, input_flag
);
363 CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in
.IsOk());
365 // Next: Check char per char if the returned data is valid.
366 const char *pbuf
= GetDataBuffer();
367 for (fail_pos
= 0; !zstream_in
.Eof(); fail_pos
++)
369 last_value
= zstream_in
.GetC();
370 if (zstream_in
.LastRead() != 1 ||
371 last_value
!= pbuf
[fail_pos
])
375 bWasEOF
= zstream_in
.Eof();
378 // Remove the temp file...
379 ::wxRemoveFile(FILENAME_GZ
);
381 // Check state of the verify action.
382 if (fail_pos
!= DATABUFFER_SIZE
|| !bWasEOF
)
384 wxString
msg(wxString::Format(_T("Wrong data item at pos %d (Org_val %d != Zlib_val %d), with compression level %d"),
385 fail_pos
, GetDataBuffer()[fail_pos
], last_value
, compress_level
));
386 CPPUNIT_FAIL(string(msg
.mb_str()));
390 void zlibStream::doDecompress_ExternalData(const unsigned char *data
, const char *value
, size_t data_size
, size_t value_size
, int flag
)
392 // See that the input is ok.
393 wxASSERT(data
!= NULL
);
394 wxASSERT(value
!= NULL
);
395 wxASSERT(data_size
> 0);
396 wxASSERT(value_size
> 0);
398 // Quickly try to see if the data is valid.
401 case wxZLIB_NO_HEADER
:
403 #if WXWIN_COMPATIBILITY_2_4
404 case wxZLIB_24COMPATIBLE
:
407 if (!(data_size
>= 1 && data
[0] == 0x78))
408 wxLogError(_T("zlib data seems to not be zlib data!"));
411 if (!(data_size
>= 2 && data
[0] == 0x1F && data
[1] == 0x8B))
412 wxLogError(_T("gzip data seems to not be gzip data!"));
415 if (!(data_size
>= 1 && data
[0] == 0x78) ||
416 !(data_size
>= 2 && data
[0] == 0x1F && data
[1] == 0x8B))
417 wxLogError(_T("Data seems to not be zlib or gzip data!"));
419 wxLogError(_T("Unknown flag, skipping quick test."));
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());
428 bool bValueEq
= true;
430 for (i
= 0; !zstream_in
.Eof(); i
++)
432 char last_value
= zstream_in
.GetC();
434 // First check if it is a valid read.
435 if (zstream_in
.LastRead() == 1)
438 if (last_value
!= value
[i
])
446 // If the read failed and turned the stream to Eof we stop reading.
447 if (zstream_in
.Eof())
450 CPPUNIT_ASSERT_MESSAGE("Stream is no longer ok!", zstream_in
.IsOk());
453 // Don't go over the end of the value buffer...
456 // And if we do then try to see how long the stream actually is.
457 while (!zstream_in
.Eof())
459 // Move one item along in the stream.
460 (void)zstream_in
.GetC();
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.
465 // Note: Incase you ask your self, why 5, the answer is no reason...
466 // it is not to big and not to small a size, nothing more
467 // nothing less to it.
468 if (i
> (value_size
*5))
470 // Note: Please make sure Input_Eof test passed.
471 CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop");
478 CPPUNIT_ASSERT_MESSAGE("Could not decompress the compressed data, original and restored value did not match.",
479 i
== value_size
&& bValueEq
);
482 wxZlibInputStream
*zlibStream::DoCreateInStream()
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());
491 wxZlibOutputStream
*zlibStream::DoCreateOutStream()
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());
499 void zlibStream::DoDeleteInStream()
501 delete m_pTmpMemInStream
;
502 m_pTmpMemInStream
= NULL
;
504 void zlibStream::DoDeleteOutStream()
506 delete m_pTmpMemOutStream
;
507 m_pTmpMemOutStream
= NULL
;
511 void zlibStream::genExtTestData(wxTextOutputStream
&out
, const char *buf
, int flag
)
517 wxMemoryOutputStream memstream_out
;
519 wxZlibOutputStream
zstream_out(memstream_out
, wxZ_DEFAULT_COMPRESSION
, flag
);
520 zstream_out
.Write(buf
, strlen(buf
));
522 if (flag
== wxZLIB_NO_HEADER
)
523 memstream_out
.PutC(' ');
525 size
= memstream_out
.GetSize();
526 data
= new unsigned char[size
];
527 memstream_out
.CopyTo(data
, size
);
530 out
<< _T("void zlibStream::Decompress_wxXXXData()") << _T("\n");
531 out
<< _T("{") << _T("\n") << _T(" const unsigned char data[] = {");
534 for (i
= 0; i
< size
; i
++)
537 out
<< wxString::Format(_T("%d,"), data
[i
]);
539 out
<< wxString::Format(_T("%d"), data
[i
]);
543 out
<< _T("};") << _T("\n");
544 out
<< _T(" const char *value = \"") << wxString(buf
, wxConvUTF8
) << _T("\";") << _T("\n");
545 out
<< _T(" const size_t data_size = sizeof(data);") << _T("\n");
546 out
<< _T(" const size_t value_size = strlen(value);") << _T("\n");
547 out
<< _T(" doDecompress_ExternalData(data, value, data_size, value_size);") << _T("\n");
548 out
<< _T("}") << _T("\n");
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
)