Compare commits

...

11 Commits

Author SHA1 Message Date
ed2b34add0 Fix net.i2p.util.Addresses::getIP empty string
The standard library behavior is to return the localhost when null or an empty string is passed.
getIP seeked to override that behavior, but didn't treat the empty string case.
2021-01-25 18:32:19 +01:00
8f931f3d16 Add @since 0.9.49 to net.i2p.util.Addresses 2021-01-25 18:27:31 +01:00
453dd4076f test net.i2p.util.Addresses::getPort 2021-01-21 21:34:59 +01:00
0263ba23b0 test net.i2p.util.Addresses::toString 2021-01-21 00:08:26 +01:00
61ffbce1c1 test net.i2p.util.Addresses::isDeprecated 2021-01-20 23:19:59 +01:00
78a09c27f7 test net.i2p.util.Addresses::isDynamic 2021-01-20 23:19:59 +01:00
30f3e8c6ed test net.i2p.util.Addresses::isIP{v6,v4,}Address 2021-01-20 23:19:59 +01:00
23352c2d76 test net.i2p.util.Addresses::getIP with IP string 2021-01-20 23:15:50 +01:00
6b3bf4cb78 test net.i2p.util.Addresses::getIP with empty string 2021-01-20 23:15:50 +01:00
b37c718785 test net.i2p.util.Addresses::getIP with null input 2021-01-20 23:15:49 +01:00
3276cfca82 Correct @return of Addresses.getAddresses 2021-01-20 19:44:44 +01:00
2 changed files with 151 additions and 9 deletions

View File

@@ -40,7 +40,7 @@ import net.i2p.data.DataHelper;
* @author zzz
*/
public abstract class Addresses {
private static final File IF_INET6_FILE = new File("/proc/net/if_inet6");
private static final long INET6_CACHE_EXPIRE = 10*60*1000;
private static final boolean INET6_CACHE_ENABLED = !SystemVersion.isMac() && !SystemVersion.isWindows() &&
@@ -122,10 +122,9 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses including wildcard
* @param includeLocal whether to include local
* @param includeIPv6 whether to include IPV6
* @return a Set of all addresses
* @return a sorted set of all addresses including wildcard
* @since 0.8.3
*/
public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6) {
@@ -141,11 +140,10 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses
* @param includeSiteLocal whether to include private like 192.168.x.x
* @param includeLoopbackAndWildcard whether to include 127.x.x.x and 0.0.0.0
* @param includeIPv6 whether to include IPV6
* @return a Set of all addresses
* @return a sorted set of all addresses
* @since 0.9.4
*/
public static SortedSet<String> getAddresses(boolean includeSiteLocal,
@@ -163,12 +161,11 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses
* @param includeSiteLocal whether to include private like 192.168.x.x
* @param includeLoopbackAndWildcard whether to include 127.x.x.x and 0.0.0.0
* @param includeIPv6 whether to include IPV6
* @param includeIPv6Temporary whether to include IPV6 temporary addresses
* @return a Set of all addresses
* @return a sorted set of all addresses
* @since 0.9.46
*/
public static SortedSet<String> getAddresses(boolean includeSiteLocal,
@@ -343,7 +340,7 @@ public abstract class Addresses {
return "(bad IP length " + addr.length + "):" + port;
}
}
/**
* Convenience method to convert and validate a port String
* without throwing an exception.
@@ -402,7 +399,7 @@ public abstract class Addresses {
* @since 0.9.3
*/
public static byte[] getIP(String host) {
if (host == null)
if (host == null || host.isEmpty())
return null;
byte[] rv;
synchronized (_IPAddress) {

View File

@@ -0,0 +1,145 @@
package net.i2p.util;
import org.junit.Test;
import java.net.Inet6Address;
import java.net.UnknownHostException;
import static org.junit.Assert.*;
/**
* @since 0.9.49
*/
public class AddressesTest {
@Test
public void getIPNull() {
assertNull(Addresses.getIP(null));
}
@Test
public void getIPEmptyString() {
assertNull(Addresses.getIP(""));
}
@Test
public void getIPWithIPString() {
byte[] address = {
1, 2, 3, 4
};
assertArrayEquals(address, Addresses.getIP("1.2.3.4"));
}
@Test
public void getPort() {
assertEquals(80, Addresses.getPort("80"));
}
@Test
public void getPort__invalidPort() {
String[] strings = {
"",
" 80",
"-100",
"a",
"99999",
null
};
for (String string : strings) {
assertEquals(0, Addresses.getPort(string));
}
}
@Test
public void isIPAddress() {
assertTrue(Addresses.isIPAddress("127.0.0.1"));
assertTrue(Addresses.isIPAddress("::1"));
}
@Test
public void isIPv6Address() {
assertTrue(Addresses.isIPv6Address("::1"));
assertFalse(Addresses.isIPv6Address(""));
}
@Test
public void isIPv4Address() {
assertTrue(Addresses.isIPv4Address("127.0.0.1"));
assertFalse(Addresses.isIPv4Address(""));
}
/**
* Should always return false when the address isn't in the cache
*/
@Test
public void isDynamic() throws UnknownHostException {
String host = "localhost";
byte[] address = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertFalse(Addresses.isDynamic((Inet6Address) Inet6Address.getByAddress(host, address)));
}
/**
* Should always return false when the address isn't in the cache
*/
@Test
public void isDeprecated() throws UnknownHostException {
String host = "localhost";
byte[] address = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertFalse(Addresses.isDeprecated((Inet6Address) Inet6Address.getByAddress(host, address)));
}
@Test
public void testToString() {
byte[] address = {127, 0, 0, 1};
assertEquals("127.0.0.1", Addresses.toString(address));
}
@Test
public void testToString__ipv4withPort() {
byte[] address = {127, 0, 0, 1};
assertEquals("127.0.0.1:80", Addresses.toString(address, 80));
}
@Test
public void testToString__ipv6withPort() {
byte[] address = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1,
};
assertEquals("[0:0:0:0:0:0:0:1]:80", Addresses.toString(address, 80));
}
@Test
public void testToString__null() {
assertEquals("null", Addresses.toString(null));
}
@Test
public void testToString__nullWithPort() {
assertEquals("null:80", Addresses.toString(null, 80));
}
@Test
public void testToString__badLength() {
byte[] address = {1};
assertTrue(Addresses.toString(address).startsWith("bad IP length"));
}
@Test
public void testToString__badLengthWithPort() {
byte[] address = {1};
String string = Addresses.toString(address, 80);
String expectedStartString = "(bad IP length";
assertTrue(
String.format("%s doesn't start with: %s", string, expectedStartString),
string.startsWith(expectedStartString)
);
String expectedEndString = "80";
assertTrue(
String.format("%s doesn't end with: %s", string, expectedEndString),
string.endsWith(expectedEndString)
);
}
}