Fix/update/refactor InboundTest

This commit is contained in:
zab
2013-01-07 19:03:30 +00:00
parent ba5005c467
commit 680c31b843
3 changed files with 88 additions and 106 deletions

View File

@@ -6,23 +6,18 @@ import java.util.ArrayList;
import java.util.List;
import net.i2p.data.Hash;
import net.i2p.data.RouterIdentity;
import net.i2p.data.RouterInfo;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DataMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public abstract class GatewayTestBase {
public abstract class GatewayTestBase extends RouterTestBase {
protected static RouterContext _context;
private static TunnelGatewayPumper _pumper;
protected static TunnelCreatorConfig _config;
private TunnelGateway.QueuePreprocessor _preprocessor;
protected TunnelGateway.Sender _sender;
@@ -30,18 +25,8 @@ public abstract class GatewayTestBase {
private TunnelGateway _gw;
@BeforeClass
public static void globalSetUp() {
// order of these matters
Router r = new Router();
_context = new RouterContext(r);
_context.initAll();
r.runRouter();
RouterIdentity rIdentity = new TestRouterIdentity();
RouterInfo rInfo = new RouterInfo();
rInfo.setIdentity(rIdentity);
r.setRouterInfo(rInfo);
public static void gatewayClassSetup() {
_pumper = new TunnelGatewayPumper(_context);
_config = prepareConfig(8);
}
@Before
@@ -149,13 +134,6 @@ public abstract class GatewayTestBase {
}
}
private static class TestRouterIdentity extends RouterIdentity {
@Override
public Hash getHash() {
return Hash.FAKE_HASH;
}
}
private static DataMessage getTestMessage(int size) {
DataMessage m = new DataMessage(_context);
m.setData(new byte[size]);
@@ -207,38 +185,4 @@ public abstract class GatewayTestBase {
return null;
}
}
private static TunnelCreatorConfig prepareConfig(int numHops) {
Hash peers[] = new Hash[numHops];
byte tunnelIds[][] = new byte[numHops][4];
for (int i = 0; i < numHops; i++) {
peers[i] = new Hash();
peers[i].setData(new byte[Hash.HASH_LENGTH]);
_context.random().nextBytes(peers[i].getData());
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);
cfg.setExpiration(_context.clock().now() + 60000);
cfg.setIVKey(_context.keyGenerator().generateSessionKey());
cfg.setLayerKey(_context.keyGenerator().generateSessionKey());
if (i > 0)
cfg.setReceiveFrom(peers[i-1]);
else
cfg.setReceiveFrom(null);
cfg.setReceiveTunnelId(tunnelIds[i]);
if (i < numHops - 1) {
cfg.setSendTo(peers[i+1]);
cfg.setSendTunnelId(tunnelIds[i+1]);
} else {
cfg.setSendTo(null);
cfg.setSendTunnelId(null);
}
}
return config;
}
}

View File

@@ -8,78 +8,42 @@ package net.i2p.router.tunnel;
*
*/
import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import org.junit.Test;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.router.RouterContext;
import static junit.framework.Assert.assertTrue;
/**
* Quick unit test for base functionality of inbound tunnel
* operation
*
*/
public class InboundTest extends TestCase{
private RouterContext _context;
public void setUp() {
_context = new RouterContext(null);
}
public class InboundTest extends RouterTestBase {
@Test
public void testInbound() {
int numHops = 8;
TunnelCreatorConfig config = prepareConfig(numHops);
byte orig[] = new byte[128];
byte message[] = new byte[128];
_context.random().nextBytes(orig); // might as well fill the IV
System.arraycopy(orig, 0, message, 0, message.length);
InboundGatewayProcessor p = new InboundGatewayProcessor(_context, config.getConfig(0));
InboundGatewayProcessor p = new InboundGatewayProcessor(_context, _config.getConfig(0));
p.process(message, 0, message.length, null);
for (int i = 1; i < numHops-1; i++) {
HopProcessor hop = new HopProcessor(_context, config.getConfig(i));
Hash prev = config.getConfig(i).getReceiveFrom();
HopProcessor hop = new HopProcessor(_context, _config.getConfig(i));
Hash prev = _config.getConfig(i).getReceiveFrom();
assertTrue(hop.process(message, 0, message.length, prev));
}
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, config);
assertTrue(end.retrievePreprocessedData(message, 0, message.length, config.getPeer(numHops-2)));
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, _config);
assertTrue(end.retrievePreprocessedData(message, 0, message.length, _config.getPeer(numHops-2)));
assertTrue(DataHelper.eq(orig, 16, message, 16, orig.length - 16));
}
private TunnelCreatorConfig prepareConfig(int numHops) {
Hash peers[] = new Hash[numHops];
byte tunnelIds[][] = new byte[numHops][4];
for (int i = 0; i < numHops; i++) {
peers[i] = new Hash();
peers[i].setData(new byte[Hash.HASH_LENGTH]);
_context.random().nextBytes(peers[i].getData());
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);
cfg.setExpiration(_context.clock().now() + 60000);
cfg.setIVKey(_context.keyGenerator().generateSessionKey());
cfg.setLayerKey(_context.keyGenerator().generateSessionKey());
if (i > 0)
cfg.setReceiveFrom(peers[i-1]);
else
cfg.setReceiveFrom(null);
cfg.setReceiveTunnelId(tunnelIds[i]);
if (i < numHops - 1) {
cfg.setSendTo(peers[i+1]);
cfg.setSendTunnelId(tunnelIds[i+1]);
} else {
cfg.setSendTo(null);
cfg.setSendTunnelId(null);
}
}
return config;
}
}

View File

@@ -0,0 +1,74 @@
package net.i2p.router.tunnel;
import net.i2p.data.Hash;
import net.i2p.data.RouterIdentity;
import net.i2p.data.RouterInfo;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import org.junit.BeforeClass;
/**
* Base class for tests that need a functioning router set up.
*
* @author zab
*/
public abstract class RouterTestBase {
protected static RouterContext _context;
protected static TunnelCreatorConfig _config;
@BeforeClass
public static void routerClassSetup() {
// order of these matters
Router r = new Router();
_context = new RouterContext(r);
_context.initAll();
r.runRouter();
RouterIdentity rIdentity = new TestRouterIdentity();
RouterInfo rInfo = new RouterInfo();
rInfo.setIdentity(rIdentity);
r.setRouterInfo(rInfo);
_config = prepareConfig(8);
}
private static TunnelCreatorConfig prepareConfig(int numHops) {
Hash peers[] = new Hash[numHops];
byte tunnelIds[][] = new byte[numHops][4];
for (int i = 0; i < numHops; i++) {
peers[i] = new Hash();
peers[i].setData(new byte[Hash.HASH_LENGTH]);
_context.random().nextBytes(peers[i].getData());
_context.random().nextBytes(tunnelIds[i]);
}
TunnelCreatorConfig config = new TunnelCreatorConfig(_context, numHops, false);
for (int i = 0; i < numHops; i++) {
config.setPeer(i, peers[i]);
HopConfig cfg = config.getConfig(i);
cfg.setExpiration(_context.clock().now() + 60000);
cfg.setIVKey(_context.keyGenerator().generateSessionKey());
cfg.setLayerKey(_context.keyGenerator().generateSessionKey());
if (i > 0)
cfg.setReceiveFrom(peers[i-1]);
else
cfg.setReceiveFrom(null);
cfg.setReceiveTunnelId(tunnelIds[i]);
if (i < numHops - 1) {
cfg.setSendTo(peers[i+1]);
cfg.setSendTunnelId(tunnelIds[i+1]);
} else {
cfg.setSendTo(null);
cfg.setSendTunnelId(null);
}
}
return config;
}
private static class TestRouterIdentity extends RouterIdentity {
@Override
public Hash getHash() {
return Hash.FAKE_HASH;
}
}
}