Added tests for base_n encodings, fixed base32 + base36
This commit is contained in:
parent
19b6de0e24
commit
0e681dfb1c
4 changed files with 130 additions and 9 deletions
|
|
@ -361,6 +361,7 @@ target_link_libraries( ecc_test fc )
|
||||||
add_executable( all_tests tests/all_tests.cpp
|
add_executable( all_tests tests/all_tests.cpp
|
||||||
tests/compress/compress.cpp
|
tests/compress/compress.cpp
|
||||||
tests/crypto/aes_test.cpp
|
tests/crypto/aes_test.cpp
|
||||||
|
tests/crypto/base_n_tests.cpp
|
||||||
tests/crypto/blind.cpp
|
tests/crypto/blind.cpp
|
||||||
tests/network/ntp_test.cpp
|
tests/network/ntp_test.cpp
|
||||||
tests/thread/task_cancel.cpp
|
tests/thread/task_cancel.cpp
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,17 @@ namespace fc
|
||||||
{
|
{
|
||||||
auto len = cyoBase32DecodeGetLength( b32.size() );
|
auto len = cyoBase32DecodeGetLength( b32.size() );
|
||||||
std::vector<char> v(len);
|
std::vector<char> v(len);
|
||||||
cyoBase32Decode( v.data(), b32.c_str(), b32.size() );
|
len = cyoBase32Decode( v.data(), b32.c_str(), b32.size() );
|
||||||
|
v.resize( len );
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string to_base32( const char* data, size_t len )
|
std::string to_base32( const char* data, size_t len )
|
||||||
{
|
{
|
||||||
auto s = cyoBase16EncodeGetLength(len);
|
auto s = cyoBase32EncodeGetLength(len);
|
||||||
std::vector<char> b32;
|
std::vector<char> b32;
|
||||||
b32.resize(s);
|
b32.resize(s);
|
||||||
cyoBase16Encode( b32.data(), data, len );
|
cyoBase32Encode( b32.data(), data, len );
|
||||||
b32.resize( b32.size()-1); // strip the nullterm
|
b32.resize( b32.size()-1); // strip the nullterm
|
||||||
return std::string(b32.begin(),b32.end());
|
return std::string(b32.begin(),b32.end());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@ namespace fc
|
||||||
out[pos] = base36[(value % _36).to_int64()];
|
out[pos] = base36[(value % _36).to_int64()];
|
||||||
}
|
}
|
||||||
} while (value /= _36);
|
} while (value /= _36);
|
||||||
|
while (*data++ == 0) {
|
||||||
|
out[--pos] = '0';
|
||||||
|
}
|
||||||
return &out[pos]; //fc::string( &out[pos], out.size() - pos);
|
return &out[pos]; //fc::string( &out[pos], out.size() - pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,21 +32,39 @@ namespace fc
|
||||||
|
|
||||||
std::vector<char> from_base36( const fc::string& b36 )
|
std::vector<char> from_base36( const fc::string& b36 )
|
||||||
{
|
{
|
||||||
|
if ( b36.empty() ) {
|
||||||
|
std::vector<char> empty;
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
fc::bigint value;
|
fc::bigint value;
|
||||||
|
|
||||||
fc::bigint pos = 0;
|
fc::bigint pos = 0;
|
||||||
fc::bigint _36(36);
|
fc::bigint _36(36);
|
||||||
for( auto itr = b36.begin(); itr != b36.end(); ++itr )
|
for( auto itr = b36.rbegin(); itr != b36.rend(); ++itr )
|
||||||
{
|
{
|
||||||
if( *itr - '0' < 10 ) value = value + _36.exp(pos) * fc::bigint(*itr - '0');
|
if( *itr >= '0' && *itr <= '9' )
|
||||||
else if( *itr - 'a' < 26 ) value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'a'));
|
value = value + _36.exp(pos) * fc::bigint(*itr - '0');
|
||||||
else if( *itr - 'A' < 26 ) value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A'));
|
else if( *itr >= 'a' && *itr <= 'z' )
|
||||||
|
value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'a'));
|
||||||
|
else if( *itr >= 'A' && *itr <= 'Z' )
|
||||||
|
value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A'));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
wlog("unknown '${char}'", ("char",fc::string(&*itr,1)) );
|
wlog("unknown '${char}'", ("char",fc::string(&*itr,1)) );
|
||||||
}
|
}
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
return value;
|
|
||||||
|
std::vector<char> bytes = value;
|
||||||
|
int leading_zeros = 0, len = bytes.size();
|
||||||
|
const char *in = b36.c_str();
|
||||||
|
while (*in++ == '0') { leading_zeros++; }
|
||||||
|
char* first = bytes.data();
|
||||||
|
while (*first == 0) { first++; len--; }
|
||||||
|
std::vector<char> result;
|
||||||
|
result.resize(leading_zeros + len, 0);
|
||||||
|
memcpy( result.data() + leading_zeros, first, len );
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
99
tests/crypto/base_n_tests.cpp
Normal file
99
tests/crypto/base_n_tests.cpp
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
#include <fc/crypto/base32.hpp>
|
||||||
|
#include <fc/crypto/base36.hpp>
|
||||||
|
#include <fc/crypto/base58.hpp>
|
||||||
|
#include <fc/crypto/base64.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static const std::string TEST1("");
|
||||||
|
static const std::string TEST2("\0\00101", 4);
|
||||||
|
static const std::string TEST3("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||||
|
|
||||||
|
static void test_32( const std::string& test, const std::string& expected )
|
||||||
|
{
|
||||||
|
std::vector<char> vec( test.begin(), test.end() );
|
||||||
|
fc::string enc1 = fc::to_base32( vec );
|
||||||
|
fc::string enc2 = fc::to_base32( test.c_str(), test.size() );
|
||||||
|
BOOST_CHECK_EQUAL( enc1, enc2 );
|
||||||
|
BOOST_CHECK_EQUAL( expected, enc2 );
|
||||||
|
|
||||||
|
std::vector<char> dec = fc::from_base32( enc1 );
|
||||||
|
BOOST_CHECK_EQUAL( vec.size(), dec.size() );
|
||||||
|
BOOST_CHECK( !memcmp( vec.data(), dec.data(), vec.size() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(fc_crypto)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(base32_test)
|
||||||
|
{
|
||||||
|
test_32( TEST1, "" );
|
||||||
|
test_32( TEST2, "AAATAMI=" );
|
||||||
|
test_32( TEST3, "IFBEGRCFIZDUQSKKJNGE2TSPKBIVEU2UKVLFOWCZLI======" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_36( const std::string& test, const std::string& expected )
|
||||||
|
{
|
||||||
|
std::vector<char> vec( test.begin(), test.end() );
|
||||||
|
fc::string enc1 = fc::to_base36( vec );
|
||||||
|
fc::string enc2 = fc::to_base36( test.c_str(), test.size() );
|
||||||
|
BOOST_CHECK_EQUAL( enc1, enc2 );
|
||||||
|
BOOST_CHECK_EQUAL( expected, enc2 );
|
||||||
|
|
||||||
|
std::vector<char> dec = fc::from_base36( enc1 );
|
||||||
|
BOOST_CHECK_EQUAL( vec.size(), dec.size() );
|
||||||
|
BOOST_CHECK( !memcmp( vec.data(), dec.data(), vec.size() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(base36_test)
|
||||||
|
{
|
||||||
|
test_36( TEST1, "" );
|
||||||
|
test_36( TEST2, "01o35" );
|
||||||
|
test_36( TEST3, "l4ksdleyi5pnl0un5raue268ptj43dwjwmz15ie2" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_58( const std::string& test, const std::string& expected )
|
||||||
|
{
|
||||||
|
std::vector<char> vec( test.begin(), test.end() );
|
||||||
|
fc::string enc1 = fc::to_base58( vec );
|
||||||
|
fc::string enc2 = fc::to_base58( test.c_str(), test.size() );
|
||||||
|
BOOST_CHECK_EQUAL( enc1, enc2 );
|
||||||
|
BOOST_CHECK_EQUAL( expected, enc2 );
|
||||||
|
|
||||||
|
std::vector<char> dec = fc::from_base58( enc1 );
|
||||||
|
BOOST_CHECK_EQUAL( vec.size(), dec.size() );
|
||||||
|
BOOST_CHECK( !memcmp( vec.data(), dec.data(), vec.size() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(base58_test)
|
||||||
|
{
|
||||||
|
test_58( TEST1, "" );
|
||||||
|
test_58( TEST2, "1Q9e" );
|
||||||
|
test_58( TEST3, "2zuFXTJSTRK6ESktqhM2QDBkCnH1U46CnxaD" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_64( const std::string& test, const std::string& expected )
|
||||||
|
{
|
||||||
|
fc::string enc1 = fc::base64_encode( test );
|
||||||
|
fc::string enc2 = fc::base64_encode( test.c_str(), test.size() );
|
||||||
|
BOOST_CHECK_EQUAL( enc1, enc2 );
|
||||||
|
BOOST_CHECK_EQUAL( expected, enc2 );
|
||||||
|
|
||||||
|
std::string dec = fc::base64_decode( enc1 );
|
||||||
|
BOOST_CHECK_EQUAL( test.size(), dec.size() );
|
||||||
|
BOOST_CHECK_EQUAL( test, dec );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(base64_test)
|
||||||
|
{
|
||||||
|
test_64( TEST1, "" );
|
||||||
|
test_64( TEST2, "AAEwMQ==" );
|
||||||
|
test_64( TEST3, "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo=" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
Loading…
Reference in a new issue