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