From 566221b732f4b8d4fc64723bdadd14481aab585d Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 2 Sep 2019 18:07:23 +0000 Subject: [PATCH] Tests: Fix InboundTest so it can be run without a real router --- .../tunnel/InboundEndpointProcessor.java | 10 ++- .../net/i2p/router/tunnel/InboundIT.java | 50 ----------- .../net/i2p/router/tunnel/InboundTest.java | 89 +++++++++++++++++++ 3 files changed, 97 insertions(+), 52 deletions(-) delete mode 100644 router/java/test/junit/net/i2p/router/tunnel/InboundIT.java create mode 100644 router/java/test/junit/net/i2p/router/tunnel/InboundTest.java diff --git a/router/java/src/net/i2p/router/tunnel/InboundEndpointProcessor.java b/router/java/src/net/i2p/router/tunnel/InboundEndpointProcessor.java index a45b1dd8dd..1ea3cac8d9 100644 --- a/router/java/src/net/i2p/router/tunnel/InboundEndpointProcessor.java +++ b/router/java/src/net/i2p/router/tunnel/InboundEndpointProcessor.java @@ -1,6 +1,7 @@ package net.i2p.router.tunnel; import net.i2p.data.Hash; +import net.i2p.router.ProfileManager; import net.i2p.router.RouterContext; import net.i2p.util.Log; import net.i2p.util.SimpleByteCache; @@ -79,8 +80,13 @@ public boolean retrievePreprocessedData(byte orig[], int offset, int length, Has int rtt = 0; // dunno... may not be related to an rtt if (_log.shouldLog(Log.DEBUG)) _log.debug("Received a " + length + "byte message through tunnel " + _config); - for (int i = 0; i < _config.getLength(); i++) - _context.profileManager().tunnelDataPushed(_config.getPeer(i), rtt, length); + ProfileManager pm = _context.profileManager(); + // null for unit tests + if (pm != null) { + for (int i = 0; i < _config.getLength(); i++) { + pm.tunnelDataPushed(_config.getPeer(i), rtt, length); + } + } _config.incrementVerifiedBytesTransferred(length); } diff --git a/router/java/test/junit/net/i2p/router/tunnel/InboundIT.java b/router/java/test/junit/net/i2p/router/tunnel/InboundIT.java deleted file mode 100644 index 96bc3033ce..0000000000 --- a/router/java/test/junit/net/i2p/router/tunnel/InboundIT.java +++ /dev/null @@ -1,50 +0,0 @@ -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 org.junit.Test; - -import net.i2p.data.DataHelper; -import net.i2p.data.Hash; - -import static org.junit.Assert.assertTrue; - -/** - * Quick unit test for base functionality of inbound tunnel - * operation - * - */ -public class InboundIT extends RouterITBase { - - @Test - @SuppressWarnings("deprecation") - public void testInbound() { - int numHops = 8; - - byte orig[] = new byte[128]; - byte message[] = new byte[128]; - _context.random().nextBytes(orig); // might as well fill the IV - System.arraycopy(orig, 0, message, 0, message.length); - - InboundGatewayProcessor p = new InboundGatewayProcessor(_context, _config.getConfig(0)); - 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(); - assertTrue(hop.process(message, 0, message.length, prev)); - } - - InboundEndpointProcessor end = new InboundEndpointProcessor(_context, _config); - assertTrue(end.retrievePreprocessedData(message, 0, message.length, _config.getPeer(numHops-2))); - - assertTrue(DataHelper.eq(orig, 16, message, 16, orig.length - 16)); - } - -} diff --git a/router/java/test/junit/net/i2p/router/tunnel/InboundTest.java b/router/java/test/junit/net/i2p/router/tunnel/InboundTest.java new file mode 100644 index 0000000000..4f0b829555 --- /dev/null +++ b/router/java/test/junit/net/i2p/router/tunnel/InboundTest.java @@ -0,0 +1,89 @@ +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 junit.framework.TestCase; +import org.junit.Test; +import static org.junit.Assert.assertTrue; + +import net.i2p.data.DataHelper; +import net.i2p.data.Hash; +import net.i2p.router.RouterContext; + +/** + * Quick unit test for base functionality of inbound tunnel + * operation + * + */ +public class InboundTest extends TestCase { + private RouterContext _context; + + public void setUp() { + _context = new RouterContext(null); + } + + @Test + @SuppressWarnings("deprecation") + public void testInbound() { + int numHops = 8; + TunnelCreatorConfig config = prepareConfig(numHops); + + byte orig[] = new byte[128]; + byte message[] = new byte[128]; + _context.random().nextBytes(orig); // might as well fill the IV + System.arraycopy(orig, 0, message, 0, message.length); + + InboundGatewayProcessor p = new InboundGatewayProcessor(_context, config.getConfig(0)); + 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(); + assertTrue(hop.process(message, 0, message.length, prev)); + } + + InboundEndpointProcessor end = new InboundEndpointProcessor(_context, config); + assertTrue(end.retrievePreprocessedData(message, 0, message.length, config.getPeer(numHops-2))); + + 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 TCConfig(_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; + } +}