1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
* Copyright (c) 2011 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Elements/IBB.h>
#include <Swiften/Elements/JingleIBBTransportPayload.h>
#include <Swiften/Elements/JingleS5BTransportPayload.h>
#include <Swiften/FileTransfer/ByteArrayWriteBytestream.h>
#include <Swiften/FileTransfer/IncomingJingleFileTransfer.h>
#include <Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.h>
#include <Swiften/FileTransfer/LocalJingleTransportCandidateGeneratorFactory.h>
#include <Swiften/FileTransfer/RemoteJingleTransportCandidateSelector.h>
#include <Swiften/FileTransfer/RemoteJingleTransportCandidateSelectorFactory.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamProxy.h>
#include <Swiften/Jingle/FakeJingleSession.h>
#include <Swiften/Network/DummyTimerFactory.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/DummyConnectionFactory.h>
#include <Swiften/Network/PlatformNATTraversalWorker.h>
#include <Swiften/Queries/IQRouter.h>
#include <iostream>
using namespace Swift;
using namespace boost;
class FakeRemoteJingleTransportCandidateSelector : public RemoteJingleTransportCandidateSelector {
void addRemoteTransportCandidates(JingleTransportPayload::ref cand) {
candidate = cand;
}
void selectCandidate() {
boost::shared_ptr<JingleS5BTransportPayload> payload = make_shared<JingleS5BTransportPayload>();
payload->setCandidateError(true);
payload->setSessionID(candidate->getSessionID());
onRemoteTransportCandidateSelectFinished(payload);
}
void setMinimumPriority(int) {
}
bool isActualCandidate(JingleTransportPayload::ref) {
return false;
}
int getPriority(JingleTransportPayload::ref) {
return 0;
}
JingleTransport::ref selectTransport(JingleTransportPayload::ref) {
return JingleTransport::ref();
}
private:
JingleTransportPayload::ref candidate;
};
class FakeRemoteJingleTransportCandidateSelectorFactory : public RemoteJingleTransportCandidateSelectorFactory {
public:
virtual ~FakeRemoteJingleTransportCandidateSelectorFactory() {
}
virtual RemoteJingleTransportCandidateSelector* createCandidateSelector() {
return new FakeRemoteJingleTransportCandidateSelector();
}
};
class FakeLocalJingleTransportCandidateGenerator : public LocalJingleTransportCandidateGenerator {
public:
virtual void generateLocalTransportCandidates(JingleTransportPayload::ref payload) {
JingleS5BTransportPayload::ref payL = make_shared<JingleS5BTransportPayload>();
payL->setSessionID(payload->getSessionID());
onLocalTransportCandidatesGenerated(payL);
}
void emitonLocalTransportCandidatesGenerated(JingleTransportPayload::ref payload) {
onLocalTransportCandidatesGenerated(payload);
}
virtual bool isActualCandidate(JingleTransportPayload::ref) {
return false;
}
virtual int getPriority(JingleTransportPayload::ref) {
return 0;
}
virtual JingleTransport::ref selectTransport(JingleTransportPayload::ref) {
return JingleTransport::ref();
}
};
class FakeLocalJingleTransportCandidateGeneratorFactory : public LocalJingleTransportCandidateGeneratorFactory {
public:
virtual LocalJingleTransportCandidateGenerator* createCandidateGenerator() {
return new FakeLocalJingleTransportCandidateGenerator();
}
};
class IncomingJingleFileTransferTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(IncomingJingleFileTransferTest);
CPPUNIT_TEST(test_AcceptOnyIBBSendsSessionAccept);
CPPUNIT_TEST(test_OnlyIBBTransferReceiveWorks);
CPPUNIT_TEST(test_AcceptFailingS5BFallsBackToIBB);
CPPUNIT_TEST_SUITE_END();
public:
shared_ptr<IncomingJingleFileTransfer> createTestling() {
JID ourJID("our@jid.org/full");
return make_shared<IncomingJingleFileTransfer>(ourJID, shared_ptr<JingleSession>(fakeJingleSession), jingleContentPayload, fakeRJTCSF.get(), fakeLJTCF.get(), iqRouter, bytestreamRegistry, bytestreamProxy, timerFactory);
}
IQ::ref createIBBRequest(IBB::ref ibb, const JID& from, const std::string& id) {
IQ::ref request = IQ::createRequest(IQ::Set, JID("foo@bar.com/baz"), id, ibb);
request->setFrom(from);
return request;
}
void setUp() {
eventLoop = new DummyEventLoop();
fakeJingleSession = new FakeJingleSession("foo@bar.com/baz", "mysession");
jingleContentPayload = make_shared<JingleContentPayload>();
fakeRJTCSF = make_shared<FakeRemoteJingleTransportCandidateSelectorFactory>();
fakeLJTCF = make_shared<FakeLocalJingleTransportCandidateGeneratorFactory>();
stanzaChannel = new DummyStanzaChannel();
iqRouter = new IQRouter(stanzaChannel);
bytestreamRegistry = new SOCKS5BytestreamRegistry();
timerFactory = new DummyTimerFactory();
connectionFactory = new DummyConnectionFactory(eventLoop);
bytestreamProxy = new SOCKS5BytestreamProxy(connectionFactory, timerFactory);
}
void tearDown() {
delete bytestreamProxy;
delete connectionFactory;
delete timerFactory;
delete bytestreamRegistry;
delete iqRouter;
delete stanzaChannel;
delete eventLoop;
}
// Tests whether IncomingJingleFileTransfer would accept a IBB only file transfer.
void test_AcceptOnyIBBSendsSessionAccept() {
//1. create your test incoming file transfer
shared_ptr<JingleFileTransferDescription> desc = make_shared<JingleFileTransferDescription>();
desc->addOffer(StreamInitiationFileInfo("foo.txt", "", 10));
jingleContentPayload->addDescription(desc);
JingleIBBTransportPayload::ref tpRef = make_shared<JingleIBBTransportPayload>();
tpRef->setSessionID("mysession");
jingleContentPayload->addTransport(tpRef);
shared_ptr<IncomingJingleFileTransfer> fileTransfer = createTestling();
//2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one)
shared_ptr<ByteArrayWriteBytestream> byteStream = make_shared<ByteArrayWriteBytestream>();
fileTransfer->accept(byteStream);
// check whether accept has been called
getCall<FakeJingleSession::AcceptCall>(0);
}
void test_OnlyIBBTransferReceiveWorks() {
//1. create your test incoming file transfer
shared_ptr<JingleFileTransferDescription> desc = make_shared<JingleFileTransferDescription>();
desc->addOffer(StreamInitiationFileInfo("file.txt", "", 10));
jingleContentPayload->addDescription(desc);
JingleIBBTransportPayload::ref tpRef = make_shared<JingleIBBTransportPayload>();
tpRef->setSessionID("mysession");
jingleContentPayload->addTransport(tpRef);
shared_ptr<IncomingJingleFileTransfer> fileTransfer = createTestling();
//2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one)
shared_ptr<ByteArrayWriteBytestream> byteStream = make_shared<ByteArrayWriteBytestream>();
fileTransfer->accept(byteStream);
// check whether accept has been called
getCall<FakeJingleSession::AcceptCall>(0);
stanzaChannel->onIQReceived(createIBBRequest(IBB::createIBBOpen("mysession", 0x10), "foo@bar.com/baz", "id-open"));
stanzaChannel->onIQReceived(createIBBRequest(IBB::createIBBData("mysession", 0, createByteArray("abc")), "foo@bar.com/baz", "id-a"));
CPPUNIT_ASSERT(createByteArray("abc") == byteStream->getData());
}
void test_AcceptFailingS5BFallsBackToIBB() {
//1. create your test incoming file transfer
addFileTransferDescription();
// add SOCKS5BytestreamTransportPayload
JingleS5BTransportPayload::ref payLoad = addJingleS5BPayload();
shared_ptr<IncomingJingleFileTransfer> fileTransfer = createTestling();
//2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one)
shared_ptr<ByteArrayWriteBytestream> byteStream = make_shared<ByteArrayWriteBytestream>();
fileTransfer->accept(byteStream);
// check whether accept has been called
FakeJingleSession::AcceptCall acceptCall = getCall<FakeJingleSession::AcceptCall>(0);
CPPUNIT_ASSERT_EQUAL(payLoad->getSessionID(), acceptCall.payload->getSessionID());
// check for candidate error
FakeJingleSession::InfoTransportCall infoTransportCall = getCall<FakeJingleSession::InfoTransportCall>(1);
JingleS5BTransportPayload::ref s5bPayload = dynamic_pointer_cast<JingleS5BTransportPayload>(infoTransportCall.payload);
CPPUNIT_ASSERT(s5bPayload->hasCandidateError());
// indicate transport replace (Romeo)
fakeJingleSession->onTransportReplaceReceived(getContentID(), addJingleIBBPayload());
FakeJingleSession::AcceptTransportCall acceptTransportCall = getCall<FakeJingleSession::AcceptTransportCall>(2);
// send a bit of data
stanzaChannel->onIQReceived(createIBBRequest(IBB::createIBBOpen("mysession", 0x10), "foo@bar.com/baz", "id-open"));
stanzaChannel->onIQReceived(createIBBRequest(IBB::createIBBData("mysession", 0, createByteArray("abc")), "foo@bar.com/baz", "id-a"));
CPPUNIT_ASSERT(createByteArray("abc") == byteStream->getData());
}
void test_S5BTransferReceiveTest() {
addFileTransferDescription();
JingleS5BTransportPayload::ref payLoad = addJingleS5BPayload();
}
private:
void addFileTransferDescription() {
shared_ptr<JingleFileTransferDescription> desc = make_shared<JingleFileTransferDescription>();
desc->addOffer(StreamInitiationFileInfo("file.txt", "", 10));
jingleContentPayload->addDescription(desc);
}
shared_ptr<JingleS5BTransportPayload> addJingleS5BPayload() {
JingleS5BTransportPayload::ref payLoad = make_shared<JingleS5BTransportPayload>();
payLoad->setSessionID("mysession");
jingleContentPayload->addTransport(payLoad);
return payLoad;
}
shared_ptr<JingleIBBTransportPayload> addJingleIBBPayload() {
JingleIBBTransportPayload::ref payLoad = make_shared<JingleIBBTransportPayload>();
payLoad->setSessionID("mysession");
jingleContentPayload->addTransport(payLoad);
return payLoad;
}
JingleContentID getContentID() const {
return JingleContentID(jingleContentPayload->getName(), jingleContentPayload->getCreator());
}
template <typename T> T getCall(int i) const {
size_t index = static_cast<size_t>(i);
CPPUNIT_ASSERT(index < fakeJingleSession->calledCommands.size());
T* cmd = boost::get<T>(&fakeJingleSession->calledCommands[index]);
CPPUNIT_ASSERT(cmd);
return *cmd;
}
private:
EventLoop* eventLoop;
FakeJingleSession* fakeJingleSession;
shared_ptr<JingleContentPayload> jingleContentPayload;
shared_ptr<FakeRemoteJingleTransportCandidateSelectorFactory> fakeRJTCSF;
shared_ptr<FakeLocalJingleTransportCandidateGeneratorFactory> fakeLJTCF;
DummyStanzaChannel* stanzaChannel;
IQRouter* iqRouter;
SOCKS5BytestreamRegistry* bytestreamRegistry;
DummyConnectionFactory* connectionFactory;
SOCKS5BytestreamProxy* bytestreamProxy;
DummyTimerFactory* timerFactory;
};
CPPUNIT_TEST_SUITE_REGISTRATION(IncomingJingleFileTransferTest);
|