/* * Copyright (c) 2011 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ /* * Copyright (c) 2013-2015 Isode Limited. * All rights reserved. * See the COPYING file 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 #include #include #include #include #include #include #include #include #include #include using namespace Swift; class OutgoingJingleFileTransferTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(OutgoingJingleFileTransferTest); CPPUNIT_TEST(test_SendSessionInitiateOnStart); CPPUNIT_TEST(test_FallbackToIBBAfterFailingS5B); CPPUNIT_TEST(test_ReceiveSessionTerminateAfterSessionInitiate); CPPUNIT_TEST_SUITE_END(); class FTStatusHelper { public: bool finishedCalled; FileTransferError::Type error; void handleFileTransferFinished(boost::optional error) { finishedCalled = true; if (error.is_initialized()) this->error = error.get().getType(); } }; public: boost::shared_ptr createTestling() { JID to("test@foo.com/bla"); JingleFileTransferFileInfo fileInfo; fileInfo.setDescription("some file"); fileInfo.setName("test.bin"); fileInfo.addHash(HashElement("sha-1", ByteArray())); fileInfo.setSize(1024 * 1024); return boost::shared_ptr(new OutgoingJingleFileTransfer( to, boost::shared_ptr(fakeJingleSession), stream, ftTransportFactory, timerFactory, idGen, fileInfo, FileTransferOptions().withAssistedAllowed(false).withDirectAllowed(false).withProxiedAllowed(false), crypto.get())); } 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() { crypto = boost::shared_ptr(PlatformCryptoProvider::create()); fakeJingleSession = new FakeJingleSession("foo@bar.com/baz", "mysession"); jingleContentPayload = boost::make_shared(); stanzaChannel = new DummyStanzaChannel(); iqRouter = new IQRouter(stanzaChannel); eventLoop = new DummyEventLoop(); timerFactory = new DummyTimerFactory(); connectionFactory = new DummyConnectionFactory(eventLoop); serverConnectionFactory = new DummyConnectionServerFactory(eventLoop); s5bRegistry = new SOCKS5BytestreamRegistry(); networkEnvironment = new PlatformNetworkEnvironment(); natTraverser = new PlatformNATTraversalWorker(eventLoop); bytestreamServerManager = new SOCKS5BytestreamServerManager(s5bRegistry, serverConnectionFactory, networkEnvironment, natTraverser); data.clear(); for (int n=0; n < 1024 * 1024; ++n) { data.push_back(34); } stream = boost::make_shared(data); idGen = new IDGenerator(); s5bProxy = new SOCKS5BytestreamProxiesManager(connectionFactory, timerFactory, resolver, iqRouter, "bar.com"); ftTransportFactory = new DummyFileTransferTransporterFactory(s5bRegistry, bytestreamServerManager, s5bProxy, idGen, connectionFactory, timerFactory, crypto.get(), iqRouter); } void tearDown() { delete ftTransportFactory; delete bytestreamServerManager; delete s5bProxy; delete idGen; delete s5bRegistry; delete connectionFactory; delete timerFactory; delete eventLoop; delete iqRouter; delete stanzaChannel; } void test_SendSessionInitiateOnStart() { boost::shared_ptr transfer = createTestling(); transfer->start(); FakeJingleSession::InitiateCall call = getCall(0); JingleFileTransferDescription::ref description = boost::dynamic_pointer_cast(call.description); CPPUNIT_ASSERT(description); CPPUNIT_ASSERT(static_cast(1048576) == description->getFileInfo().getSize()); JingleS5BTransportPayload::ref transport = boost::dynamic_pointer_cast(call.payload); CPPUNIT_ASSERT(transport); } void test_FallbackToIBBAfterFailingS5B() { boost::shared_ptr transfer = createTestling(); transfer->start(); FakeJingleSession::InitiateCall call = getCall(0); fakeJingleSession->handleSessionAcceptReceived(call.id, call.description, call.payload); // send candidate failure JingleS5BTransportPayload::ref candidateFailurePayload = boost::make_shared(); candidateFailurePayload->setCandidateError(true); candidateFailurePayload->setSessionID(call.payload->getSessionID()); fakeJingleSession->handleTransportInfoReceived(call.id, candidateFailurePayload); // no S5B candidates -> fallback to IBB // call at position 1 is the candidate our candidate error FakeJingleSession::ReplaceTransportCall replaceCall = getCall(2); // accept transport replace fakeJingleSession->handleTransportAcceptReceived(replaceCall.id, replaceCall.payload); IQ::ref iqOpenStanza = stanzaChannel->getStanzaAtIndex(0); CPPUNIT_ASSERT(iqOpenStanza); IBB::ref ibbOpen = iqOpenStanza->getPayload(); CPPUNIT_ASSERT(ibbOpen); CPPUNIT_ASSERT_EQUAL(IBB::Open, ibbOpen->getAction()); } void test_ReceiveSessionTerminateAfterSessionInitiate() { boost::shared_ptr transfer = createTestling(); transfer->start(); getCall(0); FTStatusHelper helper; helper.finishedCalled = false; transfer->onFinished.connect(bind(&FTStatusHelper::handleFileTransferFinished, &helper, _1)); fakeJingleSession->handleSessionTerminateReceived(JinglePayload::Reason(JinglePayload::Reason::Busy)); CPPUNIT_ASSERT_EQUAL(true, helper.finishedCalled); CPPUNIT_ASSERT(FileTransferError::PeerError == helper.error); } //TODO: some more testcases private: void addFileTransferDescription() { boost::shared_ptr desc = boost::make_shared(); desc->setFileInfo(JingleFileTransferFileInfo()); jingleContentPayload->addDescription(desc); } boost::shared_ptr addJingleS5BPayload() { JingleS5BTransportPayload::ref payLoad = boost::make_shared(); payLoad->setSessionID("mysession"); jingleContentPayload->addTransport(payLoad); return payLoad; } boost::shared_ptr addJingleIBBPayload() { JingleIBBTransportPayload::ref payLoad = boost::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: std::vector data; boost::shared_ptr stream; FakeJingleSession* fakeJingleSession; boost::shared_ptr jingleContentPayload; FileTransferTransporterFactory* ftTransportFactory; SOCKS5BytestreamServerManager* bytestreamServerManager; DummyStanzaChannel* stanzaChannel; IQRouter* iqRouter; IDGenerator* idGen; EventLoop *eventLoop; SOCKS5BytestreamRegistry* s5bRegistry; SOCKS5BytestreamProxiesManager* s5bProxy; DummyTimerFactory* timerFactory; DummyConnectionFactory* connectionFactory; DummyConnectionServerFactory* serverConnectionFactory; boost::shared_ptr crypto; NetworkEnvironment* networkEnvironment; NATTraverser* natTraverser; DomainNameResolver* resolver; }; CPPUNIT_TEST_SUITE_REGISTRATION(OutgoingJingleFileTransferTest);