You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [27/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,887 @@
+/* Copyright 2003, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+public class Inet6AddressTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.Inet6Address#isMulticastAddress()
+ */
+ public void test_isMulticastAddress() {
+
+ String addrName = "";
+ InetAddress addr = null;
+
+ try {
+
+ // IP V6 regular multicast and non-multicast tests
+ //
+ // Create 2 IP v6 addresses and call "isMulticastAddress()"
+ // A prefix of "11111111" means that the address is multicast
+ // The first one will be one with the prefix the second without
+
+ addrName = "FFFF::42:42"; // 11111111 = FFFF
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Multicast address " + addrName + " not detected.", addr
+ .isMulticastAddress());
+
+ addrName = "42::42:42"; // an non-multicast address
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Non multicast address " + addrName
+ + " reporting as a multicast address.", !addr
+ .isMulticastAddress());
+
+ // IPv4-compatible IPv6 address tests
+ //
+ // Now create 2 IP v6 addresses that are IP v4 compatable
+ // to IP v6 addresses. The address prefix for a multicast ip v4
+ // address is 1110 for the last 16 bits ::d.d.d.d
+ // We expect these to be false
+
+ addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 compatable address " + addrName
+ + " reported incorrectly as multicast.", !addr
+ .isMulticastAddress());
+
+ addrName = "::42.42.42.42"; // an ipv4 non-multicast address
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 compatable address " + addrName
+ + " reported incorrectly as multicast.", !addr
+ .isMulticastAddress());
+
+ // IPv4-mapped IPv6 address tests
+ //
+ // Now create 2 IP v6 addresses that are IP v4 compatable
+ // to IP v6 addresses. The address prefix for a multicast ip v4
+ // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
+
+ addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
+ // 224
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-mapped IPv6 multicast address " + addrName
+ + " not detected.", addr.isMulticastAddress());
+
+ addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
+ + " reporting as a multicast address.", !addr
+ .isMulticastAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isAnyLocalAddress()
+ */
+ public void test_isAnyLocalAddress() {
+
+ String addrName = "";
+ InetAddress addr = null;
+
+ try {
+
+ // test to ensure that the unspecified address returns tru
+ addrName = "::0"; // The unspecified address
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "The unspecified (also known as wildcard and any local address) "
+ + addrName + " not detected.", addr
+ .isAnyLocalAddress());
+
+ addrName = "::"; // another form of the unspecified address
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "The unspecified (also known as wildcard and any local address) "
+ + addrName + " not detected.", addr
+ .isAnyLocalAddress());
+
+ addrName = "::1"; // The loopback address
+ addr = InetAddress.getByName(addrName);
+ assertTrue("The addresses " + addrName
+ + " incorrectly reporting an the unspecified address.",
+ !addr.isAnyLocalAddress());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isLoopbackAddress()
+ */
+ public void test_isLoopbackAddress() {
+
+ String addrName = "";
+ try {
+
+ // IP V6 regular address tests for loopback
+ // The loopback address for IPv6 is ::1
+
+ addrName = "::1";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 loopback address " + addrName + " not detected.",
+ addr.isLoopbackAddress());
+
+ addrName = "::2";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address incorrectly " + addrName
+ + " detected as a loopback address.", !addr
+ .isLoopbackAddress());
+
+ // a loopback address should be 127.d.d.d
+ addrName = "42:42::42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address incorrectly " + addrName
+ + " detected as a loopback address.", !addr
+ .isLoopbackAddress());
+
+ // IPv4-compatible IPv6 address tests
+ //
+ // Now create 2 IP v6 addresses that are IP v4 compatable
+ // to IP v6 addresses. The address prefix for a multicast ip v4
+ // address is 1110 for the last 16 bits ::d.d.d.d
+ // We expect these to be false, as they are not IPv4 addresses
+
+ // a loopback address should be 127.d.d.d
+ addrName = "::127.0.0.0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 address " + addrName
+ + " detected incorrectly as a loopback.", !addr
+ .isLoopbackAddress());
+
+ addrName = "::127.42.42.42"; // a loopback address should be
+ // 127.d.d.d
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 address " + addrName
+ + " detected incorrectly as a loopback.", !addr
+ .isLoopbackAddress());
+
+ // a loopback address should be 127.d.d.d
+ addrName = "::42.42.42.42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 address " + addrName
+ + " detected incorrectly as a loopback.", !addr
+ .isLoopbackAddress());
+
+ // IPv4-mapped IPv6 address tests
+ //
+ // Now create 2 IP v6 addresses that are IP v4 compatable
+ // to IP v6 addresses. The address prefix for a multicast ip v4
+ // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
+
+ // a loopback address should be 127.d.d.d
+ addrName = "::FFFF:127.0.0.0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 loopback address " + addrName
+ + " not detected.", addr.isLoopbackAddress());
+
+ // a loopback address should be 127.d.d.d
+ addrName = "::FFFF:127.42.42.42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 loopback address " + addrName
+ + " not detected.", addr.isLoopbackAddress());
+
+ // a loopback address should be 127.d.d.d
+ addrName = "::FFFF:42.42.42.42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName
+ + " detected as a loopback address.", !addr
+ .isLoopbackAddress());
+
+ } catch (UnknownHostException e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isLinkLocalAddress()
+ */
+ public void test_isLinkLocalAddress() {
+
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for link local addresses
+ //
+ // Link local addresses are FE80:: -
+ // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
+
+ addrName = "FE80::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 link local address " + addrName + " not detected.",
+ addr.isLinkLocalAddress());
+
+ addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 link local address " + addrName + " not detected.",
+ addr.isLinkLocalAddress());
+
+ addrName = "FEC0::1";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address " + addrName
+ + " detected incorrectly as a link local address.", !addr
+ .isLinkLocalAddress());
+
+ addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address " + addrName
+ + " detected incorrectly as a link local address.", !addr
+ .isLinkLocalAddress());
+
+ addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address " + addrName
+ + " detected incorrectly as a link local address.", !addr
+ .isLinkLocalAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isSiteLocalAddress()
+ */
+ public void test_isSiteLocalAddress() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for link local addresses
+ //
+ // Link local addresses are FEC0::0 through to
+ // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
+
+ addrName = "FEC0::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 site local address " + addrName + " not detected.",
+ addr.isSiteLocalAddress());
+
+ addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 site local address " + addrName + " not detected.",
+ addr.isSiteLocalAddress());
+
+ addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address " + addrName
+ + " detected incorrectly as a site local address.", !addr
+ .isSiteLocalAddress());
+
+ addrName = "FFC0::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 address " + addrName
+ + " detected incorrectly as a site local address.", !addr
+ .isSiteLocalAddress());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isMCGlobal()
+ */
+ public void test_isMCGlobal() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for Mulitcase Global addresses
+ //
+ // Multicast global addresses are FFxE:/112 where x is
+ // a set of flags, and the addition 112 bits make up
+ // the global address space
+
+ addrName = "FF0E::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 global mutlicast address " + addrName
+ + " not detected.", addr.isMCGlobal());
+
+ addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 global multicast address " + addrName
+ + " not detected.", addr.isMCGlobal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFFE::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 global mutlicast address " + addrName
+ + " not detected.", addr.isMCGlobal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 global multicast address " + addrName
+ + " not detected.", addr.isMCGlobal());
+
+ // a sample MC organizational address
+ addrName = "FF08:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast organizational " + addrName
+ + " incorrectly indicated as a global address.", !addr
+ .isMCGlobal());
+
+ // a sample MC site address
+ addrName = "FF05:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast site address " + addrName
+ + " incorrectly indicated as a global address.", !addr
+ .isMCGlobal());
+
+ // a sample MC link address
+ addrName = "FF02:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast link address " + addrName
+ + " incorrectly indicated as a global address.", !addr
+ .isMCGlobal());
+
+ // a sample MC Node
+ addrName = "FF01:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast node address " + addrName
+ + " incorrectly indicated as a global address.", !addr
+ .isMCGlobal());
+
+ // IPv4-mapped IPv6 address tests
+ addrName = "::FFFF:224.0.1.0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 global multicast address " + addrName
+ + " not identified as a global multicast address.", addr
+ .isMCGlobal());
+
+ addrName = "::FFFF:238.255.255.255";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 global multicast address " + addrName
+ + " not identified as a global multicast address.", addr
+ .isMCGlobal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isMCNodeLocal()
+ */
+ public void test_isMCNodeLocal() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for Mulitcase node local addresses
+ //
+ // Multicast node local addresses are FFx1:/112 where x is
+ // a set of flags, and the addition 112 bits make up
+ // the global address space
+
+ addrName = "FF01::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 node-local mutlicast address " + addrName
+ + " not detected.", addr.isMCNodeLocal());
+
+ addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 node-local multicast address " + addrName
+ + " not detected.", addr.isMCNodeLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF1::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 node-local mutlicast address " + addrName
+ + " not detected.", addr.isMCNodeLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 node-local multicast address " + addrName
+ + " not detected.", addr.isMCNodeLocal());
+
+ // a sample MC organizational address
+ addrName = "FF08:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast organizational address " + addrName
+ + " incorrectly indicated as a node-local address.", !addr
+ .isMCNodeLocal());
+
+ // a sample MC site address
+ addrName = "FF05:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast site address " + addrName
+ + " incorrectly indicated as a node-local address.", !addr
+ .isMCNodeLocal());
+
+ // a sample MC link address
+ addrName = "FF02:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast link address " + addrName
+ + " incorrectly indicated as a node-local address.", !addr
+ .isMCNodeLocal());
+
+ // a sample MC global address
+ addrName = "FF0E:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 mulitcast node address " + addrName
+ + " incorrectly indicated as a node-local address.", !addr
+ .isMCNodeLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isMCLinkLocal()
+ */
+ public void test_isMCLinkLocal() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for Mulitcase link local addresses
+ //
+ // Multicast link local addresses are FFx2:/112 where x is
+ // a set of flags, and the addition 112 bits make up
+ // the global address space
+
+ addrName = "FF02::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 link local multicast address " + addrName
+ + " not detected.", addr.isMCLinkLocal());
+
+ addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 link local multicast address " + addrName
+ + " not detected.", addr.isMCLinkLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF2::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 link local multicast address " + addrName
+ + " not detected.", addr.isMCLinkLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 link local multicast address " + addrName
+ + " not detected.", addr.isMCLinkLocal());
+
+ // a sample MC organizational address
+ addrName = "FF08:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 organization multicast address "
+ + addrName
+ + " incorrectly indicated as a link-local mulitcast address.",
+ !addr.isMCLinkLocal());
+
+ // a sample MC site address
+ addrName = "FF05:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 site-local mulitcast address "
+ + addrName
+ + " incorrectly indicated as a link-local mulitcast address.",
+ !addr.isMCLinkLocal());
+
+ // a sample MC global address
+ addrName = "FF0E:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 global multicast address "
+ + addrName
+ + " incorrectly indicated as a link-local mulitcast address.",
+ !addr.isMCLinkLocal());
+
+ // a sample MC Node
+ addrName = "FF01:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 mulitcast node address "
+ + addrName
+ + " incorrectly indicated as a link-local mulitcast address.",
+ !addr.isMCLinkLocal());
+
+ // Ipv4-mapped IPv6 addresses
+
+ addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " not identified as a link-local multicast address.",
+ addr.isMCLinkLocal());
+
+ addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " not identified as a link-local multicast address.",
+ addr.isMCLinkLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isMCSiteLocal()
+ */
+ public void test_isMCSiteLocal() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for Multicast site-local addresses
+ //
+ // Multicast global addresses are FFx5:/112 where x is
+ // a set of flags, and the addition 112 bits make up
+ // the global address space
+
+ addrName = "FF05::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 site-local mutlicast address " + addrName
+ + " not detected.", addr.isMCSiteLocal());
+
+ addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 site-local multicast address " + addrName
+ + " not detected.", addr.isMCSiteLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF5::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 site-local mutlicast address " + addrName
+ + " not detected.", addr.isMCSiteLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 site-local multicast address " + addrName
+ + " not detected.", addr.isMCSiteLocal());
+
+ // a sample MC organizational address
+ addrName = "FF08:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 organization multicast address "
+ + addrName
+ + " incorrectly indicated as a site-local mulitcast address.",
+ !addr.isMCSiteLocal());
+
+ // a sample MC global address
+ addrName = "FF0E:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 global mulitcast address "
+ + addrName
+ + " incorrectly indicated as a site-local mulitcast address.",
+ !addr.isMCSiteLocal());
+
+ // a sample MC link address
+ addrName = "FF02:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 link-local multicast address "
+ + addrName
+ + " incorrectly indicated as a site-local mulitcast address.",
+ !addr.isMCSiteLocal());
+
+ // a sample MC Node
+ addrName = "FF01:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 mulitcast node address "
+ + addrName
+ + " incorrectly indicated as a site-local mulitcast address.",
+ !addr.isMCSiteLocal());
+
+ // IPv4-mapped IPv6 addresses
+ addrName = "::FFFF:239.255.0.0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 site-local multicast address " + addrName
+ + " not identified as a site-local multicast address.",
+ addr.isMCSiteLocal());
+
+ addrName = "::FFFF:239.255.255.255";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 site-local multicast address " + addrName
+ + " not identified as a site-local multicast address.",
+ addr.isMCSiteLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isMCOrgLocal()
+ */
+ public void test_isMCOrgLocal() {
+ String addrName = "";
+ try {
+ // IP V6 regular address tests for Mulitcase organization-local
+ // addresses
+ //
+ // Multicast global addresses are FFxE:/112 where x is
+ // a set of flags, and the addition 112 bits make up
+ // the global address space
+
+ addrName = "FF08::0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 organization-local mutlicast address " + addrName
+ + " not detected.", addr.isMCOrgLocal());
+
+ addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 organization-local multicast address " + addrName
+ + " not detected.", addr.isMCOrgLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF8::0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 organization-local mutlicast address " + addrName
+ + " not detected.", addr.isMCOrgLocal());
+
+ // a currently invalid address as the prefix FFxE
+ // is only valid for x = {1,0} as the rest are reserved
+ addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv6 organization-local multicast address " + addrName
+ + " not detected.", addr.isMCOrgLocal());
+
+ // a sample MC global address
+ addrName = "FF0E:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 global multicast address "
+ + addrName
+ + " incorrectly indicated as an organization-local mulitcast address.",
+ !addr.isMCOrgLocal());
+
+ // a sample MC site address
+ addrName = "FF05:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 site-local mulitcast address "
+ + addrName
+ + " incorrectly indicated as an organization-local mulitcast address.",
+ !addr.isMCOrgLocal());
+
+ // a sample MC link address
+ addrName = "FF02:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 link-local multicast address "
+ + addrName
+ + " incorrectly indicated as an organization-local mulitcast address.",
+ !addr.isMCOrgLocal());
+
+ // a sample MC Node
+ addrName = "FF01:42:42:42:42:42:42:42";
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv6 mulitcast node address "
+ + addrName
+ + " incorrectly indicated as an organization-local mulitcast address.",
+ !addr.isMCOrgLocal());
+
+ // IPv4-mapped IPv6 addresses
+
+ addrName = "::FFFF:239.192.0.0";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 org-local multicast address " + addrName
+ + " not identified as a org-local multicast address.", addr
+ .isMCOrgLocal());
+
+ addrName = "::FFFF:239.195.255.255";
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 org-local multicast address " + addrName
+ + " not identified as a org-local multicast address.", addr
+ .isMCOrgLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#isIPv4CompatibleAddress()
+ */
+ public void test_isIPv4CompatibleAddress() {
+ String addrName = "";
+ Inet6Address addr = null;
+
+ try {
+
+ // Tests a number of addresses to see if they are compatable with
+ // IPv6 addresses
+
+ addrName = "FFFF::42:42"; // 11111111 = FFFF
+ addr = (Inet6Address) InetAddress.getByName(addrName);
+ assertTrue("A non-compatable IPv6 address " + addrName
+ + " incorrectly identified as a IPv4 compatable address.",
+ !addr.isIPv4CompatibleAddress());
+
+ // IPv4-compatible IPv6 address tests
+ //
+ // Now create 2 IP v6 addresses that are IP v4 compatable
+ // to IP v6 addresses.
+
+ addrName = "::0.0.0.0";
+ addr = (Inet6Address) InetAddress.getByName(addrName);
+ assertTrue("IPv4 compatable address " + addrName
+ + " not detected correctly.", addr
+ .isIPv4CompatibleAddress());
+
+ addrName = "::255.255.255.255"; // an ipv4 non-multicast address
+ addr = (Inet6Address) InetAddress.getByName(addrName);
+ assertTrue("IPv4 compatable address " + addrName
+ + " not detected correctly.", addr
+ .isIPv4CompatibleAddress());
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet6Address#getAddress()
+ */
+ public void test_getAddress() {
+ // TODO : Implementation
+ }
+
+ /**
+ * @tests java.net.Inet6Address#getByName(java.lang.String)
+ */
+ public void test_getByNameLjava_lang_String() {
+ // ones to add "::255.255.255.255", "::FFFF:0.0.0.0",
+ // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F",
+ // "[F:F:F:F:F:F:F:F]"
+ String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
+ "::1", "0", /* jdk1.5 accepts 0 as valid */
+ "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
+ "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
+ "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
+
+ String invalidIPAddresses[] = { ":", "FFFF:FFFF", "1:1", "::1.2.3.444" };
+
+ for (int i = 0; i < validIPAddresses.length; i++) {
+
+ try {
+ InetAddress.getByName(validIPAddresses[i]);
+ } catch (Exception e) {
+ fail("Valid IP address not recognized: "
+ + validIPAddresses[i]);
+ }
+ if (!validIPAddresses[i].equals("0")) {
+ String tempIPAddress = "[" + validIPAddresses[i] + "]";
+ try {
+ InetAddress.getByName(tempIPAddress);
+ } catch (Exception e) {
+ fail("Valid IP address not recognized: "
+ + tempIPAddress);
+ }
+ }
+ }
+
+ for (int i = 0; i < invalidIPAddresses.length; i++) {
+ try {
+ InetAddress.getByName(invalidIPAddresses[i]);
+ fail(
+ "Invalid IP address incorrectly recognized as valid: "
+ + invalidIPAddresses[i]);
+ } catch (Exception e) {
+ }
+ ;
+
+ String tempIPAddress = "[" + invalidIPAddresses[i] + "]";
+ try {
+ InetAddress.getByName(tempIPAddress);
+ fail(
+ "Invalid IP address incorrectly recognized as valid: "
+ + tempIPAddress);
+ } catch (Exception e) {
+ }
+ ;
+
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+ int bytesToInt(byte bytes[], int start) {
+
+ int byteMask = 255;
+ int value = ((bytes[start + 3] & byteMask))
+ | ((bytes[start + 2] & byteMask) << 8)
+ | ((bytes[start + 1] & byteMask) << 16)
+ | ((bytes[start] & byteMask) << 24);
+ return value;
+
+ }
+
+ String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
+
+ String fullString = "";
+ int times = bytes.length / 4;
+ int intArray[] = new int[times];
+ for (int i = 0; i < times; i++) {
+ intArray[i] = bytesToInt(bytes, i * 4);
+ }
+
+ return intArrayToHexString(intArray, leadingZeros);
+ }
+
+ void intToBytes(int value, byte bytes[], int start) {
+
+ int byteMask = 255;
+ bytes[start + 3] = (byte) (value & byteMask);
+ bytes[start + 2] = (byte) ((value >> 8) & byteMask);
+ bytes[start + 1] = (byte) ((value >> 16) & byteMask);
+ bytes[start] = (byte) ((value >> 24) & byteMask);
+ }
+
+ String intArrayToHexString(int ints[], boolean leadingZeros) {
+
+ String fullString = "";
+ String tempString;
+ int intsLength = ints.length;
+ for (int i = 0; i < intsLength; i++) {
+ tempString = Integer.toHexString(ints[i]);
+ while (tempString.length() < 4 && leadingZeros) {
+ tempString = "0" + tempString;
+ }
+ if (i + 1 < intsLength) {
+ tempString += ":";
+ }
+ fullString += tempString;
+ }
+
+ return fullString.toUpperCase();
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetAddressTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetAddressTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetAddressTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetAddressTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,457 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import tests.support.Support_Configuration;
+
+public class InetAddressTest extends junit.framework.TestCase {
+
+ private static boolean someoneDone[] = new boolean[2];
+
+ protected static boolean threadedTestSucceeded;
+
+ protected static String threadedTestErrorString;
+
+ /**
+ * This class is used to test inet_ntoa, gethostbyaddr and gethostbyname
+ * functions in the VM to make sure they're threadsafe. getByName will cause
+ * the gethostbyname function to be called. getHostName will cause the
+ * gethostbyaddr to be called. getHostAddress will cause inet_ntoa to be
+ * called.
+ */
+ static class threadsafeTestThread extends Thread {
+ private String lookupName;
+
+ private InetAddress testAddress;
+
+ private int testType;
+
+ /*
+ * REP_NUM can be adjusted if desired. Since this error is
+ * non-deterministic it may not always occur. Setting REP_NUM higher,
+ * increases the chances of an error being detected, but causes the test
+ * to take longer. Because the Java threads spend a lot of time
+ * performing operations other than running the native code that may not
+ * be threadsafe, it is quite likely that several thousand iterations
+ * will elapse before the first error is detected.
+ */
+ private static final int REP_NUM = 20000;
+
+ public threadsafeTestThread(String name, String lookupName,
+ InetAddress testAddress, int type) {
+ super(name);
+ this.lookupName = lookupName;
+ this.testAddress = testAddress;
+ testType = type;
+ }
+
+ public void run() {
+ try {
+ String correctName = testAddress.getHostName();
+ String correctAddress = testAddress.getHostAddress();
+ long startTime = System.currentTimeMillis();
+
+ synchronized (someoneDone) {
+ }
+
+ for (int i = 0; i < REP_NUM; i++) {
+ if (someoneDone[testType]) {
+ break;
+ } else if ((i % 25) == 0
+ && System.currentTimeMillis() - startTime > 240000) {
+ System.out
+ .println("Exiting due to time limitation after "
+ + i + " iterations");
+ break;
+ }
+
+ InetAddress ia = InetAddress.getByName(lookupName);
+ String hostName = ia.getHostName();
+ String hostAddress = ia.getHostAddress();
+
+ if (!correctName.equals(hostName)) {
+ threadedTestSucceeded = false;
+ threadedTestErrorString = (testType == 0 ? "gethostbyname"
+ : "gethostbyaddr")
+ + ": getHostName() returned "
+ + hostName
+ + " instead of " + correctName;
+ break;
+ }
+ if (!correctAddress.equals(hostAddress)) {
+ threadedTestSucceeded = false;
+ threadedTestErrorString = (testType == 0 ? "gethostbyname"
+ : "gethostbyaddr")
+ + ": getHostName() returned "
+ + hostAddress
+ + " instead of " + correctAddress;
+ break;
+ }
+
+ }
+ someoneDone[testType] = true;
+ } catch (Exception e) {
+ threadedTestSucceeded = false;
+ threadedTestErrorString = e.toString();
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean java.net.InetAddress.equals(java.lang.Object)
+ try {
+ InetAddress ia1 = InetAddress
+ .getByName(Support_Configuration.InetTestAddress);
+ InetAddress ia2 = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ assertTrue("Equals returned incorrect result - " + ia1 + " != "
+ + ia2, ia1.equals(ia2));
+ } catch (Exception e) {
+ fail("Exception during equals test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getAddress()
+ */
+ public void test_getAddress() {
+ // Test for method byte [] java.net.InetAddress.getAddress()
+ try {
+ InetAddress ia = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ byte[] caddr = Support_Configuration.InetTestCaddr;
+ byte[] addr = ia.getAddress();
+ for (int i = 0; i < addr.length; i++)
+ assertTrue("Incorrect address returned", caddr[i] == addr[i]);
+ } catch (java.net.UnknownHostException e) {
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getAllByName(java.lang.String)
+ */
+ public void test_getAllByNameLjava_lang_String() {
+ // Test for method java.net.InetAddress []
+ // java.net.InetAddress.getAllByName(java.lang.String)
+
+ try {
+ InetAddress[] ia = InetAddress
+ .getAllByName(Support_Configuration.SpecialInetTestAddress);
+ assertTrue(
+ "Incorrect number of aliases returned: "
+ + ia.length
+ + " should be "
+ + Support_Configuration.SpecialInetTestAddressNumber,
+ ia.length == Support_Configuration.SpecialInetTestAddressNumber);
+ } catch (Exception e) {
+ fail("Exception during getAllByName : " + e.getMessage());
+ }
+
+ // check the getByName if there is a security manager.
+ System.setSecurityManager(new SecurityManager());
+ try {
+ boolean exception = false;
+ try {
+ InetAddress.getByName("3d.com");
+ } catch (SecurityException ex) {
+ exception = true;
+ } catch (Exception ex) {
+ fail("getByName threw wrong exception : " + ex.getMessage());
+ }
+ assertTrue("expected SecurityException", exception);
+ } finally {
+ System.setSecurityManager(null);
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getByName(java.lang.String)
+ */
+ public void test_getByNameLjava_lang_String() {
+ // Test for method java.net.InetAddress
+ // java.net.InetAddress.getByName(java.lang.String)
+ try {
+ InetAddress ia2 = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ assertTrue("Equals returned incorrect result: " + ia2.getHostName()
+ + " != " + Support_Configuration.InetTestAddress, ia2
+ .getHostName()
+ .equals(Support_Configuration.InetTestAddress));
+ } catch (Exception e) {
+ fail("Exception during equals test : " + e.getMessage());
+ }
+
+ // TODO : Test to ensure all the address formats are recognized
+ }
+
+ /**
+ * @tests java.net.InetAddress#getHostAddress()
+ */
+ public void test_getHostAddress() {
+ // Test for method java.lang.String
+ // java.net.InetAddress.getHostAddress()
+ try {
+ InetAddress ia2 = InetAddress
+ .getByName(Support_Configuration.InetTestAddress);
+ assertTrue("getHostAddress returned incorrect result: "
+ + ia2.getHostAddress() + " != "
+ + Support_Configuration.InetTestIP, ia2.getHostAddress()
+ .equals(Support_Configuration.InetTestIP));
+ } catch (Exception e) {
+ fail("Exception during getHostAddress test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getHostName()
+ */
+ public void test_getHostName() {
+ // Test for method java.lang.String java.net.InetAddress.getHostName()
+ try {
+ InetAddress ia = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ assertTrue("Incorrect host name returned: " + ia.getHostName()
+ + " != " + Support_Configuration.InetTestAddress, ia
+ .getHostName()
+ .equals(Support_Configuration.InetTestAddress));
+ } catch (Exception e) {
+ fail("Exception during getHostName : " + e.getMessage());
+ }
+
+ // Test for any of the host lookups, where the default SecurityManager
+ // is installed.
+
+ try {
+ String exp = Support_Configuration.InetTestIP;
+ System.setSecurityManager(new SecurityManager());
+ InetAddress ia = InetAddress.getByName(exp);
+ String ans = ia.getHostName();
+ assertTrue("usingSecurityManager failed, ans: " + ans + " exp: "
+ + Support_Configuration.InetTestAddress, ans
+ .equals(Support_Configuration.InetTestAddress));
+ } catch (Exception e) {
+ fail("Exception during usingSecurityManager test : "
+ + e.getMessage());
+ } finally {
+ System.setSecurityManager(null);
+ }
+
+ // Make sure there is no caching
+ String originalPropertyValue = System
+ .getProperty("networkaddress.cache.ttl");
+ System.setProperty("networkaddress.cache.ttl", "0");
+
+ // Test for threadsafety
+ try {
+ System.out
+ .println("\nTesting the threadsafety of getHostName. This test could produce unpredictable results if getHostName is not threadsafe.");
+ InetAddress lookup1 = InetAddress
+ .getByName(Support_Configuration.InetTestAddress);
+ assertTrue(lookup1 + " expected "
+ + Support_Configuration.InetTestIP,
+ Support_Configuration.InetTestIP.equals(lookup1
+ .getHostAddress()));
+ InetAddress lookup2 = InetAddress
+ .getByName(Support_Configuration.InetTestAddress2);
+ assertTrue(lookup2 + " expected "
+ + Support_Configuration.InetTestIP2,
+ Support_Configuration.InetTestIP2.equals(lookup2
+ .getHostAddress()));
+ threadsafeTestThread thread1 = new threadsafeTestThread("1",
+ lookup1.getHostName(), lookup1, 0);
+ threadsafeTestThread thread2 = new threadsafeTestThread("2",
+ lookup2.getHostName(), lookup2, 0);
+ threadsafeTestThread thread3 = new threadsafeTestThread("3",
+ lookup1.getHostAddress(), lookup1, 1);
+ threadsafeTestThread thread4 = new threadsafeTestThread("4",
+ lookup2.getHostAddress(), lookup2, 1);
+
+ // initialize the flags
+ threadedTestSucceeded = true;
+ synchronized (someoneDone) {
+ thread1.start();
+ thread2.start();
+ thread3.start();
+ thread4.start();
+ }
+ System.out.println("Started threads, joining...");
+ thread1.join();
+ thread2.join();
+ thread3.join();
+ thread4.join();
+ assertTrue(threadedTestErrorString, threadedTestSucceeded);
+ } catch (Exception e) {
+ fail("Exception during threadsafe test : " + e.getMessage());
+ } finally {
+ // restore the old value of the property
+ if (originalPropertyValue == null)
+ // setting the property to -1 has the same effect as having the
+ // property be null
+ System.setProperty("networkaddress.cache.ttl", "-1");
+ else
+ System.setProperty("networkaddress.cache.ttl",
+ originalPropertyValue);
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getLocalHost()
+ */
+ public void test_getLocalHost() {
+ // Test for method java.net.InetAddress
+ // java.net.InetAddress.getLocalHost()
+ try {
+ // We don't know the host name or ip of the machine
+ // running the test, so we can't build our own address
+ DatagramSocket dg = new DatagramSocket(0, InetAddress
+ .getLocalHost());
+ assertTrue("Incorrect host returned", InetAddress.getLocalHost()
+ .equals(dg.getLocalAddress()));
+ dg.close();
+ } catch (Exception e) {
+ fail("Exception during getLocalHost test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#hashCode()
+ */
+ public void test_hashCode() {
+ // Test for method int java.net.InetAddress.hashCode()
+ try {
+ InetAddress host = InetAddress
+ .getByName(Support_Configuration.InetTestAddress);
+ int hashcode = host.hashCode();
+ assertTrue("Incorrect hash returned: " + hashcode + " from host: "
+ + host, hashcode == Support_Configuration.InetTestHashcode);
+ } catch (java.net.UnknownHostException e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#isMulticastAddress()
+ */
+ public void test_isMulticastAddress() {
+ // Test for method boolean java.net.InetAddress.isMulticastAddress()
+ try {
+ InetAddress ia2 = InetAddress.getByName("239.255.255.255");
+ assertTrue("isMulticastAddress returned incorrect result", ia2
+ .isMulticastAddress());
+ } catch (Exception e) {
+ fail("Exception during isMulticastAddress test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.net.InetAddress.toString()
+ try {
+ InetAddress ia2 = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ assertTrue("toString returned incorrect result", ia2.toString()
+ .equals(
+ Support_Configuration.InetTestAddress + "/"
+ + Support_Configuration.InetTestIP));
+ } catch (Exception e) {
+ fail("Exception duruing equals test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getByAddress(java.lang.String, byte[])
+ */
+ public void test_getByAddressLjava_lang_String$B() {
+ // Check an IPv4 address with an IPv6 hostname
+ byte ipAddress[] = { 127, 0, 0, 1 };
+ String addressStr = "::1";
+ try {
+ InetAddress addr = InetAddress.getByAddress(addressStr, ipAddress);
+ addr = InetAddress.getByAddress(ipAddress);
+ } catch (UnknownHostException e) {
+ fail("Unexcepted problem creating IP Address "
+ + ipAddress.length);
+ }
+
+ byte ipAddress2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 127, 0, 0,
+ 1 };
+ addressStr = "::1";
+ try {
+ InetAddress addr = InetAddress.getByAddress(addressStr, ipAddress2);
+ addr = InetAddress.getByAddress(ipAddress);
+ } catch (UnknownHostException e) {
+ fail("Unexcepted problem creating IP Address "
+ + ipAddress.length);
+ }
+ }
+
+ /**
+ * @tests java.net.InetAddress#getCanonicalHostName()
+ */
+ public void test_getCanonicalHostName() {
+
+ try {
+ InetAddress theAddress = null;
+ theAddress = InetAddress.getLocalHost();
+ assertTrue("getCanonicalHostName returned a zero length string ",
+ theAddress.getCanonicalHostName().length() != 0);
+ assertTrue("getCanonicalHostName returned an empty string ",
+ !theAddress.equals(""));
+ } catch (Exception e) {
+ fail("Unexcepted exception testing getCanonicalHostName:"
+ + e.toString());
+ }
+ ;
+
+ // test against an expected value
+ try {
+ InetAddress ia = InetAddress
+ .getByName(Support_Configuration.InetTestIP);
+ assertTrue("Incorrect host name returned by getCanonicalHostHame: "
+ + ia.getCanonicalHostName() + " != "
+ + Support_Configuration.InetTestAddress, ia.getHostName()
+ .equals(Support_Configuration.InetTestAddress));
+ } catch (Exception e) {
+ fail(
+ "Exception during getCanonicalHostName - InetAddress.getByName: "
+ + e);
+ }
+
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,187 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.zip.ZipFile;
+
+import tests.support.resource.Support_Resources;
+
+public class JarURLConnectionTest extends junit.framework.TestCase {
+
+ JarURLConnection juc;
+
+ URLConnection uc;
+
+ /**
+ * @tests java.net.JarURLConnection#getAttributes()
+ */
+ public void test_getAttributes() {
+ try {
+ URL u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/swt.dll"));
+ juc = (JarURLConnection) u.openConnection();
+ java.util.jar.Attributes a = juc.getJarEntry().getAttributes();
+ assertTrue("Returned incorrect Attributes", a.get(
+ new java.util.jar.Attributes.Name("Digest-Algorithms"))
+ .equals("SHA MD5"));
+ } catch (java.io.IOException e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.JarURLConnection#getEntryName()
+ */
+ public void test_getEntryName() {
+ try {
+ URL u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/plus.bmp"));
+ juc = (JarURLConnection) u.openConnection();
+ assertTrue("Returned incorrect entryName", juc.getEntryName()
+ .equals("plus.bmp"));
+ u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/"));
+ juc = (JarURLConnection) u.openConnection();
+ assertTrue("Returned incorrect entryName",
+ juc.getEntryName() == null);
+ } catch (java.io.IOException e) {
+ fail("IOException during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.JarURLConnection#getJarEntry()
+ */
+ public void test_getJarEntry() {
+ try {
+ URL u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/plus.bmp"));
+ juc = (JarURLConnection) u.openConnection();
+ assertTrue("Returned incorrect JarEntry", juc.getJarEntry()
+ .getName().equals("plus.bmp"));
+ u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/"));
+ juc = (JarURLConnection) u.openConnection();
+ assertTrue("Returned incorrect JarEntry", juc.getJarEntry() == null);
+ } catch (java.io.IOException e) {
+ fail("IOException during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.JarURLConnection#getJarFile()
+ */
+ public void test_getJarFile() {
+ URL url = null;
+ try {
+ url = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/missing"));
+ } catch (MalformedURLException e) {
+ fail("Unexpected MalformedURLException : " + e.getMessage());
+ } catch (java.io.IOException e) {
+ fail("Unexpected IOException : " + e.getMessage());
+ }
+ JarURLConnection connection = null;
+ try {
+ connection = (JarURLConnection) url.openConnection();
+ } catch (IOException e) {
+ fail("Unexpected IOException : " + e.getMessage());
+ }
+ int exception = 0;
+ try {
+ connection.connect();
+ } catch (IOException e) {
+ exception = 1;
+ }
+ assertTrue("Did not throw exception on connect", exception == 1);
+ exception = 0;
+ try {
+ connection.getJarFile();
+ } catch (IOException e) {
+ exception = 1;
+ }
+ assertTrue("Did not throw exception after connect", exception == 1);
+ File resources = Support_Resources.createTempFolder();
+ try {
+ Support_Resources.copyFile(resources, null, "hyts_att.jar");
+ File file = new File(resources.toString() + "/hyts_att.jar");
+ URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
+ JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
+ ZipFile jf1 = con1.getJarFile();
+ JarURLConnection con2 = (JarURLConnection) fUrl1.openConnection();
+ ZipFile jf2 = con2.getJarFile();
+ assertTrue("file: JarFiles not the same", jf1 == jf2);
+ jf1.close();
+ assertTrue("File should exist", file.exists());
+ new URL("jar:" + Support_Resources.getResourceURL("/JUC/lf.jar!/"));
+ con1 = (JarURLConnection) fUrl1.openConnection();
+ jf1 = con1.getJarFile();
+ con2 = (JarURLConnection) fUrl1.openConnection();
+ jf2 = con2.getJarFile();
+ assertTrue("http: JarFiles not the same", jf1 == jf2);
+ jf1.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("Unexpected exception : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.JarURLConnection#getJarFileURL()
+ */
+ public void test_getJarFileURL() {
+ try {
+ URL fileURL = new URL(Support_Resources
+ .getResourceURL("/JUC/lf.jar"));
+ URL u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/plus.bmp"));
+ juc = (JarURLConnection) u.openConnection();
+ assertTrue("Returned incorrect file URL", juc.getJarFileURL()
+ .equals(fileURL));
+ } catch (java.io.IOException e) {
+ fail("IOException during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.JarURLConnection#getMainAttributes()
+ */
+ public void test_getMainAttributes() {
+ try {
+ URL u = new URL("jar:"
+ + Support_Resources.getResourceURL("/JUC/lf.jar!/swt.dll"));
+ juc = (JarURLConnection) u.openConnection();
+ java.util.jar.Attributes a = juc.getMainAttributes();
+ assertTrue("Returned incorrect Attributes", a.get(
+ java.util.jar.Attributes.Name.MANIFEST_VERSION).equals(
+ "1.0"));
+ } catch (java.io.IOException e) {
+ fail("IOException during test : " + e.getMessage());
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MalformedURLExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MalformedURLExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MalformedURLExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MalformedURLExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,71 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class MalformedURLExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.MalformedURLException#MalformedURLException()
+ */
+ public void test_Constructor() {
+ // Test for method java.net.MalformedURLException()
+ boolean passed;
+ passed = false;
+ try {
+ new URL("notAProtocol://www.ibm.com");
+ } catch (MalformedURLException e) {
+ // correct
+ passed = true;
+ } catch (Exception e) {
+ fail("Wrong exception thrown : " + e.getMessage());
+ }
+ assertTrue("Failed to throw correct exception", passed);
+ }
+
+ /**
+ * @tests java.net.MalformedURLException#MalformedURLException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.net.MalformedURLException(java.lang.String)
+ final String myString = "Gawsh!";
+ try {
+ if (true)
+ throw new MalformedURLException(myString);
+ } catch (MalformedURLException e) {
+ assertTrue("Incorrect exception text", e.toString().indexOf(
+ myString) >= 0);
+ return;
+ }
+ fail("Exception not thrown");
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}