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"
35 using namespace CppUnit
;
37 #define DATABUFFER_SIZE 1024
39 static const wxString FILENAME_GZ
= _T("zlibtest.gz");
41 ///////////////////////////////////////////////////////////////////////////////
44 // Try to fully test wxZlibInputStream and wxFileOutputStream
46 class zlibStream
: public BaseStreamTestCase
<wxZlibInputStream
, wxZlibOutputStream
>
50 virtual ~zlibStream();
52 CPPUNIT_TEST_SUITE(zlibStream
);
53 // Base class stream tests the zlibstream supports.
54 CPPUNIT_TEST_FAIL(Input_GetSize
);
55 CPPUNIT_TEST(Input_GetC
);
56 CPPUNIT_TEST(Input_Read
);
57 CPPUNIT_TEST(Input_Eof
);
58 CPPUNIT_TEST(Input_LastRead
);
59 CPPUNIT_TEST_FAIL(Input_SeekI
);
60 CPPUNIT_TEST(Input_TellI
);
61 CPPUNIT_TEST(Input_Peek
);
62 CPPUNIT_TEST(Input_Ungetch
);
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
);
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 CPPUNIT_TEST(TestStream_GZip_Default
);
80 CPPUNIT_TEST(TestStream_GZip_NoComp
);
81 CPPUNIT_TEST(TestStream_GZip_SpeedComp
);
82 CPPUNIT_TEST(TestStream_GZip_BestComp
);
83 CPPUNIT_TEST(TestStream_ZLibGZip
);
84 CPPUNIT_TEST(Decompress_BadData
);
85 CPPUNIT_TEST(Decompress_wx24Data
);
86 CPPUNIT_TEST_SUITE_END();
89 // Test different stream construct settings.
90 void TestStream_NoHeader_Default();
91 void TestStream_NoHeader_NoComp();
92 void TestStream_NoHeader_SpeedComp();
93 void TestStream_NoHeader_BestComp();
94 void TestStream_ZLib_Default();
95 void TestStream_ZLib_NoComp();
96 void TestStream_ZLib_SpeedComp();
97 void TestStream_ZLib_BestComp();
98 void TestStream_GZip_Default();
99 void TestStream_GZip_NoComp();
100 void TestStream_GZip_SpeedComp();
101 void TestStream_GZip_BestComp();
102 void TestStream_ZLibGZip();
103 // Try to decompress bad data.
104 void Decompress_BadData();
105 // Decompress a data file created with wx 2.4.
106 // Note: This test is limited in testing range!
107 void Decompress_wx24Data();
110 const char *GetDataBuffer();
111 const unsigned char *GetCompressedData();
112 void doTestStreamData(int input_flag
, int output_flag
, int compress_level
);
115 // Implement base class functions.
116 virtual wxZlibInputStream
*DoCreateInStream();
117 virtual wxZlibOutputStream
*DoCreateOutStream();
118 virtual void DoDeleteInStream();
119 virtual void DoDeleteOutStream();
122 char m_DataBuffer
[DATABUFFER_SIZE
];
123 size_t m_SizeCompressedData
;
124 unsigned char *m_pCompressedData
;
126 // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream.
127 wxMemoryInputStream
*m_pTmpMemInStream
;
128 wxMemoryOutputStream
*m_pTmpMemOutStream
;
131 zlibStream::zlibStream()
132 :m_SizeCompressedData(0),
133 m_pCompressedData(NULL
),
134 m_pTmpMemInStream(NULL
),
135 m_pTmpMemOutStream(NULL
)
137 // Init the data buffer.
138 for (size_t i
= 0; i
< DATABUFFER_SIZE
; i
++)
139 m_DataBuffer
[i
] = (i
% 0xFF);
141 // Set extra base config settings.
142 m_bSimpleTellITest
= true;
143 m_bSimpleTellOTest
= true;
146 zlibStream::~zlibStream()
148 delete m_pCompressedData
;
150 delete m_pTmpMemInStream
;
151 delete m_pTmpMemOutStream
;
154 void zlibStream::TestStream_NoHeader_Default()
156 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_DEFAULT_COMPRESSION
);
158 void zlibStream::TestStream_NoHeader_NoComp()
160 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_NO_COMPRESSION
);
162 void zlibStream::TestStream_NoHeader_SpeedComp()
164 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_BEST_SPEED
);
166 void zlibStream::TestStream_NoHeader_BestComp()
168 doTestStreamData(wxZLIB_NO_HEADER
, wxZLIB_NO_HEADER
, wxZ_BEST_COMPRESSION
);
171 void zlibStream::TestStream_ZLib_Default()
173 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_DEFAULT_COMPRESSION
);
175 void zlibStream::TestStream_ZLib_NoComp()
177 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_NO_COMPRESSION
);
179 void zlibStream::TestStream_ZLib_SpeedComp()
181 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_BEST_SPEED
);
183 void zlibStream::TestStream_ZLib_BestComp()
185 doTestStreamData(wxZLIB_ZLIB
, wxZLIB_ZLIB
, wxZ_BEST_COMPRESSION
);
188 void zlibStream::TestStream_GZip_Default()
190 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_DEFAULT_COMPRESSION
);
192 void zlibStream::TestStream_GZip_NoComp()
194 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_NO_COMPRESSION
);
196 void zlibStream::TestStream_GZip_SpeedComp()
198 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_BEST_SPEED
);
200 void zlibStream::TestStream_GZip_BestComp()
202 doTestStreamData(wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_BEST_COMPRESSION
);
205 void zlibStream::TestStream_ZLibGZip()
207 // Only use default compression level, as this test is
208 // for testing if the streams can determine the stream type info them self...
209 doTestStreamData(wxZLIB_ZLIB
|wxZLIB_GZIP
, wxZLIB_ZLIB
, wxZ_DEFAULT_COMPRESSION
);
210 doTestStreamData(wxZLIB_ZLIB
|wxZLIB_GZIP
, wxZLIB_GZIP
, wxZ_DEFAULT_COMPRESSION
);
213 void zlibStream::Decompress_BadData()
215 // Setup the bad data stream and the zlib stream.
216 wxMemoryInputStream
memstream_in(GetDataBuffer(), DATABUFFER_SIZE
);
217 CPPUNIT_ASSERT(memstream_in
.IsOk());
218 wxZlibInputStream
zstream_in(memstream_in
);
219 CPPUNIT_ASSERT(zstream_in
.IsOk()); // We did not yet read from the stream
220 // so it should still be OK.
221 // Try to force the stream to go to bad status.
222 CPPUNIT_ASSERT(!zstream_in
.Eof());
223 if (zstream_in
.IsOk())
226 // Because of the bad data in the input stream the zlib
227 // stream should be marked as NOT OK.
228 CPPUNIT_ASSERT(!zstream_in
.IsOk());
231 void zlibStream::Decompress_wx24Data()
233 // The wx24_value was used in a wxWidgets 2.4(.2)
234 // application to produce wx24_data, using wxZlibOutputStream.
235 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};
236 const char *wx24_value
= "Hello world";
237 // Size of the value and date items.
238 const size_t data_size
= sizeof(wx24_data
);
239 const size_t value_size
= strlen(wx24_value
);
241 const unsigned char *buf
= GetCompressedData();
242 m_pTmpMemInStream
= new wxMemoryInputStream(buf
, m_SizeCompressedData
);
244 wxMemoryInputStream
memstream_in(wx24_data
, data_size
);
245 CPPUNIT_ASSERT(memstream_in
.IsOk());
246 wxZlibInputStream
zstream_in(memstream_in
);
247 CPPUNIT_ASSERT(zstream_in
.IsOk());
250 for (i
= 0; !zstream_in
.Eof(); i
++)
252 char last_value
= zstream_in
.GetC();
253 if (last_value
!= wx24_value
[i
])
256 // Don't go over the end of the value buffer...
257 if (wx24_value
[i
] == '\0')
259 // And if we do then try to see how long the stream actually is.
260 for (/* nothing */; !zstream_in
.Eof(); i
++)
262 // Move one item along in the stream.
263 (void)zstream_in
.GetC();
265 // Check if we are in an infinite loop by multiplying value_size
266 // by 5 to have a *much* bigger range then the real range.
267 // Note: Incase you ask your self, why 5, the answer is no reason...
268 // it is not to big and not to small a size, nothing more
269 // nothing less to it.
270 if (i
> (value_size
*5))
272 // Note: Please make sure Input_Eof test passed.
273 CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop");
282 CPPUNIT_ASSERT_MESSAGE("Could not decompress data that was compressed with wxWidgets 2.4.x", i
== (value_size
+ 1));
285 const char *zlibStream::GetDataBuffer()
290 const unsigned char *zlibStream::GetCompressedData()
292 if (!m_pCompressedData
)
295 // Construct the compressed data live.
296 wxMemoryOutputStream memstream_out
;
298 const char *buf
= "01234567890123456789012345678901234567890123456789"; /* = 50 */
299 wxZlibOutputStream
zstream_out(memstream_out
);
300 zstream_out
.Write(buf
, strlen(buf
));
304 m_SizeCompressedData
= memstream_out
.GetSize();
305 m_pCompressedData
= new unsigned char[m_SizeCompressedData
];
306 memstream_out
.CopyTo(m_pCompressedData
, m_SizeCompressedData
);
308 // Or use recorded compressed data.
309 const unsigned char tmp
[] = {120,218,51,48,52,50,54,49,53,51,183,176,52,32,142,197,0,0,3,229,10,9,0};
310 m_SizeCompressedData
= sizeof(tmp
);
311 m_pCompressedData
= new unsigned char[m_SizeCompressedData
+1];
312 memcpy(m_pCompressedData
, tmp
, m_SizeCompressedData
);
316 CPPUNIT_ASSERT(m_pCompressedData
!= NULL
);
317 return m_pCompressedData
;
320 void zlibStream::doTestStreamData(int input_flag
, int output_flag
, int compress_level
)
326 { // Part one: Create a compressed file.
327 wxFileOutputStream
fstream_out(FILENAME_GZ
);
328 CPPUNIT_ASSERT(fstream_out
.IsOk());
329 wxZlibOutputStream
zstream_out(fstream_out
, compress_level
, output_flag
);
330 if (!zstream_out
.IsOk())
332 if (output_flag
== wxZLIB_GZIP
)
333 CPPUNIT_FAIL("Could not create the gzip output stream. Note: gzip requires zlib 1.2+!");
335 CPPUNIT_FAIL("Could not create the output stream");
338 // Next: Compress some data so the file is containing something.
339 zstream_out
.Write(GetDataBuffer(), DATABUFFER_SIZE
);
342 { // Part two: Verify that the compressed data when uncompressed
343 // matches the original data.
344 wxFileInputStream
fstream_in(FILENAME_GZ
);
345 CPPUNIT_ASSERT(fstream_in
.IsOk());
346 wxZlibInputStream
zstream_in(fstream_in
, input_flag
);
347 CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in
.IsOk());
349 // Next: Check char per char if the returned data is valid.
350 const char *pbuf
= GetDataBuffer();
351 for (fail_pos
= 0; !zstream_in
.Eof(); fail_pos
++)
353 last_value
= zstream_in
.GetC();
354 if (last_value
!= pbuf
[fail_pos
])
358 bWasEOF
= zstream_in
.Eof();
361 // Remove the temp file...
362 ::wxRemoveFile(FILENAME_GZ
);
364 // Check state of the verify action.
365 if (fail_pos
!= DATABUFFER_SIZE
|| !bWasEOF
)
367 wxString
msg(wxString::Format(_T("Wrong data item at pos %d (Org_val %d != Zlib_val %d), with compression level %d"),
368 fail_pos
, GetDataBuffer()[fail_pos
], last_value
, compress_level
));
369 CPPUNIT_FAIL(string(msg
.mb_str()));
373 wxZlibInputStream
*zlibStream::DoCreateInStream()
375 const unsigned char *buf
= GetCompressedData();
376 m_pTmpMemInStream
= new wxMemoryInputStream(buf
, m_SizeCompressedData
);
377 CPPUNIT_ASSERT(m_pTmpMemInStream
->IsOk());
378 wxZlibInputStream
*pzstream_in
= new wxZlibInputStream(*m_pTmpMemInStream
);
379 CPPUNIT_ASSERT(pzstream_in
->IsOk());
382 wxZlibOutputStream
*zlibStream::DoCreateOutStream()
384 m_pTmpMemOutStream
= new wxMemoryOutputStream();
385 CPPUNIT_ASSERT(m_pTmpMemOutStream
->IsOk());
386 wxZlibOutputStream
*pzstream_out
= new wxZlibOutputStream(*m_pTmpMemOutStream
);
387 CPPUNIT_ASSERT(pzstream_out
->IsOk());
390 void zlibStream::DoDeleteInStream()
392 delete m_pTmpMemInStream
;
393 m_pTmpMemInStream
= NULL
;
395 void zlibStream::DoDeleteOutStream()
397 delete m_pTmpMemOutStream
;
398 m_pTmpMemOutStream
= NULL
;
401 // Register the stream sub suite, by using some stream helper macro.
402 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
403 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(zlibStream
)