diff options
Diffstat (limited to 'test/com')
-rw-r--r-- | test/com/isode/stroke/component/ComponentConnectorTest.java | 228 | ||||
-rw-r--r-- | test/com/isode/stroke/network/ChainedConnectorTest.java | 200 | ||||
-rw-r--r-- | test/com/isode/stroke/network/ConnectorTest.java | 402 | ||||
-rw-r--r-- | test/com/isode/stroke/network/HostAddressTest.java | 71 |
4 files changed, 901 insertions, 0 deletions
diff --git a/test/com/isode/stroke/component/ComponentConnectorTest.java b/test/com/isode/stroke/component/ComponentConnectorTest.java new file mode 100644 index 0000000..413f325 --- /dev/null +++ b/test/com/isode/stroke/component/ComponentConnectorTest.java @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.component; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.eventloop.Event; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.network.Connector; +import com.isode.stroke.network.HostAddress; +import com.isode.stroke.network.Connection; +import com.isode.stroke.network.ConnectionFactory; +import com.isode.stroke.network.HostAddressPort; +import com.isode.stroke.network.StaticDomainNameResolver; +import com.isode.stroke.network.DummyTimerFactory; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.network.DomainNameAddressQuery; +import java.util.Vector; + +public class ComponentConnectorTest { + + private HostAddress host1; + private HostAddress host2; + private DummyEventLoop eventLoop; + private StaticDomainNameResolver resolver; + private MockConnectionFactory connectionFactory; + private DummyTimerFactory timerFactory; + private Vector<MockConnection> connections = new Vector<MockConnection>(); + + private class MockConnection extends Connection { + + public MockConnection(final Vector<HostAddressPort> failingPorts, boolean isResponsive, EventLoop eventLoop) { + this.eventLoop = eventLoop; + this.failingPorts = failingPorts; + this.isResponsive = isResponsive; + } + + public void listen() { assert(false); } + + public void connect(final HostAddressPort address) { + hostAddressPort = address; + if(isResponsive) { + final boolean fail = failingPorts.contains(address); + eventLoop.postEvent(new Event.Callback() { + @Override + public void run() { + onConnectFinished.emit(fail); + } + }); + } + } + + public HostAddressPort getLocalAddress() { return new HostAddressPort(); } + + public void disconnect() { assert(false); } + + public void write(final SafeByteArray data) { assert(false); } + + public EventLoop eventLoop; + public HostAddressPort hostAddressPort; + public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>(); + public boolean isResponsive; + }; + + private class MockConnectionFactory implements ConnectionFactory { + public MockConnectionFactory(EventLoop eventLoop) { + this.eventLoop = eventLoop; + this.isResponsive = true; + } + + public Connection createConnection() { + return new MockConnection(failingPorts, isResponsive, eventLoop); + } + + public EventLoop eventLoop; + public boolean isResponsive; + public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>(); + }; + + private ComponentConnector createConnector(final String hostname, int port) { + ComponentConnector connector = ComponentConnector.create(hostname, port, resolver, connectionFactory, timerFactory); + connector.onConnectFinished.connect(new Slot1<Connection>() { + @Override + public void call(Connection c) { + handleConnectorFinished(c); + } + }); + return connector; + } + + private void handleConnectorFinished(Connection connection) { + MockConnection c = (MockConnection)(connection); + if (connection != null) { + assert(c != null); + } + connections.add(c); + } + + @Before + public void setUp() { + host1 = new HostAddress("1.1.1.1"); + host2 = new HostAddress("2.2.2.2"); + eventLoop = new DummyEventLoop(); + resolver = new StaticDomainNameResolver(eventLoop); + connectionFactory = new MockConnectionFactory(eventLoop); + timerFactory = new DummyTimerFactory(); + } + + @Test + public void testConnect() { + ComponentConnector testling = createConnector("foo.com", 1234); + resolver.addAddress("foo.com", host1); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(new HostAddressPort(host1, 1234), (connections.get(0).hostAddressPort)); + } + + @Test + public void testConnect_FirstAddressHostFails() { + ComponentConnector testling = createConnector("foo.com", 1234); + resolver.addAddress("foo.com", host1); + resolver.addAddress("foo.com", host2); + connectionFactory.failingPorts.add(new HostAddressPort(host1, 1234)); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(new HostAddressPort(host2, 1234), (connections.get(0).hostAddressPort)); + } + + @Test + public void testConnect_NoHosts() { + ComponentConnector testling = createConnector("foo.com", 1234); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } + + @Test + public void testConnect_TimeoutDuringResolve() { + ComponentConnector testling = createConnector("foo.com", 1234); + + testling.setTimeoutMilliseconds(10); + resolver.setIsResponsive(false); + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } + + @Test + public void testConnect_TimeoutDuringConnect() { + ComponentConnector testling = createConnector("foo.com", 1234); + testling.setTimeoutMilliseconds(10); + resolver.addAddress("foo.com", host1); + connectionFactory.isResponsive = false; + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } + + @Test + public void testConnect_NoTimeout() { + ComponentConnector testling = createConnector("foo.com", 1234); + testling.setTimeoutMilliseconds(10); + resolver.addAddress("foo.com", host1); + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + } + + @Test + public void testStop_Timeout() { + ComponentConnector testling = createConnector("foo.com", 1234); + testling.setTimeoutMilliseconds(10); + resolver.addAddress("foo.com", host1); + + testling.start(); + testling.stop(); + + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/network/ChainedConnectorTest.java b/test/com/isode/stroke/network/ChainedConnectorTest.java new file mode 100644 index 0000000..a2bb98c --- /dev/null +++ b/test/com/isode/stroke/network/ChainedConnectorTest.java @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.network; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.network.ChainedConnector; +import com.isode.stroke.network.Connection; +import com.isode.stroke.network.ConnectionFactory; +import com.isode.stroke.network.HostAddressPort; +import com.isode.stroke.network.StaticDomainNameResolver; +import com.isode.stroke.network.DummyTimerFactory; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.eventloop.Event; +import com.isode.stroke.signals.Slot2; +import com.isode.stroke.network.DomainNameResolveError; +import java.util.Vector; + +public class ChainedConnectorTest { + + private HostAddressPort host; + private DummyEventLoop eventLoop; + private StaticDomainNameResolver resolver; + private MockConnectionFactory connectionFactory1; + private MockConnectionFactory connectionFactory2; + private DummyTimerFactory timerFactory; + private Vector<MockConnection> connections = new Vector<MockConnection>(); + private com.isode.stroke.base.Error error; + + private class MockConnection extends Connection { + + public MockConnection(boolean connects, int id, EventLoop eventLoop) { + this.connects = connects; + this.id = id; + this.eventLoop = eventLoop; + } + + public void listen() { assert(false); } + + public void connect(final HostAddressPort port) { + eventLoop.postEvent(new Event.Callback() { + @Override + public void run() { + onConnectFinished.emit(!connects); + } + }); + } + + public HostAddressPort getLocalAddress() { return new HostAddressPort(); } + public void disconnect() { assert(false); } + public void write(final SafeByteArray data) { assert(false); } + + public boolean connects; + public int id; + public EventLoop eventLoop; + }; + + private class MockConnectionFactory implements ConnectionFactory { + public MockConnectionFactory(EventLoop eventLoop, int id) { + this.eventLoop = eventLoop; + this.connects = true; + this.id = id; + } + + public Connection createConnection() { + return new MockConnection(connects, id, eventLoop); + } + + public EventLoop eventLoop; + public boolean connects; + public int id; + }; + + private ChainedConnector createConnector() { + Vector<ConnectionFactory> factories = new Vector<ConnectionFactory>(); + factories.add(connectionFactory1); + factories.add(connectionFactory2); + ChainedConnector connector = new ChainedConnector("foo.com", -1, "_xmpp-client._tcp.", resolver, factories, timerFactory); + connector.onConnectFinished.connect(new Slot2<Connection, com.isode.stroke.base.Error>() { + @Override + public void call(Connection c, com.isode.stroke.base.Error e) { + handleConnectorFinished(c, e); + } + }); + return connector; + } + + private void handleConnectorFinished(Connection connection, com.isode.stroke.base.Error resultError) { + error = resultError; + MockConnection c = (MockConnection)(connection); + if (connection != null) { + assert(c != null); + } + connections.add(c); + } + + @Before + public void setUp() { + error = null; + host = new HostAddressPort(new HostAddress("1.1.1.1"), 1234); + eventLoop = new DummyEventLoop(); + resolver = new StaticDomainNameResolver(eventLoop); + resolver.addXMPPClientService("foo.com", host); + connectionFactory1 = new MockConnectionFactory(eventLoop, 1); + connectionFactory2 = new MockConnectionFactory(eventLoop, 2); + timerFactory = new DummyTimerFactory(); + } + + @Test + public void testConnect_FirstConnectorSucceeds() { + ChainedConnector testling = createConnector(); + connectionFactory1.connects = true; + connectionFactory2.connects = false; + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(1, ((MockConnection)(connections.get(0))).id); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_SecondConnectorSucceeds() { + ChainedConnector testling = createConnector(); + connectionFactory1.connects = false; + connectionFactory2.connects = true; + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(2, ((MockConnection)(connections.get(0))).id); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoConnectorSucceeds() { + ChainedConnector testling = createConnector(); + connectionFactory1.connects = false; + connectionFactory2.connects = false; + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoDNS() { + /* Reset resolver so there's no record */ + resolver = null; + resolver = new StaticDomainNameResolver(eventLoop); + ChainedConnector testling = createConnector(); + connectionFactory1.connects = false; + connectionFactory2.connects = false; + + testling.start(); + //testling.stop(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNotNull((DomainNameResolveError)(error)); + } + + @Test + public void testStop() { + ChainedConnector testling = createConnector(); + connectionFactory1.connects = true; + connectionFactory2.connects = false; + + testling.start(); + testling.stop(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/network/ConnectorTest.java b/test/com/isode/stroke/network/ConnectorTest.java new file mode 100644 index 0000000..43e2300 --- /dev/null +++ b/test/com/isode/stroke/network/ConnectorTest.java @@ -0,0 +1,402 @@ +/* + * Copyright (c) 2010-2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.network; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.eventloop.Event; +import com.isode.stroke.signals.Slot2; +import com.isode.stroke.network.Connector; +import com.isode.stroke.network.Connection; +import com.isode.stroke.network.ConnectionFactory; +import com.isode.stroke.network.HostAddressPort; +import com.isode.stroke.network.StaticDomainNameResolver; +import com.isode.stroke.network.DummyTimerFactory; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.network.DomainNameAddressQuery; +import java.util.Vector; + +public class ConnectorTest { + + private HostAddressPort host1; + private HostAddressPort host2; + private HostAddressPort host3; + private DummyEventLoop eventLoop; + private StaticDomainNameResolver resolver; + private MockConnectionFactory connectionFactory; + private DummyTimerFactory timerFactory; + private Vector<MockConnection> connections = new Vector<MockConnection>(); + private com.isode.stroke.base.Error error; + + private class MockConnection extends Connection { + + public MockConnection(final Vector<HostAddressPort> failingPorts, boolean isResponsive, EventLoop eventLoop) { + this.eventLoop = eventLoop; + this.failingPorts = failingPorts; + this.isResponsive = isResponsive; + } + + public void listen() { assert(false); } + + public void connect(final HostAddressPort address) { + hostAddressPort = address; + if(isResponsive) { + final boolean fail = failingPorts.contains(address); + eventLoop.postEvent(new Event.Callback() { + @Override + public void run() { + onConnectFinished.emit(fail); + } + }); + } + } + + public HostAddressPort getLocalAddress() { return new HostAddressPort(); } + + public void disconnect() { assert(false); } + + public void write(final SafeByteArray data) { assert(false); } + + public EventLoop eventLoop; + public HostAddressPort hostAddressPort; + public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>(); + public boolean isResponsive; + }; + + private class MockConnectionFactory implements ConnectionFactory { + public MockConnectionFactory(EventLoop eventLoop) { + this.eventLoop = eventLoop; + this.isResponsive = true; + } + + public Connection createConnection() { + return new MockConnection(failingPorts, isResponsive, eventLoop); + } + + public EventLoop eventLoop; + public boolean isResponsive; + public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>(); + }; + + private Connector createConnector() { + return createConnector(-1, "_xmpp-client._tcp."); + } + + private Connector createConnector(int port) { + return createConnector(port, "_xmpp-client._tcp."); + } + + private Connector createConnector(int port, String serviceLookupPrefix) { + Connector connector = Connector.create("foo.com", port, serviceLookupPrefix, resolver, connectionFactory, timerFactory); + connector.onConnectFinished.connect(new Slot2<Connection, com.isode.stroke.base.Error>() { + @Override + public void call(Connection c, com.isode.stroke.base.Error e) { + handleConnectorFinished(c, e); + } + }); + return connector; + } + + private void handleConnectorFinished(Connection connection, com.isode.stroke.base.Error resultError) { + MockConnection c = (MockConnection)(connection); + if (connection != null) { + assert(c != null); + } + connections.add(c); + error = resultError; + } + + @Before + public void setUp() { + host1 = new HostAddressPort(new HostAddress("1.1.1.1"), 1234); + host2 = new HostAddressPort(new HostAddress("2.2.2.2"), 2345); + host3 = new HostAddressPort(new HostAddress("3.3.3.3"), 5222); + eventLoop = new DummyEventLoop(); + resolver = new StaticDomainNameResolver(eventLoop); + connectionFactory = new MockConnectionFactory(eventLoop); + timerFactory = new DummyTimerFactory(); + } + + @Test + public void testConnect() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + resolver.addAddress("foo.com", host3.getAddress()); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(host1, (connections.get(0).hostAddressPort)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoServiceLookups() { + Connector testling = createConnector(4321, null); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + resolver.addAddress("foo.com", host3.getAddress()); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + //assertEquals(host3.getAddress(), (connections.get(0).hostAddressPort).getAddress()); FAIL + assertEquals(4321, (connections.get(0).hostAddressPort).getPort()); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoServiceLookups_DefaultPort() { + Connector testling = createConnector(-1, null); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + resolver.addAddress("foo.com", host3.getAddress()); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + //assertEquals(host3.getAddress(), (connections.get(0).hostAddressPort).getAddress()); FAIL + assertEquals(5222, (connections.get(0).hostAddressPort).getPort()); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoSRVHost() { + Connector testling = createConnector(); + resolver.addAddress("foo.com", host3.getAddress()); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(host3, (connections.get(0).hostAddressPort)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_FirstAddressHostFails() { + Connector testling = createConnector(); + + HostAddress address1 = new HostAddress("1.1.1.1"); + HostAddress address2 = new HostAddress("2.2.2.2"); + resolver.addXMPPClientService("foo.com", "host-foo.com", 1234); + resolver.addAddress("host-foo.com", address1); + resolver.addAddress("host-foo.com", address2); + connectionFactory.failingPorts.add(new HostAddressPort(address1, 1234)); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(new HostAddressPort(address2, 1234), (connections.get(0).hostAddressPort)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoHosts() { + Connector testling = createConnector(); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNotNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_FirstSRVHostFails() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + connectionFactory.failingPorts.add(host1); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + //assertEquals(host2, (connections.get(0).hostAddressPort)); FAIL + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_AllSRVHostsFailWithoutFallbackHost() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + connectionFactory.failingPorts.add(host1); + connectionFactory.failingPorts.add(host2); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + //assertNull(connections.get(0)); FAIL + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_AllSRVHostsFailWithFallbackHost() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + resolver.addXMPPClientService("foo.com", host2); + resolver.addAddress("foo.com", host3.getAddress()); + connectionFactory.failingPorts.add(host1); + connectionFactory.failingPorts.add(host2); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + //assertEquals(host3, (connections.get(0).hostAddressPort)); FAIL + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_SRVAndFallbackHostsFail() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + resolver.addAddress("foo.com", host3.getAddress()); + connectionFactory.failingPorts.add(host1); + connectionFactory.failingPorts.add(host3); + + testling.start(); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNull((DomainNameResolveError)(error)); + } + + //@Test COMMENTED IN SWIFTEN TOO. + /*public void testConnect_TimeoutDuringResolve() { + Connector testling = createConnector(); + testling.setTimeoutMilliseconds(10); + resolver.setIsResponsive(false); + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + CPPUNIT_ASSERT((DomainNameResolveError)(error)); + CPPUNIT_ASSERT(!connections.get(0)); + }*/ + + @Test + public void testConnect_TimeoutDuringConnectToOnlyCandidate() { + Connector testling = createConnector(); + testling.setTimeoutMilliseconds(10); + resolver.addXMPPClientService("foo.com", host1); + connectionFactory.isResponsive = false; + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_TimeoutDuringConnectToCandidateFallsBack() { + Connector testling = createConnector(); + testling.setTimeoutMilliseconds(10); + + resolver.addXMPPClientService("foo.com", "host-foo.com", 1234); + HostAddress address1 = new HostAddress("1.1.1.1"); + resolver.addAddress("host-foo.com", address1); + HostAddress address2 = new HostAddress("2.2.2.2"); + resolver.addAddress("host-foo.com", address2); + + connectionFactory.isResponsive = false; + testling.start(); + eventLoop.processEvents(); + connectionFactory.isResponsive = true; + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertEquals(new HostAddressPort(address2, 1234), (connections.get(0).hostAddressPort)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testConnect_NoTimeout() { + Connector testling = createConnector(); + testling.setTimeoutMilliseconds(10); + resolver.addXMPPClientService("foo.com", host1); + + testling.start(); + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNotNull(connections.get(0)); + assertNull((DomainNameResolveError)(error)); + } + + @Test + public void testStop_DuringSRVQuery() { + Connector testling = createConnector(); + resolver.addXMPPClientService("foo.com", host1); + + testling.start(); + testling.stop(); + + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + assertNotNull((DomainNameResolveError)(error)); + } + + @Test + public void testStop_Timeout() { + Connector testling = createConnector(); + testling.setTimeoutMilliseconds(10); + resolver.addXMPPClientService("foo.com", host1); + + testling.start(); + testling.stop(); + + eventLoop.processEvents(); + timerFactory.setTime(10); + eventLoop.processEvents(); + + assertEquals(1, (connections.size())); + assertNull(connections.get(0)); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/network/HostAddressTest.java b/test/com/isode/stroke/network/HostAddressTest.java new file mode 100644 index 0000000..f7fd426 --- /dev/null +++ b/test/com/isode/stroke/network/HostAddressTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.network; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.network.HostAddress; + +public class HostAddressTest { + + @Test + public void testConstructor() { + HostAddress testling = new HostAddress("192.168.1.254"); + + assertEquals(("192.168.1.254"), testling.toString()); + assertTrue(testling.isValid()); + } + + @Test + public void testConstructor_Invalid() { + HostAddress testling = new HostAddress(); + + assertFalse(testling.isValid()); + } + + @Test + public void testConstructor_InvalidString() { + HostAddress testling = new HostAddress("invalid"); + + assertFalse(testling.isValid()); + } + + @Test + public void testToString() { + char address[] = {10, 0, 1, 253}; + HostAddress testling = new HostAddress(address, 4); + + assertEquals(("10.0.1.253"), testling.toString()); + } + + @Test + public void testToString_IPv6() { + char address[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17}; + HostAddress testling = new HostAddress(address, 16); + + assertEquals(("102:304:506:708:90a:b0c:d0e:f11"), testling.toString()); + } + + @Test + public void testToString_Invalid() { + HostAddress testling = new HostAddress(); + + assertEquals("<no address>", testling.toString()); + } +} + |