Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 17 | #include <cppunit/TestAssert.h> |
| 18 | #include <cppunit/TestFixture.h> |
| 19 | #include <cppunit/extensions/HelperMacros.h> |
| 20 | |
| 21 | #include "test_runner.h" |
| 22 | #include "fileutils.h" |
| 23 | |
| 24 | #include <string> |
| 25 | #include <iostream> |
| 26 | #include <cstdlib> |
| 27 | #include <unistd.h> |
| 28 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 29 | namespace dhtnet { namespace fileutils { namespace test { |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 30 | |
| 31 | class FileutilsTest : public CppUnit::TestFixture { |
| 32 | public: |
| 33 | static std::string name() { return "fileutils"; } |
| 34 | |
| 35 | void setUp(); |
| 36 | void tearDown(); |
| 37 | |
| 38 | private: |
| 39 | void testCheckDir(); |
| 40 | void testPath(); |
| 41 | void testReadDirectory(); |
| 42 | void testLoadFile(); |
Adrien Béraud | 5aec410 | 2024-02-22 14:15:56 -0500 | [diff] [blame] | 43 | void testIdList(); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 44 | |
| 45 | CPPUNIT_TEST_SUITE(FileutilsTest); |
| 46 | CPPUNIT_TEST(testCheckDir); |
| 47 | CPPUNIT_TEST(testPath); |
| 48 | CPPUNIT_TEST(testReadDirectory); |
| 49 | CPPUNIT_TEST(testLoadFile); |
Adrien Béraud | 5aec410 | 2024-02-22 14:15:56 -0500 | [diff] [blame] | 50 | CPPUNIT_TEST(testIdList); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 51 | CPPUNIT_TEST_SUITE_END(); |
| 52 | |
| 53 | static constexpr auto tmpFileName = "temp_file"; |
| 54 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 55 | std::filesystem::path TEST_PATH; |
| 56 | std::filesystem::path NON_EXISTANT_PATH_BASE; |
| 57 | std::filesystem::path NON_EXISTANT_PATH; |
| 58 | std::filesystem::path EXISTANT_FILE; |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name()); |
| 62 | |
| 63 | void |
| 64 | FileutilsTest::setUp() |
| 65 | { |
Adrien Béraud | 5aec410 | 2024-02-22 14:15:56 -0500 | [diff] [blame] | 66 | char template_name[] = {"unit_tests_XXXXXX"}; |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 67 | |
| 68 | // Generate a temporary directory with a file inside |
| 69 | auto directory = mkdtemp(template_name); |
| 70 | CPPUNIT_ASSERT(directory); |
| 71 | |
| 72 | TEST_PATH = directory; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 73 | EXISTANT_FILE = TEST_PATH / tmpFileName; |
| 74 | NON_EXISTANT_PATH_BASE = TEST_PATH / "not_existing_path"; |
| 75 | NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE / "test"; |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 76 | |
| 77 | auto* fd = fopen(EXISTANT_FILE.c_str(), "w"); |
| 78 | fwrite("RING", 1, 4, fd); |
| 79 | fclose(fd); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | FileutilsTest::tearDown() |
| 84 | { |
| 85 | unlink(EXISTANT_FILE.c_str()); |
| 86 | rmdir(TEST_PATH.c_str()); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | FileutilsTest::testCheckDir() |
| 91 | { |
| 92 | // check existed directory |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 93 | CPPUNIT_ASSERT(check_dir(TEST_PATH)); |
| 94 | CPPUNIT_ASSERT(isDirectory(TEST_PATH)); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 95 | // check non-existent directory |
| 96 | CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH)); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 97 | CPPUNIT_ASSERT(check_dir(NON_EXISTANT_PATH)); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 98 | CPPUNIT_ASSERT(isDirectory(NON_EXISTANT_PATH)); |
| 99 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0); |
| 100 | CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH_BASE)); |
| 101 | //remove an non existent directory |
Sébastien Blin | 55be5da | 2024-02-12 11:29:54 -0500 | [diff] [blame] | 102 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | FileutilsTest::testPath() |
| 107 | { |
| 108 | CPPUNIT_ASSERT(isPathRelative("relativePath")); |
| 109 | CPPUNIT_ASSERT(isFile(EXISTANT_FILE)); |
| 110 | CPPUNIT_ASSERT(!isDirectory(EXISTANT_FILE)); |
| 111 | CPPUNIT_ASSERT(isDirectory(TEST_PATH)); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | FileutilsTest::testReadDirectory() |
| 116 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 117 | CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH / "readDirectory" / "test1")); |
| 118 | CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH / "readDirectory" / "test2")); |
| 119 | auto dirs = readDirectory(TEST_PATH / "readDirectory"); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 120 | CPPUNIT_ASSERT(dirs.size() == 2); |
| 121 | CPPUNIT_ASSERT( |
| 122 | (dirs.at(0).compare("test1") == 0 && dirs.at(1).compare("test2") == 0) |
| 123 | || (dirs.at(1).compare("test1") == 0 && dirs.at(0).compare("test2") == 0)); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 124 | CPPUNIT_ASSERT(removeAll(TEST_PATH / "readDirectory") == 0); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | FileutilsTest::testLoadFile() |
| 129 | { |
| 130 | auto file = loadFile(EXISTANT_FILE); |
| 131 | CPPUNIT_ASSERT(file.size() == 4); |
| 132 | CPPUNIT_ASSERT(file.at(0) == 'R'); |
| 133 | CPPUNIT_ASSERT(file.at(1) == 'I'); |
| 134 | CPPUNIT_ASSERT(file.at(2) == 'N'); |
| 135 | CPPUNIT_ASSERT(file.at(3) == 'G'); |
| 136 | } |
| 137 | |
Adrien Béraud | 5aec410 | 2024-02-22 14:15:56 -0500 | [diff] [blame] | 138 | void |
| 139 | FileutilsTest::testIdList() |
| 140 | { |
| 141 | auto path = TEST_PATH / "idList"; |
| 142 | IdList list(path); |
| 143 | list.add(1); |
| 144 | list.add(2); |
| 145 | IdList list2(path); |
| 146 | CPPUNIT_ASSERT(!list.add(1)); |
| 147 | CPPUNIT_ASSERT(!list.add(2)); |
| 148 | CPPUNIT_ASSERT(!list2.add(1)); |
| 149 | CPPUNIT_ASSERT(!list2.add(2)); |
| 150 | CPPUNIT_ASSERT(list2.add(10)); |
| 151 | CPPUNIT_ASSERT(list2.add(11)); |
| 152 | list = {path}; |
| 153 | CPPUNIT_ASSERT(list.add(5)); |
| 154 | CPPUNIT_ASSERT(list.add(6)); |
| 155 | CPPUNIT_ASSERT(!list.add(1)); |
| 156 | CPPUNIT_ASSERT(!list.add(2)); |
| 157 | CPPUNIT_ASSERT(!list.add(10)); |
| 158 | CPPUNIT_ASSERT(!list.add(11)); |
| 159 | CPPUNIT_ASSERT(removeAll(path) == 0); |
| 160 | } |
| 161 | |
| 162 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 163 | }}} // namespace dhtnet::test::fileutils |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 164 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 165 | JAMI_TEST_RUNNER(dhtnet::fileutils::test::FileutilsTest::name()); |