Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_cfile.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <fc/io/cfile.hpp>
Include dependency graph for test_cfile.cpp:

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   io
 

Functions

 BOOST_AUTO_TEST_CASE (test_simple)
 
 BOOST_AUTO_TEST_CASE (test_hole_punching)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   io

Definition at line 1 of file test_cfile.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( test_hole_punching )

Definition at line 61 of file test_cfile.cpp.

62 {
64 return;
65
66 fc::temp_file tmpfile;
67 cfile file;
68 file.set_file_path(tmpfile.path());
69 file.open("a+b");
70 file.close();
71 file.open("w+b");
72
73 std::vector<char> a, b, c, d, e, f, g, h, i, j;
74 a.assign(file.filesystem_block_size(), 'A'); //0
75 b.assign(file.filesystem_block_size(), 'B'); //1
76 c.assign(file.filesystem_block_size()/4, 'C'); //2
77 d.assign(file.filesystem_block_size()/4, 'D');
78 e.assign(file.filesystem_block_size()/4, 'E');
79 f.assign(file.filesystem_block_size()/4, 'F');
80 g.assign(file.filesystem_block_size()/2, 'G'); //3
81 h.assign(file.filesystem_block_size()/2, 'H');
82 i.assign(file.filesystem_block_size(), 'I'); //4
83 j.assign(file.filesystem_block_size(), 'J'); //5
84
85 std::vector<char> nom, nom2, nom4;
86 nom.resize(file.filesystem_block_size());
87 nom2.resize(file.filesystem_block_size()/2);
88 nom4.resize(file.filesystem_block_size()/4);
89
90 file.write(a.data(), a.size());
91 file.write(b.data(), b.size());
92 file.write(c.data(), c.size());
93 file.write(d.data(), d.size());
94 file.write(e.data(), e.size());
95 file.write(f.data(), f.size());
96 file.write(g.data(), g.size());
97 file.write(h.data(), h.size());
98
99 //should do nothing
100 file.punch_hole(4, 8);
101 file.seek(0);
102 file.read(nom.data(), nom.size());
103 BOOST_TEST_REQUIRE(nom == a);
104
105 //should also do nothing
106 file.punch_hole(file.filesystem_block_size(), file.filesystem_block_size()+file.filesystem_block_size()/2);
107 file.seek(file.filesystem_block_size());
108 file.read(nom.data(), nom.size());
109 BOOST_TEST_REQUIRE(nom == b);
110
111 //should only wipe out B
112 file.punch_hole(file.filesystem_block_size(), file.filesystem_block_size()*2+file.filesystem_block_size()/2);
113 file.seek(0);
114 file.read(nom.data(), nom.size());
115 BOOST_TEST_REQUIRE(nom == a);
116 file.read(nom.data(), nom.size());
117 BOOST_TEST_REQUIRE(nom != b);
118 file.read(nom4.data(), nom4.size());
119 BOOST_TEST_REQUIRE(nom4 == c);
120
121 //write some stuff at the end after we had punched
122 file.seek_end(0);
123 file.write(i.data(), i.size());
124 file.write(j.data(), j.size());
125
126 //check C is intact
127 file.seek(file.filesystem_block_size()*2);
128 file.read(nom4.data(), nom4.size());
129 BOOST_TEST_REQUIRE(nom4 == c);
130
131 //should wipe out C,D,E,F
132 file.punch_hole(file.filesystem_block_size()*2, file.filesystem_block_size()*3+file.filesystem_block_size()/2);
133 file.seek(file.filesystem_block_size()*2);
134 file.read(nom4.data(), nom4.size());
135 BOOST_TEST_REQUIRE(nom4 != c);
136
137 //so check that G,H,I are still intact
138 file.seek(file.filesystem_block_size()*3);
139 file.read(nom2.data(), nom2.size());
140 BOOST_TEST_REQUIRE(nom2 == g);
141 file.read(nom2.data(), nom2.size());
142 BOOST_TEST_REQUIRE(nom2 == h);
143 file.read(nom.data(), nom.size());
144 BOOST_TEST_REQUIRE(nom == i);
145
146 //check I is intact
147 file.seek(file.filesystem_block_size()*4);
148 file.read(nom.data(), nom.size());
149 BOOST_TEST_REQUIRE(nom == i);
150
151 //should only wipe out I
152 file.punch_hole(file.filesystem_block_size()*4, file.filesystem_block_size()*5);
153 file.seek(file.filesystem_block_size()*4);
154 file.read(nom.data(), nom.size());
155 BOOST_TEST_REQUIRE(nom != i);
156 file.read(nom.data(), nom.size());
157 BOOST_TEST_REQUIRE(nom == j);
158 }
static bool supports_hole_punching()
Definition cfile.hpp:182
void set_file_path(fc::path file_path)
Definition cfile.hpp:37
const fc::path & path() const
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
CK_ULONG d
uint16_t j
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( test_simple )

Definition at line 9 of file test_cfile.cpp.

10 {
11 fc::temp_directory tempdir;
12
13 cfile t;
14 t.set_file_path( tempdir.path() / "test" );
15 t.open( "ab+" );
16 BOOST_CHECK( t.is_open() );
17 BOOST_CHECK( fc::exists( tempdir.path() / "test") );
18
19 t.open( "rb+" );
20 BOOST_CHECK( t.is_open() );
21 t.write( "abc", 3 );
22 BOOST_CHECK_EQUAL( t.tellp(), 3 );
23 std::vector<char> v(3);
24 t.seek( 0 );
25 BOOST_CHECK_EQUAL( t.tellp(), 0 );
26 t.read( &v[0], 3 );
27
28 BOOST_CHECK_EQUAL( v[0], 'a' );
29 BOOST_CHECK_EQUAL( v[1], 'b' );
30 BOOST_CHECK_EQUAL( v[2], 'c' );
31
32 t.seek_end( -2 );
33 BOOST_CHECK_EQUAL( t.tellp(), 1 );
34 t.read( &v[0], 1 );
35 BOOST_CHECK_EQUAL( v[0], 'b' );
36
37 int x = 42, y = 0;
38 t.seek( 1 );
39 t.write( reinterpret_cast<char*>( &x ), sizeof( x ) );
40 t.seek( 1 );
41 t.read( reinterpret_cast<char*>( &y ), sizeof( y ) );
42 BOOST_CHECK_EQUAL( x, y );
43
44 t.close();
45 BOOST_CHECK( !t.is_open() );
46
47 // re-open and read again
48 t.open( "rb+" );
49 BOOST_CHECK( t.is_open() );
50
51 y = 0;
52 t.seek( 1 );
53 t.read( reinterpret_cast<char*>( &y ), sizeof( y ) );
54 BOOST_CHECK_EQUAL( x, y );
55
56 t.close();
58 BOOST_CHECK( !fc::exists( tempdir.path() / "test") );
59 }
void read(char *d, size_t n)
Definition cfile.hpp:114
void close()
Definition cfile.hpp:202
void seek_end(long loc)
Definition cfile.hpp:96
void seek(long loc)
Definition cfile.hpp:87
void open(const char *mode)
Definition cfile.hpp:65
fc::path get_file_path() const
Definition cfile.hpp:41
size_t tellp() const
Definition cfile.hpp:79
bool is_open() const
Definition cfile.hpp:45
void write(const char *d, size_t n)
Definition cfile.hpp:127
bool exists(const path &p)
void remove_all(const path &p)
uint64_t y
Definition sha3.cpp:34
Here is the call graph for this function: