1414import com .maxmind .db .Reader ;
1515
1616import org .apache .lucene .util .Constants ;
17+ import org .elasticsearch .common .network .InetAddresses ;
1718import org .elasticsearch .common .network .NetworkAddress ;
1819import org .elasticsearch .core .IOUtils ;
20+ import org .elasticsearch .core .Strings ;
1921import org .elasticsearch .core .SuppressForbidden ;
2022import org .elasticsearch .test .ESTestCase ;
2123import org .junit .After ;
@@ -113,7 +115,10 @@ public void testAsnFree() {
113115 entry ("asn" , 16625L ),
114116 entry ("network" , "23.32.184.0/21" ),
115117 entry ("domain" , "akamai.com" )
116- )
118+ ),
119+ Map .ofEntries (entry ("name" , "organization_name" ), entry ("asn" , "asn" ), entry ("network" , "network" ), entry ("domain" , "domain" )),
120+ Set .of ("ip" ),
121+ Set .of ()
117122 );
118123 }
119124
@@ -133,7 +138,17 @@ public void testAsnStandard() {
133138 entry ("domain" , "tpx.com" ),
134139 entry ("type" , "hosting" ),
135140 entry ("country_iso_code" , "US" )
136- )
141+ ),
142+ Map .ofEntries (
143+ entry ("name" , "organization_name" ),
144+ entry ("asn" , "asn" ),
145+ entry ("network" , "network" ),
146+ entry ("domain" , "domain" ),
147+ entry ("country" , "country_iso_code" ),
148+ entry ("type" , "type" )
149+ ),
150+ Set .of ("ip" ),
151+ Set .of ()
137152 );
138153 }
139154
@@ -188,7 +203,16 @@ public void testCountryFree() {
188203 entry ("country_iso_code" , "IE" ),
189204 entry ("continent_name" , "Europe" ),
190205 entry ("continent_code" , "EU" )
191- )
206+ ),
207+ Map .ofEntries (
208+ entry ("continent_name" , "continent_name" ),
209+ entry ("continent" , "continent_code" ),
210+ entry ("country" , "country_iso_code" ),
211+ entry ("country_name" , "country_name" ),
212+ entry ("type" , "type" )
213+ ),
214+ Set .of ("ip" ),
215+ Set .of ("network" )
192216 );
193217 }
194218
@@ -208,7 +232,18 @@ public void testGeolocationStandard() {
208232 entry ("timezone" , "Europe/London" ),
209233 entry ("postal_code" , "E1W" ),
210234 entry ("location" , Map .of ("lat" , 51.50853 , "lon" , -0.12574 ))
211- )
235+ ),
236+ Map .ofEntries (
237+ entry ("country" , "country_iso_code" ),
238+ entry ("region" , "region_name" ),
239+ entry ("city" , "city_name" ),
240+ entry ("timezone" , "timezone" ),
241+ entry ("postal_code" , "postal_code" ),
242+ entry ("lat" , "location" ),
243+ entry ("lng" , "location" )
244+ ),
245+ Set .of ("ip" , "location" ),
246+ Set .of ("geoname_id" , "region_code" )
212247 );
213248 }
214249
@@ -266,7 +301,16 @@ public void testPrivacyDetectionStandard() {
266301 entry ("relay" , false ),
267302 entry ("tor" , false ),
268303 entry ("vpn" , true )
269- )
304+ ),
305+ Map .ofEntries (
306+ entry ("hosting" , "hosting" ),
307+ entry ("proxy" , "proxy" ),
308+ entry ("relay" , "relay" ),
309+ entry ("tor" , "tor" ),
310+ entry ("vpn" , "vpn" )
311+ ),
312+ Set .of ("ip" ),
313+ Set .of ("network" , "service" )
270314 );
271315 }
272316
@@ -286,7 +330,17 @@ public void testPrivacyDetectionStandardNonEmptyService() {
286330 entry ("relay" , false ),
287331 entry ("tor" , false ),
288332 entry ("vpn" , true )
289- )
333+ ),
334+ Map .ofEntries (
335+ entry ("hosting" , "hosting" ),
336+ entry ("proxy" , "proxy" ),
337+ entry ("service" , "service" ),
338+ entry ("relay" , "relay" ),
339+ entry ("tor" , "tor" ),
340+ entry ("vpn" , "vpn" )
341+ ),
342+ Set .of ("ip" ),
343+ Set .of ("network" )
290344 );
291345 }
292346
@@ -438,7 +492,15 @@ private static File pathToFile(Path databasePath) {
438492 return databasePath .toFile ();
439493 }
440494
441- private void assertExpectedLookupResults (String databaseName , String ip , IpDataLookup lookup , Map <String , Object > expected ) {
495+ private void assertExpectedLookupResults (
496+ String databaseName ,
497+ String ip ,
498+ IpDataLookup lookup ,
499+ Map <String , Object > expected ,
500+ Map <String , String > keyMappings ,
501+ Set <String > knownAdditionalKeys ,
502+ Set <String > knownMissingKeys
503+ ) {
442504 try (DatabaseReaderLazyLoader loader = loader (databaseName )) {
443505 Map <String , Object > actual = lookup .getData (loader , ip );
444506 assertThat (
@@ -449,13 +511,50 @@ private void assertExpectedLookupResults(String databaseName, String ip, IpDataL
449511 for (Map .Entry <String , Object > entry : expected .entrySet ()) {
450512 assertThat ("Unexpected value for key [" + entry .getKey () + "]" , actual .get (entry .getKey ()), equalTo (entry .getValue ()));
451513 }
514+ assertActualResultsMatchReader (actual , databaseName , ip , keyMappings , knownAdditionalKeys , knownMissingKeys );
452515 } catch (AssertionError e ) {
453516 fail (e , "Assert failed for database [%s] with address [%s]" , databaseName , ip );
454517 } catch (Exception e ) {
455518 fail (e , "Exception for database [%s] with address [%s]" , databaseName , ip );
456519 }
457520 }
458521
522+ private void assertActualResultsMatchReader (
523+ Map <String , Object > actual ,
524+ String databaseName ,
525+ String ip ,
526+ Map <String , String > keyMappings ,
527+ Set <String > knownAdditionalKeys ,
528+ Set <String > knownMissingKeys
529+ ) throws IOException {
530+ Path databasePath = tmpDir .resolve (databaseName );
531+ try (Reader reader = new Reader (pathToFile (databasePath ))) {
532+ @ SuppressWarnings ("unchecked" )
533+ Map <String , Object > data = reader .get (InetAddresses .forString (ip ), Map .class );
534+ for (String key : data .keySet ()) {
535+ if (keyMappings .containsKey (key )) {
536+ assertTrue (
537+ Strings .format (
538+ "The reader returned key [%s] that is expected to map to key [%s], but [%s] did not appear in the "
539+ + "actual data" ,
540+ key ,
541+ keyMappings .get (key ),
542+ keyMappings .get (key )
543+ ),
544+ actual .containsKey (keyMappings .get (key ))
545+ );
546+ } else if (knownMissingKeys .contains (key ) == false ) {
547+ fail (null , "The reader returned unexpected key [%s]" , key );
548+ }
549+ }
550+ for (String key : actual .keySet ()) {
551+ if (keyMappings .containsValue (key ) == false && knownAdditionalKeys .contains (key ) == false ) {
552+ fail (null , "Unexpected key [%s] in results" , key );
553+ }
554+ }
555+ }
556+ }
557+
459558 private DatabaseReaderLazyLoader loader (final String databaseName ) {
460559 Path path = tmpDir .resolve (databaseName );
461560 copyDatabase ("ipinfo/" + databaseName , path ); // the ipinfo databases are prefixed on the test classpath
0 commit comments