Electroneum
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"
33 
34 namespace epee
35 {
36  namespace tests
37  {
38 
39 
40  struct test_struct
41  {
42 
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 
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 
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 
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 
const char * res
Definition: hmac_keccak.cpp:41
test_struct get_test_struct()
Definition: storage_tests.h:76
hsection open_section(const std::string &section_name, hsection hparent_section, bool create_if_notexist=false)
bool test_storages(const std::string &tests_folder)
Definition: storage_tests.h:95
bool get_value(const std::string &value_name, t_value &val, hsection hparent_section)
::std::string string
Definition: gtest-port.h:1097
std::list< int > m_list_of_int
Definition: storage_tests.h:47
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
std::list< std::string > m_list_of_str
Definition: storage_tests.h:46
t3
Definition: pow225521.h:103
unsigned __int64 uint64_t
Definition: stdint.h:136
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
t2
Definition: pow22523.h:103
std::list< test_struct > m_list_of_self
Definition: storage_tests.h:48