harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r543786 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/ test/java/tests/api/java/net/
Date Sat, 02 Jun 2007 21:18:49 GMT
Author: ayza
Date: Sat Jun  2 14:18:48 2007
New Revision: 543786

URL: http://svn.apache.org/viewvc?view=rev&rev=543786
Log:
Applying significantly reworked patch from HARMONY-2276 ([classlib][net] DatagramPacket truncate
packet length different than RI)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramPacket.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocket.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramPacket.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramPacket.java?view=diff&rev=543786&r1=543785&r2=543786
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramPacket.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramPacket.java
Sat Jun  2 14:18:48 2007
@@ -31,14 +31,25 @@
 
     byte[] data;
 
+    /**
+     * Length of the data to be sent or size of data that was received via
+     * DatagramSocket#receive() method call. 
+     */
     int length;
 
+    /**
+     * Size of internal buffer that is used to store received data. Should be
+     * greater or equal to "length" field.
+     */
+    int capacity;
+
     InetAddress address;
 
     int port = -1; // The default port number is -1
 
     int offset = 0;
 
+
     /**
      * Constructs a new <code>DatagramPacket</code> suitable for receiving
      * datagram packets of length up to <code>length</code>.
@@ -186,6 +197,7 @@
         data = buf;
         offset = anOffset;
         length = aLength;
+        capacity = aLength;
     }
 
     /**
@@ -196,6 +208,7 @@
      */
     public synchronized void setData(byte[] buf) {
         length = buf.length; // This will check for null
+        capacity = buf.length;
         data = buf;
         offset = 0;
     }
@@ -211,6 +224,7 @@
             throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
         }
         length = len;
+        capacity = len;
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocket.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocket.java?view=diff&rev=543786&r1=543785&r2=543786
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocket.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocket.java
Sat Jun  2 14:18:48 2007
@@ -320,32 +320,40 @@
     public synchronized void receive(DatagramPacket pack) throws IOException {
         checkClosedAndBind(true);
 
-        boolean secure = true;
-
-        InetAddress senderAddr = null;
-
-        int senderPort = 0;
+        InetAddress senderAddr;
+        int senderPort;
         DatagramPacket tempPack = new DatagramPacket(new byte[1], 1);
+        
+        // means that we have received the packet into the temporary buffer
         boolean copy = false;
 
         SecurityManager security = System.getSecurityManager();
-        if (address != null || security != null) { // The socket is connected
+
+        if (address != null || security != null) { 
+            // The socket is connected or we need to check security permissions
+
             // Check pack before peeking
             if (pack == null) {
                 throw new NullPointerException();
             }
-            secure = false;
-            while (!secure) {
+ 
+            // iterate over incoming packets
+            while (true) {
                 copy = false;
+                
+                // let's get sender's address and port
                 try {
                     senderPort = impl.peekData(tempPack);
                     senderAddr = tempPack.getAddress();
                 } catch (SocketException e) {
                     if (e.getMessage().equals(
                             "The socket does not support the operation")) { //$NON-NLS-1$
-                        tempPack = new DatagramPacket(new byte[pack.length],
-                                pack.getLength());
+                        // receive packet to temporary buffer
+                        tempPack = new DatagramPacket(new byte[pack.capacity],
+                                pack.capacity);
                         impl.receive(tempPack);
+                        // tempPack's length field is now updated, capacity is unchanged

+                        // let's extract address & port
                         senderAddr = tempPack.getAddress();
                         senderPort = tempPack.getPort();
                         copy = true;
@@ -353,46 +361,45 @@
                         throw e;
                     }
                 }
+                
                 if (address == null) {
+                    // if we are not connected let's check if we are allowed to
+                    // receive packets from sender's address and port 
                     try {
                         security.checkAccept(senderAddr.getHostName(),
                                 senderPort);
-                        if (!copy) {
-                            secure = true;
-                        }
+                        // address & port are valid
                         break;
                     } catch (SecurityException e) {
                         if (!copy) {
-                            if (tempPack == null) {
-                                tempPack = new DatagramPacket(
-                                        new byte[pack.length], pack.length);
-                            }
+                            // drop this packet and continue
                             impl.receive(tempPack);
                         }
                     }
                 } else if (port == senderPort && address.equals(senderAddr)) {
-                    if (!copy) {
-                        secure = true;
-                    }
+                    // we are connected and the packet came
+                    // from the address & port we are connected to
                     break;
                 } else if (!copy) {
-                    if (tempPack == null) {
-                        tempPack = new DatagramPacket(new byte[pack.length],
-                                pack.length);
-                    }
+                    // drop packet and continue
                     impl.receive(tempPack);
                 }
             }
         }
+
         if (copy) {
             System.arraycopy(tempPack.getData(), 0, pack.getData(), pack
                     .getOffset(), tempPack.getLength());
-            pack.setLength(tempPack.getLength());
+            // we shouldn't update the pack's capacity field in order to be
+            // compatible with RI
+            pack.length = tempPack.length;
             pack.setAddress(tempPack.getAddress());
             pack.setPort(tempPack.getPort());
-        }
-        if (secure) {
+        } else {
+            pack.setLength(pack.capacity);
             impl.receive(pack);
+            // pack's length field is now updated by native code call;
+            // pack's capacity field is unchanged 
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java?view=diff&rev=543786&r1=543785&r2=543786
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java
Sat Jun  2 14:18:48 2007
@@ -27,6 +27,7 @@
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.MulticastSocket;
 import java.net.NetworkInterface;
 import java.net.PortUnreachableException;
 import java.net.SocketAddress;
@@ -727,7 +728,7 @@
 	/**
 	 * @tests java.net.DatagramSocket#receive(java.net.DatagramPacket)
 	 */
-	public void test_receiveLjava_net_DatagramPacket() {
+	public void test_receiveLjava_net_DatagramPacket() throws IOException {
 		// Test for method void
 		// java.net.DatagramSocket.receive(java.net.DatagramPacket)
 
@@ -867,9 +868,76 @@
 		} catch (IOException e) {
 			fail("Unexpected IOException : " + e.getMessage());
 		}
+
 	}
 
-	/**
+    /**
+     * Tests receive() method in various combinations with
+     * DatagramPacket#getLength() and DatagramPacket#getLength(). 
+     * This is regression test for HARMONY-2276.
+     * 
+     * @throws IOException if some I/O error occured
+     */
+    public void test2276() throws IOException {
+        final String ADDRESS = "239.255.2.3";
+        final int PORT = Support_PortManager.getNextPortForUDP();
+        InetAddress group = InetAddress.getByName(ADDRESS);
+        MulticastSocket socket = new MulticastSocket(PORT);
+        byte[] recvData = new byte[100];
+        DatagramPacket recvDatagram = new DatagramPacket(recvData, recvData.length);    
   
+
+        String message = "Hello, world!";
+        String longerMessage = message + " again.";
+        String veryLongMessage = longerMessage + " Forever!";
+
+        socket.joinGroup(group);
+        socket.setSoTimeout(5000); // prevent eternal block in
+                                   // socket.receive()
+        // send & recieve packet
+        byte[] sendData = message.getBytes();
+        DatagramPacket sendDatagram = new DatagramPacket(sendData, 0,
+                sendData.length, group, PORT);
+        socket.send(sendDatagram);
+        socket.receive(recvDatagram);
+        String recvMessage = new String(recvData, 0, recvDatagram.getLength());
+        assertEquals(message, recvMessage);
+        
+        // send & receive longer packet
+        sendData = longerMessage.getBytes();
+        sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
+                group, PORT);
+        socket.send(sendDatagram);
+        socket.receive(recvDatagram);
+        recvMessage = new String(recvData, 0, recvDatagram.getLength());
+        assertEquals(longerMessage, recvMessage);
+
+        // tricky case, added to test compatibility with RI;
+        // depends on the previous test case
+        sendData = veryLongMessage.getBytes();
+        sendDatagram = new DatagramPacket(sendData, 0, sendData.length, group,
+                PORT);
+        socket.send(sendDatagram);
+        recvDatagram.setLength(recvDatagram.getLength()); // !!!
+        socket.receive(recvDatagram);
+        recvMessage = new String(recvData, 0, recvDatagram.getLength());
+        assertEquals(longerMessage, recvMessage);
+
+        // tests if received packet is truncated after length was set to 1
+        sendData = message.getBytes();
+        sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
+                group, PORT);
+        socket.send(sendDatagram);
+        recvDatagram.setLength(1);
+        socket.receive(recvDatagram);
+        assertEquals("Received message was not truncated", 1,
+                recvDatagram.getLength());
+        assertSame("Received message is invalid", sendData[0], recvData[0]);
+
+        socket.leaveGroup(group);
+        socket.close();
+    }
+
+    /**
 	 * @tests java.net.DatagramSocket#send(java.net.DatagramPacket)
 	 */
 	public void test_sendLjava_net_DatagramPacket() throws Exception {

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java?view=diff&rev=543786&r1=543785&r2=543786
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java
Sat Jun  2 14:18:48 2007
@@ -61,19 +61,31 @@
 
 		volatile public byte[] rbuf = new byte[512];
 
-		volatile DatagramPacket rdp = null;
+        volatile DatagramPacket rdp = null;
+        
+        private InetAddress groupAddr = null;
+        private SocketAddress groupSockAddr = null;
+        private NetworkInterface groupNI = null;
 
-		public void run() {
+        public void run() {
 			try {
-				while (running) {
+                byte[] tmpbuf = new byte[512];
+                DatagramPacket tmpPack =
+                        new DatagramPacket(tmpbuf, tmpbuf.length);
+                
+                while (running) {
 					try {
-						ms.receive(rdp);
-					} catch (java.io.InterruptedIOException e) {
-						Thread.yield();
+                        ms.receive(tmpPack);
+                        
+                        System.arraycopy(tmpPack.getData(), 0, rdp.getData(),
+                                rdp.getOffset(), tmpPack.getLength());
+                        rdp.setLength(tmpPack.getLength());
+                        rdp.setAddress(tmpPack.getAddress());
+                        rdp.setPort(tmpPack.getPort());
+                    } catch (java.io.InterruptedIOException e) {
+                        Thread.yield();
 					}
-					;
 				}
-				;
 			} catch (java.io.IOException e) {
 				System.out.println("Multicast server failed: " + e);
 			} finally {
@@ -81,33 +93,39 @@
 			}
 		}
 
-		public synchronized void leaveGroup(InetAddress aGroup)
-				throws java.io.IOException {
-			ms.leaveGroup(aGroup);
-		}
-
 		public void stopServer() {
 			running = false;
-		}
-
-		public MulticastServer(InetAddress anAddress, int aPort)
-				throws java.io.IOException {
-			rbuf = new byte[512];
-			rbuf[0] = -1;
-			rdp = new DatagramPacket(rbuf, rbuf.length);
-			ms = new MulticastSocket(aPort);
-			ms.setSoTimeout(2000);
-			ms.joinGroup(anAddress);
-		}
+            try {
+                if (groupAddr != null) {
+                    ms.leaveGroup(groupAddr);
+                } else if (groupSockAddr != null) {
+                    ms.leaveGroup(groupSockAddr, groupNI);
+                }
+            } catch (IOException e) {}
+        }
 
-		public MulticastServer(SocketAddress anAddress, int aPort,
+        public MulticastServer(InetAddress anAddress, int aPort)
+                throws java.io.IOException {
+            rbuf = new byte[512];
+            rbuf[0] = -1;
+            rdp = new DatagramPacket(rbuf, rbuf.length);
+            ms = new MulticastSocket(aPort);
+            ms.setSoTimeout(2000);
+            groupAddr = anAddress;
+            ms.joinGroup(groupAddr);
+        }
+        
+        
+        public MulticastServer(SocketAddress anAddress, int aPort,
 				NetworkInterface netInterface) throws java.io.IOException {
 			rbuf = new byte[512];
 			rbuf[0] = -1;
 			rdp = new DatagramPacket(rbuf, rbuf.length);
 			ms = new MulticastSocket(aPort);
 			ms.setSoTimeout(2000);
-			ms.joinGroup(anAddress, netInterface);
+            groupSockAddr = anAddress;
+            groupNI = netInterface;
+            ms.joinGroup(groupSockAddr, groupNI);
 		}
 	}
 
@@ -319,7 +337,7 @@
 	public void test_joinGroupLjava_net_InetAddress() {
 		// Test for method void
 		// java.net.MulticastSocket.joinGroup(java.net.InetAddress)
-		String msg = null;
+        String msg = null;
 		InetAddress group = null;
 		int[] ports = Support_PortManager.getNextPortsForUDP(2);
 		int groupPort = ports[0];
@@ -337,9 +355,8 @@
 		} catch (Exception e) {
 			fail("Exception during joinGroup test: " + e.toString());
 		}
-		assertTrue("Group member did not recv data: ", new String(server.rdp
+        assertTrue("Group member did not recv data: ", new String(server.rdp
 				.getData(), 0, server.rdp.getLength()).equals(msg));
-
 	}
 
 	/**



Mime
View raw message