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/04/25 12:08:59 UTC

svn commit: r396823 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/Inet6Address.java test/java/tests/api/java/net/Inet6AddressTest.java

Author: tellison
Date: Tue Apr 25 03:08:54 2006
New Revision: 396823

URL: http://svn.apache.org/viewcvs?rev=396823&view=rev
Log:
Apply patch HARMONY-398 (J2SE5 Enhancement: Three ipv6-related new methods in java.net.Inet6Address)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Inet6Address.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Inet6Address.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Inet6Address.java?rev=396823&r1=396822&r2=396823&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Inet6Address.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Inet6Address.java Tue Apr 25 03:08:54 2006
@@ -20,6 +20,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamField;
+import java.util.Enumeration;
 
 import org.apache.harmony.luni.util.Inet6Util;
 
@@ -39,6 +40,11 @@
 	boolean scope_ifname_set = false;
 
 	String ifname = null;
+	
+	/*
+	 * scoped interface.
+	 */
+	private transient NetworkInterface scopedIf = null;
 
 	Inet6Address(byte address[]) {
 		ipaddress = address;
@@ -97,6 +103,79 @@
 	}
 	
 	/**
+	 * Constructs an IPv6 address according to the given <code>host</code>,
+	 * <code>addr</code> and <code>nif</code>. <code>scope_id</code> is
+	 * set according to the given <code>nif</code> and the
+	 * <code>addr<code> type(e.g. site local or link local).
+	 * 
+	 * @param host
+	 *            host name associated with the address
+	 * @param addr
+	 *            network address
+	 * @param nif
+	 *            the Network Interface that this address is associated with.
+	 * @return an Inet6Address instance
+	 * @throws UnknownHostException
+	 *             if the address is null or of invalid length, or the
+	 *             interface doesn't have a numeric scope id for the given
+	 *             address type.
+	 */
+	public static Inet6Address getByAddress(String host, byte[] addr,
+			NetworkInterface nif) throws UnknownHostException {
+		Inet6Address address = Inet6Address.getByAddress(host, addr, 0);
+		
+		// if nif is null, nothing needs to be set.
+		if(null == nif){
+			return address;
+		}
+		
+		// find the first address which matches the type addr,
+		// then set the scope_id, ifname and scopedIf.
+		Enumeration addressList = nif.getInetAddresses();
+		while (addressList.hasMoreElements()) {
+			InetAddress ia = (InetAddress) addressList.nextElement();
+			if (ia.getAddress().length == 16) {
+				Inet6Address v6ia = (Inet6Address) ia;
+				boolean isSameType = v6ia.compareLocalType(address);
+				if (isSameType) {
+					address.scope_id_set = true;
+					address.scope_id = v6ia.scope_id;
+					address.scope_ifname_set = true;
+					address.ifname = nif.getName();
+					address.scopedIf = nif;
+					break;
+				}
+			}
+		}
+		// if no address matches the type of addr, throws an UnknownHostException.
+		if (!address.scope_id_set) {
+			throw new UnknownHostException(
+					"Scope id is not found for the given address");
+		}
+		return address;
+	}
+	
+
+	/*
+	 * Returns true if one of following cases is true:
+	 * 1. both addresses are site local;
+	 * 2. both addresses are link local;
+	 * 3. ia is neither site local nor link local;
+	 */
+	private boolean compareLocalType(Inet6Address ia) {
+		if (ia.isSiteLocalAddress() && isSiteLocalAddress()) {
+			return true;
+		}
+		if (ia.isLinkLocalAddress() && isLinkLocalAddress()) {
+			return true;
+		}
+		if( !ia.isSiteLocalAddress() && !ia.isLinkLocalAddress()){
+			return true;
+		}
+		return false;
+	}
+	
+	/**
 	 * Constructs an InetAddress, representing the <code>address</code> and
 	 * <code>hostName</code> and <code>scope_id</code>
 	 * 
@@ -277,7 +356,33 @@
 	public String getHostAddress() {
 		return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
 	}
+	
+	/**
+	 * Returns the <code>scope id</code> of this address if it is associated
+	 * with an interface. Otherwise returns zero.
+	 * 
+	 * @return the scope_id.
+	 */
+	public int getScopeId() {
+		if(scope_id_set){
+			return scope_id;
+		}
+		return 0;
+	}
 
+	/**
+	 * Returns the network interface if this address is instanced with a scoped
+	 * network interface. Otherwise returns null.
+	 * 
+	 * @return the scoped network interface.
+	 */
+	public NetworkInterface getScopedInterface() {
+		if(scope_ifname_set){
+			return scopedIf;
+		}
+		return null;
+	}
+	
 	public int hashCode() {
 		/* Returns the low order int as the hash code */
 		return bytesToInt(ipaddress, 12);
@@ -342,6 +447,9 @@
 	 * @return String the description, as host/address
 	 */
 	public String toString() {
+		if (ifname != null) {
+			return super.toString() + "%" + ifname;
+		}
 		if (scope_id != 0) {
 			return super.toString() + "%" + scope_id;
 		}

Modified: 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=396823&r1=396822&r2=396823&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet6AddressTest.java Tue Apr 25 03:08:54 2006
@@ -17,6 +17,7 @@
 
 import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.NetworkInterface;
 import java.net.UnknownHostException;
 
 public class Inet6AddressTest extends junit.framework.TestCase {
@@ -856,11 +857,66 @@
 		Inet6Address.getByAddress("123", addr2, -1);
 	}
 
-	protected void setUp() {
+	/**
+	 * @tests java.net.Inet6Address#getByAddress(String, byte[],
+	 *        NetworkInterface)
+	 */
+	public void test_getByAddressLString$BLNetworkInterface()
+			throws UnknownHostException {
+		NetworkInterface nif = null;
+		try {
+			Inet6Address.getByAddress("123", null, nif);
+			fail("should throw UnknownHostException");
+		} catch (UnknownHostException uhe) {
+			// expected
+		}
+		byte[] addr1 = { (byte) 127, 0, 0, 1 };
+		try {
+			Inet6Address.getByAddress("123", addr1, nif);
+			fail("should throw UnknownHostException");
+		} catch (UnknownHostException uhe) {
+			// expected
+		}
+		byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
+				0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
+
+				0x7C, (byte) 0xB2 };
+		// should not throw any exception
+		Inet6Address.getByAddress("123", addr2, nif);
+	}
+
+	/**
+	 * @throws UnknownHostException
+	 * @tests java.net.Inet6Address#getScopeID()
+	 */
+	public void test_getScopeID() throws UnknownHostException {
+		Inet6Address v6ia;
+		byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
+				0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
+				(byte) 0xB2 };
+
+		v6ia = Inet6Address.getByAddress("123", addr, 3);
+		assertEquals(3, v6ia.getScopeId());
+
+		v6ia = Inet6Address.getByAddress("123", addr, 0);
+		assertEquals(0, v6ia.getScopeId());
+
+		v6ia = Inet6Address.getByAddress("123", addr, -1);
+		assertEquals(0, v6ia.getScopeId());
 	}
 
-	protected void tearDown() {
+	/**
+	 * @tests java.net.Inet6Address#getScopedInterface()
+	 */
+	public void test_getScopedInterface() throws UnknownHostException {
+		byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
+				(byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
+				(byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
+		Inet6Address v6Addr;
+		v6Addr = Inet6Address.getByAddress("123", addr, null);
+		assertNull(v6Addr.getScopedInterface());
 	}
+
 	
 	int bytesToInt(byte bytes[], int start) {