blob: fa353a6ad2bb619aa98abd8e51638a89bd6caa49 [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();
43 void testIsDirectoryWritable();
44
45 CPPUNIT_TEST_SUITE(FileutilsTest);
46 CPPUNIT_TEST(testCheckDir);
47 CPPUNIT_TEST(testPath);
48 CPPUNIT_TEST(testReadDirectory);
49 CPPUNIT_TEST(testLoadFile);
50 CPPUNIT_TEST(testIsDirectoryWritable);
51 CPPUNIT_TEST_SUITE_END();
52
53 static constexpr auto tmpFileName = "temp_file";
54
55 std::string TEST_PATH;
56 std::string NON_EXISTANT_PATH_BASE;
57 std::string NON_EXISTANT_PATH;
58 std::string EXISTANT_FILE;
59};
60
61CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name());
62
63void
64FileutilsTest::setUp()
65{
66 char template_name[] = {"ring_unit_tests_XXXXXX"};
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;
73 EXISTANT_FILE = TEST_PATH + DIR_SEPARATOR_STR + tmpFileName;
74 NON_EXISTANT_PATH_BASE = TEST_PATH + DIR_SEPARATOR_STR + "not_existing_path";
75 NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE + DIR_SEPARATOR_STR + "test";
76
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
93 CPPUNIT_ASSERT(check_dir(TEST_PATH.c_str()));
94 CPPUNIT_ASSERT(isDirectory(TEST_PATH.c_str()));
95 // check non-existent directory
96 CPPUNIT_ASSERT(!isDirectory(NON_EXISTANT_PATH));
97 CPPUNIT_ASSERT(check_dir(NON_EXISTANT_PATH.c_str()));
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
102 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == -1);
103}
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{
117 CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory" + DIR_SEPARATOR_STR + "test1"));
118 CPPUNIT_ASSERT(recursive_mkdir(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory" + DIR_SEPARATOR_STR + "test2"));
119 auto dirs = readDirectory(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory");
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));
124 CPPUNIT_ASSERT(removeAll(TEST_PATH + DIR_SEPARATOR_STR + "readDirectory") == 0);
125}
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
138void
139FileutilsTest::testIsDirectoryWritable()
140{
141 CPPUNIT_ASSERT(recursive_mkdir(NON_EXISTANT_PATH_BASE));
142 CPPUNIT_ASSERT(isDirectoryWritable(NON_EXISTANT_PATH_BASE));
143 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0);
144 // Create directory with permission: read by owner
145 CPPUNIT_ASSERT(recursive_mkdir(NON_EXISTANT_PATH_BASE, 0400));
146 CPPUNIT_ASSERT(!isDirectoryWritable(NON_EXISTANT_PATH_BASE));
147 CPPUNIT_ASSERT(removeAll(NON_EXISTANT_PATH_BASE) == 0);
148}
149
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400150}}} // namespace dhtnet::test::fileutils
Adrien Béraudefe27372023-05-27 18:56:29 -0400151
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400152JAMI_TEST_RUNNER(dhtnet::fileutils::test::FileutilsTest::name());
Adrien Béraudefe27372023-05-27 18:56:29 -0400153