|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertThrows; |
8 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 9 |
|
| 10 | +import com.maxmind.db.Networks; |
10 | 11 | import com.maxmind.db.Reader; |
11 | 12 | import com.maxmind.geoip2.exception.AddressNotFoundException; |
12 | 13 | import com.maxmind.geoip2.exception.GeoIp2Exception; |
|
27 | 28 | import java.net.InetAddress; |
28 | 29 | import java.net.URISyntaxException; |
29 | 30 | import java.net.URL; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Paths; |
30 | 33 | import java.util.Arrays; |
31 | 34 | import org.junit.jupiter.api.BeforeEach; |
32 | 35 | import org.junit.jupiter.api.Test; |
@@ -475,4 +478,84 @@ private File getFile(String filename) throws URISyntaxException { |
475 | 478 | .getResource("/maxmind-db/test-data/" + filename); |
476 | 479 | return new File(resource.toURI()); |
477 | 480 | } |
| 481 | + |
| 482 | + /** |
| 483 | + * Tests that all records in each test database can be deserialized. |
| 484 | + * This test iterates over every network in the database and performs |
| 485 | + * a lookup to ensure no deserialization errors occur. |
| 486 | + * |
| 487 | + * <p>Based on the reproduction test from GitHub issue #644. |
| 488 | + * https://github.com/maxmind/GeoIP2-java/issues/644 |
| 489 | + */ |
| 490 | + @Test |
| 491 | + public void testAllRecordsDeserialize() throws Exception { |
| 492 | + var testDataDir = Paths.get( |
| 493 | + getClass().getResource("/maxmind-db/test-data").toURI() |
| 494 | + ); |
| 495 | + |
| 496 | + try (var stream = Files.newDirectoryStream(testDataDir, "*.mmdb")) { |
| 497 | + for (var dbPath : stream) { |
| 498 | + var filename = dbPath.getFileName().toString(); |
| 499 | + |
| 500 | + if (shouldSkipDatabase(filename)) { |
| 501 | + continue; |
| 502 | + } |
| 503 | + |
| 504 | + try (var reader = new Reader(dbPath.toFile()); |
| 505 | + var dbReader = new DatabaseReader.Builder(dbPath.toFile()).build()) { |
| 506 | + |
| 507 | + var dbType = reader.getMetadata().databaseType(); |
| 508 | + var networks = reader.networks(Object.class); |
| 509 | + |
| 510 | + while (networks.hasNext()) { |
| 511 | + var ip = networks.next().network().networkAddress(); |
| 512 | + lookupByDatabaseType(dbReader, dbType, ip); |
| 513 | + } |
| 514 | + } |
| 515 | + } |
| 516 | + } |
| 517 | + } |
| 518 | + |
| 519 | + private boolean shouldSkipDatabase(String filename) { |
| 520 | + if (filename.startsWith("MaxMind-DB-")) { |
| 521 | + return true; |
| 522 | + } |
| 523 | + if (filename.contains("Broken") || filename.contains("Invalid")) { |
| 524 | + return true; |
| 525 | + } |
| 526 | + // These database types don't have model classes in GeoIP2-java |
| 527 | + if (filename.contains("DensityIncome") |
| 528 | + || filename.contains("User-Count") |
| 529 | + || filename.contains("Static-IP-Score")) { |
| 530 | + return true; |
| 531 | + } |
| 532 | + return false; |
| 533 | + } |
| 534 | + |
| 535 | + private void lookupByDatabaseType(DatabaseReader reader, String dbType, InetAddress ip) |
| 536 | + throws IOException, GeoIp2Exception { |
| 537 | + if (dbType.contains("City")) { |
| 538 | + reader.city(ip); |
| 539 | + } else if (dbType.contains("Country")) { |
| 540 | + reader.country(ip); |
| 541 | + } else if (dbType.contains("Enterprise")) { |
| 542 | + reader.enterprise(ip); |
| 543 | + } else if (dbType.equals("GeoIP-Anonymous-Plus")) { |
| 544 | + reader.anonymousPlus(ip); |
| 545 | + } else if (dbType.equals("GeoIP2-Anonymous-IP")) { |
| 546 | + reader.anonymousIp(ip); |
| 547 | + } else if (dbType.equals("GeoIP2-ISP")) { |
| 548 | + reader.isp(ip); |
| 549 | + } else if (dbType.equals("GeoIP2-IP-Risk")) { |
| 550 | + reader.ipRisk(ip); |
| 551 | + } else if (dbType.equals("GeoIP2-Domain")) { |
| 552 | + reader.domain(ip); |
| 553 | + } else if (dbType.equals("GeoLite2-ASN")) { |
| 554 | + reader.asn(ip); |
| 555 | + } else if (dbType.equals("GeoIP2-Connection-Type")) { |
| 556 | + reader.connectionType(ip); |
| 557 | + } else { |
| 558 | + throw new IllegalArgumentException("Unknown database type: " + dbType); |
| 559 | + } |
| 560 | + } |
478 | 561 | } |
0 commit comments