Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Mar 26, 2021
1 parent 112b3c5 commit 551bc73
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
27 changes: 25 additions & 2 deletions com/ip2proxy/IP2Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private enum Modes {
private boolean LASTSEEN_ENABLED;
private boolean THREAT_ENABLED;

private static final String _ModuleVersion = "3.0.1";
private static final String _ModuleVersion = "3.0.2";

public IP2Proxy() {

Expand Down Expand Up @@ -157,6 +157,7 @@ public String GetDatabaseVersion() {
/**
* This function returns ans integer to state if it proxy.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return -1 if error, 0 if not a proxy, 1 if proxy except DCH and SES, 2 if proxy and either DCH or SES
*/
public int IsProxy(String IP) throws IOException {
Expand All @@ -166,6 +167,7 @@ public int IsProxy(String IP) throws IOException {
/**
* This function returns the country code.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Country code
*/
public String GetCountryShort(String IP) throws IOException {
Expand All @@ -175,6 +177,7 @@ public String GetCountryShort(String IP) throws IOException {
/**
* This function returns the country name.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Country name
*/
public String GetCountryLong(String IP) throws IOException {
Expand All @@ -184,6 +187,7 @@ public String GetCountryLong(String IP) throws IOException {
/**
* This function returns the region name.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Region name
*/
public String GetRegion(String IP) throws IOException {
Expand All @@ -193,6 +197,7 @@ public String GetRegion(String IP) throws IOException {
/**
* This function returns the city name.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return City name
*/
public String GetCity(String IP) throws IOException {
Expand All @@ -202,6 +207,7 @@ public String GetCity(String IP) throws IOException {
/**
* This function returns the ISP name.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return ISP name
*/
public String GetISP(String IP) throws IOException {
Expand All @@ -211,6 +217,7 @@ public String GetISP(String IP) throws IOException {
/**
* This function returns the proxy type.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Proxy type
*/
public String GetProxyType(String IP) throws IOException {
Expand All @@ -220,6 +227,7 @@ public String GetProxyType(String IP) throws IOException {
/**
* This function returns the domain.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Domain
*/
public String GetDomain(String IP) throws IOException {
Expand All @@ -229,6 +237,7 @@ public String GetDomain(String IP) throws IOException {
/**
* This function returns the usage type.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Proxy type
*/
public String GetUsageType(String IP) throws IOException {
Expand All @@ -238,6 +247,7 @@ public String GetUsageType(String IP) throws IOException {
/**
* This function returns the Autonomous System Number.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Autonomous System Number
*/
public String GetASN(String IP) throws IOException {
Expand All @@ -247,6 +257,7 @@ public String GetASN(String IP) throws IOException {
/**
* This function returns the Autonomous System name.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Autonomous System name
*/
public String GetAS(String IP) throws IOException {
Expand All @@ -256,6 +267,7 @@ public String GetAS(String IP) throws IOException {
/**
* This function returns number of days the proxy was last seen.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Number of days last seen
*/
public String GetLastSeen(String IP) throws IOException {
Expand All @@ -265,6 +277,7 @@ public String GetLastSeen(String IP) throws IOException {
/**
* This function returns the threat type of the proxy.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Threat type of the proxy
*/
public String GetThreat(String IP) throws IOException {
Expand All @@ -274,6 +287,7 @@ public String GetThreat(String IP) throws IOException {
/**
* This function returns proxy result.
* @param IP IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return Proxy result
*/
public ProxyResult GetAll(String IP) throws IOException {
Expand All @@ -282,6 +296,7 @@ public ProxyResult GetAll(String IP) throws IOException {

/**
* This function destroys the mapped bytes.
* @return 0 to indicate no errors
*/
public int Close() {
DestroyMappedBytes();
Expand Down Expand Up @@ -449,13 +464,20 @@ private boolean LoadBIN() throws IOException {
/**
* This function initialize the component with the BIN file path and IO mode.
* @param DatabasePath Path to the BIN database file
* @param IOMode Default is file IO
* @throws IOException If an input or output exception occurred
* @return -1 if encounter error else 0
*/
public int Open(String DatabasePath) throws IOException {
return Open(DatabasePath, IOModes.IP2PROXY_FILE_IO);
}

/**
* This function initialize the component with the BIN file path and IO mode.
* @param DatabasePath Path to the BIN database file
* @param IOMode Default is file IO
* @throws IOException If an input or output exception occurred
* @return -1 if encounter error else 0
*/
public int Open(String DatabasePath, IOModes IOMode) throws IOException {
if (_DBType == 0) {
_IPDatabasePath = DatabasePath;
Expand All @@ -479,6 +501,7 @@ public int Open(String DatabasePath, IOModes IOMode) throws IOException {
/**
* This function to query IP2Proxy data.
* @param IPAddress IP Address you wish to query
* @throws IOException If an input or output exception occurred
* @return IP2Proxy data
*/
public ProxyResult ProxyQuery(String IPAddress) throws IOException {
Expand Down
147 changes: 147 additions & 0 deletions test/java/IP2ProxyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.file.*;
import java.io.*;
import com.ip2proxy.*;

class IP2ProxyTest {
private static IP2Proxy Proxy;
private static ProxyResult All;
private static String binfile = "IP2PROXY-LITE-PX1.BIN";
private static String binfilepath;
private static String ip = "8.8.8.8";

@BeforeAll
static void Setup() {
Path binpath = Paths.get("src","test","resources", binfile);
binfilepath = binpath.toFile().getAbsolutePath();
}

@BeforeEach
void Init() {
Proxy = new IP2Proxy();
}

@Test
void TestOpenException() {
assertThrows(IOException.class, () -> {
Proxy.Open("dummy.bin");
});
}

@Test
void TestQueryIsProxy() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Is_Proxy, 0);
}
}

@Test
void TestQueryProxyType() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Proxy_Type, "NOT SUPPORTED");
}
}

@Test
void TestQueryCountryShort() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Country_Short, "-");
}
}

@Test
void TestQueryCountryLong() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Country_Long, "-");
}
}

@Test
void TestQueryRegion() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Region, "NOT SUPPORTED");
}
}

@Test
void TestQueryCity() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.City, "NOT SUPPORTED");
}
}

@Test
void TestQueryISP() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.ISP, "NOT SUPPORTED");
}
}

@Test
void TestQueryDomain() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Domain, "NOT SUPPORTED");
}
}

@Test
void TestQueryUsageType() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Usage_Type, "NOT SUPPORTED");
}
}

@Test
void TestQueryASN() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.ASN, "NOT SUPPORTED");
}
}

@Test
void TestQueryAS() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.AS, "NOT SUPPORTED");
}
}

@Test
void TestQueryLastSeen() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Last_Seen, "NOT SUPPORTED");
}
}

@Test
void TestQueryThreat() throws IOException {
if (Proxy.Open(binfilepath) == 0) {
All = Proxy.GetAll(ip);
assertEquals(All.Threat, "NOT SUPPORTED");
}
}

@AfterEach
void TearDown() {
Proxy.Close();
Proxy = null;
}
}


Binary file added test/resources/IP2PROXY-LITE-PX1.BIN
Binary file not shown.

0 comments on commit 551bc73

Please sign in to comment.