2004-04-01 05:13:47 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/streams/zlibstream.cpp
|
|
|
|
// Purpose: Test wxZlibInputStream/wxZlibOutputStream
|
|
|
|
// Author: Hans Van Leemputten
|
|
|
|
// Copyright: (c) 2004 Hans Van Leemputten
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2004-04-01 05:13:47 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
2004-11-22 00:00:19 -05:00
|
|
|
// and "wx/cppunit.h"
|
|
|
|
#include "testprec.h"
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/zstream.h"
|
|
|
|
#include "wx/wfstream.h"
|
|
|
|
#include "wx/mstream.h"
|
2004-04-18 15:26:20 -04:00
|
|
|
#include "wx/txtstrm.h"
|
2009-03-06 11:26:15 -05:00
|
|
|
#include "wx/buffer.h"
|
2004-04-18 15:26:20 -04:00
|
|
|
|
2004-04-01 05:13:47 -05:00
|
|
|
#include "bstream.h"
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
using std::string;
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
#define DATABUFFER_SIZE 1024
|
|
|
|
|
|
|
|
static const wxString FILENAME_GZ = wxT("zlibtest.gz");
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The test case
|
|
|
|
//
|
2004-04-11 16:59:33 -04:00
|
|
|
// Try to fully test wxZlibInputStream and wxZlibOutputStream
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
class zlibStream : public BaseStreamTestCase<wxZlibInputStream, wxZlibOutputStream>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
zlibStream();
|
|
|
|
virtual ~zlibStream();
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(zlibStream);
|
|
|
|
// Base class stream tests the zlibstream supports.
|
2017-10-31 17:04:12 -04:00
|
|
|
CPPUNIT_TEST(Input_GetSizeFail);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST(Input_GetC);
|
|
|
|
CPPUNIT_TEST(Input_Read);
|
|
|
|
CPPUNIT_TEST(Input_Eof);
|
|
|
|
CPPUNIT_TEST(Input_LastRead);
|
2007-03-17 11:38:09 -04:00
|
|
|
CPPUNIT_TEST(Input_CanRead);
|
2017-10-31 17:04:12 -04:00
|
|
|
CPPUNIT_TEST(Input_SeekIFail);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST(Input_TellI);
|
|
|
|
CPPUNIT_TEST(Input_Peek);
|
|
|
|
CPPUNIT_TEST(Input_Ungetch);
|
|
|
|
|
|
|
|
CPPUNIT_TEST(Output_PutC);
|
|
|
|
CPPUNIT_TEST(Output_Write);
|
|
|
|
CPPUNIT_TEST(Output_LastWrite);
|
2017-10-31 17:04:12 -04:00
|
|
|
CPPUNIT_TEST(Output_SeekOFail);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST(Output_TellO);
|
|
|
|
|
|
|
|
// Other test specific for zlib stream test case.
|
|
|
|
CPPUNIT_TEST(TestStream_NoHeader_Default);
|
|
|
|
CPPUNIT_TEST(TestStream_NoHeader_NoComp);
|
|
|
|
CPPUNIT_TEST(TestStream_NoHeader_SpeedComp);
|
|
|
|
CPPUNIT_TEST(TestStream_NoHeader_BestComp);
|
2009-03-06 11:26:15 -05:00
|
|
|
CPPUNIT_TEST(TestStream_NoHeader_Dictionary);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST(TestStream_ZLib_Default);
|
|
|
|
CPPUNIT_TEST(TestStream_ZLib_NoComp);
|
|
|
|
CPPUNIT_TEST(TestStream_ZLib_SpeedComp);
|
|
|
|
CPPUNIT_TEST(TestStream_ZLib_BestComp);
|
2017-10-30 09:16:11 -04:00
|
|
|
CPPUNIT_TEST(TestStream_GZip_Default);
|
|
|
|
CPPUNIT_TEST(TestStream_GZip_NoComp);
|
|
|
|
CPPUNIT_TEST(TestStream_GZip_SpeedComp);
|
|
|
|
CPPUNIT_TEST(TestStream_GZip_BestComp);
|
|
|
|
CPPUNIT_TEST(TestStream_GZip_Dictionary);
|
|
|
|
CPPUNIT_TEST(TestStream_ZLibGZip);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST(Decompress_BadData);
|
2004-04-18 15:26:20 -04:00
|
|
|
CPPUNIT_TEST(Decompress_wx251_zlib114_Data_NoHeader);
|
|
|
|
CPPUNIT_TEST(Decompress_wx251_zlib114_Data_ZLib);
|
2017-10-30 09:16:11 -04:00
|
|
|
CPPUNIT_TEST(Decompress_gzip135Data);
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Test different stream construct settings.
|
|
|
|
void TestStream_NoHeader_Default();
|
|
|
|
void TestStream_NoHeader_NoComp();
|
|
|
|
void TestStream_NoHeader_SpeedComp();
|
|
|
|
void TestStream_NoHeader_BestComp();
|
2009-03-06 11:26:15 -05:00
|
|
|
void TestStream_NoHeader_Dictionary();
|
2004-04-01 05:13:47 -05:00
|
|
|
void TestStream_ZLib_Default();
|
|
|
|
void TestStream_ZLib_NoComp();
|
|
|
|
void TestStream_ZLib_SpeedComp();
|
|
|
|
void TestStream_ZLib_BestComp();
|
|
|
|
void TestStream_GZip_Default();
|
|
|
|
void TestStream_GZip_NoComp();
|
|
|
|
void TestStream_GZip_SpeedComp();
|
|
|
|
void TestStream_GZip_BestComp();
|
2009-03-06 11:26:15 -05:00
|
|
|
void TestStream_GZip_Dictionary();
|
2004-04-01 05:13:47 -05:00
|
|
|
void TestStream_ZLibGZip();
|
|
|
|
// Try to decompress bad data.
|
|
|
|
void Decompress_BadData();
|
2004-11-10 16:29:08 -05:00
|
|
|
// Decompress data that was compress by an external app.
|
2004-04-18 15:26:20 -04:00
|
|
|
// (like test wx 2.4.2, 2.5.1 and gzip data)
|
2004-04-01 05:13:47 -05:00
|
|
|
// Note: This test is limited in testing range!
|
2004-04-18 15:26:20 -04:00
|
|
|
void Decompress_wx251_zlib114_Data_NoHeader();
|
|
|
|
void Decompress_wx251_zlib114_Data_ZLib();
|
2004-11-10 16:29:08 -05:00
|
|
|
void Decompress_gzip135Data();
|
2004-04-01 05:13:47 -05:00
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
private:
|
2004-04-01 05:13:47 -05:00
|
|
|
const char *GetDataBuffer();
|
|
|
|
const unsigned char *GetCompressedData();
|
2009-03-06 11:26:15 -05:00
|
|
|
void doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf = NULL);
|
2004-04-18 15:26:20 -04:00
|
|
|
void doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag = wxZLIB_AUTO);
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Implement base class functions.
|
2018-07-29 05:09:17 -04:00
|
|
|
virtual wxZlibInputStream *DoCreateInStream() wxOVERRIDE;
|
|
|
|
virtual wxZlibOutputStream *DoCreateOutStream() wxOVERRIDE;
|
|
|
|
virtual void DoDeleteInStream() wxOVERRIDE;
|
|
|
|
virtual void DoDeleteOutStream() wxOVERRIDE;
|
2004-04-01 05:13:47 -05:00
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
// Helper that can be used to create new wx compatibility tests...
|
2004-04-18 15:26:20 -04:00
|
|
|
// Otherwise not used by the tests.
|
|
|
|
void genExtTestData(wxTextOutputStream &out, const char *buf, int flag);
|
|
|
|
|
2004-04-01 05:13:47 -05:00
|
|
|
private:
|
|
|
|
char m_DataBuffer[DATABUFFER_SIZE];
|
|
|
|
size_t m_SizeCompressedData;
|
|
|
|
unsigned char *m_pCompressedData;
|
2009-03-06 11:26:15 -05:00
|
|
|
wxMemoryBuffer m_Dictionary;
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
// Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream.
|
|
|
|
wxMemoryInputStream *m_pTmpMemInStream;
|
|
|
|
wxMemoryOutputStream *m_pTmpMemOutStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
zlibStream::zlibStream()
|
|
|
|
:m_SizeCompressedData(0),
|
|
|
|
m_pCompressedData(NULL),
|
|
|
|
m_pTmpMemInStream(NULL),
|
|
|
|
m_pTmpMemOutStream(NULL)
|
|
|
|
{
|
|
|
|
// Init the data buffer.
|
|
|
|
for (size_t i = 0; i < DATABUFFER_SIZE; i++)
|
|
|
|
m_DataBuffer[i] = (i % 0xFF);
|
|
|
|
|
2009-03-06 11:26:15 -05:00
|
|
|
m_Dictionary.AppendData(m_DataBuffer, sizeof(m_DataBuffer) / 2);
|
|
|
|
|
2004-04-01 05:13:47 -05:00
|
|
|
// Set extra base config settings.
|
|
|
|
m_bSimpleTellITest = true;
|
|
|
|
m_bSimpleTellOTest = true;
|
2004-04-18 15:26:20 -04:00
|
|
|
|
|
|
|
/* Example code on how to produce test data...
|
|
|
|
{
|
|
|
|
wxFFileOutputStream fstream_out(wxT("gentest.cpp"));
|
|
|
|
wxTextOutputStream out( fstream_out );
|
|
|
|
|
|
|
|
genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4", wxZLIB_NO_HEADER);
|
|
|
|
genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4", wxZLIB_ZLIB);
|
|
|
|
}
|
|
|
|
*/
|
2004-04-01 05:13:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
zlibStream::~zlibStream()
|
|
|
|
{
|
2004-11-09 05:29:47 -05:00
|
|
|
delete[] m_pCompressedData;
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
delete m_pTmpMemInStream;
|
|
|
|
delete m_pTmpMemOutStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zlibStream::TestStream_NoHeader_Default()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_NoHeader_NoComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_NO_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_NoHeader_SpeedComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_SPEED);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_NoHeader_BestComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_COMPRESSION);
|
|
|
|
}
|
2009-03-06 11:26:15 -05:00
|
|
|
void zlibStream::TestStream_NoHeader_Dictionary()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION, &m_Dictionary);
|
|
|
|
}
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
void zlibStream::TestStream_ZLib_Default()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_ZLib_NoComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_NO_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_ZLib_SpeedComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_SPEED);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_ZLib_BestComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_COMPRESSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zlibStream::TestStream_GZip_Default()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_GZip_NoComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_NO_COMPRESSION);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_GZip_SpeedComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_SPEED);
|
|
|
|
}
|
|
|
|
void zlibStream::TestStream_GZip_BestComp()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_COMPRESSION);
|
|
|
|
}
|
2009-03-06 11:26:15 -05:00
|
|
|
void zlibStream::TestStream_GZip_Dictionary()
|
|
|
|
{
|
|
|
|
doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION, &m_Dictionary);
|
|
|
|
}
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
void zlibStream::TestStream_ZLibGZip()
|
|
|
|
{
|
2004-11-10 16:29:08 -05:00
|
|
|
// Only use default compression level, as this test is
|
2004-04-01 05:13:47 -05:00
|
|
|
// for testing if the streams can determine the stream type info them self...
|
2004-04-18 15:26:20 -04:00
|
|
|
doTestStreamData(wxZLIB_AUTO, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION);
|
|
|
|
doTestStreamData(wxZLIB_AUTO, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION);
|
2004-04-01 05:13:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void zlibStream::Decompress_BadData()
|
|
|
|
{
|
|
|
|
// Setup the bad data stream and the zlib stream.
|
|
|
|
wxMemoryInputStream memstream_in(GetDataBuffer(), DATABUFFER_SIZE);
|
|
|
|
CPPUNIT_ASSERT(memstream_in.IsOk());
|
|
|
|
wxZlibInputStream zstream_in(memstream_in);
|
2004-11-10 16:29:08 -05:00
|
|
|
CPPUNIT_ASSERT(zstream_in.IsOk()); // We did not yet read from the stream
|
2004-04-01 05:13:47 -05:00
|
|
|
// so it should still be OK.
|
|
|
|
// Try to force the stream to go to bad status.
|
|
|
|
CPPUNIT_ASSERT(!zstream_in.Eof());
|
|
|
|
if (zstream_in.IsOk())
|
|
|
|
zstream_in.GetC();
|
|
|
|
|
|
|
|
// Because of the bad data in the input stream the zlib
|
|
|
|
// stream should be marked as NOT OK.
|
|
|
|
CPPUNIT_ASSERT(!zstream_in.IsOk());
|
|
|
|
}
|
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
void zlibStream::Decompress_wx251_zlib114_Data_NoHeader()
|
|
|
|
{
|
|
|
|
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};
|
|
|
|
const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4";
|
|
|
|
const size_t data_size = sizeof(data);
|
|
|
|
const size_t value_size = strlen(value);
|
|
|
|
// We need to specify wxZLIB_NO_HEADER because wxZLIB_AUTO can't find it his self.
|
|
|
|
doDecompress_ExternalData(data, value, data_size, value_size, wxZLIB_NO_HEADER);
|
|
|
|
}
|
2004-04-01 05:13:47 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
void zlibStream::Decompress_wx251_zlib114_Data_ZLib()
|
|
|
|
{
|
|
|
|
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};
|
|
|
|
const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4";
|
|
|
|
const size_t data_size = sizeof(data);
|
|
|
|
const size_t value_size = strlen(value);
|
|
|
|
doDecompress_ExternalData(data, value, data_size, value_size);
|
|
|
|
}
|
2004-04-01 05:13:47 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
void zlibStream::Decompress_gzip135Data()
|
|
|
|
{
|
|
|
|
// Compressed data was on the command line with gzip 1.3.5.
|
|
|
|
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};
|
|
|
|
const char *gzip135_value = "Compressed data created with GNU gzip 1.3.5\n";
|
|
|
|
// Size of the value and date items.
|
|
|
|
const size_t data_size = sizeof(gzip135_data);
|
|
|
|
const size_t value_size = strlen(gzip135_value);
|
2004-04-01 05:13:47 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
// Perform a generic data test on the data.
|
|
|
|
doDecompress_ExternalData(gzip135_data, gzip135_value, data_size, value_size);
|
2004-04-01 05:13:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *zlibStream::GetDataBuffer()
|
|
|
|
{
|
|
|
|
return m_DataBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char *zlibStream::GetCompressedData()
|
|
|
|
{
|
|
|
|
if (!m_pCompressedData)
|
|
|
|
{
|
|
|
|
// Construct the compressed data live.
|
|
|
|
wxMemoryOutputStream memstream_out;
|
|
|
|
{
|
|
|
|
const char *buf = "01234567890123456789012345678901234567890123456789"; /* = 50 */
|
|
|
|
wxZlibOutputStream zstream_out(memstream_out);
|
|
|
|
zstream_out.Write(buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
// Copy the to the
|
2004-04-01 05:13:47 -05:00
|
|
|
m_SizeCompressedData = memstream_out.GetSize();
|
|
|
|
m_pCompressedData = new unsigned char[m_SizeCompressedData];
|
|
|
|
memstream_out.CopyTo(m_pCompressedData, m_SizeCompressedData);
|
|
|
|
}
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
CPPUNIT_ASSERT(m_pCompressedData != NULL);
|
2004-04-01 05:13:47 -05:00
|
|
|
return m_pCompressedData;
|
|
|
|
}
|
|
|
|
|
2009-03-06 11:26:15 -05:00
|
|
|
void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf)
|
2004-04-01 05:13:47 -05:00
|
|
|
{
|
2004-04-20 15:08:55 -04:00
|
|
|
size_t fail_pos;
|
2004-04-01 05:13:47 -05:00
|
|
|
char last_value = 0;
|
2004-04-20 15:08:55 -04:00
|
|
|
bool bWasEOF;
|
2004-04-01 05:13:47 -05:00
|
|
|
|
|
|
|
{ // Part one: Create a compressed file.
|
|
|
|
wxFileOutputStream fstream_out(FILENAME_GZ);
|
|
|
|
CPPUNIT_ASSERT(fstream_out.IsOk());
|
|
|
|
{
|
2004-04-18 15:26:20 -04:00
|
|
|
wxZlibOutputStream zstream_out(fstream_out, compress_level, output_flag);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Could not create the output stream", zstream_out.IsOk());
|
|
|
|
|
2009-03-06 11:26:15 -05:00
|
|
|
if (buf)
|
|
|
|
zstream_out.SetDictionary(*buf);
|
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
// Next: Compress some data so the file is containing something.
|
|
|
|
zstream_out.Write(GetDataBuffer(), DATABUFFER_SIZE);
|
2004-04-01 05:13:47 -05:00
|
|
|
}
|
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
// Next thing is required by zlib versions pre 1.2.0.
|
|
|
|
if (input_flag == wxZLIB_NO_HEADER)
|
|
|
|
fstream_out.PutC(' ');
|
2004-04-01 05:13:47 -05:00
|
|
|
}
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
{ // Part two: Verify that the compressed data when uncompressed
|
2004-04-01 05:13:47 -05:00
|
|
|
// matches the original data.
|
|
|
|
wxFileInputStream fstream_in(FILENAME_GZ);
|
|
|
|
CPPUNIT_ASSERT(fstream_in.IsOk());
|
|
|
|
wxZlibInputStream zstream_in(fstream_in, input_flag);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in.IsOk());
|
|
|
|
|
2009-03-06 11:26:15 -05:00
|
|
|
if (buf)
|
|
|
|
zstream_in.SetDictionary(*buf);
|
|
|
|
|
2004-04-01 05:13:47 -05:00
|
|
|
// Next: Check char per char if the returned data is valid.
|
|
|
|
const char *pbuf = GetDataBuffer();
|
|
|
|
for (fail_pos = 0; !zstream_in.Eof(); fail_pos++)
|
|
|
|
{
|
|
|
|
last_value = zstream_in.GetC();
|
2004-11-10 16:29:08 -05:00
|
|
|
if (zstream_in.LastRead() != 1 ||
|
2004-04-18 15:26:20 -04:00
|
|
|
last_value != pbuf[fail_pos])
|
2004-04-01 05:13:47 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bWasEOF = zstream_in.Eof();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the temp file...
|
|
|
|
::wxRemoveFile(FILENAME_GZ);
|
|
|
|
|
|
|
|
// Check state of the verify action.
|
|
|
|
if (fail_pos != DATABUFFER_SIZE || !bWasEOF)
|
|
|
|
{
|
2005-10-15 15:01:25 -04:00
|
|
|
wxString msg;
|
|
|
|
msg << wxT("Wrong data item at pos ") << fail_pos
|
|
|
|
<< wxT(" (Org_val ") << GetDataBuffer()[fail_pos]
|
|
|
|
<< wxT(" != Zlib_val ") << last_value
|
|
|
|
<< wxT("), with compression level ") << compress_level;
|
2004-04-01 05:13:47 -05:00
|
|
|
CPPUNIT_FAIL(string(msg.mb_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag)
|
|
|
|
{
|
|
|
|
// See that the input is ok.
|
|
|
|
wxASSERT(data != NULL);
|
|
|
|
wxASSERT(value != NULL);
|
|
|
|
wxASSERT(data_size > 0);
|
|
|
|
wxASSERT(value_size > 0);
|
|
|
|
|
|
|
|
// Quickly try to see if the data is valid.
|
|
|
|
switch (flag)
|
|
|
|
{
|
|
|
|
case wxZLIB_NO_HEADER:
|
|
|
|
break;
|
|
|
|
case wxZLIB_ZLIB:
|
|
|
|
if (!(data_size >= 1 && data[0] == 0x78))
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2004-04-18 15:26:20 -04:00
|
|
|
wxLogError(wxT("zlib data seems to not be zlib data!"));
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2004-04-18 15:26:20 -04:00
|
|
|
break;
|
|
|
|
case wxZLIB_GZIP:
|
|
|
|
if (!(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B))
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2004-04-18 15:26:20 -04:00
|
|
|
wxLogError(wxT("gzip data seems to not be gzip data!"));
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2004-04-18 15:26:20 -04:00
|
|
|
break;
|
|
|
|
case wxZLIB_AUTO:
|
2004-11-10 16:29:08 -05:00
|
|
|
if (!(data_size >= 1 && data[0] == 0x78) ||
|
2004-04-18 15:26:20 -04:00
|
|
|
!(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B))
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2004-04-18 15:26:20 -04:00
|
|
|
wxLogError(wxT("Data seems to not be zlib or gzip data!"));
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2017-11-02 11:55:00 -04:00
|
|
|
break;
|
2004-04-18 15:26:20 -04:00
|
|
|
default:
|
|
|
|
wxLogError(wxT("Unknown flag, skipping quick test."));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creat the needed streams.
|
|
|
|
wxMemoryInputStream memstream_in(data, data_size);
|
|
|
|
CPPUNIT_ASSERT(memstream_in.IsOk());
|
|
|
|
wxZlibInputStream zstream_in(memstream_in, flag);
|
|
|
|
CPPUNIT_ASSERT(zstream_in.IsOk());
|
|
|
|
|
|
|
|
bool bValueEq = true;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; !zstream_in.Eof(); i++)
|
|
|
|
{
|
|
|
|
char last_value = zstream_in.GetC();
|
|
|
|
|
|
|
|
// First check if it is a valid read.
|
|
|
|
if (zstream_in.LastRead() == 1)
|
|
|
|
{
|
|
|
|
// Check the values
|
|
|
|
if (last_value != value[i])
|
|
|
|
{
|
|
|
|
bValueEq = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the read failed and turned the stream to Eof we stop reading.
|
|
|
|
if (zstream_in.Eof())
|
|
|
|
break;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Stream is no longer ok!", zstream_in.IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't go over the end of the value buffer...
|
|
|
|
if (i == value_size)
|
|
|
|
{
|
|
|
|
// And if we do then try to see how long the stream actually is.
|
|
|
|
while (!zstream_in.Eof())
|
|
|
|
{
|
|
|
|
// Move one item along in the stream.
|
|
|
|
(void)zstream_in.GetC();
|
|
|
|
i++;
|
2004-11-10 16:29:08 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
// Check if we are in an infinite loop by multiplying value_size
|
|
|
|
// by 5 to have a *much* bigger range then the real range.
|
2011-04-03 16:31:32 -04:00
|
|
|
// Note: In case you ask yourself, why 5, the answer is no reason...
|
|
|
|
// it is not too big and not to small a size, nothing more
|
2004-04-18 15:26:20 -04:00
|
|
|
// nothing less to it.
|
2004-11-10 16:29:08 -05:00
|
|
|
if (i > (value_size*5))
|
2004-04-18 15:26:20 -04:00
|
|
|
{
|
|
|
|
// Note: Please make sure Input_Eof test passed.
|
|
|
|
CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Replace CppUnit with Catch for unit tests
Drop the legacy CppUnit testing framework used for the unit tests.
Replacing it with Catch has the advantage of not requiring CppUnit
libraries to be installed on the system in order to be able to run
tests (Catch is header-only and a copy of it is now included in the
main repository itself) and, in the future, of being able to write
the tests in a much more natural way.
For now, however, avoid changing the existing tests code as much as
[reasonably] possible to avoid introducing bugs in them and provide
the CppUnit compatibility macros in the new wx/catch_cppunit.h header
which allow to preserve the 99% of the existing code unchanged. Some
of the required changes are:
- Decompose asserts using "a && b" conditions into multiple asserts
checking "a" and "b" independently. This would have been better
even with CppUnit (to know which part of condition exactly failed)
and is required with Catch.
- Use extra parentheses around such conditions when they can't be
easily decomposed in the arrays test, due to the use of macros.
This is not ideal from the point of view of messages given when
the tests fail but will do for now.
- Rewrite asserts using "a || b" as a combination of condition
checks and assert macros. Again, this is better anyhow, and is
required with Catch. Incidentally, this allowed to fix a bug in
the "exec" unit test which didn't leave enough time for the new
process to be launched before trying to kill it.
- Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros,
our emulation of this macro can be used only once.
- Provide string conversions using Catch-specific StringMaker for
a couple of types.
- Replace custom wxImage comparison with a Catch-specific matcher
class.
- Remove most of test running logic from test.cpp, in particular don't
parse command line ourselves any longer but use Catch built-in
command line parser. This is a source of a minor regression:
previously, both "Foo" and "FooTestCase" could be used as the name of
the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( i, value_size );
|
|
|
|
CPPUNIT_ASSERT( bValueEq );
|
2004-04-18 15:26:20 -04:00
|
|
|
}
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
wxZlibInputStream *zlibStream::DoCreateInStream()
|
|
|
|
{
|
2004-04-01 05:13:47 -05:00
|
|
|
const unsigned char *buf = GetCompressedData();
|
|
|
|
m_pTmpMemInStream = new wxMemoryInputStream(buf, m_SizeCompressedData);
|
|
|
|
CPPUNIT_ASSERT(m_pTmpMemInStream->IsOk());
|
|
|
|
wxZlibInputStream *pzstream_in = new wxZlibInputStream(*m_pTmpMemInStream);
|
|
|
|
CPPUNIT_ASSERT(pzstream_in->IsOk());
|
|
|
|
return pzstream_in;
|
|
|
|
}
|
|
|
|
wxZlibOutputStream *zlibStream::DoCreateOutStream()
|
2004-11-10 16:29:08 -05:00
|
|
|
{
|
2004-04-01 05:13:47 -05:00
|
|
|
m_pTmpMemOutStream = new wxMemoryOutputStream();
|
|
|
|
CPPUNIT_ASSERT(m_pTmpMemOutStream->IsOk());
|
|
|
|
wxZlibOutputStream *pzstream_out = new wxZlibOutputStream(*m_pTmpMemOutStream);
|
|
|
|
CPPUNIT_ASSERT(pzstream_out->IsOk());
|
|
|
|
return pzstream_out;
|
|
|
|
}
|
2004-11-10 16:29:08 -05:00
|
|
|
void zlibStream::DoDeleteInStream()
|
|
|
|
{
|
2004-04-01 05:13:47 -05:00
|
|
|
delete m_pTmpMemInStream;
|
|
|
|
m_pTmpMemInStream = NULL;
|
|
|
|
}
|
2004-11-10 16:29:08 -05:00
|
|
|
void zlibStream::DoDeleteOutStream()
|
|
|
|
{
|
2004-04-01 05:13:47 -05:00
|
|
|
delete m_pTmpMemOutStream;
|
|
|
|
m_pTmpMemOutStream = NULL;
|
|
|
|
}
|
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
|
|
|
|
void zlibStream::genExtTestData(wxTextOutputStream &out, const char *buf, int flag)
|
|
|
|
{
|
2004-04-20 15:08:55 -04:00
|
|
|
unsigned char *data;
|
|
|
|
size_t size;
|
2004-11-10 16:29:08 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
{ // Gen data
|
|
|
|
wxMemoryOutputStream memstream_out;
|
|
|
|
{
|
|
|
|
wxZlibOutputStream zstream_out(memstream_out, wxZ_DEFAULT_COMPRESSION, flag);
|
|
|
|
zstream_out.Write(buf, strlen(buf));
|
|
|
|
}
|
|
|
|
if (flag == wxZLIB_NO_HEADER)
|
|
|
|
memstream_out.PutC(' ');
|
2004-11-10 16:29:08 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
size = memstream_out.GetSize();
|
|
|
|
data = new unsigned char[size];
|
|
|
|
memstream_out.CopyTo(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
out << wxT("void zlibStream::Decompress_wxXXXData()") << wxT("\n");
|
|
|
|
out << wxT("{") << wxT("\n") << wxT(" const unsigned char data[] = {");
|
2004-11-10 16:29:08 -05:00
|
|
|
|
2004-04-18 15:26:20 -04:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (i+1 != size)
|
|
|
|
out << wxString::Format(wxT("%d,"), data[i]);
|
|
|
|
else
|
|
|
|
out << wxString::Format(wxT("%d"), data[i]);
|
2004-11-10 16:29:08 -05:00
|
|
|
}
|
2004-04-18 15:26:20 -04:00
|
|
|
delete [] data;
|
|
|
|
|
|
|
|
out << wxT("};") << wxT("\n");
|
|
|
|
out << wxT(" const char *value = \"") << wxString(buf, wxConvUTF8) << wxT("\";") << wxT("\n");
|
|
|
|
out << wxT(" const size_t data_size = sizeof(data);") << wxT("\n");
|
|
|
|
out << wxT(" const size_t value_size = strlen(value);") << wxT("\n");
|
|
|
|
out << wxT(" doDecompress_ExternalData(data, value, data_size, value_size);") << wxT("\n");
|
|
|
|
out << wxT("}") << wxT("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-01 05:13:47 -05:00
|
|
|
// Register the stream sub suite, by using some stream helper macro.
|
|
|
|
// Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
|
|
|
|
STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(zlibStream)
|
|
|
|
|