summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2014-06-17 14:47:50 -0700
committerElliott Hughes <enh@google.com>2014-06-17 14:47:50 -0700
commit6188cc840d4987d2445740ce8a13dd335b6c6f73 (patch)
tree94bc23d983f20338a7ec1debc16ccfc250ad0488 /luni/src/test/java
parentaaddf1a8047e874ebba53b3b19d781d4a269423f (diff)
parent63744c884dd4b4f4307f2b021fb894af164972af (diff)
downloadlibcore-6188cc840d4987d2445740ce8a13dd335b6c6f73.zip
libcore-6188cc840d4987d2445740ce8a13dd335b6c6f73.tar.gz
libcore-6188cc840d4987d2445740ce8a13dd335b6c6f73.tar.bz2
resolved conflicts for merge of 63744c88 to master
Change-Id: I2dbbda225d0fbe61da34f93abbb466ed490ba405
Diffstat (limited to 'luni/src/test/java')
-rw-r--r--luni/src/test/java/libcore/java/lang/CharacterTest.java62
-rw-r--r--luni/src/test/java/libcore/java/net/SocketTest.java4
-rw-r--r--luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java1120
-rw-r--r--luni/src/test/java/libcore/java/nio/channels/DatagramChannelTest.java411
-rw-r--r--luni/src/test/java/libcore/java/nio/channels/ServerSocketChannelTest.java119
-rw-r--r--luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java325
-rw-r--r--luni/src/test/java/libcore/java/util/CurrencyTest.java7
-rw-r--r--luni/src/test/java/libcore/java/util/zip/ZipFileTest.java90
-rw-r--r--luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java91
-rw-r--r--luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java58
10 files changed, 30 insertions, 2257 deletions
diff --git a/luni/src/test/java/libcore/java/lang/CharacterTest.java b/luni/src/test/java/libcore/java/lang/CharacterTest.java
index 0028521..94e3b96 100644
--- a/luni/src/test/java/libcore/java/lang/CharacterTest.java
+++ b/luni/src/test/java/libcore/java/lang/CharacterTest.java
@@ -277,66 +277,4 @@ public class CharacterTest extends junit.framework.TestCase {
}
}
}
-
- public void test_UnicodeScript_forName() throws Exception {
- try {
- Character.UnicodeScript.forName(null);
- fail();
- } catch (NullPointerException expected) {
- }
-
- try {
- Character.UnicodeScript.forName("existential_dilemmas");
- fail();
- } catch (IllegalArgumentException expected) {
- }
-
- // Note that ICU is pretty lenient about block names and their abbreviations.
- assertSame(Character.UnicodeScript.MALAYALAM, Character.UnicodeScript.forName("Malayalam"));
- assertSame(Character.UnicodeScript.MALAYALAM, Character.UnicodeScript.forName("MalayaLAM"));
- assertSame(Character.UnicodeScript.MALAYALAM, Character.UnicodeScript.forName("Mlym"));
- assertSame(Character.UnicodeScript.MALAYALAM, Character.UnicodeScript.forName("MlYM"));
-
- assertSame(Character.UnicodeScript.OLD_SOUTH_ARABIAN, Character.UnicodeScript.forName("Old_south_arabian"));
-
- // NOTE: This test fails on the RI because they're much stricter in
- // their matching. Strict enough that they fail on "Old south arabian", despite
- // it being the official name AND the alias for this script.
- assertSame(Character.UnicodeScript.OLD_SOUTH_ARABIAN, Character.UnicodeScript.forName("Old south arabian"));
- assertSame(Character.UnicodeScript.OLD_SOUTH_ARABIAN, Character.UnicodeScript.forName("SARB"));
-
- // A script that's recognized by ICU but not a part of the standard
- // java script values.
- try {
- Character.UnicodeScript.forName("Old north arabian");
- fail();
- } catch (IllegalArgumentException expected) {
- }
- }
-
- public void test_UnicodeScript_of() throws Exception {
- try {
- Character.UnicodeScript.of(-1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
-
- try {
- Character.UnicodeScript.of(0xffffff);
- fail();
- } catch (IllegalArgumentException expected) {
- }
-
- // The example from the ICU4C unit tests.
- assertSame(Character.UnicodeScript.MALAYALAM, Character.UnicodeScript.of(0x0D02));
-
- // Special cases:
- //
- // 0640 is the ARABIC_TATWEEL, used by both Mandiac & Syriac
- assertSame(Character.UnicodeScript.COMMON, Character.UnicodeScript.of(0x0640));
- // 0300 is the COMBINING GRAVE ACCENT, which should be INHERITED because it's
- // a nonspacing mark.
- assertSame(Character.UnicodeScript.INHERITED, Character.UnicodeScript.of(0x0300));
- assertSame(Character.UnicodeScript.COMMON, Character.UnicodeScript.of(0x0640));
- }
}
diff --git a/luni/src/test/java/libcore/java/net/SocketTest.java b/luni/src/test/java/libcore/java/net/SocketTest.java
index b9ed99c..fb09be0 100644
--- a/luni/src/test/java/libcore/java/net/SocketTest.java
+++ b/luni/src/test/java/libcore/java/net/SocketTest.java
@@ -90,7 +90,7 @@ public class SocketTest extends junit.framework.TestCase {
// Open a local server port.
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
- ssc.bind(listenAddr, 0);
+ ssc.socket().bind(listenAddr, 0);
ServerSocket ss = ssc.socket();
// Open a socket to the local port.
@@ -110,7 +110,7 @@ public class SocketTest extends junit.framework.TestCase {
in.socket().setTcpNoDelay(false);
}
- InetSocketAddress listenAddress = (InetSocketAddress) in.getLocalAddress();
+ InetSocketAddress listenAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
diff --git a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
deleted file mode 100644
index f0db3ba..0000000
--- a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
+++ /dev/null
@@ -1,1120 +0,0 @@
-package libcore.java.nio.channels;
-
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
-import java.io.IOException;
-import java.net.Inet4Address;
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.InterfaceAddress;
-import java.net.NetworkInterface;
-import java.net.SocketAddress;
-import java.net.SocketException;
-import java.net.StandardSocketOptions;
-import java.nio.ByteBuffer;
-import java.nio.channels.ClosedChannelException;
-import java.nio.channels.DatagramChannel;
-import java.nio.channels.MembershipKey;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-
-/**
- * Tests associated with multicast behavior of DatagramChannel.
- */
-public class DatagramChannelMulticastTest extends TestCase {
-
- private static InetAddress lookup(String s) {
- try {
- return InetAddress.getByName(s);
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- }
-
- // These IP addresses aren't inherently "good" or "bad"; they're just used like that.
- // We use the "good" addresses for our actual group, and the "bad" addresses are for a group that
- // we won't actually set up.
- private static final InetAddress GOOD_MULTICAST_IPv4 = lookup("239.255.0.1");
- private static final InetAddress BAD_MULTICAST_IPv4 = lookup("239.255.0.2");
- private static final InetAddress GOOD_MULTICAST_IPv6 = lookup("ff05::7:7");
- private static final InetAddress BAD_MULTICAST_IPv6 = lookup("ff05::7:8");
-
- // Special addresses.
- private static final InetAddress WILDCARD_IPv4 = lookup("0.0.0.0");
- private static final InetAddress WILDCARD_IPv6 = lookup("::");
-
- // Arbitrary unicast addresses. Used when the value doesn't actually matter. e.g. for source
- // filters.
- private static final InetAddress UNICAST_IPv4_1 = lookup("192.168.1.1");
- private static final InetAddress UNICAST_IPv4_2 = lookup("192.168.1.2");
- private static final InetAddress UNICAST_IPv6_1 = lookup("2001:db8::1");
- private static final InetAddress UNICAST_IPv6_2 = lookup("2001:db8::2");
-
- private List<NetworkInterface> ipv4networkInterfaces = new ArrayList<NetworkInterface>();
- private List<NetworkInterface> ipv6networkInterfaces = new ArrayList<NetworkInterface>();
- private NetworkInterface ipv4networkInterface;
- private NetworkInterface ipv6networkInterface;
- private NetworkInterface loopbackInterface;
-
- @Override
- protected void setUp() throws Exception {
- // The loopback interface isn't actually useful for sending/receiving multicast messages but it
- // can be used as a dummy for tests where that does not matter.
- loopbackInterface = NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress());
- assertNotNull(loopbackInterface);
- assertTrue(loopbackInterface.isLoopback());
- assertFalse(loopbackInterface.supportsMulticast());
-
- Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
- assertNotNull(interfaces);
- // Only consider interfaces that have addresses associated with them. Otherwise tests don't work
- // so well.
- while (interfaces.hasMoreElements()) {
- NetworkInterface networkInterface = interfaces.nextElement();
- if (willWorkForMulticast(networkInterface)) {
- Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
- while (addresses.hasMoreElements()) {
- final InetAddress nextAddress = addresses.nextElement();
- if (nextAddress instanceof Inet4Address) {
- ipv4networkInterfaces.add(networkInterface);
- } else if (nextAddress instanceof Inet6Address) {
- ipv6networkInterfaces.add(networkInterface);
- }
- }
- }
- }
- assertTrue(
- "Test environment must have network interfaces capable of both IPv4 and IPv6 multicast",
- ipv4networkInterfaces.size() > 0 && ipv6networkInterfaces.size() > 0);
- ipv4networkInterface = ipv4networkInterfaces.get(0);
- ipv6networkInterface = ipv6networkInterfaces.get(0);
- }
-
- public void test_open() throws IOException {
- DatagramChannel dc = DatagramChannel.open();
-
- // Unlike MulticastSocket, DatagramChannel has SO_REUSEADDR set to false by default.
- assertFalse(dc.getOption(StandardSocketOptions.SO_REUSEADDR));
-
- // Confirm multicast loop is on by default as specified in the docs. Many tests in this class
- // depend on this being true.
- assertTrue(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- assertNull(dc.getLocalAddress());
- assertTrue(dc.isOpen());
- assertFalse(dc.isConnected());
-
- dc.close();
- }
-
- public void test_bind_null() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- assertNotNull(dc.getLocalAddress());
- assertTrue(dc.isOpen());
- assertFalse(dc.isConnected());
-
- dc.close();
- try {
- dc.getLocalAddress();
- fail();
- } catch (ClosedChannelException expected) {
- }
- assertFalse(dc.isOpen());
- assertFalse(dc.isConnected());
- }
-
- public void test_joinAnySource_afterClose() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- dc.close();
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
-
- public void test_joinAnySource_nullGroupAddress() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(null, ipv4networkInterface);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinAnySource_nullNetworkInterface() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv4, null);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinAnySource_nonMulticastGroupAddress_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- test_joinAnySource_illegalArgumentExpected(networkInterface, UNICAST_IPv4_1);
- }
- }
-
- public void test_joinAnySource_nonMulticastGroupAddress_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- test_joinAnySource_illegalArgumentExpected(networkInterface, UNICAST_IPv6_1);
- }
- }
-
- private void test_joinAnySource_illegalArgumentExpected(NetworkInterface networkInterface,
- InetAddress group) throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(group, networkInterface);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinAnySource_IPv4() throws Exception {
- test_joinAnySource(ipv4networkInterfaces, GOOD_MULTICAST_IPv4, BAD_MULTICAST_IPv4);
- }
-
- public void test_joinAnySource_IPv6() throws Exception {
- test_joinAnySource(ipv6networkInterfaces, GOOD_MULTICAST_IPv6, BAD_MULTICAST_IPv6);
- }
-
- private void test_joinAnySource(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group, InetAddress group2)
- throws Exception {
- for (NetworkInterface networkInterface : networkInterfaces) {
- // Set up a receiver join the group on networkInterface.
- DatagramChannel receiverChannel = createReceiverChannel();
- InetSocketAddress localAddress = (InetSocketAddress) receiverChannel.getLocalAddress();
- receiverChannel.join(group, networkInterface);
-
- // Send a message to the group we joined.
- String msg = "Hello World";
- sendMessage(group, localAddress.getPort(), msg, networkInterface);
-
- // Now verify that we received the data as expected.
- ByteBuffer recvBuffer = ByteBuffer.allocate(100);
- SocketAddress sourceAddress = receiverChannel.receive(recvBuffer);
- assertNotNull(sourceAddress);
- assertEquals(msg, new String(recvBuffer.array(), 0, recvBuffer.position()));
-
- // Send a message to the group we did not join.
- String msg2 = "Hello World - Different Group";
- sendMessage(group2, localAddress.getPort(), msg2, networkInterface);
- recvBuffer.position(0);
- // Now verify that we didn't receive the second message.
- SocketAddress sourceAddress2 = receiverChannel.receive(recvBuffer);
- assertNull(sourceAddress2);
-
- receiverChannel.close();
- }
- }
-
- public void test_joinAnySource_processLimit() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- for (byte i = 1; i <= 25; i++) {
- InetAddress groupAddress = Inet4Address.getByName("239.255.0." + i);
- try {
- dc.join(groupAddress, ipv4networkInterface);
- } catch (SocketException e) {
- // There is a limit, that's ok according to the RI docs. For this test a lower bound of 20
- // is used, which appears to be the default linux limit.
- // See /proc/sys/net/ipv4/igmp_max_memberships.
- assertTrue(i > 20);
- break;
- }
- }
-
- dc.close();
- }
-
- public void test_joinAnySource_blockLimit() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey key = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- for (byte i = 1; i <= 15; i++) {
- InetAddress sourceAddress = Inet4Address.getByName("10.0.0." + i);
- try {
- key.block(sourceAddress);
- } catch (SocketException e) {
- // There is a limit, that's ok according to the RI docs. For this test a lower bound of 10
- // is used, which appears to be the default linux limit.
- // See /proc/sys/net/ipv4/igmp_max_msf.
- assertTrue(i > 10);
- break;
- }
- }
-
- dc.close();
- }
-
- /** Confirms that calling join() does not cause an implicit bind() to take place. */
- public void test_joinAnySource_doesNotCauseBind_IPv4() throws Exception {
- test_joinAnySource_doesNotCauseBind(ipv4networkInterfaces, GOOD_MULTICAST_IPv4);
- }
-
- public void test_joinAnySource_doesNotCauseBind_IPv6() throws Exception {
- test_joinAnySource_doesNotCauseBind(ipv6networkInterfaces, GOOD_MULTICAST_IPv6);
- }
-
- private void test_joinAnySource_doesNotCauseBind(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group) throws IOException {
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = DatagramChannel.open();
- dc.join(group, networkInterface);
- assertNull(dc.getLocalAddress());
- dc.close();
- }
- }
-
- public void test_joinAnySource_networkInterfaces() throws Exception {
- // Check that we can join on specific interfaces and that we only receive if data is
- // received on that interface. This test is only really useful on devices with multiple
- // non-loopback interfaces.
-
- ArrayList<NetworkInterface> realInterfaces = new ArrayList<NetworkInterface>();
- Enumeration<NetworkInterface> theInterfaces = NetworkInterface.getNetworkInterfaces();
- while (theInterfaces.hasMoreElements()) {
- NetworkInterface thisInterface = theInterfaces.nextElement();
- // Skip interfaces that do not support multicast - there's no point in proving they cannot
- // send / receive multicast messages.
- if (willWorkForMulticast(thisInterface)) {
- realInterfaces.add(thisInterface);
- }
- }
-
- for (NetworkInterface thisInterface : realInterfaces) {
- // Get the first address on the interface.
- Enumeration<InetAddress> addresses = thisInterface.getInetAddresses();
-
- while (addresses.hasMoreElements()) {
- InetAddress listenAddress = addresses.nextElement();
-
- // Start a server which is joined to the group and has only asked for packets on this
- // interface.
- NetworkInterface sendingInterface;
- InetAddress group;
- if (listenAddress instanceof Inet4Address) {
- group = GOOD_MULTICAST_IPv4;
- sendingInterface = ipv4networkInterface;
- } else {
- group = GOOD_MULTICAST_IPv6;
- sendingInterface = ipv6networkInterface;
- }
- DatagramChannel dc = createReceiverChannel();
- InetSocketAddress localAddress = (InetSocketAddress) dc.getLocalAddress();
- dc.join(group, thisInterface);
-
- // Now send out a packet on sendingInterface. We should only see the packet if we send
- // it on the same interface we are listening on (thisInterface).
- String msg = "Hello World - Again " + thisInterface.getName();
- sendMessage(group, localAddress.getPort(), msg, sendingInterface);
-
- ByteBuffer recvBuffer = ByteBuffer.allocate(100);
- SocketAddress sourceAddress = dc.receive(recvBuffer);
- if (thisInterface.equals(sendingInterface)) {
- assertEquals(msg, new String(recvBuffer.array(), 0, recvBuffer.position()));
- } else {
- assertNull(sourceAddress);
- }
-
- dc.close();
- }
- }
- }
-
- /** Confirms that the scope of each membership is network interface-level. */
- public void test_join_canMixJoinTypesOnDifferentInterfaces() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- MembershipKey membershipKey1 = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- MembershipKey membershipKey2 = dc.join(GOOD_MULTICAST_IPv4, loopbackInterface, UNICAST_IPv4_1);
- assertNotSame(membershipKey1, membershipKey2);
-
- dc.close();
- }
-
- public void test_joinAnySource_multiple_joins_IPv4()
- throws Exception {
- test_joinAnySource_multiple_joins(ipv4networkInterfaces, GOOD_MULTICAST_IPv4);
- }
-
- public void test_joinAnySource_multiple_joins_IPv6()
- throws Exception {
- test_joinAnySource_multiple_joins(ipv6networkInterfaces, GOOD_MULTICAST_IPv6);
- }
-
- private void test_joinAnySource_multiple_joins(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group) throws Exception {
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = createReceiverChannel();
-
- MembershipKey membershipKey1 = dc.join(group, networkInterface);
-
- MembershipKey membershipKey2 = dc.join(group, loopbackInterface);
- assertFalse(membershipKey1.equals(membershipKey2));
-
- MembershipKey membershipKey1_2 = dc.join(group, networkInterface);
- assertEquals(membershipKey1, membershipKey1_2);
-
- dc.close();
- }
- }
-
- public void test_joinAnySource_multicastLoopOption_IPv4() throws Exception {
- test_joinAnySource_multicastLoopOption(ipv4networkInterfaces, GOOD_MULTICAST_IPv4);
- }
-
- public void test_joinAnySource_multicastLoopOption_IPv6() throws Exception {
- test_joinAnySource_multicastLoopOption(ipv6networkInterfaces, GOOD_MULTICAST_IPv6);
- }
-
- private void test_joinAnySource_multicastLoopOption(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group) throws Exception {
- final String message = "Hello, world!";
-
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = DatagramChannel.open();
- configureChannelForReceiving(dc);
- dc.bind(null /* leave the OS to determine the port, and use the wildcard address */);
-
- // Make sure that the sent packets will be sent via the interface we will be joining with.
- dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);
- dc.join(group, networkInterface);
-
- // Test with loop on.
- dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, true /* enable loop */);
-
- InetSocketAddress localAddress = (InetSocketAddress) dc.getLocalAddress();
-
- // Send the datagram.
- sendMessage(dc, message, new InetSocketAddress(group, localAddress.getPort()));
-
- // Receive the datagram.
- ByteBuffer recvBuffer = ByteBuffer.allocate(100);
- SocketAddress sourceAddress = dc.receive(recvBuffer);
- assertNotNull(sourceAddress);
-
- String recvMessage = new String(recvBuffer.array(), 0, recvBuffer.position());
- assertEquals(message, recvMessage);
-
- // Turn off loop.
- dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false /* enable loopback */);
-
- // Send another datagram.
- recvBuffer.position(0);
- sendMessage(dc, message, new InetSocketAddress(group, localAddress.getPort()));
-
- SocketAddress sourceAddress2 = dc.receive(recvBuffer);
- assertNull(sourceAddress2);
-
- dc.close();
- }
- }
-
- public void testMembershipKeyAccessors_IPv4() throws Exception {
- testMembershipKeyAccessors(ipv4networkInterfaces, GOOD_MULTICAST_IPv4);
- }
-
- public void testMembershipKeyAccessors_IPv6() throws Exception {
- testMembershipKeyAccessors(ipv6networkInterfaces, GOOD_MULTICAST_IPv6);
- }
-
- private void testMembershipKeyAccessors(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group) throws Exception {
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = createReceiverChannel();
-
- MembershipKey key = dc.join(group, networkInterface);
- assertSame(dc, key.channel());
- assertSame(group, key.group());
- assertTrue(key.isValid());
- assertSame(networkInterface, key.networkInterface());
- assertNull(key.sourceAddress());
- }
- }
-
- public void test_dropAnySource_twice_IPv4() throws Exception {
- test_dropAnySource_twice(ipv4networkInterfaces, GOOD_MULTICAST_IPv4);
- }
-
- public void test_dropAnySource_twice_IPv6() throws Exception {
- test_dropAnySource_twice(ipv6networkInterfaces, GOOD_MULTICAST_IPv6);
- }
-
- private void test_dropAnySource_twice(
- Iterable<NetworkInterface> networkInterfaces, InetAddress group)
- throws Exception {
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(group, networkInterface);
-
- assertTrue(membershipKey.isValid());
- membershipKey.drop();
- assertFalse(membershipKey.isValid());
-
- // Try to leave a group we are no longer a member of. It should do nothing.
- membershipKey.drop();
-
- dc.close();
- }
- }
-
- public void test_close_invalidatesMembershipKey() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
-
- assertTrue(membershipKey.isValid());
-
- dc.close();
-
- assertFalse(membershipKey.isValid());
- }
-
- public void test_block_null() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- try {
- membershipKey.block(null);
- fail();
- } catch (NullPointerException expected) {
- }
-
- dc.close();
- }
-
- public void test_block_mixedAddressTypes_IPv4() throws Exception {
- test_block_illegalArgument(ipv4networkInterfaces, GOOD_MULTICAST_IPv4, UNICAST_IPv6_1);
- }
-
- public void test_block_mixedAddressTypes_IPv6() throws Exception {
- test_block_illegalArgument(ipv6networkInterfaces, GOOD_MULTICAST_IPv6, UNICAST_IPv4_1);
- }
-
- private void test_block_illegalArgument(
- Iterable<NetworkInterface> networkInterfaces, InetAddress groupAddress,
- InetAddress badBlockAddress) throws Exception {
-
- for (NetworkInterface networkInterface : networkInterfaces) {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(groupAddress, networkInterface);
- try {
- membershipKey.block(badBlockAddress);
- fail();
- } catch (IllegalArgumentException expected) {
- }
-
- dc.close();
- }
- }
-
- public void test_block_cannotBlockWithSourceSpecificMembership() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey =
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, UNICAST_IPv4_1);
- try {
- membershipKey.block(UNICAST_IPv4_2);
- fail();
- } catch (IllegalStateException expected) {
- }
-
- dc.close();
- }
-
- public void test_block_multipleBlocksIgnored() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- membershipKey.block(UNICAST_IPv4_1);
-
- MembershipKey membershipKey2 = membershipKey.block(UNICAST_IPv4_1);
- assertSame(membershipKey2, membershipKey);
-
- dc.close();
- }
-
- public void test_block_wildcardAddress() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- try {
- membershipKey.block(WILDCARD_IPv4);
- fail();
- } catch (IllegalArgumentException expected) {
- }
-
- dc.close();
- }
-
- public void test_unblock_multipleUnblocksFail() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
-
- try {
- membershipKey.unblock(UNICAST_IPv4_1);
- fail();
- } catch (IllegalStateException expected) {
- }
-
- assertTrue(membershipKey.isValid());
-
- membershipKey.block(UNICAST_IPv4_1);
- membershipKey.unblock(UNICAST_IPv4_1);
-
- try {
- membershipKey.unblock(UNICAST_IPv4_1);
- fail();
- } catch (IllegalStateException expected) {
- }
-
- dc.close();
- }
-
- public void test_unblock_null() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- membershipKey.block(UNICAST_IPv4_1);
-
- try {
- membershipKey.unblock(null);
- fail();
- } catch (IllegalStateException expected) {
- // Either of these exceptions are fine.
- } catch (NullPointerException expected) {
- // Either of these exception are fine.
- }
-
- dc.close();
- }
-
- public void test_unblock_mixedAddressTypes_IPv4() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface);
- try {
- membershipKey.unblock(UNICAST_IPv6_1);
- fail();
- } catch (IllegalStateException expected) {
- // Either of these exceptions are fine.
- } catch (IllegalArgumentException expected) {
- // Either of these exceptions are fine.
- }
-
- dc.close();
- }
-
- public void test_unblock_mixedAddressTypes_IPv6() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv6, ipv6networkInterface);
- try {
- membershipKey.unblock(UNICAST_IPv4_1);
- fail();
- } catch (IllegalStateException expected) {
- // Either of these exceptions are fine.
- } catch (IllegalArgumentException expected) {
- // Either of these exceptions are fine.
- }
-
- dc.close();
- }
-
- /** Checks that block() works when the receiver is bound to the multicast group address */
- public void test_block_filtersAsExpected_groupBind_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- InetAddress ipv4LocalAddress = getLocalIpv4Address(networkInterface);
- test_block_filtersAsExpected(
- networkInterface,
- ipv4LocalAddress /* senderBindAddress */,
- GOOD_MULTICAST_IPv4 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv4 /* groupAddress */);
- }
- }
-
- /** Checks that block() works when the receiver is bound to the multicast group address */
- public void test_block_filtersAsExpected_groupBind_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- InetAddress ipv6LocalAddress = getLocalIpv6Address(networkInterface);
- test_block_filtersAsExpected(
- networkInterface,
- ipv6LocalAddress /* senderBindAddress */,
- GOOD_MULTICAST_IPv6 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv6 /* groupAddress */);
- }
- }
-
- /** Checks that block() works when the receiver is bound to the "any" address */
- public void test_block_filtersAsExpected_anyBind_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- InetAddress ipv4LocalAddress = getLocalIpv4Address(networkInterface);
- test_block_filtersAsExpected(
- networkInterface,
- ipv4LocalAddress /* senderBindAddress */,
- WILDCARD_IPv4 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv4 /* groupAddress */);
- }
- }
-
- /** Checks that block() works when the receiver is bound to the "any" address */
- public void test_block_filtersAsExpected_anyBind_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- InetAddress ipv6LocalAddress = getLocalIpv6Address(networkInterface);
- test_block_filtersAsExpected(
- networkInterface,
- ipv6LocalAddress /* senderBindAddress */,
- WILDCARD_IPv6 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv6 /* groupAddress */);
- }
- }
-
- private void test_block_filtersAsExpected(
- NetworkInterface networkInterface, InetAddress senderBindAddress,
- InetAddress receiverBindAddress, InetAddress groupAddress) throws Exception {
-
- DatagramChannel sendingChannel = DatagramChannel.open();
- // In order to block a sender the sender's address must be known. The sendingChannel is
- // explicitly bound to a known, non-loopback address.
- sendingChannel.bind(new InetSocketAddress(senderBindAddress, 0));
- InetSocketAddress sendingAddress = (InetSocketAddress) sendingChannel.getLocalAddress();
-
- DatagramChannel receivingChannel = DatagramChannel.open();
- configureChannelForReceiving(receivingChannel);
- receivingChannel.bind(
- new InetSocketAddress(receiverBindAddress, 0) /* local port left to the OS to determine */);
- InetSocketAddress localReceivingAddress =
- (InetSocketAddress) receivingChannel.getLocalAddress();
- InetSocketAddress groupSocketAddress =
- new InetSocketAddress(groupAddress, localReceivingAddress.getPort());
- MembershipKey membershipKey =
- receivingChannel.join(groupSocketAddress.getAddress(), networkInterface);
-
- ByteBuffer receiveBuffer = ByteBuffer.allocate(10);
-
- // Send a message. It should be received.
- String msg1 = "Hello1";
- sendMessage(sendingChannel, msg1, groupSocketAddress);
- InetSocketAddress sourceAddress1 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
- assertEquals(sendingAddress, sourceAddress1);
- assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
-
- // Now block the sender.
- membershipKey.block(sendingAddress.getAddress());
-
- // Send a message. It should be filtered.
- String msg2 = "Hello2";
- sendMessage(sendingChannel, msg2, groupSocketAddress);
- receiveBuffer.position(0);
- InetSocketAddress sourceAddress2 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
- assertNull(sourceAddress2);
-
- // Now unblock the sender.
- membershipKey.unblock(sendingAddress.getAddress());
-
- // Send a message. It should be received.
- String msg3 = "Hello3";
- sendMessage(sendingChannel, msg3, groupSocketAddress);
- receiveBuffer.position(0);
- InetSocketAddress sourceAddress3 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
- assertEquals(sourceAddress3, sendingAddress);
- assertEquals(msg3, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
-
- sendingChannel.close();
- receivingChannel.close();
- }
-
- public void test_joinSourceSpecific_nullGroupAddress() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(null, ipv4networkInterface, UNICAST_IPv4_1);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_afterClose() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- dc.close();
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, UNICAST_IPv4_1);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
-
- public void test_joinSourceSpecific_nullNetworkInterface() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv4, null, UNICAST_IPv4_1);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nonMulticastGroupAddress_IPv4() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(UNICAST_IPv4_1, ipv4networkInterface, UNICAST_IPv4_1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nonMulticastGroupAddress_IPv6() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(UNICAST_IPv6_1, ipv6networkInterface, UNICAST_IPv6_1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nullSourceAddress_IPv4() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, null);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nullSourceAddress_IPv6() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv6, ipv6networkInterface, null);
- fail();
- } catch (NullPointerException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_mixedAddressTypes() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, UNICAST_IPv6_1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- try {
- dc.join(GOOD_MULTICAST_IPv6, ipv6networkInterface, UNICAST_IPv4_1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nonUnicastSourceAddress_IPv4() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, BAD_MULTICAST_IPv4);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_nonUnicastSourceAddress_IPv6() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- try {
- dc.join(GOOD_MULTICAST_IPv6, ipv6networkInterface, BAD_MULTICAST_IPv6);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- dc.close();
- }
-
- public void test_joinSourceSpecific_multipleSourceAddressLimit() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- for (byte i = 1; i <= 20; i++) {
- InetAddress sourceAddress = Inet4Address.getByAddress(new byte[] { 10, 0, 0, i});
- try {
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, sourceAddress);
- } catch (SocketException e) {
- // There is a limit, that's ok according to the RI docs. For this test a lower bound of 10
- // is used, which appears to be the default linux limit. See /proc/sys/net/ipv4/igmp_max_msf
- assertTrue(i > 10);
- break;
- }
- }
-
- dc.close();
- }
-
- /**
- * Checks that a source-specific join() works when the receiver is bound to the multicast group
- * address
- */
- public void test_joinSourceSpecific_groupBind_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- InetAddress ipv4LocalAddress = getLocalIpv4Address(networkInterface);
- test_joinSourceSpecific(
- networkInterface,
- ipv4LocalAddress /* senderBindAddress */,
- GOOD_MULTICAST_IPv4 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv4 /* groupAddress */,
- UNICAST_IPv4_1 /* badSenderAddress */);
- }
- }
-
- /**
- * Checks that a source-specific join() works when the receiver is bound to the multicast group
- * address
- */
- public void test_joinSourceSpecific_groupBind_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- InetAddress ipv6LocalAddress = getLocalIpv6Address(networkInterface);
- test_joinSourceSpecific(
- networkInterface,
- ipv6LocalAddress /* senderBindAddress */,
- GOOD_MULTICAST_IPv6 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv6 /* groupAddress */,
- UNICAST_IPv6_1 /* badSenderAddress */);
- }
- }
-
- /** Checks that a source-specific join() works when the receiver is bound to the "any" address */
- public void test_joinSourceSpecific_anyBind_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- InetAddress ipv4LocalAddress = getLocalIpv4Address(networkInterface);
- test_joinSourceSpecific(
- networkInterface,
- ipv4LocalAddress /* senderBindAddress */,
- WILDCARD_IPv4 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv4 /* groupAddress */,
- UNICAST_IPv4_1 /* badSenderAddress */);
- }
- }
-
- /** Checks that a source-specific join() works when the receiver is bound to the "any" address */
- public void test_joinSourceSpecific_anyBind_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- InetAddress ipv6LocalAddress = getLocalIpv6Address(networkInterface);
- test_joinSourceSpecific(
- networkInterface,
- ipv6LocalAddress /* senderBindAddress */,
- WILDCARD_IPv6 /* receiverBindAddress */,
- GOOD_MULTICAST_IPv6 /* groupAddress */,
- UNICAST_IPv6_1 /* badSenderAddress */);
- }
- }
-
- /**
- * Checks that the source-specific membership is correctly source-filtered.
- *
- * @param networkInterface the network interface to use when joining
- * @param senderBindAddress the address to bind the sender socket to
- * @param receiverBindAddress the address to bind the receiver socket to
- * @param groupAddress the group address to join
- * @param badSenderAddress a unicast address to join to perform a negative test
- */
- private void test_joinSourceSpecific(
- NetworkInterface networkInterface, InetAddress senderBindAddress,
- InetAddress receiverBindAddress, InetAddress groupAddress, InetAddress badSenderAddress)
- throws Exception {
- DatagramChannel sendingChannel = DatagramChannel.open();
- // In order to be source-specific the sender's address must be known. The sendingChannel is
- // explicitly bound to a known, non-loopback address.
- sendingChannel.bind(new InetSocketAddress(senderBindAddress, 0));
- InetSocketAddress sendingAddress = (InetSocketAddress) sendingChannel.getLocalAddress();
-
- DatagramChannel receivingChannel = DatagramChannel.open();
- configureChannelForReceiving(receivingChannel);
- receivingChannel.bind(
- new InetSocketAddress(receiverBindAddress, 0) /* local port left to the OS to determine */);
-
- InetSocketAddress localReceivingAddress =
- (InetSocketAddress) receivingChannel.getLocalAddress();
- InetSocketAddress groupSocketAddress =
- new InetSocketAddress(groupAddress, localReceivingAddress.getPort());
- MembershipKey membershipKey1 = receivingChannel
- .join(groupSocketAddress.getAddress(), networkInterface, senderBindAddress);
-
- ByteBuffer receiveBuffer = ByteBuffer.allocate(10);
-
- // Send a message. It should be received.
- String msg1 = "Hello1";
- sendMessage(sendingChannel, msg1, groupSocketAddress);
- InetSocketAddress sourceAddress1 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
- assertEquals(sourceAddress1, sendingAddress);
- assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
-
- membershipKey1.drop();
-
- receivingChannel.join(groupSocketAddress.getAddress(), networkInterface, badSenderAddress);
-
- // Send a message. It should not be received.
- String msg2 = "Hello2";
- sendMessage(sendingChannel, msg2, groupSocketAddress);
- InetSocketAddress sourceAddress2 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
- assertNull(sourceAddress2);
-
- receivingChannel.close();
- sendingChannel.close();
- }
-
- public void test_dropSourceSpecific_twice_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- test_dropSourceSpecific_twice(
- networkInterface,
- GOOD_MULTICAST_IPv4 /* groupAddress */,
- UNICAST_IPv4_1 /* sourceAddress */);
- }
- }
-
- public void test_dropSourceSpecific_twice_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- test_dropSourceSpecific_twice(
- networkInterface,
- GOOD_MULTICAST_IPv6 /* groupAddress */,
- UNICAST_IPv6_1 /* sourceAddress */);
- }
- }
-
- private void test_dropSourceSpecific_twice(
- NetworkInterface networkInterface, InetAddress groupAddress, InetAddress sourceAddress)
- throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey = dc.join(groupAddress, networkInterface, sourceAddress);
-
- assertTrue(membershipKey.isValid());
- membershipKey.drop();
- assertFalse(membershipKey.isValid());
-
- // Try to leave a group we are no longer a member of. It should do nothing.
- membershipKey.drop();
-
- dc.close();
- }
-
- public void test_dropSourceSpecific_sourceKeysAreIndependent_IPv4() throws Exception {
- for (NetworkInterface networkInterface : ipv4networkInterfaces) {
- test_dropSourceSpecific_sourceKeysAreIndependent(
- networkInterface,
- GOOD_MULTICAST_IPv4 /* groupAddress */,
- UNICAST_IPv4_1 /* sourceAddress1 */,
- UNICAST_IPv4_2 /* sourceAddress2 */);
- }
- }
-
- public void test_dropSourceSpecific_sourceKeysAreIndependent_IPv6() throws Exception {
- for (NetworkInterface networkInterface : ipv6networkInterfaces) {
- test_dropSourceSpecific_sourceKeysAreIndependent(
- networkInterface,
- GOOD_MULTICAST_IPv6 /* groupAddress */,
- UNICAST_IPv6_1 /* sourceAddress1 */,
- UNICAST_IPv6_2 /* sourceAddress2 */);
- }
- }
-
- private void test_dropSourceSpecific_sourceKeysAreIndependent(
- NetworkInterface networkInterface, InetAddress groupAddress, InetAddress sourceAddress1,
- InetAddress sourceAddress2)
- throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey1 = dc.join(groupAddress, networkInterface, sourceAddress1);
- MembershipKey membershipKey2 = dc.join(groupAddress, networkInterface, sourceAddress2);
- assertFalse(membershipKey1.equals(membershipKey2));
- assertTrue(membershipKey1.isValid());
- assertTrue(membershipKey2.isValid());
-
- membershipKey1.drop();
-
- assertFalse(membershipKey1.isValid());
- assertTrue(membershipKey2.isValid());
-
- dc.close();
- }
-
- public void test_drop_keyBehaviorAfterDrop() throws Exception {
- DatagramChannel dc = createReceiverChannel();
- MembershipKey membershipKey =
- dc.join(GOOD_MULTICAST_IPv4, ipv4networkInterface, UNICAST_IPv4_1);
- membershipKey.drop();
- assertFalse(membershipKey.isValid());
-
- try {
- membershipKey.block(UNICAST_IPv4_1);
- } catch (IllegalStateException expected) {
- }
-
- try {
- membershipKey.unblock(UNICAST_IPv4_1);
- } catch (IllegalStateException expected) {
- }
-
- assertSame(dc, membershipKey.channel());
- assertSame(GOOD_MULTICAST_IPv4, membershipKey.group());
- assertSame(UNICAST_IPv4_1, membershipKey.sourceAddress());
- assertSame(ipv4networkInterface, membershipKey.networkInterface());
- }
-
- private static DatagramChannel createReceiverChannel() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- configureChannelForReceiving(dc);
- dc.bind(null /* leave the OS to determine the port, and use the wildcard address */);
- return dc;
- }
-
- private static void configureChannelForReceiving(DatagramChannel receivingChannel)
- throws Exception {
-
- // NOTE: At the time of writing setSoTimeout() has no effect in the RI, making these tests hang
- // if the channel is in blocking mode.
- receivingChannel.socket().setSoTimeout(200);
- receivingChannel.configureBlocking(true);
- // configureBlocking(false) can be used instead in the RI and we rely on the network to the
- // local host being instantaneous.
- // receivingChannel.configureBlocking(false);
- }
-
- private static boolean willWorkForMulticast(NetworkInterface iface) throws IOException {
- return iface.isUp()
- // Typically loopback interfaces do not support multicast, but they are ruled out
- // explicitly here anyway.
- && !iface.isLoopback() && iface.supportsMulticast()
- && iface.getInetAddresses().hasMoreElements();
- }
-
- private static void sendMessage(
- InetAddress targetGroup, int targetPort, String msg, NetworkInterface sendingInterface)
- throws IOException {
- // Any datagram socket can send to a group. It does not need to have joined the group.
- DatagramChannel dc = DatagramChannel.open();
- dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, sendingInterface);
- sendMessage(dc, msg, new InetSocketAddress(targetGroup, targetPort));
- dc.close();
- }
-
- private static void sendMessage(
- DatagramChannel sendingChannel, String msg, InetSocketAddress targetAddress)
- throws IOException {
-
- ByteBuffer sendBuffer = ByteBuffer.wrap(msg.getBytes());
- sendingChannel.send(sendBuffer, targetAddress);
- }
-
- private static InetAddress getLocalIpv4Address(NetworkInterface networkInterface) {
- for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
- if (interfaceAddress.getAddress() instanceof Inet4Address) {
- return interfaceAddress.getAddress();
- }
- }
- throw new AssertionFailedError("Unable to find local IPv4 address for " + networkInterface);
- }
-
- private static InetAddress getLocalIpv6Address(NetworkInterface networkInterface) {
- for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
- if (interfaceAddress.getAddress() instanceof Inet6Address) {
- return interfaceAddress.getAddress();
- }
- }
- throw new AssertionFailedError("Unable to find local IPv6 address for " + networkInterface);
- }
-
-}
diff --git a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelTest.java
index cd4bb22..efcfece 100644
--- a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelTest.java
@@ -23,9 +23,6 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
-import java.net.SocketException;
-import java.net.SocketOption;
-import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
@@ -61,7 +58,7 @@ public class DatagramChannelTest extends junit.framework.TestCase {
DatagramChannel dc = DatagramChannel.open();
try {
dc.configureBlocking(false);
- dc.bind(null);
+ dc.socket().bind(null);
// Should return immediately, since we're non-blocking.
assertNull(dc.receive(ByteBuffer.allocate(2048)));
} finally {
@@ -72,8 +69,6 @@ public class DatagramChannelTest extends junit.framework.TestCase {
public void testInitialState() throws Exception {
DatagramChannel dc = DatagramChannel.open();
try {
- assertNull(dc.getLocalAddress());
-
DatagramSocket socket = dc.socket();
assertFalse(socket.isBound());
assertFalse(socket.getBroadcast());
@@ -93,408 +88,20 @@ public class DatagramChannelTest extends junit.framework.TestCase {
}
}
- public void test_supportedOptions() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- Set<SocketOption<?>> options = dc.supportedOptions();
-
- // Probe some values. This is not intended to be complete.
- assertTrue(options.contains(StandardSocketOptions.SO_REUSEADDR));
- assertFalse(options.contains(StandardSocketOptions.TCP_NODELAY));
- }
-
- public void test_getOption_unsupportedOption() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- try {
- dc.getOption(StandardSocketOptions.TCP_NODELAY);
- fail();
- } catch (UnsupportedOperationException expected) {}
-
- dc.close();
- }
-
- public void test_getOption_afterClose() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.close();
-
- try {
- dc.getOption(StandardSocketOptions.SO_RCVBUF);
- fail();
- } catch (ClosedChannelException expected) {}
- }
-
- public void test_setOption_afterClose() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.close();
-
- try {
- dc.setOption(StandardSocketOptions.SO_RCVBUF, 1234);
- fail();
- } catch (ClosedChannelException expected) {}
- }
-
- public void test_getOption_SO_RCVBUF_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- int value = dc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertTrue(value > 0);
- assertEquals(value, dc.socket().getReceiveBufferSize());
-
- dc.close();
- }
-
- public void test_setOption_SO_RCVBUF_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- trySetReceiveBufferSizeOption(dc);
-
- dc.close();
- }
-
- private static void trySetReceiveBufferSizeOption(DatagramChannel dc) throws IOException {
- int initialValue = dc.getOption(StandardSocketOptions.SO_RCVBUF);
- try {
- dc.setOption(StandardSocketOptions.SO_RCVBUF, -1);
- fail();
- } catch (IllegalArgumentException expected) {}
- int actualValue = dc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertEquals(initialValue, actualValue);
- assertEquals(initialValue, dc.socket().getReceiveBufferSize());
-
- int newBufferSize = initialValue - 1;
- dc.setOption(StandardSocketOptions.SO_RCVBUF, newBufferSize);
- actualValue = dc.getOption(StandardSocketOptions.SO_RCVBUF);
- // The Linux Kernel actually doubles the value it is given and may choose to ignore it.
- // This assertion may be brittle.
- assertTrue(actualValue != initialValue);
- assertEquals(actualValue, dc.socket().getReceiveBufferSize());
- }
-
- public void test_getOption_SO_SNDBUF_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- int value = dc.getOption(StandardSocketOptions.SO_SNDBUF);
- assertTrue(value > 0);
- assertEquals(value, dc.socket().getSendBufferSize());
-
- dc.close();
- }
-
- public void test_setOption_SO_SNDBUF_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- trySetSendBufferSizeOption(dc);
-
- dc.close();
- }
-
- private static void trySetSendBufferSizeOption(DatagramChannel dc) throws IOException {
- int initialValue = dc.getOption(StandardSocketOptions.SO_SNDBUF);
- try {
- dc.setOption(StandardSocketOptions.SO_SNDBUF, -1);
- fail();
- } catch (IllegalArgumentException expected) {}
- int actualValue = dc.getOption(StandardSocketOptions.SO_SNDBUF);
- assertEquals(initialValue, actualValue);
- assertEquals(initialValue, dc.socket().getSendBufferSize());
-
- int newBufferSize = initialValue - 1;
- dc.setOption(StandardSocketOptions.SO_SNDBUF, newBufferSize);
- actualValue = dc.getOption(StandardSocketOptions.SO_SNDBUF);
- // The Linux Kernel actually doubles the value it is given and may choose to ignore it.
- // This assertion may be brittle.
- assertTrue(actualValue != initialValue);
- assertEquals(actualValue, dc.socket().getSendBufferSize());
- }
-
- public void test_getOption_IP_MULTICAST_IF_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- NetworkInterface networkInterface = dc.getOption(StandardSocketOptions.IP_MULTICAST_IF);
- assertNull(networkInterface);
-
- dc.close();
- }
-
- public void test_getOption_IP_MULTICAST_IF_nullCheck() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- try {
- dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, null);
- fail();
- } catch (IllegalArgumentException expected) {}
-
- dc.close();
- }
-
- public void test_setOption_IP_MULTICAST_IF_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
- assertTrue(networkInterfaces.hasMoreElements());
- while (networkInterfaces.hasMoreElements()) {
- trySetNetworkInterfaceOption(dc, networkInterfaces.nextElement());
- }
-
- dc.close();
- }
-
- public void test_setOption_IP_MULTICAST_IF_afterBind() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.bind(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0));
-
- Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
- assertTrue(networkInterfaces.hasMoreElements());
- while (networkInterfaces.hasMoreElements()) {
- trySetNetworkInterfaceOption(dc, networkInterfaces.nextElement());
- }
-
- dc.close();
- }
-
- private static void trySetNetworkInterfaceOption(
- DatagramChannel dc, NetworkInterface networkInterface) throws IOException {
-
- NetworkInterface initialValue = dc.getOption(StandardSocketOptions.IP_MULTICAST_IF);
- try {
- dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, null);
- fail();
- } catch (IllegalArgumentException expected) {}
- assertEquals(initialValue, dc.getOption(StandardSocketOptions.IP_MULTICAST_IF));
-
- dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);
- NetworkInterface actualValue =
- dc.getOption(StandardSocketOptions.IP_MULTICAST_IF);
- assertEquals(networkInterface, actualValue);
- }
-
- public void test_getOption_IP_MULTICAST_LOOP_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- assertTrue(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- dc.close();
- }
-
- public void test_getOption_IP_MULTICAST_LOOP_nullCheck() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- try {
- dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, null);
- fail();
- } catch (IllegalArgumentException expected) {}
-
- dc.close();
- }
-
- public void test_setOption_IP_MULTICAST_LOOP_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- assertTrue(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false);
- assertFalse(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- dc.close();
- }
-
- public void test_setOption_IP_MULTICAST_LOOP_afterBind() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.bind(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0));
-
- assertTrue(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false);
- assertFalse(dc.getOption(StandardSocketOptions.IP_MULTICAST_LOOP));
-
- dc.close();
- }
-
- public void test_getOption_IP_MULTICAST_TTL_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- int value = dc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- assertEquals(1, value);
-
- dc.close();
- }
-
- public void test_setOption_IP_MULTICAST_TTL_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- trySetMulticastTtlOption(dc);
-
- dc.close();
- }
-
- private static void trySetMulticastTtlOption(DatagramChannel dc) throws IOException {
- int initialValue = dc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- try {
- dc.setOption(StandardSocketOptions.IP_MULTICAST_TTL, -1);
- fail();
- } catch (IllegalArgumentException expected) {}
- int actualValue = dc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- assertEquals(initialValue, actualValue);
-
- int newTtl = initialValue + 1;
- dc.setOption(StandardSocketOptions.IP_MULTICAST_TTL, newTtl);
- actualValue = dc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- assertEquals(newTtl, actualValue);
- }
-
- public void test_setOption_IP_MULTICAST_TTL_afterBind() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.bind(null);
-
- trySetMulticastTtlOption(dc);
-
- dc.close();
- }
-
- public void test_getOption_SO_BROADCAST_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- assertFalse(dc.getOption(StandardSocketOptions.SO_BROADCAST));
-
- dc.close();
- }
-
- public void test_setOption_SO_BROADCAST_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- trySetSoBroadcastOption(dc);
-
- dc.close();
- }
-
- private static void trySetSoBroadcastOption(DatagramChannel dc) throws IOException {
- boolean initialValue = dc.getOption(StandardSocketOptions.SO_BROADCAST);
-
- dc.setOption(StandardSocketOptions.SO_BROADCAST, !initialValue);
- boolean actualValue = dc.getOption(StandardSocketOptions.SO_BROADCAST);
- assertEquals(!initialValue, actualValue);
- }
-
- public void test_setOption_SO_BROADCAST_afterBind() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.bind(null);
-
- trySetSoBroadcastOption(dc);
-
- dc.close();
- }
-
- public void test_getOption_IP_TOS_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- int value = dc.getOption(StandardSocketOptions.IP_TOS);
- assertEquals(0, value);
- assertEquals(value, dc.socket().getTrafficClass());
-
- dc.close();
- }
-
- public void test_setOption_IP_TOS_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- trySetTosOption(dc);
-
- dc.close();
- }
-
- private static void trySetTosOption(DatagramChannel dc) throws IOException {
- int initialValue = dc.getOption(StandardSocketOptions.IP_TOS);
- try {
- dc.setOption(StandardSocketOptions.IP_TOS, -1);
- fail();
- } catch (IllegalArgumentException expected) {}
- assertEquals(initialValue, (int) dc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(initialValue, dc.socket().getTrafficClass());
-
- try {
- dc.setOption(StandardSocketOptions.IP_TOS, 256);
- fail();
- } catch (IllegalArgumentException expected) {}
- assertEquals(initialValue, (int) dc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(initialValue, dc.socket().getTrafficClass());
-
- int newValue = (initialValue + 1) % 255;
- dc.setOption(StandardSocketOptions.IP_TOS, newValue);
- assertEquals(newValue, (int) dc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(newValue, dc.socket().getTrafficClass());
- }
-
- public void test_setOption_IP_TOS_afterBind() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
- dc.bind(null);
-
- trySetTosOption(dc);
-
- dc.close();
- }
-
- public void test_getOption_SO_REUSEADDR_defaults() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- boolean value = dc.getOption(StandardSocketOptions.SO_REUSEADDR);
- assertFalse(value);
- assertFalse(dc.socket().getReuseAddress());
-
- dc.close();
- }
-
- public void test_setOption_SO_REUSEADDR_afterOpen() throws Exception {
- DatagramChannel dc = DatagramChannel.open();
-
- boolean initialValue = dc.getOption(StandardSocketOptions.SO_REUSEADDR);
- dc.setOption(StandardSocketOptions.SO_REUSEADDR, !initialValue);
- assertEquals(!initialValue, (boolean) dc.getOption(StandardSocketOptions.SO_REUSEADDR));
- assertEquals(!initialValue, dc.socket().getReuseAddress());
-
- dc.close();
- }
-
public void test_bind_unresolvedAddress() throws IOException {
DatagramChannel dc = DatagramChannel.open();
try {
- dc.bind(new InetSocketAddress("unresolvedname", 31415));
+ dc.socket().bind(new InetSocketAddress("unresolvedname", 31415));
fail();
- } catch (UnresolvedAddressException expected) {
+ } catch (IOException expected) {
}
- assertNull(dc.getLocalAddress());
assertTrue(dc.isOpen());
assertFalse(dc.isConnected());
dc.close();
}
- public void test_bind_noReuseAddress() throws Exception {
- DatagramChannel dc1 = DatagramChannel.open();
- dc1.setOption(StandardSocketOptions.SO_REUSEADDR, false);
- DatagramChannel dc2 = DatagramChannel.open();
- dc1.setOption(StandardSocketOptions.SO_REUSEADDR, false);
-
- dc1.bind(null);
-
- try {
- dc2.bind(dc1.getLocalAddress());
- fail();
- } catch (IOException expected) {}
-
- dc1.close();
- dc2.close();
- }
-
- public void test_bind_withReuseAddress() throws Exception {
- DatagramChannel dc1 = DatagramChannel.open();
- dc1.setOption(StandardSocketOptions.SO_REUSEADDR, true);
- DatagramChannel dc2 = DatagramChannel.open();
- dc2.setOption(StandardSocketOptions.SO_REUSEADDR, true);
-
- dc1.bind(null);
- dc2.bind(dc1.getLocalAddress());
-
- dc1.close();
- dc2.close();
- }
-
public void test_bind_any_IPv4() throws Exception {
test_bind_any(InetAddress.getByName("0.0.0.0"));
}
@@ -505,12 +112,12 @@ public class DatagramChannelTest extends junit.framework.TestCase {
private void test_bind_any(InetAddress bindAddress) throws Exception {
DatagramChannel dc = DatagramChannel.open();
- dc.bind(new InetSocketAddress(bindAddress, 0));
+ dc.socket().bind(new InetSocketAddress(bindAddress, 0));
assertTrue(dc.isOpen());
assertFalse(dc.isConnected());
- InetSocketAddress actualAddress = (InetSocketAddress) dc.getLocalAddress();
+ InetSocketAddress actualAddress = (InetSocketAddress) dc.socket().getLocalSocketAddress();
assertTrue(actualAddress.getAddress().isAnyLocalAddress());
assertTrue(actualAddress.getPort() > 0);
@@ -537,18 +144,16 @@ public class DatagramChannelTest extends junit.framework.TestCase {
private void test_bind(InetAddress bindAddress) throws IOException {
DatagramChannel dc = DatagramChannel.open();
- dc.bind(new InetSocketAddress(bindAddress, 0));
+ dc.socket().bind(new InetSocketAddress(bindAddress, 0));
- InetSocketAddress actualAddress = (InetSocketAddress) dc.getLocalAddress();
+ InetSocketAddress actualAddress = (InetSocketAddress) dc.socket().getLocalSocketAddress();
assertEquals(bindAddress, actualAddress.getAddress());
assertTrue(actualAddress.getPort() > 0);
dc.close();
}
- private static InetAddress getNonLoopbackNetworkInterfaceAddress(boolean ipv4)
- throws SocketException {
-
+ private static InetAddress getNonLoopbackNetworkInterfaceAddress(boolean ipv4) throws IOException {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
diff --git a/luni/src/test/java/libcore/java/nio/channels/ServerSocketChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/ServerSocketChannelTest.java
index e819d82..1178b70 100644
--- a/luni/src/test/java/libcore/java/nio/channels/ServerSocketChannelTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/ServerSocketChannelTest.java
@@ -21,8 +21,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
-import java.net.SocketOption;
-import java.net.StandardSocketOptions;
+import java.net.SocketException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
@@ -36,7 +35,7 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
ServerSocketChannel ssc = ServerSocketChannel.open();
try {
ssc.configureBlocking(false);
- ssc.bind(null);
+ ssc.socket().bind(null);
// Should return immediately, since we're non-blocking.
assertNull(ssc.accept());
} finally {
@@ -48,7 +47,7 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
public void test_open_initialState() throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
try {
- assertNull(ssc.getLocalAddress());
+ assertNull(ssc.socket().getLocalSocketAddress());
ServerSocket socket = ssc.socket();
assertFalse(socket.isBound());
@@ -67,12 +66,12 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
public void test_bind_unresolvedAddress() throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
try {
- ssc.bind(new InetSocketAddress("unresolvedname", 31415));
+ ssc.socket().bind(new InetSocketAddress("unresolvedname", 31415));
fail();
- } catch (UnresolvedAddressException expected) {
+ } catch (SocketException expected) {
}
- assertNull(ssc.getLocalAddress());
+ assertNull(ssc.socket().getLocalSocketAddress());
assertTrue(ssc.isOpen());
ssc.close();
@@ -80,8 +79,8 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
public void test_bind_nullBindsToAll() throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
- ssc.bind(null);
- InetSocketAddress boundAddress = (InetSocketAddress) ssc.getLocalAddress();
+ ssc.socket().bind(null);
+ InetSocketAddress boundAddress = (InetSocketAddress) ssc.socket().getLocalSocketAddress();
assertTrue(boundAddress.getAddress().isAnyLocalAddress());
assertFalse(boundAddress.getAddress().isLinkLocalAddress());
assertFalse(boundAddress.getAddress().isLoopbackAddress());
@@ -106,8 +105,8 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
public void test_bind_loopback() throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
- ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
- InetSocketAddress boundAddress = (InetSocketAddress) ssc.getLocalAddress();
+ ssc.socket().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
+ InetSocketAddress boundAddress = (InetSocketAddress) ssc.socket().getLocalSocketAddress();
assertFalse(boundAddress.getAddress().isAnyLocalAddress());
assertFalse(boundAddress.getAddress().isLinkLocalAddress());
assertTrue(boundAddress.getAddress().isLoopbackAddress());
@@ -146,102 +145,4 @@ public class ServerSocketChannelTest extends junit.framework.TestCase {
return false;
}
}
-
- public void test_supportedOptions() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
- Set<SocketOption<?>> options = ssc.supportedOptions();
-
- // Probe some values. This is not intended to be complete.
- assertTrue(options.contains(StandardSocketOptions.SO_REUSEADDR));
- assertFalse(options.contains(StandardSocketOptions.IP_MULTICAST_TTL));
- }
-
- public void test_getOption_unsupportedOption() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
- try {
- ssc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- fail();
- } catch (UnsupportedOperationException expected) {}
-
- ssc.close();
- }
-
- public void test_getOption_afterClose() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
- ssc.close();
-
- try {
- ssc.getOption(StandardSocketOptions.SO_RCVBUF);
- fail();
- } catch (ClosedChannelException expected) {}
- }
-
- public void test_setOption_afterClose() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
- ssc.close();
-
- try {
- ssc.setOption(StandardSocketOptions.SO_RCVBUF, 1234);
- fail();
- } catch (ClosedChannelException expected) {}
- }
-
- public void test_getOption_SO_RCVBUF_defaults() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
-
- int value = ssc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertTrue(value > 0);
- assertEquals(value, ssc.socket().getReceiveBufferSize());
-
- ssc.close();
- }
-
- public void test_setOption_SO_RCVBUF_afterOpen() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
-
- trySetReceiveBufferSizeOption(ssc);
-
- ssc.close();
- }
-
- private static void trySetReceiveBufferSizeOption(ServerSocketChannel ssc) throws IOException {
- int initialValue = ssc.getOption(StandardSocketOptions.SO_RCVBUF);
- try {
- ssc.setOption(StandardSocketOptions.SO_RCVBUF, -1);
- fail();
- } catch (IllegalArgumentException expected) {}
- int actualValue = ssc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertEquals(initialValue, actualValue);
- assertEquals(initialValue, ssc.socket().getReceiveBufferSize());
-
- int newBufferSize = initialValue - 1;
- ssc.setOption(StandardSocketOptions.SO_RCVBUF, newBufferSize);
- actualValue = ssc.getOption(StandardSocketOptions.SO_RCVBUF);
- // The Linux Kernel actually doubles the value it is given and may choose to ignore it.
- // This assertion may be brittle.
- assertTrue(actualValue != initialValue);
- assertEquals(actualValue, ssc.socket().getReceiveBufferSize());
- }
-
- public void test_getOption_SO_REUSEADDR_defaults() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
-
- boolean value = ssc.getOption(StandardSocketOptions.SO_REUSEADDR);
- assertTrue(value);
- assertTrue(ssc.socket().getReuseAddress());
-
- ssc.close();
- }
-
- public void test_setOption_SO_REUSEADDR_afterOpen() throws Exception {
- ServerSocketChannel ssc = ServerSocketChannel.open();
-
- boolean initialValue = ssc.getOption(StandardSocketOptions.SO_REUSEADDR);
- ssc.setOption(StandardSocketOptions.SO_REUSEADDR, !initialValue);
- assertEquals(!initialValue, (boolean) ssc.getOption(StandardSocketOptions.SO_REUSEADDR));
- assertEquals(!initialValue, ssc.socket().getReuseAddress());
-
- ssc.close();
- }
-
}
diff --git a/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java
index 6bba862..a54b30a 100644
--- a/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java
@@ -24,8 +24,6 @@ import java.net.Socket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
-import java.net.SocketOption;
-import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;
@@ -143,7 +141,7 @@ public class SocketChannelTest extends junit.framework.TestCase {
public void test_open_initialState() throws Exception {
SocketChannel sc = SocketChannel.open();
try {
- assertNull(sc.getLocalAddress());
+ assertNull(sc.socket().getLocalSocketAddress());
Socket socket = sc.socket();
assertFalse(socket.isBound());
@@ -166,12 +164,12 @@ public class SocketChannelTest extends junit.framework.TestCase {
public void test_bind_unresolvedAddress() throws IOException {
SocketChannel sc = SocketChannel.open();
try {
- sc.bind(new InetSocketAddress("unresolvedname", 31415));
+ sc.socket().bind(new InetSocketAddress("unresolvedname", 31415));
fail();
- } catch (UnresolvedAddressException expected) {
+ } catch (IOException expected) {
}
- assertNull(sc.getLocalAddress());
+ assertNull(sc.socket().getLocalSocketAddress());
assertTrue(sc.isOpen());
assertFalse(sc.isConnected());
@@ -181,16 +179,16 @@ public class SocketChannelTest extends junit.framework.TestCase {
/** Checks that the SocketChannel and associated Socket agree on the socket state. */
public void test_bind_socketStateSync() throws IOException {
SocketChannel sc = SocketChannel.open();
- assertNull(sc.getLocalAddress());
+ assertNull(sc.socket().getLocalSocketAddress());
Socket socket = sc.socket();
assertNull(socket.getLocalSocketAddress());
assertFalse(socket.isBound());
InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0);
- sc.bind(bindAddr);
+ sc.socket().bind(bindAddr);
- InetSocketAddress actualAddr = (InetSocketAddress) sc.getLocalAddress();
+ InetSocketAddress actualAddr = (InetSocketAddress) sc.socket().getLocalSocketAddress();
assertEquals(actualAddr, socket.getLocalSocketAddress());
assertEquals(bindAddr.getHostName(), actualAddr.getHostName());
assertTrue(socket.isBound());
@@ -209,14 +207,14 @@ public class SocketChannelTest extends junit.framework.TestCase {
*/
public void test_bind_socketObjectCreationAfterBind() throws IOException {
SocketChannel sc = SocketChannel.open();
- assertNull(sc.getLocalAddress());
+ assertNull(sc.socket().getLocalSocketAddress());
InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0);
- sc.bind(bindAddr);
+ sc.socket().bind(bindAddr);
// Socket object creation after bind().
Socket socket = sc.socket();
- InetSocketAddress actualAddr = (InetSocketAddress) sc.getLocalAddress();
+ InetSocketAddress actualAddr = (InetSocketAddress) sc.socket().getLocalSocketAddress();
assertEquals(actualAddr, socket.getLocalSocketAddress());
assertEquals(bindAddr.getHostName(), actualAddr.getHostName());
assertTrue(socket.isBound());
@@ -276,307 +274,4 @@ public class SocketChannelTest extends junit.framework.TestCase {
ss.close();
sc.close();
}
-
- public void test_supportedOptions() throws Exception {
- SocketChannel sc = SocketChannel.open();
- Set<SocketOption<?>> options = sc.supportedOptions();
-
- // Probe some values. This is not intended to be complete.
- assertTrue(options.contains(StandardSocketOptions.SO_REUSEADDR));
- assertFalse(options.contains(StandardSocketOptions.IP_MULTICAST_TTL));
- }
-
- public void test_getOption_unsupportedOption() throws Exception {
- SocketChannel sc = SocketChannel.open();
- try {
- sc.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
- fail();
- } catch (UnsupportedOperationException expected) {
- }
-
- sc.close();
- }
-
- public void test_getOption_afterClose() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.close();
-
- try {
- sc.getOption(StandardSocketOptions.SO_RCVBUF);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
-
- public void test_setOption_afterClose() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.close();
-
- try {
- sc.setOption(StandardSocketOptions.SO_RCVBUF, 1234);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
-
- public void test_getOption_SO_RCVBUF_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- int value = sc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertTrue(value > 0);
- assertEquals(value, sc.socket().getReceiveBufferSize());
-
- sc.close();
- }
-
- public void test_setOption_SO_RCVBUF_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetReceiveBufferSizeOption(sc);
-
- sc.close();
- }
-
- private static void trySetReceiveBufferSizeOption(SocketChannel sc) throws IOException {
- int initialValue = sc.getOption(StandardSocketOptions.SO_RCVBUF);
- try {
- sc.setOption(StandardSocketOptions.SO_RCVBUF, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- int actualValue = sc.getOption(StandardSocketOptions.SO_RCVBUF);
- assertEquals(initialValue, actualValue);
- assertEquals(initialValue, sc.socket().getReceiveBufferSize());
-
- int newBufferSize = initialValue - 1;
- sc.setOption(StandardSocketOptions.SO_RCVBUF, newBufferSize);
- actualValue = sc.getOption(StandardSocketOptions.SO_RCVBUF);
- // The Linux Kernel actually doubles the value it is given and may choose to ignore it.
- // This assertion may be brittle.
- assertTrue(actualValue != initialValue);
- assertEquals(actualValue, sc.socket().getReceiveBufferSize());
- }
-
- public void test_getOption_SO_SNDBUF_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- int bufferSize = sc.getOption(StandardSocketOptions.SO_SNDBUF);
- assertTrue(bufferSize > 0);
- assertEquals(bufferSize, sc.socket().getSendBufferSize());
-
- sc.close();
- }
-
- public void test_setOption_SO_SNDBUF_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetSendBufferSizeOption(sc);
-
- sc.close();
- }
-
- private static void trySetSendBufferSizeOption(SocketChannel sc) throws IOException {
- int initialValue = sc.getOption(StandardSocketOptions.SO_SNDBUF);
- try {
- sc.setOption(StandardSocketOptions.SO_SNDBUF, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- int actualValue = sc.getOption(StandardSocketOptions.SO_SNDBUF);
- assertEquals(initialValue, actualValue);
- assertEquals(initialValue, sc.socket().getSendBufferSize());
-
- int newValue = initialValue - 1;
- sc.setOption(StandardSocketOptions.SO_SNDBUF, newValue);
- actualValue = sc.getOption(StandardSocketOptions.SO_SNDBUF);
- // The Linux Kernel actually doubles the value it is given and may choose to ignore it.
- // This assertion may be brittle.
- assertTrue(actualValue != initialValue);
- assertEquals(actualValue, sc.socket().getSendBufferSize());
- }
-
- public void test_getOption_SO_KEEPALIVE_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- assertFalse(sc.getOption(StandardSocketOptions.SO_KEEPALIVE));
-
- sc.close();
- }
-
- public void test_setOption_SO_KEEPALIVE_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetSoKeepaliveOption(sc);
-
- sc.close();
- }
-
- private static void trySetSoKeepaliveOption(SocketChannel sc) throws IOException {
- boolean initialValue = sc.getOption(StandardSocketOptions.SO_KEEPALIVE);
-
- sc.setOption(StandardSocketOptions.SO_KEEPALIVE, !initialValue);
- boolean actualValue = sc.getOption(StandardSocketOptions.SO_KEEPALIVE);
- assertEquals(!initialValue, actualValue);
- }
-
- public void test_setOption_SO_KEEPALIVE_afterBind() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.bind(null);
-
- trySetSoKeepaliveOption(sc);
-
- sc.close();
- }
-
- public void test_getOption_IP_TOS_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- int value = sc.getOption(StandardSocketOptions.IP_TOS);
- assertEquals(0, value);
- assertEquals(value, sc.socket().getTrafficClass());
-
- sc.close();
- }
-
- public void test_setOption_IP_TOS_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetTosOption(sc);
-
- sc.close();
- }
-
- private static void trySetTosOption(SocketChannel sc) throws IOException {
- int initialValue = sc.getOption(StandardSocketOptions.IP_TOS);
- try {
- sc.setOption(StandardSocketOptions.IP_TOS, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- assertEquals(initialValue, (int) sc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(initialValue, sc.socket().getTrafficClass());
-
- try {
- sc.setOption(StandardSocketOptions.IP_TOS, 256);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- assertEquals(initialValue, (int) sc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(initialValue, sc.socket().getTrafficClass());
-
- int newValue = (initialValue + 1) % 255;
- sc.setOption(StandardSocketOptions.IP_TOS, newValue);
- assertEquals(newValue, (int) sc.getOption(StandardSocketOptions.IP_TOS));
- assertEquals(newValue, sc.socket().getTrafficClass());
- }
-
- public void test_setOption_IP_TOS_afterBind() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.bind(null);
-
- trySetTosOption(sc);
-
- sc.close();
- }
-
- public void test_getOption_SO_LINGER_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- int value = sc.getOption(StandardSocketOptions.SO_LINGER);
- assertTrue(value < 0);
- assertEquals(value, sc.socket().getSoLinger());
-
- sc.close();
- }
-
- public void test_setOption_SO_LINGER_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetLingerOption(sc);
-
- sc.close();
- }
-
- private static void trySetLingerOption(SocketChannel sc) throws IOException {
- int initialValue = sc.getOption(StandardSocketOptions.SO_LINGER);
- // Any negative value disables the setting, -1 is used to report SO_LINGER being disabled.
- sc.setOption(StandardSocketOptions.SO_LINGER, -2);
- int soLingerDisabled = -1;
- assertEquals(soLingerDisabled, (int) sc.getOption(StandardSocketOptions.SO_LINGER));
- assertEquals(soLingerDisabled, sc.socket().getSoLinger());
-
- sc.setOption(StandardSocketOptions.SO_LINGER, 65536);
- assertEquals(65535, (int) sc.getOption(StandardSocketOptions.SO_LINGER));
- assertEquals(65535, sc.socket().getSoLinger());
-
- int newValue = (initialValue + 1) % 65535;
- sc.setOption(StandardSocketOptions.SO_LINGER, newValue);
- assertEquals(newValue, (int) sc.getOption(StandardSocketOptions.SO_LINGER));
- assertEquals(newValue, sc.socket().getSoLinger());
- }
-
- public void test_setOption_SO_LINGER_afterBind() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.bind(null);
-
- trySetLingerOption(sc);
-
- sc.close();
- }
-
- public void test_getOption_SO_REUSEADDR_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- boolean value = sc.getOption(StandardSocketOptions.SO_REUSEADDR);
- assertFalse(value);
- assertFalse(sc.socket().getReuseAddress());
-
- sc.close();
- }
-
- public void test_setOption_SO_REUSEADDR_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- boolean initialValue = sc.getOption(StandardSocketOptions.SO_REUSEADDR);
- sc.setOption(StandardSocketOptions.SO_REUSEADDR, !initialValue);
- assertEquals(!initialValue, (boolean) sc.getOption(StandardSocketOptions.SO_REUSEADDR));
- assertEquals(!initialValue, sc.socket().getReuseAddress());
-
- sc.close();
- }
-
- public void test_getOption_TCP_NODELAY_defaults() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- boolean value = sc.getOption(StandardSocketOptions.TCP_NODELAY);
- assertFalse(value);
- assertFalse(sc.socket().getTcpNoDelay());
-
- sc.close();
- }
-
- public void test_setOption_TCP_NODELAY_afterOpen() throws Exception {
- SocketChannel sc = SocketChannel.open();
-
- trySetNoDelay(sc);
-
- sc.close();
- }
-
- private static void trySetNoDelay(SocketChannel sc) throws IOException {
- boolean initialValue = sc.getOption(StandardSocketOptions.TCP_NODELAY);
- sc.setOption(StandardSocketOptions.TCP_NODELAY, !initialValue);
- assertEquals(!initialValue, (boolean) sc.getOption(StandardSocketOptions.TCP_NODELAY));
- assertEquals(!initialValue, sc.socket().getTcpNoDelay());
- }
-
- public void test_setOption_TCP_NODELAY_afterBind() throws Exception {
- SocketChannel sc = SocketChannel.open();
- sc.bind(null);
-
- trySetNoDelay(sc);
-
- sc.close();
- }
-
}
diff --git a/luni/src/test/java/libcore/java/util/CurrencyTest.java b/luni/src/test/java/libcore/java/util/CurrencyTest.java
index fb7fbf6..61a22fd 100644
--- a/luni/src/test/java/libcore/java/util/CurrencyTest.java
+++ b/luni/src/test/java/libcore/java/util/CurrencyTest.java
@@ -67,13 +67,6 @@ public class CurrencyTest extends junit.framework.TestCase {
assertEquals(-1, Currency.getInstance("XXX").getDefaultFractionDigits());
}
- public void test_getNumericCode() throws Exception {
- assertEquals(840, Currency.getInstance("USD").getNumericCode());
- assertEquals(826, Currency.getInstance("GBP").getNumericCode());
- assertEquals(999, Currency.getInstance("XXX").getNumericCode());
- assertEquals(0, Currency.getInstance("XFU").getNumericCode());
- }
-
// http://code.google.com/p/android/issues/detail?id=38622
public void test_getSymbol_38622() throws Exception {
// The CLDR data had the Portuguese symbol for "EUR" in pt, not in pt_PT.
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
index 81ff673..a9ff56f 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
@@ -217,96 +217,6 @@ public final class ZipFileTest extends TestCase {
}
}
- public void testNullCharset() throws IOException {
- try {
- new ZipFile(createTemporaryZipFile(), null);
- fail();
- } catch (NullPointerException expected) {
- }
- }
-
- // Tests that non-UTF8 encoded zip files can be interpreted. Relies on ZipOutputStream.
- public void testNonUtf8Encoding() throws IOException {
- Charset charset = Charset.forName("Cp437");
- String encodingDependentString = "\u00FB";
- assertEncodingDiffers(encodingDependentString, charset, StandardCharsets.US_ASCII,
- StandardCharsets.UTF_8);
- String name = "name" + encodingDependentString;
- String comment = "comment" + encodingDependentString;
-
- File result = createTemporaryZipFile();
- OutputStream os = new BufferedOutputStream(new FileOutputStream(result));
- ZipOutputStream out = new ZipOutputStream(os, charset);
- out.setComment(comment);
- ZipEntry writeEntry = new ZipEntry(name);
- writeEntry.setComment(comment);
- out.putNextEntry(writeEntry);
- out.write("FileContentsIrrelevant".getBytes());
- out.closeEntry();
- out.close();
-
- ZipFile zipFile = new ZipFile(result, StandardCharsets.US_ASCII);
- assertNull(zipFile.getEntry(name));
- assertFalse(zipFile.getComment().equals(comment));
- zipFile.close();
-
- zipFile = new ZipFile(result, charset);
- ZipEntry readEntry = zipFile.getEntry(name);
- assertNotNull(readEntry);
- assertEquals(name, readEntry.getName());
- assertEquals(comment, readEntry.getComment());
- assertEquals(comment, zipFile.getComment());
- zipFile.close();
- }
-
- // Tests that UTF8 encoded zip files can be interpreted when the constructor is provided with a
- // non-UTF-8 encoding. Relies on ZipOutputStream.
- public void testUtf8EncodingOverridesConstructor() throws IOException {
- Charset charset = Charset.forName("Cp437");
- String encodingDependentString = "\u00FB";
- assertEncodingDiffers(encodingDependentString, charset, StandardCharsets.UTF_8);
- String name = "name" + encodingDependentString;
- String comment = "comment" + encodingDependentString;
-
- File result = createTemporaryZipFile();
- OutputStream os = new BufferedOutputStream(new FileOutputStream(result));
- ZipOutputStream out = new ZipOutputStream(os, StandardCharsets.UTF_8);
- // The file comment does not get meta-data about the character encoding.
- out.setComment(comment);
- // The entry will be tagged as being UTF-8 encoded.
- ZipEntry writeEntry = new ZipEntry(name);
- writeEntry.setComment(comment);
- out.putNextEntry(writeEntry);
- out.write("FileContentsIrrelevant".getBytes());
- out.closeEntry();
- out.close();
-
- ZipFile zipFile = new ZipFile(result, charset);
- // The entry should be found, because it should be tagged as being UTF-8 encoded.
- ZipEntry readEntry = zipFile.getEntry(name);
- assertNotNull(readEntry);
- assertEquals(name, readEntry.getName());
- assertEquals(comment, readEntry.getComment());
- // We expect the comment to be mangled because it is not tagged.
- assertFalse(zipFile.getComment().equals(comment));
- zipFile.close();
- }
-
- /**
- * Asserts the byte encoding for the string is different for all the supplied character
- * sets.
- */
- private void assertEncodingDiffers(String string, Charset... charsets) {
- Set<List<Byte>> encodings = new HashSet<List<Byte>>();
- for (int i = 0; i < charsets.length; i++) {
- List<Byte> byteList = new ArrayList<Byte>();
- for (byte b : string.getBytes(charsets[i])) {
- byteList.add(b);
- }
- assertTrue("Encoding has been seen before", encodings.add(byteList));
- }
- }
-
/**
* Compresses the given number of files, each of the given size, into a .zip archive.
*/
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
index 2daa127..49990a3 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
@@ -50,97 +50,6 @@ public final class ZipInputStreamTest extends TestCase {
assertTrue(Arrays.equals(data, unzip("r", ZipOutputStreamTest.zip("r", data))));
}
- public void testNullCharset() throws IOException {
- try {
- new ZipInputStream(new ByteArrayInputStream(new byte[1]), null);
- fail();
- } catch (NullPointerException expected) {
- }
- }
-
- // Tests that non-UTF8 encoded zip file entries can be interpreted. Relies on ZipOutputStream.
- public void testNonUtf8Encoding() throws IOException {
- Charset charset = Charset.forName("Cp437");
- String encodingDependentString = "\u00FB";
- assertEncodingDiffers(encodingDependentString, charset, StandardCharsets.US_ASCII,
- StandardCharsets.UTF_8);
- String name = "name" + encodingDependentString;
- String comment = "comment" + encodingDependentString;
-
- ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
- ZipOutputStream out = new ZipOutputStream(bytesOutputStream, charset);
- ZipEntry writeEntry = new ZipEntry(name);
- writeEntry.setComment(comment);
- out.putNextEntry(writeEntry);
- out.write("FileContentsIrrelevant".getBytes());
- out.closeEntry();
- out.close();
-
- ByteArrayInputStream bytesInputStream =
- new ByteArrayInputStream(bytesOutputStream.toByteArray());
- ZipInputStream in = new ZipInputStream(bytesInputStream, StandardCharsets.US_ASCII);
- ZipEntry readEntry = in.getNextEntry();
- // Due to the way ZipInputStream works it never returns entry comments.
- assertNull("ZipInputStream must not retrieve comments", readEntry.getComment());
- assertFalse(readEntry.getName().equals(name));
- in.close();
-
- bytesInputStream = new ByteArrayInputStream(bytesOutputStream.toByteArray());
- in = new ZipInputStream(bytesInputStream, charset);
- readEntry = in.getNextEntry();
- // Because ZipInputStream never reads the central directory it never returns entry
- // comments or the file comment.
- assertNull("ZipInputStream must not retrieve comments", readEntry.getComment());
- assertEquals(name, readEntry.getName());
- in.close();
- }
-
- // Tests that UTF8 encoded zip file entries can be interpreted when the constructor is provided
- // with a non-UTF-8 encoding. Relies on ZipOutputStream.
- public void testUtf8EncodingOverridesConstructor() throws IOException {
- Charset charset = Charset.forName("Cp437");
- String encodingDependentString = "\u00FB";
- assertEncodingDiffers(encodingDependentString, charset, StandardCharsets.UTF_8);
- String name = "name" + encodingDependentString;
- String comment = "comment" + encodingDependentString;
-
- ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
- ZipOutputStream out = new ZipOutputStream(bytesOutputStream, StandardCharsets.UTF_8);
- // The entry will be tagged as being UTF-8 encoded.
- ZipEntry writeEntry = new ZipEntry(name);
- writeEntry.setComment(comment);
- out.putNextEntry(writeEntry);
- out.write("FileContentsIrrelevant".getBytes());
- out.closeEntry();
- out.close();
-
- ByteArrayInputStream bytesInputStream =
- new ByteArrayInputStream(bytesOutputStream.toByteArray());
- ZipInputStream in = new ZipInputStream(bytesInputStream, charset);
- ZipEntry readEntry = in.getNextEntry();
- // Because ZipInputStream never reads the central directory it never returns entry
- // comments or the file comment.
- assertNull("ZipInputStream must not retrieve comments", readEntry.getComment());
- assertNotNull(readEntry);
- assertEquals(name, readEntry.getName());
- in.close();
- }
-
- /**
- * Asserts the byte encoding for the string is different for all the supplied character
- * sets.
- */
- private void assertEncodingDiffers(String string, Charset... charsets) {
- Set<List<Byte>> encodings = new HashSet<List<Byte>>();
- for (int i = 0; i < charsets.length; i++) {
- List<Byte> byteList = new ArrayList<Byte>();
- for (byte b : string.getBytes(charsets[i])) {
- byteList.add(b);
- }
- assertTrue("Encoding has been seen before", encodings.add(byteList));
- }
- }
-
public static byte[] unzip(String name, byte[] bytes) throws IOException {
ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(bytes));
ByteArrayOutputStream out = new ByteArrayOutputStream();
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java
index 92afffa..e69f010 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java
@@ -78,14 +78,6 @@ public final class ZipOutputStreamTest extends TestCase {
}
}
- public void testNullCharset() throws IOException {
- try {
- new ZipOutputStream(new ByteArrayOutputStream(), null);
- fail();
- } catch (NullPointerException expected) {
- }
- }
-
/** Regression test for null comment causing a NullPointerException during write. */
public void testNullComment() throws IOException {
ZipOutputStream out = new ZipOutputStream(new ByteArrayOutputStream());
@@ -95,54 +87,4 @@ public final class ZipOutputStreamTest extends TestCase {
out.closeEntry();
out.finish();
}
-
- /** Contrived test to force a longer name than can be stored. */
- public void testLongName() throws IOException {
- int maxNameBytes = 0xffff; // 2 bytes
- String longName = createString(maxNameBytes);
-
- ZipEntry entry = new ZipEntry(longName);
-
- // Using UTF-16 will result in name bytes twice as large as is supported by Zip.
- // UTF-16 is an unlikely character set to actually want to use with Zip but enables
- // the edge-case behavior required without using direct field access.
- ZipOutputStream out = new ZipOutputStream(
- new ByteArrayOutputStream(), StandardCharsets.UTF_16);
- try {
- out.putNextEntry(entry);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- }
-
- /** Contrived test to force a longer comment than can be stored. */
- public void testLongComment() throws IOException {
- int maxCommentBytes = 0xffff; // 2 bytes
- String longComment = createString(maxCommentBytes);
-
- ZipEntry entry = new ZipEntry("name");
- // setComment() should pass, because it is at the limit of what ZipEntry will detect as
- // valid (since it uses UTF-8 as a worst-case guess).
- entry.setComment(longComment);
-
- // Using UTF-16 will result in comment bytes twice as large as is supported by Zip.
- // UTF-16 is an unlikely character set to actually want to use with Zip but enables
- // the edge-case behavior required without using direct field access.
- ZipOutputStream out = new ZipOutputStream(
- new ByteArrayOutputStream(), StandardCharsets.UTF_16);
- try {
- out.putNextEntry(entry);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- }
-
- private static String createString(int numChars) {
- char c = 'a';
- StringBuilder sb = new StringBuilder(numChars);
- for (int i = 0; i < numChars; i++) {
- sb.append(c);
- }
- return sb.toString();
- }
}