This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
103 lines (88 loc) · 3.22 KB
/
tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <maxminddb.h>
#include "mkmock.hpp"
MKMOCK_DEFINE_HOOK(MMDB_lookup_string_mmdb_error, int);
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
// -----------------------------
#define MKMMDB_INLINE_IMPL // inline the implementation
#define MKMMDB_MOCK // enable mocking
#include "mkmmdb.hpp"
TEST_CASE("When MMDB_lookup_string fails with mmdb_error") {
MKMOCK_WITH_ENABLED_HOOK(
MMDB_lookup_string_mmdb_error, MMDB_CORRUPT_SEARCH_TREE_ERROR,
{
std::vector<std::string> logs;
mk::mmdb::Handle handle;
std::string asn;
REQUIRE(handle.open(".mkbuild/download/asn.mmdb", logs) == true);
REQUIRE(handle.lookup_asn2("8.8.8.8", asn, logs) == false);
});
}
TEST_CASE("When MMDB_get_value fails") {
std::vector<std::string> logs;
MMDB_entry_data_s data{};
REQUIRE(mk::mmdb::MMDB_get_value_check(
MMDB_CORRUPT_SEARCH_TREE_ERROR, data, MMDB_DATA_TYPE_UTF8_STRING,
logs) == false);
}
TEST_CASE("When MMDB_get_value does not contain data") {
std::vector<std::string> logs;
MMDB_entry_data_s data{};
REQUIRE(mk::mmdb::MMDB_get_value_check(
MMDB_SUCCESS, data, MMDB_DATA_TYPE_UTF8_STRING, logs) == false);
}
TEST_CASE("When MMDB_get_value does not contain the right type") {
std::vector<std::string> logs;
MMDB_entry_data_s data{};
data.has_data = true;
data.type = MMDB_DATA_TYPE_UINT32;
REQUIRE(mk::mmdb::MMDB_get_value_check(
MMDB_SUCCESS, data, MMDB_DATA_TYPE_UTF8_STRING, logs) == false);
}
TEST_CASE("When lookup_cc fails because MMDB provides us a bad value") {
MKMOCK_WITH_ENABLED_HOOK(
finish_lookup_cc_check, false,
{
std::vector<std::string> logs;
mk::mmdb::Handle handle;
std::string cc;
REQUIRE(handle.open(".mkbuild/download/country.mmdb", logs) == true);
REQUIRE(handle.lookup_cc("8.8.8.8", cc, logs) == false);
});
}
TEST_CASE("When lookup_asn2 fails because MMDB provides us a bad value") {
MKMOCK_WITH_ENABLED_HOOK(
finish_lookup_asn_check, false,
{
std::vector<std::string> logs;
mk::mmdb::Handle handle;
std::string asn;
REQUIRE(handle.open(".mkbuild/download/asn.mmdb", logs) == true);
REQUIRE(handle.lookup_asn2("8.8.8.8", asn, logs) == false);
});
}
TEST_CASE("When lookup_org fails because MMDB provides us a bad value") {
MKMOCK_WITH_ENABLED_HOOK(
finish_lookup_org_check, false,
{
std::vector<std::string> logs;
mk::mmdb::Handle handle;
std::string org;
REQUIRE(handle.open(".mkbuild/download/asn.mmdb", logs) == true);
REQUIRE(handle.lookup_org("8.8.8.8", org, logs) == false);
});
}
TEST_CASE("MKMMDB_ABORT_IF_NULLPTR works as expected") {
REQUIRE_THROWS([]() {
const char *p = nullptr;
MKMMDB_ABORT_IF_NULLPTR(p);
}());
}