Don't include unit test sources in code coverage (RIPD-1132):

Most files containing unit test code are moved to
src/test. JTx and the test client code are not yet moved.
This commit is contained in:
Brad Chase
2016-09-02 15:25:05 -04:00
committed by Vinnie Falco
parent 8687f64429
commit 8f97889176
165 changed files with 2090 additions and 1693 deletions

View File

@@ -1,81 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/CheckLibraryVersions.h>
#include <ripple/basics/impl/CheckLibraryVersionsImpl.h>
#include <ripple/beast/unit_test.h>
namespace ripple {
namespace version {
struct CheckLibraryVersions_test : beast::unit_test::suite
{
void print_message()
{
log << "ssl minimal: " << openSSLMinimal << "\n"
<< "ssl actual: " << openSSLVersion() << "\n"
<< "boost minimal: " << boostMinimal << "\n"
<< "boost actual: " << boostVersion() << "\n"
<< std::flush;
}
void test_bad_ssl()
{
std::string error;
try {
checkOpenSSL(openSSLVersion(0x0090819fL));
} catch (std::runtime_error& e) {
error = e.what();
}
auto expectedError = "Your OpenSSL library is out of date.\n"
"Your version: 0.9.8-o\n"
"Required version: ";
unexpected(error.find(expectedError) != 0, error);
}
void test_bad_boost()
{
std::string error;
try {
checkBoost(boostVersion(105400));
} catch (std::runtime_error& e) {
error = e.what();
}
auto expectedError = "Your Boost library is out of date.\n"
"Your version: 1.54.0\n"
"Required version: ";
unexpected(error.find(expectedError) != 0, error);
}
void run()
{
print_message();
checkLibraryVersions();
test_bad_ssl();
test_bad_boost();
}
};
BEAST_DEFINE_TESTSUITE(CheckLibraryVersions, ripple_basics, ripple);
} // namespace version
} // namespace ripple

View File

@@ -1,97 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/chrono.h>
#include <ripple/basics/KeyCache.h>
#include <ripple/beast/unit_test.h>
#include <ripple/beast/clock/manual_clock.h>
namespace ripple {
class KeyCache_test : public beast::unit_test::suite
{
public:
void run ()
{
TestStopwatch clock;
clock.set (0);
using Key = std::string;
using Cache = KeyCache <Key>;
// Insert an item, retrieve it, and age it so it gets purged.
{
Cache c ("test", clock, 1, 2);
BEAST_EXPECT(c.size () == 0);
BEAST_EXPECT(c.insert ("one"));
BEAST_EXPECT(! c.insert ("one"));
BEAST_EXPECT(c.size () == 1);
BEAST_EXPECT(c.exists ("one"));
BEAST_EXPECT(c.touch_if_exists ("one"));
++clock;
c.sweep ();
BEAST_EXPECT(c.size () == 1);
BEAST_EXPECT(c.exists ("one"));
++clock;
c.sweep ();
BEAST_EXPECT(c.size () == 0);
BEAST_EXPECT(! c.exists ("one"));
BEAST_EXPECT(! c.touch_if_exists ("one"));
}
// Insert two items, have one expire
{
Cache c ("test", clock, 2, 2);
BEAST_EXPECT(c.insert ("one"));
BEAST_EXPECT(c.size () == 1);
BEAST_EXPECT(c.insert ("two"));
BEAST_EXPECT(c.size () == 2);
++clock;
c.sweep ();
BEAST_EXPECT(c.size () == 2);
BEAST_EXPECT(c.touch_if_exists ("two"));
++clock;
c.sweep ();
BEAST_EXPECT(c.size () == 1);
BEAST_EXPECT(c.exists ("two"));
}
// Insert three items (1 over limit), sweep
{
Cache c ("test", clock, 2, 3);
BEAST_EXPECT(c.insert ("one"));
++clock;
BEAST_EXPECT(c.insert ("two"));
++clock;
BEAST_EXPECT(c.insert ("three"));
++clock;
BEAST_EXPECT(c.size () == 3);
c.sweep ();
BEAST_EXPECT(c.size () < 3);
}
}
};
BEAST_DEFINE_TESTSUITE(KeyCache,common,ripple);
}

View File

@@ -1,94 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/RangeSet.h>
#include <ripple/beast/unit_test.h>
namespace ripple {
class RangeSet_test : public beast::unit_test::suite
{
public:
RangeSet createPredefinedSet ()
{
RangeSet set;
// Set will include:
// [ 0, 5]
// [10,15]
// [20,25]
// etc...
for (int i = 0; i < 10; ++i)
set.setRange (10 * i, 10 * i + 5);
return set;
}
void testMembership ()
{
testcase ("membership");
RangeSet r1, r2;
r1.setRange (1, 10);
r1.clearValue (5);
r1.setRange (11, 20);
r2.setRange (1, 4);
r2.setRange (6, 10);
r2.setRange (10, 20);
BEAST_EXPECT(!r1.hasValue (5));
BEAST_EXPECT(r2.hasValue (9));
}
void testPrevMissing ()
{
testcase ("prevMissing");
RangeSet const set = createPredefinedSet ();
for (int i = 0; i < 100; ++i)
{
int const oneBelowRange = (10*(i/10))-1;
int const expectedPrevMissing =
((i % 10) > 6) ? (i-1) : oneBelowRange;
BEAST_EXPECT(set.prevMissing (i) == expectedPrevMissing);
}
}
void run ()
{
testMembership ();
testPrevMissing ();
// TODO: Traverse functions must be tested
}
};
BEAST_DEFINE_TESTSUITE(RangeSet,ripple_basics,ripple);
} // ripple

View File

@@ -1,103 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/ToString.h>
#include <ripple/beast/unit_test.h>
namespace ripple {
class StringUtilities_test : public beast::unit_test::suite
{
public:
void testUnHexSuccess (std::string const& strIn, std::string const& strExpected)
{
auto rv = strUnHex (strIn);
BEAST_EXPECT(rv.second);
BEAST_EXPECT(makeSlice(rv.first) == makeSlice(strExpected));
}
void testUnHexFailure (std::string const& strIn)
{
auto rv = strUnHex (strIn);
BEAST_EXPECT(! rv.second);
BEAST_EXPECT(rv.first.empty());
}
void testUnHex ()
{
testcase ("strUnHex");
testUnHexSuccess ("526970706c6544", "RippleD");
testUnHexSuccess ("A", "\n");
testUnHexSuccess ("0A", "\n");
testUnHexSuccess ("D0A", "\r\n");
testUnHexSuccess ("0D0A", "\r\n");
testUnHexSuccess ("200D0A", " \r\n");
testUnHexSuccess ("282A2B2C2D2E2F29", "(*+,-./)");
// Check for things which contain some or only invalid characters
testUnHexFailure ("123X");
testUnHexFailure ("V");
testUnHexFailure ("XRP");
}
void testParseUrl ()
{
testcase ("parseUrl");
std::string strScheme;
std::string strDomain;
int iPort;
std::string strPath;
BEAST_EXPECT (parseUrl ("lower://domain", strScheme, strDomain, iPort, strPath));
BEAST_EXPECT(strScheme == "lower");
BEAST_EXPECT(strDomain == "domain");
BEAST_EXPECT(iPort == -1);
BEAST_EXPECT(strPath == "");
BEAST_EXPECT(parseUrl ("UPPER://domain:234/", strScheme, strDomain, iPort, strPath));
BEAST_EXPECT(strScheme == "upper");
BEAST_EXPECT(iPort == 234);
BEAST_EXPECT(strPath == "/");
BEAST_EXPECT(parseUrl ("Mixed://domain/path", strScheme, strDomain, iPort, strPath));
BEAST_EXPECT(strScheme == "mixed");
BEAST_EXPECT(strPath == "/path");
}
void testToString ()
{
testcase ("toString");
auto result = to_string("hello");
BEAST_EXPECT(result == "hello");
}
void run ()
{
testParseUrl ();
testUnHex ();
testToString ();
}
};
BEAST_DEFINE_TESTSUITE(StringUtilities, ripple_basics, ripple);
} // ripple

View File

@@ -1,152 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/chrono.h>
#include <ripple/basics/TaggedCache.h>
#include <ripple/beast/unit_test.h>
#include <ripple/beast/clock/manual_clock.h>
namespace ripple {
/*
I guess you can put some items in, make sure they're still there. Let some
time pass, make sure they're gone. Keep a strong pointer to one of them, make
sure you can still find it even after time passes. Create two objects with
the same key, canonicalize them both and make sure you get the same object.
Put an object in but keep a strong pointer to it, advance the clock a lot,
then canonicalize a new object with the same key, make sure you get the
original object.
*/
class TaggedCache_test : public beast::unit_test::suite
{
public:
void run ()
{
beast::Journal const j;
TestStopwatch clock;
clock.set (0);
using Key = int;
using Value = std::string;
using Cache = TaggedCache <Key, Value>;
Cache c ("test", 1, 1, clock, j);
// Insert an item, retrieve it, and age it so it gets purged.
{
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 0);
BEAST_EXPECT(! c.insert (1, "one"));
BEAST_EXPECT(c.getCacheSize() == 1);
BEAST_EXPECT(c.getTrackSize() == 1);
{
std::string s;
BEAST_EXPECT(c.retrieve (1, s));
BEAST_EXPECT(s == "one");
}
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize () == 0);
BEAST_EXPECT(c.getTrackSize () == 0);
}
// Insert an item, maintain a strong pointer, age it, and
// verify that the entry still exists.
{
BEAST_EXPECT(! c.insert (2, "two"));
BEAST_EXPECT(c.getCacheSize() == 1);
BEAST_EXPECT(c.getTrackSize() == 1);
{
Cache::mapped_ptr p (c.fetch (2));
BEAST_EXPECT(p != nullptr);
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 1);
}
// Make sure its gone now that our reference is gone
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 0);
}
// Insert the same key/value pair and make sure we get the same result
{
BEAST_EXPECT(! c.insert (3, "three"));
{
Cache::mapped_ptr const p1 (c.fetch (3));
Cache::mapped_ptr p2 (std::make_shared <Value> ("three"));
c.canonicalize (3, p2);
BEAST_EXPECT(p1.get() == p2.get());
}
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 0);
}
// Put an object in but keep a strong pointer to it, advance the clock a lot,
// then canonicalize a new object with the same key, make sure you get the
// original object.
{
// Put an object in
BEAST_EXPECT(! c.insert (4, "four"));
BEAST_EXPECT(c.getCacheSize() == 1);
BEAST_EXPECT(c.getTrackSize() == 1);
{
// Keep a strong pointer to it
Cache::mapped_ptr p1 (c.fetch (4));
BEAST_EXPECT(p1 != nullptr);
BEAST_EXPECT(c.getCacheSize() == 1);
BEAST_EXPECT(c.getTrackSize() == 1);
// Advance the clock a lot
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 1);
// Canonicalize a new object with the same key
Cache::mapped_ptr p2 (std::make_shared <std::string> ("four"));
BEAST_EXPECT(c.canonicalize (4, p2, false));
BEAST_EXPECT(c.getCacheSize() == 1);
BEAST_EXPECT(c.getTrackSize() == 1);
// Make sure we get the original object
BEAST_EXPECT(p1.get() == p2.get());
}
++clock;
c.sweep ();
BEAST_EXPECT(c.getCacheSize() == 0);
BEAST_EXPECT(c.getTrackSize() == 0);
}
}
};
BEAST_DEFINE_TESTSUITE(TaggedCache,common,ripple);
}

View File

@@ -1,114 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2016 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/base_uint.h>
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/digest.h>
namespace ripple {
namespace test {
struct base_uint_test : beast::unit_test::suite
{
using test96 = base_uint<96>;
template<class bt>
void checkHash(bt const& t, std::string const& hash)
{
auto h = sha512Half(t);
BEAST_EXPECT(to_string(h) == hash);
}
void run()
{
Blob raw{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
BEAST_EXPECT(test96::bytes == raw.size());
test96 u(raw);
BEAST_EXPECT(raw.size() == u.size());
BEAST_EXPECT(to_string(u) == "0102030405060708090A0B0C");
BEAST_EXPECT(*u.data() == 1);
BEAST_EXPECT(u.signum() == 1);
BEAST_EXPECT(!!u);
BEAST_EXPECT(!u.isZero());
BEAST_EXPECT(u.isNonZero());
unsigned char t = 0;
for (auto& d : u)
{
BEAST_EXPECT(d == ++t);
}
checkHash(u, "25996DBE31A04FF03E4E63C9C647AC6D459475E2845529238F3D08F4A37EBC54");
test96 v(~u);
BEAST_EXPECT(to_string(v) == "FEFDFCFBFAF9F8F7F6F5F4F3");
BEAST_EXPECT(*v.data() == 0xfe);
BEAST_EXPECT(v.signum() == 1);
BEAST_EXPECT(!!v);
BEAST_EXPECT(!v.isZero());
BEAST_EXPECT(v.isNonZero());
t = 0xff;
for (auto& d : v)
{
BEAST_EXPECT(d == --t);
}
checkHash(v, "019A8C0B8307FD822A1346546200328DB40B4565793FD4799B83D780BDB634CE");
BEAST_EXPECT(compare(u, v) < 0);
BEAST_EXPECT(compare(v, u) > 0);
v = u;
BEAST_EXPECT(v == u);
test96 z(beast::zero);
BEAST_EXPECT(to_string(z) == "000000000000000000000000");
BEAST_EXPECT(*z.data() == 0);
BEAST_EXPECT(*z.begin() == 0);
BEAST_EXPECT(*std::prev(z.end(), 1) == 0);
BEAST_EXPECT(z.signum() == 0);
BEAST_EXPECT(!z);
BEAST_EXPECT(z.isZero());
BEAST_EXPECT(!z.isNonZero());
for (auto& d : z)
{
BEAST_EXPECT(d == 0);
}
checkHash(z, "666A9A1A7542E895A9F447D1C3E0FFD679BBF6346E0C43F5C7A733C46F5E56C2");
test96 n(z);
n++;
BEAST_EXPECT(n == test96(1));
n--;
BEAST_EXPECT(n == beast::zero);
BEAST_EXPECT(n == z);
n--;
BEAST_EXPECT(to_string(n) == "FFFFFFFFFFFFFFFFFFFFFFFF");
n = beast::zero;
BEAST_EXPECT(n == z);
auto hash = sha512Half(u, v, z, n);
BEAST_EXPECT(to_string(hash) ==
"55CAB39C72F2572057114B4732210605AA22C6E89243FBB5F9943130D813F902");
}
};
BEAST_DEFINE_TESTSUITE(base_uint, ripple_basics, ripple);
} // namespace test
} // namespace ripple

View File

@@ -1,62 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/contract.h>
#include <ripple/beast/unit_test.h>
#include <string>
namespace ripple {
class contract_test : public beast::unit_test::suite
{
public:
void run ()
{
try
{
Throw<std::runtime_error>("Throw test");
}
catch (std::runtime_error const& e)
{
BEAST_EXPECT(std::string(e.what()) == "Throw test");
try
{
Rethrow();
}
catch (std::runtime_error const& e)
{
BEAST_EXPECT(std::string(e.what()) == "Throw test");
}
catch (...)
{
BEAST_EXPECT(false);
}
}
catch (...)
{
BEAST_EXPECT(false);
}
}
};
BEAST_DEFINE_TESTSUITE(contract,basics,ripple);
}

View File

@@ -1,265 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/hardened_hash.h>
#include <ripple/beast/unit_test.h>
#include <boost/functional/hash.hpp>
#include <array>
#include <cstdint>
#include <iomanip>
#include <functional>
#include <unordered_map>
#include <unordered_set>
namespace ripple {
namespace detail {
template <class T>
class test_user_type_member
{
private:
T t;
public:
explicit test_user_type_member (T const& t_ = T())
: t (t_)
{
}
template <class Hasher>
friend void hash_append (Hasher& h, test_user_type_member const& a) noexcept
{
using beast::hash_append;
hash_append (h, a.t);
}
};
template <class T>
class test_user_type_free
{
private:
T t;
public:
explicit test_user_type_free (T const& t_ = T())
: t (t_)
{
}
template <class Hasher>
friend void hash_append (Hasher& h, test_user_type_free const& a) noexcept
{
using beast::hash_append;
hash_append (h, a.t);
}
};
} // detail
} // ripple
//------------------------------------------------------------------------------
namespace ripple {
namespace detail {
template <class T>
using test_hardened_unordered_set =
std::unordered_set <T, hardened_hash <>>;
template <class T>
using test_hardened_unordered_map =
std::unordered_map <T, int, hardened_hash <>>;
template <class T>
using test_hardened_unordered_multiset =
std::unordered_multiset <T, hardened_hash <>>;
template <class T>
using test_hardened_unordered_multimap =
std::unordered_multimap <T, int, hardened_hash <>>;
} // detail
template <std::size_t Bits, class UInt = std::uint64_t>
class unsigned_integer
{
private:
static_assert (std::is_integral<UInt>::value &&
std::is_unsigned <UInt>::value,
"UInt must be an unsigned integral type");
static_assert (Bits%(8*sizeof(UInt))==0,
"Bits must be a multiple of 8*sizeof(UInt)");
static_assert (Bits >= (8*sizeof(UInt)),
"Bits must be at least 8*sizeof(UInt)");
static std::size_t const size = Bits/(8*sizeof(UInt));
std::array <UInt, size> m_vec;
public:
using value_type = UInt;
static std::size_t const bits = Bits;
static std::size_t const bytes = bits / 8;
template <class Int>
static
unsigned_integer
from_number (Int v)
{
unsigned_integer result;
for (std::size_t i (1); i < size; ++i)
result.m_vec [i] = 0;
result.m_vec[0] = v;
return result;
}
void*
data() noexcept
{
return &m_vec[0];
}
void const*
data() const noexcept
{
return &m_vec[0];
}
template <class Hasher>
friend void hash_append(Hasher& h, unsigned_integer const& a) noexcept
{
using beast::hash_append;
hash_append (h, a.m_vec);
}
friend
std::ostream&
operator<< (std::ostream& s, unsigned_integer const& v)
{
for (std::size_t i (0); i < size; ++i)
s <<
std::hex <<
std::setfill ('0') <<
std::setw (2*sizeof(UInt)) <<
v.m_vec[i]
;
return s;
}
};
using sha256_t = unsigned_integer <256, std::size_t>;
static_assert (sha256_t::bits == 256,
"sha256_t must have 256 bits");
} // ripple
//------------------------------------------------------------------------------
namespace ripple {
class hardened_hash_test
: public beast::unit_test::suite
{
public:
template <class T>
void
check ()
{
T t{};
hardened_hash <>() (t);
pass();
}
template <template <class T> class U>
void
check_user_type()
{
check <U <bool>> ();
check <U <char>> ();
check <U <signed char>> ();
check <U <unsigned char>> ();
// These cause trouble for boost
//check <U <char16_t>> ();
//check <U <char32_t>> ();
check <U <wchar_t>> ();
check <U <short>> ();
check <U <unsigned short>> ();
check <U <int>> ();
check <U <unsigned int>> ();
check <U <long>> ();
check <U <long long>> ();
check <U <unsigned long>> ();
check <U <unsigned long long>> ();
check <U <float>> ();
check <U <double>> ();
check <U <long double>> ();
}
template <template <class T> class C >
void
check_container()
{
{
C <detail::test_user_type_member <std::string>> c;
}
pass();
{
C <detail::test_user_type_free <std::string>> c;
}
pass();
}
void
test_user_types()
{
testcase ("user types");
check_user_type <detail::test_user_type_member> ();
check_user_type <detail::test_user_type_free> ();
}
void
test_containers()
{
testcase ("containers");
check_container <detail::test_hardened_unordered_set>();
check_container <detail::test_hardened_unordered_map>();
check_container <detail::test_hardened_unordered_multiset>();
check_container <detail::test_hardened_unordered_multimap>();
}
void
run ()
{
test_user_types();
test_containers();
}
};
BEAST_DEFINE_TESTSUITE(hardened_hash,basics,ripple);
} // ripple

View File

@@ -1,74 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2016 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/mulDiv.h>
#include <ripple/beast/unit_test.h>
namespace ripple {
namespace test {
struct mulDiv_test : beast::unit_test::suite
{
void run()
{
const auto max = std::numeric_limits<std::uint64_t>::max();
const std::uint64_t max32 = std::numeric_limits<std::uint32_t>::max();
auto result = mulDiv(85, 20, 5);
BEAST_EXPECT(result.first && result.second == 340);
result = mulDiv(20, 85, 5);
BEAST_EXPECT(result.first && result.second == 340);
result = mulDiv(0, max - 1, max - 3);
BEAST_EXPECT(result.first && result.second == 0);
result = mulDiv(max - 1, 0, max - 3);
BEAST_EXPECT(result.first && result.second == 0);
result = mulDiv(max, 2, max / 2);
BEAST_EXPECT(result.first && result.second == 4);
result = mulDiv(max, 1000, max / 1000);
BEAST_EXPECT(result.first && result.second == 1000000);
result = mulDiv(max, 1000, max / 1001);
BEAST_EXPECT(result.first && result.second == 1001000);
// 2^64 / 5 = 3689348814741910323, but we lose some precision
// starting in the 10th digit to avoid the overflow.
result = mulDiv(max32 + 1, max32 + 1, 5);
BEAST_EXPECT(result.first && result.second == 3689348813882916864);
// Overflow
result = mulDiv(max - 1, max - 2, 5);
BEAST_EXPECT(!result.first && result.second == max);
try
{
mulDivThrow(max - 1, max - 2, 5);
fail();
}
catch (std::overflow_error const& e)
{
BEAST_EXPECT(e.what() == std::string("mulDiv"));
}
}
};
BEAST_DEFINE_TESTSUITE(mulDiv, ripple_basics, ripple);
} // namespace test
} // namespace ripple