Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Second attempt at reaching 100% of coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Jan 7, 2019
1 parent b625335 commit 0b9b547
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
55 changes: 35 additions & 20 deletions mkmmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ struct MMDB_s_deleter {
// MMDB_s_uptr is a unique pointer to MMDB_s.
using MMDB_s_uptr = std::unique_ptr<MMDB_s, MMDB_s_deleter>;

// MKMMDB_NOEXCEPT allows to remove the noexcept specifier in unit tests.
#ifndef MKMMDB_NOEXCEPT
#define MKMMDB_NOEXCEPT noexcept
#endif

// Handle::Impl contains Handle's internals.
class Handle::Impl {
public:
Expand All @@ -119,21 +124,21 @@ class Handle::Impl {
// the country code. @param logs is where to store logs. Returns true on
// success and false on failure.
bool finish_lookup_cc(MMDB_entry_s *entry, std::string &cc,
std::vector<std::string> &logs) noexcept;
std::vector<std::string> &logs) MKMMDB_NOEXCEPT;

// finish_lookup_asn finishes a ASN lookup. @param entry is the entry that
// should contain the results. @param asn is the place where we'll write
// the ASN string. @param logs is where to store logs. Returns true on
// success and false on failure.
bool finish_lookup_asn(MMDB_entry_s *entry, std::string &asn,
std::vector<std::string> &logs) noexcept;
std::vector<std::string> &logs) MKMMDB_NOEXCEPT;

// finish_lookup_org finishes a ORG lookup. @param entry is the entry that
// should contain the results. @param org is the place where we'll write
// the ORG string. @param logs is where to store logs. Returns true on
// success and false on failure.
bool finish_lookup_org(MMDB_entry_s *entry, std::string &org,
std::vector<std::string> &logs) noexcept;
std::vector<std::string> &logs) MKMMDB_NOEXCEPT;
};

Handle::Handle() noexcept { impl.reset(new Handle::Impl); }
Expand Down Expand Up @@ -208,16 +213,28 @@ static bool MMDB_get_value_check(
return true;
}

bool Handle::Impl::finish_lookup_cc(MMDB_entry_s *entry, std::string &cc,
std::vector<std::string> &logs) noexcept {
if (!entry) {
abort();
}
// MKMMDB_ABORT allows to check in unit tests that we would abort.
#ifndef MKMMDB_ABORT
#define MKMMDB_ABORT abort()
#endif

// MKMMDB_ABORT_IF_NULLPTR calls abort if @p Pointer is nullptr.
#define MKMMDB_ABORT_IF_NULLPTR(Pointer) \
do { \
if (Pointer == nullptr) { \
MKMMDB_ABORT; \
} \
} while (0)

bool Handle::Impl::finish_lookup_cc(
MMDB_entry_s *entry, std::string &cc,
std::vector<std::string> &logs) MKMMDB_NOEXCEPT {
MKMMDB_ABORT_IF_NULLPTR(entry);
MMDB_entry_data_s data{};
auto mmdb_error = MMDB_get_value(
entry, &data, "registered_country", "iso_code", nullptr);
auto ok = MMDB_get_value_check(
mmdb_error, data, MMDB_DATA_TYPE_UTF8_STRING, logs);
mmdb_error, data, MMDB_DATA_TYPE_UTF8_STRING, logs);
MKMOCK_HOOK(finish_lookup_cc_check, ok);
if (!ok) {
return false;
Expand All @@ -234,11 +251,10 @@ bool Handle::lookup_cc(const std::string &ip, std::string &cc,
});
}

bool Handle::Impl::finish_lookup_asn(MMDB_entry_s *entry, std::string &asn,
std::vector<std::string> &logs) noexcept {
if (!entry) {
abort();
}
bool Handle::Impl::finish_lookup_asn(
MMDB_entry_s *entry, std::string &asn,
std::vector<std::string> &logs) MKMMDB_NOEXCEPT {
MKMMDB_ABORT_IF_NULLPTR(entry);
MMDB_entry_data_s data{};
auto mmdb_error = MMDB_get_value(
entry, &data, "autonomous_system_number", nullptr);
Expand All @@ -259,16 +275,15 @@ bool Handle::lookup_asn(const std::string &ip, std::string &asn,
});
}

bool Handle::Impl::finish_lookup_org(MMDB_entry_s *entry, std::string &org,
std::vector<std::string> &logs) noexcept {
if (!entry) {
abort();
}
bool Handle::Impl::finish_lookup_org(
MMDB_entry_s *entry, std::string &org,
std::vector<std::string> &logs) MKMMDB_NOEXCEPT {
MKMMDB_ABORT_IF_NULLPTR(entry);
MMDB_entry_data_s data{};
auto mmdb_error = MMDB_get_value(
entry, &data, "autonomous_system_organization", nullptr);
auto ok = MMDB_get_value_check(
mmdb_error, data, MMDB_DATA_TYPE_UTF8_STRING, logs);
mmdb_error, data, MMDB_DATA_TYPE_UTF8_STRING, logs);
MKMOCK_HOOK(finish_lookup_org_check, ok);
if (!ok) {
return false;
Expand Down
10 changes: 10 additions & 0 deletions unit-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ MKMOCK_DEFINE_HOOK(finish_lookup_cc_check, bool);
MKMOCK_DEFINE_HOOK(finish_lookup_asn_check, bool);
MKMOCK_DEFINE_HOOK(finish_lookup_org_check, bool);

// Override MKMMDB_ABORT so we can actually verify we would abort
#define MKMMDB_ABORT throw std::exception()

// Disable noexcept specifier for functions that may MKMMDB_ABORT
#define MKMMDB_NOEXCEPT // Nothing

// Include mkmmdb implementation
// -----------------------------

Expand Down Expand Up @@ -90,3 +96,7 @@ TEST_CASE("When lookup_org fails because MMDB provides us a bad value") {
REQUIRE(handle.lookup_org("8.8.8.8", org, logs) == false);
});
}

TEST_CASE("MKMMDB_ABORT_IF_NULLPTR works as expected") {
REQUIRE_THROWS([]() { MKMMDB_ABORT_IF_NULLPTR(nullptr); }());
}

0 comments on commit 0b9b547

Please sign in to comment.