From 37909d51936318a73ddeb6f6f8bb965b210fd0b6 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 26 Jul 2018 09:16:32 +0100 Subject: [PATCH] acceptor test --- .../core/connection/ConnectionAcceptor.groovy | 2 +- .../UltrapeerConnectionManager.groovy | 2 + .../connection/ConnectionAcceptorTest.groovy | 114 ++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 core/src/test/groovy/com/muwire/core/connection/ConnectionAcceptorTest.groovy diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy index 26bc7080..80d720ef 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy @@ -65,7 +65,7 @@ class ConnectionAcceptor { private void acceptLoop() { while(true) { def incoming = acceptor.accept() - log.info("accepted connection from ${incoming.destination}") + log.info("accepted connection from ${incoming.destination.toBase32()}") switch(trustService.getLevel(incoming.destination)) { case TrustLevel.TRUSTED : break case TrustLevel.NEUTRAL : diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy index c16d04cc..3f597040 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -9,6 +9,8 @@ import net.i2p.data.Destination class UltrapeerConnectionManager extends ConnectionManager { final int maxPeers, maxLeafs + + UltrapeerConnectionManager() {} public UltrapeerConnectionManager(EventBus eventBus, int maxPeers, int maxLeafs) { super(eventBus) diff --git a/core/src/test/groovy/com/muwire/core/connection/ConnectionAcceptorTest.groovy b/core/src/test/groovy/com/muwire/core/connection/ConnectionAcceptorTest.groovy new file mode 100644 index 00000000..7abb0000 --- /dev/null +++ b/core/src/test/groovy/com/muwire/core/connection/ConnectionAcceptorTest.groovy @@ -0,0 +1,114 @@ +package com.muwire.core.connection + +import java.util.concurrent.CopyOnWriteArrayList + +import org.junit.After +import org.junit.Before +import org.junit.Test + +import com.muwire.core.Destinations +import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings +import com.muwire.core.hostcache.HostCache +import com.muwire.core.trust.TrustLevel +import com.muwire.core.trust.TrustService + +import groovy.mock.interceptor.MockFor + +class ConnectionAcceptorTest { + + EventBus eventBus + final Destinations destinations = new Destinations() + def settings + + def connectionManagerMock + UltrapeerConnectionManager connectionManager + + def i2pAcceptorMock + I2PAcceptor i2pAcceptor + + def hostCacheMock + HostCache hostCache + + def trustServiceMock + TrustService trustService + + ConnectionAcceptor acceptor + List connectionEvents + InputStream inputStream + OutputStream outputStream + + @Before + void before() { + connectionManagerMock = new MockFor(UltrapeerConnectionManager.class) + i2pAcceptorMock = new MockFor(I2PAcceptor.class) + hostCacheMock = new MockFor(HostCache.class) + trustServiceMock = new MockFor(TrustService.class) + } + + @After + void after() { + acceptor?.stop() + connectionManagerMock.verify connectionManager + i2pAcceptorMock.verify i2pAcceptor + hostCacheMock.verify hostCache + trustServiceMock.verify trustService + Thread.sleep(100) + } + + private void initMocks() { + connectionEvents = new CopyOnWriteArrayList() + eventBus = new EventBus() + def listener = new Object() { + void onConnectionEvent(ConnectionEvent e) { + connectionEvents.add e + } + } + eventBus.register(ConnectionEvent.class, listener) + + connectionManager = connectionManagerMock.proxyInstance() + i2pAcceptor = i2pAcceptorMock.proxyInstance() + hostCache = hostCacheMock.proxyInstance() + trustService = trustServiceMock.proxyInstance() + + acceptor = new ConnectionAcceptor(eventBus, connectionManager, settings, i2pAcceptor, hostCache, trustService) + acceptor.start() + Thread.sleep(100) + } + + @Test + void testSuccessfulLeaf() { + settings = new MuWireSettings() { + boolean isLeaf() { + false + } + } + i2pAcceptorMock.demand.accept { + def is = new PipedInputStream() + outputStream = new PipedOutputStream(is) + def os = new PipedOutputStream() + inputStream = new PipedInputStream(os) + new Endpoint(destinations.dest1, is, os) + } + i2pAcceptorMock.demand.accept { Thread.sleep(Integer.MAX_VALUE) } + connectionManagerMock.demand.hasLeafSlots() { true } + trustServiceMock.demand.getLevel { dest -> + assert dest == destinations.dest1 + TrustLevel.TRUSTED + } + + initMocks() + + outputStream.write("MuWire leaf".bytes) + byte [] OK = new byte[2] + def dis = new DataInputStream(inputStream) + dis.readFully(OK) + assert OK == "OK".bytes + + Thread.sleep(50) + assert connectionEvents.size() == 1 + def event = connectionEvents[0] + assert event.endpoint.destination == destinations.dest1 + assert event.status == ConnectionAttemptStatus.SUCCESSFUL + } +}