axmol/tests/cpp-tests/Source/ZipTest/ZipTests.cpp

189 lines
5.1 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ZipTests.h"
#include <sstream>
#include "unzip/unzip.h"
#include "unzip/crypt.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
ZipTests::ZipTests()
{
2019-11-23 20:27:39 +08:00
ADD_TEST_CASE(UnZipNormalFile);
ADD_TEST_CASE(UnZipWithPassword);
}
std::string ZipTest::title() const
{
2019-11-23 20:27:39 +08:00
return "Unzip Test";
}
static void unzipTest(Label* label,
std::string_view originFile,
std::string_view tmpName,
std::string_view zipFile,
std::string_view password = "")
2019-11-23 20:27:39 +08:00
{
auto fu = FileUtils::getInstance();
2022-10-18 19:17:36 +08:00
ax::Data origContent;
2019-11-23 20:27:39 +08:00
const int BUFF_SIZE = 1024;
char* buff = nullptr;
2019-11-23 20:27:39 +08:00
std::vector<char> fileData;
bool hasError = false;
2019-11-23 20:27:39 +08:00
unz_file_info fileInfo = {0};
char fileName[40] = {0};
2019-11-23 20:27:39 +08:00
std::string newLocal{fu->getWritablePath()};
newLocal += tmpName;
// copy file to support android
2019-11-23 20:27:39 +08:00
if (fu->isFileExist(newLocal))
{
2022-07-16 10:43:05 +08:00
AXLOG("Remove file %s", newLocal.c_str());
2019-11-23 20:27:39 +08:00
fu->removeFile(newLocal);
}
2022-07-16 10:43:05 +08:00
AXLOG("Copy %s to %s", zipFile.data(), newLocal.c_str());
2019-11-23 20:27:39 +08:00
auto writeSuccess = fu->writeDataToFile(fu->getDataFromFile(zipFile), newLocal);
if (!writeSuccess)
{
2019-11-23 20:27:39 +08:00
label->setString("Failed to copy zip file to writable path");
return;
}
unzFile fp = unzOpen(newLocal.c_str());
if (!fp)
{
2022-07-16 10:43:05 +08:00
AXLOG("Failed to open zip file %s", newLocal.c_str());
2019-11-23 20:27:39 +08:00
label->setString("Failed to open zip file");
return;
}
int err = unzGoToFirstFile(fp);
if (err != UNZ_OK)
{
2019-11-23 20:27:39 +08:00
label->setString("Failed to local first file");
goto close_and_return;
}
unzGetCurrentFileInfo(fp, &fileInfo, fileName, sizeof(fileName) - 1, nullptr, 0, nullptr, 0);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
AXASSERT(strncmp("10k.txt", fileName, 7) == 0, "file name should be 10k.txt");
2019-11-23 20:27:39 +08:00
if (password.empty())
2019-11-23 20:27:39 +08:00
{
err = unzOpenCurrentFile(fp);
}
else
{
err = unzOpenCurrentFilePassword(fp, password.data());
2019-11-23 20:27:39 +08:00
}
if (err != UNZ_OK)
{
2019-11-23 20:27:39 +08:00
label->setString("failed to open zip file");
goto close_and_return;
}
buff = new char[BUFF_SIZE];
for (;;)
{
int retSize = unzReadCurrentFile(fp, buff, BUFF_SIZE);
if (retSize < 0)
{
2019-11-23 20:27:39 +08:00
hasError = true;
break;
}
else if (retSize == 0)
{
2019-11-23 20:27:39 +08:00
break;
}
fileData.insert(fileData.end(), buff, buff + retSize);
}
delete[] buff;
if (hasError)
{
2019-11-23 20:27:39 +08:00
label->setString("unzip error! read error!");
goto close_and_return;
}
origContent = FileUtils::getInstance()->getDataFromFile(originFile);
if (origContent.getSize() == fileData.size() &&
memcmp(origContent.getBytes(), fileData.data(), fileData.size()) == 0)
{
2019-11-23 20:27:39 +08:00
label->setString("unzip ok!");
}
else
{
2019-11-23 20:27:39 +08:00
label->setString("unzip error! data mismatch!");
}
close_and_return:
unzClose(fp);
}
void UnZipNormalFile::onEnter()
{
2019-11-23 20:27:39 +08:00
TestCase::onEnter();
const auto winSize = Director::getInstance()->getWinSize();
Label* label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
label->setPosition(winSize.width / 2, winSize.height / 2);
2019-11-23 20:27:39 +08:00
addChild(label);
unzipTest(label, "zip/10k.txt", "10-nopasswd.zip", "zip/10k-nopass.zip");
}
std::string UnZipNormalFile::subtitle() const
{
2019-11-23 20:27:39 +08:00
return "unzip without password";
}
void UnZipWithPassword::onEnter()
{
2019-11-23 20:27:39 +08:00
TestCase::onEnter();
const auto winSize = Director::getInstance()->getWinSize();
Label* label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
label->setPosition(winSize.width / 2, winSize.height / 2);
2019-11-23 20:27:39 +08:00
addChild(label);
unzipTest(label, "zip/10k.txt", "10.zip", "zip/10k.zip", "123456");
}
std::string UnZipWithPassword::subtitle() const
{
2019-11-23 20:27:39 +08:00
return "unzip with password";
}