mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Added new test case for decoding the RLP data in the rlptest.json
This commit is contained in:
parent
16c7fe5dad
commit
b8da12f2b8
127
rlp.cpp
127
rlp.cpp
@ -21,10 +21,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include "JsonSpiritHeaders.h"
|
#include "JsonSpiritHeaders.h"
|
||||||
#include <Log.h>
|
#include <Log.h>
|
||||||
#include <RLP.h>
|
#include <RLP.h>
|
||||||
|
#include <Common.h>
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace eth;
|
using namespace eth;
|
||||||
@ -54,29 +57,137 @@ namespace eth
|
|||||||
_rlp.append(s);
|
_rlp.append(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void getRLPTestCases(js::mValue& v)
|
||||||
|
{
|
||||||
|
string s = asString(contents("../../tests/rlptest.json"));
|
||||||
|
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() );
|
||||||
|
uint i;
|
||||||
|
for( i = 0; i < arr.size(); i++ )
|
||||||
|
{
|
||||||
|
RLP item = u[i];
|
||||||
|
checkRLPAgainstJson(arr[i], item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_ERROR("Invalid Javascript object!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(rlp_test)
|
BOOST_AUTO_TEST_CASE(rlp_encoding_test)
|
||||||
{
|
{
|
||||||
cnote << "Testing RLP...";
|
cnote << "Testing RLP Encoding...";
|
||||||
js::mValue v;
|
js::mValue v;
|
||||||
string s = asString(contents("../../tests/rlptest.json"));
|
eth::test::getRLPTestCases(v);
|
||||||
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);
|
|
||||||
for (auto& i: v.get_obj())
|
for (auto& i: v.get_obj())
|
||||||
{
|
{
|
||||||
js::mObject& o = i.second.get_obj();
|
js::mObject& o = i.second.get_obj();
|
||||||
cnote << i.first;
|
cnote << i.first;
|
||||||
|
eth::test::checkRLPTestCase(o);
|
||||||
|
|
||||||
RLPStream s;
|
RLPStream s;
|
||||||
eth::test::buildRLP(o["in"], s);
|
eth::test::buildRLP(o["in"], s);
|
||||||
BOOST_REQUIRE(!o["out"].is_null());
|
|
||||||
BOOST_CHECK(o["out"].get_str() == toHex(s.out()) );
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
eth::test::getRLPTestCases(v);
|
||||||
|
for (auto& i: v.get_obj())
|
||||||
|
{
|
||||||
|
js::mObject& o = i.second.get_obj();
|
||||||
|
cnote << i.first;
|
||||||
|
eth::test::checkRLPTestCase(o);
|
||||||
|
|
||||||
|
js::mValue& inputData = o["in"];
|
||||||
|
bytes payloadToDecode = fromHex(o["out"].get_str());
|
||||||
|
|
||||||
|
RLP payload(payloadToDecode);
|
||||||
|
|
||||||
|
eth::test::checkRLPAgainstJson(inputData, payload);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user