]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/zlibstream.cpp
commited streams test suite (not part of build yet, coming soon)
[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 "bstream.h"
33
34 using namespace std;
35 using namespace CppUnit;
36
37 #define DATABUFFER_SIZE 1024
38
39 static const wxString FILENAME_GZ = _T("zlibtest.gz");
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // The test case
43 //
44 // Try to fully test wxZlibInputStream and wxFileOutputStream
45
46 class zlibStream : public BaseStreamTestCase<wxZlibInputStream, wxZlibOutputStream>
47 {
48 public:
49 zlibStream();
50 virtual ~zlibStream();
51
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);
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 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();
87
88 protected:
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();
108
109 private:
110 const char *GetDataBuffer();
111 const unsigned char *GetCompressedData();
112 void doTestStreamData(int input_flag, int output_flag, int compress_level);
113
114 private:
115 // Implement base class functions.
116 virtual wxZlibInputStream *DoCreateInStream();
117 virtual wxZlibOutputStream *DoCreateOutStream();
118 virtual void DoDeleteInStream();
119 virtual void DoDeleteOutStream();
120
121 private:
122 char m_DataBuffer[DATABUFFER_SIZE];
123 size_t m_SizeCompressedData;
124 unsigned char *m_pCompressedData;
125
126 // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream.
127 wxMemoryInputStream *m_pTmpMemInStream;
128 wxMemoryOutputStream *m_pTmpMemOutStream;
129 };
130
131 zlibStream::zlibStream()
132 :m_SizeCompressedData(0),
133 m_pCompressedData(NULL),
134 m_pTmpMemInStream(NULL),
135 m_pTmpMemOutStream(NULL)
136 {
137 // Init the data buffer.
138 for (size_t i = 0; i < DATABUFFER_SIZE; i++)
139 m_DataBuffer[i] = (i % 0xFF);
140
141 // Set extra base config settings.
142 m_bSimpleTellITest = true;
143 m_bSimpleTellOTest = true;
144 }
145
146 zlibStream::~zlibStream()
147 {
148 delete m_pCompressedData;
149
150 delete m_pTmpMemInStream;
151 delete m_pTmpMemOutStream;
152 }
153
154 void zlibStream::TestStream_NoHeader_Default()
155 {
156 doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION);
157 }
158 void zlibStream::TestStream_NoHeader_NoComp()
159 {
160 doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_NO_COMPRESSION);
161 }
162 void zlibStream::TestStream_NoHeader_SpeedComp()
163 {
164 doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_SPEED);
165 }
166 void zlibStream::TestStream_NoHeader_BestComp()
167 {
168 doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_COMPRESSION);
169 }
170
171 void zlibStream::TestStream_ZLib_Default()
172 {
173 doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION);
174 }
175 void zlibStream::TestStream_ZLib_NoComp()
176 {
177 doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_NO_COMPRESSION);
178 }
179 void zlibStream::TestStream_ZLib_SpeedComp()
180 {
181 doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_SPEED);
182 }
183 void zlibStream::TestStream_ZLib_BestComp()
184 {
185 doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_COMPRESSION);
186 }
187
188 void zlibStream::TestStream_GZip_Default()
189 {
190 doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION);
191 }
192 void zlibStream::TestStream_GZip_NoComp()
193 {
194 doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_NO_COMPRESSION);
195 }
196 void zlibStream::TestStream_GZip_SpeedComp()
197 {
198 doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_SPEED);
199 }
200 void zlibStream::TestStream_GZip_BestComp()
201 {
202 doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_COMPRESSION);
203 }
204
205 void zlibStream::TestStream_ZLibGZip()
206 {
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);
211 }
212
213 void zlibStream::Decompress_BadData()
214 {
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())
224 zstream_in.GetC();
225
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());
229 }
230
231 void zlibStream::Decompress_wx24Data()
232 {
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);
240
241 const unsigned char *buf = GetCompressedData();
242 m_pTmpMemInStream = new wxMemoryInputStream(buf, m_SizeCompressedData);
243
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());
248
249 size_t i;
250 for (i = 0; !zstream_in.Eof(); i++)
251 {
252 char last_value = zstream_in.GetC();
253 if (last_value != wx24_value[i])
254 break;
255
256 // Don't go over the end of the value buffer...
257 if (wx24_value[i] == '\0')
258 {
259 // And if we do then try to see how long the stream actually is.
260 for (/* nothing */; !zstream_in.Eof(); i++)
261 {
262 // Move one item along in the stream.
263 (void)zstream_in.GetC();
264
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))
271 {
272 // Note: Please make sure Input_Eof test passed.
273 CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop");
274 return;
275 }
276 }
277
278 break;
279 }
280 }
281
282 CPPUNIT_ASSERT_MESSAGE("Could not decompress data that was compressed with wxWidgets 2.4.x", i == (value_size + 1));
283 }
284
285 const char *zlibStream::GetDataBuffer()
286 {
287 return m_DataBuffer;
288 }
289
290 const unsigned char *zlibStream::GetCompressedData()
291 {
292 if (!m_pCompressedData)
293 {
294 #if 1
295 // Construct the compressed data live.
296 wxMemoryOutputStream memstream_out;
297 {
298 const char *buf = "01234567890123456789012345678901234567890123456789"; /* = 50 */
299 wxZlibOutputStream zstream_out(memstream_out);
300 zstream_out.Write(buf, strlen(buf));
301 }
302
303 // Copy the to the
304 m_SizeCompressedData = memstream_out.GetSize();
305 m_pCompressedData = new unsigned char[m_SizeCompressedData];
306 memstream_out.CopyTo(m_pCompressedData, m_SizeCompressedData);
307 #else
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);
313 #endif
314 }
315
316 CPPUNIT_ASSERT(m_pCompressedData != NULL);
317 return m_pCompressedData;
318 }
319
320 void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level)
321 {
322 size_t fail_pos = 0;
323 char last_value = 0;
324 bool bWasEOF = true;
325
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())
331 {
332 if (output_flag == wxZLIB_GZIP)
333 CPPUNIT_FAIL("Could not create the gzip output stream. Note: gzip requires zlib 1.2+!");
334 else
335 CPPUNIT_FAIL("Could not create the output stream");
336 }
337
338 // Next: Compress some data so the file is containing something.
339 zstream_out.Write(GetDataBuffer(), DATABUFFER_SIZE);
340 }
341
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());
348
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++)
352 {
353 last_value = zstream_in.GetC();
354 if (last_value != pbuf[fail_pos])
355 break;
356 }
357
358 bWasEOF = zstream_in.Eof();
359 }
360
361 // Remove the temp file...
362 ::wxRemoveFile(FILENAME_GZ);
363
364 // Check state of the verify action.
365 if (fail_pos != DATABUFFER_SIZE || !bWasEOF)
366 {
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()));
370 }
371 }
372
373 wxZlibInputStream *zlibStream::DoCreateInStream()
374 {
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());
380 return pzstream_in;
381 }
382 wxZlibOutputStream *zlibStream::DoCreateOutStream()
383 {
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());
388 return pzstream_out;
389 }
390 void zlibStream::DoDeleteInStream()
391 {
392 delete m_pTmpMemInStream;
393 m_pTmpMemInStream = NULL;
394 }
395 void zlibStream::DoDeleteOutStream()
396 {
397 delete m_pTmpMemOutStream;
398 m_pTmpMemOutStream = NULL;
399 }
400
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)
404