Skip to content

Commit

Permalink
Tests: Address Book unit-test checks unique entry
Browse files Browse the repository at this point in the history
New test case to ensure address book only inserts unique entries.

New test fixture struct to convert a subscription line into an address
book entry.

Refs monero-project#835
  • Loading branch information
coneiric committed Mar 23, 2018
1 parent 9275963 commit 75b5712
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions tests/unit_tests/client/address_book/impl.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** //
* Copyright (c) 2015-2017, The Kovri I2P Router Project //
* Copyright (c) 2015-2018, The Kovri I2P Router Project //
* //
* All rights reserved. //
* //
Expand Down Expand Up @@ -45,6 +45,56 @@
/// @class SubscriptionFixture
struct SubscriptionFixture {

struct AddressBookEntry
{
public:
AddressBookEntry() : m_Host{""}, m_Address{} {}

explicit AddressBookEntry(const std::string& subscription_line)
: m_Host{""}, m_Address{}
{
try
{
std::size_t pos = subscription_line.find('=');
if (pos == std::string::npos)
throw std::runtime_error(
"AddressBookEntry: invalid subscription line");
std::string const host = subscription_line.substr(0, pos++);
std::string const address = subscription_line.substr(pos);
kovri::core::IdentityEx ident;
ident.FromBase64(address);
const auto& ident_hash = ident.GetIdentHash();
m_Host = host;
m_Address = ident_hash;
}
catch (...)
{
kovri::core::Exception ex;
ex.Dispatch(__func__);
throw;
}
}

const std::string& GetHost() const
{
return m_Host;
}

const kovri::core::IdentHash& GetAddress() const
{
return m_Address;
}

private:
/// @var m_Host
/// @brief Human-readable I2P hostname
std::string m_Host;

/// @var m_Address
/// @brief Hash of I2P address
kovri::core::IdentHash m_Address;
};

/// @brief Validates given lines as proven addressbook host/address pairs
/// @param lines Lines to validate
/// @return Only valid data that was parsed
Expand Down Expand Up @@ -170,4 +220,22 @@ BOOST_AUTO_TEST_CASE(PGPClearSign) {

// TODO(unassigned): more cases?

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(RejectDuplicateEntry)
{
// Ensure valid subscription line creates an entry
BOOST_CHECK_NO_THROW(AddressBookEntry const entry(subscription.front()));

AddressBookEntry const entry(subscription.front());
const std::string& host = entry.GetHost();
const auto& address = entry.GetAddress();

// Ensure valid entry is inserted
BOOST_CHECK_NO_THROW(book.InsertAddress(host, address));
// Ensure address book throws for duplicate host
BOOST_CHECK_THROW(book.InsertAddress(host, address), std::runtime_error);
// Ensure address book throws for duplicate address
BOOST_CHECK_THROW(
book.InsertAddress("unique." + host, address), std::runtime_error);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 75b5712

Please sign in to comment.