LCOV - code coverage report
Current view: top level - test/unitTest/fileutils - testFileutils.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 57 57 100.0 %
Date: 2024-11-15 09:04:49 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       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
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include <cppunit/TestAssert.h>
      19             : #include <cppunit/TestFixture.h>
      20             : #include <cppunit/extensions/HelperMacros.h>
      21             : 
      22             : #include "../../test_runner.h"
      23             : #include "fileutils.h"
      24             : 
      25             : #include "jami.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           2 :     static std::string name() { return "fileutils"; }
      37             : 
      38             :     void setUp();
      39             :     void tearDown();
      40             : 
      41             : private:
      42             :     void testPath();
      43             :     void testLoadFile();
      44             :     void testIsDirectoryWritable();
      45             :     void testGetCleanPath();
      46             :     void testFullPath();
      47             : 
      48           2 :     CPPUNIT_TEST_SUITE(FileutilsTest);
      49           1 :     CPPUNIT_TEST(testPath);
      50           1 :     CPPUNIT_TEST(testLoadFile);
      51           1 :     CPPUNIT_TEST(testIsDirectoryWritable);
      52           1 :     CPPUNIT_TEST(testGetCleanPath);
      53           1 :     CPPUNIT_TEST(testFullPath);
      54           4 :     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           5 : FileutilsTest::setUp()
      68             : {
      69           5 :     char template_name[] = {"ring_unit_tests_XXXXXX"};
      70             : 
      71             :     // Generate a temporary directory with a file inside
      72           5 :     auto directory = mkdtemp(template_name);
      73           5 :     CPPUNIT_ASSERT(directory);
      74             : 
      75           5 :     TEST_PATH = directory;
      76           5 :     EXISTANT_FILE = TEST_PATH + DIR_SEPARATOR_STR + tmpFileName;
      77           5 :     NON_EXISTANT_PATH_BASE = TEST_PATH + DIR_SEPARATOR_STR + "not_existing_path";
      78           5 :     NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE + DIR_SEPARATOR_STR + "test";
      79             : 
      80           5 :     auto* fd = fopen(EXISTANT_FILE.c_str(), "w");
      81           5 :     fwrite("RING", 1, 4, fd);
      82           5 :     fclose(fd);
      83           5 : }
      84             : 
      85             : void
      86           5 : FileutilsTest::tearDown()
      87             : {
      88           5 :     unlink(EXISTANT_FILE.c_str());
      89           5 :     rmdir(TEST_PATH.c_str());
      90           5 : }
      91             : 
      92             : void
      93           1 : FileutilsTest::testPath()
      94             : {
      95           1 :     CPPUNIT_ASSERT(isPathRelative("relativePath"));
      96           1 :     CPPUNIT_ASSERT(std::filesystem::is_regular_file(EXISTANT_FILE));
      97           1 :     CPPUNIT_ASSERT(!std::filesystem::is_directory(EXISTANT_FILE));
      98           1 :     CPPUNIT_ASSERT(std::filesystem::is_directory(TEST_PATH));
      99           1 : }
     100             : 
     101             : void
     102           1 : FileutilsTest::testLoadFile()
     103             : {
     104           2 :     auto file = loadFile(EXISTANT_FILE);
     105           1 :     CPPUNIT_ASSERT(file.size() == 4);
     106           1 :     CPPUNIT_ASSERT(file.at(0) == 'R');
     107           1 :     CPPUNIT_ASSERT(file.at(1) == 'I');
     108           1 :     CPPUNIT_ASSERT(file.at(2) == 'N');
     109           1 :     CPPUNIT_ASSERT(file.at(3) == 'G');
     110           1 : }
     111             : 
     112             : void
     113           1 : FileutilsTest::testIsDirectoryWritable()
     114             : {
     115           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::recursive_mkdir(NON_EXISTANT_PATH_BASE));
     116           1 :     CPPUNIT_ASSERT(isDirectoryWritable(NON_EXISTANT_PATH_BASE));
     117           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::removeAll(NON_EXISTANT_PATH_BASE) == 0);
     118             :     // Create directory with permission: read by owner
     119           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::recursive_mkdir(NON_EXISTANT_PATH_BASE, 0400));
     120           1 :     CPPUNIT_ASSERT(!isDirectoryWritable(NON_EXISTANT_PATH_BASE));
     121           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::removeAll(NON_EXISTANT_PATH_BASE) == 0);
     122           1 : }
     123             : 
     124             : void
     125           1 : FileutilsTest::testGetCleanPath()
     126             : {
     127             :     //empty base
     128           1 :     CPPUNIT_ASSERT(getCleanPath("", NON_EXISTANT_PATH).compare(NON_EXISTANT_PATH) == 0);
     129             :     //the base is not contain in the path
     130           1 :     CPPUNIT_ASSERT(getCleanPath(NON_EXISTANT_PATH, NON_EXISTANT_PATH_BASE).compare(NON_EXISTANT_PATH_BASE) == 0);
     131             :     //the method is use correctly
     132           1 :     CPPUNIT_ASSERT(getCleanPath(NON_EXISTANT_PATH_BASE, NON_EXISTANT_PATH).compare("test") == 0);
     133           1 : }
     134             : 
     135             : void
     136           1 : FileutilsTest::testFullPath()
     137             : {
     138             :     //empty base
     139           1 :     CPPUNIT_ASSERT(getFullPath("", "relativePath").compare("relativePath") == 0);
     140             :     //the path is not relative
     141           1 :     CPPUNIT_ASSERT(getFullPath(NON_EXISTANT_PATH_BASE, "/tmp").compare("/tmp") == 0);
     142             :     //the method is use correctly
     143           1 :     CPPUNIT_ASSERT(getFullPath(NON_EXISTANT_PATH_BASE, "test").compare(NON_EXISTANT_PATH) == 0);
     144           1 : }
     145             : 
     146             : }}} // namespace jami::test::fileutils
     147             : 
     148           1 : RING_TEST_RUNNER(jami::fileutils::test::FileutilsTest::name());

Generated by: LCOV version 1.14