Files
OpenVulkano/tests/IO/Archive/ZipWriterTest.cpp
2025-05-22 21:49:56 +02:00

114 lines
5.1 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include "Base/Utils.hpp"
#include "IO/Archive/ZipWriter.hpp"
#include "IO/AppFolders.hpp"
#include <fstream>
#include <filesystem>
#include <string>
using namespace OpenVulkano;
TEST_CASE("Empty zip file", "[ZipWriter]")
{
const auto emptyZipPath = AppFolders::GetAppTempDir() / "empty.zip";
{
ZipWriter writer(emptyZipPath);
}
auto mem = Utils::ReadFile(emptyZipPath);
const int expectSize = 22;
Array<uint8_t> expect = {0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
CHECK(mem.Size() == expectSize);
CHECK(mem == expect);
std::filesystem::remove(emptyZipPath);
}
TEST_CASE("Zip with one file(AAA.txt that has 'AAA')", "[ZipWriter]")
{
const auto oneFileZipPath = AppFolders::GetAppTempDir() / "one_file.zip";
{
ZipWriter writer(oneFileZipPath);
FileDescription desc = FileDescription::MkFile("AAA.txt", 3);
desc.modTime = {};
desc.createTime = {};
char buffer[] = {'A', 'A', 'A'};
writer.AddFile(desc, buffer);
}
auto mem = Utils::ReadFile(oneFileZipPath);
const int expectSize = 151;
Array<uint8_t> expect =
{
0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec, 0xa7, 0x31, 0xa0, 0x66, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00,
0x00, 0x00, 0x41, 0x41, 0x41, 0x2e, 0x74, 0x78, 0x74, 0x41, 0x41, 0x41, 0x50, 0x4b, 0x01, 0x02, 0x1f, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec,
0xa7, 0x31, 0xa0, 0x66, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x41, 0x41, 0x41, 0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d,
0x01, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00
};
CHECK(mem.Size() == expectSize);
CHECK(mem == expect);
std::filesystem::remove(oneFileZipPath);
}
TEST_CASE("Zip with two files(AAA.txt that has 'AAA', BBB.bin that has 'BBB')", "[ZipWriter]")
{
const auto twoFilesZipPath = AppFolders::GetAppTempDir() / "two_files.zip";
{
ZipWriter writer(twoFilesZipPath);
FileDescription aaa = FileDescription::MkFile("AAA.txt", 3);
aaa.modTime = {};
aaa.createTime = {};
char aaaBuffer[] = {'A', 'A', 'A'};
FileDescription bbb = FileDescription::MkFile("BBB.bin", 3);
bbb.modTime = {};
bbb.createTime = {};
char bbbBuffer[] = {'B', 'B', 'B'};
writer.AddFile(aaa, aaaBuffer);
writer.AddFile(bbb, bbbBuffer);
}
auto mem = Utils::ReadFile(twoFilesZipPath);
const int expectSize = 280;
Array<uint8_t> expect =
{
0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec, 0xa7, 0x31, 0xa0, 0x66, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00,
0x00, 0x00, 0x41, 0x41, 0x41, 0x2e, 0x74, 0x78, 0x74, 0x41, 0x41, 0x41, 0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec, 0x87, 0x8d,
0xc2, 0xd6, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x42, 0x42, 0x42, 0x2e, 0x62, 0x69, 0x6e, 0x42, 0x42, 0x42, 0x50, 0x4b, 0x01, 0x02,
0x1f, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec, 0xa7, 0x31, 0xa0, 0x66, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x41, 0x41, 0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d,
0x01, 0x50, 0x4b, 0x01, 0x02, 0x1f, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xec, 0x87, 0x8d, 0xc2, 0xd6, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x42, 0x42, 0x42, 0x2e, 0x62, 0x69, 0x6e, 0x0a, 0x00,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x00, 0x80, 0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x00, 0x80,
0x3e, 0xd5, 0xde, 0xb1, 0x9d, 0x01, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00
};
CHECK(mem.Size() == expectSize);
CHECK(mem == expect);
std::filesystem::remove(twoFilesZipPath);
}