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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
//------------------------------------------------------------------------
//
// Eureka DOOM Editor
//
// Copyright (C) 2020 Ioan Chera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "lib_file.h"
#include "m_strings.h"
#include "testUtils/TempDirContext.hpp"
#include "gtest/gtest.h"
#include "FL/filename.H"
#include <fstream>
class LibFileTempDir : public TempDirContext
{
};
TEST(LibFile, HasExtension)
{
ASSERT_FALSE(HasExtension("man/doc."));
ASSERT_TRUE(HasExtension("man/doom.wad"));
ASSERT_FALSE(HasExtension("man.wad/doom"));
ASSERT_TRUE(HasExtension("man.wad/doom.wad"));
ASSERT_TRUE(HasExtension("man.wad/doom..wad"));
ASSERT_FALSE(HasExtension(".okay"));
ASSERT_FALSE(HasExtension("man/.okay"));
ASSERT_TRUE(HasExtension("man/..okay"));
ASSERT_FALSE(HasExtension("man/.okay."));
ASSERT_TRUE(HasExtension("man/.okay.wad"));
ASSERT_FALSE(HasExtension("/."));
ASSERT_FALSE(HasExtension("."));
ASSERT_FALSE(HasExtension(".."));
ASSERT_FALSE(HasExtension("abc/.."));
ASSERT_FALSE(HasExtension("../.."));
ASSERT_FALSE(HasExtension(""));
}
TEST(LibFile, MatchExtension)
{
ASSERT_TRUE(MatchExtensionNoCase("man/doc.", nullptr));
ASSERT_TRUE(MatchExtensionNoCase("man/doc.", ""));
ASSERT_FALSE(MatchExtensionNoCase("man/.doc.", ".doc"));
ASSERT_TRUE(MatchExtensionNoCase("man/doc. ", ". "));
ASSERT_TRUE(MatchExtensionNoCase("man.wad/doom", nullptr));
ASSERT_FALSE(MatchExtensionNoCase("man.wad/doom", "doom"));
ASSERT_TRUE(MatchExtensionNoCase("man.wad/doom.wad", ".WAD"));
ASSERT_TRUE(MatchExtensionNoCase("man.wad/doom..wad", ".WAD"));
ASSERT_TRUE(MatchExtensionNoCase(".okay", ""));
ASSERT_FALSE(MatchExtensionNoCase(".okay", ".okay"));
ASSERT_TRUE(MatchExtensionNoCase("man/.okay", ""));
ASSERT_FALSE(MatchExtensionNoCase("man/.okay", ".okay"));
ASSERT_TRUE(MatchExtensionNoCase("man/.okay.WAD", ".wad"));
ASSERT_TRUE(MatchExtensionNoCase("/.", nullptr));
ASSERT_TRUE(MatchExtensionNoCase(".", nullptr));
ASSERT_TRUE(MatchExtensionNoCase("..", nullptr));
ASSERT_FALSE(MatchExtensionNoCase("..", "."));
ASSERT_TRUE(MatchExtensionNoCase("", nullptr));
}
TEST(LibFile, ReplaceExtension)
{
ASSERT_EQ(ReplaceExtension("man/doc.", "wad"), "man/doc.wad");
ASSERT_EQ(ReplaceExtension("man/doc.", "WAD"), "man/doc.WAD");
ASSERT_EQ(ReplaceExtension("man/doc.", ""), "man/doc");
ASSERT_EQ(ReplaceExtension("man/doc.", nullptr), "man/doc");
ASSERT_EQ(ReplaceExtension("man/.doc", ""), "man/.doc");
ASSERT_EQ(ReplaceExtension("man/.doc", nullptr), "man/.doc");
ASSERT_EQ(ReplaceExtension("man/.doc", "wad"), "man/.doc.wad");
ASSERT_EQ(ReplaceExtension("man.wad/doom", "waD"), "man.wad/doom.waD");
ASSERT_EQ(ReplaceExtension("man.wad/doom.wad", ".txt"), "man.wad/doom.txt");
ASSERT_EQ(ReplaceExtension("man.wad/doom.wad", ""), "man.wad/doom");
ASSERT_EQ(ReplaceExtension("man.wad/doom.wad", nullptr), "man.wad/doom");
ASSERT_EQ(ReplaceExtension("man.wad/doom..wad", nullptr), "man.wad/doom.");
ASSERT_EQ(ReplaceExtension("man.wad/doom..wad", ""), "man.wad/doom.");
ASSERT_EQ(ReplaceExtension("man.wad/doom..wad", "txt"), "man.wad/doom..txt");
ASSERT_EQ(ReplaceExtension(".okay", "txt"), ".okay.txt");
ASSERT_EQ(ReplaceExtension(".okay", ""), ".okay");
ASSERT_EQ(ReplaceExtension(".okay", nullptr), ".okay");
ASSERT_EQ(ReplaceExtension("/.", nullptr), "/.");
ASSERT_EQ(ReplaceExtension("/.", "txt"), "/..txt");
ASSERT_EQ(ReplaceExtension(".", "txt"), "..txt");
ASSERT_EQ(ReplaceExtension("", "txt"), ".txt");
ASSERT_EQ(ReplaceExtension(".", ""), ".");
ASSERT_EQ(ReplaceExtension(".", nullptr), ".");
ASSERT_EQ(ReplaceExtension("..", ""), "..");
ASSERT_EQ(ReplaceExtension("..", nullptr), "..");
ASSERT_EQ(ReplaceExtension("..", "txt"), "..txt");
ASSERT_EQ(ReplaceExtension("..txt", "wad"), "..wad");
ASSERT_EQ(ReplaceExtension("..txt", ""), ".");
ASSERT_EQ(ReplaceExtension("..txt", nullptr), ".");
ASSERT_EQ(ReplaceExtension("", ""), "");
ASSERT_EQ(ReplaceExtension("", nullptr), "");
ASSERT_EQ(ReplaceExtension("", "wad"), ".wad");
}
TEST(LibFile, FilenameGetPath)
{
ASSERT_EQ(FilenameGetPath("path/to/file"), "path/to");
ASSERT_EQ(FilenameGetPath("path/to" DIR_SEP_STR DIR_SEP_STR "file"), "path/to");
ASSERT_EQ(FilenameGetPath("file"), ".");
ASSERT_EQ(FilenameGetPath(""), ".");
ASSERT_EQ(FilenameGetPath(DIR_SEP_STR "file"), DIR_SEP_STR);
ASSERT_EQ(FilenameGetPath("/doom/"), "/doom");
ASSERT_TRUE(FilenameGetPath("///doom.wad") == "/" || FilenameGetPath("///doom.wad") == "\\");
#ifdef _WIN32
ASSERT_EQ(FilenameGetPath("C:" DIR_SEP_STR "file"), "C:" DIR_SEP_STR);
#endif
}
TEST(LibFile, GetBaseName)
{
ASSERT_EQ(GetBaseName("path/to///fileA.wad"), "fileA.wad");
ASSERT_EQ(GetBaseName("path/to/fileB"), "fileB");
ASSERT_EQ(GetBaseName("/fileC.txt"), "fileC.txt");
ASSERT_EQ(GetBaseName("/file"), "file");
ASSERT_EQ(GetBaseName("//dir/file"), "file");
ASSERT_EQ(GetBaseName("/file"), "file");
ASSERT_EQ(GetBaseName("fil"), "fil");
ASSERT_EQ(GetBaseName(""), "");
}
TEST(LibFile, FilenameIsBare)
{
ASSERT_TRUE(FilenameIsBare(""));
ASSERT_TRUE(FilenameIsBare("Doom"));
ASSERT_TRUE(FilenameIsBare("DOOM"));
ASSERT_TRUE(FilenameIsBare("doom"));
ASSERT_FALSE(FilenameIsBare("/doom"));
ASSERT_FALSE(FilenameIsBare("/doom.wad"));
ASSERT_FALSE(FilenameIsBare("doom.wad"));
ASSERT_FALSE(FilenameIsBare("C:\\doom"));
}
TEST(LibFile, GetAbsolutePath)
{
char path[FL_PATH_MAX];
int result = fl_filename_absolute(path, sizeof(path), "Hello");
ASSERT_NE(result, 0); // absolute path stays absolute
SString stringResult = GetAbsolutePath("Hello").generic_u8string();
ASSERT_STREQ(stringResult.c_str(), path);
}
TEST(LibFile, Escape)
{
ASSERT_EQ(escape("/path with spaces and a quote\"/ okay"), "\"/path with spaces and a quote\"\"/ okay\"");
}
TEST_F(LibFileTempDir, FileExists)
{
fs::path path = getSubPath("hello");
ASSERT_FALSE(FileExists(path));
std::ofstream os(path);
ASSERT_TRUE(os.is_open());
os.close();
mDeleteList.push(path);
ASSERT_TRUE(FileExists(path));
fs::remove(path);
mDeleteList.pop();
// Deleted, now must be back to false
ASSERT_FALSE(FileExists(path));
ASSERT_TRUE(FileMakeDir(path));
mDeleteList.push(path);
ASSERT_FALSE(FileExists(path));
}
TEST_F(LibFileTempDir, FileDelete)
{
fs::path path = getSubPath("file");
ASSERT_FALSE(FileDelete(path)); // can't delete what is not there
std::ofstream os(path);
ASSERT_TRUE(os.is_open());
mDeleteList.push(path);
os << "Hello";
os.close();
ASSERT_TRUE(FileDelete(path));
mDeleteList.pop();
}
// NOTE: FileChangeDir modifies the process state
TEST_F(LibFileTempDir, FileMakeDir)
{
fs::path path = getSubPath("dir");
ASSERT_TRUE(FileMakeDir(path));
mDeleteList.push(path);
// Disallow overwriting
ASSERT_FALSE(FileMakeDir(path));
// Disallow inexistent intermediary paths
ASSERT_FALSE(FileMakeDir(getSubPath(fs::path("dir2") / "dir3")));
}
TEST_F(LibFileTempDir, FileMakeDirs)
{
fs::path path = getSubPath("dir1/dir2/dir3");
ASSERT_TRUE(FileMakeDirs(path));
mDeleteList.push(getSubPath("dir1"));
mDeleteList.push(getSubPath("dir1/dir2"));
mDeleteList.push(getSubPath("dir1/dir2/dir3"));
}
TEST_F(LibFileTempDir, FileLoad)
{
// Must test a sufficiently large file
std::vector<char> randomData;
randomData.reserve(40000);
for(int i = 0; i < 40000; ++i)
randomData.push_back(static_cast<char>(rand() & 0xff));
fs::path path = getSubPath("file");
std::ofstream os(path, std::ios::binary);
ASSERT_TRUE(os.is_open());
mDeleteList.push(path);
os.write(randomData.data(), randomData.size());
os.close();
std::vector<uint8_t> result;
ASSERT_TRUE(FileLoad(path, result));
ASSERT_EQ(result.size(), 40000);
ASSERT_EQ(memcmp(result.data(), randomData.data(), result.size()), 0);
// Mustn't read dirs
ASSERT_FALSE(FileLoad(mTempDir, result));
// Mustn't read inexistent files
ASSERT_FALSE(FileLoad(getSubPath("file2"), result));
#ifndef _WIN32
// Mustn't read special files
ASSERT_FALSE(FileLoad("/dev/null", result));
ASSERT_FALSE(FileLoad("/dev/urandom", result));
#endif
}
TEST_F(LibFileTempDir, ScanDirectory)
{
// 1. Add a file, a "hidden" file, a folder and a "hidden" folder
fs::path path = getSubPath("file");
std::ofstream os(path);
ASSERT_TRUE(os.is_open());
mDeleteList.push(path);
os << "hello";
os.close();
fs::path filePath = path;
path = getSubPath(".file");
os.open(path);
ASSERT_TRUE(os.is_open());
mDeleteList.push(path);
os << "shadow";
os.close();
path = getSubPath("dir");
ASSERT_TRUE(FileMakeDir(path));
mDeleteList.push(path);
path = getSubPath(".dir");
ASSERT_TRUE(FileMakeDir(path));
mDeleteList.push(path);
fs::path dotDirPath = path;
// Also nest another file, to check it doesn't get listed
path = getSubPath(fs::path("dir") / "file2");
os.open(path);
ASSERT_TRUE(os.is_open());
mDeleteList.push(path);
os << "shadow2";
os.close();
int result = ScanDirectory(mTempDir, [](const fs::path &name, int flags)
{
if(name == "file")
ASSERT_EQ(flags, 0);
else if(name == ".file")
ASSERT_TRUE(flags & SCAN_F_Hidden);
else if(name == "dir")
ASSERT_TRUE(flags & SCAN_F_IsDir);
else if(name == ".dir")
ASSERT_EQ(flags & (SCAN_F_IsDir | SCAN_F_Hidden), SCAN_F_IsDir | SCAN_F_Hidden);
else
ASSERT_FALSE(true); // error if getting here
});
ASSERT_EQ(result, 4); // scan no more
// Now also try on some non-folder files
result = ScanDirectory(filePath, [](const fs::path &name, int flags)
{
ASSERT_FALSE(true); // should not get here
});
ASSERT_EQ(result, SCAN_ERR_NotDir);
result = ScanDirectory(getSubPath("illegal"), [](const fs::path &name, int flags)
{
ASSERT_FALSE(true); // should not get here
});
ASSERT_EQ(result, SCAN_ERR_NoExist);
// Also scan an empty dir
result = ScanDirectory(dotDirPath, [](const fs::path &name, int flags)
{
ASSERT_FALSE(true); // should not get here
});
ASSERT_EQ(result, 0);
}
|