From 312b9d15b28abadf72015fbec33d9386e90f5ff2 Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 31 Jan 2010 13:02:51 +0000 Subject: [PATCH] Preparing to merge StunStack and StunProvider as there is no good reason for them to live apart. --- .../ice4j/MessageEventDispatchingTest.java | 107 +++++++++++++++--- test/org/ice4j/StunTestSuite.java | 8 +- .../OptionalAttributeAttributeTest.java | 4 +- .../UnknownAttributesAttributeTest.java | 10 +- test/org/ice4j/attribute/XorOnlyTest.java | 2 +- test/org/ice4j/stack/DatagramCollector.java | 13 +-- test/org/ice4j/stack/ShallowStackTest.java | 96 +++++++++++----- 7 files changed, 172 insertions(+), 68 deletions(-) diff --git a/test/org/ice4j/MessageEventDispatchingTest.java b/test/org/ice4j/MessageEventDispatchingTest.java index a3b36a0..5b62074 100644 --- a/test/org/ice4j/MessageEventDispatchingTest.java +++ b/test/org/ice4j/MessageEventDispatchingTest.java @@ -8,6 +8,7 @@ package org.ice4j; import java.net.*; import java.util.*; +import java.util.logging.*; import junit.framework.*; @@ -36,13 +37,13 @@ public class MessageEventDispatchingTest extends TestCase * The Address of the server. */ TransportAddress serverAddress - = new TransportAddress("127.0.0.2", 5255, Transport.UDP); + = new TransportAddress("127.0.0.1", 5255, Transport.UDP); /** * The address of the second server. */ TransportAddress serverAddress2 - = new TransportAddress("127.0.0.2", 5259, Transport.UDP); + = new TransportAddress("127.0.0.1", 5259, Transport.UDP); /** * The socket that the client is using. @@ -91,8 +92,8 @@ public class MessageEventDispatchingTest extends TestCase stunStack = StunStack.getInstance(); clientSock = new DatagramSocket(clientAddress); - serverSock = new DatagramSocket(clientAddress); - serverSock = new DatagramSocket(serverAddress2); + serverSock = new DatagramSocket(serverAddress); + serverSock2 = new DatagramSocket(serverAddress2); stunStack.addSocket(clientSock); stunStack.addSocket(serverSock); @@ -114,9 +115,13 @@ public class MessageEventDispatchingTest extends TestCase */ protected void tearDown() throws Exception { + stunStack.removeSocket(clientAddress); + stunStack.removeSocket(serverAddress); + stunStack.removeSocket(serverAddress2); + clientSock.close(); serverSock.close(); - serverSock.close(); + serverSock2.close(); requestCollector = null; responseCollector = null; @@ -131,12 +136,14 @@ public class MessageEventDispatchingTest extends TestCase */ public void testClientTransactionTimeouts() throws Exception { - + String oldRetransValue = System.getProperty( + "org.ice4j.MAX_RETRANSMISSIONS"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "1"); stunStack.getProvider().sendRequest(bindingRequest, serverAddress, clientAddress, responseCollector); - Thread.sleep(12000); + responseCollector.waitForTimeout(); assertEquals( "No timeout was produced upon expiration of a client transaction", @@ -145,6 +152,14 @@ public class MessageEventDispatchingTest extends TestCase assertEquals( "No timeout was produced upon expiration of a client transaction", responseCollector.receivedResponses.get(0), "timeout"); + + //restore the retransmissions prop in case others are counting on + //defaults. + if(oldRetransValue != null) + System.getProperty( "org.ice4j.MAX_RETRANSMISSIONS", + oldRetransValue); + else + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); } /** @@ -162,7 +177,7 @@ public class MessageEventDispatchingTest extends TestCase clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(50); + requestCollector.waitForRequest(); //verify assertTrue("No MessageEvents have been dispatched", @@ -192,7 +207,8 @@ public class MessageEventDispatchingTest extends TestCase clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(50); + requestCollector.waitForRequest(); + requestCollector2.waitForRequest(); //verify assertTrue( @@ -220,7 +236,7 @@ public class MessageEventDispatchingTest extends TestCase responseCollector); //wait for the message to arrive - Thread.sleep(50); + requestCollector.waitForRequest(); StunMessageEvent evt = requestCollector.receivedRequests.get(0); byte[] tid = evt.getMessage().getTransactionID(); @@ -230,7 +246,7 @@ public class MessageEventDispatchingTest extends TestCase clientAddress); //wait for retransmissions - Thread.sleep(50); + responseCollector.waitForResponse(); //verify that we got the response. assertTrue( @@ -254,7 +270,26 @@ public class MessageEventDispatchingTest extends TestCase */ public void requestReceived(StunMessageEvent evt) { - receivedRequests.add(evt); + synchronized (this) + { + receivedRequests.add(evt); + notifyAll(); + } + } + + public void waitForRequest() + { + synchronized(this) + { + if (receivedRequests.size() > 0) + return; + try + { + wait(50); + } + catch (InterruptedException e) + {} + } } } @@ -275,7 +310,11 @@ public class MessageEventDispatchingTest extends TestCase */ public void processResponse(StunMessageEvent responseEvt) { - receivedResponses.add(responseEvt); + synchronized(this) + { + receivedResponses.add(responseEvt); + notifyAll(); + } } /** @@ -283,7 +322,47 @@ public class MessageEventDispatchingTest extends TestCase */ public void processTimeout() { - receivedResponses.add(new String("timeout")); + synchronized(this) + { + receivedResponses.add(new String("timeout")); + notifyAll(); + } + } + + /** + * Waits for a short period of time for a response to arrive + */ + public void waitForResponse() + { + synchronized(this) + { + try + { + if (receivedResponses.size() > 0) + return; + wait(50); + } + catch (InterruptedException e) + {} + } + } + + /** + * Waits for a long period of time for a timeout trigger to fire. + */ + public void waitForTimeout() + { + synchronized(this) + { + try + { + if (receivedResponses.size() > 0) + return; + wait(12000); + } + catch (InterruptedException e) + {} + } } } diff --git a/test/org/ice4j/StunTestSuite.java b/test/org/ice4j/StunTestSuite.java index 7c014fe..278df51 100644 --- a/test/org/ice4j/StunTestSuite.java +++ b/test/org/ice4j/StunTestSuite.java @@ -34,7 +34,7 @@ public class StunTestSuite public static Test suite() { TestSuite suite = new TestSuite(); -/* + //attributes suite.addTestSuite(org.ice4j.attribute. AddressAttributeTest.class); @@ -59,13 +59,13 @@ public class StunTestSuite //messages suite.addTestSuite(org.ice4j.message.MessageFactoryTest.class); suite.addTestSuite(org.ice4j.message.MessageTest.class); -*/ + //stack suite.addTestSuite(org.ice4j.stack.ShallowStackTest.class); -/* + //event dispatching suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class); - + /* //transactions suite.addTestSuite(org.ice4j.TransactionSupportTests.class); diff --git a/test/org/ice4j/attribute/OptionalAttributeAttributeTest.java b/test/org/ice4j/attribute/OptionalAttributeAttributeTest.java index ed1e21f..1e428a1 100644 --- a/test/org/ice4j/attribute/OptionalAttributeAttributeTest.java +++ b/test/org/ice4j/attribute/OptionalAttributeAttributeTest.java @@ -52,8 +52,8 @@ public class OptionalAttributeAttributeTest extends TestCase /** - * Test whetner sample binary arrays are correctly decoded. - * @throws StunException + * Test whether sample binary arrays are correctly decoded. + * @throws StunException if anything goes wrong. */ public void testDecodeAttributeBody() throws StunException { diff --git a/test/org/ice4j/attribute/UnknownAttributesAttributeTest.java b/test/org/ice4j/attribute/UnknownAttributesAttributeTest.java index 7c198e4..c8ae1c3 100644 --- a/test/org/ice4j/attribute/UnknownAttributesAttributeTest.java +++ b/test/org/ice4j/attribute/UnknownAttributesAttributeTest.java @@ -1,8 +1,8 @@ /* - * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). * - * Distributable under LGPL license. - * See terms of license at gnu.org. + * Distributable under LGPL license. See terms of license at gnu.org. */ package org.ice4j.attribute; @@ -84,7 +84,7 @@ public class UnknownAttributesAttributeTest extends TestCase { /** * Tests whether a sample binary array is properly decoded. - * @throws StunException + * @throws StunException if anything goes wrong. */ public void testDecodeAttributeBody() throws StunException { //a copy of the array in the fixture: @@ -224,7 +224,7 @@ public class UnknownAttributesAttributeTest extends TestCase { unknownAttributesAttribute.addAttributeID(expectedId1); unknownAttributesAttribute.addAttributeID(expectedId2); - Iterator iterator = unknownAttributesAttribute.getAttributes(); + Iterator iterator = unknownAttributesAttribute.getAttributes(); actualId1 = ((Character)iterator.next()).charValue(); actualId2 = ((Character)iterator.next()).charValue();; diff --git a/test/org/ice4j/attribute/XorOnlyTest.java b/test/org/ice4j/attribute/XorOnlyTest.java index 5307428..b3822b3 100644 --- a/test/org/ice4j/attribute/XorOnlyTest.java +++ b/test/org/ice4j/attribute/XorOnlyTest.java @@ -64,7 +64,7 @@ public class XorOnlyTest extends TestCase /** * Test positive and negative XorOnly.equals() returns - * @throws Exteption if decoding fails + * @throws Exception if decoding fails */ public void testEquals() throws Exception { diff --git a/test/org/ice4j/stack/DatagramCollector.java b/test/org/ice4j/stack/DatagramCollector.java index b39c34b..5824d55 100644 --- a/test/org/ice4j/stack/DatagramCollector.java +++ b/test/org/ice4j/stack/DatagramCollector.java @@ -28,7 +28,7 @@ public class DatagramCollector synchronized (this) { - notify(); + notifyAll(); } } @@ -45,15 +45,6 @@ public class DatagramCollector receivedPacket = new DatagramPacket(new byte[4096], 4096); new Thread(this).start(); - - //give the guy a chance to start - try - { - Thread.sleep(200); - } - catch (InterruptedException ex) - { - } } public void waitForPacket() @@ -62,7 +53,7 @@ public class DatagramCollector { try { - wait(1000); + wait(50); } catch (InterruptedException e) { diff --git a/test/org/ice4j/stack/ShallowStackTest.java b/test/org/ice4j/stack/ShallowStackTest.java index d9bb561..3f0b807 100644 --- a/test/org/ice4j/stack/ShallowStackTest.java +++ b/test/org/ice4j/stack/ShallowStackTest.java @@ -40,7 +40,8 @@ public class ShallowStackTest extends TestCase { private DatagramPacket bindingRequestPacket = new DatagramPacket(new byte[4096], 4096); - public ShallowStackTest(String name) { + public ShallowStackTest(String name) + { super(name); } @@ -60,12 +61,9 @@ public class ShallowStackTest extends TestCase { stunStack = StunStack.getInstance(); stunProvider = stunStack.getProvider(); -System.out.println("binding 1!!!"); //access point localSock = new DatagramSocket(localAddress); - stunStack.addSocket(localSock); -System.out.println("binding 2!!!"); //init the dummy server dummyServerSocket = new DatagramSocket( dummyServerAddress ); @@ -75,13 +73,10 @@ System.out.println("binding 2!!!"); throws Exception { msgFixture.tearDown(); -System.out.println("tearing down!!!"); stunStack.removeSocket(localAddress); -System.out.println("unbinding 1!!!"); localSock.close(); -System.out.println("unbinding 2!!!"); dummyServerSocket.close(); @@ -89,6 +84,7 @@ System.out.println("unbinding 2!!!"); super.tearDown(); } + /** * Sends a binding request using the stack to a bare socket, and verifies * that it is received and that the contents of the datagram corresponds to @@ -100,22 +96,22 @@ System.out.println("unbinding 2!!!"); throws Exception { Request bindingRequest = MessageFactory.createBindingRequest(); -System.out.println("test 1!!!"); + dgramCollector.startListening(dummyServerSocket); -System.out.println("test 2!!!"); + stunProvider.sendRequest(bindingRequest, dummyServerAddress, localAddress, new SimpleResponseCollector()); -System.out.println("test 3!!!"); + //wait for its arrival dgramCollector.waitForPacket(); -System.out.println("test 4!!!"); + DatagramPacket receivedPacket = dgramCollector.collectPacket(); -System.out.println("test 5!!!"); + assertTrue("The stack did not properly send a Binding Request", (receivedPacket.getLength() > 0)); -System.out.println("test 6!!!"); + Request receivedRequest = (Request)Request.decode(receivedPacket.getData(), (char)0, @@ -124,13 +120,13 @@ System.out.println("test 6!!!"); +"one that was sent.", bindingRequest, //expected receivedRequest); // actual -System.out.println("test 7!!!"); + //wait for retransmissions dgramCollector.startListening(dummyServerSocket); -System.out.println("test 8!!!"); + dgramCollector.waitForPacket(); -System.out.println("test 9!!!"); + receivedPacket = dgramCollector.collectPacket(); assertTrue("The stack did not retransmit a Binding Request", @@ -143,9 +139,6 @@ System.out.println("test 9!!!"); assertEquals("The retransmitted request did not match the original.", bindingRequest, //expected receivedRequest); // actual - - - } /** @@ -166,7 +159,7 @@ System.out.println("test 9!!!"); localAddress)); //wait for the packet to arrive - try{ Thread.sleep(50); }catch (InterruptedException ex){} + requestCollector.waitForRequest(); Request collectedRequest = requestCollector.collectedRequest; @@ -199,12 +192,7 @@ System.out.println("test 9!!!"); localAddress)); //wait for the packet to arrive - try{ - synchronized(this) - { - wait(50); - } - }catch (InterruptedException ex){} + requestCollector.waitForRequest(); Request collectedRequest = requestCollector.collectedRequest; @@ -231,7 +219,7 @@ System.out.println("test 9!!!"); dummyServerAddress); //wait for its arrival - try{ Thread.sleep(50); }catch (InterruptedException ex){} + dgramCollector.waitForPacket(); DatagramPacket receivedPacket = dgramCollector.collectPacket(); @@ -261,7 +249,7 @@ System.out.println("test 9!!!"); collector); //wait for its arrival - try{ Thread.sleep(50); }catch (InterruptedException ex){} + collector.waitForResponse(); //create the right response byte response[] = new byte[msgFixture.bindingResponse.length]; @@ -282,7 +270,7 @@ System.out.println("test 9!!!"); localAddress)); //wait for the packet to arrive - try{ Thread.sleep(50); }catch (InterruptedException ex){} + collector.waitForResponse(); Response collectedResponse = collector.collectedResponse; @@ -300,12 +288,37 @@ System.out.println("test 9!!!"); public void processResponse(StunMessageEvent evt) { collectedResponse = (Response)evt.getMessage(); - logger.info("Received response."); + logger.finest("Received response."); + + synchronized(this) + { + notifyAll(); + } } public void processTimeout() { logger.info("Timeout"); + + synchronized(this) + { + notifyAll(); + } + } + + public void waitForResponse() + { + synchronized(this) + { + try + { + wait(50); + } + catch (InterruptedException e) + { + logger.log(Level.INFO, "oops", e); + } + } } } @@ -313,11 +326,32 @@ System.out.println("test 9!!!"); implements RequestListener { private Request collectedRequest = null; + public void requestReceived(StunMessageEvent evt) { collectedRequest = (Request)evt.getMessage(); stunProvider.removeRequestListener(this); - logger.info("Received request."); + logger.finest("Received request."); + + synchronized(this) + { + notifyAll(); + } + } + + public void waitForRequest() + { + synchronized(this) + { + try + { + wait(50); + } + catch (InterruptedException e) + { + logger.log(Level.INFO, "oops", e); + } + } } } /*