aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests/json_serialization.cpp
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2019-11-07 05:45:06 +0000
committerLee Clagett <code@leeclagett.com>2020-03-05 14:20:56 +0000
commit0f78b06e8c578819f831a513490278a5f70b01af (patch)
tree2f940ff50b1676b92887a4a755b7eb1ba462c04c /tests/unit_tests/json_serialization.cpp
parentMerge pull request #6057 (diff)
downloadmonero-0f78b06e8c578819f831a513490278a5f70b01af.tar.xz
Various improvements to the ZMQ JSON-RPC handling:
- Finding handling function in ZMQ JSON-RPC now uses binary search - Temporary `std::vector`s in JSON output now use `epee::span` to prevent allocations. - Binary -> hex in JSON output no longer allocates temporary buffer - C++ structs -> JSON skips intermediate DOM creation, and instead write directly to an output stream.
Diffstat (limited to 'tests/unit_tests/json_serialization.cpp')
-rw-r--r--tests/unit_tests/json_serialization.cpp47
1 files changed, 27 insertions, 20 deletions
diff --git a/tests/unit_tests/json_serialization.cpp b/tests/unit_tests/json_serialization.cpp
index 53d9f84c7..6f98d854d 100644
--- a/tests/unit_tests/json_serialization.cpp
+++ b/tests/unit_tests/json_serialization.cpp
@@ -3,6 +3,8 @@
#include <boost/range/adaptor/indexed.hpp>
#include <gtest/gtest.h>
#include <rapidjson/document.h>
+#include <rapidjson/stringbuffer.h>
+#include <rapidjson/writer.h>
#include <vector>
#include "crypto/hash.h"
@@ -80,6 +82,27 @@ namespace
return tx;
}
+
+ template<typename T>
+ T test_json(const T& value)
+ {
+ rapidjson::StringBuffer buffer;
+ {
+ rapidjson::Writer<rapidjson::StringBuffer> dest{buffer};
+ cryptonote::json::toJsonValue(dest, value);
+ }
+
+ rapidjson::Document doc;
+ doc.Parse(buffer.GetString());
+ if (doc.HasParseError() || !doc.IsObject())
+ {
+ throw cryptonote::json::PARSE_FAIL();
+ }
+
+ T out{};
+ cryptonote::json::fromJsonValue(doc, out);
+ return out;
+ }
} // anonymous
TEST(JsonSerialization, MinerTransaction)
@@ -91,11 +114,7 @@ TEST(JsonSerialization, MinerTransaction)
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx, tx_hash));
- rapidjson::Document doc;
- cryptonote::json::toJsonValue(doc, miner_tx, doc);
-
- cryptonote::transaction miner_tx_copy;
- cryptonote::json::fromJsonValue(doc, miner_tx_copy);
+ cryptonote::transaction miner_tx_copy = test_json(miner_tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx_copy, tx_copy_hash));
@@ -126,11 +145,7 @@ TEST(JsonSerialization, RegularTransaction)
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
- rapidjson::Document doc;
- cryptonote::json::toJsonValue(doc, tx, doc);
-
- cryptonote::transaction tx_copy;
- cryptonote::json::fromJsonValue(doc, tx_copy);
+ cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));
@@ -161,11 +176,7 @@ TEST(JsonSerialization, RingctTransaction)
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
- rapidjson::Document doc;
- cryptonote::json::toJsonValue(doc, tx, doc);
-
- cryptonote::transaction tx_copy;
- cryptonote::json::fromJsonValue(doc, tx_copy);
+ cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));
@@ -196,11 +207,7 @@ TEST(JsonSerialization, BulletproofTransaction)
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
- rapidjson::Document doc;
- cryptonote::json::toJsonValue(doc, tx, doc);
-
- cryptonote::transaction tx_copy;
- cryptonote::json::fromJsonValue(doc, tx_copy);
+ cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));