| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Descent Developers |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <filesystem> |
| 21 | #include <vector> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include "cfile.h" |
| 24 | |
| 25 | TEST(D3, CFileIO) { |
| 26 | int lib_handle = cf_OpenLibrary("TestDir/test.hog"); |
| 27 | CFILE *file_handle = cfopen("lowercase.txt", "rb"); |
| 28 | char buf[5]; |
| 29 | cf_ReadString(buf, 5, file_handle); |
| 30 | EXPECT_STREQ(buf, "TEST"); |
| 31 | cf_Rewind(file_handle); |
| 32 | |
| 33 | EXPECT_EQ(cf_ReadByte(file_handle), 84); |
| 34 | cf_Rewind(file_handle); |
| 35 | |
| 36 | EXPECT_EQ(cf_ReadShort(file_handle), 17748); |
| 37 | cf_Rewind(file_handle); |
| 38 | |
| 39 | EXPECT_EQ(cf_ReadInt(file_handle), 1414743380); |
| 40 | cf_Rewind(file_handle); |
| 41 | |
| 42 | cf_CloseLibrary(lib_handle); |
| 43 | } |
| 44 | |
| 45 | TEST(D3, CFileLibrary) { |
| 46 | // First pass - without search path in "TestDir" (i.e. not search actual files in directory) |
| 47 | // Second pass - with search path (files in directory goes first) |
| 48 | for (int i = 0; i < 2; i++) { |
| 49 | if (i != 0) { |
| 50 | EXPECT_EQ(cf_SetSearchPath("TestDir"), true); |
| 51 | } |
| 52 | |
| 53 | int lib_handle = cf_OpenLibrary("TestDir/test.hog"); |
| 54 | EXPECT_NE(lib_handle, 0); |
| 55 | |
| 56 | CFILE *file_handle = cf_OpenFileInLibrary("lowercase.txt", lib_handle); |
| 57 | EXPECT_NE(file_handle, nullptr); |
| 58 | file_handle = cfopen("lowercase.txt", "rb"); |
| 59 | EXPECT_NE(file_handle, nullptr); |
| 60 | // Length in hog is 4, in directory is 0 |
| 61 | EXPECT_EQ(cfilelength(file_handle), i == 0 ? 4 : 0); |
| 62 | EXPECT_EQ(cf_CalculateFileCRC(file_handle), i == 0 ? 4008350648 : 0); |
| 63 | |
| 64 | cf_ClearAllSearchPaths(); |
| 65 | cf_CloseLibrary(lib_handle); |
| 66 | } |
| 67 | |
| 68 | // Try to search on non-initialized paths and files |
| 69 | CFILE *file_handle = cfopen("lowercase.txt", "rb"); |
| 70 | EXPECT_EQ(file_handle, nullptr); |
| 71 | } |
| 72 | |
| 73 | TEST(D3, CFileCaseSensitiveSearchNew) { |
| 74 | const std::vector<std::filesystem::path> test_paths = { |
| 75 | std::filesystem::path("TestDir") / "CamelCase.txt", |
| 76 | std::filesystem::path("TestDir") / "lowercase.txt", |
| 77 | std::filesystem::path("TestDir") / "UPPERCASE.TXT", |
| 78 | }; |
| 79 | |
| 80 | std::filesystem::path filename_new = cf_FindRealFileNameCaseInsensitive("no-exist-file.txt", "no-exist-dir"); |
| 81 | EXPECT_TRUE(filename_new.empty()); |
| 82 | |
| 83 | filename_new = cf_FindRealFileNameCaseInsensitive("no-exist-file.txt"); |
| 84 | EXPECT_TRUE(filename_new.empty()); |
| 85 | |
| 86 | auto cwd = std::filesystem::current_path(); |
| 87 | |
| 88 | for (auto const &item : test_paths) { |
| 89 | auto directory = cwd / item.parent_path(); |
| 90 | std::filesystem::path file = item.filename(); |
| 91 | std::string file_lc = item.filename().u8string(); |
| 92 | std::transform(file_lc.begin(), file_lc.end(), file_lc.begin(), ::tolower); |
| 93 | std::string file_uc = item.filename().u8string(); |
| 94 | std::transform(file_uc.begin(), file_uc.end(), file_uc.begin(), ::toupper); |
| 95 | |
| 96 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_lc, directory).empty()); |
| 97 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_uc, directory).empty()); |
| 98 | |
| 99 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(directory / file_lc).empty()); |
| 100 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(directory / file_uc).empty()); |
| 101 | |
| 102 | // Now try case-insensitive path with non-existing directory in search. |
| 103 | // Expected not found on case-sensitive fs. |
| 104 | file_lc = item.u8string(); |
| 105 | std::transform(file_lc.begin(), file_lc.end(), file_lc.begin(), ::tolower); |
| 106 | file_uc = item.u8string(); |
| 107 | std::transform(file_uc.begin(), file_uc.end(), file_uc.begin(), ::toupper); |
| 108 | |
| 109 | if (std::filesystem::is_regular_file(file_lc)) { |
| 110 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_lc, cwd).empty()); |
| 111 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_lc).empty()); |
| 112 | } else { |
| 113 | EXPECT_TRUE(cf_FindRealFileNameCaseInsensitive(file_lc, cwd).empty()); |
| 114 | EXPECT_TRUE(cf_FindRealFileNameCaseInsensitive(file_lc).empty()); |
| 115 | } |
| 116 | if (std::filesystem::is_regular_file(file_uc)) { |
| 117 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_uc, cwd).empty()); |
| 118 | EXPECT_FALSE(cf_FindRealFileNameCaseInsensitive(file_uc).empty()); |
| 119 | } else { |
| 120 | EXPECT_TRUE(cf_FindRealFileNameCaseInsensitive(file_uc, cwd).empty()); |
| 121 | EXPECT_TRUE(cf_FindRealFileNameCaseInsensitive(file_uc).empty()); |
| 122 | } |
| 123 | } |
| 124 | } |