Fabcoin Core  0.16.2
P2P Digital Currency
hexPrefix.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <fstream>
19 #include <boost/test/unit_test.hpp>
21 #include <libdevcore/Log.h>
22 #include <libdevcore/CommonIO.h>
23 #include <libdevcore/Base64.h>
24 #include <libdevcore/TrieCommon.h>
26 
27 using namespace std;
28 using namespace dev;
29 using namespace dev::test;
30 namespace js = json_spirit;
31 
32 BOOST_AUTO_TEST_SUITE(Crypto)
33 
35 
36 BOOST_AUTO_TEST_CASE(hexPrefix_test)
37 {
38 
39  string testPath = test::getTestPath();
40  testPath += "/BasicTests";
41 
42  cnote << "Testing Hex-Prefix-Encode...";
43  js::mValue v;
44  string s = contentsString(testPath + "/hexencodetest.json");
45  BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content from 'hexencodetest.json' is empty. Have you cloned the 'tests' repo branch develop?");
46  js::read_string(s, v);
47  for (auto& i: v.get_obj())
48  {
49  js::mObject& o = i.second.get_obj();
50  cnote << i.first;
51  bytes v;
52  for (auto& i: o["seq"].get_array())
53  v.push_back((byte)i.get_int());
54  auto e = hexPrefixEncode(v, o["term"].get_bool());
55  BOOST_REQUIRE( ! o["out"].is_null() );
56  BOOST_CHECK( o["out"].get_str() == toHex(e) );
57  }
58 }
59 
61 {
62  static char const* const s_tests[][2] =
63  {
64  {"", ""},
65  {"f", "Zg=="},
66  {"fo", "Zm8="},
67  {"foo", "Zm9v"},
68  {"foob", "Zm9vYg=="},
69  {"fooba", "Zm9vYmE="},
70  {"foobar", "Zm9vYmFy"},
71  {
72  "So?<p>"
73  "This 4, 5, 6, 7, 8, 9, z, {, |, } tests Base64 encoder. "
74  "Show me: @, A, B, C, D, E, F, G, H, I, J, K, L, M, "
75  "N, O, P, Q, R, S, T, U, V, W, X, Y, Z, [, \\, ], ^, _, `, "
76  "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s.",
77  "U28/PHA+VGhpcyA0LCA1LCA2LCA3LCA4LCA5LCB6LCB7LCB8LCB9IHRlc3RzIEJhc2U2NCBlbmNv"
78  "ZGVyLiBTaG93IG1lOiBALCBBLCBCLCBDLCBELCBFLCBGLCBHLCBILCBJLCBKLCBLLCBMLCBNLCBO"
79  "LCBPLCBQLCBRLCBSLCBTLCBULCBVLCBWLCBXLCBYLCBZLCBaLCBbLCBcLCBdLCBeLCBfLCBgLCBh"
80  "LCBiLCBjLCBkLCBlLCBmLCBnLCBoLCBpLCBqLCBrLCBsLCBtLCBuLCBvLCBwLCBxLCByLCBzLg=="
81  }
82  };
83  static const auto c_numTests = sizeof(s_tests) / sizeof(s_tests[0]);
84 
85  for (size_t i = 0; i < c_numTests; ++i)
86  {
87  auto expectedDecoded = std::string{s_tests[i][0]};
88  auto expectedEncoded = std::string{s_tests[i][1]};
89 
90  auto encoded = toBase64(expectedDecoded);
91  BOOST_CHECK_EQUAL(expectedEncoded, encoded);
92  auto decodedBytes = fromBase64(expectedEncoded);
93  auto decoded = bytesConstRef{decodedBytes.data(), decodedBytes.size()}.toString();
94  BOOST_CHECK_EQUAL(decoded, expectedDecoded);
95  }
96 }
97 
const Object & get_obj() const
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
uint8_t byte
Definition: Common.h:57
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
Definition: TrieCommon.cpp:43
BOOST_AUTO_TEST_CASE(hexPrefix_test)
Definition: hexPrefix.cpp:36
std::hash for asio::adress
Definition: Common.h:323
std::string contentsString(std::string const &_file)
Retrieve and returns the contents of the given file as a std::string.
bool read_string(const String_type &s, Value_type &value)
std::vector< byte > bytes
Definition: Common.h:75
std::string getTestPath()
Definition: Common.cpp:35
std::string toBase64(bytesConstRef _in)
Definition: Base64.cpp:49
mConfig::Object_type mObject
#define cnote
Definition: Log.h:303
_T * data() const
Definition: vector_ref.h:51
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
bytes fromBase64(std::string const &_in)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define e(i)
Definition: sha.cpp:733
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17