Skip to content

Commit 551bc73

Browse files
committed
Added unit test
1 parent 112b3c5 commit 551bc73

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed

com/ip2proxy/IP2Proxy.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private enum Modes {
119119
private boolean LASTSEEN_ENABLED;
120120
private boolean THREAT_ENABLED;
121121

122-
private static final String _ModuleVersion = "3.0.1";
122+
private static final String _ModuleVersion = "3.0.2";
123123

124124
public IP2Proxy() {
125125

@@ -157,6 +157,7 @@ public String GetDatabaseVersion() {
157157
/**
158158
* This function returns ans integer to state if it proxy.
159159
* @param IP IP Address you wish to query
160+
* @throws IOException If an input or output exception occurred
160161
* @return -1 if error, 0 if not a proxy, 1 if proxy except DCH and SES, 2 if proxy and either DCH or SES
161162
*/
162163
public int IsProxy(String IP) throws IOException {
@@ -166,6 +167,7 @@ public int IsProxy(String IP) throws IOException {
166167
/**
167168
* This function returns the country code.
168169
* @param IP IP Address you wish to query
170+
* @throws IOException If an input or output exception occurred
169171
* @return Country code
170172
*/
171173
public String GetCountryShort(String IP) throws IOException {
@@ -175,6 +177,7 @@ public String GetCountryShort(String IP) throws IOException {
175177
/**
176178
* This function returns the country name.
177179
* @param IP IP Address you wish to query
180+
* @throws IOException If an input or output exception occurred
178181
* @return Country name
179182
*/
180183
public String GetCountryLong(String IP) throws IOException {
@@ -184,6 +187,7 @@ public String GetCountryLong(String IP) throws IOException {
184187
/**
185188
* This function returns the region name.
186189
* @param IP IP Address you wish to query
190+
* @throws IOException If an input or output exception occurred
187191
* @return Region name
188192
*/
189193
public String GetRegion(String IP) throws IOException {
@@ -193,6 +197,7 @@ public String GetRegion(String IP) throws IOException {
193197
/**
194198
* This function returns the city name.
195199
* @param IP IP Address you wish to query
200+
* @throws IOException If an input or output exception occurred
196201
* @return City name
197202
*/
198203
public String GetCity(String IP) throws IOException {
@@ -202,6 +207,7 @@ public String GetCity(String IP) throws IOException {
202207
/**
203208
* This function returns the ISP name.
204209
* @param IP IP Address you wish to query
210+
* @throws IOException If an input or output exception occurred
205211
* @return ISP name
206212
*/
207213
public String GetISP(String IP) throws IOException {
@@ -211,6 +217,7 @@ public String GetISP(String IP) throws IOException {
211217
/**
212218
* This function returns the proxy type.
213219
* @param IP IP Address you wish to query
220+
* @throws IOException If an input or output exception occurred
214221
* @return Proxy type
215222
*/
216223
public String GetProxyType(String IP) throws IOException {
@@ -220,6 +227,7 @@ public String GetProxyType(String IP) throws IOException {
220227
/**
221228
* This function returns the domain.
222229
* @param IP IP Address you wish to query
230+
* @throws IOException If an input or output exception occurred
223231
* @return Domain
224232
*/
225233
public String GetDomain(String IP) throws IOException {
@@ -229,6 +237,7 @@ public String GetDomain(String IP) throws IOException {
229237
/**
230238
* This function returns the usage type.
231239
* @param IP IP Address you wish to query
240+
* @throws IOException If an input or output exception occurred
232241
* @return Proxy type
233242
*/
234243
public String GetUsageType(String IP) throws IOException {
@@ -238,6 +247,7 @@ public String GetUsageType(String IP) throws IOException {
238247
/**
239248
* This function returns the Autonomous System Number.
240249
* @param IP IP Address you wish to query
250+
* @throws IOException If an input or output exception occurred
241251
* @return Autonomous System Number
242252
*/
243253
public String GetASN(String IP) throws IOException {
@@ -247,6 +257,7 @@ public String GetASN(String IP) throws IOException {
247257
/**
248258
* This function returns the Autonomous System name.
249259
* @param IP IP Address you wish to query
260+
* @throws IOException If an input or output exception occurred
250261
* @return Autonomous System name
251262
*/
252263
public String GetAS(String IP) throws IOException {
@@ -256,6 +267,7 @@ public String GetAS(String IP) throws IOException {
256267
/**
257268
* This function returns number of days the proxy was last seen.
258269
* @param IP IP Address you wish to query
270+
* @throws IOException If an input or output exception occurred
259271
* @return Number of days last seen
260272
*/
261273
public String GetLastSeen(String IP) throws IOException {
@@ -265,6 +277,7 @@ public String GetLastSeen(String IP) throws IOException {
265277
/**
266278
* This function returns the threat type of the proxy.
267279
* @param IP IP Address you wish to query
280+
* @throws IOException If an input or output exception occurred
268281
* @return Threat type of the proxy
269282
*/
270283
public String GetThreat(String IP) throws IOException {
@@ -274,6 +287,7 @@ public String GetThreat(String IP) throws IOException {
274287
/**
275288
* This function returns proxy result.
276289
* @param IP IP Address you wish to query
290+
* @throws IOException If an input or output exception occurred
277291
* @return Proxy result
278292
*/
279293
public ProxyResult GetAll(String IP) throws IOException {
@@ -282,6 +296,7 @@ public ProxyResult GetAll(String IP) throws IOException {
282296

283297
/**
284298
* This function destroys the mapped bytes.
299+
* @return 0 to indicate no errors
285300
*/
286301
public int Close() {
287302
DestroyMappedBytes();
@@ -449,13 +464,20 @@ private boolean LoadBIN() throws IOException {
449464
/**
450465
* This function initialize the component with the BIN file path and IO mode.
451466
* @param DatabasePath Path to the BIN database file
452-
* @param IOMode Default is file IO
467+
* @throws IOException If an input or output exception occurred
453468
* @return -1 if encounter error else 0
454469
*/
455470
public int Open(String DatabasePath) throws IOException {
456471
return Open(DatabasePath, IOModes.IP2PROXY_FILE_IO);
457472
}
458473

474+
/**
475+
* This function initialize the component with the BIN file path and IO mode.
476+
* @param DatabasePath Path to the BIN database file
477+
* @param IOMode Default is file IO
478+
* @throws IOException If an input or output exception occurred
479+
* @return -1 if encounter error else 0
480+
*/
459481
public int Open(String DatabasePath, IOModes IOMode) throws IOException {
460482
if (_DBType == 0) {
461483
_IPDatabasePath = DatabasePath;
@@ -479,6 +501,7 @@ public int Open(String DatabasePath, IOModes IOMode) throws IOException {
479501
/**
480502
* This function to query IP2Proxy data.
481503
* @param IPAddress IP Address you wish to query
504+
* @throws IOException If an input or output exception occurred
482505
* @return IP2Proxy data
483506
*/
484507
public ProxyResult ProxyQuery(String IPAddress) throws IOException {

test/java/IP2ProxyTest.java

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import org.junit.jupiter.api.BeforeAll;
2+
import org.junit.jupiter.api.BeforeEach;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.AfterEach;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import java.nio.file.*;
8+
import java.io.*;
9+
import com.ip2proxy.*;
10+
11+
class IP2ProxyTest {
12+
private static IP2Proxy Proxy;
13+
private static ProxyResult All;
14+
private static String binfile = "IP2PROXY-LITE-PX1.BIN";
15+
private static String binfilepath;
16+
private static String ip = "8.8.8.8";
17+
18+
@BeforeAll
19+
static void Setup() {
20+
Path binpath = Paths.get("src","test","resources", binfile);
21+
binfilepath = binpath.toFile().getAbsolutePath();
22+
}
23+
24+
@BeforeEach
25+
void Init() {
26+
Proxy = new IP2Proxy();
27+
}
28+
29+
@Test
30+
void TestOpenException() {
31+
assertThrows(IOException.class, () -> {
32+
Proxy.Open("dummy.bin");
33+
});
34+
}
35+
36+
@Test
37+
void TestQueryIsProxy() throws IOException {
38+
if (Proxy.Open(binfilepath) == 0) {
39+
All = Proxy.GetAll(ip);
40+
assertEquals(All.Is_Proxy, 0);
41+
}
42+
}
43+
44+
@Test
45+
void TestQueryProxyType() throws IOException {
46+
if (Proxy.Open(binfilepath) == 0) {
47+
All = Proxy.GetAll(ip);
48+
assertEquals(All.Proxy_Type, "NOT SUPPORTED");
49+
}
50+
}
51+
52+
@Test
53+
void TestQueryCountryShort() throws IOException {
54+
if (Proxy.Open(binfilepath) == 0) {
55+
All = Proxy.GetAll(ip);
56+
assertEquals(All.Country_Short, "-");
57+
}
58+
}
59+
60+
@Test
61+
void TestQueryCountryLong() throws IOException {
62+
if (Proxy.Open(binfilepath) == 0) {
63+
All = Proxy.GetAll(ip);
64+
assertEquals(All.Country_Long, "-");
65+
}
66+
}
67+
68+
@Test
69+
void TestQueryRegion() throws IOException {
70+
if (Proxy.Open(binfilepath) == 0) {
71+
All = Proxy.GetAll(ip);
72+
assertEquals(All.Region, "NOT SUPPORTED");
73+
}
74+
}
75+
76+
@Test
77+
void TestQueryCity() throws IOException {
78+
if (Proxy.Open(binfilepath) == 0) {
79+
All = Proxy.GetAll(ip);
80+
assertEquals(All.City, "NOT SUPPORTED");
81+
}
82+
}
83+
84+
@Test
85+
void TestQueryISP() throws IOException {
86+
if (Proxy.Open(binfilepath) == 0) {
87+
All = Proxy.GetAll(ip);
88+
assertEquals(All.ISP, "NOT SUPPORTED");
89+
}
90+
}
91+
92+
@Test
93+
void TestQueryDomain() throws IOException {
94+
if (Proxy.Open(binfilepath) == 0) {
95+
All = Proxy.GetAll(ip);
96+
assertEquals(All.Domain, "NOT SUPPORTED");
97+
}
98+
}
99+
100+
@Test
101+
void TestQueryUsageType() throws IOException {
102+
if (Proxy.Open(binfilepath) == 0) {
103+
All = Proxy.GetAll(ip);
104+
assertEquals(All.Usage_Type, "NOT SUPPORTED");
105+
}
106+
}
107+
108+
@Test
109+
void TestQueryASN() throws IOException {
110+
if (Proxy.Open(binfilepath) == 0) {
111+
All = Proxy.GetAll(ip);
112+
assertEquals(All.ASN, "NOT SUPPORTED");
113+
}
114+
}
115+
116+
@Test
117+
void TestQueryAS() throws IOException {
118+
if (Proxy.Open(binfilepath) == 0) {
119+
All = Proxy.GetAll(ip);
120+
assertEquals(All.AS, "NOT SUPPORTED");
121+
}
122+
}
123+
124+
@Test
125+
void TestQueryLastSeen() throws IOException {
126+
if (Proxy.Open(binfilepath) == 0) {
127+
All = Proxy.GetAll(ip);
128+
assertEquals(All.Last_Seen, "NOT SUPPORTED");
129+
}
130+
}
131+
132+
@Test
133+
void TestQueryThreat() throws IOException {
134+
if (Proxy.Open(binfilepath) == 0) {
135+
All = Proxy.GetAll(ip);
136+
assertEquals(All.Threat, "NOT SUPPORTED");
137+
}
138+
}
139+
140+
@AfterEach
141+
void TearDown() {
142+
Proxy.Close();
143+
Proxy = null;
144+
}
145+
}
146+
147+

test/resources/IP2PROXY-LITE-PX1.BIN

86.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)