|
38 | 38 | import static java.util.Map.entry; |
39 | 39 | import static org.elasticsearch.ingest.geoip.GeoIpTestUtils.copyDatabase; |
40 | 40 | import static org.elasticsearch.ingest.geoip.IpinfoIpDataLookups.parseAsn; |
| 41 | +import static org.elasticsearch.ingest.geoip.IpinfoIpDataLookups.parseBoolean; |
41 | 42 | import static org.elasticsearch.ingest.geoip.IpinfoIpDataLookups.parseLocationDouble; |
| 43 | +import static org.hamcrest.Matchers.anyOf; |
42 | 44 | import static org.hamcrest.Matchers.empty; |
43 | 45 | import static org.hamcrest.Matchers.equalTo; |
44 | 46 | import static org.hamcrest.Matchers.is; |
@@ -93,6 +95,21 @@ public void testParseAsn() { |
93 | 95 | assertThat(parseAsn("anythingelse"), nullValue()); |
94 | 96 | } |
95 | 97 |
|
| 98 | + public void testParseBoolean() { |
| 99 | + // expected cases: "true" is true and "" is false |
| 100 | + assertThat(parseBoolean("true"), equalTo(true)); |
| 101 | + assertThat(parseBoolean(""), equalTo(false)); |
| 102 | + assertThat(parseBoolean("false"), equalTo(false)); // future proofing |
| 103 | + // defensive case: null becomes null, this is not expected fwiw |
| 104 | + assertThat(parseBoolean(null), nullValue()); |
| 105 | + // defensive cases: we strip whitespace and ignore case |
| 106 | + assertThat(parseBoolean(" "), equalTo(false)); |
| 107 | + assertThat(parseBoolean(" TrUe "), equalTo(true)); |
| 108 | + assertThat(parseBoolean(" FaLSE "), equalTo(false)); |
| 109 | + // bottom case: a non-parsable string is null |
| 110 | + assertThat(parseBoolean(randomAlphaOfLength(8)), nullValue()); |
| 111 | + } |
| 112 | + |
96 | 113 | public void testParseLocationDouble() { |
97 | 114 | // expected case: "123.45" is 123.45 |
98 | 115 | assertThat(parseLocationDouble("123.45"), equalTo(123.45)); |
@@ -287,6 +304,76 @@ public void testGeolocationInvariants() { |
287 | 304 | } |
288 | 305 | } |
289 | 306 |
|
| 307 | + public void testPrivacyDetection() throws IOException { |
| 308 | + assumeFalse("https://github.com/elastic/elasticsearch/issues/114266", Constants.WINDOWS); |
| 309 | + Path configDir = tmpDir; |
| 310 | + copyDatabase("ipinfo/privacy_detection_sample.mmdb", configDir.resolve("privacy_detection_sample.mmdb")); |
| 311 | + |
| 312 | + GeoIpCache cache = new GeoIpCache(1000); // real cache to test purging of entries upon a reload |
| 313 | + ConfigDatabases configDatabases = new ConfigDatabases(configDir, cache); |
| 314 | + configDatabases.initialize(resourceWatcherService); |
| 315 | + |
| 316 | + // testing the first row in the sample database |
| 317 | + try (DatabaseReaderLazyLoader loader = configDatabases.getDatabase("privacy_detection_sample.mmdb")) { |
| 318 | + IpDataLookup lookup = new IpinfoIpDataLookups.PrivacyDetection(Database.PrivacyDetection.properties()); |
| 319 | + Map<String, Object> data = lookup.getData(loader, "1.53.59.33"); |
| 320 | + assertThat( |
| 321 | + data, |
| 322 | + equalTo( |
| 323 | + Map.ofEntries( |
| 324 | + entry("ip", "1.53.59.33"), |
| 325 | + entry("hosting", false), |
| 326 | + entry("proxy", false), |
| 327 | + entry("relay", false), |
| 328 | + entry("tor", false), |
| 329 | + entry("vpn", true) |
| 330 | + ) |
| 331 | + ) |
| 332 | + ); |
| 333 | + } |
| 334 | + |
| 335 | + // testing a row with a non-empty service in the sample database |
| 336 | + try (DatabaseReaderLazyLoader loader = configDatabases.getDatabase("privacy_detection_sample.mmdb")) { |
| 337 | + IpDataLookup lookup = new IpinfoIpDataLookups.PrivacyDetection(Database.PrivacyDetection.properties()); |
| 338 | + Map<String, Object> data = lookup.getData(loader, "216.131.74.65"); |
| 339 | + assertThat( |
| 340 | + data, |
| 341 | + equalTo( |
| 342 | + Map.ofEntries( |
| 343 | + entry("ip", "216.131.74.65"), |
| 344 | + entry("hosting", true), |
| 345 | + entry("proxy", false), |
| 346 | + entry("service", "FastVPN"), |
| 347 | + entry("relay", false), |
| 348 | + entry("tor", false), |
| 349 | + entry("vpn", true) |
| 350 | + ) |
| 351 | + ) |
| 352 | + ); |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + public void testPrivacyDetectionInvariants() { |
| 357 | + assumeFalse("https://github.com/elastic/elasticsearch/issues/114266", Constants.WINDOWS); |
| 358 | + Path configDir = tmpDir; |
| 359 | + copyDatabase("ipinfo/privacy_detection_sample.mmdb", configDir.resolve("privacy_detection_sample.mmdb")); |
| 360 | + |
| 361 | + { |
| 362 | + final Set<String> expectedColumns = Set.of("network", "service", "hosting", "proxy", "relay", "tor", "vpn"); |
| 363 | + |
| 364 | + Path databasePath = configDir.resolve("privacy_detection_sample.mmdb"); |
| 365 | + assertDatabaseInvariants(databasePath, (ip, row) -> { |
| 366 | + assertThat(row.keySet(), equalTo(expectedColumns)); |
| 367 | + |
| 368 | + for (String booleanColumn : Set.of("hosting", "proxy", "relay", "tor", "vpn")) { |
| 369 | + String bool = (String) row.get(booleanColumn); |
| 370 | + assertThat(bool, anyOf(equalTo("true"), equalTo(""), equalTo("false"))); |
| 371 | + assertThat(parseBoolean(bool), notNullValue()); |
| 372 | + } |
| 373 | + }); |
| 374 | + } |
| 375 | + } |
| 376 | + |
290 | 377 | private static void assertDatabaseInvariants(final Path databasePath, final BiConsumer<InetAddress, Map<String, Object>> rowConsumer) { |
291 | 378 | try (Reader reader = new Reader(pathToFile(databasePath))) { |
292 | 379 | Networks<?> networks = reader.networks(Map.class); |
|
0 commit comments