|
| 1 | +#include <maxminddb.h> |
| 2 | + |
| 3 | +#include <cstdio> |
| 4 | +#include <filesystem> |
| 5 | +#include <stdexcept> |
| 6 | +#include <string_view> |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +namespace fs = std::filesystem; |
| 11 | + |
| 12 | +class Database { |
| 13 | +public: |
| 14 | + explicit Database(const fs::path& path) { |
| 15 | + const int status = MMDB_open(path.string().c_str(), MMDB_MODE_MMAP, &database); |
| 16 | + if (status != MMDB_SUCCESS) throw std::runtime_error(MMDB_strerror(status)); |
| 17 | + } |
| 18 | + |
| 19 | + ~Database() { MMDB_close(&database); } |
| 20 | + |
| 21 | + Database(const Database&) = delete; |
| 22 | + Database& operator=(const Database&) = delete; |
| 23 | + |
| 24 | + const MMDB_metadata_s& metadata() const { return database.metadata; } |
| 25 | + |
| 26 | + MMDB_entry_s lookup(const char* address) const { |
| 27 | + int address_error{}; |
| 28 | + int database_error{}; |
| 29 | + auto result = MMDB_lookup_string(&database, address, &address_error, &database_error); |
| 30 | + if (address_error || database_error != MMDB_SUCCESS || !result.found_entry) { |
| 31 | + throw std::runtime_error(address); |
| 32 | + } |
| 33 | + return result.entry; |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + MMDB_s database{}; |
| 38 | +}; |
| 39 | + |
| 40 | +void expect_country(const Database& database, const char* address, std::string_view expected) { |
| 41 | + auto entry = database.lookup(address); |
| 42 | + MMDB_entry_data_s country{}; |
| 43 | + if (MMDB_get_value(&entry, &country, "country", "iso_code", nullptr) != MMDB_SUCCESS || |
| 44 | + !country.has_data || country.type != MMDB_DATA_TYPE_UTF8_STRING || |
| 45 | + std::string_view(country.utf8_string, country.data_size) != expected) { |
| 46 | + throw std::runtime_error(address); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void expect_asn(const Database& database, const char* address, uint32_t expected) { |
| 51 | + auto entry = database.lookup(address); |
| 52 | + MMDB_entry_data_s asn{}; |
| 53 | + if (MMDB_get_value(&entry, &asn, "autonomous_system_number", nullptr) != MMDB_SUCCESS || |
| 54 | + !asn.has_data || asn.type != MMDB_DATA_TYPE_UINT32 || asn.uint32 != expected) { |
| 55 | + throw std::runtime_error(address); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +int main() try { |
| 62 | +#ifdef _WIN32 |
| 63 | + WSADATA winsock{}; |
| 64 | + if (WSAStartup(MAKEWORD(2, 2), &winsock) != 0) return 1; |
| 65 | +#endif |
| 66 | + const auto fixtures = fs::path(__FILE__).parent_path().parent_path() / "fixtures"; |
| 67 | + const Database country(fixtures / "GeoIP2-Country-Test.mmdb"); |
| 68 | + const Database asn(fixtures / "GeoLite2-ASN-Test.mmdb"); |
| 69 | + |
| 70 | + expect_country(country, "2.125.160.216", "GB"); |
| 71 | + expect_country(country, "2001:218::1", "JP"); |
| 72 | + expect_asn(asn, "1.0.0.1", 15169); |
| 73 | + expect_asn(asn, "2001:1700::1", 6730); |
| 74 | + const Database metadata(fixtures / "MaxMind-DB-test-ipv4-24.mmdb"); |
| 75 | + const auto& languages = metadata.metadata().languages; |
| 76 | + if (std::string_view(country.metadata().database_type) != "GeoIP2-Country" || |
| 77 | + std::string_view(asn.metadata().database_type) != "GeoLite2-ASN" || |
| 78 | + languages.count != 2 || |
| 79 | + std::string_view(languages.names[0]) != "en" || |
| 80 | + std::string_view(languages.names[1]) != "zh") { |
| 81 | + throw std::runtime_error("unexpected database metadata"); |
| 82 | + } |
| 83 | +#ifdef _WIN32 |
| 84 | + WSACleanup(); |
| 85 | +#endif |
| 86 | + std::printf("libmaxminddb %s: IPv4, IPv6, country codes, ASNs and language metadata passed\n", |
| 87 | + MMDB_lib_version()); |
| 88 | + return 0; |
| 89 | +} catch (const std::exception& error) { |
| 90 | + std::fprintf(stderr, "MMDB query failed: %s\n", error.what()); |
| 91 | + return 1; |
| 92 | +} |
0 commit comments