blob: 72ad2d23a1516d7aa907658c9392ce0ce0b9818c [file] [log] [blame]
Adrien Béraudefe27372023-05-27 18:56:29 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
Adrien Béraudefe27372023-05-27 18:56:29 -04003 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraudefe27372023-05-27 18:56:29 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraudefe27372023-05-27 18:56:29 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraudefe27372023-05-27 18:56:29 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraudefe27372023-05-27 18:56:29 -040016 */
Adrien Béraudefe27372023-05-27 18:56:29 -040017#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éraud1ae60aa2023-07-07 09:55:09 -040029namespace dhtnet { namespace fileutils { namespace test {
Adrien Béraudefe27372023-05-27 18:56:29 -040030
31class FileutilsTest : public CppUnit::TestFixture {
32public:
33 static std::string name() { return "fileutils"; }
34
35 void setUp();
36 void tearDown();
37
38private:
39 void testCheckDir();
40 void testPath();
41 void testReadDirectory();
42 void testLoadFile();
Adrien Béraud5aec4102024-02-22 14:15:56 -050043 void testIdList();
Adrien Béraudefe27372023-05-27 18:56:29 -040044
45 CPPUNIT_TEST_SUITE(FileutilsTest);
46 CPPUNIT_TEST(testCheckDir);
47 CPPUNIT_TEST(testPath);
48 CPPUNIT_TEST(testReadDirectory);
49 CPPUNIT_TEST(testLoadFile);
Adrien Béraud5aec4102024-02-22 14:15:56 -050050 CPPUNIT_TEST(testIdList);
Adrien Béraudefe27372023-05-27 18:56:29 -040051 CPPUNIT_TEST_SUITE_END();
52
53 static constexpr auto tmpFileName = "temp_file";
54
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040055 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éraudefe27372023-05-27 18:56:29 -040059};
60
61CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name());
62
63void
64FileutilsTest::setUp()
65{
Adrien Béraud5aec4102024-02-22 14:15:56 -050066 char template_name[] = {"unit_tests_XXXXXX"};
Adrien Béraudefe27372023-05-27 18:56:29 -040067
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éraud2a4e73d2023-08-27 12:53:55 -040073 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éraudefe27372023-05-27 18:56:29 -040076
77 auto* fd = fopen(EXISTANT_FILE.c_str(), "w");
78 fwrite("RING", 1, 4, fd);
79 fclose(fd);
80}
81
82void
83FileutilsTest::tearDown()
84{
85 unlink(EXISTANT_FILE.c_str());
86 rmdir(TEST_PATH.c_str());
87}
88
89void
90FileutilsTest::testCheckDir()
91{
92 // check existed directory
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040093 CPPUNIT_ASSERT(check_dir(TEST_PATH));
94 CPPUNIT_ASSERT(isDirectory(TEST_PATH));
Adrien Béraudefe27372023-05-27 18:56:29 -040095 // check non-existent directory
96 CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH));
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040097 CPPUNIT_ASSERT(check_dir(NON_EXISTANT_PATH));
Adrien Béraudefe27372023-05-27 18:56:29 -040098 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 Blin55be5da2024-02-12 11:29:54 -0500102 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0);
Adrien Béraudefe27372023-05-27 18:56:29 -0400103}
104
105void
106FileutilsTest::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
114void
115FileutilsTest::testReadDirectory()
116{
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400117 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éraudefe27372023-05-27 18:56:29 -0400120 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éraud2a4e73d2023-08-27 12:53:55 -0400124 CPPUNIT_ASSERT(removeAll(TEST_PATH / "readDirectory") == 0);
Adrien Béraudefe27372023-05-27 18:56:29 -0400125}
126
127void
128FileutilsTest::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éraud5aec4102024-02-22 14:15:56 -0500138void
139FileutilsTest::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éraud1ae60aa2023-07-07 09:55:09 -0400163}}} // namespace dhtnet::test::fileutils
Adrien Béraudefe27372023-05-27 18:56:29 -0400164
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400165JAMI_TEST_RUNNER(dhtnet::fileutils::test::FileutilsTest::name());