This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
RLP.cpp
89 lines (67 loc) · 1.94 KB
/
RLP.cpp
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
/* Aleth: Ethereum C++ client, tools and libraries.
* Copyright 2018 Aleth Autors.
* Licensed under the GNU General Public License, Version 3. See the LICENSE file.
*/
#include <libdevcore/RLP.h>
#include <gtest/gtest.h>
using namespace dev;
using namespace std;
TEST(RLP, emptyArrayList)
{
try
{
bytes payloadToDecode = fromHex("80");
RLP payload(payloadToDecode);
ostringstream() << payload;
payloadToDecode = fromHex("с0");
RLP payload2(payloadToDecode);
ostringstream() << payload2;
}
catch (std::exception const& _e)
{
ADD_FAILURE() << "Exception: " << _e.what();
}
}
TEST(RLP, actualSize)
{
EXPECT_EQ(RLP{}.actualSize(), 0);
bytes b{0x79};
EXPECT_EQ(RLP{b}.actualSize(), 1);
b = {0x80};
EXPECT_EQ(RLP{b}.actualSize(), 1);
b = {0x81, 0xff};
EXPECT_EQ(RLP{b}.actualSize(), 2);
b = {0xc0};
EXPECT_EQ(RLP{b}.actualSize(), 1);
b = {0xc1, 0x00};
EXPECT_EQ(RLP{b}.actualSize(), 2);
}
TEST(RLP, bignumSerialization)
{
// Tests that a bignum can be serialized and deserialized with the RLP classes.
dev::u256 bignum("10000000000000000000000");
dev::RLPStream rlpStm{};
rlpStm << bignum;
auto buffer = rlpStm.out();
dev::RLP rlp{buffer};
dev::u256 bignumPost = rlp.toInt<dev::u256>();
EXPECT_EQ(bignum, bignumPost) << "The post-processed bignum does not match the original.";
}
TEST(RLP, toArray)
{
auto const data = rlpList(1, 2, 3);
RLP rlp{data};
array<uint8_t, 3> const expected = {{1, 2, 3}};
EXPECT_EQ((rlp.toArray<uint8_t, 3>()), expected);
}
TEST(RLP, toArrayFail)
{
// non-list RLP data
auto const data = rlp(0);
RLP rlp{data};
// toArray doesn't throw by default
array<uint8_t, 3> const expected = {};
EXPECT_EQ((rlp.toArray<uint8_t, 3>()), expected);
// toArray throws in strict mode
EXPECT_THROW((rlp.toArray<uint8_t, 3>(RLP::VeryStrict)), BadCast);
}