PIVX Core  5.6.99
P2P Digital Currency
flatfile_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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 "flatfile.h"
6 #include "test/test_pivx.h"
7 
8 #include <boost/test/unit_test.hpp>
9 
11 
12 BOOST_AUTO_TEST_CASE(flatfile_filename)
13 {
14  auto data_dir = SetDataDir("flatfile_test");
15 
16  FlatFilePos pos(456, 789);
17 
18  FlatFileSeq seq1(data_dir, "a", 16 * 1024);
19  BOOST_CHECK_EQUAL(seq1.FileName(pos), data_dir / "a00456.dat");
20 
21  FlatFileSeq seq2(data_dir / "a", "b", 16 * 1024);
22  BOOST_CHECK_EQUAL(seq2.FileName(pos), data_dir / "a" / "b00456.dat");
23 }
24 
25 BOOST_AUTO_TEST_CASE(flatfile_open)
26 {
27  auto data_dir = SetDataDir("flatfile_test");
28  FlatFileSeq seq(data_dir, "a", 16 * 1024);
29 
30  std::string line1("A purely peer-to-peer version of electronic cash would allow online "
31  "payments to be sent directly from one party to another without going "
32  "through a financial institution.");
33  std::string line2("Digital signatures provide part of the solution, but the main benefits are "
34  "lost if a trusted third party is still required to prevent double-spending.");
35 
36  size_t pos1 = 0;
37  size_t pos2 = pos1 + GetSerializeSize(line1, CLIENT_VERSION);
38 
39  // Write first line to file.
40  {
41  CAutoFile file(seq.Open(FlatFilePos(0, pos1)), SER_DISK, CLIENT_VERSION);
42  file << LIMITED_STRING(line1, 256);
43  }
44 
45  // Attempt to append to file opened in read-only mode.
46  {
47  CAutoFile file(seq.Open(FlatFilePos(0, pos2), true), SER_DISK, CLIENT_VERSION);
48  BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);
49  }
50 
51  // Append second line to file.
52  {
53  CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
54  file << LIMITED_STRING(line2, 256);
55  }
56 
57  // Read text from file in read-only mode.
58  {
59  std::string text;
60  CAutoFile file(seq.Open(FlatFilePos(0, pos1), true), SER_DISK, CLIENT_VERSION);
61 
62  file >> LIMITED_STRING(text, 256);
63  BOOST_CHECK_EQUAL(text, line1);
64 
65  file >> LIMITED_STRING(text, 256);
66  BOOST_CHECK_EQUAL(text, line2);
67  }
68 
69  // Read text from file with position offset.
70  {
71  std::string text;
72  CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
73 
74  file >> LIMITED_STRING(text, 256);
75  BOOST_CHECK_EQUAL(text, line2);
76  }
77 
78  // Ensure another file in the sequence has no data.
79  {
80  std::string text;
81  CAutoFile file(seq.Open(FlatFilePos(1, pos2)), SER_DISK, CLIENT_VERSION);
82  BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);
83  }
84 }
85 
86 BOOST_AUTO_TEST_CASE(flatfile_allocate)
87 {
88  auto data_dir = SetDataDir("flatfile_test");
89  FlatFileSeq seq(data_dir, "a", 100);
90 
91  bool out_of_space;
92 
93  BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 0), 1, out_of_space), 100);
94  BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 0))), 100);
95  BOOST_CHECK(!out_of_space);
96 
97  BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 1, out_of_space), 0);
98  BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 100);
99  BOOST_CHECK(!out_of_space);
100 
101  BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 2, out_of_space), 101);
102  BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 200);
103  BOOST_CHECK(!out_of_space);
104 }
105 
106 BOOST_AUTO_TEST_CASE(flatfile_flush)
107 {
108  auto data_dir = SetDataDir("flatfile_test");
109  FlatFileSeq seq(data_dir, "a", 100);
110 
111  bool out_of_space;
112  seq.Allocate(FlatFilePos(0, 0), 1, out_of_space);
113 
114  // Flush without finalize should not truncate file.
115  seq.Flush(FlatFilePos(0, 1));
116  BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 100);
117 
118  // Flush with finalize should truncate file.
119  seq.Flush(FlatFilePos(0, 1), true);
120  BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 1);
121 }
122 
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:47
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:28
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space)
Allocate additional space in a file after the given starting position.
Definition: flatfile.cpp:55
bool Flush(const FlatFilePos &pos, bool finalize=false)
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
Definition: flatfile.cpp:81
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:33
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(flatfile_filename)
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ SER_DISK
Definition: serialize.h:175
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
#define LIMITED_STRING(obj, n)
Definition: serialize.h:515
Basic testing setup.
Definition: test_pivx.h:51