more tests and fixes
This commit is contained in:
@@ -101,8 +101,8 @@ class ConnectionEstablisher {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch(read) {
|
switch(read) {
|
||||||
case 'O': readK(endpoint); break
|
case (byte)'O': readK(endpoint); break
|
||||||
case 'R': readEJECT(endpoint); break
|
case (byte)'R': readEJECT(endpoint); break
|
||||||
default :
|
default :
|
||||||
log.warning("unknown response $read")
|
log.warning("unknown response $read")
|
||||||
fail endpoint
|
fail endpoint
|
||||||
@@ -156,7 +156,7 @@ class ConnectionEstablisher {
|
|||||||
byte[] payload = new byte[payloadSize]
|
byte[] payload = new byte[payloadSize]
|
||||||
dais.readFully(payload)
|
dais.readFully(payload)
|
||||||
|
|
||||||
JsonSlurper json = new JsonSlurper()
|
def json = new JsonSlurper()
|
||||||
json = json.parse(payload)
|
json = json.parse(payload)
|
||||||
|
|
||||||
if (json.tryHosts == null) {
|
if (json.tryHosts == null) {
|
||||||
@@ -172,7 +172,7 @@ class ConnectionEstablisher {
|
|||||||
log.log(Level.WARNING,"Problem parsing post-rejection payload",ignore)
|
log.log(Level.WARNING,"Problem parsing post-rejection payload",ignore)
|
||||||
} finally {
|
} finally {
|
||||||
// the end
|
// the end
|
||||||
e.closeQuietly()
|
e.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import com.muwire.core.Destinations
|
|||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
import com.muwire.core.MuWireSettings
|
import com.muwire.core.MuWireSettings
|
||||||
import com.muwire.core.hostcache.HostCache
|
import com.muwire.core.hostcache.HostCache
|
||||||
|
import com.muwire.core.hostcache.HostDiscoveredEvent
|
||||||
|
|
||||||
|
import groovy.json.JsonOutput
|
||||||
import groovy.mock.interceptor.MockFor
|
import groovy.mock.interceptor.MockFor
|
||||||
|
|
||||||
class ConnectionEstablisherTest {
|
class ConnectionEstablisherTest {
|
||||||
@@ -18,6 +20,9 @@ class ConnectionEstablisherTest {
|
|||||||
EventBus eventBus
|
EventBus eventBus
|
||||||
final Destinations destinations = new Destinations()
|
final Destinations destinations = new Destinations()
|
||||||
List<ConnectionEvent> connectionEvents
|
List<ConnectionEvent> connectionEvents
|
||||||
|
List<HostDiscoveredEvent> discoveredEvents
|
||||||
|
DataInputStream inputStream
|
||||||
|
DataOutputStream outputStream
|
||||||
|
|
||||||
def i2pConnectorMock
|
def i2pConnectorMock
|
||||||
I2PConnector i2pConnector
|
I2PConnector i2pConnector
|
||||||
@@ -36,13 +41,18 @@ class ConnectionEstablisherTest {
|
|||||||
@Before
|
@Before
|
||||||
void before() {
|
void before() {
|
||||||
connectionEvents = new CopyOnWriteArrayList()
|
connectionEvents = new CopyOnWriteArrayList()
|
||||||
|
discoveredEvents = new CopyOnWriteArrayList()
|
||||||
def listener = new Object() {
|
def listener = new Object() {
|
||||||
void onConnectionEvent(ConnectionEvent e) {
|
void onConnectionEvent(ConnectionEvent e) {
|
||||||
connectionEvents.add(e)
|
connectionEvents.add(e)
|
||||||
}
|
}
|
||||||
|
void onHostDiscoveredEvent(HostDiscoveredEvent e) {
|
||||||
|
discoveredEvents.add e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
eventBus = new EventBus()
|
eventBus = new EventBus()
|
||||||
eventBus.register(ConnectionEvent.class, listener)
|
eventBus.register(ConnectionEvent.class, listener)
|
||||||
|
eventBus.register(HostDiscoveredEvent.class, listener)
|
||||||
i2pConnectorMock = new MockFor(I2PConnector.class)
|
i2pConnectorMock = new MockFor(I2PConnector.class)
|
||||||
connectionManagerMock = new MockFor(ConnectionManager.class)
|
connectionManagerMock = new MockFor(ConnectionManager.class)
|
||||||
hostCacheMock = new MockFor(HostCache.class)
|
hostCacheMock = new MockFor(HostCache.class)
|
||||||
@@ -54,6 +64,7 @@ class ConnectionEstablisherTest {
|
|||||||
i2pConnectorMock.verify i2pConnector
|
i2pConnectorMock.verify i2pConnector
|
||||||
connectionManagerMock.verify connectionManager
|
connectionManagerMock.verify connectionManager
|
||||||
hostCacheMock.verify hostCache
|
hostCacheMock.verify hostCache
|
||||||
|
Thread.sleep(100)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initMocks() {
|
private void initMocks() {
|
||||||
@@ -68,6 +79,7 @@ class ConnectionEstablisherTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConnectFails() {
|
void testConnectFails() {
|
||||||
|
settings = new MuWireSettings()
|
||||||
connectionManagerMock.ignore.needsConnections {
|
connectionManagerMock.ignore.needsConnections {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -92,4 +104,184 @@ class ConnectionEstablisherTest {
|
|||||||
assert event.incoming == false
|
assert event.incoming == false
|
||||||
assert event.status == ConnectionAttemptStatus.FAILED
|
assert event.status == ConnectionAttemptStatus.FAILED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConnectionSucceedsPeer() {
|
||||||
|
settings = new MuWireSettings() {
|
||||||
|
boolean isLeaf() {false}
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.needsConnections {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
hostCacheMock.ignore.getHosts { num ->
|
||||||
|
assert num == 1
|
||||||
|
[destinations.dest1]
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.isConnected { dest ->
|
||||||
|
assert dest == destinations.dest1
|
||||||
|
false
|
||||||
|
}
|
||||||
|
i2pConnectorMock.demand.connect { dest ->
|
||||||
|
PipedOutputStream os = new PipedOutputStream()
|
||||||
|
inputStream = new DataInputStream(new PipedInputStream(os))
|
||||||
|
PipedInputStream is = new PipedInputStream()
|
||||||
|
outputStream = new DataOutputStream(new PipedOutputStream(is))
|
||||||
|
new Endpoint(dest, is, os)
|
||||||
|
}
|
||||||
|
|
||||||
|
initMocks()
|
||||||
|
|
||||||
|
byte [] header = new byte[11]
|
||||||
|
inputStream.readFully(header)
|
||||||
|
assert header == "MuWire peer".bytes
|
||||||
|
|
||||||
|
outputStream.write("OK".bytes)
|
||||||
|
outputStream.flush()
|
||||||
|
|
||||||
|
Thread.sleep(100)
|
||||||
|
|
||||||
|
assert connectionEvents.size() == 1
|
||||||
|
def event = connectionEvents[0]
|
||||||
|
assert event.endpoint.destination == destinations.dest1
|
||||||
|
assert event.incoming == false
|
||||||
|
assert event.status == ConnectionAttemptStatus.SUCCESSFUL
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConnectionSucceedsLeaf() {
|
||||||
|
settings = new MuWireSettings() {
|
||||||
|
boolean isLeaf() {true}
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.needsConnections {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
hostCacheMock.ignore.getHosts { num ->
|
||||||
|
assert num == 1
|
||||||
|
[destinations.dest1]
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.isConnected { dest ->
|
||||||
|
assert dest == destinations.dest1
|
||||||
|
false
|
||||||
|
}
|
||||||
|
i2pConnectorMock.demand.connect { dest ->
|
||||||
|
PipedOutputStream os = new PipedOutputStream()
|
||||||
|
inputStream = new DataInputStream(new PipedInputStream(os))
|
||||||
|
PipedInputStream is = new PipedInputStream()
|
||||||
|
outputStream = new DataOutputStream(new PipedOutputStream(is))
|
||||||
|
new Endpoint(dest, is, os)
|
||||||
|
}
|
||||||
|
|
||||||
|
initMocks()
|
||||||
|
|
||||||
|
byte [] header = new byte[11]
|
||||||
|
inputStream.readFully(header)
|
||||||
|
assert header == "MuWire leaf".bytes
|
||||||
|
|
||||||
|
outputStream.write("OK".bytes)
|
||||||
|
outputStream.flush()
|
||||||
|
|
||||||
|
Thread.sleep(100)
|
||||||
|
|
||||||
|
assert connectionEvents.size() == 1
|
||||||
|
def event = connectionEvents[0]
|
||||||
|
assert event.endpoint.destination == destinations.dest1
|
||||||
|
assert event.incoming == false
|
||||||
|
assert event.status == ConnectionAttemptStatus.SUCCESSFUL
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConnectionRejected() {
|
||||||
|
settings = new MuWireSettings() {
|
||||||
|
boolean isLeaf() {false}
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.needsConnections {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
hostCacheMock.ignore.getHosts { num ->
|
||||||
|
assert num == 1
|
||||||
|
[destinations.dest1]
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.isConnected { dest ->
|
||||||
|
assert dest == destinations.dest1
|
||||||
|
false
|
||||||
|
}
|
||||||
|
i2pConnectorMock.demand.connect { dest ->
|
||||||
|
PipedOutputStream os = new PipedOutputStream()
|
||||||
|
inputStream = new DataInputStream(new PipedInputStream(os))
|
||||||
|
PipedInputStream is = new PipedInputStream()
|
||||||
|
outputStream = new DataOutputStream(new PipedOutputStream(is))
|
||||||
|
new Endpoint(dest, is, os)
|
||||||
|
}
|
||||||
|
|
||||||
|
initMocks()
|
||||||
|
|
||||||
|
byte [] header = new byte[11]
|
||||||
|
inputStream.readFully(header)
|
||||||
|
assert header == "MuWire peer".bytes
|
||||||
|
|
||||||
|
outputStream.write("REJECT".bytes)
|
||||||
|
outputStream.flush()
|
||||||
|
|
||||||
|
Thread.sleep(100)
|
||||||
|
|
||||||
|
assert connectionEvents.size() == 1
|
||||||
|
def event = connectionEvents[0]
|
||||||
|
assert event.endpoint.destination == destinations.dest1
|
||||||
|
assert event.incoming == false
|
||||||
|
assert event.status == ConnectionAttemptStatus.REJECTED
|
||||||
|
assert discoveredEvents.isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConnectionRejectedSuggestions() {
|
||||||
|
settings = new MuWireSettings() {
|
||||||
|
boolean isLeaf() {false}
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.needsConnections {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
hostCacheMock.ignore.getHosts { num ->
|
||||||
|
assert num == 1
|
||||||
|
[destinations.dest1]
|
||||||
|
}
|
||||||
|
connectionManagerMock.ignore.isConnected { dest ->
|
||||||
|
assert dest == destinations.dest1
|
||||||
|
false
|
||||||
|
}
|
||||||
|
i2pConnectorMock.demand.connect { dest ->
|
||||||
|
PipedOutputStream os = new PipedOutputStream()
|
||||||
|
inputStream = new DataInputStream(new PipedInputStream(os))
|
||||||
|
PipedInputStream is = new PipedInputStream()
|
||||||
|
outputStream = new DataOutputStream(new PipedOutputStream(is))
|
||||||
|
new Endpoint(dest, is, os)
|
||||||
|
}
|
||||||
|
|
||||||
|
initMocks()
|
||||||
|
|
||||||
|
byte [] header = new byte[11]
|
||||||
|
inputStream.readFully(header)
|
||||||
|
assert header == "MuWire peer".bytes
|
||||||
|
|
||||||
|
outputStream.write("REJECT".bytes)
|
||||||
|
outputStream.flush()
|
||||||
|
|
||||||
|
def json = [:]
|
||||||
|
json.tryHosts = [destinations.dest2.toBase64()]
|
||||||
|
json = JsonOutput.toJson(json)
|
||||||
|
outputStream.writeShort(json.bytes.length)
|
||||||
|
outputStream.write(json.bytes)
|
||||||
|
outputStream.flush()
|
||||||
|
Thread.sleep(100)
|
||||||
|
|
||||||
|
assert connectionEvents.size() == 1
|
||||||
|
def event = connectionEvents[0]
|
||||||
|
assert event.endpoint.destination == destinations.dest1
|
||||||
|
assert event.incoming == false
|
||||||
|
assert event.status == ConnectionAttemptStatus.REJECTED
|
||||||
|
|
||||||
|
assert discoveredEvents.size() == 1
|
||||||
|
event = discoveredEvents[0]
|
||||||
|
assert event.destination == destinations.dest2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user