2005-09-12 comwiz
* Migrated the router tests to junit
This commit is contained in:
11
build.xml
11
build.xml
@ -324,13 +324,12 @@
|
||||
<ant dir="core/java/" target="clover.report" />
|
||||
</target>
|
||||
<target name="test.report" depends="junit.report, clover.report"/>
|
||||
<target name="useclover">
|
||||
<taskdef resource="clovertasks"/>
|
||||
<mkdir dir="reports/core/clover" />
|
||||
<mkdir dir="reports/core/clover" />
|
||||
<clover-setup initString="reports/core/clover/coverage.db"/>
|
||||
</target>
|
||||
<target name="fulltest">
|
||||
<ant dir="core/java/" target="fulltest" />
|
||||
<ant dir="router/java/" target="fulltest" />
|
||||
</target>
|
||||
<target name="fullclovertest">
|
||||
<ant dir="core/java/" target="fullclovertest" />
|
||||
<ant dir="router/java/" target="fullclovertest" />
|
||||
</target>
|
||||
</project>
|
||||
|
@ -13,7 +13,7 @@
|
||||
<target name="compileTest">
|
||||
<mkdir dir="./build" />
|
||||
<mkdir dir="./build/obj" />
|
||||
<javac srcdir="./src:./test" debug="true" source="1.3" target="1.3" deprecation="on" destdir="./build/obj" classpath="./lib/junit.jar" />
|
||||
<javac srcdir="./src:./test" debug="true" source="1.3" target="1.3" deprecation="on" destdir="./build/obj" />
|
||||
</target>
|
||||
<target name="jar" depends="compile">
|
||||
<jar destfile="./build/i2p.jar" basedir="./build/obj" includes="**/*.class" />
|
||||
@ -31,7 +31,6 @@
|
||||
<classpath>
|
||||
<pathelement path="${classpath}" />
|
||||
<pathelement location="./build/obj" />
|
||||
<pathelement location="./lib/junit.jar" />
|
||||
<pathelement location="../../installer/lib/jbigi/jbigi.jar" />
|
||||
<pathelement path="${ant.home}/lib/clover.jar"/>
|
||||
</classpath>
|
||||
@ -91,7 +90,8 @@
|
||||
<mkdir dir="../../reports/core/clover" />
|
||||
<clover-setup initString="../../reports/core/clover/coverage.db"/>
|
||||
</target>
|
||||
<target name="fulltest" depends="useclover, test, test.report" />
|
||||
<target name="fulltest" depends="test, junit.report" />
|
||||
<target name="fullclovertest" depends="useclover, test, test.report" />
|
||||
<target name="clean">
|
||||
<delete dir="./build" />
|
||||
</target>
|
||||
|
@ -1,4 +1,7 @@
|
||||
$Id: history.txt,v 1.246 2005/09/12 20:12:44 jrandom Exp $
|
||||
$Id: history.txt,v 1.247 2005/09/12 22:32:30 jrandom Exp $
|
||||
|
||||
2005-09-12 comwiz
|
||||
* Migrated the router tests to junit
|
||||
|
||||
2005-09-12 jrandom
|
||||
* Removed guaranteed delivery mode entirely (so existing i2phex clients
|
||||
|
@ -5,14 +5,25 @@
|
||||
<target name="builddep">
|
||||
<ant dir="../../core/java/" target="build" />
|
||||
</target>
|
||||
<target name="builddeptest">
|
||||
<ant dir="../../core/java/" target="jarTest" />
|
||||
</target>
|
||||
<target name="compile">
|
||||
<mkdir dir="./build" />
|
||||
<mkdir dir="./build/obj" />
|
||||
<javac srcdir="./src" debug="true" source="1.3" target="1.3" deprecation="on" destdir="./build/obj" classpath="../../core/java/build/i2p.jar" />
|
||||
</target>
|
||||
<target name="compileTest" depends="builddeptest">
|
||||
<mkdir dir="./build" />
|
||||
<mkdir dir="./build/obj" />
|
||||
<javac srcdir="./src:./test" debug="true" source="1.3" target="1.3" deprecation="on" destdir="./build/obj" classpath="../../core/java/build/i2ptest.jar" />
|
||||
</target>
|
||||
<target name="jar" depends="compile">
|
||||
<jar destfile="./build/router.jar" basedir="./build/obj" includes="**/*.class" />
|
||||
</target>
|
||||
<target name="jarTest" depends="compileTest">
|
||||
<jar destfile="./build/routertest.jar" basedir="./build/obj" includes="**/*.class" />
|
||||
</target>
|
||||
<target name="javadoc">
|
||||
<mkdir dir="./build" />
|
||||
<mkdir dir="./build/javadoc" />
|
||||
@ -23,6 +34,71 @@
|
||||
splitindex="true"
|
||||
windowtitle="I2P Router" />
|
||||
</target>
|
||||
<target name="test" depends="compileTest">
|
||||
<junit printsummary="on" fork="yes">
|
||||
<classpath>
|
||||
<pathelement path="${classpath}" />
|
||||
<pathelement location="./build/obj" />
|
||||
<pathelement location="../../core/java/build/i2ptest.jar" />
|
||||
<pathelement location="../../installer/lib/jbigi/jbigi.jar" />
|
||||
<pathelement path="${ant.home}/lib/clover.jar"/>
|
||||
</classpath>
|
||||
<batchtest>
|
||||
<fileset dir="./test/">
|
||||
<include name="**/*Test.java" />
|
||||
</fileset>
|
||||
</batchtest>
|
||||
<formatter type="xml"/>
|
||||
</junit>
|
||||
<mkdir dir="../../reports/" />
|
||||
<mkdir dir="../../reports/router/" />
|
||||
<mkdir dir="../../reports/router/junit/" />
|
||||
<delete>
|
||||
<fileset dir="../../reports/router/junit">
|
||||
<include name="TEST-*.xml"/>
|
||||
</fileset>
|
||||
</delete>
|
||||
<copy todir="../../reports/router/junit">
|
||||
<fileset dir=".">
|
||||
<include name="TEST-*.xml"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
<delete>
|
||||
<fileset dir=".">
|
||||
<include name="TEST-*.xml"/>
|
||||
</fileset>
|
||||
</delete>
|
||||
</target>
|
||||
<target name="junit.report">
|
||||
<junitreport todir="../../reports/router/junit">
|
||||
<fileset dir="../../reports/router/junit">
|
||||
<include name="TEST-*.xml"/>
|
||||
</fileset>
|
||||
<report format="frames" todir="../../reports/router/html/junit"/>
|
||||
</junitreport>
|
||||
</target>
|
||||
<target name="clover.report">
|
||||
<taskdef resource="clovertasks"/>
|
||||
<mkdir dir="../../reports/" />
|
||||
<mkdir dir="../../reports/router" />
|
||||
<mkdir dir="../../reports/router/clover" />
|
||||
<clover-setup initString="../../reports/router/clover/coverage.db"/>
|
||||
<clover-report>
|
||||
<current outfile="../../reports/router/html/clover">
|
||||
<format type="html"/>
|
||||
</current>
|
||||
</clover-report>
|
||||
</target>
|
||||
<target name="test.report" depends="junit.report, clover.report"/>
|
||||
<target name="useclover">
|
||||
<taskdef resource="clovertasks"/>
|
||||
<mkdir dir="../../reports/" />
|
||||
<mkdir dir="../../reports/router/" />
|
||||
<mkdir dir="../../reports/router/clover" />
|
||||
<clover-setup initString="../../reports/router/clover/coverage.db"/>
|
||||
</target>
|
||||
<target name="fulltest" depends="cleandep, builddeptest, jarTest, test, junit.report" />
|
||||
<target name="fullclovertest" depends="cleandep, builddeptest, useclover, jarTest, test, test.report" />
|
||||
<target name="clean">
|
||||
<delete dir="./build" />
|
||||
</target>
|
||||
|
@ -144,7 +144,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
||||
if (size < 15 + CHECKSUM_LENGTH) throw new DataFormatException("Unable to build the message");
|
||||
byte buf[] = new byte[size];
|
||||
int read = toByteArray(buf);
|
||||
if (read < 0)
|
||||
if (read < 0) throw new DataFormatException("Unable to build the message");
|
||||
out.write(buf, 0, read);
|
||||
}
|
||||
|
||||
|
@ -1,184 +0,0 @@
|
||||
package net.i2p.router.tunnel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.Base64;
|
||||
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.util.Log;
|
||||
|
||||
/**
|
||||
* Test the batching behavior of the preprocessor with one, two, or three
|
||||
* messages of various sizes and settings.
|
||||
*
|
||||
*/
|
||||
public class BatchedFragmentTest extends FragmentTest {
|
||||
|
||||
public BatchedFragmentTest() {
|
||||
super();
|
||||
BatchedPreprocessor.DEFAULT_DELAY = 200;
|
||||
}
|
||||
|
||||
protected TunnelGateway.QueuePreprocessor createPreprocessor(I2PAppContext ctx) {
|
||||
return new BatchedPreprocessor(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a small message, wait a second, then send a large message, pushing
|
||||
* the first one through immediately, with the rest of the large one passed
|
||||
* after a brief delay.
|
||||
*
|
||||
*/
|
||||
public void runBatched() {
|
||||
TunnelGateway.Pending pending1 = createPending(10, false, false);
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending1);
|
||||
|
||||
TunnelGateway.Pending pending2 = createPending(1024, false, false);
|
||||
|
||||
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
|
||||
SenderImpl sender = new SenderImpl();
|
||||
DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending1.getData(), pending2.getData());
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending1.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
boolean keepGoing = true;
|
||||
boolean alreadyAdded = false;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
if (keepGoing) {
|
||||
try { Thread.sleep(150); } catch (InterruptedException ie) {}
|
||||
|
||||
if (!alreadyAdded) {
|
||||
messages.add(pending2);
|
||||
alreadyAdded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (handleReceiver.receivedOk())
|
||||
_log.info("Receive batched ok");
|
||||
else
|
||||
_log.info("Failed to receive batched");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a small message, wait a second, then send a large message, pushing
|
||||
* the first one through immediately, with the rest of the large one passed
|
||||
* after a brief delay.
|
||||
*
|
||||
*/
|
||||
public void runBatches() {
|
||||
int success = 0;
|
||||
//success += testBatched(1, false, false, 1024, false, false);
|
||||
// this takes a long fucking time
|
||||
for (int i = 1; i <= 1024; i++) {
|
||||
success += testBatched(i, false, false, 1024, false, false, 1024, false, false);
|
||||
success += testBatched(i, true, false, 1024, false, false, 1024, false, false);
|
||||
success += testBatched(i, true, true, 1024, false, false, 1024, false, false);
|
||||
success += testBatched(i, false, false, 1024, true, false, 1024, false, false);
|
||||
success += testBatched(i, true, false, 1024, true, false, 1024, false, false);
|
||||
success += testBatched(i, true, true, 1024, true, false, 1024, false, false);
|
||||
success += testBatched(i, false, false, 1024, true, true, 1024, false, false);
|
||||
success += testBatched(i, true, false, 1024, true, true, 1024, false, false);
|
||||
success += testBatched(i, true, true, 1024, true, true, 1024, false, false);
|
||||
|
||||
success += testBatched(i, false, false, 1024, false, false, 1024, true, false);
|
||||
success += testBatched(i, true, false, 1024, false, false, 1024, true, false);
|
||||
success += testBatched(i, true, true, 1024, false, false, 1024, true, false);
|
||||
success += testBatched(i, false, false, 1024, true, false, 1024, true, false);
|
||||
success += testBatched(i, true, false, 1024, true, false, 1024, true, false);
|
||||
success += testBatched(i, true, true, 1024, true, false, 1024, true, false);
|
||||
success += testBatched(i, false, false, 1024, true, true, 1024, true, false);
|
||||
success += testBatched(i, true, false, 1024, true, true, 1024, true, false);
|
||||
success += testBatched(i, true, true, 1024, true, true, 1024, true, false);
|
||||
|
||||
success += testBatched(i, false, false, 1024, false, false, 1024, true, true);
|
||||
success += testBatched(i, true, false, 1024, false, false, 1024, true, true);
|
||||
success += testBatched(i, true, true, 1024, false, false, 1024, true, true);
|
||||
success += testBatched(i, false, false, 1024, true, false, 1024, true, true);
|
||||
success += testBatched(i, true, false, 1024, true, false, 1024, true, true);
|
||||
success += testBatched(i, true, true, 1024, true, false, 1024, true, true);
|
||||
success += testBatched(i, false, false, 1024, true, true, 1024, true, true);
|
||||
success += testBatched(i, true, false, 1024, true, true, 1024, true, true);
|
||||
success += testBatched(i, true, true, 1024, true, true, 1024, true, true);
|
||||
}
|
||||
|
||||
_log.info("** Batches complete with " + success + " successful runs");
|
||||
}
|
||||
|
||||
private int 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);
|
||||
|
||||
boolean ok = runBatch(pending1, pending2, pending3);
|
||||
if (ok) {
|
||||
_log.info("OK: " + firstSize + "." + firstRouter + "." + firstTunnel
|
||||
+ " " + secondSize + "." + secondRouter + "." + secondTunnel
|
||||
+ " " + thirdSize + "." + thirdRouter + "." + thirdTunnel);
|
||||
return 1;
|
||||
} else {
|
||||
_log.info("FAIL: " + firstSize + "." + firstRouter + "." + firstTunnel
|
||||
+ " " + secondSize + "." + secondRouter + "." + secondTunnel
|
||||
+ " " + thirdSize + "." + thirdRouter + "." + thirdTunnel);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean runBatch(TunnelGateway.Pending pending1, TunnelGateway.Pending pending2, TunnelGateway.Pending pending3) {
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending1);
|
||||
|
||||
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
|
||||
SenderImpl sender = new SenderImpl();
|
||||
DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending1.getData(), pending2.getData(), pending3.getData());
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending1.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
boolean keepGoing = true;
|
||||
int added = 0;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
if ( (keepGoing) || ((messages.size() == 0) && (added < 2) ) ) {
|
||||
try { Thread.sleep(150); } catch (InterruptedException ie) {}
|
||||
|
||||
if (added == 0) {
|
||||
_log.debug("Adding pending2");
|
||||
messages.add(pending2);
|
||||
added++;
|
||||
keepGoing = true;
|
||||
} else if (added == 1) {
|
||||
_log.debug("Adding pending3");
|
||||
messages.add(pending3);
|
||||
added++;
|
||||
keepGoing = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return handleReceiver.receivedOk();
|
||||
}
|
||||
|
||||
|
||||
public void runTests() {
|
||||
//super.runVaried();
|
||||
//super.runTests();
|
||||
//runBatched();
|
||||
runBatches();
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
BatchedFragmentTest t = new BatchedFragmentTest();
|
||||
t.runTests();
|
||||
}
|
||||
}
|
@ -10,10 +10,10 @@ package net.i2p.data.i2np;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataFormatException;
|
||||
import net.i2p.data.DataStructure;
|
||||
import net.i2p.data.StructureTest;
|
||||
import net.i2p.data.TestData;
|
||||
import net.i2p.data.RouterInfo;
|
||||
import net.i2p.data.RouterInfoTest;
|
||||
import net.i2p.data.i2np.DatabaseStoreMessage;
|
||||
@ -24,20 +24,18 @@ import net.i2p.util.Clock;
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class DatabaseStoreMessageTest extends StructureTest {
|
||||
static {
|
||||
TestData.registerTest(new DatabaseStoreMessageTest(), "DatabaseStoreMessage");
|
||||
}
|
||||
public class DatabaseStoreMessageTest extends StructureTest {
|
||||
public DataStructure createDataStructure() throws DataFormatException {
|
||||
DatabaseStoreMessage msg = new DatabaseStoreMessage(_context);
|
||||
DatabaseStoreMessage msg = new DatabaseStoreMessage(I2PAppContext.getGlobalContext());
|
||||
RouterInfo info = (RouterInfo)new RouterInfoTest().createDataStructure();
|
||||
msg.setKey(info.getIdentity().getHash());
|
||||
msg.setMessageExpiration(Clock.getInstance().now());
|
||||
msg.setUniqueId(42);
|
||||
msg.setUniqueId(666);
|
||||
msg.setRouterInfo(info);
|
||||
return msg;
|
||||
}
|
||||
public DataStructure createStructureToRead() { return new DatabaseStoreMessage(_context); }
|
||||
|
||||
public static void main(String args[]) { TestData.main(new String[] { "test", "i2np.DatabaseStoreMessage", "foo.dat" }); }
|
||||
public DataStructure createStructureToRead() {
|
||||
return new DatabaseStoreMessage(I2PAppContext.getGlobalContext());
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import net.i2p.data.DataStructure;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.data.SessionKey;
|
||||
import net.i2p.data.StructureTest;
|
||||
import net.i2p.data.TestData;
|
||||
import net.i2p.data.TunnelId;
|
||||
import net.i2p.data.i2np.DeliveryInstructions;
|
||||
import net.i2p.util.Log;
|
||||
@ -23,34 +22,30 @@ import net.i2p.util.Log;
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class DeliveryInstructionsTest extends StructureTest {
|
||||
private final static Log _log = new Log(DeliveryInstructionsTest.class);
|
||||
static {
|
||||
TestData.registerTest(new DeliveryInstructionsTest(), "DeliveryInstructions");
|
||||
}
|
||||
public class DeliveryInstructionsTest extends StructureTest {
|
||||
public DataStructure createDataStructure() throws DataFormatException {
|
||||
DeliveryInstructions instructions = new DeliveryInstructions();
|
||||
instructions.setDelayRequested(true);
|
||||
instructions.setDelaySeconds(42);
|
||||
instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_TUNNEL);
|
||||
instructions.setEncrypted(true);
|
||||
SessionKey key = new SessionKey();
|
||||
byte keyData[] = new byte[SessionKey.KEYSIZE_BYTES];
|
||||
for (int i = 0; i < keyData.length; i++)
|
||||
keyData[i] = (byte)i;
|
||||
key.setData(keyData);
|
||||
instructions.setEncryptionKey(key);
|
||||
Hash hash = new Hash();
|
||||
byte hashData[] = new byte[32];
|
||||
for (int i = 0; i < hashData.length; i++)
|
||||
hashData[i] = (byte)(i%32);
|
||||
hash.setData(hashData);
|
||||
instructions.setRouter(hash);
|
||||
TunnelId id = new TunnelId();
|
||||
id.setTunnelId(666);
|
||||
instructions.setTunnelId(id);
|
||||
_log.debug("Instructions created: " + instructions + "\nBase 64: " + instructions.toBase64());
|
||||
return instructions;
|
||||
instructions.setDelayRequested(true);
|
||||
instructions.setDelaySeconds(42);
|
||||
instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_TUNNEL);
|
||||
instructions.setEncrypted(true);
|
||||
SessionKey key = new SessionKey();
|
||||
byte keyData[] = new byte[SessionKey.KEYSIZE_BYTES];
|
||||
for (int i = 0; i < keyData.length; i++)
|
||||
keyData[i] = (byte)i;
|
||||
key.setData(keyData);
|
||||
instructions.setEncryptionKey(key);
|
||||
Hash hash = new Hash();
|
||||
byte hashData[] = new byte[32];
|
||||
for (int i = 0; i < hashData.length; i++)
|
||||
hashData[i] = (byte)(i%32);
|
||||
hash.setData(hashData);
|
||||
instructions.setRouter(hash);
|
||||
TunnelId id = new TunnelId();
|
||||
id.setTunnelId(666);
|
||||
instructions.setTunnelId(id);
|
||||
|
||||
return instructions;
|
||||
}
|
||||
public DataStructure createStructureToRead() { return new DeliveryInstructions(); }
|
||||
}
|
||||
|
@ -17,64 +17,46 @@ import net.i2p.data.DataFormatException;
|
||||
import net.i2p.data.i2np.DatabaseStoreMessage;
|
||||
import net.i2p.data.i2np.I2NPMessage;
|
||||
import net.i2p.data.i2np.I2NPMessageReader;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test harness for loading / storing I2NP DatabaseStore message objects
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class I2NPMessageReaderTest implements I2NPMessageReader.I2NPMessageEventListener {
|
||||
private final static Log _log = new Log(I2NPMessageReaderTest.class);
|
||||
private static RouterContext _context = new RouterContext(null);
|
||||
public class I2NPMessageReaderTest extends TestCase implements I2NPMessageReader.I2NPMessageEventListener{
|
||||
|
||||
public static void main(String args[]) {
|
||||
I2NPMessageReaderTest test = new I2NPMessageReaderTest();
|
||||
test.runTest();
|
||||
try { Thread.sleep(30*1000); } catch (InterruptedException ie) {}
|
||||
}
|
||||
public void setUp(){}
|
||||
|
||||
public void runTest() {
|
||||
public void testI2NPMessageReader() throws IOException, DataFormatException{
|
||||
InputStream data = getData();
|
||||
test(data);
|
||||
}
|
||||
|
||||
private InputStream getData() {
|
||||
private InputStream getData() throws IOException, DataFormatException{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
DatabaseStoreMessage msg = (DatabaseStoreMessage)new DatabaseStoreMessageTest().createDataStructure();
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
_log.debug("DB Store message in tunnel contains: " + msg);
|
||||
msg.writeBytes(baos);
|
||||
} catch (DataFormatException dfe) {
|
||||
_log.error("Error building data", dfe);
|
||||
} catch (IOException ioe) {
|
||||
_log.error("Error writing stream", ioe);
|
||||
}
|
||||
DatabaseStoreMessage msg = (DatabaseStoreMessage)new DatabaseStoreMessageTest().createDataStructure();
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
return new ByteArrayInputStream(baos.toByteArray());
|
||||
}
|
||||
|
||||
private void test(InputStream in) {
|
||||
_log.debug("Testing the input stream");
|
||||
I2NPMessageReader reader = new I2NPMessageReader(_context, in, this);
|
||||
_log.debug("Created, beginning reading");
|
||||
I2NPMessageReader reader = new I2NPMessageReader(new RouterContext(null), in, this);
|
||||
reader.startReading();
|
||||
_log.debug("Reading commenced");
|
||||
}
|
||||
|
||||
public void disconnected(I2NPMessageReader reader) {
|
||||
_log.debug("Disconnected");
|
||||
}
|
||||
|
||||
public void messageReceived(I2NPMessageReader reader, I2NPMessage message, long msToRead, int size) {
|
||||
_log.debug("Message received: " + message);
|
||||
}
|
||||
|
||||
public void readError(I2NPMessageReader reader, Exception error) {
|
||||
_log.debug("Read error: " + error.getMessage(), error);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,79 @@
|
||||
package net.i2p.router.networkdb.kademlia;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.util.RandomSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test KBucketImpl
|
||||
*
|
||||
* @author comwiz
|
||||
*/
|
||||
|
||||
public class KBucketImplTest extends TestCase{
|
||||
private I2PAppContext context;
|
||||
|
||||
public void setUp(){
|
||||
context = I2PAppContext.getGlobalContext();
|
||||
}
|
||||
|
||||
public void testLimits() {
|
||||
int low = 0;
|
||||
int high = 4;
|
||||
|
||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), Hash.FAKE_HASH);
|
||||
bucket.setRange(low, high);
|
||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
||||
assertTrue(bucket.shouldContain(lowerBoundKey));//
|
||||
assertTrue(bucket.shouldContain(upperBoundKey));
|
||||
|
||||
}
|
||||
|
||||
public void testRand() {
|
||||
int low = 1;
|
||||
int high = 2000;
|
||||
|
||||
Hash local = Hash.FAKE_HASH;
|
||||
local.prepareCache();
|
||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), local);
|
||||
bucket.setRange(low, high);
|
||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Hash rnd = bucket.generateRandomKey();
|
||||
assertTrue(bucket.shouldContain(rnd));//
|
||||
}
|
||||
}
|
||||
|
||||
public void testRand2() {
|
||||
int low = 1;
|
||||
int high = 2000;
|
||||
|
||||
byte hash[] = new byte[Hash.HASH_LENGTH];
|
||||
RandomSource.getInstance().nextBytes(hash);
|
||||
Hash local = new Hash(hash);
|
||||
local.prepareCache();
|
||||
KBucketImpl bucket = new KBucketImpl(I2PAppContext.getGlobalContext(), local);
|
||||
bucket.setRange(low, high);
|
||||
Hash lowerBoundKey = bucket.getRangeBeginKey();
|
||||
Hash upperBoundKey = bucket.getRangeEndKey();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Hash rnd = bucket.generateRandomKey();
|
||||
assertTrue(bucket.shouldContain(rnd));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package net.i2p.router.networkdb.kademlia;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Hash;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test KBucketSet
|
||||
*
|
||||
* @author comwiz
|
||||
*/
|
||||
|
||||
public class KBucketSetTest extends TestCase{
|
||||
private I2PAppContext context;
|
||||
private KBucketSet set;
|
||||
|
||||
public void setUp(){
|
||||
context = I2PAppContext.getGlobalContext();
|
||||
set = new KBucketSet(context, Hash.FAKE_HASH);
|
||||
}
|
||||
|
||||
public void testRandom(){
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
byte val[] = new byte[Hash.HASH_LENGTH];
|
||||
context.random().nextBytes(val);
|
||||
assertTrue(set.add(new Hash(val)));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSelf() {
|
||||
assertTrue(set.add(Hash.FAKE_HASH));
|
||||
}
|
||||
}
|
@ -1,13 +1,25 @@
|
||||
package net.i2p.router.transport;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.router.RouterContext;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Stress out the bandwidth limiter by running a series of push and pull tests
|
||||
* through bandwidth limited streams. This includes pushing data through
|
||||
@ -19,19 +31,19 @@ import java.util.Properties;
|
||||
* 10 concurrent threads is, well, slow.
|
||||
*
|
||||
*/
|
||||
public class BandwidthLimiterTest {
|
||||
public class BandwidthLimiterTest extends TestCase{
|
||||
private RouterContext _context;
|
||||
private Log _log;
|
||||
private final static int NUM_MB = 1;
|
||||
private final static int NUM_KB = 256;
|
||||
|
||||
public BandwidthLimiterTest() {
|
||||
public void setUp() {
|
||||
_context = new RouterContext(null);
|
||||
_log = _context.logManager().getLog(BandwidthLimiterTest.class);
|
||||
//_context.jobQueue().runQueue(1);
|
||||
}
|
||||
|
||||
public void prepareLimiter(int inKBps, int outKBps, int inBurst, int outBurst) {
|
||||
Properties props = new Properties();
|
||||
public void tearDown(){
|
||||
}
|
||||
|
||||
private void prepareLimiter(int inKBps, int outKBps, int inBurst, int outBurst) {
|
||||
Properties props = System.getProperties();
|
||||
props.setProperty(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, ""+inKBps);
|
||||
props.setProperty(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, ""+outKBps);
|
||||
props.setProperty(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH_PEAK, ""+inBurst);
|
||||
@ -39,7 +51,6 @@ public class BandwidthLimiterTest {
|
||||
//props.setProperty(TrivialBandwidthLimiter.PROP_REPLENISH_FREQUENCY, ""+10*1000);
|
||||
System.setProperties(props);
|
||||
_context.bandwidthLimiter().reinitialize();
|
||||
_log.debug("Limiter prepared");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,7 +59,7 @@ public class BandwidthLimiterTest {
|
||||
* chunks.
|
||||
*
|
||||
*/
|
||||
public long testOutboundThrottle(int numBytes, int numBytesPerWrite) {
|
||||
private long testOutboundThrottle(int numBytes, int numBytesPerWrite) {
|
||||
byte source[] = new byte[numBytesPerWrite];
|
||||
new Random().nextBytes(source);
|
||||
NullOutputStream target = new NullOutputStream();
|
||||
@ -73,7 +84,7 @@ public class BandwidthLimiterTest {
|
||||
* chunks.
|
||||
*
|
||||
*/
|
||||
public long testInboundThrottle(int numBytes, int numBytesPerRead) {
|
||||
private long testInboundThrottle(int numBytes, int numBytesPerRead) {
|
||||
FakeInputStream source = new FakeInputStream(numBytes);
|
||||
BandwidthLimitedInputStream in = new BandwidthLimitedInputStream(_context, source, null);
|
||||
long before = System.currentTimeMillis();
|
||||
@ -95,20 +106,33 @@ public class BandwidthLimiterTest {
|
||||
*
|
||||
*/
|
||||
public void testOutbound() {
|
||||
double error;
|
||||
double predict;
|
||||
|
||||
prepareLimiter(-1, -1, -1, -1);
|
||||
_log.info("Begin unlimited push of " + NUM_MB);
|
||||
long ms = testOutboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** Unlimited pushed " + NUM_MB + "MB in " + ms + "ms");
|
||||
prepareLimiter(-1, 4, -1, 4*1024);
|
||||
ms = testOutboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 4KBps pushed " + NUM_MB + "MB in " + ms + "ms");
|
||||
long ms = testOutboundThrottle(NUM_KB*1024, 1*1024);
|
||||
|
||||
/*prepareLimiter(-1, 4, -1, 4*1024);
|
||||
ms = testOutboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/4)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);*/
|
||||
|
||||
prepareLimiter(-1, 32, -1, 32*1024);
|
||||
ms = testOutboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 32KBps pushed " + NUM_MB + "MB in " + ms + "ms");
|
||||
ms = testOutboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/32)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);
|
||||
|
||||
prepareLimiter(-1, 256, -1, 256*1024);
|
||||
_log.info("Begin 256KBps push of " + NUM_MB);
|
||||
ms = testOutboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 256KBps pushed " + NUM_MB + "MB in " + ms + "ms");
|
||||
ms = testOutboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/256)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,52 +141,68 @@ public class BandwidthLimiterTest {
|
||||
*
|
||||
*/
|
||||
public void testInbound() {
|
||||
double predict;
|
||||
double error;
|
||||
|
||||
prepareLimiter(-1, -1, -1, -1);
|
||||
long ms = testInboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** Unlimited pulled " + NUM_MB + "MB in " + ms + "ms");
|
||||
prepareLimiter(4, -1, 4*1024, -1);
|
||||
ms = testInboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 4KBps pulled " + NUM_MB + "MB in " + ms + "ms");
|
||||
long ms = testInboundThrottle(NUM_KB*1024, 1*1024);
|
||||
|
||||
/*prepareLimiter(4, -1, 4*1024, -1);
|
||||
ms = testInboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/4)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);*/
|
||||
|
||||
prepareLimiter(32, -1, 32*1024, -1);
|
||||
ms = testInboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 32KBps pulled " + NUM_MB + "MB in " + ms + "ms");
|
||||
ms = testInboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/32)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);
|
||||
|
||||
prepareLimiter(256, -1, 256*1024, -1);
|
||||
ms = testInboundThrottle(NUM_MB*1024*1024, 1*1024);
|
||||
_log.info("** 256KBps pulled " + NUM_MB + "MB in " + ms + "ms");
|
||||
ms = testInboundThrottle(NUM_KB*1024, 1*1024);
|
||||
predict = (NUM_KB/256)*1000;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testOutboundContention() {
|
||||
double predict;
|
||||
double error;
|
||||
long ms;
|
||||
long end;
|
||||
long start;
|
||||
|
||||
prepareLimiter(-1, -1, -1, -1);
|
||||
long start = System.currentTimeMillis();
|
||||
long runningTimes[] = testOutboundContention(10, NUM_MB*1024*1024);
|
||||
long end = System.currentTimeMillis();
|
||||
_log.info("** Done with unlimited " + NUM_MB + "MB test with 10 concurrent threads after " + (end-start) + "ms: " + displayTimes(runningTimes));
|
||||
start = System.currentTimeMillis();
|
||||
//long runningTimes[] = testOutboundContention(10, NUM_KB*1024);
|
||||
end = System.currentTimeMillis();
|
||||
|
||||
//prepareLimiter(-1, 4, -1, 5*1024*1024);
|
||||
//start = System.currentTimeMillis();
|
||||
//runningTimes = testOutboundContention(10, NUM_MB*1024*1024);
|
||||
//runningTimes = testOutboundContention(10, NUM_KB*1024);
|
||||
//end = System.currentTimeMillis();
|
||||
//_log.info("** Done with 4KBps " + NUM_MB + "MB test with 10 concurrent threads after " + (end-start) + "ms: " + displayTimes(runningTimes));
|
||||
prepareLimiter(-1, 32, -1, 32*1024);
|
||||
start = System.currentTimeMillis();
|
||||
runningTimes = testOutboundContention(10, NUM_MB*1024*1024);
|
||||
end = System.currentTimeMillis();
|
||||
_log.info("** Done with 32KBps " + NUM_MB + "MB test with 10 concurrent threads after " + (end-start) + "ms: " + displayTimes(runningTimes));
|
||||
|
||||
//prepareLimiter(-1, 32, -1, 32*1024);
|
||||
//start = System.currentTimeMillis();
|
||||
//runningTimes = testOutboundContention(10, NUM_KB*1024);
|
||||
//end = System.currentTimeMillis();
|
||||
|
||||
prepareLimiter(-1, 256, -1, 256*1024);
|
||||
start = System.currentTimeMillis();
|
||||
runningTimes = testOutboundContention(10, NUM_MB*1024*1024);
|
||||
testOutboundContention(10, NUM_KB*1024);
|
||||
end = System.currentTimeMillis();
|
||||
_log.info("** Done with 256KBps " + NUM_MB + "MB test with 10 concurrent threads after " + (end-start) + "ms: " + displayTimes(runningTimes));
|
||||
}
|
||||
|
||||
private String displayTimes(long times[]) {
|
||||
StringBuffer rv = new StringBuffer();
|
||||
for (int i = 0; i < times.length; i++) {
|
||||
rv.append(times[i]);
|
||||
if (i + 1 <= times.length)
|
||||
rv.append(' ');
|
||||
}
|
||||
return rv.toString();
|
||||
ms = end-start;
|
||||
predict = (NUM_KB/256)*1000*10;
|
||||
error = predict/ms;
|
||||
//assertTrue(error>.89);
|
||||
assertTrue(error<1.05);
|
||||
|
||||
}
|
||||
|
||||
private long[] testOutboundContention(int numConcurrent, int numBytes) {
|
||||
@ -170,7 +210,7 @@ public class BandwidthLimiterTest {
|
||||
for (int i = 0; i < numConcurrent; i++) {
|
||||
threads[i] = new OutboundRunner(numBytes);
|
||||
}
|
||||
_log.debug("Starting up outbound contention test for " + numBytes + " with " + numConcurrent + " runners");
|
||||
|
||||
for (int i = 0; i < numConcurrent; i++)
|
||||
threads[i].start();
|
||||
for (int i = 0; i < numConcurrent; i++) {
|
||||
@ -196,17 +236,30 @@ public class BandwidthLimiterTest {
|
||||
public void run() {
|
||||
Thread.currentThread().setName("Out" + _runnerNum);
|
||||
_runningTime = testOutboundThrottle(_numBytes, 8*1024);
|
||||
_log.debug("Outbound runner " + _runnerNum + " pushed " + _numBytes + " in " + _runningTime + "ms");
|
||||
}
|
||||
public long getRunningTime() { return _runningTime; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class NullOutputStream extends OutputStream {
|
||||
public void write(int param) {}
|
||||
}
|
||||
|
||||
class FakeInputStream extends InputStream {
|
||||
private volatile int _numRead;
|
||||
private int _size;
|
||||
|
||||
public static void main(String args[]) {
|
||||
BandwidthLimiterTest test = new BandwidthLimiterTest();
|
||||
test.testOutbound();
|
||||
test.testInbound();
|
||||
//test.testOutboundContention();
|
||||
System.exit(0);
|
||||
public FakeInputStream(int size) {
|
||||
_size = size;
|
||||
_numRead = 0;
|
||||
}
|
||||
public int read() {
|
||||
int rv = 0;
|
||||
if (_numRead >= _size)
|
||||
rv = -1;
|
||||
else
|
||||
rv = 42;
|
||||
_numRead++;
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package net.i2p.router.transport;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Read up to a specified number of bytes, then EOF.
|
||||
* Uses pretty much no memory.
|
||||
*
|
||||
*/
|
||||
public class FakeInputStream extends InputStream {
|
||||
private volatile int _numRead;
|
||||
private int _size;
|
||||
|
||||
public FakeInputStream(int size) {
|
||||
_size = size;
|
||||
_numRead = 0;
|
||||
}
|
||||
public int read() {
|
||||
int rv = 0;
|
||||
if (_numRead >= _size)
|
||||
rv = -1;
|
||||
else
|
||||
rv = 42;
|
||||
_numRead++;
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package net.i2p.router.transport;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Output stream for when we don't care whats written
|
||||
*
|
||||
*/
|
||||
public class NullOutputStream extends OutputStream {
|
||||
public void write(int param) {}
|
||||
}
|
158
router/java/test/net/i2p/router/tunnel/BatchedFragmentTest.java
Normal file
158
router/java/test/net/i2p/router/tunnel/BatchedFragmentTest.java
Normal file
@ -0,0 +1,158 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.Base64;
|
||||
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.util.Log;
|
||||
|
||||
/**
|
||||
* Test the batching behavior of the preprocessor with one, two, or three
|
||||
* messages of various sizes and settings.
|
||||
*
|
||||
*/
|
||||
public class BatchedFragmentTest extends FragmentTest {
|
||||
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
BatchedPreprocessor.DEFAULT_DELAY = 200;
|
||||
}
|
||||
|
||||
protected TunnelGateway.QueuePreprocessor createPreprocessor(I2PAppContext ctx) {
|
||||
return new BatchedPreprocessor(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a small message, wait a second, then send a large message, pushing
|
||||
* the first one through immediately, with the rest of the large one passed
|
||||
* after a brief delay.
|
||||
*
|
||||
*/
|
||||
public void testBatched() {
|
||||
TunnelGateway.Pending pending1 = createPending(10, false, false);
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending1);
|
||||
|
||||
TunnelGateway.Pending pending2 = createPending(1024, false, false);
|
||||
|
||||
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
|
||||
SenderImpl sender = new SenderImpl();
|
||||
DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending1.getData(), pending2.getData());
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending1.getData();
|
||||
|
||||
boolean keepGoing = true;
|
||||
boolean alreadyAdded = false;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
if (keepGoing) {
|
||||
try { Thread.sleep(150); } catch (InterruptedException ie) {}
|
||||
|
||||
if (!alreadyAdded) {
|
||||
messages.add(pending2);
|
||||
alreadyAdded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(handleReceiver.receivedOk());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a small message, wait a second, then send a large message, pushing
|
||||
* the first one through immediately, with the rest of the large one passed
|
||||
* after a brief delay.
|
||||
*
|
||||
*/
|
||||
public void runBatches() {
|
||||
//success += testBatched(1, false, false, 1024, false, false);
|
||||
// this takes a long fucking time
|
||||
for (int i = 1; i <= 1024; i++) {
|
||||
testBatched(i, false, false, 1024, false, false, 1024, false, false);
|
||||
testBatched(i, true, false, 1024, false, false, 1024, false, false);
|
||||
testBatched(i, true, true, 1024, false, false, 1024, false, false);
|
||||
testBatched(i, false, false, 1024, true, false, 1024, false, false);
|
||||
testBatched(i, true, false, 1024, true, false, 1024, false, false);
|
||||
testBatched(i, true, true, 1024, true, false, 1024, false, false);
|
||||
testBatched(i, false, false, 1024, true, true, 1024, false, false);
|
||||
testBatched(i, true, false, 1024, true, true, 1024, false, false);
|
||||
testBatched(i, true, true, 1024, true, true, 1024, false, false);
|
||||
|
||||
testBatched(i, false, false, 1024, false, false, 1024, true, false);
|
||||
testBatched(i, true, false, 1024, false, false, 1024, true, false);
|
||||
testBatched(i, true, true, 1024, false, false, 1024, true, false);
|
||||
testBatched(i, false, false, 1024, true, false, 1024, true, false);
|
||||
testBatched(i, true, false, 1024, true, false, 1024, true, false);
|
||||
testBatched(i, true, true, 1024, true, false, 1024, true, false);
|
||||
testBatched(i, false, false, 1024, true, true, 1024, true, false);
|
||||
testBatched(i, true, false, 1024, true, true, 1024, true, false);
|
||||
testBatched(i, true, true, 1024, true, true, 1024, true, false);
|
||||
|
||||
testBatched(i, false, false, 1024, false, false, 1024, true, true);
|
||||
testBatched(i, true, false, 1024, false, false, 1024, true, true);
|
||||
testBatched(i, true, true, 1024, false, false, 1024, true, true);
|
||||
testBatched(i, false, false, 1024, true, false, 1024, true, true);
|
||||
testBatched(i, true, false, 1024, true, false, 1024, true, true);
|
||||
testBatched(i, true, true, 1024, true, false, 1024, true, true);
|
||||
testBatched(i, false, false, 1024, true, true, 1024, true, true);
|
||||
testBatched(i, true, false, 1024, true, true, 1024, true, true);
|
||||
testBatched(i, true, true, 1024, true, true, 1024, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
runBatch(pending1, pending2, pending3);
|
||||
}
|
||||
|
||||
private void runBatch(TunnelGateway.Pending pending1, TunnelGateway.Pending pending2, TunnelGateway.Pending pending3) {
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending1);
|
||||
|
||||
TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context);
|
||||
SenderImpl sender = new SenderImpl();
|
||||
DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending1.getData(), pending2.getData(), pending3.getData());
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending1.getData();
|
||||
|
||||
boolean keepGoing = true;
|
||||
int added = 0;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
if ( (keepGoing) || ((messages.size() == 0) && (added < 2) ) ) {
|
||||
try { Thread.sleep(150); } catch (InterruptedException ie) {}
|
||||
|
||||
if (added == 0) {
|
||||
messages.add(pending2);
|
||||
added++;
|
||||
keepGoing = true;
|
||||
} else if (added == 1) {
|
||||
messages.add(pending3);
|
||||
added++;
|
||||
keepGoing = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(handleReceiver.receivedOk());
|
||||
}
|
||||
}
|
@ -1,4 +1,12 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import net.i2p.I2PAppContext;
|
||||
@ -10,18 +18,18 @@ import net.i2p.data.i2np.DataMessage;
|
||||
import net.i2p.data.i2np.I2NPMessage;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Simple test to see if the fragmentation is working, testing the preprocessor,
|
||||
* FragmentHandler, and FragmentedMessage operation.
|
||||
*
|
||||
*/
|
||||
public class FragmentTest {
|
||||
public class FragmentTest extends TestCase{
|
||||
protected I2PAppContext _context;
|
||||
protected Log _log;
|
||||
|
||||
public FragmentTest() {
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(getClass());
|
||||
_context.random().nextBoolean();
|
||||
FragmentHandler.MAX_DEFRAGMENT_TIME = 10*1000;
|
||||
}
|
||||
@ -34,7 +42,7 @@ public class FragmentTest {
|
||||
* Send a message that fits inside a single fragment through
|
||||
*
|
||||
*/
|
||||
public void runSingle() {
|
||||
public void testSingle() {
|
||||
TunnelGateway.Pending pending = createPending(949, false, false);
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending);
|
||||
@ -45,23 +53,21 @@ public class FragmentTest {
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
|
||||
boolean keepGoing = true;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
if (keepGoing)
|
||||
try { Thread.sleep(100); } catch (InterruptedException ie) {}
|
||||
}
|
||||
if (handleReceiver.receivedOk())
|
||||
_log.info("received OK");
|
||||
assertTrue(handleReceiver.receivedOk());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message with two fragments through with no delay
|
||||
*
|
||||
*/
|
||||
public void runMultiple() {
|
||||
public void testMultiple() {
|
||||
TunnelGateway.Pending pending = createPending(2048, false, false);
|
||||
ArrayList messages = new ArrayList();
|
||||
messages.add(pending);
|
||||
@ -72,7 +78,6 @@ public class FragmentTest {
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
boolean keepGoing = true;
|
||||
while (keepGoing) {
|
||||
@ -80,8 +85,7 @@ public class FragmentTest {
|
||||
if (keepGoing)
|
||||
try { Thread.sleep(100); } catch (InterruptedException ie) {}
|
||||
}
|
||||
if (handleReceiver.receivedOk())
|
||||
_log.info("received OK");
|
||||
assertTrue(handleReceiver.receivedOk());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,7 +102,7 @@ public class FragmentTest {
|
||||
FragmentHandler handler = new FragmentHandler(_context, new DefragmentedReceiverImpl(pending.getData()));
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 11*1000);
|
||||
byte msg[] = pending.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
boolean keepGoing = true;
|
||||
while (keepGoing) {
|
||||
keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver);
|
||||
@ -108,20 +112,11 @@ public class FragmentTest {
|
||||
}
|
||||
|
||||
public void runVaried() {
|
||||
int failures = 0;
|
||||
for (int i = 0; i <= 4096; i++) {
|
||||
boolean ok = runVaried(i, false, false);
|
||||
if (!ok) { _log.error("** processing " + i+ " w/ no router, no tunnel failed"); failures++; }
|
||||
ok = runVaried(i, true, false);
|
||||
if (!ok) { _log.error("** processing " + i+ " w/ router, no tunnel failed"); failures++; }
|
||||
ok = runVaried(i, true, true);
|
||||
if (!ok) { _log.error("** processing " + i+ " w/ router, tunnel failed"); failures++; }
|
||||
else _log.info("Tests pass for size " + i);
|
||||
assertTrue(runVaried(i, false, false));
|
||||
assertTrue(runVaried(i, true, false));
|
||||
assertTrue(runVaried(i, true, true));
|
||||
}
|
||||
if (failures == 0)
|
||||
_log.info("** success after all varied tests");
|
||||
else
|
||||
_log.error("** failed " + failures +" varied tests");
|
||||
}
|
||||
|
||||
protected boolean runVaried(int size, boolean includeRouter, boolean includeTunnel) {
|
||||
@ -135,7 +130,6 @@ public class FragmentTest {
|
||||
FragmentHandler handler = new FragmentHandler(_context, handleReceiver);
|
||||
ReceiverImpl receiver = new ReceiverImpl(handler, 0);
|
||||
byte msg[] = pending.getData();
|
||||
_log.debug("SEND(" + msg.length + "): " + Base64.encode(msg) + " " + _context.sha().calculateHash(msg).toBase64());
|
||||
|
||||
boolean keepGoing = true;
|
||||
while (keepGoing) {
|
||||
@ -226,20 +220,4 @@ public class FragmentTest {
|
||||
return _received == 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void runTests() {
|
||||
runVaried();
|
||||
_log.info("\n===========================Begin runSingle()\n\n");
|
||||
runSingle();
|
||||
_log.info("\n===========================Begin runMultiple()\n\n");
|
||||
runMultiple();
|
||||
_log.info("\n===========================Begin runDelayed() (should have 3 errors)\n\n");
|
||||
runDelayed();
|
||||
_log.info("\n===========================After runDelayed()\n\n");
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
FragmentTest t = new FragmentTest();
|
||||
t.runTests();
|
||||
}
|
||||
}
|
@ -1,4 +1,12 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -11,11 +19,13 @@ import net.i2p.data.i2np.DataMessage;
|
||||
import net.i2p.data.i2np.I2NPMessage;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Quick unit test for base functionality of inbound tunnel
|
||||
* operation
|
||||
*/
|
||||
public class InboundGatewayTest {
|
||||
public class InboundGatewayTest extends TestCase{
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
private TunnelCreatorConfig _config;
|
||||
@ -24,33 +34,18 @@ public class InboundGatewayTest {
|
||||
private TestReceiver _receiver;
|
||||
private TunnelGateway _gw;
|
||||
|
||||
public InboundGatewayTest() {
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(InboundGatewayTest.class);
|
||||
}
|
||||
|
||||
public void runTest() {
|
||||
int numHops = 8;
|
||||
int runCount = 1;
|
||||
_config = prepareConfig(numHops);
|
||||
_config = prepareConfig(8);
|
||||
_preprocessor = new TrivialPreprocessor(_context);
|
||||
_sender = new InboundSender(_context, _config.getConfig(0));
|
||||
_receiver = new TestReceiver(_config);
|
||||
_gw = new TunnelGateway(_context, _preprocessor, _sender, _receiver);
|
||||
|
||||
// single fragment
|
||||
testSmall(runCount);
|
||||
// includes target router instructions
|
||||
testRouter(runCount);
|
||||
// includes target router & tunnel instructions
|
||||
testTunnel(runCount);
|
||||
// multiple fragments
|
||||
testLarge(runCount);
|
||||
|
||||
try { Thread.sleep(5*1000); } catch (Exception e) {}
|
||||
}
|
||||
|
||||
private void testSmall(int runCount) {
|
||||
public void testSmall() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -60,25 +55,22 @@ public class InboundGatewayTest {
|
||||
java.util.Arrays.fill(m.getData(), (byte)0xFF);
|
||||
m.setMessageExpiration(_context.clock().now() + 60*1000);
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
_log.debug("Sending " + m.getUniqueId());
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, null, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testRouter(int runCount) {
|
||||
public void testRouter() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -90,25 +82,22 @@ public class InboundGatewayTest {
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
Hash to = new Hash(new byte[Hash.HASH_LENGTH]);
|
||||
java.util.Arrays.fill(to.getData(), (byte)0xFF);
|
||||
_log.debug("Sending " + m.getUniqueId() + " to " + to);
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, to, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testTunnel(int runCount) {
|
||||
public void testTunnel() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -121,25 +110,22 @@ public class InboundGatewayTest {
|
||||
Hash to = new Hash(new byte[Hash.HASH_LENGTH]);
|
||||
java.util.Arrays.fill(to.getData(), (byte)0xFF);
|
||||
TunnelId tunnel = new TunnelId(42);
|
||||
_log.debug("Sending " + m.getUniqueId() + " to " + to + "/" + tunnel);
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, to, tunnel);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testLarge(int runCount) {
|
||||
public void testLarge() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -149,22 +135,17 @@ public class InboundGatewayTest {
|
||||
java.util.Arrays.fill(m.getData(), (byte)0xFF);
|
||||
m.setMessageExpiration(_context.clock().now() + 60*1000);
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
_log.debug("Sending " + m.getUniqueId());
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, null, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
try { Thread.sleep(60*1000); } catch (Exception e) {}
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
//try { Thread.sleep(60*1000); } catch (Exception e) {}
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,28 +163,17 @@ public class InboundGatewayTest {
|
||||
|
||||
for (int i = 1; i <= _config.getLength() - 2; i++) {
|
||||
HopProcessor hop = new HopProcessor(_context, _config.getConfig(i));
|
||||
boolean ok = hop.process(encrypted, 0, encrypted.length, _config.getConfig(i).getReceiveFrom());
|
||||
if (!ok)
|
||||
_log.error("Error processing at hop " + i);
|
||||
//else
|
||||
// _log.info("Processing OK at hop " + i);
|
||||
assertTrue(hop.process(encrypted, 0, encrypted.length, _config.getConfig(i).getReceiveFrom()));
|
||||
}
|
||||
|
||||
// now handle it at the endpoint
|
||||
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, _config);
|
||||
boolean ok = end.retrievePreprocessedData(encrypted, 0, encrypted.length, _config.getPeer(_config.getLength()-2));
|
||||
if (!ok)
|
||||
_log.error("Error retrieving cleartext at the endpoint");
|
||||
assertTrue(end.retrievePreprocessedData(encrypted, 0, encrypted.length, _config.getPeer(_config.getLength()-2)));
|
||||
|
||||
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Received " + Base64.encode(encrypted));
|
||||
|
||||
_handler.receiveTunnelMessage(encrypted, 0, encrypted.length);
|
||||
_log.debug("\n\ndone receiving message\n\n");
|
||||
}
|
||||
public void receiveComplete(I2NPMessage msg, Hash toRouter, TunnelId toTunnel) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Completed " + msg.getUniqueId() + " to " + toRouter + "/" + toTunnel);
|
||||
_received.add(msg);
|
||||
}
|
||||
public List clearReceived() {
|
||||
@ -245,9 +215,4 @@ public class InboundGatewayTest {
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
InboundGatewayTest test = new InboundGatewayTest();
|
||||
test.runTest();
|
||||
}
|
||||
}
|
@ -1,4 +1,12 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.Base64;
|
||||
@ -6,59 +14,42 @@ import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Quick unit test for base functionality of inbound tunnel
|
||||
* operation
|
||||
*
|
||||
*/
|
||||
public class InboundTest {
|
||||
public class InboundTest extends TestCase{
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
|
||||
public InboundTest() {
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(InboundTest.class);
|
||||
}
|
||||
|
||||
public void runTest() {
|
||||
int numHops = 8;
|
||||
TunnelCreatorConfig config = prepareConfig(numHops);
|
||||
long start = _context.clock().now();
|
||||
for (int i = 0; i < 1; i++)
|
||||
runTest(numHops, config);
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for 1000 messages: " + time);
|
||||
}
|
||||
|
||||
private void runTest(int numHops, TunnelCreatorConfig config) {
|
||||
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);
|
||||
|
||||
_log.debug("orig: \n" + Base64.encode(orig, 16, orig.length-16));
|
||||
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();
|
||||
boolean ok = hop.process(message, 0, message.length, prev);
|
||||
if (!ok)
|
||||
_log.error("Error processing at hop " + i);
|
||||
//else
|
||||
// _log.info("Processing OK at hop " + i);
|
||||
assertTrue(hop.process(message, 0, message.length, prev));
|
||||
}
|
||||
|
||||
InboundEndpointProcessor end = new InboundEndpointProcessor(_context, config);
|
||||
boolean ok = end.retrievePreprocessedData(message, 0, message.length, config.getPeer(numHops-2));
|
||||
if (!ok) {
|
||||
_log.error("Error retrieving cleartext at the endpoint");
|
||||
try { Thread.sleep(5*1000); } catch (Exception e) {}
|
||||
}
|
||||
assertTrue(end.retrievePreprocessedData(message, 0, message.length, config.getPeer(numHops-2)));
|
||||
|
||||
//_log.debug("After: " + Base64.encode(message, 16, orig.length-16));
|
||||
boolean eq = DataHelper.eq(orig, 16, message, 16, orig.length - 16);
|
||||
_log.info("equal? " + eq);
|
||||
assertTrue(DataHelper.eq(orig, 16, message, 16, orig.length - 16));
|
||||
}
|
||||
|
||||
private TunnelCreatorConfig prepareConfig(int numHops) {
|
||||
@ -93,9 +84,4 @@ public class InboundTest {
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
InboundTest test = new InboundTest();
|
||||
test.runTest();
|
||||
}
|
||||
}
|
@ -1,4 +1,12 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -11,46 +19,32 @@ import net.i2p.data.i2np.DataMessage;
|
||||
import net.i2p.data.i2np.I2NPMessage;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Quick unit test for base functionality of outbound tunnel
|
||||
* operation
|
||||
*/
|
||||
public class OutboundGatewayTest {
|
||||
public class OutboundGatewayTest extends TestCase{
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
private TunnelCreatorConfig _config;
|
||||
private TunnelGateway.QueuePreprocessor _preprocessor;
|
||||
private TunnelGateway.Sender _sender;
|
||||
private TestReceiver _receiver;
|
||||
private TunnelGateway _gw;
|
||||
|
||||
public OutboundGatewayTest() {
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(OutboundGatewayTest.class);
|
||||
}
|
||||
|
||||
public void runTest() {
|
||||
int numHops = 8;
|
||||
int runCount = 1;
|
||||
_config = prepareConfig(numHops);
|
||||
_config = prepareConfig(8);
|
||||
_preprocessor = new TrivialPreprocessor(_context);
|
||||
_sender = new OutboundSender(_context, _config);
|
||||
_receiver = new TestReceiver(_config);
|
||||
_gw = new TunnelGateway(_context, _preprocessor, _sender, _receiver);
|
||||
|
||||
// single fragment
|
||||
testSmall(runCount);
|
||||
// includes target router instructions
|
||||
testRouter(runCount);
|
||||
// includes target router & tunnel instructions
|
||||
testTunnel(runCount);
|
||||
// multiple fragments
|
||||
testLarge(runCount);
|
||||
|
||||
try { Thread.sleep(5*1000); } catch (Exception e) {}
|
||||
}
|
||||
|
||||
private void testSmall(int runCount) {
|
||||
public void testSmall() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -60,25 +54,22 @@ public class OutboundGatewayTest {
|
||||
java.util.Arrays.fill(m.getData(), (byte)0xFF);
|
||||
m.setMessageExpiration(_context.clock().now() + 60*1000);
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
_log.debug("Sending " + m.getUniqueId());
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, null, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testRouter(int runCount) {
|
||||
public void testRouter() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -90,25 +81,22 @@ public class OutboundGatewayTest {
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
Hash to = new Hash(new byte[Hash.HASH_LENGTH]);
|
||||
java.util.Arrays.fill(to.getData(), (byte)0xFF);
|
||||
_log.debug("Sending " + m.getUniqueId() + " to " + to);
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, to, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testTunnel(int runCount) {
|
||||
public void testTunnel() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -121,25 +109,22 @@ public class OutboundGatewayTest {
|
||||
Hash to = new Hash(new byte[Hash.HASH_LENGTH]);
|
||||
java.util.Arrays.fill(to.getData(), (byte)0xFF);
|
||||
TunnelId tunnel = new TunnelId(42);
|
||||
_log.debug("Sending " + m.getUniqueId() + " to " + to + "/" + tunnel);
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, to, tunnel);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
private void testLarge(int runCount) {
|
||||
public void testLarge() {
|
||||
int runCount = 1;
|
||||
|
||||
List messages = new ArrayList(runCount);
|
||||
long start = _context.clock().now();
|
||||
|
||||
@ -149,22 +134,17 @@ public class OutboundGatewayTest {
|
||||
java.util.Arrays.fill(m.getData(), (byte)0xFF);
|
||||
m.setMessageExpiration(_context.clock().now() + 60*1000);
|
||||
m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
|
||||
_log.debug("Sending " + m.getUniqueId());
|
||||
byte data[] = m.toByteArray();
|
||||
_log.debug("SEND(" + data.length + "): " + Base64.encode(data) + " " + _context.sha().calculateHash(data).toBase64());
|
||||
messages.add(m);
|
||||
_gw.add(m, null, null);
|
||||
}
|
||||
|
||||
long time = _context.clock().now() - start;
|
||||
try { Thread.sleep(60*1000); } catch (Exception e) {}
|
||||
_log.debug("Time for " + runCount + " messages: " + time);
|
||||
//try { Thread.sleep(60*1000); } catch (Exception e) {}
|
||||
|
||||
List received = _receiver.clearReceived();
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (!received.contains(((I2NPMessage)messages.get(i)))) {
|
||||
_log.error("Message " + i + " not received");
|
||||
}
|
||||
assertTrue(received.contains(((I2NPMessage)messages.get(i))));
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,22 +162,14 @@ public class OutboundGatewayTest {
|
||||
|
||||
for (int i = 1; i < _config.getLength(); i++) {
|
||||
HopProcessor hop = new HopProcessor(_context, _config.getConfig(i));
|
||||
boolean ok = hop.process(encrypted, 0, encrypted.length, _config.getConfig(i).getReceiveFrom());
|
||||
if (!ok)
|
||||
_log.error("Error processing at hop " + i);
|
||||
//else
|
||||
// _log.info("Processing OK at hop " + i);
|
||||
assertTrue(hop.process(encrypted, 0, encrypted.length, _config.getConfig(i).getReceiveFrom()));
|
||||
|
||||
}
|
||||
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Received " + Base64.encode(encrypted));
|
||||
|
||||
_handler.receiveTunnelMessage(encrypted, 0, encrypted.length);
|
||||
_log.debug("\n\ndone receiving message\n\n");
|
||||
}
|
||||
public void receiveComplete(I2NPMessage msg, Hash toRouter, TunnelId toTunnel) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Completed " + msg.getUniqueId() + " to " + toRouter + "/" + toTunnel);
|
||||
_received.add(msg);
|
||||
}
|
||||
public List clearReceived() {
|
||||
@ -239,9 +211,4 @@ public class OutboundGatewayTest {
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
OutboundGatewayTest test = new OutboundGatewayTest();
|
||||
test.runTest();
|
||||
}
|
||||
}
|
@ -1,4 +1,12 @@
|
||||
package net.i2p.router.tunnel;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.Base64;
|
||||
@ -6,21 +14,21 @@ import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Quick unit test for base functionality of outbound tunnel
|
||||
* operation
|
||||
*
|
||||
*/
|
||||
public class OutboundTest {
|
||||
public class OutboundTest extends TestCase{
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
|
||||
public OutboundTest() {
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(OutboundTest.class);
|
||||
}
|
||||
|
||||
public void runTest() {
|
||||
public void testOutbound() {
|
||||
int numHops = 8;
|
||||
TunnelCreatorConfig config = prepareConfig(numHops);
|
||||
|
||||
@ -35,16 +43,10 @@ public class OutboundTest {
|
||||
for (int i = 0; i < numHops; i++) {
|
||||
HopProcessor hop = new HopProcessor(_context, config.getConfig(i));
|
||||
Hash prev = config.getConfig(i).getReceiveFrom();
|
||||
boolean ok = hop.process(message, 0, message.length, prev);
|
||||
if (!ok)
|
||||
_log.error("Error processing at hop " + i);
|
||||
//else
|
||||
// _log.info("Processing OK at hop " + i);
|
||||
assertTrue(hop.process(message, 0, message.length, prev));
|
||||
}
|
||||
|
||||
_log.debug("After: " + Base64.encode(message, 16, orig.length-16));
|
||||
boolean eq = DataHelper.eq(orig, 16, message, 16, orig.length - 16);
|
||||
_log.info("equal? " + eq);
|
||||
}
|
||||
|
||||
private TunnelCreatorConfig prepareConfig(int numHops) {
|
||||
@ -59,6 +61,7 @@ public class OutboundTest {
|
||||
|
||||
TunnelCreatorConfig config = new TunnelCreatorConfig(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());
|
||||
@ -78,9 +81,4 @@ public class OutboundTest {
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
OutboundTest test = new OutboundTest();
|
||||
test.runTest();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user