147 lines
4.0 KiB
C++
147 lines
4.0 KiB
C++
/**
|
||
* @file test_tool.cpp
|
||
* @brief tool 模块单元测试(Archiver 归档器)
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @author CodeBuddy
|
||
* @date 26-09-03
|
||
*
|
||
* @note
|
||
*/
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "tool/dl_tool_archiver.h"
|
||
#include "io/dl_file.h"
|
||
#include "render/dl_factory.h"
|
||
|
||
namespace
|
||
{
|
||
//! 构造一个测试用的临时源目录(含 a.txt 与 sub/b.txt)
|
||
bool CreateTestSrc(const std::string& dir_src)
|
||
{
|
||
if (!dl::File::CreateFolder(dir_src))
|
||
return false;
|
||
if (!dl::File::CreateFolder(dir_src + "sub/"))
|
||
return false;
|
||
if (!dl::File::SaveString("hello world", dir_src + "a.txt"))
|
||
return false;
|
||
if (!dl::File::SaveString("second file", dir_src + "sub/b.txt"))
|
||
return false;
|
||
return true;
|
||
}
|
||
}
|
||
|
||
TEST(ArchiverTest, CompressDecompress)
|
||
{
|
||
std::string dir_src = dl::g_factory->GetPathTemp() + "dl_archiver_src/";
|
||
std::string dir_out = dl::g_factory->GetPathTemp() + "dl_archiver_out/";
|
||
std::string file_arc = dl::g_factory->GetPathTemp() + "dl_archiver.bin";
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(dir_out, true);
|
||
dl::File::Delete(file_arc);
|
||
|
||
ASSERT_TRUE(CreateTestSrc(dir_src));
|
||
|
||
dl::Tool::Archiver::Param p;
|
||
p._src = dir_src;
|
||
p._out = file_arc;
|
||
p._compressMode = dl::Memory::E_NONE;
|
||
p._bCrc = false;
|
||
dl::Tool::Archiver::Compress(p);
|
||
ASSERT_TRUE(dl::File::IsExist(file_arc));
|
||
|
||
ASSERT_TRUE(dl::Tool::Archiver::Decompress(file_arc, dir_out));
|
||
EXPECT_TRUE(dl::File::IsExist(dir_out + "a.txt"));
|
||
EXPECT_TRUE(dl::File::IsExist(dir_out + "sub/b.txt"));
|
||
EXPECT_EQ(dl::File::GetString(dir_out + "a.txt"), "hello world");
|
||
EXPECT_EQ(dl::File::GetString(dir_out + "sub/b.txt"), "second file");
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(dir_out, true);
|
||
dl::File::Delete(file_arc);
|
||
}
|
||
|
||
TEST(ArchiverTest, CompressDecompressLz4)
|
||
{
|
||
std::string dir_src = dl::g_factory->GetPathTemp() + "dl_archiver_src/";
|
||
std::string dir_out = dl::g_factory->GetPathTemp() + "dl_archiver_out/";
|
||
std::string file_arc = dl::g_factory->GetPathTemp() + "dl_archiver.bin";
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(dir_out, true);
|
||
dl::File::Delete(file_arc);
|
||
|
||
ASSERT_TRUE(CreateTestSrc(dir_src));
|
||
|
||
dl::Tool::Archiver::Param p;
|
||
p._src = dir_src;
|
||
p._out = file_arc;
|
||
p._compressMode = dl::Memory::E_LZ4;
|
||
p._bCrc = false;
|
||
dl::Tool::Archiver::Compress(p);
|
||
ASSERT_TRUE(dl::File::IsExist(file_arc));
|
||
|
||
ASSERT_TRUE(dl::Tool::Archiver::Decompress(file_arc, dir_out));
|
||
EXPECT_TRUE(dl::File::IsExist(dir_out + "a.txt"));
|
||
EXPECT_TRUE(dl::File::IsExist(dir_out + "sub/b.txt"));
|
||
EXPECT_EQ(dl::File::GetString(dir_out + "a.txt"), "hello world");
|
||
EXPECT_EQ(dl::File::GetString(dir_out + "sub/b.txt"), "second file");
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(dir_out, true);
|
||
dl::File::Delete(file_arc);
|
||
}
|
||
|
||
TEST(ArchiverTest, GetFileList)
|
||
{
|
||
std::string dir_src = dl::g_factory->GetPathTemp() + "dl_archiver_src/";
|
||
std::string file_arc = dl::g_factory->GetPathTemp() + "dl_archiver.bin";
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(file_arc);
|
||
|
||
ASSERT_TRUE(CreateTestSrc(dir_src));
|
||
|
||
dl::Tool::Archiver::Param p;
|
||
p._src = dir_src;
|
||
p._out = file_arc;
|
||
p._compressMode = dl::Memory::E_NONE;
|
||
p._bCrc = false;
|
||
dl::Tool::Archiver::Compress(p);
|
||
|
||
dl::Buffer buf = dl::File::GetBuffer(file_arc);
|
||
ASSERT_FALSE(buf.empty());
|
||
|
||
dl::Tool::Archiver::FileList list;
|
||
ASSERT_TRUE(dl::Tool::Archiver::GetFileList(buf, list));
|
||
EXPECT_EQ(list.size(), 2u);
|
||
|
||
bool found_a = false;
|
||
bool found_b = false;
|
||
for (auto& [name, info] : list)
|
||
{
|
||
if (name.find("a.txt") != std::string::npos)
|
||
found_a = true;
|
||
if (name.find("b.txt") != std::string::npos)
|
||
found_b = true;
|
||
}
|
||
EXPECT_TRUE(found_a);
|
||
EXPECT_TRUE(found_b);
|
||
|
||
dl::File::Delete(dir_src, true);
|
||
dl::File::Delete(file_arc);
|
||
}
|
||
|
||
TEST(ArchiverTest, DecompressBadType)
|
||
{
|
||
std::string bad = "this is not an archiver file";
|
||
dl::Buffer buf = dl::CreateBuffer(dl::CreateBufferView(bad));
|
||
|
||
std::string dir_out = dl::g_factory->GetPathTemp() + "dl_archiver_bad/";
|
||
dl::File::Delete(dir_out, true);
|
||
EXPECT_FALSE(dl::Tool::Archiver::Decompress(buf, dir_out));
|
||
dl::File::Delete(dir_out, true);
|
||
}
|