forked from I2P_Developers/i2p.i2p
Fix/update/refactor InboundTest
This commit is contained in:
@@ -6,23 +6,18 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
import net.i2p.data.RouterIdentity;
|
|
||||||
import net.i2p.data.RouterInfo;
|
|
||||||
import net.i2p.data.TunnelId;
|
import net.i2p.data.TunnelId;
|
||||||
import net.i2p.data.i2np.DataMessage;
|
import net.i2p.data.i2np.DataMessage;
|
||||||
import net.i2p.data.i2np.I2NPMessage;
|
import net.i2p.data.i2np.I2NPMessage;
|
||||||
import net.i2p.router.Router;
|
|
||||||
import net.i2p.router.RouterContext;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public abstract class GatewayTestBase {
|
public abstract class GatewayTestBase extends RouterTestBase {
|
||||||
|
|
||||||
protected static RouterContext _context;
|
|
||||||
private static TunnelGatewayPumper _pumper;
|
private static TunnelGatewayPumper _pumper;
|
||||||
protected static TunnelCreatorConfig _config;
|
|
||||||
|
|
||||||
private TunnelGateway.QueuePreprocessor _preprocessor;
|
private TunnelGateway.QueuePreprocessor _preprocessor;
|
||||||
protected TunnelGateway.Sender _sender;
|
protected TunnelGateway.Sender _sender;
|
||||||
@@ -30,18 +25,8 @@ public abstract class GatewayTestBase {
|
|||||||
private TunnelGateway _gw;
|
private TunnelGateway _gw;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void globalSetUp() {
|
public static void gatewayClassSetup() {
|
||||||
// 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);
|
|
||||||
_pumper = new TunnelGatewayPumper(_context);
|
_pumper = new TunnelGatewayPumper(_context);
|
||||||
_config = prepareConfig(8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@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) {
|
private static DataMessage getTestMessage(int size) {
|
||||||
DataMessage m = new DataMessage(_context);
|
DataMessage m = new DataMessage(_context);
|
||||||
m.setData(new byte[size]);
|
m.setData(new byte[size]);
|
||||||
@@ -207,38 +185,4 @@ public abstract class GatewayTestBase {
|
|||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -8,78 +8,42 @@ package net.i2p.router.tunnel;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import org.junit.Test;
|
||||||
import net.i2p.I2PAppContext;
|
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Hash;
|
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
|
* Quick unit test for base functionality of inbound tunnel
|
||||||
* operation
|
* operation
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class InboundTest extends TestCase{
|
public class InboundTest extends RouterTestBase {
|
||||||
private RouterContext _context;
|
|
||||||
|
|
||||||
public void setUp() {
|
|
||||||
_context = new RouterContext(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testInbound() {
|
public void testInbound() {
|
||||||
int numHops = 8;
|
int numHops = 8;
|
||||||
TunnelCreatorConfig config = prepareConfig(numHops);
|
|
||||||
|
|
||||||
byte orig[] = new byte[128];
|
byte orig[] = new byte[128];
|
||||||
byte message[] = new byte[128];
|
byte message[] = new byte[128];
|
||||||
_context.random().nextBytes(orig); // might as well fill the IV
|
_context.random().nextBytes(orig); // might as well fill the IV
|
||||||
System.arraycopy(orig, 0, message, 0, message.length);
|
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);
|
p.process(message, 0, message.length, null);
|
||||||
|
|
||||||
for (int i = 1; i < numHops-1; i++) {
|
for (int i = 1; i < numHops-1; i++) {
|
||||||
HopProcessor hop = new HopProcessor(_context, config.getConfig(i));
|
HopProcessor hop = new HopProcessor(_context, _config.getConfig(i));
|
||||||
Hash prev = config.getConfig(i).getReceiveFrom();
|
Hash prev = _config.getConfig(i).getReceiveFrom();
|
||||||
assertTrue(hop.process(message, 0, message.length, prev));
|
assertTrue(hop.process(message, 0, message.length, prev));
|
||||||
}
|
}
|
||||||
|
|
||||||
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, config);
|
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, _config);
|
||||||
assertTrue(end.retrievePreprocessedData(message, 0, message.length, config.getPeer(numHops-2)));
|
assertTrue(end.retrievePreprocessedData(message, 0, message.length, _config.getPeer(numHops-2)));
|
||||||
|
|
||||||
assertTrue(DataHelper.eq(orig, 16, message, 16, orig.length - 16));
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user