Preparing for some serious refactoring of the stun stack
This commit is contained in:
parent
0d7d7f8a68
commit
cc345098db
@ -94,9 +94,9 @@ public class MessageEventDispatchingTest extends TestCase
|
|||||||
serverSock = new DatagramSocket(clientAddress);
|
serverSock = new DatagramSocket(clientAddress);
|
||||||
serverSock = new DatagramSocket(serverAddress2);
|
serverSock = new DatagramSocket(serverAddress2);
|
||||||
|
|
||||||
stunStack.installNetAccessPoint(clientSock);
|
stunStack.addSocket(clientSock);
|
||||||
stunStack.installNetAccessPoint(serverSock);
|
stunStack.addSocket(serverSock);
|
||||||
stunStack.installNetAccessPoint(serverSock2);
|
stunStack.addSocket(serverSock2);
|
||||||
|
|
||||||
bindingRequest = MessageFactory.createBindingRequest();
|
bindingRequest = MessageFactory.createBindingRequest();
|
||||||
bindingResponse = MessageFactory.createBindingResponse(
|
bindingResponse = MessageFactory.createBindingResponse(
|
||||||
|
|||||||
@ -1,26 +1,40 @@
|
|||||||
/*
|
/*
|
||||||
* 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.
|
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||||
* See terms of license at gnu.org.
|
|
||||||
*/
|
*/
|
||||||
package org.ice4j;
|
package org.ice4j;
|
||||||
|
|
||||||
import junit.framework.*;
|
import junit.framework.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all tests in the ice4j project.
|
||||||
|
*
|
||||||
|
* @author Emil Ivov
|
||||||
|
*/
|
||||||
public class StunTestSuite
|
public class StunTestSuite
|
||||||
extends TestCase
|
extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of the suite
|
||||||
|
*
|
||||||
|
* @param s test name
|
||||||
|
*/
|
||||||
public StunTestSuite(String s)
|
public StunTestSuite(String s)
|
||||||
{
|
{
|
||||||
super(s);
|
super(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the suite of tests to run.
|
||||||
|
* @return the suite of tests to run.
|
||||||
|
*/
|
||||||
public static Test suite()
|
public static Test suite()
|
||||||
{
|
{
|
||||||
TestSuite suite = new TestSuite();
|
TestSuite suite = new TestSuite();
|
||||||
|
/*
|
||||||
//attributes
|
//attributes
|
||||||
suite.addTestSuite(org.ice4j.attribute.
|
suite.addTestSuite(org.ice4j.attribute.
|
||||||
AddressAttributeTest.class);
|
AddressAttributeTest.class);
|
||||||
@ -45,10 +59,10 @@ public class StunTestSuite
|
|||||||
//messages
|
//messages
|
||||||
suite.addTestSuite(org.ice4j.message.MessageFactoryTest.class);
|
suite.addTestSuite(org.ice4j.message.MessageFactoryTest.class);
|
||||||
suite.addTestSuite(org.ice4j.message.MessageTest.class);
|
suite.addTestSuite(org.ice4j.message.MessageTest.class);
|
||||||
|
*/
|
||||||
//stack
|
//stack
|
||||||
suite.addTestSuite(org.ice4j.stack.ShallowStackTest.class);
|
suite.addTestSuite(org.ice4j.stack.ShallowStackTest.class);
|
||||||
|
/*
|
||||||
//event dispatching
|
//event dispatching
|
||||||
suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class);
|
suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class);
|
||||||
|
|
||||||
@ -59,7 +73,7 @@ public class StunTestSuite
|
|||||||
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest.class);
|
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest.class);
|
||||||
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v6.class);
|
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v6.class);
|
||||||
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v4v6.class);
|
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v4v6.class);
|
||||||
|
*/
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,8 +65,8 @@ public class TransactionSupportTests extends TestCase
|
|||||||
clientSock = new DatagramSocket(clientAddress);
|
clientSock = new DatagramSocket(clientAddress);
|
||||||
serverSock = new DatagramSocket(serverAddress);
|
serverSock = new DatagramSocket(serverAddress);
|
||||||
|
|
||||||
StunStack.getInstance().installNetAccessPoint(clientSock);
|
StunStack.getInstance().addSocket(clientSock);
|
||||||
StunStack.getInstance().installNetAccessPoint(serverSock);
|
StunStack.getInstance().addSocket(serverSock);
|
||||||
|
|
||||||
bindingRequest = MessageFactory.createBindingRequest();
|
bindingRequest = MessageFactory.createBindingRequest();
|
||||||
bindingResponse = MessageFactory.createBindingResponse(
|
bindingResponse = MessageFactory.createBindingResponse(
|
||||||
|
|||||||
@ -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.
|
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||||
* See terms of license at gnu.org.
|
|
||||||
*/
|
*/
|
||||||
package org.ice4j.stack;
|
package org.ice4j.stack;
|
||||||
|
|
||||||
@ -23,7 +23,14 @@ public class DatagramCollector
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
sock.receive(receivedPacket);
|
sock.receive(receivedPacket);
|
||||||
|
|
||||||
|
synchronized (this)
|
||||||
|
{
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
@ -49,6 +56,22 @@ public class DatagramCollector
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void waitForPacket()
|
||||||
|
{
|
||||||
|
synchronized(this)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
wait(1000);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public DatagramPacket collectPacket()
|
public DatagramPacket collectPacket()
|
||||||
{
|
{
|
||||||
//recycle
|
//recycle
|
||||||
|
|||||||
@ -18,10 +18,7 @@ import org.ice4j.message.*;
|
|||||||
/**
|
/**
|
||||||
* All unit stack tests should be provided later. I just don't have the time now.
|
* All unit stack tests should be provided later. I just don't have the time now.
|
||||||
*
|
*
|
||||||
* <p>Organisation: <p> Louis Pasteur University, Strasbourg, France</p>
|
|
||||||
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
|
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
* @version 0.1
|
|
||||||
*/
|
*/
|
||||||
public class ShallowStackTest extends TestCase {
|
public class ShallowStackTest extends TestCase {
|
||||||
|
|
||||||
@ -32,17 +29,16 @@ public class ShallowStackTest extends TestCase {
|
|||||||
private StunStack stunStack = null;
|
private StunStack stunStack = null;
|
||||||
private MsgFixture msgFixture = null;
|
private MsgFixture msgFixture = null;
|
||||||
|
|
||||||
private TransportAddress stun4jAddressOfDummyImpl = null;
|
private TransportAddress dummyServerAddress = null;
|
||||||
private InetSocketAddress socketAddressOfStun4jStack = null;
|
private TransportAddress localAddress = null;
|
||||||
|
|
||||||
private DatagramCollector dgramCollector = new DatagramCollector();
|
private DatagramCollector dgramCollector = new DatagramCollector();
|
||||||
|
|
||||||
private TransportAddress localAddress = null;
|
|
||||||
private DatagramSocket localSock = null;
|
private DatagramSocket localSock = null;
|
||||||
|
|
||||||
private DatagramSocket dummyImplSocket = null;
|
private DatagramSocket dummyServerSocket = null;
|
||||||
private DatagramPacket bindingRequestPacket
|
private DatagramPacket bindingRequestPacket
|
||||||
= new DatagramPacket(new byte[4096], 4096);
|
= new DatagramPacket(new byte[4096], 4096);
|
||||||
|
|
||||||
public ShallowStackTest(String name) {
|
public ShallowStackTest(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
@ -55,39 +51,46 @@ public class ShallowStackTest extends TestCase {
|
|||||||
msgFixture = new MsgFixture();
|
msgFixture = new MsgFixture();
|
||||||
msgFixture.setUp();
|
msgFixture.setUp();
|
||||||
//Addresses
|
//Addresses
|
||||||
stun4jAddressOfDummyImpl = new TransportAddress(
|
dummyServerAddress = new TransportAddress(
|
||||||
"127.0.0.1", 6000, Transport.UDP);
|
"127.0.0.1", 6004, Transport.UDP);
|
||||||
socketAddressOfStun4jStack = new TransportAddress(
|
localAddress = new TransportAddress(
|
||||||
"127.0.0.1", 5000, Transport.UDP);
|
"127.0.0.1", 5004, Transport.UDP);
|
||||||
|
|
||||||
//init the stack
|
//init the stack
|
||||||
stunStack = StunStack.getInstance();
|
stunStack = StunStack.getInstance();
|
||||||
stunProvider = stunStack.getProvider();
|
stunProvider = stunStack.getProvider();
|
||||||
|
|
||||||
|
System.out.println("binding 1!!!");
|
||||||
//access point
|
//access point
|
||||||
localAddress = new TransportAddress("127.0.0.1", 5000, Transport.UDP);
|
|
||||||
localSock = new DatagramSocket(localAddress);
|
localSock = new DatagramSocket(localAddress);
|
||||||
|
|
||||||
stunStack.installNetAccessPoint(localSock);
|
stunStack.addSocket(localSock);
|
||||||
|
System.out.println("binding 2!!!");
|
||||||
|
|
||||||
//init the phoney stack
|
//init the dummy server
|
||||||
dummyImplSocket = new DatagramSocket( 6000 );
|
dummyServerSocket = new DatagramSocket( dummyServerAddress );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tearDown()
|
protected void tearDown()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
msgFixture.tearDown();
|
msgFixture.tearDown();
|
||||||
|
System.out.println("tearing down!!!");
|
||||||
|
stunStack.removeSocket(localAddress);
|
||||||
|
|
||||||
|
System.out.println("unbinding 1!!!");
|
||||||
|
localSock.close();
|
||||||
|
|
||||||
|
System.out.println("unbinding 2!!!");
|
||||||
|
dummyServerSocket.close();
|
||||||
|
|
||||||
dummyImplSocket.close();
|
|
||||||
|
|
||||||
msgFixture = null;
|
msgFixture = null;
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a binding request using the stack to a phoney socket, and verifies
|
* 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
|
* that it is received and that the contents of the datagram corresponds to
|
||||||
* the request that was sent.
|
* the request that was sent.
|
||||||
*
|
*
|
||||||
@ -97,22 +100,22 @@ public class ShallowStackTest extends TestCase {
|
|||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
Request bindingRequest = MessageFactory.createBindingRequest();
|
Request bindingRequest = MessageFactory.createBindingRequest();
|
||||||
|
System.out.println("test 1!!!");
|
||||||
dgramCollector.startListening(dummyImplSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
|
System.out.println("test 2!!!");
|
||||||
stunProvider.sendRequest(bindingRequest,
|
stunProvider.sendRequest(bindingRequest,
|
||||||
stun4jAddressOfDummyImpl,
|
dummyServerAddress,
|
||||||
localAddress,
|
localAddress,
|
||||||
new SimpleResponseCollector());
|
new SimpleResponseCollector());
|
||||||
|
System.out.println("test 3!!!");
|
||||||
//wait for its arrival
|
//wait for its arrival
|
||||||
try{ Thread.sleep(500); }catch (InterruptedException ex){}
|
dgramCollector.waitForPacket();
|
||||||
|
System.out.println("test 4!!!");
|
||||||
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
||||||
|
System.out.println("test 5!!!");
|
||||||
assertTrue("The stack did not properly send a Binding Request",
|
assertTrue("The stack did not properly send a Binding Request",
|
||||||
(receivedPacket.getLength() > 0));
|
(receivedPacket.getLength() > 0));
|
||||||
|
System.out.println("test 6!!!");
|
||||||
Request receivedRequest =
|
Request receivedRequest =
|
||||||
(Request)Request.decode(receivedPacket.getData(),
|
(Request)Request.decode(receivedPacket.getData(),
|
||||||
(char)0,
|
(char)0,
|
||||||
@ -121,13 +124,13 @@ public class ShallowStackTest extends TestCase {
|
|||||||
+"one that was sent.",
|
+"one that was sent.",
|
||||||
bindingRequest, //expected
|
bindingRequest, //expected
|
||||||
receivedRequest); // actual
|
receivedRequest); // actual
|
||||||
|
System.out.println("test 7!!!");
|
||||||
//wait for retransmissions
|
//wait for retransmissions
|
||||||
|
|
||||||
dgramCollector.startListening(dummyImplSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
|
System.out.println("test 8!!!");
|
||||||
try{ Thread.sleep(1000); }catch (InterruptedException ex){}
|
dgramCollector.waitForPacket();
|
||||||
|
System.out.println("test 9!!!");
|
||||||
receivedPacket = dgramCollector.collectPacket();
|
receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
assertTrue("The stack did not retransmit a Binding Request",
|
assertTrue("The stack did not retransmit a Binding Request",
|
||||||
@ -157,13 +160,13 @@ public class ShallowStackTest extends TestCase {
|
|||||||
SimpleRequestCollector requestCollector = new SimpleRequestCollector();
|
SimpleRequestCollector requestCollector = new SimpleRequestCollector();
|
||||||
stunProvider.addRequestListener(requestCollector);
|
stunProvider.addRequestListener(requestCollector);
|
||||||
|
|
||||||
dummyImplSocket.send(new DatagramPacket(
|
dummyServerSocket.send(new DatagramPacket(
|
||||||
msgFixture.bindingRequest2,
|
msgFixture.bindingRequest2,
|
||||||
msgFixture.bindingRequest2.length,
|
msgFixture.bindingRequest2.length,
|
||||||
socketAddressOfStun4jStack));
|
localAddress));
|
||||||
|
|
||||||
//wait for the packet to arrive
|
//wait for the packet to arrive
|
||||||
try{ Thread.sleep(500); }catch (InterruptedException ex){}
|
try{ Thread.sleep(50); }catch (InterruptedException ex){}
|
||||||
|
|
||||||
Request collectedRequest = requestCollector.collectedRequest;
|
Request collectedRequest = requestCollector.collectedRequest;
|
||||||
|
|
||||||
@ -190,16 +193,16 @@ public class ShallowStackTest extends TestCase {
|
|||||||
SimpleRequestCollector requestCollector = new SimpleRequestCollector();
|
SimpleRequestCollector requestCollector = new SimpleRequestCollector();
|
||||||
stunProvider.addRequestListener(requestCollector);
|
stunProvider.addRequestListener(requestCollector);
|
||||||
|
|
||||||
dummyImplSocket.send(new DatagramPacket(
|
dummyServerSocket.send(new DatagramPacket(
|
||||||
msgFixture.bindingRequest,
|
msgFixture.bindingRequest,
|
||||||
msgFixture.bindingRequest.length,
|
msgFixture.bindingRequest.length,
|
||||||
socketAddressOfStun4jStack));
|
localAddress));
|
||||||
|
|
||||||
//wait for the packet to arrive
|
//wait for the packet to arrive
|
||||||
try{
|
try{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
wait(500);
|
wait(50);
|
||||||
}
|
}
|
||||||
}catch (InterruptedException ex){}
|
}catch (InterruptedException ex){}
|
||||||
|
|
||||||
@ -220,15 +223,15 @@ public class ShallowStackTest extends TestCase {
|
|||||||
msgFixture.ADDRESS_ATTRIBUTE_PORT_3, Transport.UDP));
|
msgFixture.ADDRESS_ATTRIBUTE_PORT_3, Transport.UDP));
|
||||||
|
|
||||||
//---------- send & receive the response -------------------------------
|
//---------- send & receive the response -------------------------------
|
||||||
dgramCollector.startListening(dummyImplSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
|
|
||||||
stunProvider.sendResponse(collectedRequest.getTransactionID(),
|
stunProvider.sendResponse(collectedRequest.getTransactionID(),
|
||||||
bindingResponse,
|
bindingResponse,
|
||||||
localAddress,
|
localAddress,
|
||||||
stun4jAddressOfDummyImpl);
|
dummyServerAddress);
|
||||||
|
|
||||||
//wait for its arrival
|
//wait for its arrival
|
||||||
try{ Thread.sleep(500); }catch (InterruptedException ex){}
|
try{ Thread.sleep(50); }catch (InterruptedException ex){}
|
||||||
|
|
||||||
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
@ -253,12 +256,12 @@ public class ShallowStackTest extends TestCase {
|
|||||||
Request bindingRequest = MessageFactory.createBindingRequest();
|
Request bindingRequest = MessageFactory.createBindingRequest();
|
||||||
|
|
||||||
stunProvider.sendRequest(bindingRequest,
|
stunProvider.sendRequest(bindingRequest,
|
||||||
stun4jAddressOfDummyImpl,
|
dummyServerAddress,
|
||||||
localAddress,
|
localAddress,
|
||||||
collector);
|
collector);
|
||||||
|
|
||||||
//wait for its arrival
|
//wait for its arrival
|
||||||
try{ Thread.sleep(500); }catch (InterruptedException ex){}
|
try{ Thread.sleep(50); }catch (InterruptedException ex){}
|
||||||
|
|
||||||
//create the right response
|
//create the right response
|
||||||
byte response[] = new byte[msgFixture.bindingResponse.length];
|
byte response[] = new byte[msgFixture.bindingResponse.length];
|
||||||
@ -274,12 +277,12 @@ public class ShallowStackTest extends TestCase {
|
|||||||
|
|
||||||
//send the response
|
//send the response
|
||||||
|
|
||||||
dummyImplSocket.send(new DatagramPacket(response,
|
dummyServerSocket.send(new DatagramPacket(response,
|
||||||
response.length,
|
response.length,
|
||||||
socketAddressOfStun4jStack));
|
localAddress));
|
||||||
|
|
||||||
//wait for the packet to arrive
|
//wait for the packet to arrive
|
||||||
try{ Thread.sleep(500); }catch (InterruptedException ex){}
|
try{ Thread.sleep(50); }catch (InterruptedException ex){}
|
||||||
|
|
||||||
Response collectedResponse = collector.collectedResponse;
|
Response collectedResponse = collector.collectedResponse;
|
||||||
|
|
||||||
|
|||||||
@ -60,7 +60,7 @@ public class ResponseSequenceServer
|
|||||||
|
|
||||||
localSocket = new DatagramSocket(serverAddress);
|
localSocket = new DatagramSocket(serverAddress);
|
||||||
|
|
||||||
stunStack.installNetAccessPoint(localSocket);
|
stunStack.addSocket(localSocket);
|
||||||
stunProvider.addRequestListener(serverAddress, this);
|
stunProvider.addRequestListener(serverAddress, this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user