Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_json.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE io_json
2#include <boost/test/included/unit_test.hpp>
3
4#include <fc/io/json.hpp>
6
7using namespace fc;
8
9BOOST_AUTO_TEST_SUITE(json_test_suite)
10
11namespace json_test_util {
12 constexpr size_t exception_limit_size = 250;
13
17
19 if (s >= exception_limit_size) {
20 throw fc::timeout_exception(fc::exception_code::timeout_exception_code, "timeout_exception", "execution timed out" );
21 }
22 };
23
27
29 // no limitation
30 };
31
32 const auto time_except_verf_func = [](const fc::timeout_exception& ex) -> bool {
33 return (static_cast<int>(ex.code_value) == static_cast<int>(fc::exception_code::timeout_exception_code));
34 };
35
36 const auto length_limit_except_verf_func = [](const fc::assert_exception& ex) -> bool {
37 return (static_cast<int>(ex.code_value) == static_cast<int>(fc::exception_code::assert_exception_code));
38 };
39
40 constexpr size_t repeat_char_num = 512;
41 const std::string repeat_chars(repeat_char_num, 'a');
42 const string escape_input_str = "\\b\\f\\n\\r\\t-\\-\\\\-\\x0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f" \
43 "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f-" + repeat_chars;
44
45} // namespace json_test_util
46
47BOOST_AUTO_TEST_CASE(to_string_test)
48{
49 { // to_string( const variant& v, const fc::time_point& deadline, const uint64_t max_len = max_length_limit, output_formatting format = stringify_large_ints_and_doubles);
50 {
52 std::string deadline_exception_at_start_str;
53 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, fc::time_point::min(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit),
54 fc::timeout_exception,
56 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
57 }
58 {
59 constexpr size_t max_len = 10;
61 std::string deadline_exception_at_start_str;
62 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
63 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, max_len),
64 fc::assert_exception,
66 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
67 }
68 {
70 std::string length_exception_in_mid_str;
71 BOOST_CHECK_NO_THROW(length_exception_in_mid_str = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit));
72 BOOST_CHECK_EQUAL(length_exception_in_mid_str, "\"" + json_test_util::repeat_chars + "\"");
73 }
74 }
75 { // to_string( const variant& v, const yield_function_t& yield, output_formatting format = stringify_large_ints_and_doubles);
78 {
79 std::string deadline_exception_at_start_str;
80 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, json_test_util::yield_deadline_exception_at_start),
81 fc::timeout_exception,
83 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
84 }
85 {
86 std::string deadline_exception_in_mid_str;
87 BOOST_CHECK_EXCEPTION(deadline_exception_in_mid_str = json::to_string( v, json_test_util::yield_deadline_exception_in_mid),
88 fc::timeout_exception,
90 BOOST_CHECK_EQUAL(deadline_exception_in_mid_str.empty(), true);
91 }
92 {
93 std::string length_exception_in_mid_str;
94 BOOST_CHECK_EXCEPTION(length_exception_in_mid_str = json::to_string( v, json_test_util::yield_length_exception),
95 fc::assert_exception,
97 BOOST_CHECK_EQUAL(length_exception_in_mid_str.empty(), true);
98 }
99 {
100 std::string no_exception_str;
101 BOOST_CHECK_NO_THROW(no_exception_str = json::to_string( v, json_test_util::yield_no_limitation));
102 BOOST_CHECK_EQUAL(no_exception_str, "\"" + json_test_util::repeat_chars + "\"");
103 }
104 }
105 { // to_string template call
106 const uint16_t id = 1000;
107 const uint64_t len = 3;
108 const std::string id_ret_1 = json::to_string( id, fc::time_point::maximum());
109 BOOST_CHECK_EQUAL(std::to_string(id), id_ret_1);
110
111 // exceed length
112 std::string id_ret_2;
113 BOOST_REQUIRE_THROW(id_ret_2 = json::to_string( id, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, len), fc::assert_exception);
114 BOOST_CHECK_EQUAL(id_ret_2.empty(), true);
115
116 // time_out
117 std::string id_ret_3;
118 BOOST_REQUIRE_THROW(id_ret_3 = json::to_string( id, fc::time_point::now() - fc::milliseconds(1) ), fc::timeout_exception);
119 BOOST_CHECK_EQUAL(id_ret_3.empty(), true);
120 }
121}
122
123BOOST_AUTO_TEST_CASE(to_pretty_string_test)
124{
125 { // to_pretty_string( const variant& v, const fc::time_point& deadline, const uint64_t max_len = max_length_limit, output_formatting format = stringify_large_ints_and_doubles );
126 {
128 std::string deadline_exception_at_start_str;
129 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, fc::time_point::min(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit),
130 fc::timeout_exception,
132 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
133 }
134 {
135 constexpr size_t max_len = 10;
137 std::string deadline_exception_at_start_str;
138 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
139 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, max_len),
140 fc::assert_exception,
142 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
143 }
144 {
146 std::string length_exception_in_mid_str;
147 BOOST_CHECK_NO_THROW(length_exception_in_mid_str = json::to_pretty_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit));
148 BOOST_CHECK_EQUAL(length_exception_in_mid_str, "\"" + json_test_util::repeat_chars + "\"");
149 }
150 }
151 { // to_pretty_string( const variant& v, const yield_function_t& yield, output_formatting format = stringify_large_ints_and_doubles );
154 {
155 std::string deadline_exception_at_start_str;
156 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, json_test_util::yield_deadline_exception_at_start),
157 fc::timeout_exception,
159 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
160 }
161 {
162 std::string deadline_exception_in_mid_str;
163 BOOST_CHECK_EXCEPTION(deadline_exception_in_mid_str = json::to_pretty_string( v, json_test_util::yield_deadline_exception_in_mid),
164 fc::timeout_exception,
166 BOOST_CHECK_EQUAL(deadline_exception_in_mid_str.empty(), true);
167 }
168 {
169 std::string length_exception_in_mid_str;
170 BOOST_CHECK_EXCEPTION(length_exception_in_mid_str = json::to_pretty_string( v, json_test_util::yield_length_exception),
171 fc::assert_exception,
173 BOOST_CHECK_EQUAL(length_exception_in_mid_str.empty(), true);
174 }
175 {
176 std::string no_exception_str;
177 BOOST_CHECK_NO_THROW(no_exception_str = json::to_pretty_string( v, json_test_util::yield_no_limitation));
178 BOOST_CHECK_EQUAL(no_exception_str, "\"" + json_test_util::repeat_chars + "\"");
179 }
180 }
181 { // to_pretty_string template call
182 const uint16_t id = 1000;
183 const uint64_t len = 3;
184 const std::string id_ret_1 = json::to_pretty_string( id, fc::time_point::maximum());
185 BOOST_CHECK_EQUAL(std::to_string(id), id_ret_1);
186
187 // exceed length
188 std::string id_ret_2;
189 BOOST_REQUIRE_THROW(id_ret_2 = json::to_pretty_string( id, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, len), fc::assert_exception);
190 BOOST_CHECK_EQUAL(id_ret_2.empty(), true);
191
192 // time_out
193 std::string id_ret_3;
194 BOOST_REQUIRE_THROW(id_ret_3 = json::to_pretty_string( id, fc::time_point::now() - fc::milliseconds(1) ), fc::timeout_exception);
195 BOOST_CHECK_EQUAL(id_ret_3.empty(), true);
196 }
197}
198
199BOOST_AUTO_TEST_CASE(escape_string_test)
200{
201 std::string escape_out_str;
204 BOOST_CHECK_LT(json_test_util::escape_input_str.size() - json_test_util::repeat_char_num, json_test_util::exception_limit_size); // by using size_different to calculate expected string
205 {
206 // simulate exceed time exception at the beginning of processing
208 fc::timeout_exception,
210 }
211 {
212 // simulate exceed time exception in the middle of processing
214 fc::timeout_exception,
216 }
217 {
218 // length limitation exception in the middle of processing
220 fc::assert_exception,
222 }
223}
224
225BOOST_AUTO_TEST_SUITE_END()
static constexpr time_point min()
Definition time.hpp:47
static time_point now()
Definition time.cpp:14
static constexpr time_point maximum()
Definition time.hpp:46
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
Defines exception's used by fc.
#define FC_CHECK_DEADLINE(DEADLINE,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
constexpr microseconds milliseconds(int64_t s)
Definition time.hpp:33
@ timeout_exception_code
timeout exceptions
Definition exception.hpp:22
@ assert_exception_code
Definition exception.hpp:30
std::string escape_string(const std::string_view &str, const json::yield_function_t &yield, bool escape_control_chars=true)
Definition json.cpp:495
const json::yield_function_t yield_deadline_exception_in_mid
Definition test_json.cpp:18
const json::yield_function_t yield_deadline_exception_at_start
Definition test_json.cpp:14
const json::yield_function_t yield_no_limitation
Definition test_json.cpp:28
const json::yield_function_t yield_length_exception
Definition test_json.cpp:24
const auto length_limit_except_verf_func
Definition test_json.cpp:36
constexpr size_t exception_limit_size
Definition test_json.cpp:12
const auto time_except_verf_func
Definition test_json.cpp:32
const string escape_input_str
Definition test_json.cpp:42
constexpr size_t repeat_char_num
Definition test_json.cpp:40
const std::string repeat_chars(repeat_char_num, 'a')
unsigned short uint16_t
Definition stdint.h:125
unsigned __int64 uint64_t
Definition stdint.h:136
BOOST_AUTO_TEST_CASE(to_string_test)
Definition test_json.cpp:47
char * s
size_t len