Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ public class IpInfoAnonymousResult implements AnonymousResult {
private final boolean hostingProvider;
private final boolean proxy;
private final boolean relay;
private final String service;

@MaxMindDbConstructor
public IpInfoAnonymousResult(
@MaxMindDbParameter(name = "vpn") String vpn,
@MaxMindDbParameter(name = "tor") String torExitNode,
@MaxMindDbParameter(name = "relay") String relay,
@MaxMindDbParameter(name = "proxy") String proxy,
@MaxMindDbParameter(name = "hosting") String hostingProvider) {
@MaxMindDbParameter(name = "hosting") String hostingProvider,
@MaxMindDbParameter(name = "service") String service) {
this.vpn = Boolean.parseBoolean(vpn);
this.torExitNode = Boolean.parseBoolean(torExitNode);
this.relay = Boolean.parseBoolean(relay);
this.proxy = Boolean.parseBoolean(proxy);
this.hostingProvider = Boolean.parseBoolean(hostingProvider);
this.service = service;
}

@Override
Expand All @@ -33,7 +36,7 @@ public boolean hostingProvider() {

@Override
public boolean vpn() {
return vpn;
return vpn && (service != null && !service.isBlank());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
public class IpInfoMmdbTestIT {
private static final Path cityDatabase =
Paths.get(
"/Users/gluiz/Downloads/ipinfo/standard_location/mmdb/standard_location-20230210.mmdb");
"/Users/gluiz/Downloads/ipinfo_static/standard_location/mmdb/standard_location-20230210.mmdb");
private static final Path anonymousDatabase =
Paths.get("/Users/gluiz/Downloads/ipinfo/privacy/mmdb/privacy_detection_sample.mmdb");
Paths.get("/Users/gluiz/Downloads/ipinfo_static/privacy/mmdb/privacy-20230209.mmdb");

@Test
public void lookupWithCityDatabase() throws IOException {
Expand Down Expand Up @@ -58,7 +58,7 @@ public void lookupWithCityDatabase() throws IOException {
}

@Test
public void lookupWithCityDatabaseAndAnonymous() throws IOException {
public void lookupWithCityDatabaseAndAnonymousEmptyServiceName() throws IOException {
final IP ipToLookup = new IP(InetAddress.getByName("110.142.177.68"));
final IPResolver build =
new Builder()
Expand All @@ -75,7 +75,7 @@ public void lookupWithCityDatabaseAndAnonymous() throws IOException {
final IpInformation expected =
IpInformation.builder()
.withCountryCodeAlpha2("AU")
.isVpn(true)
.isVpn(false)
.withPostcode("3061")
.withCityGeonameId("2158177")
.withCity("Melbourne")
Expand All @@ -86,4 +86,33 @@ public void lookupWithCityDatabaseAndAnonymous() throws IOException {
.build();
assertEquals(Optional.of(expected), resolve.get(new ProviderKey("custom") {}));
}

@Test
public void lookupWithCityDatabaseAndAnonymousWithServiceName() throws IOException {
final IP ipToLookup = new IP(InetAddress.getByName("31.171.154.78"));
final IPResolver build =
new Builder()
.withProvider(
new MmdbDatabase(
"custom",
new IpInfoLocationSource(cityDatabase),
new IpInfoAnonymousSource(anonymousDatabase)))
.build();

final Map<ProviderKey, Optional<IpInformation>> resolve = build.resolve(ipToLookup);
assertEquals(1, resolve.size());
assertTrue(resolve.containsKey(new ProviderKey("custom") {}));
final IpInformation expected =
IpInformation.builder()
.withCountryCodeAlpha2("AL")
.isVpn(true)
.withCityGeonameId("3183875")
.withCity("Tirana")
.withLeastSpecificDivision("Tirana")
.withMostSpecificDivision("Tirana")
.withStartOfRange(ipToLookup)
.withEndOfRange(ipToLookup)
.build();
assertEquals(Optional.of(expected), resolve.get(new ProviderKey("custom") {}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package technology.dice.dicewhere.building.mmdb.ipinfo;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class IpInfoAnonymousResultTest {

@Test
public void testVpnWithValidServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("true", "false", "false", "false", "false", "ExpressVPN");

assertTrue(
result.vpn(), "Should be detected as VPN when vpn=true and service name is provided");
}

@Test
public void testVpnWithEmptyServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("true", "false", "false", "false", "false", "");

assertFalse(result.vpn(), "Should not be detected as VPN when service name is empty");
}

@Test
public void testVpnWithBlankServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("true", "false", "false", "false", "false", " ");

assertFalse(result.vpn(), "Should not be detected as VPN when service name is blank");
}

@Test
public void testVpnWithNullServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("true", "false", "false", "false", "false", null);

assertFalse(result.vpn(), "Should not be detected as VPN when service name is null");
}

@Test
public void testVpnFalseWithServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "ExpressVPN");

assertFalse(
result.vpn(), "Should not be detected as VPN when vpn=false regardless of service name");
}

@Test
public void testVpnFalseWithoutServiceName() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertFalse(result.vpn(), "Should not be detected as VPN when vpn=false and no service name");
}

@Test
public void testHostingProvider() {
IpInfoAnonymousResult resultTrue =
new IpInfoAnonymousResult("false", "false", "false", "false", "true", "");
IpInfoAnonymousResult resultFalse =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertTrue(resultTrue.hostingProvider(), "Should detect hosting provider when true");
assertFalse(resultFalse.hostingProvider(), "Should not detect hosting provider when false");
}

@Test
public void testTorExitNode() {
IpInfoAnonymousResult resultTrue =
new IpInfoAnonymousResult("false", "true", "false", "false", "false", "");
IpInfoAnonymousResult resultFalse =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertTrue(resultTrue.torExitNode(), "Should detect Tor exit node when true");
assertFalse(resultFalse.torExitNode(), "Should not detect Tor exit node when false");
}

@Test
public void testResidentialProxy() {
IpInfoAnonymousResult resultTrue =
new IpInfoAnonymousResult("false", "false", "true", "false", "false", "");
IpInfoAnonymousResult resultFalse =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertTrue(resultTrue.residentialProxy(), "Should detect residential proxy when relay=true");
assertFalse(
resultFalse.residentialProxy(), "Should not detect residential proxy when relay=false");
}

@Test
public void testPublicProxy() {
IpInfoAnonymousResult resultTrue =
new IpInfoAnonymousResult("false", "false", "false", "true", "false", "");
IpInfoAnonymousResult resultFalse =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertTrue(resultTrue.publicProxy(), "Should detect public proxy when true");
assertFalse(resultFalse.publicProxy(), "Should not detect public proxy when false");
}

@Test
public void testAllFieldsTrue() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("true", "true", "true", "true", "true", "TestService");

assertTrue(result.vpn(), "Should be detected as VPN with service name");
assertTrue(result.torExitNode(), "Should detect Tor exit node");
assertTrue(result.residentialProxy(), "Should detect residential proxy");
assertTrue(result.publicProxy(), "Should detect public proxy");
assertTrue(result.hostingProvider(), "Should detect hosting provider");
}

@Test
public void testAllFieldsFalse() {
IpInfoAnonymousResult result =
new IpInfoAnonymousResult("false", "false", "false", "false", "false", "");

assertFalse(result.vpn(), "Should not be detected as VPN");
assertFalse(result.torExitNode(), "Should not detect Tor exit node");
assertFalse(result.residentialProxy(), "Should not detect residential proxy");
assertFalse(result.publicProxy(), "Should not detect public proxy");
assertFalse(result.hostingProvider(), "Should not detect hosting provider");
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<guava.version>21.0</guava.version>
<ipaddress.version>4.2.0</ipaddress.version>
<mapdb.version>3.0.7</mapdb.version>
<mapdb.version>3.0.10</mapdb.version>
<protobuf.version>3.21.12</protobuf.version>
<junit.version>5.9.2</junit.version>
<mockito.version>5.1.1</mockito.version>
Expand Down