Updated JUnit tests in net.i2p.router.tunnel - almost all bugs fixed

This commit is contained in:
str4d
2013-01-02 21:29:54 +00:00
parent d6d1b51970
commit dae66d7f73
6 changed files with 54 additions and 33 deletions

View File

@ -11,6 +11,7 @@ package net.i2p.router.tunnel;
import java.util.ArrayList;
import net.i2p.I2PAppContext;
import net.i2p.router.RouterContext;
/**
* Test the batching behavior of the preprocessor with one, two, or three
@ -24,8 +25,8 @@ public class BatchedFragmentTest extends FragmentTest {
BatchedPreprocessor.DEFAULT_DELAY = 200;
}
protected TunnelGateway.QueuePreprocessor createPreprocessor(I2PAppContext ctx) {
return new BatchedPreprocessor(ctx);
protected TunnelGateway.QueuePreprocessor createPreprocessor(RouterContext ctx) {
return new BatchedPreprocessor(ctx, "testBatchedPreprocessor");
}
/**
@ -35,11 +36,11 @@ public class BatchedFragmentTest extends FragmentTest {
*
*/
public void testBatched() {
TunnelGateway.Pending pending1 = createPending(10, false, false);
PendingGatewayMessage pending1 = createPending(10, false, false);
ArrayList messages = new ArrayList();
messages.add(pending1);
TunnelGateway.Pending pending2 = createPending(1024, false, false);
PendingGatewayMessage pending2 = createPending(1024, false, false);
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
SenderImpl sender = new SenderImpl();
@ -111,14 +112,14 @@ public class BatchedFragmentTest extends FragmentTest {
private void testBatched(int firstSize, boolean firstRouter, boolean firstTunnel,
int secondSize, boolean secondRouter, boolean secondTunnel,
int thirdSize, boolean thirdRouter, boolean thirdTunnel) {
TunnelGateway.Pending pending1 = createPending(firstSize, firstRouter, firstTunnel);
TunnelGateway.Pending pending2 = createPending(secondSize, secondRouter, secondTunnel);
TunnelGateway.Pending pending3 = createPending(thirdSize, thirdRouter, thirdTunnel);
PendingGatewayMessage pending1 = createPending(firstSize, firstRouter, firstTunnel);
PendingGatewayMessage pending2 = createPending(secondSize, secondRouter, secondTunnel);
PendingGatewayMessage pending3 = createPending(thirdSize, thirdRouter, thirdTunnel);
runBatch(pending1, pending2, pending3);
}
private void runBatch(TunnelGateway.Pending pending1, TunnelGateway.Pending pending2, TunnelGateway.Pending pending3) {
private void runBatch(PendingGatewayMessage pending1, PendingGatewayMessage pending2, PendingGatewayMessage pending3) {
ArrayList messages = new ArrayList();
messages.add(pending1);

View File

@ -17,6 +17,7 @@ import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DataMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.RouterContext;
/**
* Simple test to see if the fragmentation is working, testing the preprocessor,
@ -24,15 +25,15 @@ import net.i2p.data.i2np.I2NPMessage;
*
*/
public class FragmentTest extends TestCase{
protected I2PAppContext _context;
protected RouterContext _context;
public void setUp() {
_context = I2PAppContext.getGlobalContext();
_context = (RouterContext) I2PAppContext.getGlobalContext();
_context.random().nextBoolean();
FragmentHandler.MAX_DEFRAGMENT_TIME = 10*1000;
}
protected TunnelGateway.QueuePreprocessor createPreprocessor(I2PAppContext ctx) {
protected TunnelGateway.QueuePreprocessor createPreprocessor(RouterContext ctx) {
return new TrivialPreprocessor(ctx);
}
@ -41,7 +42,7 @@ public class FragmentTest extends TestCase{
*
*/
public void testSingle() {
TunnelGateway.Pending pending = createPending(949, false, false);
PendingGatewayMessage pending = createPending(949, false, false);
ArrayList messages = new ArrayList();
messages.add(pending);
@ -66,7 +67,7 @@ public class FragmentTest extends TestCase{
*
*/
public void testMultiple() {
TunnelGateway.Pending pending = createPending(2048, false, false);
PendingGatewayMessage pending = createPending(2048, false, false);
ArrayList messages = new ArrayList();
messages.add(pending);
@ -92,7 +93,7 @@ public class FragmentTest extends TestCase{
*
*/
public void runDelayed() {
TunnelGateway.Pending pending = createPending(2048, false, false);
PendingGatewayMessage pending = createPending(2048, false, false);
ArrayList messages = new ArrayList();
messages.add(pending);
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
@ -118,7 +119,7 @@ public class FragmentTest extends TestCase{
}
protected boolean runVaried(int size, boolean includeRouter, boolean includeTunnel) {
TunnelGateway.Pending pending = createPending(size, includeRouter, includeTunnel);
PendingGatewayMessage pending = createPending(size, includeRouter, includeTunnel);
ArrayList messages = new ArrayList();
messages.add(pending);
@ -139,7 +140,7 @@ public class FragmentTest extends TestCase{
return handleReceiver.receivedOk();
}
protected TunnelGateway.Pending createPending(int size, boolean includeRouter, boolean includeTunnel) {
protected PendingGatewayMessage createPending(int size, boolean includeRouter, boolean includeTunnel) {
DataMessage m = new DataMessage(_context);
byte data[] = new byte[size];
_context.random().nextBytes(data);
@ -155,11 +156,11 @@ public class FragmentTest extends TestCase{
}
if (includeTunnel)
toTunnel = new TunnelId(_context.random().nextLong(TunnelId.MAX_ID_VALUE));
return new TunnelGateway.Pending(m, toRouter, toTunnel);
return new PendingGatewayMessage(m, toRouter, toTunnel);
}
protected class SenderImpl implements TunnelGateway.Sender {
public void sendPreprocessed(byte[] preprocessed, TunnelGateway.Receiver receiver) {
public long sendPreprocessed(byte[] preprocessed, TunnelGateway.Receiver receiver) {
receiver.receiveEncrypted(preprocessed);
}
}
@ -170,10 +171,15 @@ public class FragmentTest extends TestCase{
_handler = handler;
_delay = delay;
}
public void receiveEncrypted(byte[] encrypted) {
public long receiveEncrypted(byte[] encrypted) {
_handler.receiveTunnelMessage(encrypted, 0, encrypted.length);
try { Thread.sleep(_delay); } catch (Exception e) {}
}
@Override
public Hash getSendTo() {
// TODO Auto-generated method stub
return null;
}
}
protected class DefragmentedReceiverImpl implements FragmentHandler.DefragmentedReceiver {

View File

@ -17,6 +17,7 @@ import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DataMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
/**
@ -24,7 +25,7 @@ import net.i2p.util.Log;
* operation
*/
public class InboundGatewayTest extends TestCase{
private I2PAppContext _context;
private RouterContext _context;
private Log _log;
private TunnelCreatorConfig _config;
private TunnelGateway.QueuePreprocessor _preprocessor;
@ -33,7 +34,7 @@ public class InboundGatewayTest extends TestCase{
private TunnelGateway _gw;
public void setUp() {
_context = I2PAppContext.getGlobalContext();
_context = (RouterContext) I2PAppContext.getGlobalContext();
_config = prepareConfig(8);
_preprocessor = new TrivialPreprocessor(_context);
_sender = new InboundSender(_context, _config.getConfig(0));
@ -156,7 +157,7 @@ public class InboundGatewayTest extends TestCase{
_handler = new FragmentHandler(_context, TestReceiver.this);
_received = new ArrayList(1000);
}
public void receiveEncrypted(byte[] encrypted) {
public long receiveEncrypted(byte[] encrypted) {
// fake all the hops...
for (int i = 1; i <= _config.getLength() - 2; i++) {
@ -179,6 +180,11 @@ public class InboundGatewayTest extends TestCase{
_received = new ArrayList();
return rv;
}
@Override
public Hash getSendTo() {
// TODO Auto-generated method stub
return null;
}
}
private TunnelCreatorConfig prepareConfig(int numHops) {
@ -191,7 +197,7 @@ public class InboundGatewayTest extends TestCase{
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(numHops, false);
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);

View File

@ -12,6 +12,7 @@ import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.router.RouterContext;
/**
* Quick unit test for base functionality of inbound tunnel
@ -19,10 +20,10 @@ import net.i2p.data.Hash;
*
*/
public class InboundTest extends TestCase{
private I2PAppContext _context;
private RouterContext _context;
public void setUp() {
_context = I2PAppContext.getGlobalContext();
_context = (RouterContext) I2PAppContext.getGlobalContext();
}
public void testInbound() {
@ -59,7 +60,7 @@ public class InboundTest extends TestCase{
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(numHops, false);
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);

View File

@ -17,13 +17,14 @@ import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DataMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.RouterContext;
/**
* Quick unit test for base functionality of outbound tunnel
* operation
*/
public class OutboundGatewayTest extends TestCase{
private I2PAppContext _context;
private RouterContext _context;
private TunnelCreatorConfig _config;
private TunnelGateway.QueuePreprocessor _preprocessor;
private TunnelGateway.Sender _sender;
@ -31,7 +32,7 @@ public class OutboundGatewayTest extends TestCase{
private TunnelGateway _gw;
public void setUp() {
_context = I2PAppContext.getGlobalContext();
_context = (RouterContext) I2PAppContext.getGlobalContext();
_config = prepareConfig(8);
_preprocessor = new TrivialPreprocessor(_context);
_sender = new OutboundSender(_context, _config);
@ -154,7 +155,7 @@ public class OutboundGatewayTest extends TestCase{
_handler = new FragmentHandler(_context, TestReceiver.this);
_received = new ArrayList(1000);
}
public void receiveEncrypted(byte[] encrypted) {
public long receiveEncrypted(byte[] encrypted) {
// fake all the hops...
for (int i = 1; i < _config.getLength(); i++) {
@ -174,6 +175,11 @@ public class OutboundGatewayTest extends TestCase{
_received = new ArrayList();
return rv;
}
@Override
public Hash getSendTo() {
// TODO Auto-generated method stub
return null;
}
}
private TunnelCreatorConfig prepareConfig(int numHops) {
@ -186,7 +192,7 @@ public class OutboundGatewayTest extends TestCase{
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(numHops, false);
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);

View File

@ -12,6 +12,7 @@ import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.router.RouterContext;
/**
* Quick unit test for base functionality of outbound tunnel
@ -19,10 +20,10 @@ import net.i2p.data.Hash;
*
*/
public class OutboundTest extends TestCase{
private I2PAppContext _context;
private RouterContext _context;
public void setUp() {
_context = I2PAppContext.getGlobalContext();
_context = (RouterContext) I2PAppContext.getGlobalContext();
}
public void testOutbound() {
@ -56,7 +57,7 @@ public class OutboundTest extends TestCase{
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(numHops, false);
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);