1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00

Fixed random map format test crash

This commit is contained in:
AlexVinS 2017-06-14 11:56:35 +03:00
parent bb0f388f23
commit 02ae95aca3
6 changed files with 58 additions and 49 deletions

View File

@ -7,7 +7,7 @@
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CZipSaver.h"
@ -17,10 +17,10 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st
owner(owner_)
{
zip_fileinfo fileInfo;
std::time_t t = time(nullptr);
fileInfo.dosDate = 0;
fileInfo.dosDate = 0;
struct tm * localTime = std::localtime(&t);
fileInfo.tmz_date.tm_hour = localTime->tm_hour;
fileInfo.tmz_date.tm_mday = localTime->tm_mday;
@ -28,10 +28,10 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st
fileInfo.tmz_date.tm_mon = localTime->tm_mon;
fileInfo.tmz_date.tm_sec = localTime->tm_sec;
fileInfo.tmz_date.tm_year = localTime->tm_year;
fileInfo.external_fa = 0; //???
fileInfo.internal_fa = 0;
fileInfo.external_fa = 0; //???
fileInfo.internal_fa = 0;
int status = zipOpenNewFileInZip4_64(
handle,
archiveFilename.c_str(),
@ -53,10 +53,10 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st
0,//flagBase
0//zip64
);
if(status != ZIP_OK)
throw new std::runtime_error("CZipOutputStream: zipOpenNewFileInZip failed");
owner->activeStream = this;
}
@ -71,7 +71,7 @@ CZipOutputStream::~CZipOutputStream()
si64 CZipOutputStream::write(const ui8 * data, si64 size)
{
int ret = zipWriteInFileInZip(handle, (const void*)data, (unsigned)size);
if (ret == ZIP_OK)
return size;
else
@ -79,41 +79,41 @@ si64 CZipOutputStream::write(const ui8 * data, si64 size)
}
///CZipSaver
CZipSaver::CZipSaver(std::shared_ptr<CIOApi> api, const std::string & path):
CZipSaver::CZipSaver(std::shared_ptr<CIOApi> api, const boost::filesystem::path & path):
ioApi(api),
zipApi(ioApi->getApiStructure()),
handle(nullptr),
activeStream(nullptr)
{
handle = zipOpen2_64(path.c_str(), APPEND_STATUS_CREATE, nullptr, &zipApi);
handle = zipOpen2_64((const void *) & path, APPEND_STATUS_CREATE, nullptr, &zipApi);
if (handle == nullptr)
throw new std::runtime_error("CZipSaver: Failed to create archive");
throw new std::runtime_error("CZipSaver: Failed to create archive");
}
CZipSaver::~CZipSaver()
{
if(activeStream != nullptr)
{
logGlobal->error("CZipSaver::~CZipSaver: active stream found");
logGlobal->error("CZipSaver::~CZipSaver: active stream found");
zipCloseFileInZip(handle);
}
if(handle != nullptr)
{
int status = zipClose(handle, nullptr);
if (status != ZIP_OK)
logGlobal->errorStream() << "CZipSaver: archive finalize failed: "<<status;
logGlobal->errorStream() << "CZipSaver: archive finalize failed: "<<status;
}
}
std::unique_ptr<COutputStream> CZipSaver::addFile(const std::string & archiveFilename)
{
if(activeStream != nullptr)
throw new std::runtime_error("CZipSaver::addFile: stream already opened");
std::unique_ptr<COutputStream> stream(new CZipOutputStream(this, handle, archiveFilename));
return std::move(stream);
}

View File

@ -23,16 +23,16 @@ public:
* @brief constructs zip stream from already opened file
* @param archive archive handle, must be opened
* @param archiveFilename name of file to write
*/
*/
explicit CZipOutputStream(CZipSaver * owner_, zipFile archive, const std::string & archiveFilename);
~CZipOutputStream();
si64 write(const ui8 * data, si64 size) override;
si64 seek(si64 position) override {return -1;};
si64 tell() override {return 0;};
si64 skip(si64 delta) override {return 0;};
si64 getSize() override {return 0;};
si64 getSize() override {return 0;};
private:
zipFile handle;
CZipSaver * owner;
@ -40,17 +40,17 @@ private:
class DLL_LINKAGE CZipSaver
{
public:
explicit CZipSaver(std::shared_ptr<CIOApi> api, const std::string & path);
public:
explicit CZipSaver(std::shared_ptr<CIOApi> api, const boost::filesystem::path & path);
virtual ~CZipSaver();
std::unique_ptr<COutputStream> addFile(const std::string & archiveFilename);
private:
std::shared_ptr<CIOApi> ioApi;
zlib_filefunc64_def zipApi;
zlib_filefunc64_def zipApi;
zipFile handle;
///due to minizip design only one file stream may opened at a time
COutputStream * activeStream;
friend class CZipOutputStream;

View File

@ -204,7 +204,7 @@ zlib_filefunc64_def CProxyROIOApi::getApiStructure()
CInputStream * CProxyROIOApi::openFile(const boost::filesystem::path& filename, int mode)
{
logGlobal->traceStream() << "CProxyIOApi: stream opened for " <<filename.string() <<" with mode "<<mode;
logGlobal->traceStream() << "CProxyROIOApi: stream opened for " <<filename.string() <<" with mode "<<mode;
data->seek(0);
return data;

View File

@ -21,7 +21,9 @@
#include "../lib/CRandomGenerator.h"
#include "../lib/VCMI_Lib.h"
BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_Type)
BOOST_AUTO_TEST_SUITE(CMapEditManager_Suite)
BOOST_AUTO_TEST_CASE(DrawTerrain_Type)
{
logGlobal->info("CMapEditManager_DrawTerrain_Type start");
try
@ -110,7 +112,7 @@ BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_Type)
logGlobal->info("CMapEditManager_DrawTerrain_Type finish");
}
BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_View)
BOOST_AUTO_TEST_CASE(DrawTerrain_View)
{
logGlobal->info("CMapEditManager_DrawTerrain_View start");
try
@ -178,3 +180,5 @@ BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_View)
}
logGlobal->info("CMapEditManager_DrawTerrain_View finish");
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -26,7 +26,9 @@
static const int TEST_RANDOM_SEED = 1337;
BOOST_AUTO_TEST_CASE(MapFormat_Random)
BOOST_AUTO_TEST_SUITE(MapFormat_Suite)
BOOST_AUTO_TEST_CASE(Random)
{
logGlobal->info("MapFormat_Random start");
BOOST_TEST_CHECKPOINT("MapFormat_Random start");
@ -115,7 +117,7 @@ static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::st
}
}
BOOST_AUTO_TEST_CASE(MapFormat_Objects)
BOOST_AUTO_TEST_CASE(Objects)
{
logGlobal->info("MapFormat_Objects start");
@ -202,3 +204,5 @@ BOOST_AUTO_TEST_CASE(MapFormat_Objects)
logGlobal->info("MapFormat_Objects finish");
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -9,44 +9,45 @@
*
*/
#include "StdInc.h"
#include <boost/test/unit_test.hpp>
#include "../lib/filesystem/CMemoryBuffer.h"
struct CMemoryBufferFixture
{
CMemoryBuffer subject;
};
BOOST_FIXTURE_TEST_CASE(CMemoryBuffer_Empty, CMemoryBufferFixture)
BOOST_AUTO_TEST_SUITE(CMemoryBuffer_Suite)
BOOST_FIXTURE_TEST_CASE(empty, CMemoryBufferFixture)
{
BOOST_REQUIRE_EQUAL(0, subject.tell());
BOOST_REQUIRE_EQUAL(0, subject.getSize());
si32 dummy = 1337;
auto ret = subject.read((ui8 *)&dummy, sizeof(si32));
BOOST_CHECK_EQUAL(0, ret);
BOOST_CHECK_EQUAL(0, ret);
BOOST_CHECK_EQUAL(1337, dummy);
BOOST_CHECK_EQUAL(0, subject.tell());
}
BOOST_FIXTURE_TEST_CASE(CMemoryBuffer_Write, CMemoryBufferFixture)
BOOST_FIXTURE_TEST_CASE(write, CMemoryBufferFixture)
{
const si32 initial = 1337;
subject.write((const ui8 *)&initial, sizeof(si32));
BOOST_CHECK_EQUAL(4, subject.tell());
subject.seek(0);
BOOST_CHECK_EQUAL(0, subject.tell());
si32 current = 0;
auto ret = subject.read((ui8 *)&current, sizeof(si32));
BOOST_CHECK_EQUAL(sizeof(si32), ret);
BOOST_CHECK_EQUAL(sizeof(si32), ret);
BOOST_CHECK_EQUAL(initial, current);
BOOST_CHECK_EQUAL(4, subject.tell());
BOOST_CHECK_EQUAL(4, subject.tell());
}
BOOST_AUTO_TEST_SUITE_END()