Adrien BĂ©raud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * Author: Olivier Gregoire <olivier.gregoire@savoirfairelinux.com> |
| 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, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include <cppunit/TestAssert.h> |
| 21 | #include <cppunit/TestFixture.h> |
| 22 | #include <cppunit/extensions/HelperMacros.h> |
| 23 | |
| 24 | #include "test_runner.h" |
| 25 | #include "fileutils.h" |
| 26 | |
| 27 | #include <string> |
| 28 | #include <iostream> |
| 29 | #include <cstdlib> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | namespace jami { namespace fileutils { namespace test { |
| 33 | |
| 34 | class FileutilsTest : public CppUnit::TestFixture { |
| 35 | public: |
| 36 | static std::string name() { return "fileutils"; } |
| 37 | |
| 38 | void setUp(); |
| 39 | void tearDown(); |
| 40 | |
| 41 | private: |
| 42 | void testCheckDir(); |
| 43 | void testPath(); |
| 44 | void testReadDirectory(); |
| 45 | void testLoadFile(); |
| 46 | void testIsDirectoryWritable(); |
| 47 | |
| 48 | CPPUNIT_TEST_SUITE(FileutilsTest); |
| 49 | CPPUNIT_TEST(testCheckDir); |
| 50 | CPPUNIT_TEST(testPath); |
| 51 | CPPUNIT_TEST(testReadDirectory); |
| 52 | CPPUNIT_TEST(testLoadFile); |
| 53 | CPPUNIT_TEST(testIsDirectoryWritable); |
| 54 | CPPUNIT_TEST_SUITE_END(); |
| 55 | |
| 56 | static constexpr auto tmpFileName = "temp_file"; |
| 57 | |
| 58 | std::string TEST_PATH; |
| 59 | std::string NON_EXISTANT_PATH_BASE; |
| 60 | std::string NON_EXISTANT_PATH; |
| 61 | std::string EXISTANT_FILE; |
| 62 | }; |
| 63 | |
| 64 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name()); |
| 65 | |
| 66 | void |
| 67 | FileutilsTest::setUp() |
| 68 | { |
| 69 | char template_name[] = {"ring_unit_tests_XXXXXX"}; |
| 70 | |
| 71 | // Generate a temporary directory with a file inside |
| 72 | auto directory = mkdtemp(template_name); |
| 73 | CPPUNIT_ASSERT(directory); |
| 74 | |
| 75 | TEST_PATH = directory; |
| 76 | EXISTANT_FILE = TEST_PATH + DIR_SEPARATOR_STR + tmpFileName; |
| 77 | NON_EXISTANT_PATH_BASE = TEST_PATH + DIR_SEPARATOR_STR + "not_existing_path"; |
| 78 | NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE + DIR_SEPARATOR_STR + "test"; |
| 79 | |
| 80 | auto* fd = fopen(EXISTANT_FILE.c_str(), "w"); |
| 81 | fwrite("RING", 1, 4, fd); |
| 82 | fclose(fd); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | FileutilsTest::tearDown() |
| 87 | { |
| 88 | unlink(EXISTANT_FILE.c_str()); |
| 89 | rmdir(TEST_PATH.c_str()); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | FileutilsTest::testCheckDir() |
| 94 | { |
| 95 | // check existed directory |
| 96 | CPPUNIT_ASSERT(check_dir(TEST_PATH.c_str())); |
| 97 | CPPUNIT_ASSERT(isDirectory(TEST_PATH.c_str())); |
| 98 | // check non-existent directory |
| 99 | CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH)); |
| 100 | CPPUNIT_ASSERT(check_dir(NON_EXISTANT_PATH.c_str())); |
| 101 | CPPUNIT_ASSERT(isDirectory(NON_EXISTANT_PATH)); |
| 102 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0); |
| 103 | CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH_BASE)); |
| 104 | //remove an non existent directory |
| 105 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == -1); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | FileutilsTest::testPath() |
| 110 | { |
| 111 | CPPUNIT_ASSERT(isPathRelative("relativePath")); |
| 112 | CPPUNIT_ASSERT(isFile(EXISTANT_FILE)); |
| 113 | CPPUNIT_ASSERT(!isDirectory(EXISTANT_FILE)); |
| 114 | CPPUNIT_ASSERT(isDirectory(TEST_PATH)); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | FileutilsTest::testReadDirectory() |
| 119 | { |
| 120 | CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory" + DIR_SEPARATOR_STR + "test1")); |
| 121 | CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory" + DIR_SEPARATOR_STR + "test2")); |
| 122 | auto dirs = readDirectory(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory"); |
| 123 | CPPUNIT_ASSERT(dirs.size() == 2); |
| 124 | CPPUNIT_ASSERT( |
| 125 | (dirs.at(0).compare("test1") == 0 && dirs.at(1).compare("test2") == 0) |
| 126 | || (dirs.at(1).compare("test1") == 0 && dirs.at(0).compare("test2") == 0)); |
| 127 | CPPUNIT_ASSERT(removeAll(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory") == 0); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | FileutilsTest::testLoadFile() |
| 132 | { |
| 133 | auto file = loadFile(EXISTANT_FILE); |
| 134 | CPPUNIT_ASSERT(file.size() == 4); |
| 135 | CPPUNIT_ASSERT(file.at(0) == 'R'); |
| 136 | CPPUNIT_ASSERT(file.at(1) == 'I'); |
| 137 | CPPUNIT_ASSERT(file.at(2) == 'N'); |
| 138 | CPPUNIT_ASSERT(file.at(3) == 'G'); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | FileutilsTest::testIsDirectoryWritable() |
| 143 | { |
| 144 | CPPUNIT_ASSERT(recursive_mkdir(NON_EXISTANT_PATH_BASE)); |
| 145 | CPPUNIT_ASSERT(isDirectoryWritable(NON_EXISTANT_PATH_BASE)); |
| 146 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0); |
| 147 | // Create directory with permission: read by owner |
| 148 | CPPUNIT_ASSERT(recursive_mkdir(NON_EXISTANT_PATH_BASE, 0400)); |
| 149 | CPPUNIT_ASSERT(!isDirectoryWritable(NON_EXISTANT_PATH_BASE)); |
| 150 | CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0); |
| 151 | } |
| 152 | |
| 153 | }}} // namespace jami::test::fileutils |
| 154 | |
| 155 | JAMI_TEST_RUNNER(jami::fileutils::test::FileutilsTest::name()); |
| 156 | |