Add tests for alignedbuffer

pull/172/head
Michael 6 years ago
parent 037cb87523
commit ba95767f57
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -600,25 +600,25 @@ set(DNS_SRC
)
set(TEST_SRC
test/main.cpp
test/alignedbuffer_unittest.cpp
test/base32_unittest.cpp
test/dht_unittest.cpp
test/encrypted_frame_unittest.cpp
test/exit_unittest.cpp
test/hiddenservice_unittest.cpp
test/traffic_transfer_unittest.cpp
test/main.cpp
test/net_unittest.cpp
test/obtain_exit_unittest.cpp
test/pq_unittest.cpp
test/net_unittest.cpp
test/test_dns_unit.cpp
test/test_dnsc_unit.cpp
test/test_dnsd_unit.cpp
test/test_dnslib.cpp
test/test_llarp_queue.cpp
test/test_llarp_aligned.cpp
test/test_llarp_queue_manager.cpp
test/test_llarp_queue.cpp
test/test_llarp_thread_pool.cpp
test/test_service_address.cpp
test/traffic_transfer_unittest.cpp
)
set(TEST_EXE testAll)

@ -17,10 +17,6 @@ extern "C"
{
extern void
randombytes(unsigned char* const ptr, unsigned long long sz);
extern void
sodium_memzero(void* const ptr, const size_t sz);
extern int
sodium_is_zero(const unsigned char* ptr, size_t sz);
}
namespace llarp
{
@ -48,7 +44,7 @@ namespace llarp
}
}
AlignedBuffer(const std::array< byte_t, SIZE >& buf)
AlignedBuffer(const Data& buf)
{
new(&val) Data;
std::copy(buf.begin(), buf.end(), as_array().begin());
@ -68,7 +64,7 @@ namespace llarp
friend std::ostream&
operator<<(std::ostream& out, const AlignedBuffer& self)
{
char tmp[(1 + sz) * 2] = {0};
char tmp[(sz * 2) + 1] = {0};
return out << HexEncode(self, tmp);
}
@ -154,10 +150,25 @@ namespace llarp
as_array().fill(f);
}
Data&
as_array()
{
return reinterpret_cast< Data& >(val);
}
const Data&
as_array() const
{
return reinterpret_cast< const Data& >(val);
}
bool
IsZero() const
{
return sodium_is_zero(as_array().data(), SIZE) != 0;
auto notZero = [](byte_t b) { return b != 0; };
return std::find_if(as_array().begin(), as_array().end(), notZero)
== as_array().end();
}
void
@ -249,19 +260,6 @@ namespace llarp
typename std::aligned_storage< sizeof(Data),
alignof(std::max_align_t) >::type;
AlignedStorage val;
protected:
Data&
as_array()
{
return reinterpret_cast< Data& >(val);
}
const Data&
as_array() const
{
return reinterpret_cast< const Data& >(val);
}
};
} // namespace llarp

@ -1,33 +0,0 @@
#include <gtest/gtest.h>
#include <crypto.hpp>
using Buffer_t = llarp::AlignedBuffer< 32 >;
using Map_t = std::unordered_map< Buffer_t, int, Buffer_t::Hash >;
struct AlignedBufferTest : public ::testing::Test
{
AlignedBufferTest() : crypto(llarp::Crypto::sodium{})
{
}
llarp::Crypto crypto;
};
TEST_F(AlignedBufferTest, TestHash)
{
Buffer_t k, other_k;
k.Randomize();
other_k.Randomize();
Map_t m;
ASSERT_TRUE(m.empty());
ASSERT_TRUE(m.emplace(k, 1).second);
ASSERT_TRUE(m.find(k) != m.end());
ASSERT_TRUE(m[k] == 1);
ASSERT_FALSE(m.find(other_k) != m.end());
ASSERT_TRUE(m.size() == 1);
Buffer_t k_copy = k;
ASSERT_FALSE(m.emplace(k_copy, 2).second);
ASSERT_FALSE(m[k_copy] == 2);
ASSERT_TRUE(m[k_copy] == 1);
};

@ -0,0 +1,232 @@
#include <gtest/gtest.h>
#include <aligned.hpp>
#include <crypto.hpp>
#include <iostream>
#include <sstream>
#include <type_traits>
#include <unordered_map>
using TestSizes =
::testing::Types< std::integral_constant< std::size_t, 2 >,
std::integral_constant< std::size_t, 3 >,
std::integral_constant< std::size_t, 4 >,
std::integral_constant< std::size_t, 8 >,
std::integral_constant< std::size_t, 16 >,
std::integral_constant< std::size_t, 32 >,
std::integral_constant< std::size_t, 64 >,
std::integral_constant< std::size_t, 1024 > >;
template < typename T >
struct AlignedBufferTest : public ::testing::Test
{
AlignedBufferTest() : crypto(llarp::Crypto::sodium{})
{
}
llarp::Crypto crypto;
};
TYPED_TEST_CASE(AlignedBufferTest, TestSizes);
TYPED_TEST(AlignedBufferTest, Constructor)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
EXPECT_EQ(b.size(), TypeParam::value);
}
TYPED_TEST(AlignedBufferTest, CopyConstructor)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
Buffer c = b;
EXPECT_TRUE(c.IsZero());
c.Fill(1);
EXPECT_FALSE(c.IsZero());
Buffer d = c;
EXPECT_FALSE(d.IsZero());
}
TYPED_TEST(AlignedBufferTest, AltConstructors)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
b.Fill(2);
Buffer c(b.as_array());
EXPECT_FALSE(c.IsZero());
Buffer d(c.data());
EXPECT_FALSE(d.IsZero());
}
TYPED_TEST(AlignedBufferTest, Assignment)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
Buffer c;
c = b;
EXPECT_TRUE(c.IsZero());
c.Fill(1);
EXPECT_FALSE(c.IsZero());
Buffer d;
d = c;
EXPECT_FALSE(d.IsZero());
}
TYPED_TEST(AlignedBufferTest, StreamOut)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
std::stringstream stream;
stream << b;
EXPECT_EQ(stream.str(), std::string(TypeParam::value * 2, '0'));
stream.str("");
b.Fill(255);
stream << b;
EXPECT_EQ(stream.str(), std::string(TypeParam::value * 2, 'f'));
}
TYPED_TEST(AlignedBufferTest, BitwiseNot)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
Buffer c = ~b;
EXPECT_FALSE(c.IsZero());
for(auto val : c.as_array())
{
EXPECT_EQ(255, val);
}
Buffer d = ~c;
EXPECT_TRUE(d.IsZero());
}
TYPED_TEST(AlignedBufferTest, Operators)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
Buffer c = b;
EXPECT_EQ(b, c);
EXPECT_GE(b, c);
EXPECT_LE(b, c);
EXPECT_GE(c, b);
EXPECT_LE(c, b);
c.Fill(1);
EXPECT_NE(b, c);
EXPECT_LT(b, c);
EXPECT_GT(c, b);
}
TYPED_TEST(AlignedBufferTest, Xor)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
Buffer c;
b.Fill(255);
c.Fill(255);
EXPECT_FALSE(b.IsZero());
EXPECT_FALSE(c.IsZero());
Buffer d = b ^ c;
// 1 ^ 1 = 0
EXPECT_TRUE(d.IsZero());
// Verify unchanged
EXPECT_FALSE(b.IsZero());
EXPECT_FALSE(c.IsZero());
Buffer e, f;
e.Fill(255);
Buffer g = e ^ f;
// 1 ^ 0 = 1
EXPECT_FALSE(g.IsZero());
Buffer h, i;
i.Fill(255);
Buffer j = h ^ i;
// 0 ^ 1 = 1
EXPECT_FALSE(j.IsZero());
}
TYPED_TEST(AlignedBufferTest, XorAssign)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b, c;
b.Fill(255);
c.Fill(255);
EXPECT_FALSE(b.IsZero());
EXPECT_FALSE(c.IsZero());
b ^= c;
EXPECT_TRUE(b.IsZero());
}
TYPED_TEST(AlignedBufferTest, Zero)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
Buffer b;
EXPECT_TRUE(b.IsZero());
b.Fill(127);
EXPECT_FALSE(b.IsZero());
b.Zero();
EXPECT_TRUE(b.IsZero());
}
TYPED_TEST(AlignedBufferTest, TestHash)
{
using Buffer = llarp::AlignedBuffer< TypeParam::value >;
using Map_t = std::unordered_map< Buffer, int, typename Buffer::Hash >;
Buffer k, other_k;
k.Randomize();
other_k.Randomize();
Map_t m;
EXPECT_TRUE(m.empty());
EXPECT_TRUE(m.emplace(k, 1).second);
EXPECT_TRUE(m.find(k) != m.end());
EXPECT_TRUE(m[k] == 1);
EXPECT_FALSE(m.find(other_k) != m.end());
EXPECT_TRUE(m.size() == 1);
Buffer k_copy = k;
EXPECT_FALSE(m.emplace(k_copy, 2).second);
EXPECT_FALSE(m[k_copy] == 2);
EXPECT_TRUE(m[k_copy] == 1);
};
// TEST_P(Copy, )
Loading…
Cancel
Save