diff options
Diffstat (limited to 'tests/unit_tests/json_serialization.cpp')
-rw-r--r-- | tests/unit_tests/json_serialization.cpp | 47 |
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)); |