LCOV - code coverage report
Current view: top level - src/test - flatfile_tests.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 66 66 100.0 %
Date: 2026-06-25 07:23:51 Functions: 31 31 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2019-2021 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 <clientversion.h>
       6             : #include <flatfile.h>
       7             : #include <streams.h>
       8             : #include <test/util/setup_common.h>
       9             : #include <util/system.h>
      10             : 
      11             : #include <boost/test/unit_test.hpp>
      12             : 
      13         146 : BOOST_FIXTURE_TEST_SUITE(flatfile_tests, BasicTestingSetup)
      14             : 
      15         149 : BOOST_AUTO_TEST_CASE(flatfile_filename)
      16             : {
      17           1 :     const auto data_dir = m_args.GetDataDirBase();
      18             : 
      19           1 :     FlatFilePos pos(456, 789);
      20             : 
      21           1 :     FlatFileSeq seq1(data_dir, "a", 16 * 1024);
      22           1 :     BOOST_CHECK_EQUAL(seq1.FileName(pos), data_dir / "a00456.dat");
      23             : 
      24           1 :     FlatFileSeq seq2(data_dir / "a", "b", 16 * 1024);
      25           1 :     BOOST_CHECK_EQUAL(seq2.FileName(pos), data_dir / "a" / "b00456.dat");
      26             : 
      27             :     // Check default constructor IsNull
      28           1 :     assert(FlatFilePos{}.IsNull());
      29           1 : }
      30             : 
      31         149 : BOOST_AUTO_TEST_CASE(flatfile_open)
      32             : {
      33           1 :     const auto data_dir = m_args.GetDataDirBase();
      34           1 :     FlatFileSeq seq(data_dir, "a", 16 * 1024);
      35             : 
      36           1 :     std::string line1("A purely peer-to-peer version of electronic cash would allow online "
      37             :                       "payments to be sent directly from one party to another without going "
      38             :                       "through a financial institution.");
      39           1 :     std::string line2("Digital signatures provide part of the solution, but the main benefits are "
      40             :                       "lost if a trusted third party is still required to prevent double-spending.");
      41             : 
      42           1 :     size_t pos1 = 0;
      43           1 :     size_t pos2 = pos1 + GetSerializeSize(line1, CLIENT_VERSION);
      44             : 
      45             :     // Write first line to file.
      46             :     {
      47           1 :         AutoFile file{seq.Open(FlatFilePos(0, pos1))};
      48           1 :         file << LIMITED_STRING(line1, 256);
      49           1 :     }
      50             : 
      51             :     // Attempt to append to file opened in read-only mode.
      52             :     {
      53           1 :         AutoFile file{seq.Open(FlatFilePos(0, pos2), true)};
      54           2 :         BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);
      55           1 :     }
      56             : 
      57             :     // Append second line to file.
      58             :     {
      59           1 :         AutoFile file{seq.Open(FlatFilePos(0, pos2))};
      60           1 :         file << LIMITED_STRING(line2, 256);
      61           1 :     }
      62             : 
      63             :     // Read text from file in read-only mode.
      64             :     {
      65           1 :         std::string text;
      66           1 :         AutoFile file{seq.Open(FlatFilePos(0, pos1), true)};
      67             : 
      68           1 :         file >> LIMITED_STRING(text, 256);
      69           1 :         BOOST_CHECK_EQUAL(text, line1);
      70             : 
      71           1 :         file >> LIMITED_STRING(text, 256);
      72           1 :         BOOST_CHECK_EQUAL(text, line2);
      73           1 :     }
      74             : 
      75             :     // Read text from file with position offset.
      76             :     {
      77           1 :         std::string text;
      78           1 :         AutoFile file{seq.Open(FlatFilePos(0, pos2))};
      79             : 
      80           1 :         file >> LIMITED_STRING(text, 256);
      81           1 :         BOOST_CHECK_EQUAL(text, line2);
      82           1 :     }
      83             : 
      84             :     // Ensure another file in the sequence has no data.
      85             :     {
      86           1 :         std::string text;
      87           1 :         AutoFile file{seq.Open(FlatFilePos(1, pos2))};
      88           2 :         BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);
      89           1 :     }
      90           3 : }
      91             : 
      92         149 : BOOST_AUTO_TEST_CASE(flatfile_allocate)
      93             : {
      94           1 :     const auto data_dir = m_args.GetDataDirBase();
      95           1 :     FlatFileSeq seq(data_dir, "a", 100);
      96             : 
      97             :     bool out_of_space;
      98             : 
      99           1 :     BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 0), 1, out_of_space), 100U);
     100           1 :     BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 0))), 100U);
     101           1 :     BOOST_CHECK(!out_of_space);
     102             : 
     103           1 :     BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 1, out_of_space), 0U);
     104           1 :     BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 100U);
     105           1 :     BOOST_CHECK(!out_of_space);
     106             : 
     107           1 :     BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 2, out_of_space), 101U);
     108           1 :     BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 200U);
     109           1 :     BOOST_CHECK(!out_of_space);
     110           1 : }
     111             : 
     112         149 : BOOST_AUTO_TEST_CASE(flatfile_flush)
     113             : {
     114           1 :     const auto data_dir = m_args.GetDataDirBase();
     115           1 :     FlatFileSeq seq(data_dir, "a", 100);
     116             : 
     117             :     bool out_of_space;
     118           1 :     seq.Allocate(FlatFilePos(0, 0), 1, out_of_space);
     119             : 
     120             :     // Flush without finalize should not truncate file.
     121           1 :     seq.Flush(FlatFilePos(0, 1));
     122           1 :     BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 100U);
     123             : 
     124             :     // Flush with finalize should truncate file.
     125           1 :     seq.Flush(FlatFilePos(0, 1), true);
     126           1 :     BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 1U);
     127           1 : }
     128             : 
     129         146 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.16