Browse Source

Merge pull request #2768 from debris/icap_tests

icap encoding and decoding tests
cl-refactor
Gav Wood 10 years ago
parent
commit
4ad74e18c8
  1. 5
      libdevcore/Base64.h
  2. 54
      test/libdevcore/Base36.cpp
  3. 80
      test/libethcore/icap.cpp

5
libdevcore/Base64.h

@ -44,7 +44,10 @@ template <size_t N> inline std::string toBase36(FixedHash<N> const& _h)
typename FixedHash<N>::Arith a = _h;
std::string ret;
for (; a > 0; a /= 36)
ret = c_alphabet[(unsigned)a % 36] + ret;
{
unsigned r = (unsigned)(a - a / 36 * 36); // boost's % is broken
ret = c_alphabet[r] + ret;
}
return ret;
}

54
test/libdevcore/Base36.cpp

@ -0,0 +1,54 @@
/*
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 Base36.cpp
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#include <boost/test/unit_test.hpp>
#include <libdevcore/Base64.h>
#include <libethcore/ICAP.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
BOOST_AUTO_TEST_SUITE(Base36Tests)
BOOST_AUTO_TEST_CASE(basicEncoding)
{
FixedHash<2> value("0x0048");
string encoded = toBase36<2>(value);
BOOST_CHECK_EQUAL(encoded, "20");
}
BOOST_AUTO_TEST_CASE(basicEncoding2)
{
FixedHash<2> value("0x0072");
string encoded = toBase36<2>(value);
BOOST_CHECK_EQUAL(encoded, "36");
}
BOOST_AUTO_TEST_CASE(basicEncoding3)
{
FixedHash<2> value("0xffff");
string encoded = toBase36<2>(value);
BOOST_CHECK_EQUAL(encoded, "1EKF");
}
BOOST_AUTO_TEST_SUITE_END()

80
test/libethcore/icap.cpp

@ -0,0 +1,80 @@
/*
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 icap.cpp
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#include <boost/test/unit_test.hpp>
#include <libethcore/ICAP.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
BOOST_AUTO_TEST_SUITE(IcapTests)
BOOST_AUTO_TEST_CASE(addressEncoding)
{
Address address("0x52dc504a422f0e2a9e7632a34a50f1a82f8224c7");
ICAP icap(address);
BOOST_CHECK_EQUAL(icap.encoded(), "XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O7");
}
BOOST_AUTO_TEST_CASE(addressEncodingRandomString)
{
Address address("0x11c5496aee77c1ba1f0854206a26dda82a81d6d8");
ICAP icap(address);
BOOST_CHECK_EQUAL(icap.encoded(), "XE1222Q908LN1QBBU6XUQSO1OHWJIOS46OO");
}
BOOST_AUTO_TEST_CASE(addressEncodingWithZeroPrefix)
{
Address address("0x00c5496aee77c1ba1f0854206a26dda82a81d6d8");
ICAP icap(address);
BOOST_CHECK_EQUAL(icap.encoded(), "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
}
BOOST_AUTO_TEST_CASE(addressDecoding)
{
Address address("0x52dc504a422f0e2a9e7632a34a50f1a82f8224c7");
ICAP icap = ICAP::decoded("XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O7");
BOOST_CHECK_EQUAL(icap.direct(), address);
}
BOOST_AUTO_TEST_CASE(addressDecodingRandomString)
{
Address address("0x11c5496aee77c1ba1f0854206a26dda82a81d6d8");
ICAP icap = ICAP::decoded("XE1222Q908LN1QBBU6XUQSO1OHWJIOS46OO");
BOOST_CHECK_EQUAL(icap.direct(), address);
}
BOOST_AUTO_TEST_CASE(addressDecodingWithZeroPrefix)
{
Address address("0x00c5496aee77c1ba1f0854206a26dda82a81d6d8");
ICAP icap = ICAP::decoded("XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
BOOST_CHECK_EQUAL(icap.direct(), address);
}
BOOST_AUTO_TEST_CASE(addressDecodingAndEncoding)
{
std::string encoded = "XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O7";
ICAP icap = ICAP::decoded(encoded);
BOOST_CHECK_EQUAL(icap.encoded(), encoded);
}
BOOST_AUTO_TEST_SUITE_END()
Loading…
Cancel
Save