2017-10-22 22:13:53 +00:00
|
|
|
#include "test.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2022-04-22 23:04:48 +00:00
|
|
|
#include <base/logger.h>
|
2017-10-22 22:13:53 +00:00
|
|
|
#include <base/system.h>
|
2021-04-21 11:13:29 +00:00
|
|
|
#include <engine/storage.h>
|
2017-10-22 22:13:53 +00:00
|
|
|
|
2021-06-20 09:30:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-10-22 22:13:53 +00:00
|
|
|
CTestInfo::CTestInfo()
|
|
|
|
{
|
|
|
|
const ::testing::TestInfo *pTestInfo =
|
|
|
|
::testing::UnitTest::GetInstance()->current_test_info();
|
2021-12-20 14:00:21 +00:00
|
|
|
char aBuf[IO_MAX_PATH_LENGTH];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%s.%s", pTestInfo->test_case_name(), pTestInfo->name());
|
|
|
|
IStorage::FormatTmpPath(m_aFilename, sizeof(m_aFilename), aBuf);
|
2017-10-22 22:13:53 +00:00
|
|
|
}
|
2017-11-24 09:51:46 +00:00
|
|
|
|
2021-04-21 11:13:29 +00:00
|
|
|
IStorage *CTestInfo::CreateTestStorage()
|
|
|
|
{
|
|
|
|
bool Error = fs_makedir(m_aFilename);
|
|
|
|
EXPECT_FALSE(Error);
|
|
|
|
if(Error)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return CreateTempStorage(m_aFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
class CTestInfoPath
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool m_IsDirectory;
|
2021-09-13 08:06:34 +00:00
|
|
|
char m_aData[IO_MAX_PATH_LENGTH];
|
2021-04-21 11:13:29 +00:00
|
|
|
|
|
|
|
bool operator<(const CTestInfoPath &Other) const
|
|
|
|
{
|
|
|
|
if(m_IsDirectory != Other.m_IsDirectory)
|
|
|
|
{
|
|
|
|
return m_IsDirectory < Other.m_IsDirectory;
|
|
|
|
}
|
|
|
|
return str_comp(m_aData, Other.m_aData) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CTestCollectData
|
|
|
|
{
|
|
|
|
public:
|
2021-09-13 08:06:34 +00:00
|
|
|
char m_aCurrentDir[IO_MAX_PATH_LENGTH];
|
2022-06-11 20:03:23 +00:00
|
|
|
std::vector<CTestInfoPath> *m_pvEntries;
|
2021-04-21 11:13:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int TestCollect(const char *pName, int IsDir, int Unused, void *pUser)
|
|
|
|
{
|
|
|
|
CTestCollectData *pData = (CTestCollectData *)pUser;
|
|
|
|
|
|
|
|
if(str_comp(pName, ".") == 0 || str_comp(pName, "..") == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTestInfoPath Path;
|
|
|
|
Path.m_IsDirectory = IsDir;
|
|
|
|
str_format(Path.m_aData, sizeof(Path.m_aData), "%s/%s", pData->m_aCurrentDir, pName);
|
2022-06-11 20:03:23 +00:00
|
|
|
pData->m_pvEntries->push_back(Path);
|
2021-04-21 11:13:29 +00:00
|
|
|
if(Path.m_IsDirectory)
|
|
|
|
{
|
|
|
|
CTestCollectData DataRecursive;
|
|
|
|
str_copy(DataRecursive.m_aCurrentDir, Path.m_aData, sizeof(DataRecursive.m_aCurrentDir));
|
2022-06-11 20:03:23 +00:00
|
|
|
DataRecursive.m_pvEntries = pData->m_pvEntries;
|
2021-04-21 11:13:29 +00:00
|
|
|
fs_listdir(DataRecursive.m_aCurrentDir, TestCollect, 0, &DataRecursive);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:09:08 +00:00
|
|
|
void TestDeleteTestStorageFiles(const char *pPath)
|
2021-04-21 11:13:29 +00:00
|
|
|
{
|
2022-06-15 17:34:41 +00:00
|
|
|
std::vector<CTestInfoPath> vEntries;
|
2021-04-21 11:13:29 +00:00
|
|
|
CTestCollectData Data;
|
2022-01-31 00:09:08 +00:00
|
|
|
str_copy(Data.m_aCurrentDir, pPath, sizeof(Data.m_aCurrentDir));
|
2022-06-15 17:34:41 +00:00
|
|
|
Data.m_pvEntries = &vEntries;
|
2021-04-21 11:13:29 +00:00
|
|
|
fs_listdir(Data.m_aCurrentDir, TestCollect, 0, &Data);
|
|
|
|
|
|
|
|
CTestInfoPath Path;
|
|
|
|
Path.m_IsDirectory = true;
|
|
|
|
str_copy(Path.m_aData, Data.m_aCurrentDir, sizeof(Path.m_aData));
|
2022-06-15 17:34:41 +00:00
|
|
|
vEntries.push_back(Path);
|
2021-04-21 11:13:29 +00:00
|
|
|
|
|
|
|
// Sorts directories after files.
|
2022-06-15 17:34:41 +00:00
|
|
|
std::sort(vEntries.begin(), vEntries.end());
|
2021-04-21 11:13:29 +00:00
|
|
|
|
|
|
|
// Don't delete too many files.
|
2022-06-15 17:34:41 +00:00
|
|
|
ASSERT_LE(vEntries.size(), 10);
|
|
|
|
for(auto &Entry : vEntries)
|
2021-04-21 11:13:29 +00:00
|
|
|
{
|
|
|
|
if(Entry.m_IsDirectory)
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(fs_removedir(Entry.m_aData));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(fs_remove(Entry.m_aData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:09:08 +00:00
|
|
|
CTestInfo::~CTestInfo()
|
|
|
|
{
|
|
|
|
if(!::testing::Test::HasFailure() && m_DeleteTestStorageFilesOnSuccess)
|
|
|
|
{
|
|
|
|
TestDeleteTestStorageFiles(m_aFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 17:16:14 +00:00
|
|
|
int main(int argc, const char **argv)
|
2017-11-24 09:51:46 +00:00
|
|
|
{
|
2022-06-13 16:07:29 +00:00
|
|
|
CCmdlineFix CmdlineFix(&argc, &argv);
|
2022-04-22 23:04:48 +00:00
|
|
|
log_set_global_logger_default();
|
2021-11-21 17:16:14 +00:00
|
|
|
::testing::InitGoogleTest(&argc, const_cast<char **>(argv));
|
2017-11-24 09:51:46 +00:00
|
|
|
net_init();
|
2021-03-13 15:52:35 +00:00
|
|
|
if(secure_random_init())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "random init failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-11-21 17:16:14 +00:00
|
|
|
int Result = RUN_ALL_TESTS();
|
2022-02-13 19:04:17 +00:00
|
|
|
secure_random_uninit();
|
2021-11-21 17:16:14 +00:00
|
|
|
return Result;
|
2017-11-24 09:51:46 +00:00
|
|
|
}
|