Skip to content

Commit b2031d9

Browse files
committed
Added extra tests and prevented accidental instantiation of classes
1 parent ad97603 commit b2031d9

File tree

15 files changed

+188
-79
lines changed

15 files changed

+188
-79
lines changed

library/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ android {
1919
}
2020

2121
dependencies {
22-
compile fileTree(dir: 'libs', include: ['*.jar'])
2322
testCompile 'junit:junit:4.12'
24-
testCompile 'org.mockito:mockito-core:1.9.5'
23+
testCompile 'org.mockito:mockito-core:1.10.19'
2524
provided 'com.android.support:appcompat-v7:24.2.0'
2625
}

library/src/main/java/com/stealthcopter/networktools/ARPInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
public class ARPInfo {
2323

24+
// This class is not to be instantiated
25+
private ARPInfo() {
26+
}
27+
28+
2429
/**
2530
* Try to extract a hardware MAC address from a given IP address using the
2631
* ARP cache (/proc/net/arp).

library/src/main/java/com/stealthcopter/networktools/IPTools.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,9 @@
77
*/
88
public class IPTools {
99

10-
// public void findDevicesOnSubnet(){
11-
// // TODO: provide Ip address
12-
//
13-
// // TODO: Cheat by looking at ARP Cache for a headstart.
14-
//
15-
// // TODO: Ping devices on network
16-
//
17-
// // TODO Results.
18-
// }
19-
//
20-
// public void getLocalIpAddress(){
21-
//
22-
// }
10+
// This class is not to be instantiated
11+
private IPTools() {
12+
}
2313

2414

2515
/**

library/src/main/java/com/stealthcopter/networktools/Ping.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
*/
1515
public class Ping {
1616

17+
// This class is not to be instantiated
18+
private Ping() {
19+
}
20+
1721
public interface PingListener{
1822
void onResult(PingResult pingResult);
1923
void onFinished(PingStats pingStats);

library/src/main/java/com/stealthcopter/networktools/PortScan.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
*/
1414
public class PortScan {
1515

16+
// This class is not to be instantiated
17+
private PortScan() {
18+
}
19+
1620
public interface PortListener{
1721
void onResult(int portNo, boolean open);
1822
void onFinished(ArrayList<Integer> openPorts);

library/src/main/java/com/stealthcopter/networktools/WakeOnLan.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class WakeOnLan {
2020
public static final int DEFAULT_TIMEOUT_MILLIS = 10000;
2121
public static final int DEFAULT_NO_PACKETS = 5;
2222

23+
// This class is not to be instantiated
24+
private WakeOnLan() {
25+
}
26+
2327
/**
2428
* Send a Wake-On-Lan packet to port 9 using default timeout of 10s
2529
* @param ipStr - IP String to send to

library/src/main/java/com/stealthcopter/networktools/ping/PingTools.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
*/
1111
public class PingTools {
1212

13+
// This class is not to be instantiated
14+
private PingTools() {
15+
}
16+
17+
1318
/**
14-
* This will perform a ping using the native ping tool and fall back to using a java style
15-
* request on failure.
19+
* Perform a ping using the native ping tool and fall back to using java echo request
20+
* on failure.
21+
* @param ia - address to ping
22+
* @param timeOutMillis - timeout in millisecdonds
23+
* @return - the ping results
1624
*/
1725
public static PingResult doPing(InetAddress ia, int timeOutMillis){
1826

1927
// Try native ping first
2028
try{
21-
PingResult result = PingTools.doNativePing(ia, timeOutMillis);
22-
return result;
29+
return PingTools.doNativePing(ia, timeOutMillis);
2330
} catch (InterruptedException e){
2431
PingResult pingResult = new PingResult(ia);
2532
pingResult.isReachable = false;
@@ -36,6 +43,14 @@ public static PingResult doPing(InetAddress ia, int timeOutMillis){
3643
}
3744

3845

46+
/**
47+
* Perform a ping using the native ping binary
48+
* @param ia - address to ping
49+
* @param timeOutMillis - timeout in millisecdonds
50+
* @return - the ping results
51+
* @throws IOException
52+
* @throws InterruptedException
53+
*/
3954
public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws IOException, InterruptedException {
4055
return PingNative.ping(ia, timeOutMillis);
4156
}
@@ -44,6 +59,10 @@ public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws
4459
* Tries to reach this {@code InetAddress}. This method first tries to use
4560
* ICMP <i>(ICMP ECHO REQUEST)</i>, falling back to a TCP connection
4661
* on port 7 (Echo) of the remote host.
62+
*
63+
* @param ia - address to ping
64+
* @param timeOutMillis - timeout in millisecdonds
65+
* @return - the ping results
4766
*/
4867
public static PingResult doJavaPing(InetAddress ia, int timeOutMillis){
4968
PingResult pingResult = new PingResult(ia);

library/src/main/java/com/stealthcopter/networktools/portscanning/PortScanTCP.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
public class PortScanTCP {
1212

13+
// This class is not to be instantiated
14+
private PortScanTCP() {
15+
}
16+
1317
public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis){
1418
Socket s = null;
1519
try {

library/src/test/java/com/stealthcopter/networktools/ARPInfoUnitTest.java renamed to library/src/test/java/com/stealthcopter/networktools/ARPInfoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* To work on unit tests, switch the Test Artifact in the Build Variants view.
1313
*/
14-
public class ARPInfoUnitTest {
14+
public class ARPInfoTest {
1515

1616
@Test
1717
public void nullIPsandMacsReturnNull() throws Exception {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.stealthcopter.networktools;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
/**
9+
* Created by matthew on 03/11/17.
10+
*/
11+
12+
public class IPToolsTest {
13+
14+
15+
String[] getInvalidIpAddresses(){
16+
return new String[]{"beepbeep", "nope", "hello"};
17+
}
18+
19+
String[] getIPv4Addresses(){
20+
return new String[]{"192.168.0.1", "127.0.0.1", "10.0.0.1",};
21+
}
22+
23+
String[] getIPv6Addresses(){
24+
return new String[]{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"};
25+
}
26+
27+
@Test
28+
public void testIsIPv4Address() throws Exception {
29+
30+
for (String address: getIPv4Addresses()) {
31+
assertTrue(IPTools.isIPv4Address(address));
32+
}
33+
34+
for (String address: getIPv6Addresses()) {
35+
assertFalse(IPTools.isIPv4Address(address));
36+
}
37+
38+
for (String address: getInvalidIpAddresses()) {
39+
assertFalse(IPTools.isIPv4Address(address));
40+
}
41+
}
42+
43+
@Test
44+
public void testIsIPv6Address() throws Exception {
45+
for (String address: getIPv4Addresses()) {
46+
assertFalse(IPTools.isIPv6Address(address));
47+
}
48+
49+
for (String address: getIPv6Addresses()) {
50+
assertTrue(IPTools.isIPv6Address(address));
51+
}
52+
53+
for (String address: getInvalidIpAddresses()) {
54+
assertFalse(IPTools.isIPv6Address(address));
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)