blob: 9bc3367645c0e5f3da8e50480a1ab6d36742606c [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éraudefe27372023-05-27 18:56:29 -040043
44 CPPUNIT_TEST_SUITE(FileutilsTest);
45 CPPUNIT_TEST(testCheckDir);
46 CPPUNIT_TEST(testPath);
47 CPPUNIT_TEST(testReadDirectory);
48 CPPUNIT_TEST(testLoadFile);
Adrien Béraudefe27372023-05-27 18:56:29 -040049 CPPUNIT_TEST_SUITE_END();
50
51 static constexpr auto tmpFileName = "temp_file";
52
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040053 std::filesystem::path TEST_PATH;
54 std::filesystem::path NON_EXISTANT_PATH_BASE;
55 std::filesystem::path NON_EXISTANT_PATH;
56 std::filesystem::path EXISTANT_FILE;
Adrien Béraudefe27372023-05-27 18:56:29 -040057};
58
59CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name());
60
61void
62FileutilsTest::setUp()
63{
64 char template_name[] = {"ring_unit_tests_XXXXXX"};
65
66 // Generate a temporary directory with a file inside
67 auto directory = mkdtemp(template_name);
68 CPPUNIT_ASSERT(directory);
69
70 TEST_PATH = directory;
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040071 EXISTANT_FILE = TEST_PATH / tmpFileName;
72 NON_EXISTANT_PATH_BASE = TEST_PATH / "not_existing_path";
73 NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE / "test";
Adrien Béraudefe27372023-05-27 18:56:29 -040074
75 auto* fd = fopen(EXISTANT_FILE.c_str(), "w");
76 fwrite("RING", 1, 4, fd);
77 fclose(fd);
78}
79
80void
81FileutilsTest::tearDown()
82{
83 unlink(EXISTANT_FILE.c_str());
84 rmdir(TEST_PATH.c_str());
85}
86
87void
88FileutilsTest::testCheckDir()
89{
90 // check existed directory
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040091 CPPUNIT_ASSERT(check_dir(TEST_PATH));
92 CPPUNIT_ASSERT(isDirectory(TEST_PATH));
Adrien Béraudefe27372023-05-27 18:56:29 -040093 // check non-existent directory
94 CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH));
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040095 CPPUNIT_ASSERT(check_dir(NON_EXISTANT_PATH));
Adrien Béraudefe27372023-05-27 18:56:29 -040096 CPPUNIT_ASSERT(isDirectory(NON_EXISTANT_PATH));
97 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0);
98 CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH_BASE));
99 //remove an non existent directory
100 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == -1);
101}
102
103void
104FileutilsTest::testPath()
105{
106 CPPUNIT_ASSERT(isPathRelative("relativePath"));
107 CPPUNIT_ASSERT(isFile(EXISTANT_FILE));
108 CPPUNIT_ASSERT(!isDirectory(EXISTANT_FILE));
109 CPPUNIT_ASSERT(isDirectory(TEST_PATH));
110}
111
112void
113FileutilsTest::testReadDirectory()
114{
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400115 CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH / "readDirectory" / "test1"));
116 CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH / "readDirectory" / "test2"));
117 auto dirs = readDirectory(TEST_PATH / "readDirectory");
Adrien Béraudefe27372023-05-27 18:56:29 -0400118 CPPUNIT_ASSERT(dirs.size() == 2);
119 CPPUNIT_ASSERT(
120 (dirs.at(0).compare("test1") == 0 && dirs.at(1).compare("test2") == 0)
121 || (dirs.at(1).compare("test1") == 0 && dirs.at(0).compare("test2") == 0));
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400122 CPPUNIT_ASSERT(removeAll(TEST_PATH / "readDirectory") == 0);
Adrien Béraudefe27372023-05-27 18:56:29 -0400123}
124
125void
126FileutilsTest::testLoadFile()
127{
128 auto file = loadFile(EXISTANT_FILE);
129 CPPUNIT_ASSERT(file.size() == 4);
130 CPPUNIT_ASSERT(file.at(0) == 'R');
131 CPPUNIT_ASSERT(file.at(1) == 'I');
132 CPPUNIT_ASSERT(file.at(2) == 'N');
133 CPPUNIT_ASSERT(file.at(3) == 'G');
134}
135
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400136}}} // namespace dhtnet::test::fileutils
Adrien Béraudefe27372023-05-27 18:56:29 -0400137
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400138JAMI_TEST_RUNNER(dhtnet::fileutils::test::FileutilsTest::name());
Adrien Béraudefe27372023-05-27 18:56:29 -0400139