Copilot commented on code in PR #12948:
URL: https://github.com/apache/trafficserver/pull/12948#discussion_r2920348031
##########
plugins/header_rewrite/header_rewrite_test.cc:
##########
@@ -538,12 +545,132 @@ test_tokenizer()
return errors;
}
+#if TS_USE_HRW_MAXMINDDB
+static bool
+file_exists(const char *path)
+{
+ struct stat st;
+ return stat(path, &st) == 0;
+}
+
+static const char *
+find_country_mmdb()
+{
+ static const char *paths[] = {
+ "/usr/share/GeoIP/GeoLite2-Country.mmdb",
"/usr/local/share/GeoIP/GeoLite2-Country.mmdb",
+ "/var/lib/GeoIP/GeoLite2-Country.mmdb",
"/opt/geoip/GeoLite2-Country.mmdb",
+ "/usr/share/GeoIP/GeoIP2-Country.mmdb",
"/usr/share/GeoIP/dbip-country-lite.mmdb",
+ };
+ for (auto *p : paths) {
+ if (file_exists(p)) {
+ return p;
+ }
+ }
+ if (const char *env = getenv("MMDB_COUNTRY_PATH")) {
+ if (file_exists(env)) {
+ return env;
+ }
+ }
+ return nullptr;
+}
+
+int
+test_maxmind_geo()
+{
+ const char *db_path = find_country_mmdb();
+ if (db_path == nullptr) {
+ std::cout << "SKIP: No MaxMind country mmdb found (set MMDB_COUNTRY_PATH
to override)" << std::endl;
+ return 0;
Review Comment:
The skip message says to set `MMDB_COUNTRY_PATH` “to override”, but
`find_country_mmdb()` only consults the env var after checking the built-in
paths. Either check `MMDB_COUNTRY_PATH` first (so it truly overrides), or
adjust the message to reflect the actual precedence.
##########
plugins/header_rewrite/header_rewrite_test.cc:
##########
@@ -538,12 +545,132 @@ test_tokenizer()
return errors;
}
+#if TS_USE_HRW_MAXMINDDB
+static bool
+file_exists(const char *path)
+{
+ struct stat st;
+ return stat(path, &st) == 0;
+}
+
+static const char *
+find_country_mmdb()
+{
+ static const char *paths[] = {
+ "/usr/share/GeoIP/GeoLite2-Country.mmdb",
"/usr/local/share/GeoIP/GeoLite2-Country.mmdb",
+ "/var/lib/GeoIP/GeoLite2-Country.mmdb",
"/opt/geoip/GeoLite2-Country.mmdb",
+ "/usr/share/GeoIP/GeoIP2-Country.mmdb",
"/usr/share/GeoIP/dbip-country-lite.mmdb",
+ };
+ for (auto *p : paths) {
+ if (file_exists(p)) {
+ return p;
+ }
+ }
+ if (const char *env = getenv("MMDB_COUNTRY_PATH")) {
+ if (file_exists(env)) {
+ return env;
+ }
+ }
+ return nullptr;
+}
+
+int
+test_maxmind_geo()
+{
+ const char *db_path = find_country_mmdb();
+ if (db_path == nullptr) {
+ std::cout << "SKIP: No MaxMind country mmdb found (set MMDB_COUNTRY_PATH
to override)" << std::endl;
+ return 0;
+ }
+
+ std::cout << "Testing MaxMind geo lookups with: " << db_path << std::endl;
+
+ int errors = 0;
+ MMDB_s mmdb;
+ int status = MMDB_open(db_path, MMDB_MODE_MMAP, &mmdb);
+ if (MMDB_SUCCESS != status) {
+ std::cerr << "Cannot open " << db_path << ": " << MMDB_strerror(status) <<
std::endl;
+ return 1;
+ }
+
+ // MMDB_lookup_string() returns two independent error codes:
+ // gai_error - getaddrinfo() failure (string-to-IP conversion)
+ // mmdb_error - MMDB lookup failure (database query)
+ // Check both to avoid masking failures.
+ int gai_error, mmdb_error;
+ MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "8.8.8.8",
&gai_error, &mmdb_error);
+
+ if (gai_error != 0) {
+ std::cerr << "getaddrinfo failed for 8.8.8.8: " << gai_strerror(gai_error)
<< std::endl;
+ MMDB_close(&mmdb);
+ return 1;
+ }
+ if (MMDB_SUCCESS != mmdb_error || !result.found_entry) {
+ std::cerr << "Cannot look up 8.8.8.8 in " << db_path << ": " <<
MMDB_strerror(mmdb_error) << std::endl;
+ MMDB_close(&mmdb);
+ return 1;
+ }
+
+ MMDB_entry_data_s entry_data;
+
+ // Verify "country" -> "iso_code" path (used by GEO_QUAL_COUNTRY)
+ status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code",
NULL);
+ if (MMDB_SUCCESS != status || !entry_data.has_data || entry_data.type !=
MMDB_DATA_TYPE_UTF8_STRING) {
+ std::cerr << "FAIL: country/iso_code lookup failed for 8.8.8.8" <<
std::endl;
+ ++errors;
+ } else {
+ std::string iso(entry_data.utf8_string, entry_data.data_size);
+ if (iso != "US") {
+ std::cerr << "FAIL: expected country iso_code 'US' for 8.8.8.8, got '"
<< iso << "'" << std::endl;
+ ++errors;
+ } else {
Review Comment:
This test hard-codes that `8.8.8.8` must map to country `US`. That can vary
across different .mmdb vendors/editions and over time as databases update,
which can make CI flaky when a database is present. Consider asserting only
structural expectations (e.g., iso_code is a 2-letter UTF-8 string) or making
the expected ISO code configurable via an env var alongside `MMDB_COUNTRY_PATH`.
##########
plugins/header_rewrite/conditions_geo_maxmind.cc:
##########
@@ -51,6 +51,7 @@ MMConditionGeo::initLibrary(const std::string &path)
if (MMDB_SUCCESS != status) {
Dbg(pi_dbg_ctl, "Cannot open %s - %s", path.c_str(),
MMDB_strerror(status));
delete gMaxMindDB;
+ gMaxMindDB = nullptr; // allow retry on next call instead of dangling
pointer
Review Comment:
The new comment says this `nullptr` assignment “allow[s] retry on next
call”, but `MMConditionGeo::initLibrary()` is invoked from `initHRWLibraries()`
under `std::call_once` (header_rewrite.cc), so there won’t actually be a retry.
Suggest rewording the comment to focus on the real benefit here: avoiding a
dangling global pointer after `delete`.
```suggestion
gMaxMindDB = nullptr; // avoid leaving a dangling global pointer after
delete
```
##########
plugins/header_rewrite/header_rewrite_test.cc:
##########
@@ -538,12 +545,132 @@ test_tokenizer()
return errors;
}
+#if TS_USE_HRW_MAXMINDDB
+static bool
+file_exists(const char *path)
+{
+ struct stat st;
+ return stat(path, &st) == 0;
Review Comment:
`file_exists()` returns true for any filesystem object (including
directories). If one of these paths exists as a directory/symlink,
`MMDB_open()` will then fail and the test will hard-fail. Consider checking
`S_ISREG(st.st_mode)` (and possibly resolving symlinks) so only actual MMDB
files are accepted.
```suggestion
if (stat(path, &st) != 0) {
return false;
}
return S_ISREG(st.st_mode);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]