Skip to content

Commit

Permalink
test: improve test coverage of InetSocketAddresses
Browse files Browse the repository at this point in the history
Acked-by: Paul Millar
Target: master
  • Loading branch information
kofemann committed Dec 13, 2018
1 parent 966e9da commit adef1a1
Showing 1 changed file with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
*/
package org.dcache.oncrpc4j.rpc.net;

import org.dcache.oncrpc4j.rpc.net.InetSocketAddresses;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

public class InetSocketAddressesTest {

Expand All @@ -47,14 +48,32 @@ public void testLocalHostV6() throws Exception {
}

@Test
public void testLocalHostV4Revert() throws Exception {
public void testSocketAddressToUaddrIPv4() throws Exception {
String uaddr = "127.0.0.1.203.81";
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"),52049);

assertEquals("reverce convertion failed", uaddr,
InetSocketAddresses.uaddrOf(socketAddress));
}

@Test
public void testSocketAddressToUaddrIPv6() throws Exception {
String uaddr = "fe80:0:0:0:21c:c0ff:fea0:caf4.203.81";
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("fe80::21c:c0ff:fea0:caf4"), 52049);

assertEquals("reverce convertion failed", uaddr,
InetSocketAddresses.uaddrOf(socketAddress));
}

@Test
public void testSocketAddressToUaddrIPv6WithScope() throws Exception {
String uaddr = "fe80:0:0:0:21c:c0ff:fea0:caf4.203.81";
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("fe80::21c:c0ff:fea0:caf4%1"), 52049);

assertEquals("reverce convertion failed", uaddr,
InetSocketAddresses.uaddrOf(socketAddress));
}

@Test
public void testHostAndPortIpv4() throws Exception {
String hostAndPort = "127.0.0.1:1111";
Expand All @@ -72,4 +91,47 @@ public void testHostAndPortIpv6() throws Exception {
assertEquals(InetAddress.getByName("fe80::21c:c0ff:fea0:caf4"), address.getAddress());
assertEquals(1111, address.getPort());
}

@Test(expected = IllegalArgumentException.class)
public void testHostNoPortIPv4() {
String uaddr = "127.0.0.1";
InetSocketAddresses.forUaddrString(uaddr);
}

@Test(expected = IllegalArgumentException.class)
public void testHostPartiallyNoPortIPv4() {
String uaddr = "127.0.0.1.1";
InetSocketAddresses.forUaddrString(uaddr);
}

@Test(expected = IllegalArgumentException.class)
public void testHostNoPortIPv6() {
String uaddr = "fe80::21c:c0ff:fea0:caf4";
InetSocketAddresses.forUaddrString(uaddr);
}

@Test(expected = IllegalArgumentException.class)
public void testHostPartiallyNoPortIPv6() {
String uaddr = "fe80::21c:c0ff:fea0:caf4.1";
InetSocketAddresses.forUaddrString(uaddr);
}

@Test
public void testTCPv4Netid() throws UnknownHostException {
InetAddress address = InetAddress.getByName("1.1.1.1");
assertEquals("invalid netid", "tcp", InetSocketAddresses.tcpNetidOf(address));
}

@Test
public void testTCPv6Netid() throws UnknownHostException {
InetAddress address = InetAddress.getByName("fe80::21c:c0ff:fea0:caf4");
assertEquals("invalid netid", "tcp6", InetSocketAddresses.tcpNetidOf(address));
}

@Test(expected = IllegalArgumentException.class)
public void testInvalitInetaddressType() throws UnknownHostException {
InetAddress address = mock(InetAddress.class); // not a direct instance of Inet4/6Address
assertEquals("invalid netid", "tcp6", InetSocketAddresses.tcpNetidOf(address));
}

}

0 comments on commit adef1a1

Please sign in to comment.