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

Add tests

This commit is contained in:
vanitasvitae 2017-08-05 00:43:54 +02:00
parent 1aef1aaadc
commit 9c71cb8aeb
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
7 changed files with 170 additions and 124 deletions

View file

@ -43,60 +43,45 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
public abstract D getElement();
public void addOurCandidate(JingleTransportCandidate<?> candidate) {
// Insert sorted by descending priority
// Empty list -> insert
if (ourCandidates.isEmpty()) {
ourCandidates.add(candidate);
candidate.setParent(this);
return;
}
// Insert sorted by descending priority
int i;
// Find appropriate index
for (int i = 0; i < ourCandidates.size(); i++) {
for (i = 0; i < ourCandidates.size(); i++) {
JingleTransportCandidate<?> c = ourCandidates.get(i);
// list already contains element -> return
if (c == candidate || c.equals(candidate)) {
c.setParent(this);
c.setParent(this); // Candidate might equal, but not be same, so set parent just in case
return;
}
//Found the index
if (c.getPriority() <= candidate.getPriority()) {
ourCandidates.add(i, candidate);
candidate.setParent(this);
return;
if (c.getPriority() < candidate.getPriority()) {
break;
}
}
ourCandidates.add(i, candidate);
candidate.setParent(this);
}
public void addTheirCandidate(JingleTransportCandidate<?> candidate) {
// Insert sorted by descending priority
// Empty list -> insert
if (theirCandidates.isEmpty()) {
theirCandidates.add(candidate);
candidate.setParent(this);
return;
}
int i;
// Find appropriate index
for (int i = 0; i < theirCandidates.size(); i++) {
for (i = 0; i < theirCandidates.size(); i++) {
JingleTransportCandidate<?> c = theirCandidates.get(i);
// list already contains element -> return
if (c == candidate || c.equals(candidate)) {
c.setParent(this);
c.setParent(this); // Candidate might equal, but not be same, so set parent just in case
return;
}
//Found the index
if (c.getPriority() <= candidate.getPriority()) {
theirCandidates.add(i, candidate);
candidate.setParent(this);
return;
if (c.getPriority() < candidate.getPriority()) {
break;
}
}
theirCandidates.add(i, candidate);
candidate.setParent(this);
}
public abstract void prepare(XMPPConnection connection);
@ -120,19 +105,13 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
public abstract IQ handleTransportInfo(JingleContentTransportInfoElement info, JingleElement wrapping);
public void setParent(JingleContent parent) {
if (this.parent != parent) {
this.parent = parent;
}
this.parent = parent;
}
public JingleContent getParent() {
return parent;
}
public BytestreamSession getBytestreamSession() {
return bytestreamSession;
}
public abstract void handleSessionAccept(JingleContentTransportElement transportElement, XMPPConnection connection);
public abstract void cleanup();

View file

@ -1,54 +0,0 @@
/**
*
* 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.component;
import org.jivesoftware.smackx.jingle.element.JingleAction;
/**
* Object that represents a pending jingle action. A pending jingle action is an action that was sent to the peer,
* but a response hasn't yet received.
*/
public abstract class PendingJingleAction {
private final JingleAction action;
private final JingleContent affectedContent;
public PendingJingleAction(JingleAction action, JingleContent content) {
this.action = action;
this.affectedContent = content;
}
public JingleAction getAction() {
return action;
}
public JingleContent getAffectedContent() {
return affectedContent;
}
public static class TransportReplace extends PendingJingleAction {
private final JingleTransport<?> newTransport;
public TransportReplace(JingleContent content, JingleTransport<?> newTransport) {
super(JingleAction.transport_replace, content);
this.newTransport = newTransport;
}
public JingleTransport<?> getNewTransport() {
return newTransport;
}
}
}

View file

@ -67,8 +67,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
private String ourDstAddr;
private String theirDstAddr;
private Bytestream.Mode ourMode;
private Bytestream.Mode theirMode;
private Bytestream.Mode mode;
// PEERS candidate of OUR choice.
private JingleS5BTransportCandidate ourSelectedCandidate;
@ -86,7 +85,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
*/
JingleS5BTransport(FullJid initiator, FullJid responder, String sid, Bytestream.Mode mode, List<JingleTransportCandidate<?>> ourCandidates) {
this.sid = sid;
this.ourMode = mode;
this.mode = mode;
this.ourDstAddr = Socks5Utils.createDigest(sid, initiator, responder);
Socks5Proxy.getSocks5Proxy().addTransfer(ourDstAddr);
@ -104,10 +103,9 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
*/
JingleS5BTransport(FullJid initiator, FullJid responder, List<JingleTransportCandidate<?>> ourCandidates, JingleS5BTransport other) {
this.sid = other.sid;
this.ourMode = other.theirMode;
this.mode = other.mode;
this.ourDstAddr = Socks5Utils.createDigest(other.sid, responder, initiator);
Socks5Proxy.getSocks5Proxy().addTransfer(ourDstAddr);
this.theirMode = other.theirMode;
this.theirDstAddr = other.theirDstAddr;
for (JingleTransportCandidate<?> c : ourCandidates) {
@ -122,17 +120,15 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
/**
* Create custom transport as responder.
* @param sid sessionId of the Jingle session.
* @param ourMode UPD/TCP.
* @param theirMode UPD/TCP.
* @param mode UPD/TCP.
* @param ourDstAddr SOCKS5 destination address (digest)
* @param theirDstAddr SOCKS5 destination address (digest)
* @param ourCandidates our proxy candidates.
* @param theirCandidates their proxy candidates.
*/
JingleS5BTransport(String sid, Bytestream.Mode ourMode, Bytestream.Mode theirMode, String ourDstAddr, String theirDstAddr, List<JingleTransportCandidate<?>> ourCandidates, List<JingleTransportCandidate<?>> theirCandidates) {
JingleS5BTransport(String sid, Bytestream.Mode mode, String ourDstAddr, String theirDstAddr, List<JingleTransportCandidate<?>> ourCandidates, List<JingleTransportCandidate<?>> theirCandidates) {
this.sid = sid;
this.ourMode = ourMode;
this.theirMode = theirMode;
this.mode = mode;
this.ourDstAddr = ourDstAddr;
Socks5Proxy.getSocks5Proxy().addTransfer(ourDstAddr);
this.theirDstAddr = theirDstAddr;
@ -156,8 +152,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
this.sid = original.sid;
this.ourDstAddr = original.ourDstAddr;
this.theirDstAddr = original.theirDstAddr;
this.ourMode = original.ourMode;
this.theirMode = original.theirMode;
this.mode = original.mode;
for (JingleTransportCandidate<?> c : original.getOurCandidates()) {
addOurCandidate(new JingleS5BTransportCandidate((JingleS5BTransportCandidate) c));
@ -173,7 +168,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
JingleS5BTransportElement.Builder builder = JingleS5BTransportElement.getBuilder()
.setStreamId(sid)
.setDestinationAddress(ourDstAddr)
.setMode(ourMode);
.setMode(mode);
for (JingleTransportCandidate<?> candidate : getOurCandidates()) {
builder.addTransportCandidate((JingleS5BTransportCandidateElement) candidate.getElement());
@ -194,8 +189,8 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
return theirDstAddr;
}
public Bytestream.Mode getOurMode() {
return ourMode;
public Bytestream.Mode getMode() {
return mode;
}
@Override
@ -218,8 +213,8 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
Socks5Proxy.getSocks5Proxy().addTransfer(ourDstAddr);
}
if (ourMode == null) {
ourMode = Bytestream.Mode.tcp;
if (mode == null) {
mode = Bytestream.Mode.tcp;
}
if (getOurCandidates().size() == 0) {
@ -390,7 +385,6 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
public void handleSessionAccept(JingleContentTransportElement transportElement, XMPPConnection connection) {
JingleS5BTransportElement transport = (JingleS5BTransportElement) transportElement;
theirDstAddr = transport.getDestinationAddress();
theirMode = transport.getMode();
for (JingleContentTransportCandidateElement c : transport.getCandidates()) {
JingleS5BTransportCandidateElement candidate = (JingleS5BTransportCandidateElement) c;
addTheirCandidate(new JingleS5BTransportCandidate(candidate));

View file

@ -39,7 +39,7 @@ public class JingleS5BTransportAdapter implements JingleTransportAdapter<JingleS
candidates.add(JingleS5BTransportCandidate.fromElement((JingleS5BTransportCandidateElement) e));
}
return new JingleS5BTransport(s5b.getSid(), null, s5b.getMode(), null, s5b.getDestinationAddress(), null, candidates);
return new JingleS5BTransport(s5b.getSid(), s5b.getMode(), null, s5b.getDestinationAddress(), null, candidates);
}
@Override

View file

@ -0,0 +1,25 @@
package org.jivesoftware.smackx.jingle.component;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
import org.junit.Test;
public class JingleContentTest extends SmackTestSuite {
@Test
public void jingleContentTest() {
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.responder);
assertEquals(JingleContentElement.Creator.initiator, content.getCreator());
assertEquals(JingleContentElement.Senders.responder, content.getSenders());
assertNull(content.getDescription());
assertNull(content.getTransport());
assertNull(content.getSecurity());
assertNotNull(content.getName()); //MUST NOT BE NULL!
assertEquals(0, content.getTransportBlacklist().size());
}
}

View file

@ -0,0 +1,69 @@
package org.jivesoftware.smackx.jingle.component;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
import org.jivesoftware.smackx.jingle.util.Role;
import org.junit.Test;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class JingleSessionTest extends SmackTestSuite {
@Test
public void jingleSessionTest() throws XmppStringprepException {
DummyConnection dummyConnection = new DummyConnection();
FullJid alice = JidCreate.fullFrom("alice@wonderland.lit/test123");
FullJid madHatter = JidCreate.fullFrom("mad@hat.net/cat");
JingleManager jingleManager = JingleManager.getInstanceFor(dummyConnection);
JingleSession session = new JingleSession(jingleManager, alice, madHatter, Role.initiator, "WeReAlLmAdHeRe");
assertEquals(alice, session.getInitiator());
assertEquals(madHatter, session.getResponder());
assertEquals(alice, session.getOurJid());
assertEquals(madHatter, session.getPeer());
assertEquals(0, session.getContents().size());
assertEquals("WeReAlLmAdHeRe", session.getSessionId());
assertEquals(jingleManager, session.getJingleManager());
}
@Test(expected = IllegalStateException.class)
public void getSoleContentThrowingTest() {
JingleSession session = new JingleSession(JingleManager.getInstanceFor(new DummyConnection()), null, null, Role.initiator, null);
assertTrue(session.isInitiator());
assertFalse(session.isResponder());
JingleContent c1 = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
JingleContent c2 = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
session.addContent(c1);
assertEquals(c1, session.getContent(c1.getName()));
session.addContent(c2);
assertEquals(c2, session.getContent(c2.getName()));
session.getSoleContentOrThrow();
}
@Test
public void getSoleContentTest() {
JingleSession session = new JingleSession(JingleManager.getInstanceFor(new DummyConnection()), null, null, Role.responder, null);
assertTrue(session.isResponder());
assertFalse(session.isInitiator());
assertNull(session.getSoleContentOrThrow());
JingleContent c1 = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
assertNull(c1.getParent());
session.addContent(c1);
assertEquals(session, c1.getParent());
assertEquals(c1, session.getSoleContentOrThrow());
}
}

View file

@ -25,6 +25,9 @@ import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle.component.JingleContent;
import org.jivesoftware.smackx.jingle.component.JingleTransportCandidate;
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTransportCandidateElement;
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTransportElement;
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTransportInfoElement;
@ -74,17 +77,17 @@ public class JingleS5BTransportTest extends SmackTestSuite {
"type='proxy'/>" +
"</transport>";
JingleS5BTransportElement transport = new JingleS5BTransportProvider().parse(TestUtils.getParser(xml));
assertEquals("972b7bf47291ca609517f67f86b5081086052dad", transport.getDestinationAddress());
assertEquals("vj3hs98y", transport.getSid());
assertEquals(Bytestream.Mode.tcp, transport.getMode());
assertEquals(3, transport.getCandidates().size());
JingleS5BTransportElement transportElement = new JingleS5BTransportProvider().parse(TestUtils.getParser(xml));
assertEquals("972b7bf47291ca609517f67f86b5081086052dad", transportElement.getDestinationAddress());
assertEquals("vj3hs98y", transportElement.getSid());
assertEquals(Bytestream.Mode.tcp, transportElement.getMode());
assertEquals(3, transportElement.getCandidates().size());
assertTrue(transport.hasCandidate("hft54dqy"));
assertFalse(transport.hasCandidate("invalidId"));
assertTrue(transportElement.hasCandidate("hft54dqy"));
assertFalse(transportElement.hasCandidate("invalidId"));
JingleS5BTransportCandidateElement candidate1 =
(JingleS5BTransportCandidateElement) transport.getCandidates().get(0);
assertEquals(candidate1, transport.getCandidate("hft54dqy"));
(JingleS5BTransportCandidateElement) transportElement.getCandidates().get(0);
assertEquals(candidate1, transportElement.getCandidate("hft54dqy"));
assertNotNull(candidate1.getStreamHost());
assertEquals(JingleS5BTransportCandidateElement.Type.direct.getWeight(), candidate1.getType().getWeight());
assertEquals("hft54dqy", candidate1.getCandidateId());
@ -95,7 +98,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertEquals(JingleS5BTransportCandidateElement.Type.direct, candidate1.getType());
JingleS5BTransportCandidateElement candidate2 =
(JingleS5BTransportCandidateElement) transport.getCandidates().get(1);
(JingleS5BTransportCandidateElement) transportElement.getCandidates().get(1);
assertEquals("hutr46fe", candidate2.getCandidateId());
assertEquals("24.24.24.1", candidate2.getHost());
assertEquals(JidCreate.from("romeo@montague.lit/orchard"), candidate2.getJid());
@ -104,7 +107,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertEquals(JingleS5BTransportCandidateElement.Type.direct, candidate2.getType());
JingleS5BTransportCandidateElement candidate3 =
(JingleS5BTransportCandidateElement) transport.getCandidates().get(2);
(JingleS5BTransportCandidateElement) transportElement.getCandidates().get(2);
assertEquals("xmdh4b7i", candidate3.getCandidateId());
assertEquals("123.456.7.8", candidate3.getHost());
assertEquals(JidCreate.domainBareFrom("streamer.shakespeare.lit"), candidate3.getJid());
@ -112,7 +115,37 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertEquals(7878787, candidate3.getPriority());
assertEquals(JingleS5BTransportCandidateElement.Type.proxy, candidate3.getType());
assertEquals(xml, transport.toXML().toString());
assertEquals(xml, transportElement.toXML().toString());
JingleS5BTransport transport = new JingleS5BTransportAdapter().transportFromElement(transportElement);
assertNotNull(transport);
assertEquals(transportElement.getSid(), transport.getSid());
assertEquals(transportElement.getMode(), transport.getMode());
assertEquals(transportElement.getDestinationAddress(), transport.getTheirDstAddr());
assertNull(transport.getOurDstAddr());
assertNotNull(transport.getOurCandidates());
assertEquals(0, transport.getOurCandidates().size());
assertNotNull(transport.getTheirCandidates());
assertEquals(3, transport.getTheirCandidates().size());
for (int i = 0; i < transport.getTheirCandidates().size() - 1; i++) {
assertTrue(transport.getTheirCandidates().get(i).getPriority() >= transport.getTheirCandidates().get(i + 1).getPriority());
}
JingleTransportCandidate<?> c = transport.getTheirCandidates().get(1);
transport.addTheirCandidate(c);
assertEquals(3, transport.getTheirCandidates().size());
assertTrue(c.getParent() == transport);
assertNull(transport.getParent());
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
assertNull(content.getTransport());
content.setTransport(transport);
assertEquals(transport, content.getTransport());
assertEquals(content, transport.getParent());
}
@Test