diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-02 09:55:38 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-21 09:26:58 (GMT) |
commit | e98df2cfcd3bc553b16c139a7d2adfcbe672b540 (patch) | |
tree | 449b58d27884ae4fa7441cea3c3ee7c9f0410dd4 /test/com/isode | |
parent | 43b51821ff8d03aa55209c66ea3e26080cf3cb8c (diff) | |
download | stroke-e98df2cfcd3bc553b16c139a7d2adfcbe672b540.zip stroke-e98df2cfcd3bc553b16c139a7d2adfcbe672b540.tar.bz2 |
Adds Disco Features.
Adds DiscoServiceWalker, FeatureOracle, JIDDiscoInfoResponder.
Updates CapsInfoGenerator, ClientDiscoManager,
EntityCapsManager, EntityCapsProvider, IQ Element, Request.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Tests added for:
CapsInfoGenerator, CapsManager, DiscoInfoResponder, EntityCapsManager, JIDDiscoInfoResponder.
All tests pass.
Change-Id: Ib7cd08ff6f72b7649e4819943b627459c69a1397
Diffstat (limited to 'test/com/isode')
-rw-r--r-- | test/com/isode/stroke/disco/CapsInfoGeneratorTest.java | 93 | ||||
-rw-r--r-- | test/com/isode/stroke/disco/CapsManagerTest.java | 290 | ||||
-rw-r--r-- | test/com/isode/stroke/disco/DiscoInfoResponderTest.java | 102 | ||||
-rw-r--r-- | test/com/isode/stroke/disco/EntityCapsManagerTest.java | 204 | ||||
-rw-r--r-- | test/com/isode/stroke/disco/JIDDiscoInfoResponderTest.java | 121 |
5 files changed, 810 insertions, 0 deletions
diff --git a/test/com/isode/stroke/disco/CapsInfoGeneratorTest.java b/test/com/isode/stroke/disco/CapsInfoGeneratorTest.java new file mode 100644 index 0000000..f718daf --- /dev/null +++ b/test/com/isode/stroke/disco/CapsInfoGeneratorTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2010-2013 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.disco; + +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.elements.DiscoInfo; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.CapsInfo; +import com.isode.stroke.disco.CapsInfoGenerator; +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; + +public class CapsInfoGeneratorTest { + + private CryptoProvider crypto; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + } + + @Test + public void testGenerate_XEP0115SimpleExample() { + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addIdentity(new DiscoInfo.Identity("Exodus 0.9.1", "client", "pc")); + discoInfo.addFeature("http://jabber.org/protocol/disco#items"); + discoInfo.addFeature("http://jabber.org/protocol/caps"); + discoInfo.addFeature("http://jabber.org/protocol/disco#info"); + discoInfo.addFeature("http://jabber.org/protocol/muc"); + + CapsInfoGenerator testling = new CapsInfoGenerator("http://code.google.com/p/exodus", crypto); + CapsInfo result = testling.generateCapsInfo(discoInfo); + + assertEquals("http://code.google.com/p/exodus", result.getNode()); + assertEquals("sha-1", result.getHash()); + assertEquals("QgayPKawpkPSDYmwT/WM94uAlu0=", result.getVersion()); + } + + @Test + public void testGenerate_XEP0115ComplexExample() { + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addIdentity(new DiscoInfo.Identity("Psi 0.11", "client", "pc", "en")); + discoInfo.addIdentity(new DiscoInfo.Identity("Ψ 0.11", "client", "pc", "el")); + discoInfo.addFeature("http://jabber.org/protocol/disco#items"); + discoInfo.addFeature("http://jabber.org/protocol/caps"); + discoInfo.addFeature("http://jabber.org/protocol/disco#info"); + discoInfo.addFeature("http://jabber.org/protocol/muc"); + + Form extension = new Form(Form.Type.RESULT_TYPE); + FormField field = new FormField(FormField.Type.HIDDEN_TYPE, "urn:xmpp:dataforms:softwareinfo"); + field.setName("FORM_TYPE"); + extension.addField(field); + field = new FormField(FormField.Type.LIST_MULTI_TYPE); + field.addValue("ipv6"); + field.addValue("ipv4"); + field.setName("ip_version"); + extension.addField(field); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Psi"); + field.setName("software"); + extension.addField(field); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "0.11"); + field.setName("software_version"); + extension.addField(field); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Mac"); + field.setName("os"); + extension.addField(field); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "10.5.1"); + field.setName("os_version"); + extension.addField(field); + discoInfo.addExtension(extension); + + CapsInfoGenerator testling = new CapsInfoGenerator("http://psi-im.org", crypto); + CapsInfo result = testling.generateCapsInfo(discoInfo); + + assertEquals("q07IKJEyjvHSyhy//CH0CxmKi8w=", result.getVersion()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/disco/CapsManagerTest.java b/test/com/isode/stroke/disco/CapsManagerTest.java new file mode 100644 index 0000000..c567eb8 --- /dev/null +++ b/test/com/isode/stroke/disco/CapsManagerTest.java @@ -0,0 +1,290 @@ +/* + * Copyright (c) 2010-2013 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.disco; + +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.elements.DiscoInfo; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.CapsInfo; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.disco.CapsInfoGenerator; +import com.isode.stroke.disco.CapsMemoryStorage; +import com.isode.stroke.disco.CapsManager; +import com.isode.stroke.jid.JID; +import java.util.Vector; +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; + +public class CapsManagerTest { + + private DummyStanzaChannel stanzaChannel; + private IQRouter iqRouter; + private CapsStorage storage; + private Vector<JID> changes = new Vector<JID>(); + private JID user1; + private DiscoInfo discoInfo1; + private CapsInfo capsInfo1; + private CapsInfo capsInfo1alt; + private JID user2; + private DiscoInfo discoInfo2; + private CapsInfo capsInfo2; + private CapsInfo legacyCapsInfo; + private JID user3; + private CryptoProvider crypto; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + stanzaChannel = new DummyStanzaChannel(); + iqRouter = new IQRouter(stanzaChannel); + storage = new CapsMemoryStorage(); + user1 = new JID("user1@bar.com/bla"); + discoInfo1 = new DiscoInfo(); + discoInfo1.addFeature("http://swift.im/feature1"); + capsInfo1 = new CapsInfoGenerator("http://node1.im", crypto).generateCapsInfo(discoInfo1); + capsInfo1alt = new CapsInfoGenerator("http://node2.im", crypto).generateCapsInfo(discoInfo1); + user2 = new JID("user2@foo.com/baz"); + discoInfo2 = new DiscoInfo(); + discoInfo2.addFeature("http://swift.im/feature2"); + capsInfo2 = new CapsInfoGenerator("http://node2.im", crypto).generateCapsInfo(discoInfo2); + user3 = new JID("user3@foo.com/baz"); + legacyCapsInfo = new CapsInfo("http://swift.im", "ver1", ""); + } + + private CapsManager createManager() { + CapsManager manager = new CapsManager(storage, stanzaChannel, iqRouter, crypto); + manager.setWarnOnInvalidHash(false); + //manager.onCapsChanged.connect(boost::bind(&CapsManagerTest::handleCapsChanged, this, _1)); + return manager; + } + + private void handleCapsChanged(JID jid) { + changes.add(jid); + } + + private void sendPresenceWithCaps(JID jid, CapsInfo caps) { + Presence presence = new Presence(); + presence.setFrom(jid); + presence.addPayload(caps); + stanzaChannel.onPresenceReceived.emit(presence); + } + + private void sendDiscoInfoResult(DiscoInfo discoInfo) { + stanzaChannel.onIQReceived.emit(IQ.createResult(new JID("baz@fum.com/dum"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), discoInfo)); + } + + @Test + public void testReceiveNewHashRequestsDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + + assertTrue(stanzaChannel.isRequestAtIndex(0, user1, IQ.Type.Get, new DiscoInfo())); + DiscoInfo discoInfo = stanzaChannel.sentStanzas.get(0).getPayload(new DiscoInfo()); + assertNotNull(discoInfo); + assertEquals("http://node1.im#" + capsInfo1.getVersion(), discoInfo.getNode()); + } + + @Test + public void testReceiveSameHashDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReceiveLegacyCapsDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, legacyCapsInfo); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReceiveSameHashAfterSuccesfulDiscoDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendDiscoInfoResult(discoInfo1); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReceiveSameHashFromSameUserAfterFailedDiscoDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getID())); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReceiveSameHashFromSameUserAfterIncorrectVerificationDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendDiscoInfoResult(discoInfo2); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReceiveSameHashFromDifferentUserAfterFailedDiscoRequestsDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user2, capsInfo1); + assertTrue(stanzaChannel.isRequestAtIndex(0, user2, IQ.Type.Get, new DiscoInfo())); + } + + @Test + public void testReceiveSameHashFromDifferentUserAfterIncorrectVerificationRequestsDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendDiscoInfoResult(discoInfo2); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user2, capsInfo1); + assertTrue(stanzaChannel.isRequestAtIndex(0, user2, IQ.Type.Get, new DiscoInfo())); + } + + @Test + public void testReceiveDifferentHashFromSameUserAfterFailedDiscoDoesNotRequestDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getID())); + + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo2); + + assertTrue(stanzaChannel.isRequestAtIndex(0, user1, IQ.Type.Get, new DiscoInfo())); + } + + @Test + public void testReceiveSuccesfulDiscoStoresCaps() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendDiscoInfoResult(discoInfo1); + + DiscoInfo discoInfo = storage.getDiscoInfo(capsInfo1.getVersion()); + assertNotNull(discoInfo); + assertTrue(discoInfo.hasFeature("http://swift.im/feature1")); + } + + @Test + public void testReceiveIncorrectVerificationDiscoDoesNotStoreCaps() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendDiscoInfoResult(discoInfo2); + + DiscoInfo discoInfo = storage.getDiscoInfo(capsInfo1.getVersion()); + assertNull(discoInfo); + } + + @Test + public void testReceiveFailingDiscoFallsBack() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendPresenceWithCaps(user2, capsInfo1alt); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + + assertTrue(stanzaChannel.isRequestAtIndex(1, user2, IQ.Type.Get, new DiscoInfo())); + DiscoInfo discoInfo = stanzaChannel.sentStanzas.get(1).getPayload(new DiscoInfo()); + assertNotNull(discoInfo); + assertEquals("http://node2.im#" + capsInfo1alt.getVersion(), discoInfo.getNode()); + } + + @Test + public void testReceiveNoDiscoFallsBack() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendPresenceWithCaps(user2, capsInfo1alt); + stanzaChannel.onIQReceived.emit(IQ.createResult(new JID("baz@fum.com/dum"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), new DiscoInfo())); + + assertTrue(stanzaChannel.isRequestAtIndex(1, user2, IQ.Type.Get, new DiscoInfo())); + DiscoInfo discoInfo = stanzaChannel.sentStanzas.get(1).getPayload(new DiscoInfo()); + assertNotNull(discoInfo); + assertEquals("http://node2.im#" + capsInfo1alt.getVersion(), discoInfo.getNode()); + } + + @Test + public void testReceiveFailingFallbackDiscoFallsBack() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendPresenceWithCaps(user2, capsInfo1alt); + sendPresenceWithCaps(user3, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(1).getTo(), stanzaChannel.sentStanzas.get(1).getID())); + + assertTrue(stanzaChannel.isRequestAtIndex(2, user3, IQ.Type.Get, new DiscoInfo())); + } + + @Test + public void testReceiveSameHashFromFailingUserAfterReconnectRequestsDisco() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + stanzaChannel.sentStanzas.clear(); + + sendPresenceWithCaps(user1, capsInfo1); + + assertTrue(stanzaChannel.isRequestAtIndex(0, user1, IQ.Type.Get, new DiscoInfo())); + } + + @Test + public void testReconnectResetsFallback() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + sendPresenceWithCaps(user2, capsInfo1alt); + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + stanzaChannel.sentStanzas.clear(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + + assertEquals(1, stanzaChannel.sentStanzas.size()); + } + + @Test + public void testReconnectResetsRequests() { + CapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + stanzaChannel.sentStanzas.clear(); + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + sendPresenceWithCaps(user1, capsInfo1); + + assertTrue(stanzaChannel.isRequestAtIndex(0, user1, IQ.Type.Get, new DiscoInfo())); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/disco/DiscoInfoResponderTest.java b/test/com/isode/stroke/disco/DiscoInfoResponderTest.java new file mode 100644 index 0000000..4a956d1 --- /dev/null +++ b/test/com/isode/stroke/disco/DiscoInfoResponderTest.java @@ -0,0 +1,102 @@ +/* + * 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.disco; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.DiscoInfo; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.queries.DummyIQChannel; +import com.isode.stroke.disco.DiscoInfoResponder; +import com.isode.stroke.jid.JID; +import java.util.Vector; +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; + +public class DiscoInfoResponderTest { + + private IQRouter router_; + private DummyIQChannel channel_; + + @Before + public void setUp() { + channel_ = new DummyIQChannel(); + router_ = new IQRouter(channel_); + } + + @Test + public void testHandleRequest_GetToplevelInfo() { + DiscoInfoResponder testling = new DiscoInfoResponder(router_); + testling.start(); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(discoInfo); + + DiscoInfo query = new DiscoInfo(); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + DiscoInfo payload = channel_.iqs_.get(0).getPayload(new DiscoInfo()); + assertNotNull(payload); + assertEquals("", payload.getNode()); + assertTrue(payload.hasFeature("foo")); + + testling.stop(); + } + + + @Test + public void testHandleRequest_GetNodeInfo() { + DiscoInfoResponder testling = new DiscoInfoResponder(router_); + testling.start(); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(discoInfo); + DiscoInfo discoInfoBar = new DiscoInfo(); + discoInfoBar.addFeature("bar"); + testling.setDiscoInfo("bar-node", discoInfoBar); + + DiscoInfo query = new DiscoInfo(); + query.setNode("bar-node"); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + DiscoInfo payload = channel_.iqs_.get(0).getPayload(new DiscoInfo()); + assertNotNull(payload); + assertEquals("bar-node", payload.getNode()); + assertTrue(payload.hasFeature("bar")); + + testling.stop(); + } + + + @Test + public void testHandleRequest_GetInvalidNodeInfo() { + DiscoInfoResponder testling = new DiscoInfoResponder(router_); + DiscoInfo query = new DiscoInfo(); + query.setNode("bar-node"); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com"), "id-1", query)); + testling.start(); + + assertEquals(1, channel_.iqs_.size()); + ErrorPayload payload = channel_.iqs_.get(0).getPayload(new ErrorPayload()); + assertNotNull(payload); + + testling.stop(); + } + +}
\ No newline at end of file diff --git a/test/com/isode/stroke/disco/EntityCapsManagerTest.java b/test/com/isode/stroke/disco/EntityCapsManagerTest.java new file mode 100644 index 0000000..a365bdb --- /dev/null +++ b/test/com/isode/stroke/disco/EntityCapsManagerTest.java @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2010-2013 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.disco; + +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.elements.CapsInfo; +import com.isode.stroke.elements.DiscoInfo; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.disco.EntityCapsManager; +import com.isode.stroke.disco.CapsInfoGenerator; +import com.isode.stroke.disco.CapsProvider; +import com.isode.stroke.signals.Signal1; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.jid.JID; +import java.util.Vector; +import java.util.Map; +import java.util.HashMap; +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; + +public class EntityCapsManagerTest { + + private DummyStanzaChannel stanzaChannel; + private DummyCapsProvider capsProvider; + private JID user1; + private DiscoInfo discoInfo1; + private CapsInfo capsInfo1; + private CapsInfo capsInfo1alt; + private JID user2; + private DiscoInfo discoInfo2; + private CapsInfo capsInfo2; + private CapsInfo legacyCapsInfo; + private JID user3; + private Vector<JID> changes = new Vector<JID>(); + private CryptoProvider crypto; + private SignalConnection onCapsChangedConnection; + + private class DummyCapsProvider extends CapsProvider { + public DiscoInfo getCaps(String hash) { + if(caps.containsKey(hash)) { + return caps.get(hash); + } else { + return null; + } + } + + public Map<String, DiscoInfo> caps = new HashMap<String, DiscoInfo>(); + } + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + + stanzaChannel = new DummyStanzaChannel(); + capsProvider = new DummyCapsProvider(); + + user1 = new JID("user1@bar.com/bla"); + discoInfo1 = new DiscoInfo(); + discoInfo1.addFeature("http://swift.im/feature1"); + capsInfo1 = new CapsInfoGenerator("http://node1.im", crypto).generateCapsInfo(discoInfo1); + capsInfo1alt = new CapsInfoGenerator("http://node2.im", crypto).generateCapsInfo(discoInfo1); + user2 = new JID("user2@foo.com/baz"); + discoInfo2 = new DiscoInfo(); + discoInfo2.addFeature("http://swift.im/feature2"); + capsInfo2 = new CapsInfoGenerator("http://node2.im", crypto).generateCapsInfo(discoInfo2); + user3 = new JID("user3@foo.com/baz"); + legacyCapsInfo = new CapsInfo("http://swift.im", "ver1", ""); + } + + private EntityCapsManager createManager() { + EntityCapsManager manager = new EntityCapsManager(capsProvider, stanzaChannel); + onCapsChangedConnection = manager.onCapsChanged.connect(new Slot1<JID>() { + + @Override + public void call(JID j1) { + handleCapsChanged(j1); + } + }); + return manager; + } + + private void handleCapsChanged(JID jid) { + changes.add(jid); + } + + private void sendPresenceWithCaps(JID jid, CapsInfo caps) { + Presence presence = new Presence(); + presence.setFrom(jid); + presence.addPayload(caps); + stanzaChannel.onPresenceReceived.emit(presence); + } + + private void sendUnavailablePresence(JID jid) { + Presence presence = new Presence(); + presence.setFrom(jid); + presence.setType(Presence.Type.Unavailable); + stanzaChannel.onPresenceReceived.emit(presence); + } + + @Test + public void testReceiveKnownHash() { + EntityCapsManager testling = createManager(); + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(1, changes.size()); + assertEquals(user1, changes.get(0)); + assertEquals(discoInfo1, testling.getCaps(user1)); + } + + @Test + public void testReceiveKnownHashTwiceDoesNotTriggerChange() { + EntityCapsManager testling = createManager(); + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + sendPresenceWithCaps(user1, capsInfo1); + changes.clear(); + + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, changes.size()); + } + + @Test + public void testReceiveUnknownHashDoesNotTriggerChange() { + EntityCapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + + assertEquals(0, changes.size()); + } + + @Test + public void testHashAvailable() { + EntityCapsManager testling = createManager(); + sendPresenceWithCaps(user1, capsInfo1); + + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + capsProvider.onCapsAvailable.emit(capsInfo1.getVersion()); + + assertEquals(1, changes.size()); + assertEquals(user1, changes.get(0)); + assertEquals(discoInfo1, testling.getCaps(user1)); + } + + @Test + public void testReceiveUnknownHashAfterKnownHashTriggersChangeAndClearsCaps() { + EntityCapsManager testling = createManager(); + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + sendPresenceWithCaps(user1, capsInfo1); + changes.clear(); + sendPresenceWithCaps(user1, capsInfo2); + + assertEquals(1, changes.size()); + assertEquals(user1, changes.get(0)); + assertNull(testling.getCaps(user1)); + } + + @Test + public void testReceiveUnavailablePresenceAfterKnownHashTriggersChangeAndClearsCaps() { + EntityCapsManager testling = createManager(); + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + sendPresenceWithCaps(user1, capsInfo1); + changes.clear(); + sendUnavailablePresence(user1); + + assertEquals(1, changes.size()); + assertEquals(user1, changes.get(0)); + assertNull(testling.getCaps(user1)); + } + + @Test + public void testReconnectTriggersChangeAndClearsCaps() { + EntityCapsManager testling = createManager(); + capsProvider.caps.put(capsInfo1.getVersion(), discoInfo1); + capsProvider.caps.put(capsInfo2.getVersion(), discoInfo2); + sendPresenceWithCaps(user1, capsInfo1); + sendPresenceWithCaps(user2, capsInfo2); + changes.clear(); + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + + assertEquals(2, changes.size()); + assertEquals(user1, changes.get(0)); + assertNull(testling.getCaps(user1)); + assertEquals(user2, changes.get(1)); + assertNull(testling.getCaps(user2)); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/disco/JIDDiscoInfoResponderTest.java b/test/com/isode/stroke/disco/JIDDiscoInfoResponderTest.java new file mode 100644 index 0000000..5cb2dbb --- /dev/null +++ b/test/com/isode/stroke/disco/JIDDiscoInfoResponderTest.java @@ -0,0 +1,121 @@ +/* + * 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.disco; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.DiscoInfo; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.queries.DummyIQChannel; +import com.isode.stroke.disco.DiscoInfoResponder; +import com.isode.stroke.jid.JID; +import java.util.Vector; +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; + +public class JIDDiscoInfoResponderTest { + + private IQRouter router_; + private DummyIQChannel channel_; + + @Before + public void setUp() { + channel_ = new DummyIQChannel(); + router_ = new IQRouter(channel_); + } + + @Test + public void testHandleRequest_GetToplevelInfo() { + JIDDiscoInfoResponder testling = new JIDDiscoInfoResponder(router_); + testling.start(); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(new JID("foo@bar.com/baz"), discoInfo); + + DiscoInfo query = new DiscoInfo(); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + DiscoInfo payload = channel_.iqs_.get(0).getPayload(new DiscoInfo()); + assertNotNull(payload); + assertEquals("", payload.getNode()); + assertTrue(payload.hasFeature("foo")); + + testling.stop(); + } + + @Test + public void testHandleRequest_GetNodeInfo() { + JIDDiscoInfoResponder testling = new JIDDiscoInfoResponder(router_); + testling.start(); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(new JID("foo@bar.com/baz"), discoInfo); + DiscoInfo discoInfoBar = new DiscoInfo(); + discoInfoBar.addFeature("bar"); + testling.setDiscoInfo(new JID("foo@bar.com/baz"), "bar-node", discoInfoBar); + + DiscoInfo query = new DiscoInfo(); + query.setNode("bar-node"); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + DiscoInfo payload = channel_.iqs_.get(0).getPayload(new DiscoInfo()); + assertNotNull(payload); + assertEquals("bar-node", payload.getNode()); + assertTrue(payload.hasFeature("bar")); + + testling.stop(); + } + + @Test + public void testHandleRequest_GetInvalidNodeInfo() { + JIDDiscoInfoResponder testling = new JIDDiscoInfoResponder(router_); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(new JID("foo@bar.com/baz"), discoInfo); + testling.start(); + + DiscoInfo query = new DiscoInfo(); + query.setNode("bar-node"); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + ErrorPayload payload = channel_.iqs_.get(0).getPayload(new ErrorPayload()); + assertNotNull(payload); + + testling.stop(); + } + + @Test + public void testHandleRequest_GetUnknownJID() { + JIDDiscoInfoResponder testling = new JIDDiscoInfoResponder(router_); + DiscoInfo discoInfo = new DiscoInfo(); + discoInfo.addFeature("foo"); + testling.setDiscoInfo(new JID("foo@bar.com/baz"), discoInfo); + testling.start(); + + DiscoInfo query = new DiscoInfo(); + channel_.onIQReceived.emit(IQ.createRequest(IQ.Type.Get, new JID("foo@bar.com/fum"), "id-1", query)); + + assertEquals(1, channel_.iqs_.size()); + ErrorPayload payload = channel_.iqs_.get(0).getPayload(new ErrorPayload()); + assertNotNull(payload); + + testling.stop(); + } +}
\ No newline at end of file |