/* * Copyright (c) 2011 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Swift; using namespace boost; class FakeRemoteJingleTransportCandidateSelector : public RemoteJingleTransportCandidateSelector { void addRemoteTransportCandidates(JingleTransportPayload::ref cand) { candidate = cand; } void selectCandidate() { boost::shared_ptr payload = make_shared(); 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: 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(); } virtual void start(JingleTransportPayload::ref payload) SWIFTEN_OVERRIDE { JingleS5BTransportPayload::ref payL = make_shared(); payL->setSessionID(payload->getSessionID()); onLocalTransportCandidatesGenerated(payL); } virtual void stop() SWIFTEN_OVERRIDE {} }; 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 createTestling() { JID ourJID("our@jid.org/full"); return make_shared(ourJID, shared_ptr(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(); fakeRJTCSF = make_shared(); fakeLJTCF = make_shared(); 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 desc = make_shared(); desc->addOffer(StreamInitiationFileInfo("foo.txt", "", 10)); jingleContentPayload->addDescription(desc); JingleIBBTransportPayload::ref tpRef = make_shared(); tpRef->setSessionID("mysession"); jingleContentPayload->addTransport(tpRef); shared_ptr fileTransfer = createTestling(); //2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one) shared_ptr byteStream = make_shared(); fileTransfer->accept(byteStream); // check whether accept has been called getCall(0); } void test_OnlyIBBTransferReceiveWorks() { //1. create your test incoming file transfer shared_ptr desc = make_shared(); desc->addOffer(StreamInitiationFileInfo("file.txt", "", 10)); jingleContentPayload->addDescription(desc); JingleIBBTransportPayload::ref tpRef = make_shared(); tpRef->setSessionID("mysession"); jingleContentPayload->addTransport(tpRef); shared_ptr fileTransfer = createTestling(); //2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one) shared_ptr byteStream = make_shared(); fileTransfer->accept(byteStream); // check whether accept has been called getCall(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 fileTransfer = createTestling(); //2. do 'accept' on a dummy writebytestream (you'll have to look if there already is one) shared_ptr byteStream = make_shared(); fileTransfer->accept(byteStream); // check whether accept has been called FakeJingleSession::AcceptCall acceptCall = getCall(0); CPPUNIT_ASSERT_EQUAL(payLoad->getSessionID(), acceptCall.payload->getSessionID()); // check for candidate error FakeJingleSession::InfoTransportCall infoTransportCall = getCall(1); JingleS5BTransportPayload::ref s5bPayload = dynamic_pointer_cast(infoTransportCall.payload); CPPUNIT_ASSERT(s5bPayload->hasCandidateError()); // indicate transport replace (Romeo) fakeJingleSession->onTransportReplaceReceived(getContentID(), addJingleIBBPayload()); FakeJingleSession::AcceptTransportCall acceptTransportCall = getCall(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 desc = make_shared(); desc->addOffer(StreamInitiationFileInfo("file.txt", "", 10)); jingleContentPayload->addDescription(desc); } shared_ptr addJingleS5BPayload() { JingleS5BTransportPayload::ref payLoad = make_shared(); payLoad->setSessionID("mysession"); jingleContentPayload->addTransport(payLoad); return payLoad; } shared_ptr addJingleIBBPayload() { JingleIBBTransportPayload::ref payLoad = make_shared(); payLoad->setSessionID("mysession"); jingleContentPayload->addTransport(payLoad); return payLoad; } JingleContentID getContentID() const { return JingleContentID(jingleContentPayload->getName(), jingleContentPayload->getCreator()); } template T getCall(int i) const { size_t index = static_cast(i); CPPUNIT_ASSERT(index < fakeJingleSession->calledCommands.size()); T* cmd = boost::get(&fakeJingleSession->calledCommands[index]); CPPUNIT_ASSERT(cmd); return *cmd; } private: EventLoop* eventLoop; FakeJingleSession* fakeJingleSession; shared_ptr jingleContentPayload; shared_ptr fakeRJTCSF; shared_ptr fakeLJTCF; DummyStanzaChannel* stanzaChannel; IQRouter* iqRouter; SOCKS5BytestreamRegistry* bytestreamRegistry; DummyConnectionFactory* connectionFactory; SOCKS5BytestreamProxy* bytestreamProxy; DummyTimerFactory* timerFactory; }; CPPUNIT_TEST_SUITE_REGISTRATION(IncomingJingleFileTransferTest);