1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

More tests

This commit is contained in:
vanitasvitae 2017-07-03 15:22:34 +02:00
parent 63aa2f017c
commit 55d41e3390
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 468 additions and 55 deletions

View file

@ -61,6 +61,10 @@ public final class JingleTransportMethodManager extends Manager {
transportManagers.put(manager.getNamespace(), manager); transportManagers.put(manager.getNamespace(), manager);
} }
public void unregisterTransportManager(JingleTransportManager<?> manager) {
transportManagers.remove(manager.getNamespace());
}
public static JingleTransportManager<?> getTransportManager(XMPPConnection connection, String namespace) { public static JingleTransportManager<?> getTransportManager(XMPPConnection connection, String namespace) {
return getInstanceFor(connection).getTransportManager(namespace); return getInstanceFor(connection).getTransportManager(namespace);
} }
@ -87,7 +91,7 @@ public final class JingleTransportMethodManager extends Manager {
return getTransportManager(transport.getNamespace()); return getTransportManager(transport.getNamespace());
} }
public JingleTransportManager<?> getBestAvailableTransportManager(XMPPConnection connection) { public static JingleTransportManager<?> getBestAvailableTransportManager(XMPPConnection connection) {
return getInstanceFor(connection).getBestAvailableTransportManager(); return getInstanceFor(connection).getBestAvailableTransportManager();
} }

View file

@ -0,0 +1,47 @@
/**
*
* Copyright 2017 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.junit.Test;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class FullJidAndSessionIdTest extends SmackTestSuite {
@Test
public void equalityTest() throws XmppStringprepException {
FullJid albert = JidCreate.fullFrom("albert@einstein.emc/squared");
String albertId = "10121922";
FullJidAndSessionId fns = new FullJidAndSessionId(albert, albertId);
assertEquals("10121922", fns.getSessionId());
assertEquals(JidCreate.fullFrom("albert@einstein.emc/squared"), fns.getFullJid());
FullJidAndSessionId fns2 = new FullJidAndSessionId(JidCreate.fullFrom("albert@einstein.emc/squared"), "10121922");
assertTrue(fns.equals(fns2));
assertEquals(fns.hashCode(), fns2.hashCode());
assertNotSame(fns, new FullJidAndSessionId(JidCreate.fullFrom("albert@einstein.emc/squared"), "11111111"));
}
}

View file

@ -0,0 +1,75 @@
package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertNull;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.junit.Test;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
/**
* Created by vanitas on 03.07.17.
*/
public class JingleManagerTest extends SmackTestSuite {
/**
* Might fail in *very* rare cases.
*/
@Test
public void randomTest() {
String r1 = JingleManager.randomId();
String r2 = JingleManager.randomId();
assertNotSame(r1, r2);
assertEquals(24, r1.length());
}
@Test
public void threadPoolTest() {
assertNotNull(JingleManager.getThreadPool());
}
@Test
public void handlerRegistrationTest() throws XmppStringprepException {
final XMPPConnection connection = new DummyConnection();
FullJid remote = JidCreate.fullFrom("test@test.test/test");
FullJid local = JidCreate.fullFrom("case@case.case/case");
String sid = JingleManager.randomId();
JingleSession s = new JingleSession(local, remote, Role.initiator, sid) {
@Override
public XMPPConnection getConnection() {
return connection;
}
@Override
public void onTransportMethodFailed(String namespace) {
}
};
assertNull(JingleManager.getInstanceFor(connection).registerJingleSessionHandler(remote, sid, s));
assertNotNull(JingleManager.getInstanceFor(connection).registerJingleSessionHandler(remote, sid, s));
JingleManager.getInstanceFor(connection).unregisterJingleSessionHandler(remote, sid, s);
assertNull(JingleManager.getInstanceFor(connection).registerJingleSessionHandler(remote, sid, s));
String stubNamespace = "urn:xmpp:jingle:application:stub:0";
JingleHandler stub = new JingleHandler() {
@Override
public IQ handleJingleRequest(Jingle jingle) {
return null;
}
};
assertNull(JingleManager.getInstanceFor(connection).registerDescriptionHandler(stubNamespace, stub));
assertNotNull(JingleManager.getInstanceFor(connection).registerDescriptionHandler(stubNamespace, stub));
}
}

View file

@ -0,0 +1,215 @@
/**
*
* Copyright 2017 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertNull;
import java.util.List;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.TestIQ;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.transports.jingle_ibb.JingleIBBTransportSession;
import org.junit.Test;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class JingleSessionTest {
private static final XMPPConnection connection = new DummyConnection();
private static final IQ sessionInitiateResult = new TestIQ();
private static final IQ sessionTerminateResult = new TestIQ();
private static final IQ sessionInfoResult = new TestIQ();
private static final IQ sessionAcceptResult = new TestIQ();
private static final IQ contentAddResult = new TestIQ();
private static final IQ contentAcceptResult = new TestIQ();
private static final IQ contentRejectResult = new TestIQ();
private static final IQ contentModifyResult = new TestIQ();
private static final IQ contentRemoveResult = new TestIQ();
private static final IQ descriptionInfoResult = new TestIQ();
private static final IQ securityInfoResult = new TestIQ();
private static final IQ transportAcceptResult = new TestIQ();
private static final IQ transportReplaceResult = new TestIQ();
private static final IQ transportRejectResult = new TestIQ();
@Test
public void jingleSessionTest() throws XmppStringprepException {
FullJid us5 = JidCreate.fullFrom("home@swe.et/home");
FullJid u2 = JidCreate.fullFrom("place@far.far/away");
String sessionId = "suchPopMuchWow";
JingleSession initiatedSimpleSession = new SimpleSession(us5, u2, Role.initiator, sessionId);
assertEquals(us5, initiatedSimpleSession.getInitiator());
assertEquals(u2, initiatedSimpleSession.getResponder());
assertEquals(us5, initiatedSimpleSession.getLocal());
assertEquals(u2, initiatedSimpleSession.getRemote());
assertEquals(sessionId, initiatedSimpleSession.getSessionId());
assertNotNull(initiatedSimpleSession.getContents());
String sessionId2 = "popMusicSucks";
JingleSession respondedSimpleSession = new SimpleSession(u2, us5, Role.responder, sessionId2);
assertEquals(us5, respondedSimpleSession.getLocal());
assertEquals(us5, respondedSimpleSession.getResponder());
assertEquals(u2, respondedSimpleSession.getInitiator());
assertEquals(u2, respondedSimpleSession.getRemote());
assertEquals(new FullJidAndSessionId(u2, sessionId), initiatedSimpleSession.getFullJidAndSessionId());
assertEquals(new FullJidAndSessionId(u2, sessionId2), respondedSimpleSession.getFullJidAndSessionId());
assertNull(initiatedSimpleSession.getTransportSession());
initiatedSimpleSession.setTransportSession(new JingleIBBTransportSession(initiatedSimpleSession));
assertNotNull(initiatedSimpleSession.getTransportSession());
assertNotSame(initiatedSimpleSession, respondedSimpleSession);
assertFalse(initiatedSimpleSession.equals(respondedSimpleSession));
assertNotSame(initiatedSimpleSession.hashCode(), respondedSimpleSession.hashCode());
}
@Test
public void testHandleSessionRequest() {
JingleSession s = new SimpleSession(null, null, null, null);
assertEquals(sessionAcceptResult, s.handleJingleSessionRequest(simpleAction(JingleAction.session_accept)));
assertEquals(sessionInfoResult, s.handleJingleSessionRequest(simpleAction(JingleAction.session_info)));
assertEquals(sessionInitiateResult, s.handleJingleSessionRequest(simpleAction(JingleAction.session_initiate)));
assertEquals(sessionTerminateResult, s.handleJingleSessionRequest(simpleAction(JingleAction.session_terminate)));
assertEquals(contentAcceptResult, s.handleJingleSessionRequest(simpleAction(JingleAction.content_accept)));
assertEquals(contentAddResult, s.handleJingleSessionRequest(simpleAction(JingleAction.content_add)));
assertEquals(contentModifyResult, s.handleJingleSessionRequest(simpleAction(JingleAction.content_modify)));
assertEquals(contentRejectResult, s.handleJingleSessionRequest(simpleAction(JingleAction.content_reject)));
assertEquals(contentRemoveResult, s.handleJingleSessionRequest(simpleAction(JingleAction.content_remove)));
assertEquals(descriptionInfoResult, s.handleJingleSessionRequest(simpleAction(JingleAction.description_info)));
assertEquals(securityInfoResult, s.handleJingleSessionRequest(simpleAction(JingleAction.security_info)));
assertEquals(transportAcceptResult, s.handleJingleSessionRequest(simpleAction(JingleAction.transport_accept)));
assertEquals(transportRejectResult, s.handleJingleSessionRequest(simpleAction(JingleAction.transport_reject)));
assertEquals(transportReplaceResult, s.handleJingleSessionRequest(simpleAction(JingleAction.transport_replace)));
}
private static class SimpleSession extends JingleSession {
public SimpleSession(FullJid initiator, FullJid responder, Role role, String sid) {
super(initiator, responder, role, sid);
}
public SimpleSession(FullJid initiator, FullJid responder, Role role, String sid, List<JingleContent> contents) {
super(initiator, responder, role, sid, contents);
}
@Override
public XMPPConnection getConnection() {
return connection;
}
@Override
public void onTransportMethodFailed(String namespace) {
}
@Override
public IQ handleSessionInitiate(Jingle jingle) {
return sessionInitiateResult;
}
@Override
public IQ handleSessionAccept(Jingle jingle) {
return sessionAcceptResult;
}
@Override
public IQ handleSessionTerminate(Jingle jingle) {
return sessionTerminateResult;
}
@Override
public IQ handleSessionInfo(Jingle jingle) {
return sessionInfoResult;
}
@Override
public IQ handleTransportAccept(Jingle jingle) {
return transportAcceptResult;
}
@Override
public IQ handleTransportReject(Jingle jingle) {
return transportRejectResult;
}
@Override
public IQ handleTransportReplace(Jingle jingle) {
return transportReplaceResult;
}
@Override
public IQ handleContentAdd(Jingle jingle) {
return contentAddResult;
}
@Override
public IQ handleContentAccept(Jingle jingle) {
return contentAcceptResult;
}
@Override
public IQ handleContentReject(Jingle jingle) {
return contentRejectResult;
}
@Override
public IQ handleContentRemove(Jingle jingle) {
return contentRemoveResult;
}
@Override
public IQ handleContentModify(Jingle jingle) {
return contentModifyResult;
}
@Override
public IQ handleDescriptionInfo(Jingle jingle) {
return descriptionInfoResult;
}
@Override
public IQ handleSecurityInfo(Jingle jingle) {
return securityInfoResult;
}
}
private Jingle simpleAction(JingleAction action) {
return Jingle.getBuilder().setAction(action).setSessionId("test").build();
}
}

View file

@ -0,0 +1,110 @@
package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
import org.jivesoftware.smackx.jingle.transports.JingleTransportSession;
import org.jivesoftware.smackx.jingle.transports.jingle_ibb.JingleIBBTransportManager;
import org.jivesoftware.smackx.jingle.transports.jingle_ibb.element.JingleIBBTransport;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.JingleS5BTransportManager;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransport;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class JingleTransportMethodManagerTest extends SmackTestSuite {
@Test
public void getTransportManagerTest() throws XmppStringprepException {
XMPPConnection connection = new DummyConnection();
JingleTransportMethodManager jtmm = JingleTransportMethodManager.getInstanceFor(connection);
assertNull(jtmm.getBestAvailableTransportManager());
assertNull(jtmm.getTransportManager(JingleIBBTransport.NAMESPACE_V1));
assertNull(jtmm.getTransportManager(JingleS5BTransport.NAMESPACE_V1));
jtmm.registerTransportManager(JingleIBBTransportManager.getInstanceFor(connection));
assertNull(jtmm.getTransportManager(JingleS5BTransport.NAMESPACE_V1));
assertNotNull(jtmm.getTransportManager(JingleIBBTransport.NAMESPACE_V1));
assertEquals(JingleIBBTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager());
jtmm.registerTransportManager(JingleS5BTransportManager.getInstanceFor(connection));
assertEquals(JingleS5BTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager());
jtmm.unregisterTransportManager(JingleS5BTransportManager.getInstanceFor(connection));
assertNull(jtmm.getTransportManager(JingleS5BTransport.NAMESPACE_V1));
jtmm.unregisterTransportManager(JingleIBBTransportManager.getInstanceFor(connection));
assertNull(jtmm.getBestAvailableTransportManager());
jtmm.registerTransportManager(JingleS5BTransportManager.getInstanceFor(connection));
assertEquals(JingleS5BTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager());
jtmm.registerTransportManager(JingleIBBTransportManager.getInstanceFor(connection));
assertEquals(JingleS5BTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager());
assertEquals(JingleIBBTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager(
Collections.singleton(JingleS5BTransport.NAMESPACE_V1)));
JingleStubTransportManager stub = new JingleStubTransportManager(connection);
jtmm.registerTransportManager(stub);
assertEquals(stub, JingleTransportMethodManager.getTransportManager(connection, JingleStubTransportManager.NAMESPACE));
assertEquals(JingleS5BTransportManager.getInstanceFor(connection), jtmm.getBestAvailableTransportManager());
Jingle jingle = Jingle.getBuilder().setSessionId("test").setAction(JingleAction.session_initiate)
.setInitiator(JidCreate.fullFrom("test@test.test/test"))
.addJingleContent(
JingleContent.getBuilder().setCreator(JingleContent.Creator.initiator).setName("content")
.setSenders(JingleContent.Senders.initiator).setTransport(
new JingleIBBTransport("transportId")).build()).build();
assertEquals(JingleIBBTransportManager.getInstanceFor(connection), jtmm.getTransportManager(jingle));
assertEquals(JingleIBBTransportManager.getInstanceFor(connection),
JingleTransportMethodManager.getTransportManager(connection, jingle));
Set<String> except = new HashSet<>();
except.add(JingleIBBTransport.NAMESPACE_V1);
except.add(JingleS5BTransport.NAMESPACE_V1);
assertEquals(stub, jtmm.getBestAvailableTransportManager(except));
jtmm.unregisterTransportManager(JingleS5BTransportManager.getInstanceFor(connection));
jtmm.unregisterTransportManager(JingleIBBTransportManager.getInstanceFor(connection));
assertEquals(stub, JingleTransportMethodManager.getBestAvailableTransportManager(connection));
}
private static class JingleStubTransportManager extends JingleTransportManager<JingleContentTransport> {
public static final String NAMESPACE = "urn:xmpp:jingle:transports:stub:0";
public JingleStubTransportManager(XMPPConnection connection) {
super(connection);
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public JingleTransportSession<JingleContentTransport> transportSession(JingleSession jingleSession) {
return null;
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
}
}
}

View file

@ -16,11 +16,15 @@
*/ */
package org.jivesoftware.smackx.jingle; package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent; import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -36,67 +40,25 @@ public class JingleUtilTest extends SmackTestSuite {
private XMPPConnection connection; private XMPPConnection connection;
private JingleUtil jutil; private JingleUtil jutil;
private FullJid romeo;
private FullJid juliet;
@Before @Before
public void setup() { public void setup() throws XmppStringprepException {
connection = new DummyConnection( connection = new DummyConnection(
DummyConnection.getDummyConfigurationBuilder() DummyConnection.getDummyConfigurationBuilder()
.setUsernameAndPassword("romeo@montague.lit", .setUsernameAndPassword("romeo@montague.lit",
"iluvJulibabe13").build()); "iluvJulibabe13").build());
jutil = new JingleUtil(connection); jutil = new JingleUtil(connection);
romeo = connection.getUser().asFullJidOrThrow();
juliet = JidCreate.fullFrom("juliet@capulet.lit/balcony");
} }
@Test @Test
public void sessionInitiateTest() throws XmppStringprepException { public void createAckTest() {
FullJid romeo = connection.getUser().asFullJidOrThrow(); Jingle jingle = Jingle.getBuilder().setAction(JingleAction.session_initiate).setInitiator(romeo).setSessionId("test").build();
FullJid juliet = JidCreate.fullFrom("juliet@capulet.example/yn0cl4bnw0yr3vym"); IQ result = jutil.createAck(jingle);
assertEquals(jingle.getStanzaId(), result.getStanzaId());
String sid = "851ba2"; assertTrue(result.getType() == IQ.Type.result);
String contentName = "a-file-offer";
Jingle jingle = jutil.createSessionInitiate(juliet, sid,
JingleContent.Creator.initiator, contentName, JingleContent.Senders.initiator, null, null);
String expected =
"<iq from='" + romeo.toString() + "' " +
"id='nzu25s8' " +
"to='juliet@capulet.example/yn0cl4bnw0yr3vym' " +
"type='set'>" +
"<jingle xmlns='urn:xmpp:jingle:1' " +
"action='session-initiate' " +
"initiator='romeo@montague.example/dr4hcr0st3lup4c' " +
"sid='851ba2'>" +
"<content creator='initiator' name='a-file-offer' senders='initiator'>" +
"<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>" +
"<file>" +
"<date>1969-07-21T02:56:15Z</date>" +
"<desc>This is a test. If this were a real file...</desc>" +
"<media-type>text/plain</media-type>" +
"<name>test.txt</name>" +
"<range/>" +
"<size>6144</size>" +
"<hash xmlns='urn:xmpp:hashes:2' " +
"algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>" +
"</file>" +
"</description>" +
"<transport xmlns='urn:xmpp:jingle:transports:s5b:1' " +
"mode='tcp' " +
"sid='vj3hs98y'> " +
"<candidate cid='hft54dqy' " +
"host='192.168.4.1' " +
"jid='romeo@montague.example/dr4hcr0st3lup4c' " +
"port='5086' " +
"priority='8257636' " +
"type='direct'/>" +
"<candidate cid='hutr46fe' " +
"host='24.24.24.1' " +
"jid='romeo@montague.example/dr4hcr0st3lup4c' " +
"port='5087' " +
"priority='8258636' " +
"type='direct'/>" +
"</transport>" +
"</content>" +
"</jingle>" +
"</iq>";
} }
} }