2014-01-19 14:42:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2014-02-06 18:53:46 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2014-01-19 14:42:02 +00:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2014-02-16 10:20:55 +00:00
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
2014-01-19 14:42:02 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2014-02-16 10:41:15 +00:00
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-19 14:42:02 +00:00
|
|
|
*/
|
|
|
|
/** @file rlp.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
* RLP test functions.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:55:30 +00:00
|
|
|
#include <fstream>
|
2014-04-19 23:27:24 +00:00
|
|
|
#include <sstream>
|
2014-09-05 16:24:29 +00:00
|
|
|
#include <libdevcore/Log.h>
|
|
|
|
#include <libdevcore/RLP.h>
|
|
|
|
#include <libdevcore/Common.h>
|
2014-10-07 21:07:56 +00:00
|
|
|
#include <libdevcore/CommonIO.h>
|
2014-04-19 16:30:44 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-04-19 23:27:24 +00:00
|
|
|
#include <algorithm>
|
2014-09-30 17:25:10 +00:00
|
|
|
#include "JsonSpiritHeaders.h"
|
2014-04-19 16:30:44 +00:00
|
|
|
|
2014-01-19 14:42:02 +00:00
|
|
|
using namespace std;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev;
|
2014-02-28 12:55:30 +00:00
|
|
|
namespace js = json_spirit;
|
2014-01-19 14:42:02 +00:00
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
namespace dev
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 16:30:44 +00:00
|
|
|
namespace test
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 16:30:44 +00:00
|
|
|
static void buildRLP(js::mValue& _v, RLPStream& _rlp)
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 16:30:44 +00:00
|
|
|
if (_v.type() == js::array_type)
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 16:30:44 +00:00
|
|
|
RLPStream s;
|
|
|
|
for (auto& i: _v.get_array())
|
|
|
|
buildRLP(i, s);
|
|
|
|
_rlp.appendList(s.out());
|
|
|
|
}
|
|
|
|
else if (_v.type() == js::int_type)
|
|
|
|
_rlp.append(_v.get_uint64());
|
|
|
|
else if (_v.type() == js::str_type)
|
|
|
|
{
|
|
|
|
auto s = _v.get_str();
|
|
|
|
if (s.size() && s[0] == '#')
|
|
|
|
_rlp.append(bigint(s.substr(1)));
|
|
|
|
else
|
|
|
|
_rlp.append(s);
|
2014-02-28 12:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 23:27:24 +00:00
|
|
|
|
|
|
|
static void getRLPTestCases(js::mValue& v)
|
|
|
|
{
|
2014-05-28 09:52:42 +00:00
|
|
|
string s = asString(contents("../../../tests/rlptest.json"));
|
2014-04-19 23:27:24 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE( s.length() > 0,
|
|
|
|
"Contents of 'rlptest.json' is empty. Have you cloned the 'tests' repo branch develop?");
|
|
|
|
js::read_string(s, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkRLPTestCase(js::mObject& o)
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE( o.count("in") > 0 );
|
|
|
|
BOOST_REQUIRE( o.count("out") > 0 );
|
|
|
|
BOOST_REQUIRE(!o["out"].is_null());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkRLPAgainstJson(js::mValue& v, RLP& u)
|
|
|
|
{
|
|
|
|
if ( v.type() == js::str_type )
|
|
|
|
{
|
|
|
|
const std::string& expectedText = v.get_str();
|
|
|
|
if ( expectedText.front() == '#' )
|
|
|
|
{
|
|
|
|
// Deal with bigint instead of a raw string
|
|
|
|
std::string bigIntStr = expectedText.substr(1,expectedText.length()-1);
|
|
|
|
std::stringstream bintStream(bigIntStr);
|
|
|
|
bigint val;
|
|
|
|
bintStream >> val;
|
|
|
|
BOOST_CHECK( !u.isList() );
|
|
|
|
BOOST_CHECK( !u.isNull() );
|
|
|
|
BOOST_CHECK( u ); // operator bool()
|
|
|
|
BOOST_CHECK(u == val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_CHECK( !u.isList() );
|
|
|
|
BOOST_CHECK( !u.isNull() );
|
|
|
|
BOOST_CHECK( u.isData() );
|
|
|
|
BOOST_CHECK( u );
|
|
|
|
BOOST_CHECK( u.size() == expectedText.length() );
|
|
|
|
BOOST_CHECK(u == expectedText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( v.type() == js::int_type )
|
|
|
|
{
|
|
|
|
const int expectedValue = v.get_int();
|
|
|
|
BOOST_CHECK( u.isInt() );
|
|
|
|
BOOST_CHECK( !u.isList() );
|
|
|
|
BOOST_CHECK( !u.isNull() );
|
|
|
|
BOOST_CHECK( u ); // operator bool()
|
|
|
|
BOOST_CHECK(u == expectedValue);
|
|
|
|
}
|
|
|
|
else if ( v.type() == js::array_type )
|
|
|
|
{
|
|
|
|
BOOST_CHECK( u.isList() );
|
|
|
|
BOOST_CHECK( !u.isInt() );
|
|
|
|
BOOST_CHECK( !u.isData() );
|
|
|
|
js::mArray& arr = v.get_array();
|
|
|
|
BOOST_CHECK( u.itemCount() == arr.size() );
|
2014-09-05 15:09:58 +00:00
|
|
|
unsigned i;
|
2014-04-19 23:27:24 +00:00
|
|
|
for( i = 0; i < arr.size(); i++ )
|
|
|
|
{
|
|
|
|
RLP item = u[i];
|
|
|
|
checkRLPAgainstJson(arr[i], item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Invalid Javascript object!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-28 12:55:30 +00:00
|
|
|
}
|
2014-04-19 16:30:44 +00:00
|
|
|
}
|
2014-01-19 14:42:02 +00:00
|
|
|
|
|
|
|
|
2014-04-19 23:27:24 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(rlp_encoding_test)
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 23:27:24 +00:00
|
|
|
cnote << "Testing RLP Encoding...";
|
2014-04-19 16:30:44 +00:00
|
|
|
js::mValue v;
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::getRLPTestCases(v);
|
2014-04-19 23:27:24 +00:00
|
|
|
|
2014-04-19 16:30:44 +00:00
|
|
|
for (auto& i: v.get_obj())
|
|
|
|
{
|
|
|
|
js::mObject& o = i.second.get_obj();
|
|
|
|
cnote << i.first;
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::checkRLPTestCase(o);
|
2014-04-19 23:27:24 +00:00
|
|
|
|
2014-04-19 16:30:44 +00:00
|
|
|
RLPStream s;
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::buildRLP(o["in"], s);
|
2014-04-19 23:27:24 +00:00
|
|
|
|
|
|
|
std::string expectedText(o["out"].get_str());
|
|
|
|
std::transform(expectedText.begin(), expectedText.end(), expectedText.begin(), ::tolower );
|
|
|
|
|
|
|
|
const std::string& computedText = toHex(s.out());
|
|
|
|
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << "Encoding Failed: expected: " << expectedText << std::endl;
|
|
|
|
msg << " But Computed: " << computedText;
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(
|
|
|
|
expectedText == computedText,
|
|
|
|
msg.str()
|
|
|
|
);
|
2014-04-19 16:30:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 12:55:30 +00:00
|
|
|
}
|
2014-04-19 16:30:44 +00:00
|
|
|
|
2014-04-19 23:27:24 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(rlp_decoding_test)
|
|
|
|
{
|
|
|
|
cnote << "Testing RLP decoding...";
|
|
|
|
// Uses the same test cases as encoding but in reverse.
|
|
|
|
// We read into the string of hex values, convert to bytes,
|
|
|
|
// and then compare the output structure to the json of the
|
|
|
|
// input object.
|
|
|
|
js::mValue v;
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::getRLPTestCases(v);
|
2014-04-19 23:27:24 +00:00
|
|
|
for (auto& i: v.get_obj())
|
|
|
|
{
|
|
|
|
js::mObject& o = i.second.get_obj();
|
|
|
|
cnote << i.first;
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::checkRLPTestCase(o);
|
2014-04-19 23:27:24 +00:00
|
|
|
|
|
|
|
js::mValue& inputData = o["in"];
|
|
|
|
bytes payloadToDecode = fromHex(o["out"].get_str());
|
|
|
|
|
|
|
|
RLP payload(payloadToDecode);
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
dev::test::checkRLPAgainstJson(inputData, payload);
|
2014-04-19 16:30:44 +00:00
|
|
|
|
2014-04-19 23:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 16:30:44 +00:00
|
|
|
|
|
|
|
|