-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
202 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package zmq.io.net.tcp; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.net.Inet4Address; | ||
import java.net.Inet6Address; | ||
import java.net.InetSocketAddress; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.zeromq.ZMQException; | ||
|
||
import zmq.ZError; | ||
import zmq.io.net.Address; | ||
import zmq.io.net.NetProtocol; | ||
import zmq.util.Utils; | ||
|
||
public class TcpAddressTest | ||
{ | ||
@Test | ||
public void parsesIpv6AddressSimple() throws IOException | ||
{ | ||
String addressString = "2000::a1"; | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), addressString + ":" + port); | ||
addr.resolve(true); | ||
InetSocketAddress expected = new InetSocketAddress(addressString, port); | ||
Address.IZAddress resolved = addr.resolved(); | ||
assertEquals(expected, resolved.address()); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals(port, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void parsesIpv6AddressBracket() throws IOException | ||
{ | ||
String addressString = "2000::a1"; | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), "[" + addressString + "]:" + port); | ||
addr.resolve(true); | ||
InetSocketAddress expected = new InetSocketAddress(addressString, port); | ||
Address.IZAddress resolved = addr.resolved(); | ||
assertEquals(expected, resolved.address()); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals(port, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void parsesIpv6AddressNotWanted() throws IOException | ||
{ | ||
try { | ||
String addressString = "2000::a1"; | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), | ||
addressString + ":" + port); | ||
addr.resolve(false); | ||
InetSocketAddress expected = new InetSocketAddress(addressString, | ||
port); | ||
Address.IZAddress resolved = addr.resolved(); | ||
assertEquals(expected, resolved.address()); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals(port, sa.getPort()); | ||
Assert.fail(); | ||
} | ||
catch (ZMQException e) { | ||
Assert.assertEquals(ZError.EADDRNOTAVAIL, e.getErrorCode()); | ||
Assert.assertEquals("2000::a1 not found matching IPv4/IPv6 settings", e.getMessage()); | ||
} | ||
} | ||
|
||
@Ignore // Fails on both Circleci and Travis | ||
@Test | ||
public void testGoodIPv6Google() | ||
{ | ||
Address addr = new Address(NetProtocol.tcp.name(), "www.google.com:80"); | ||
addr.resolve(true); | ||
Address.IZAddress resolved = addr.resolved(); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals(80, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void testGoodIP46Google() | ||
{ | ||
Address addr = new Address(NetProtocol.tcp.name(), "www.google.com:80"); | ||
addr.resolve(false); | ||
Address.IZAddress resolved = addr.resolved(); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet4Address); | ||
Assert.assertEquals(80, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void testBad() | ||
{ | ||
try { | ||
Address addr = new Address(NetProtocol.tcp.name(), "lclhost.:80"); | ||
addr.resolve(true); | ||
addr.resolved(); | ||
Assert.fail(); | ||
} | ||
catch (ZMQException e) { | ||
Assert.assertEquals(ZError.EADDRNOTAVAIL, e.getErrorCode()); | ||
Assert.assertEquals(e.getCause().getMessage(), e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testUnspecifiedIPv6DoubleColon() throws IOException | ||
{ | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), ":::" + port); | ||
addr.resolve(true); | ||
Address.IZAddress resolved = addr.resolved(); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals("0:0:0:0:0:0:0:0", sa.getHostString()); | ||
Assert.assertEquals(port, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void testUnspecifiedIPv6Star() throws IOException | ||
{ | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), "*:" + port); | ||
addr.resolve(true); | ||
Address.IZAddress resolved = addr.resolved(); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet6Address); | ||
Assert.assertEquals("0:0:0:0:0:0:0:0", sa.getHostString()); | ||
Assert.assertEquals(port, sa.getPort()); | ||
} | ||
|
||
@Test | ||
public void testUnspecifiedIPv4() throws IOException | ||
{ | ||
int port = Utils.findOpenPort(); | ||
Address addr = new Address(NetProtocol.tcp.name(), "*:" + port); | ||
addr.resolve(false); | ||
Address.IZAddress resolved = addr.resolved(); | ||
InetSocketAddress sa = (InetSocketAddress) resolved.address(); | ||
Assert.assertTrue(sa.getAddress() instanceof Inet4Address); | ||
Assert.assertEquals("0.0.0.0", sa.getHostString()); | ||
Assert.assertEquals(port, sa.getPort()); | ||
} | ||
} |