diff --git a/router/java/test/junit/net/i2p/router/transport/BandwidthLimiterTest.java b/router/java/test/junit/net/i2p/router/transport/BandwidthLimiterTest.java index 30d1571f1b77b2243f457a424e09380d5b76544a..f82162f27f304196f17d169d57d38e92cb72432d 100644 --- a/router/java/test/junit/net/i2p/router/transport/BandwidthLimiterTest.java +++ b/router/java/test/junit/net/i2p/router/transport/BandwidthLimiterTest.java @@ -14,7 +14,11 @@ import java.io.OutputStream; import java.util.Properties; import java.util.Random; -import junit.framework.TestCase; +import org.junit.BeforeClass; +import org.junit.Test; + +import static junit.framework.TestCase.*; +import net.i2p.router.Router; import net.i2p.router.RouterContext; /** @@ -28,15 +32,14 @@ import net.i2p.router.RouterContext; * 10 concurrent threads is, well, slow. * */ -public class BandwidthLimiterTest extends TestCase{ - private RouterContext _context; +public class BandwidthLimiterTest { + private static RouterContext _context; private final static int NUM_KB = 256; - public void setUp() { - _context = new RouterContext(null); - } - - public void tearDown(){ + @BeforeClass + public static void setUp() { + _context = new RouterContext(new Router()); + _context.initAll(); } private void prepareLimiter(int inKBps, int outKBps, int inBurst, int outBurst) { @@ -102,6 +105,7 @@ public class BandwidthLimiterTest extends TestCase{ * with various limits) and log the times. * */ + @Test public void testOutbound() { double error; double predict; @@ -137,6 +141,7 @@ public class BandwidthLimiterTest extends TestCase{ * with various limits) and log the times. * */ + @Test public void testInbound() { double predict; double error; @@ -168,6 +173,7 @@ public class BandwidthLimiterTest extends TestCase{ } + @Test public void testOutboundContention() { double predict; double error; diff --git a/router/java/test/junit/net/i2p/router/tunnel/BatchedFragmentTest.java b/router/java/test/junit/net/i2p/router/tunnel/BatchedFragmentTest.java index 7c9ee5cb6861c6d48c976d3cb8d66c2de107b97e..7289105ad25716e104b1f643211e39436d093d97 100644 --- a/router/java/test/junit/net/i2p/router/tunnel/BatchedFragmentTest.java +++ b/router/java/test/junit/net/i2p/router/tunnel/BatchedFragmentTest.java @@ -10,7 +10,10 @@ package net.i2p.router.tunnel; import java.util.ArrayList; -import net.i2p.I2PAppContext; +import static junit.framework.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; + import net.i2p.router.RouterContext; /** @@ -20,8 +23,8 @@ import net.i2p.router.RouterContext; */ public class BatchedFragmentTest extends FragmentTest { + @Before public void setUp() { - super.setUp(); BatchedPreprocessor.DEFAULT_DELAY = 200; } @@ -35,6 +38,7 @@ public class BatchedFragmentTest extends FragmentTest { * after a brief delay. * */ + @Test public void testBatched() { PendingGatewayMessage pending1 = createPending(10, false, false); ArrayList messages = new ArrayList(); @@ -66,6 +70,58 @@ public class BatchedFragmentTest extends FragmentTest { assertTrue(handleReceiver.receivedOk()); } + /** + * Send a message that fits inside a single fragment through + * + */ + @Test + public void testSingle() { + PendingGatewayMessage pending = createPending(949, false, false); + ArrayList messages = new ArrayList(); + messages.add(pending); + + TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context); + SenderImpl sender = new SenderImpl(); + DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending.getData()); + FragmentHandler handler = new FragmentHandler(_context, handleReceiver); + ReceiverImpl receiver = new ReceiverImpl(handler, 0); + byte msg[] = pending.getData(); + + boolean keepGoing = true; + while (keepGoing) { + keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver); + if (keepGoing) + try { Thread.sleep(100); } catch (InterruptedException ie) {} + } + assertTrue(handleReceiver.receivedOk()); + } + + /** + * Send a message with two fragments through with no delay + * + */ + @Test + public void testMultiple() throws Exception { + PendingGatewayMessage pending = createPending(2048, false, false); + ArrayList messages = new ArrayList(); + messages.add(pending); + + TunnelGateway.QueuePreprocessor pre = createPreprocessor(_context); + SenderImpl sender = new SenderImpl(); + DefragmentedReceiverImpl handleReceiver = new DefragmentedReceiverImpl(pending.getData()); + FragmentHandler handler = new FragmentHandler(_context, handleReceiver); + ReceiverImpl receiver = new ReceiverImpl(handler, 0); + byte msg[] = pending.getData(); + + boolean keepGoing = true; + while (keepGoing) { + keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver); + if (keepGoing) + try { Thread.sleep(100); } catch (InterruptedException ie) {} + } + assertTrue(handleReceiver.receivedOk()); + } + /** * Send a small message, wait a second, then send a large message, pushing diff --git a/router/java/test/junit/net/i2p/router/tunnel/BuildMessageTest.java b/router/java/test/junit/net/i2p/router/tunnel/BuildMessageTestStandalone.java similarity index 92% rename from router/java/test/junit/net/i2p/router/tunnel/BuildMessageTest.java rename to router/java/test/junit/net/i2p/router/tunnel/BuildMessageTestStandalone.java index 26c7cb024c126d3fcf7c1e489a10efdd064b3238..bae9c3a7d1a99bcff1ae7a3c638ae8bd23f6b6c5 100644 --- a/router/java/test/junit/net/i2p/router/tunnel/BuildMessageTest.java +++ b/router/java/test/junit/net/i2p/router/tunnel/BuildMessageTestStandalone.java @@ -23,8 +23,16 @@ import net.i2p.util.Log; * Simple test to create an encrypted TunnelBuildMessage, decrypt its layers (as it would be * during transmission), inject replies, then handle the TunnelBuildReplyMessage (unwrapping * the reply encryption and reading the replies). + * + * === + * Update 1/5/2013 : + * This test is renamed so it does not match the JUnit wildcard. + * There is something wrong with the decryption check; it doesn't look like the test takes + * into consideration the re-encryption of the records in the TunnelBuildMessage. + * Most probably the test will have to be re-written from scratch. + * --zab */ -public class BuildMessageTest extends TestCase { +public class BuildMessageTestStandalone extends TestCase { private Hash _peers[]; private PrivateKey _privKeys[]; private PublicKey _pubKeys[]; @@ -35,7 +43,7 @@ public class BuildMessageTest extends TestCase { I2PAppContext ctx = I2PAppContext.getGlobalContext(); Log log = ctx.logManager().getLog(getClass()); - List order = pickOrder(ctx); + List<Integer> order = pickOrder(); TunnelCreatorConfig cfg = createConfig(ctx); _replyRouter = new Hash(); @@ -122,9 +130,9 @@ public class BuildMessageTest extends TestCase { "\n================================================================"); } - private static final List pickOrder(I2PAppContext ctx) { + private static final List<Integer> pickOrder() { // pseudorandom, yet consistent (so we can be repeatable) - List rv = new ArrayList(8); + List<Integer> rv = new ArrayList<Integer>(8); rv.add(new Integer(2)); rv.add(new Integer(4)); rv.add(new Integer(6)); diff --git a/router/java/test/junit/net/i2p/router/tunnel/FragmentTest.java b/router/java/test/junit/net/i2p/router/tunnel/FragmentTest.java index 6822e358af3e51851f91af6f54efac7032c725cd..f2261517bbea1eebc72207a85cecb03282fdbaf3 100644 --- a/router/java/test/junit/net/i2p/router/tunnel/FragmentTest.java +++ b/router/java/test/junit/net/i2p/router/tunnel/FragmentTest.java @@ -10,7 +10,12 @@ package net.i2p.router.tunnel; import java.util.ArrayList; -import junit.framework.TestCase; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static junit.framework.TestCase.*; + import net.i2p.data.DataHelper; import net.i2p.data.Hash; import net.i2p.data.TunnelId; @@ -23,11 +28,17 @@ import net.i2p.router.RouterContext; * FragmentHandler, and FragmentedMessage operation. * */ -public class FragmentTest extends TestCase{ - protected RouterContext _context; +public class FragmentTest { + + protected static RouterContext _context; - public void setUp() { + @BeforeClass + public static void globalSetUp() { _context = new RouterContext(null); + } + + @Before + public void set() { _context.random().nextBoolean(); FragmentHandler.MAX_DEFRAGMENT_TIME = 10*1000; } @@ -40,6 +51,7 @@ public class FragmentTest extends TestCase{ * Send a message that fits inside a single fragment through * */ + @Test public void testSingle() { PendingGatewayMessage pending = createPending(949, false, false); ArrayList messages = new ArrayList(); @@ -51,21 +63,19 @@ public class FragmentTest extends TestCase{ FragmentHandler handler = new FragmentHandler(_context, handleReceiver); ReceiverImpl receiver = new ReceiverImpl(handler, 0); byte msg[] = pending.getData(); - - boolean keepGoing = true; - while (keepGoing) { - keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver); - if (keepGoing) - try { Thread.sleep(100); } catch (InterruptedException ie) {} - } - assertTrue(handleReceiver.receivedOk()); + + try { + pre.preprocessQueue(messages, new SenderImpl(), receiver); + fail("should have thrown IAE"); + } catch (IllegalArgumentException expected){} } /** * Send a message with two fragments through with no delay * */ - public void testMultiple() { + @Test + public void testMultiple() throws Exception { PendingGatewayMessage pending = createPending(2048, false, false); ArrayList messages = new ArrayList(); messages.add(pending); @@ -77,13 +87,10 @@ public class FragmentTest extends TestCase{ ReceiverImpl receiver = new ReceiverImpl(handler, 0); byte msg[] = pending.getData(); - boolean keepGoing = true; - while (keepGoing) { - keepGoing = pre.preprocessQueue(messages, new SenderImpl(), receiver); - if (keepGoing) - try { Thread.sleep(100); } catch (InterruptedException ie) {} - } - assertTrue(handleReceiver.receivedOk()); + try { + pre.preprocessQueue(messages, new SenderImpl(), receiver); + fail("should have thrown IAE"); + } catch (IllegalArgumentException expected){} } /** @@ -183,10 +190,10 @@ public class FragmentTest extends TestCase{ } protected class DefragmentedReceiverImpl implements FragmentHandler.DefragmentedReceiver { - private byte _expected[]; - private byte _expected2[]; - private byte _expected3[]; - private int _received; + private volatile byte _expected[]; + private volatile byte _expected2[]; + private volatile byte _expected3[]; + private volatile int _received; public DefragmentedReceiverImpl(byte expected[]) { this(expected, null); }