aboutsummaryrefslogtreecommitdiffstats
path: root/rlp.cpp
blob: be098d84df7651d1a3f164fa509cb5f0d62d8cb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    cpp-ethereum is distributed in the hope that it will be useful,
    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
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file rlp.cpp
 * @author Gav Wood <i@gavwood.com>
 * @date 2014
 * RLP test functions.
 */

#include <fstream>
#include <sstream>
#include <libdevcore/Log.h>
#include <libdevcore/RLP.h>
#include <libdevcore/Common.h>
#include <libdevcore/CommonIO.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include "JsonSpiritHeaders.h"
#include "TestHelper.h"

using namespace std;
using namespace dev;
namespace js = json_spirit;

namespace dev
{
    namespace test
    {
        static void buildRLP(js::mValue& _v, RLPStream& _rlp)
        {
            if (_v.type() == js::array_type)
            {
                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);
            }
        }

        static void getRLPTestCases(js::mValue& v)
        {
            string testPath = getTestPath();
            testPath += "/BasicTests";

            string s = asString(contents(testPath + "/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.empty() && 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() ); 
                unsigned 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_SUITE(BasicTests)

BOOST_AUTO_TEST_CASE(rlp_encoding_test)
{
    cnote << "Testing RLP Encoding...";
    js::mValue v;
    dev::test::getRLPTestCases(v);

    for (auto& i: v.get_obj())
    {
        js::mObject& o = i.second.get_obj();
        cnote << i.first;
        dev::test::checkRLPTestCase(o);

        RLPStream s;
        dev::test::buildRLP(o["in"], s);

        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;
    dev::test::getRLPTestCases(v);
    for (auto& i: v.get_obj())
    {
        js::mObject& o = i.second.get_obj();
        cnote << i.first;
        dev::test::checkRLPTestCase(o);
        
        js::mValue& inputData = o["in"];
        bytes payloadToDecode = fromHex(o["out"].get_str()); 

        RLP payload(payloadToDecode); 

        dev::test::checkRLPAgainstJson(inputData, payload);

    }
}

BOOST_AUTO_TEST_SUITE_END()

'deletions'>-15/+22 * ** Fixes bug #576694Matthew Barnes2009-03-254-3/+26 * updated kn.poShankar Prasad2009-03-252-76/+80 * Added two screnshots for SpanishJorge Gonzalez Gonzalez2009-03-253-0/+4 * ** Fix for bug #552583Milan Crha2009-03-252-1/+21 * Updated Spanish translationJorge Gonzalez Gonzalez2009-03-232-21/+25 * Updating Punjabi Translation (pa)Amanpreet Singh Alam2009-03-221-220/+161 * Typo fixed in French translation.Claude Paroz2009-03-222-1/+5 * Fixed wrong translation.Takeshi AIHANA2009-03-212-10/+14 * Added entries for Oriya language Translation updation.Manoj Kumar Giri2009-03-191-0/+4 * Updated Oriya Translation.Manoj Kumar Giri2009-03-191-487/+214 * ** Fix for bug #574680Milan Crha2009-03-192-21/+11 * ** Fix for bug #504767Milan Crha2009-03-197-7/+29 * Updated Spanish translationJorge Gonzalez Gonzalez2009-03-192-13/+21 * Bump the version number.Matthew Barnes2009-03-182-2/+6 * Updated Arabic translationDjihed Afifi2009-03-182-3487/+4178 * Updated Norwegian bokmål translation.Kjartan Maraas2009-03-172-434/+435 * Evolution 2.26.0 release and version bumpEVOLUTION_2_26_0Srinivasa Ragavan2009-03-173-1/+15 * Updated Turkish TranslationBaris Cicek2009-03-172-3583/+4084 * Updated British English translation.Philip Withnall2009-03-172-2533/+2543 * sv.po: Updated Swedish translationDaniel Nylander2009-03-172-4030/+5318 * Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>Alexander Shopov2009-03-162-3444/+3664 * Updated assamese translationsAmitakhya Phukan2009-03-162-9610/+8311 * Updated Gujarati TranslationsAnkitkumar Rameshchandra Patel2009-03-161-163/+93 * Added entry for Gujarati translation update.Ankitkumar Rameshchandra Patel2009-03-161-0/+4 * Added entries for Oriya language Translation updation.Manoj Kumar Giri2009-03-161-0/+11 * Updated Oriya Translation.Manoj Kumar Giri2009-03-161-121/+12 * Updated Oriya Translation.Manoj Kumar Giri2009-03-161-203/+103 * Fix msgfmt errorsDwayne Bailey2009-03-162-5/+9 * updating for Gnome Punjabi Translation by A S AlamAmanpreet Singh Alam2009-03-161-1363/+1134 * Added Afrikaans translation by Friedel Wolff. Fixes bug #575466. Added af.Andre Klapper2009-03-163-0/+23357 * Updated Oriya Translation.Manoj Kumar Giri2009-03-161-884/+448 * sv.po: Updated Swedish translationDaniel Nylander2009-03-162-2/+6 * Updated German translation, fix #574975.Hendrik Richter2009-03-162-166/+303 * Updated Russian translation by Yuriy Penkin.Nickolay V. Shmyrev2009-03-152-1117/+1249 * Updated Czech translation by Jiri Eischmann.Andre Klapper2009-03-152-1029/+330 * Updated Malayalam TranslationAni Peter2009-03-151-0/+4 * Updated Malayalam TranslationAni Peter2009-03-151-195/+280 * maithili addedRajesh Ranjan2009-03-153-0/+23120 * Updated Marathi TranslationsSandeep Shedmake2009-03-152-4326/+4848 * Updated Greek Translation by Jennie PetoumenouKostas Papadimas2009-03-152-291/+449 * Updated Lithuanian translation.Žygimantas Beručka2009-03-152-318/+317 * Added entries for Oriya language Translation updation.Manoj Kumar Giri2009-03-151-0/+4 * Updated Oriya Translation.Manoj Kumar Giri2009-03-151-1832/+2934 * Updated Czech translation by Jiri Eischmann.Andre Klapper2009-03-142-380/+385 * Translation updated by Priit LaesPriit Laes2009-03-142-7118/+1609 * Updated German translation.Andre Klapper2009-03-142-1113/+1028 * Translation updated.Gabor Kelemen2009-03-142-1/+5 * Fixed bug #567745.Claude Paroz2009-03-14