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-04-17 21:30:23 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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 "jami.h"
      28             : 
      29             : #include <string>
      30             : #include <iostream>
      31             : #include <cstdlib>
      32             : #include <unistd.h>
      33             : 
      34             : namespace jami { namespace fileutils { namespace test {
      35             : 
      36             : class FileutilsTest : public CppUnit::TestFixture {
      37             : public:
      38           2 :     static std::string name() { return "fileutils"; }
      39             : 
      40             :     void setUp();
      41             :     void tearDown();
      42             : 
      43             : private:
      44             :     void testPath();
      45             :     void testLoadFile();
      46             :     void testIsDirectoryWritable();
      47             :     void testGetCleanPath();
      48             :     void testFullPath();
      49             : 
      50           2 :     CPPUNIT_TEST_SUITE(FileutilsTest);
      51           1 :     CPPUNIT_TEST(testPath);
      52           1 :     CPPUNIT_TEST(testLoadFile);
      53           1 :     CPPUNIT_TEST(testIsDirectoryWritable);
      54           1 :     CPPUNIT_TEST(testGetCleanPath);
      55           1 :     CPPUNIT_TEST(testFullPath);
      56           4 :     CPPUNIT_TEST_SUITE_END();
      57             : 
      58             :     static constexpr auto tmpFileName = "temp_file";
      59             : 
      60             :     std::string TEST_PATH;
      61             :     std::string NON_EXISTANT_PATH_BASE;
      62             :     std::string NON_EXISTANT_PATH;
      63             :     std::string EXISTANT_FILE;
      64             : };
      65             : 
      66             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileutilsTest, FileutilsTest::name());
      67             : 
      68             : void
      69           5 : FileutilsTest::setUp()
      70             : {
      71           5 :     char template_name[] = {"ring_unit_tests_XXXXXX"};
      72             : 
      73             :     // Generate a temporary directory with a file inside
      74           5 :     auto directory = mkdtemp(template_name);
      75           5 :     CPPUNIT_ASSERT(directory);
      76             : 
      77           5 :     TEST_PATH = directory;
      78           5 :     EXISTANT_FILE = TEST_PATH + DIR_SEPARATOR_STR + tmpFileName;
      79           5 :     NON_EXISTANT_PATH_BASE = TEST_PATH + DIR_SEPARATOR_STR + "not_existing_path";
      80           5 :     NON_EXISTANT_PATH = NON_EXISTANT_PATH_BASE + DIR_SEPARATOR_STR + "test";
      81             : 
      82           5 :     auto* fd = fopen(EXISTANT_FILE.c_str(), "w");
      83           5 :     fwrite("RING", 1, 4, fd);
      84           5 :     fclose(fd);
      85           5 : }
      86             : 
      87             : void
      88           5 : FileutilsTest::tearDown()
      89             : {
      90           5 :     unlink(EXISTANT_FILE.c_str());
      91           5 :     rmdir(TEST_PATH.c_str());
      92           5 : }
      93             : 
      94             : void
      95           1 : FileutilsTest::testPath()
      96             : {
      97           1 :     CPPUNIT_ASSERT(isPathRelative("relativePath"));
      98           1 :     CPPUNIT_ASSERT(std::filesystem::is_regular_file(EXISTANT_FILE));
      99           1 :     CPPUNIT_ASSERT(!std::filesystem::is_directory(EXISTANT_FILE));
     100           1 :     CPPUNIT_ASSERT(std::filesystem::is_directory(TEST_PATH));
     101           1 : }
     102             : 
     103             : void
     104           1 : FileutilsTest::testLoadFile()
     105             : {
     106           2 :     auto file = loadFile(EXISTANT_FILE);
     107           1 :     CPPUNIT_ASSERT(file.size() == 4);
     108           1 :     CPPUNIT_ASSERT(file.at(0) == 'R');
     109           1 :     CPPUNIT_ASSERT(file.at(1) == 'I');
     110           1 :     CPPUNIT_ASSERT(file.at(2) == 'N');
     111           1 :     CPPUNIT_ASSERT(file.at(3) == 'G');
     112           1 : }
     113             : 
     114             : void
     115           1 : FileutilsTest::testIsDirectoryWritable()
     116             : {
     117           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::recursive_mkdir(NON_EXISTANT_PATH_BASE));
     118           1 :     CPPUNIT_ASSERT(isDirectoryWritable(NON_EXISTANT_PATH_BASE));
     119           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::removeAll(NON_EXISTANT_PATH_BASE) == 0);
     120             :     // Create directory with permission: read by owner
     121           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::recursive_mkdir(NON_EXISTANT_PATH_BASE, 0400));
     122           1 :     CPPUNIT_ASSERT(!isDirectoryWritable(NON_EXISTANT_PATH_BASE));
     123           1 :     CPPUNIT_ASSERT(dhtnet::fileutils::removeAll(NON_EXISTANT_PATH_BASE) == 0);
     124           1 : }
     125             : 
     126             : void
     127           1 : FileutilsTest::testGetCleanPath()
     128             : {
     129             :     //empty base
     130           1 :     CPPUNIT_ASSERT(getCleanPath("", NON_EXISTANT_PATH).compare(NON_EXISTANT_PATH) == 0);
     131             :     //the base is not contain in the path
     132           1 :     CPPUNIT_ASSERT(getCleanPath(NON_EXISTANT_PATH, NON_EXISTANT_PATH_BASE).compare(NON_EXISTANT_PATH_BASE) == 0);
     133             :     //the method is use correctly
     134           1 :     CPPUNIT_ASSERT(getCleanPath(NON_EXISTANT_PATH_BASE, NON_EXISTANT_PATH).compare("test") == 0);
     135           1 : }
     136             : 
     137             : void
     138           1 : FileutilsTest::testFullPath()
     139             : {
     140             :     //empty base
     141           1 :     CPPUNIT_ASSERT(getFullPath("", "relativePath").compare("relativePath") == 0);
     142             :     //the path is not relative
     143           1 :     CPPUNIT_ASSERT(getFullPath(NON_EXISTANT_PATH_BASE, "/tmp").compare("/tmp") == 0);
     144             :     //the method is use correctly
     145           1 :     CPPUNIT_ASSERT(getFullPath(NON_EXISTANT_PATH_BASE, "test").compare(NON_EXISTANT_PATH) == 0);
     146           1 : }
     147             : 
     148             : }}} // namespace jami::test::fileutils
     149             : 
     150           1 : RING_TEST_RUNNER(jami::fileutils::test::FileutilsTest::name());

Generated by: LCOV version 1.14