LCOV - code coverage report
Current view: top level - src/test - flatfile_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 74 74 100.0 %
Date: 2022-04-21 14:51:19 Functions: 4 4 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a