1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include "System/FileSystem/FileSystem.h"
#include <string>
#include <cstdio>
#include <sys/stat.h>
#include "System/Log/ILog.h"
#define BOOST_TEST_MODULE FileSystem
#include <boost/test/unit_test.hpp>
namespace {
struct PrepareFileSystem {
PrepareFileSystem() {
oldDir = FileSystem::GetCwd();
testCwd = GetTempDir();
if (!testCwd.empty()) {
FileSystem::ChDir(testCwd);
// create files and dirs to test
FileSystem::CreateDirectory("testDir");
WriteFile("testFile.txt", "a");
}
}
~PrepareFileSystem() {
if (!testCwd.empty()) {
// delete files and dirs created in the ctor
FileSystem::DeleteFile("testFile.txt");
FileSystem::DeleteFile("testDir");
}
FileSystem::ChDir(oldDir);
if (!testCwd.empty()) {
FileSystem::DeleteFile(testCwd);
}
}
static void WriteFile(const std::string& filePath, const std::string& content) {
FILE* testFile = fopen(filePath.c_str(), "w");
if (testFile != NULL) {
fprintf(testFile, "%s", content.c_str());
fclose(testFile);
testFile = NULL;
} else {
BOOST_FAIL("Failed to create test-file " + filePath);
}
}
std::string GetTempDir() {
if (testCwd.empty()) {
char* tmpDir = tmpnam(NULL);
if (tmpDir != NULL) {
testCwd = oldDir + tmpDir;
FileSystem::CreateDirectory(testCwd);
if (!FileSystem::DirIsWritable(testCwd)) {
BOOST_FAIL("Failed to create temporary test dir");
}
} else {
BOOST_FAIL("Failed to get temporary file name");
}
}
return testCwd;
}
std::string testCwd;
private:
std::string oldDir;
};
}
BOOST_FIXTURE_TEST_SUITE(everything, PrepareFileSystem)
BOOST_AUTO_TEST_CASE(FileExists)
{
BOOST_CHECK(FileSystem::FileExists("testFile.txt"));
BOOST_CHECK(!FileSystem::FileExists("testFile99.txt"));
BOOST_CHECK(!FileSystem::FileExists("testDir"));
BOOST_CHECK(!FileSystem::FileExists("testDir99"));
}
BOOST_AUTO_TEST_CASE(GetFileSize)
{
BOOST_CHECK(FileSystem::GetFileSize("testFile.txt") == 1);
BOOST_CHECK(FileSystem::GetFileSize("testFile99.txt") == -1);
BOOST_CHECK(FileSystem::GetFileSize("testDir") == -1);
BOOST_CHECK(FileSystem::GetFileSize("testDir99") == -1);
}
BOOST_AUTO_TEST_CASE(GetFileModificationDate)
{
BOOST_CHECK(FileSystem::GetFileModificationDate("testDir") != "");
BOOST_CHECK(FileSystem::GetFileModificationDate("testFile.txt") != "");
BOOST_CHECK(FileSystem::GetFileModificationDate("not_there") == "");
}
BOOST_AUTO_TEST_CASE(CreateDirectory)
{
// create & exists
BOOST_CHECK(FileSystem::DirIsWritable("./"));
BOOST_CHECK(FileSystem::DirExists("testDir"));
BOOST_CHECK(FileSystem::DirExists("testDir///"));
BOOST_CHECK(FileSystem::DirExists("testDir////./"));
BOOST_CHECK(FileSystem::ComparePaths("testDir", "testDir////./"));
BOOST_CHECK(!FileSystem::ComparePaths("testDir", "test Dir2"));
BOOST_CHECK(FileSystem::CreateDirectory("testDir")); // already exists
BOOST_CHECK(FileSystem::CreateDirectory("testDir1")); // should be created
BOOST_CHECK(FileSystem::CreateDirectory("test Dir2")); // should be created
// check if exists & no overwrite
BOOST_CHECK(FileSystem::CreateDirectory("test Dir2")); // already exists
BOOST_CHECK(FileSystem::DirIsWritable("test Dir2"));
BOOST_CHECK(!FileSystem::CreateDirectory("testFile.txt")); // file with this name already exists
// delete temporaries
BOOST_CHECK(FileSystem::DeleteFile("testDir1"));
BOOST_CHECK(FileSystem::DeleteFile("test Dir2"));
// check if really deleted
BOOST_CHECK(!FileSystem::DirExists("testDir1"));
BOOST_CHECK(!FileSystem::DirExists("test Dir2"));
}
BOOST_AUTO_TEST_CASE(GetDirectory)
{
#define CHECK_DIR_EXTRACTION(path, dir) \
BOOST_CHECK(FileSystem::GetDirectory(path) == dir)
CHECK_DIR_EXTRACTION("testFile.txt", "");
CHECK_DIR_EXTRACTION("./foo/testFile.txt", "./foo/");
#undef CHECK_DIR_EXTRACTION
}
BOOST_AUTO_TEST_CASE(GetNormalizedPath)
{
#define CHECK_NORM_PATH(path, normPath) \
BOOST_CHECK(FileSystem::GetNormalizedPath(path) == normPath)
CHECK_NORM_PATH("/home/userX/.spring/foo/bar///./../test.log", "/home/userX/.spring/foo/test.log");
CHECK_NORM_PATH("./symLinkToHome/foo/bar///./../test.log", "./symLinkToHome/foo/test.log");
CHECK_NORM_PATH("C:\\foo\\bar\\\\\\.\\..\\test.log", "C:/foo/test.log");
#undef CHECK_NORM_PATH
}
BOOST_AUTO_TEST_SUITE_END()
|