LCOV - code coverage report
Current view: top level - src/test - fs_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.2 % 112 110
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 10 10
Branches: 50.0 % 600 300

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : //
       5                 :             : #include <test/util/setup_common.h>
       6                 :             : #include <util/fs.h>
       7                 :             : #include <util/fs_helpers.h>
       8                 :             : 
       9                 :             : #include <boost/test/unit_test.hpp>
      10                 :             : 
      11                 :             : #include <fstream>
      12                 :             : #include <ios>
      13                 :             : #include <string>
      14                 :             : 
      15                 :             : BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
      16                 :             : 
      17   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      18                 :             : {
      19                 :           1 :     std::string u8_str = "fs_tests_₿_🏃";
      20         [ +  - ]:           1 :     std::u8string str8{u8"fs_tests_₿_🏃"};
      21   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
                   +  - ]
      22   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);
             +  -  +  - ]
      23   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);
             +  -  +  - ]
      24   [ +  -  +  -  :           3 :     BOOST_CHECK(fs::path(str8).u8string() == str8);
          +  -  +  -  +  
                      - ]
      25   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);
             +  -  +  - ]
      26   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
                   +  - ]
      27                 :             : #ifndef WIN32
      28                 :             :     // On non-windows systems, verify that arbitrary byte strings containing
      29                 :             :     // invalid UTF-8 can be round tripped successfully with PathToString and
      30                 :             :     // PathFromString. On non-windows systems, paths are just byte strings so
      31                 :             :     // these functions do not do any encoding. On windows, paths are Unicode,
      32                 :             :     // and these functions do encoding and decoding, so the behavior of this
      33                 :             :     // test would be undefined.
      34         [ +  - ]:           1 :     std::string invalid_u8_str = "\xf0";
      35   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(invalid_u8_str.size(), 1);
      36   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(invalid_u8_str)), invalid_u8_str);
                   +  - ]
      37                 :             : #endif
      38                 :           1 : }
      39                 :             : 
      40   [ +  -  +  -  :          10 : BOOST_AUTO_TEST_CASE(fsbridge_stem)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      41                 :             : {
      42                 :           1 :     std::string test_filename = "fs_tests_₿_🏃.dat";
      43         [ +  - ]:           1 :     std::string expected_stem = "fs_tests_₿_🏃";
      44   [ +  -  +  -  :           6 :     BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(test_filename).stem()), expected_stem);
          +  -  +  -  +  
                      - ]
      45                 :           1 : }
      46                 :             : 
      47   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(fsbridge_fstream)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      48                 :             : {
      49                 :           1 :     fs::path tmpfolder = m_args.GetDataDirBase();
      50                 :             :     // tmpfile1 should be the same as tmpfile2
      51   [ +  -  +  -  :           5 :     fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
                   +  - ]
      52   [ +  -  +  -  :           3 :     fs::path tmpfile2 = tmpfolder / fs::path(u8"fs_tests_₿_🏃");
                   +  - ]
      53                 :           1 :     {
      54         [ +  - ]:           1 :         std::ofstream file{tmpfile1};
      55         [ +  - ]:           1 :         file << "bitcoin";
      56                 :           1 :     }
      57                 :           1 :     {
      58         [ +  - ]:           1 :         std::ifstream file{tmpfile2};
      59         [ +  - ]:           1 :         std::string input_buffer;
      60         [ +  - ]:           1 :         file >> input_buffer;
      61   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
      62                 :           1 :     }
      63                 :           1 :     {
      64         [ +  - ]:           1 :         std::ifstream file{tmpfile1, std::ios_base::in | std::ios_base::ate};
      65         [ +  - ]:           1 :         std::string input_buffer;
      66         [ +  - ]:           1 :         file >> input_buffer;
      67   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(input_buffer, "");
      68                 :           1 :     }
      69                 :           1 :     {
      70         [ +  - ]:           1 :         std::ofstream file{tmpfile2, std::ios_base::out | std::ios_base::app};
      71         [ +  - ]:           1 :         file << "tests";
      72                 :           1 :     }
      73                 :           1 :     {
      74         [ +  - ]:           1 :         std::ifstream file{tmpfile1};
      75         [ +  - ]:           1 :         std::string input_buffer;
      76         [ +  - ]:           1 :         file >> input_buffer;
      77   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(input_buffer, "bitcointests");
      78                 :           1 :     }
      79                 :           1 :     {
      80         [ +  - ]:           1 :         std::ofstream file{tmpfile2, std::ios_base::out | std::ios_base::trunc};
      81         [ +  - ]:           1 :         file << "bitcoin";
      82                 :           1 :     }
      83                 :           1 :     {
      84         [ +  - ]:           1 :         std::ifstream file{tmpfile1};
      85         [ +  - ]:           1 :         std::string input_buffer;
      86         [ +  - ]:           1 :         file >> input_buffer;
      87   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
      88                 :           1 :     }
      89                 :           1 :     {
      90                 :             :         // Join an absolute path and a relative path.
      91   [ +  -  +  -  :           2 :         fs::path p = fsbridge::AbsPathJoin(tmpfolder, fs::u8path("fs_tests_₿_🏃"));
                   +  - ]
      92   [ +  -  +  -  :           2 :         BOOST_CHECK(p.is_absolute());
                   +  - ]
      93   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(tmpfile1, p);
      94                 :           0 :     }
      95                 :           1 :     {
      96                 :             :         // Join two absolute paths.
      97         [ +  - ]:           1 :         fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
      98   [ +  -  +  -  :           2 :         BOOST_CHECK(p.is_absolute());
                   +  - ]
      99   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(tmpfile2, p);
     100                 :           0 :     }
     101                 :           1 :     {
     102                 :             :         // Ensure joining with empty paths does not add trailing path components.
     103   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
             +  -  +  - ]
     104   [ +  -  +  -  :           3 :         BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
                   +  - ]
     105                 :             :     }
     106                 :           3 : }
     107                 :             : 
     108   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(rename)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     109                 :             : {
     110                 :           1 :     const fs::path tmpfolder{m_args.GetDataDirBase()};
     111                 :             : 
     112   [ +  -  +  - ]:           2 :     const fs::path path1{tmpfolder / "a"};
     113   [ +  -  +  - ]:           2 :     const fs::path path2{tmpfolder / "b"};
     114                 :             : 
     115         [ +  - ]:           1 :     const std::string path1_contents{"1111"};
     116         [ +  - ]:           1 :     const std::string path2_contents{"2222"};
     117                 :             : 
     118                 :           1 :     {
     119         [ +  - ]:           1 :         std::ofstream file{path1};
     120         [ +  - ]:           1 :         file << path1_contents;
     121                 :           1 :     }
     122                 :             : 
     123                 :           1 :     {
     124         [ +  - ]:           1 :         std::ofstream file{path2};
     125         [ +  - ]:           1 :         file << path2_contents;
     126                 :           1 :     }
     127                 :             : 
     128                 :             :     // Rename path1 -> path2.
     129   [ +  -  +  -  :           4 :     BOOST_CHECK(RenameOver(path1, path2));
          +  -  +  -  +  
                -  +  - ]
     130                 :             : 
     131   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::exists(path1));
             +  -  +  - ]
     132                 :             : 
     133                 :           1 :     {
     134         [ +  - ]:           1 :         std::ifstream file{path2};
     135         [ +  - ]:           1 :         std::string contents;
     136         [ +  - ]:           1 :         file >> contents;
     137   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(contents, path1_contents);
     138                 :           1 :     }
     139         [ +  - ]:           1 :     fs::remove(path2);
     140                 :           4 : }
     141                 :             : 
     142                 :             : #ifndef __MINGW64__ // no symlinks on mingw
     143   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(create_directories)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     144                 :             : {
     145                 :             :     // Test fs::create_directories workaround.
     146                 :           1 :     const fs::path tmpfolder{m_args.GetDataDirBase()};
     147                 :             : 
     148   [ +  -  +  - ]:           2 :     const fs::path dir{tmpfolder / "a"};
     149         [ +  - ]:           1 :     fs::create_directory(dir);
     150   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(dir));
             +  -  +  - ]
     151   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::is_directory(dir));
             +  -  +  - ]
     152   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::create_directories(dir));
             +  -  +  - ]
     153                 :             : 
     154   [ +  -  +  - ]:           2 :     const fs::path symlink{tmpfolder / "b"};
     155         [ +  - ]:           1 :     fs::create_directory_symlink(dir, symlink);
     156   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(symlink));
             +  -  +  - ]
     157   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::is_symlink(symlink));
             +  -  +  - ]
     158   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::is_directory(symlink));
             +  -  +  - ]
     159   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::create_directories(symlink));
             +  -  +  - ]
     160                 :             : 
     161         [ +  - ]:           1 :     fs::remove(symlink);
     162         [ +  - ]:           1 :     fs::remove(dir);
     163                 :           3 : }
     164                 :             : #endif // __MINGW64__
     165                 :             : 
     166                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1