Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
storage_tests.h
Go to the documentation of this file.
1
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above copyright
9
// notice, this list of conditions and the following disclaimer in the
10
// documentation and/or other materials provided with the distribution.
11
// * Neither the name of the Andrey N. Sabelnikov nor the
12
// names of its contributors may be used to endorse or promote products
13
// derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
//
26
27
28
29
#pragma once
30
31
#include "storages/serializeble_struct_helper.h"
32
#include "
storages/portable_storage.h
"
33
34
namespace
epee
35
{
36
namespace
tests
37
{
38
39
40
struct
test_struct
41
{
42
43
std::string
m_str
;
44
unsigned
int
m_uint
;
45
bool
m_bool
;
46
std::list<std::string>
m_list_of_str
;
47
std::list<int>
m_list_of_int
;
48
std::list<test_struct>
m_list_of_self
;
49
50
51
BEGIN_NAMED_SERIALIZE_MAP()
52
SERIALIZE_STL_ANSI_STRING(
m_str
)
53
SERIALIZE_POD(
m_uint
)
54
SERIALIZE_POD(
m_bool
)
55
SERIALIZE_STL_CONTAINER_ANSII_STRING(
m_list_of_str
)
56
SERIALIZE_STL_CONTAINER_POD(
m_list_of_int
)
57
SERIALIZE_STL_CONTAINER_T(
m_list_of_self
)
58
END_NAMED_SERIALIZE_MAP()
59
60
};
61
62
63
bool
operator == (const
test_struct
&
a
, const
test_struct
& b)
64
{
65
if
( b.m_str !=
a
.m_str
66
|| b.m_uint !=
a
.m_uint
67
|| b.m_bool !=
a
.m_bool
68
|| b.m_list_of_str !=
a
.m_list_of_str
69
|| b.m_list_of_int !=
a
.m_list_of_int
70
|| b.m_list_of_self !=
a
.m_list_of_self
71
)
72
return
false
;
73
return
true
;
74
}
75
76
inline
test_struct
get_test_struct
()
77
{
78
test_struct
t = boost::value_initialized<test_struct>();
79
t.
m_bool
=
true
;
80
t.
m_str
=
"ackamdc'kmecemcececmacmecmcm[aicm[oeicm[oeicm[qaicm[qoe"
;
81
t.
m_uint
= 233242;
82
for
(
int
i = 0; i!=500; i++)
83
t.
m_list_of_int
.push_back(i);
84
85
for
(
int
i = 0; i!=500; i++)
86
t.
m_list_of_str
.push_back(
"ssccd"
);
87
88
for
(
int
i = 0; i!=5; i++)
89
{
90
t.
m_list_of_self
.push_back(t);
91
}
92
return
t;
93
}
94
95
bool
test_storages
(
const
std::string& tests_folder)
96
{
97
98
epee::serialization::portable_storage
ps;
99
auto
s = ps.
open_section
(
"zzz"
,
nullptr
);
100
uint64_t
i = 0;
101
ps.
get_value
(
"afdsdf"
, i, s);
102
103
104
LOG_PRINT_L0
(
"Generating test struct..."
);
105
boost::filesystem::path storage_folder = tests_folder;
106
storage_folder /=
"storages"
;
107
108
109
test_struct
t =
get_test_struct
();
110
111
LOG_PRINT_L0
(
"Loading test struct from storage..."
);
112
test_struct
t2
;
113
bool
res
= epee::StorageNamed::load_struct_from_storage_file(
t2
, (storage_folder /+
"valid_storage.bin"
).
string
());
114
CHECK_AND_ASSERT_MES
(
res
,
false
,
"Failed to load valid_storage.bin"
);
115
116
LOG_PRINT_L0
(
"Comparing generated and loaded test struct..."
);
117
if
(!(t ==
t2
))
118
return
false
;
119
120
LOG_PRINT_L0
(
"Loading broken archive 1..."
);
121
test_struct
t3
;
122
res
= epee::StorageNamed::load_struct_from_storage_file(
t3
, (storage_folder /+
"invalid_storage_1.bin"
).
string
());
123
CHECK_AND_ASSERT_MES
(!
res
,
false
,
"invalid_storage_1.bin loaded, but should not "
);
124
125
126
LOG_PRINT_L0
(
"Loading broken archive 2..."
);
127
res
= epee::StorageNamed::load_struct_from_storage_file(
t3
, (storage_folder /+
"invalid_storage_2.bin"
).
string
());
128
CHECK_AND_ASSERT_MES
(!
res
,
false
,
"invalid_storage_2.bin loaded, but should not "
);
129
130
LOG_PRINT_L0
(
"Loading broken archive 3..."
);
131
res
= epee::StorageNamed::load_struct_from_storage_file(
t3
, (storage_folder /+
"invalid_storage_3.bin"
).
string
());
132
CHECK_AND_ASSERT_MES
(!
res
,
false
,
"invalid_storage_3.bin loaded, but should not "
);
133
134
LOG_PRINT_L0
(
"Loading broken archive 4..."
);
135
res
= epee::StorageNamed::load_struct_from_storage_file(
t3
, (storage_folder /+
"invalid_storage_4.bin"
).
string
());
136
CHECK_AND_ASSERT_MES
(!
res
,
false
,
"invalid_storage_3.bin loaded, but should not "
);
137
138
return
true
;
139
}
140
}
141
}
142
epee::serialization::portable_storage
Definition
portable_storage.h:51
epee::serialization::portable_storage::get_value
bool get_value(const std::string &value_name, t_value &val, hsection hparent_section)
Definition
portable_storage.h:216
epee::serialization::portable_storage::open_section
hsection open_section(const std::string §ion_name, hsection hparent_section, bool create_if_notexist=false)
Definition
portable_storage.h:182
res
const char * res
Definition
hmac_keccak.cpp:41
CHECK_AND_ASSERT_MES
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition
misc_log_ex.h:181
LOG_PRINT_L0
#define LOG_PRINT_L0(x)
Definition
misc_log_ex.h:99
epee::tests
Definition
portable_storages_test.h:41
epee::tests::test_storages
bool test_storages(const std::string &tests_folder)
Definition
storage_tests.h:95
epee::tests::get_test_struct
test_struct get_test_struct()
Definition
storage_tests.h:76
epee
Definition
ado_db_helper.h:67
a
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition
pointer.h:1124
portable_storage.h
t2
t2
Definition
pow22523.h:103
t3
t3
Definition
pow225521.h:103
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
epee::tests::test_struct
Definition
storage_tests.h:41
epee::tests::test_struct::m_bool
bool m_bool
Definition
storage_tests.h:45
epee::tests::test_struct::m_uint
unsigned int m_uint
Definition
storage_tests.h:44
epee::tests::test_struct::m_list_of_str
std::list< std::string > m_list_of_str
Definition
storage_tests.h:46
epee::tests::test_struct::m_list_of_int
std::list< int > m_list_of_int
Definition
storage_tests.h:47
epee::tests::test_struct::m_list_of_self
std::list< test_struct > m_list_of_self
Definition
storage_tests.h:48
epee::tests::test_struct::m_str
std::string m_str
Definition
storage_tests.h:43
contrib
epee
tests
src
storages
storage_tests.h
Generated on
for Electroneum by
1.17.0