diff options
author | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
commit | cfbdb43d2cadd40aa87338d41548e4bf89e146e6 (patch) | |
tree | 18d94153a302445196fc0c18586abf44a1ce4a38 /Swiften/Client/UnitTest | |
parent | 1d545a4a7fb877f021508094b88c1f17b30d8b4e (diff) | |
download | swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.zip swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.tar.bz2 |
Convert tabs to 4 spaces for all source files
Removed trailing spaces and whitespace on empty lines
in the process.
Changed CheckTabs.py tool to disallow hard tabs in source
files.
Test-Information:
Manually checked 30 random files that the conversion worked
as expected.
Change-Id: I874f99d617bd3d2bb55f02d58f22f58f9b094480
Diffstat (limited to 'Swiften/Client/UnitTest')
-rw-r--r-- | Swiften/Client/UnitTest/BlockListImplTest.cpp | 138 | ||||
-rw-r--r-- | Swiften/Client/UnitTest/ClientBlockListManagerTest.cpp | 328 | ||||
-rw-r--r-- | Swiften/Client/UnitTest/ClientSessionTest.cpp | 1560 | ||||
-rw-r--r-- | Swiften/Client/UnitTest/NickResolverTest.cpp | 256 |
4 files changed, 1141 insertions, 1141 deletions
diff --git a/Swiften/Client/UnitTest/BlockListImplTest.cpp b/Swiften/Client/UnitTest/BlockListImplTest.cpp index 9e5bc1a..0502f46 100644 --- a/Swiften/Client/UnitTest/BlockListImplTest.cpp +++ b/Swiften/Client/UnitTest/BlockListImplTest.cpp @@ -17,75 +17,75 @@ using namespace Swift; class BlockListImplTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(BlockListImplTest); - CPPUNIT_TEST(testSetItemsToSubset); - CPPUNIT_TEST(testSetItemsToSuperset); - CPPUNIT_TEST(testSetItemsAllDifferent); - CPPUNIT_TEST_SUITE_END(); - - public: - - void testSetItemsToSubset() { - std::vector<JID> subset; - subset.push_back(JID("a@example.com")); - - blockList_->setItems(subset); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), addedJIDs_.size()); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), removedJIDs_.size()); - } - - void testSetItemsToSuperset() { - std::vector<JID> superset; - superset.push_back(JID("a@example.com")); - superset.push_back(JID("b@example.com")); - superset.push_back(JID("c@example.com")); - - blockList_->setItems(superset); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), addedJIDs_.size()); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), removedJIDs_.size()); - } - - void testSetItemsAllDifferent() { - std::vector<JID> newBlockList; - newBlockList.push_back(JID("x@example.com")); - newBlockList.push_back(JID("y@example.com")); - newBlockList.push_back(JID("z@example.com")); - - blockList_->setItems(newBlockList); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), addedJIDs_.size()); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), removedJIDs_.size()); - } - - void setUp() { - blockList_ = boost::make_shared<BlockListImpl>(); - addedJIDs_.clear(); - removedJIDs_.clear(); - blockList_->addItem(JID("a@example.com")); - blockList_->addItem(JID("b@example.com")); - - blockList_->onItemAdded.connect(boost::bind(&BlockListImplTest::handleBlockListItemAdded, this, _1)); - blockList_->onItemRemoved.connect(boost::bind(&BlockListImplTest::handleBlockListItemRemoved, this, _1)); - } - - void tearDown() { - blockList_.reset(); - } - - void handleBlockListItemAdded(const JID& jid) { - addedJIDs_.push_back(jid); - } - - void handleBlockListItemRemoved(const JID& jid) { - removedJIDs_.push_back(jid); - } - - private: - boost::shared_ptr<BlockListImpl> blockList_; - std::vector<JID> addedJIDs_; - std::vector<JID> removedJIDs_; + CPPUNIT_TEST_SUITE(BlockListImplTest); + CPPUNIT_TEST(testSetItemsToSubset); + CPPUNIT_TEST(testSetItemsToSuperset); + CPPUNIT_TEST(testSetItemsAllDifferent); + CPPUNIT_TEST_SUITE_END(); + + public: + + void testSetItemsToSubset() { + std::vector<JID> subset; + subset.push_back(JID("a@example.com")); + + blockList_->setItems(subset); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), addedJIDs_.size()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), removedJIDs_.size()); + } + + void testSetItemsToSuperset() { + std::vector<JID> superset; + superset.push_back(JID("a@example.com")); + superset.push_back(JID("b@example.com")); + superset.push_back(JID("c@example.com")); + + blockList_->setItems(superset); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), addedJIDs_.size()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), removedJIDs_.size()); + } + + void testSetItemsAllDifferent() { + std::vector<JID> newBlockList; + newBlockList.push_back(JID("x@example.com")); + newBlockList.push_back(JID("y@example.com")); + newBlockList.push_back(JID("z@example.com")); + + blockList_->setItems(newBlockList); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), addedJIDs_.size()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), removedJIDs_.size()); + } + + void setUp() { + blockList_ = boost::make_shared<BlockListImpl>(); + addedJIDs_.clear(); + removedJIDs_.clear(); + blockList_->addItem(JID("a@example.com")); + blockList_->addItem(JID("b@example.com")); + + blockList_->onItemAdded.connect(boost::bind(&BlockListImplTest::handleBlockListItemAdded, this, _1)); + blockList_->onItemRemoved.connect(boost::bind(&BlockListImplTest::handleBlockListItemRemoved, this, _1)); + } + + void tearDown() { + blockList_.reset(); + } + + void handleBlockListItemAdded(const JID& jid) { + addedJIDs_.push_back(jid); + } + + void handleBlockListItemRemoved(const JID& jid) { + removedJIDs_.push_back(jid); + } + + private: + boost::shared_ptr<BlockListImpl> blockList_; + std::vector<JID> addedJIDs_; + std::vector<JID> removedJIDs_; }; CPPUNIT_TEST_SUITE_REGISTRATION(BlockListImplTest); diff --git a/Swiften/Client/UnitTest/ClientBlockListManagerTest.cpp b/Swiften/Client/UnitTest/ClientBlockListManagerTest.cpp index 53c39b5..ef2f537 100644 --- a/Swiften/Client/UnitTest/ClientBlockListManagerTest.cpp +++ b/Swiften/Client/UnitTest/ClientBlockListManagerTest.cpp @@ -25,170 +25,170 @@ using namespace Swift; class ClientBlockListManagerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ClientBlockListManagerTest); - CPPUNIT_TEST(testFetchBlockList); - CPPUNIT_TEST(testBlockCommand); - CPPUNIT_TEST(testUnblockCommand); - CPPUNIT_TEST(testUnblockAllCommand); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - ownJID_ = JID("kev@wonderland.lit"); - stanzaChannel_ = new DummyStanzaChannel(); - iqRouter_ = new IQRouter(stanzaChannel_); - iqRouter_->setJID(ownJID_); - clientBlockListManager_ = new ClientBlockListManager(iqRouter_); - } - - void testFetchBlockList() { - std::vector<JID> blockJids; - blockJids.push_back(JID("romeo@montague.net")); - blockJids.push_back(JID("iago@shakespeare.lit")); - helperInitialBlockListFetch(blockJids); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); - } - - void testBlockCommand() { - // start with an already fetched block list - helperInitialBlockListFetch(std::vector<JID>(1, JID("iago@shakespeare.lit"))); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), clientBlockListManager_->getBlockList()->getItems().size()); - CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); - - GenericRequest<BlockPayload>::ref blockRequest = clientBlockListManager_->createBlockJIDRequest(JID("romeo@montague.net")); - blockRequest->send(); - IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); - CPPUNIT_ASSERT(request.get() != NULL); - boost::shared_ptr<BlockPayload> blockPayload = request->getPayload<BlockPayload>(); - CPPUNIT_ASSERT(blockPayload.get() != NULL); - CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), blockPayload->getItems().at(0)); - - IQ::ref blockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); - stanzaChannel_->sendIQ(blockRequestResponse); - stanzaChannel_->onIQReceived(blockRequestResponse); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), clientBlockListManager_->getBlockList()->getItems().size()); - - // send block push - boost::shared_ptr<BlockPayload> pushPayload = boost::make_shared<BlockPayload>(); - pushPayload->addItem(JID("romeo@montague.net")); - IQ::ref blockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); - stanzaChannel_->sendIQ(blockPush); - stanzaChannel_->onIQReceived(blockPush); - - std::vector<JID> blockedJIDs = clientBlockListManager_->getBlockList()->getItems(); - CPPUNIT_ASSERT(blockedJIDs.end() != std::find(blockedJIDs.begin(), blockedJIDs.end(), JID("romeo@montague.net"))); - } - - void testUnblockCommand() { - // start with an already fetched block list - std::vector<JID> initialBlockList = std::vector<JID>(1, JID("iago@shakespeare.lit")); - initialBlockList.push_back(JID("romeo@montague.net")); - helperInitialBlockListFetch(initialBlockList); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); - CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); - - GenericRequest<UnblockPayload>::ref unblockRequest = clientBlockListManager_->createUnblockJIDRequest(JID("romeo@montague.net")); - unblockRequest->send(); - IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); - CPPUNIT_ASSERT(request.get() != NULL); - boost::shared_ptr<UnblockPayload> unblockPayload = request->getPayload<UnblockPayload>(); - CPPUNIT_ASSERT(unblockPayload.get() != NULL); - CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), unblockPayload->getItems().at(0)); - - IQ::ref unblockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); - stanzaChannel_->sendIQ(unblockRequestResponse); - stanzaChannel_->onIQReceived(unblockRequestResponse); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); - - // send block push - boost::shared_ptr<UnblockPayload> pushPayload = boost::make_shared<UnblockPayload>(); - pushPayload->addItem(JID("romeo@montague.net")); - IQ::ref unblockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); - stanzaChannel_->sendIQ(unblockPush); - stanzaChannel_->onIQReceived(unblockPush); - - std::vector<JID> blockedJIDs = clientBlockListManager_->getBlockList()->getItems(); - CPPUNIT_ASSERT(blockedJIDs.end() == std::find(blockedJIDs.begin(), blockedJIDs.end(), JID("romeo@montague.net"))); - } - - void testUnblockAllCommand() { - // start with an already fetched block list - std::vector<JID> initialBlockList = std::vector<JID>(1, JID("iago@shakespeare.lit")); - initialBlockList.push_back(JID("romeo@montague.net")); - initialBlockList.push_back(JID("benvolio@montague.net")); - helperInitialBlockListFetch(initialBlockList); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), clientBlockListManager_->getBlockList()->getItems().size()); - CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); - - GenericRequest<UnblockPayload>::ref unblockRequest = clientBlockListManager_->createUnblockAllRequest(); - unblockRequest->send(); - IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); - CPPUNIT_ASSERT(request.get() != NULL); - boost::shared_ptr<UnblockPayload> unblockPayload = request->getPayload<UnblockPayload>(); - CPPUNIT_ASSERT(unblockPayload.get() != NULL); - CPPUNIT_ASSERT_EQUAL(true, unblockPayload->getItems().empty()); - - IQ::ref unblockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); - stanzaChannel_->sendIQ(unblockRequestResponse); - stanzaChannel_->onIQReceived(unblockRequestResponse); - - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), clientBlockListManager_->getBlockList()->getItems().size()); - - // send block push - boost::shared_ptr<UnblockPayload> pushPayload = boost::make_shared<UnblockPayload>(); - IQ::ref unblockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); - stanzaChannel_->sendIQ(unblockPush); - stanzaChannel_->onIQReceived(unblockPush); - - CPPUNIT_ASSERT_EQUAL(true, clientBlockListManager_->getBlockList()->getItems().empty()); - } - - void tearDown() { - delete clientBlockListManager_; - delete iqRouter_; - delete stanzaChannel_; - } - - private: - void helperInitialBlockListFetch(const std::vector<JID>& blockedJids) { - boost::shared_ptr<BlockList> blockList = clientBlockListManager_->requestBlockList(); - CPPUNIT_ASSERT(blockList); - - // check for IQ request - IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(0); - CPPUNIT_ASSERT(request.get() != NULL); - boost::shared_ptr<BlockListPayload> requestPayload = request->getPayload<BlockListPayload>(); - CPPUNIT_ASSERT(requestPayload.get() != NULL); - - CPPUNIT_ASSERT_EQUAL(BlockList::Requesting, blockList->getState()); - CPPUNIT_ASSERT_EQUAL(BlockList::Requesting, clientBlockListManager_->getBlockList()->getState()); - - // build IQ response - boost::shared_ptr<BlockListPayload> responsePayload = boost::make_shared<BlockListPayload>(); - foreach(const JID& jid, blockedJids) { - responsePayload->addItem(jid); - } - - IQ::ref response = IQ::createResult(ownJID_, JID(), request->getID(), responsePayload); - stanzaChannel_->sendIQ(response); - stanzaChannel_->onIQReceived(response); - - CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); - CPPUNIT_ASSERT(responsePayload->getItems() == clientBlockListManager_->getBlockList()->getItems()); - } - - - private: - JID ownJID_; - IQRouter* iqRouter_; - DummyStanzaChannel* stanzaChannel_; - ClientBlockListManager* clientBlockListManager_; + CPPUNIT_TEST_SUITE(ClientBlockListManagerTest); + CPPUNIT_TEST(testFetchBlockList); + CPPUNIT_TEST(testBlockCommand); + CPPUNIT_TEST(testUnblockCommand); + CPPUNIT_TEST(testUnblockAllCommand); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + ownJID_ = JID("kev@wonderland.lit"); + stanzaChannel_ = new DummyStanzaChannel(); + iqRouter_ = new IQRouter(stanzaChannel_); + iqRouter_->setJID(ownJID_); + clientBlockListManager_ = new ClientBlockListManager(iqRouter_); + } + + void testFetchBlockList() { + std::vector<JID> blockJids; + blockJids.push_back(JID("romeo@montague.net")); + blockJids.push_back(JID("iago@shakespeare.lit")); + helperInitialBlockListFetch(blockJids); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); + } + + void testBlockCommand() { + // start with an already fetched block list + helperInitialBlockListFetch(std::vector<JID>(1, JID("iago@shakespeare.lit"))); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), clientBlockListManager_->getBlockList()->getItems().size()); + CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); + + GenericRequest<BlockPayload>::ref blockRequest = clientBlockListManager_->createBlockJIDRequest(JID("romeo@montague.net")); + blockRequest->send(); + IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); + CPPUNIT_ASSERT(request.get() != NULL); + boost::shared_ptr<BlockPayload> blockPayload = request->getPayload<BlockPayload>(); + CPPUNIT_ASSERT(blockPayload.get() != NULL); + CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), blockPayload->getItems().at(0)); + + IQ::ref blockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); + stanzaChannel_->sendIQ(blockRequestResponse); + stanzaChannel_->onIQReceived(blockRequestResponse); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), clientBlockListManager_->getBlockList()->getItems().size()); + + // send block push + boost::shared_ptr<BlockPayload> pushPayload = boost::make_shared<BlockPayload>(); + pushPayload->addItem(JID("romeo@montague.net")); + IQ::ref blockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); + stanzaChannel_->sendIQ(blockPush); + stanzaChannel_->onIQReceived(blockPush); + + std::vector<JID> blockedJIDs = clientBlockListManager_->getBlockList()->getItems(); + CPPUNIT_ASSERT(blockedJIDs.end() != std::find(blockedJIDs.begin(), blockedJIDs.end(), JID("romeo@montague.net"))); + } + + void testUnblockCommand() { + // start with an already fetched block list + std::vector<JID> initialBlockList = std::vector<JID>(1, JID("iago@shakespeare.lit")); + initialBlockList.push_back(JID("romeo@montague.net")); + helperInitialBlockListFetch(initialBlockList); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); + CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); + + GenericRequest<UnblockPayload>::ref unblockRequest = clientBlockListManager_->createUnblockJIDRequest(JID("romeo@montague.net")); + unblockRequest->send(); + IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); + CPPUNIT_ASSERT(request.get() != NULL); + boost::shared_ptr<UnblockPayload> unblockPayload = request->getPayload<UnblockPayload>(); + CPPUNIT_ASSERT(unblockPayload.get() != NULL); + CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), unblockPayload->getItems().at(0)); + + IQ::ref unblockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); + stanzaChannel_->sendIQ(unblockRequestResponse); + stanzaChannel_->onIQReceived(unblockRequestResponse); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), clientBlockListManager_->getBlockList()->getItems().size()); + + // send block push + boost::shared_ptr<UnblockPayload> pushPayload = boost::make_shared<UnblockPayload>(); + pushPayload->addItem(JID("romeo@montague.net")); + IQ::ref unblockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); + stanzaChannel_->sendIQ(unblockPush); + stanzaChannel_->onIQReceived(unblockPush); + + std::vector<JID> blockedJIDs = clientBlockListManager_->getBlockList()->getItems(); + CPPUNIT_ASSERT(blockedJIDs.end() == std::find(blockedJIDs.begin(), blockedJIDs.end(), JID("romeo@montague.net"))); + } + + void testUnblockAllCommand() { + // start with an already fetched block list + std::vector<JID> initialBlockList = std::vector<JID>(1, JID("iago@shakespeare.lit")); + initialBlockList.push_back(JID("romeo@montague.net")); + initialBlockList.push_back(JID("benvolio@montague.net")); + helperInitialBlockListFetch(initialBlockList); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), clientBlockListManager_->getBlockList()->getItems().size()); + CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); + + GenericRequest<UnblockPayload>::ref unblockRequest = clientBlockListManager_->createUnblockAllRequest(); + unblockRequest->send(); + IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(2); + CPPUNIT_ASSERT(request.get() != NULL); + boost::shared_ptr<UnblockPayload> unblockPayload = request->getPayload<UnblockPayload>(); + CPPUNIT_ASSERT(unblockPayload.get() != NULL); + CPPUNIT_ASSERT_EQUAL(true, unblockPayload->getItems().empty()); + + IQ::ref unblockRequestResponse = IQ::createResult(request->getFrom(), JID(), request->getID()); + stanzaChannel_->sendIQ(unblockRequestResponse); + stanzaChannel_->onIQReceived(unblockRequestResponse); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), clientBlockListManager_->getBlockList()->getItems().size()); + + // send block push + boost::shared_ptr<UnblockPayload> pushPayload = boost::make_shared<UnblockPayload>(); + IQ::ref unblockPush = IQ::createRequest(IQ::Set, ownJID_, "push1", pushPayload); + stanzaChannel_->sendIQ(unblockPush); + stanzaChannel_->onIQReceived(unblockPush); + + CPPUNIT_ASSERT_EQUAL(true, clientBlockListManager_->getBlockList()->getItems().empty()); + } + + void tearDown() { + delete clientBlockListManager_; + delete iqRouter_; + delete stanzaChannel_; + } + + private: + void helperInitialBlockListFetch(const std::vector<JID>& blockedJids) { + boost::shared_ptr<BlockList> blockList = clientBlockListManager_->requestBlockList(); + CPPUNIT_ASSERT(blockList); + + // check for IQ request + IQ::ref request = stanzaChannel_->getStanzaAtIndex<IQ>(0); + CPPUNIT_ASSERT(request.get() != NULL); + boost::shared_ptr<BlockListPayload> requestPayload = request->getPayload<BlockListPayload>(); + CPPUNIT_ASSERT(requestPayload.get() != NULL); + + CPPUNIT_ASSERT_EQUAL(BlockList::Requesting, blockList->getState()); + CPPUNIT_ASSERT_EQUAL(BlockList::Requesting, clientBlockListManager_->getBlockList()->getState()); + + // build IQ response + boost::shared_ptr<BlockListPayload> responsePayload = boost::make_shared<BlockListPayload>(); + foreach(const JID& jid, blockedJids) { + responsePayload->addItem(jid); + } + + IQ::ref response = IQ::createResult(ownJID_, JID(), request->getID(), responsePayload); + stanzaChannel_->sendIQ(response); + stanzaChannel_->onIQReceived(response); + + CPPUNIT_ASSERT_EQUAL(BlockList::Available, clientBlockListManager_->getBlockList()->getState()); + CPPUNIT_ASSERT(responsePayload->getItems() == clientBlockListManager_->getBlockList()->getItems()); + } + + + private: + JID ownJID_; + IQRouter* iqRouter_; + DummyStanzaChannel* stanzaChannel_; + ClientBlockListManager* clientBlockListManager_; }; CPPUNIT_TEST_SUITE_REGISTRATION(ClientBlockListManagerTest); diff --git a/Swiften/Client/UnitTest/ClientSessionTest.cpp b/Swiften/Client/UnitTest/ClientSessionTest.cpp index 396edf6..335b537 100644 --- a/Swiften/Client/UnitTest/ClientSessionTest.cpp +++ b/Swiften/Client/UnitTest/ClientSessionTest.cpp @@ -41,789 +41,789 @@ using namespace Swift; class ClientSessionTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ClientSessionTest); - CPPUNIT_TEST(testStart_Error); - CPPUNIT_TEST(testStart_StreamError); - CPPUNIT_TEST(testStartTLS); - CPPUNIT_TEST(testStartTLS_ServerError); - CPPUNIT_TEST(testStartTLS_ConnectError); - CPPUNIT_TEST(testStartTLS_InvalidIdentity); - CPPUNIT_TEST(testStart_StreamFeaturesWithoutResourceBindingFails); - CPPUNIT_TEST(testAuthenticate); - CPPUNIT_TEST(testAuthenticate_Unauthorized); - CPPUNIT_TEST(testAuthenticate_NoValidAuthMechanisms); - CPPUNIT_TEST(testAuthenticate_PLAINOverNonTLS); - CPPUNIT_TEST(testAuthenticate_RequireTLS); - CPPUNIT_TEST(testAuthenticate_EXTERNAL); - CPPUNIT_TEST(testStreamManagement); - CPPUNIT_TEST(testStreamManagement_Failed); - CPPUNIT_TEST(testUnexpectedChallenge); - CPPUNIT_TEST(testFinishAcksStanzas); - /* - CPPUNIT_TEST(testResourceBind); - CPPUNIT_TEST(testResourceBind_ChangeResource); - CPPUNIT_TEST(testResourceBind_EmptyResource); - CPPUNIT_TEST(testResourceBind_Error); - CPPUNIT_TEST(testSessionStart); - CPPUNIT_TEST(testSessionStart_Error); - CPPUNIT_TEST(testSessionStart_AfterResourceBind); - CPPUNIT_TEST(testWhitespacePing); - CPPUNIT_TEST(testReceiveElementAfterSessionStarted); - CPPUNIT_TEST(testSendElement); - */ - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); - idnConverter = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); - server = boost::make_shared<MockSessionStream>(); - sessionFinishedReceived = false; - needCredentials = false; - blindCertificateTrustChecker = new BlindCertificateTrustChecker(); - } - - void tearDown() { - delete blindCertificateTrustChecker; - } - - void testStart_Error() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->breakConnection(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testStart_StreamError() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->sendStreamStart(); - server->sendStreamError(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testStartTLS() { - boost::shared_ptr<ClientSession> session(createSession()); - session->setCertificateTrustChecker(blindCertificateTrustChecker); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithStartTLS(); - server->receiveStartTLS(); - CPPUNIT_ASSERT(!server->tlsEncrypted); - server->sendTLSProceed(); - CPPUNIT_ASSERT(server->tlsEncrypted); - server->onTLSEncrypted(); - server->receiveStreamStart(); - server->sendStreamStart(); - - session->finish(); - } - - void testStartTLS_ServerError() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithStartTLS(); - server->receiveStartTLS(); - server->sendTLSFailure(); - - CPPUNIT_ASSERT(!server->tlsEncrypted); - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testStartTLS_ConnectError() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithStartTLS(); - server->receiveStartTLS(); - server->sendTLSProceed(); - server->breakTLS(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testStartTLS_InvalidIdentity() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithStartTLS(); - server->receiveStartTLS(); - CPPUNIT_ASSERT(!server->tlsEncrypted); - server->sendTLSProceed(); - CPPUNIT_ASSERT(server->tlsEncrypted); - server->onTLSEncrypted(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidServerIdentity, boost::dynamic_pointer_cast<CertificateVerificationError>(sessionFinishedError)->getType()); - } - - void testStart_StreamFeaturesWithoutResourceBindingFails() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendEmptyStreamFeatures(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testAuthenticate() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - CPPUNIT_ASSERT(needCredentials); - CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); - session->sendCredentials(createSafeByteArray("mypass")); - server->receiveAuthRequest("PLAIN"); - server->sendAuthSuccess(); - server->receiveStreamStart(); - - session->finish(); - } - - void testAuthenticate_Unauthorized() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - CPPUNIT_ASSERT(needCredentials); - CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); - session->sendCredentials(createSafeByteArray("mypass")); - server->receiveAuthRequest("PLAIN"); - server->sendAuthFailure(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testAuthenticate_PLAINOverNonTLS() { - boost::shared_ptr<ClientSession> session(createSession()); - session->setAllowPLAINOverNonTLS(false); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testAuthenticate_RequireTLS() { - boost::shared_ptr<ClientSession> session(createSession()); - session->setUseTLS(ClientSession::RequireTLS); - session->setAllowPLAINOverNonTLS(true); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithMultipleAuthentication(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testAuthenticate_NoValidAuthMechanisms() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithUnknownAuthentication(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testAuthenticate_EXTERNAL() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithEXTERNALAuthentication(); - server->receiveAuthRequest("EXTERNAL"); - server->sendAuthSuccess(); - server->receiveStreamStart(); - - session->finish(); - } - - void testUnexpectedChallenge() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithEXTERNALAuthentication(); - server->receiveAuthRequest("EXTERNAL"); - server->sendChallenge(); - server->sendChallenge(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); - CPPUNIT_ASSERT(sessionFinishedReceived); - CPPUNIT_ASSERT(sessionFinishedError); - } - - void testStreamManagement() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - session->sendCredentials(createSafeByteArray("mypass")); - server->receiveAuthRequest("PLAIN"); - server->sendAuthSuccess(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithBindAndStreamManagement(); - server->receiveBind(); - server->sendBindResult(); - server->receiveStreamManagementEnable(); - server->sendStreamManagementEnabled(); - - CPPUNIT_ASSERT(session->getStreamManagementEnabled()); - // TODO: Test if the requesters & responders do their work - CPPUNIT_ASSERT_EQUAL(ClientSession::Initialized, session->getState()); - - session->finish(); - } - - void testStreamManagement_Failed() { - boost::shared_ptr<ClientSession> session(createSession()); - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - session->sendCredentials(createSafeByteArray("mypass")); - server->receiveAuthRequest("PLAIN"); - server->sendAuthSuccess(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithBindAndStreamManagement(); - server->receiveBind(); - server->sendBindResult(); - server->receiveStreamManagementEnable(); - server->sendStreamManagementFailed(); - - CPPUNIT_ASSERT(!session->getStreamManagementEnabled()); - CPPUNIT_ASSERT_EQUAL(ClientSession::Initialized, session->getState()); - - session->finish(); - } - - void testFinishAcksStanzas() { - boost::shared_ptr<ClientSession> session(createSession()); - initializeSession(session); - server->sendMessage(); - server->sendMessage(); - server->sendMessage(); - - session->finish(); - - server->receiveAck(3); - } - - private: - boost::shared_ptr<ClientSession> createSession() { - boost::shared_ptr<ClientSession> session = ClientSession::create(JID("me@foo.com"), server, idnConverter.get(), crypto.get()); - session->onFinished.connect(boost::bind(&ClientSessionTest::handleSessionFinished, this, _1)); - session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::handleSessionNeedCredentials, this)); - session->setAllowPLAINOverNonTLS(true); - return session; - } - - void initializeSession(boost::shared_ptr<ClientSession> session) { - session->start(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithPLAINAuthentication(); - session->sendCredentials(createSafeByteArray("mypass")); - server->receiveAuthRequest("PLAIN"); - server->sendAuthSuccess(); - server->receiveStreamStart(); - server->sendStreamStart(); - server->sendStreamFeaturesWithBindAndStreamManagement(); - server->receiveBind(); - server->sendBindResult(); - server->receiveStreamManagementEnable(); - server->sendStreamManagementEnabled(); - } - - void handleSessionFinished(boost::shared_ptr<Error> error) { - sessionFinishedReceived = true; - sessionFinishedError = error; - } - - void handleSessionNeedCredentials() { - needCredentials = true; - } - - class MockSessionStream : public SessionStream { - public: - struct Event { - Event(boost::shared_ptr<ToplevelElement> element) : element(element), footer(false) {} - Event(const ProtocolHeader& header) : header(header), footer(false) {} - Event() : footer(true) {} - - boost::shared_ptr<ToplevelElement> element; - boost::optional<ProtocolHeader> header; - bool footer; - }; - - MockSessionStream() : available(true), canTLSEncrypt(true), tlsEncrypted(false), compressed(false), whitespacePingEnabled(false), resetCount(0) { - } - - virtual void close() { - onClosed(boost::shared_ptr<Error>()); - } - - virtual bool isOpen() { - return available; - } - - virtual void writeHeader(const ProtocolHeader& header) { - receivedEvents.push_back(Event(header)); - } - - virtual void writeFooter() { - receivedEvents.push_back(Event()); - } - - virtual void writeElement(boost::shared_ptr<ToplevelElement> element) { - receivedEvents.push_back(Event(element)); - } - - virtual void writeData(const std::string&) { - } - - virtual bool supportsTLSEncryption() { - return canTLSEncrypt; - } - - virtual void addTLSEncryption() { - tlsEncrypted = true; - } - - virtual bool isTLSEncrypted() { - return tlsEncrypted; - } - - virtual ByteArray getTLSFinishMessage() const { - return ByteArray(); - } - - virtual Certificate::ref getPeerCertificate() const { - return Certificate::ref(new SimpleCertificate()); - } - - virtual std::vector<Certificate::ref> getPeerCertificateChain() const { - return std::vector<Certificate::ref>(); - } - - virtual boost::shared_ptr<CertificateVerificationError> getPeerCertificateVerificationError() const { - return boost::shared_ptr<CertificateVerificationError>(); - } - - virtual bool supportsZLibCompression() { - return true; - } - - virtual void addZLibCompression() { - compressed = true; - } - - virtual void setWhitespacePingEnabled(bool enabled) { - whitespacePingEnabled = enabled; - } - - virtual void resetXMPPParser() { - resetCount++; - } - - void breakConnection() { - onClosed(boost::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::ConnectionReadError)); - } - - void breakTLS() { - onClosed(boost::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::TLSError)); - } - - - void sendStreamStart() { - ProtocolHeader header; - header.setTo("foo.com"); - return onStreamStartReceived(header); - } - - void sendStreamFeaturesWithStartTLS() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->setHasStartTLS(); - onElementReceived(streamFeatures); - } - - void sendChallenge() { - onElementReceived(boost::make_shared<AuthChallenge>()); - } - - void sendStreamError() { - onElementReceived(boost::make_shared<StreamError>()); - } - - void sendTLSProceed() { - onElementReceived(boost::make_shared<TLSProceed>()); - } - - void sendTLSFailure() { - onElementReceived(boost::make_shared<StartTLSFailure>()); - } - - void sendStreamFeaturesWithMultipleAuthentication() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->addAuthenticationMechanism("PLAIN"); - streamFeatures->addAuthenticationMechanism("DIGEST-MD5"); - streamFeatures->addAuthenticationMechanism("SCRAM-SHA1"); - onElementReceived(streamFeatures); - } - - void sendStreamFeaturesWithPLAINAuthentication() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->addAuthenticationMechanism("PLAIN"); - onElementReceived(streamFeatures); - } - - void sendStreamFeaturesWithEXTERNALAuthentication() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->addAuthenticationMechanism("EXTERNAL"); - onElementReceived(streamFeatures); - } - - void sendStreamFeaturesWithUnknownAuthentication() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->addAuthenticationMechanism("UNKNOWN"); - onElementReceived(streamFeatures); - } - - void sendStreamFeaturesWithBindAndStreamManagement() { - boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); - streamFeatures->setHasResourceBind(); - streamFeatures->setHasStreamManagement(); - onElementReceived(streamFeatures); - } - - void sendEmptyStreamFeatures() { - onElementReceived(boost::make_shared<StreamFeatures>()); - } - - void sendAuthSuccess() { - onElementReceived(boost::make_shared<AuthSuccess>()); - } - - void sendAuthFailure() { - onElementReceived(boost::make_shared<AuthFailure>()); - } - - void sendStreamManagementEnabled() { - onElementReceived(boost::make_shared<StreamManagementEnabled>()); - } - - void sendStreamManagementFailed() { - onElementReceived(boost::make_shared<StreamManagementFailed>()); - } - - void sendBindResult() { - boost::shared_ptr<ResourceBind> resourceBind(new ResourceBind()); - resourceBind->setJID(JID("foo@bar.com/bla")); - boost::shared_ptr<IQ> iq = IQ::createResult(JID("foo@bar.com"), bindID, resourceBind); - onElementReceived(iq); - } - - void sendMessage() { - boost::shared_ptr<Message> message = boost::make_shared<Message>(); - message->setTo(JID("foo@bar.com/bla")); - onElementReceived(message); - } - - void receiveStreamStart() { - Event event = popEvent(); - CPPUNIT_ASSERT(event.header); - } - - void receiveStartTLS() { - Event event = popEvent(); - CPPUNIT_ASSERT(event.element); - CPPUNIT_ASSERT(boost::dynamic_pointer_cast<StartTLSRequest>(event.element)); - } - - void receiveAuthRequest(const std::string& mech) { - Event event = popEvent(); - CPPUNIT_ASSERT(event.element); - boost::shared_ptr<AuthRequest> request(boost::dynamic_pointer_cast<AuthRequest>(event.element)); - CPPUNIT_ASSERT(request); - CPPUNIT_ASSERT_EQUAL(mech, request->getMechanism()); - } - - void receiveStreamManagementEnable() { - Event event = popEvent(); - CPPUNIT_ASSERT(event.element); - CPPUNIT_ASSERT(boost::dynamic_pointer_cast<EnableStreamManagement>(event.element)); - } - - void receiveBind() { - Event event = popEvent(); - CPPUNIT_ASSERT(event.element); - boost::shared_ptr<IQ> iq = boost::dynamic_pointer_cast<IQ>(event.element); - CPPUNIT_ASSERT(iq); - CPPUNIT_ASSERT(iq->getPayload<ResourceBind>()); - bindID = iq->getID(); - } - - void receiveAck(unsigned int n) { - Event event = popEvent(); - CPPUNIT_ASSERT(event.element); - boost::shared_ptr<StanzaAck> ack = boost::dynamic_pointer_cast<StanzaAck>(event.element); - CPPUNIT_ASSERT(ack); - CPPUNIT_ASSERT_EQUAL(n, ack->getHandledStanzasCount()); - } - - Event popEvent() { - CPPUNIT_ASSERT(!receivedEvents.empty()); - Event event = receivedEvents.front(); - receivedEvents.pop_front(); - return event; - } - - bool available; - bool canTLSEncrypt; - bool tlsEncrypted; - bool compressed; - bool whitespacePingEnabled; - std::string bindID; - int resetCount; - std::deque<Event> receivedEvents; - }; - - boost::shared_ptr<IDNConverter> idnConverter; - boost::shared_ptr<MockSessionStream> server; - bool sessionFinishedReceived; - bool needCredentials; - boost::shared_ptr<Error> sessionFinishedError; - BlindCertificateTrustChecker* blindCertificateTrustChecker; - boost::shared_ptr<CryptoProvider> crypto; + CPPUNIT_TEST_SUITE(ClientSessionTest); + CPPUNIT_TEST(testStart_Error); + CPPUNIT_TEST(testStart_StreamError); + CPPUNIT_TEST(testStartTLS); + CPPUNIT_TEST(testStartTLS_ServerError); + CPPUNIT_TEST(testStartTLS_ConnectError); + CPPUNIT_TEST(testStartTLS_InvalidIdentity); + CPPUNIT_TEST(testStart_StreamFeaturesWithoutResourceBindingFails); + CPPUNIT_TEST(testAuthenticate); + CPPUNIT_TEST(testAuthenticate_Unauthorized); + CPPUNIT_TEST(testAuthenticate_NoValidAuthMechanisms); + CPPUNIT_TEST(testAuthenticate_PLAINOverNonTLS); + CPPUNIT_TEST(testAuthenticate_RequireTLS); + CPPUNIT_TEST(testAuthenticate_EXTERNAL); + CPPUNIT_TEST(testStreamManagement); + CPPUNIT_TEST(testStreamManagement_Failed); + CPPUNIT_TEST(testUnexpectedChallenge); + CPPUNIT_TEST(testFinishAcksStanzas); + /* + CPPUNIT_TEST(testResourceBind); + CPPUNIT_TEST(testResourceBind_ChangeResource); + CPPUNIT_TEST(testResourceBind_EmptyResource); + CPPUNIT_TEST(testResourceBind_Error); + CPPUNIT_TEST(testSessionStart); + CPPUNIT_TEST(testSessionStart_Error); + CPPUNIT_TEST(testSessionStart_AfterResourceBind); + CPPUNIT_TEST(testWhitespacePing); + CPPUNIT_TEST(testReceiveElementAfterSessionStarted); + CPPUNIT_TEST(testSendElement); + */ + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); + idnConverter = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); + server = boost::make_shared<MockSessionStream>(); + sessionFinishedReceived = false; + needCredentials = false; + blindCertificateTrustChecker = new BlindCertificateTrustChecker(); + } + + void tearDown() { + delete blindCertificateTrustChecker; + } + + void testStart_Error() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->breakConnection(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testStart_StreamError() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->sendStreamStart(); + server->sendStreamError(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testStartTLS() { + boost::shared_ptr<ClientSession> session(createSession()); + session->setCertificateTrustChecker(blindCertificateTrustChecker); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithStartTLS(); + server->receiveStartTLS(); + CPPUNIT_ASSERT(!server->tlsEncrypted); + server->sendTLSProceed(); + CPPUNIT_ASSERT(server->tlsEncrypted); + server->onTLSEncrypted(); + server->receiveStreamStart(); + server->sendStreamStart(); + + session->finish(); + } + + void testStartTLS_ServerError() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithStartTLS(); + server->receiveStartTLS(); + server->sendTLSFailure(); + + CPPUNIT_ASSERT(!server->tlsEncrypted); + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testStartTLS_ConnectError() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithStartTLS(); + server->receiveStartTLS(); + server->sendTLSProceed(); + server->breakTLS(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testStartTLS_InvalidIdentity() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithStartTLS(); + server->receiveStartTLS(); + CPPUNIT_ASSERT(!server->tlsEncrypted); + server->sendTLSProceed(); + CPPUNIT_ASSERT(server->tlsEncrypted); + server->onTLSEncrypted(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidServerIdentity, boost::dynamic_pointer_cast<CertificateVerificationError>(sessionFinishedError)->getType()); + } + + void testStart_StreamFeaturesWithoutResourceBindingFails() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendEmptyStreamFeatures(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testAuthenticate() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + CPPUNIT_ASSERT(needCredentials); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); + session->sendCredentials(createSafeByteArray("mypass")); + server->receiveAuthRequest("PLAIN"); + server->sendAuthSuccess(); + server->receiveStreamStart(); + + session->finish(); + } + + void testAuthenticate_Unauthorized() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + CPPUNIT_ASSERT(needCredentials); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); + session->sendCredentials(createSafeByteArray("mypass")); + server->receiveAuthRequest("PLAIN"); + server->sendAuthFailure(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testAuthenticate_PLAINOverNonTLS() { + boost::shared_ptr<ClientSession> session(createSession()); + session->setAllowPLAINOverNonTLS(false); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testAuthenticate_RequireTLS() { + boost::shared_ptr<ClientSession> session(createSession()); + session->setUseTLS(ClientSession::RequireTLS); + session->setAllowPLAINOverNonTLS(true); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithMultipleAuthentication(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testAuthenticate_NoValidAuthMechanisms() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithUnknownAuthentication(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testAuthenticate_EXTERNAL() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithEXTERNALAuthentication(); + server->receiveAuthRequest("EXTERNAL"); + server->sendAuthSuccess(); + server->receiveStreamStart(); + + session->finish(); + } + + void testUnexpectedChallenge() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithEXTERNALAuthentication(); + server->receiveAuthRequest("EXTERNAL"); + server->sendChallenge(); + server->sendChallenge(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Finished, session->getState()); + CPPUNIT_ASSERT(sessionFinishedReceived); + CPPUNIT_ASSERT(sessionFinishedError); + } + + void testStreamManagement() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + session->sendCredentials(createSafeByteArray("mypass")); + server->receiveAuthRequest("PLAIN"); + server->sendAuthSuccess(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithBindAndStreamManagement(); + server->receiveBind(); + server->sendBindResult(); + server->receiveStreamManagementEnable(); + server->sendStreamManagementEnabled(); + + CPPUNIT_ASSERT(session->getStreamManagementEnabled()); + // TODO: Test if the requesters & responders do their work + CPPUNIT_ASSERT_EQUAL(ClientSession::Initialized, session->getState()); + + session->finish(); + } + + void testStreamManagement_Failed() { + boost::shared_ptr<ClientSession> session(createSession()); + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + session->sendCredentials(createSafeByteArray("mypass")); + server->receiveAuthRequest("PLAIN"); + server->sendAuthSuccess(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithBindAndStreamManagement(); + server->receiveBind(); + server->sendBindResult(); + server->receiveStreamManagementEnable(); + server->sendStreamManagementFailed(); + + CPPUNIT_ASSERT(!session->getStreamManagementEnabled()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Initialized, session->getState()); + + session->finish(); + } + + void testFinishAcksStanzas() { + boost::shared_ptr<ClientSession> session(createSession()); + initializeSession(session); + server->sendMessage(); + server->sendMessage(); + server->sendMessage(); + + session->finish(); + + server->receiveAck(3); + } + + private: + boost::shared_ptr<ClientSession> createSession() { + boost::shared_ptr<ClientSession> session = ClientSession::create(JID("me@foo.com"), server, idnConverter.get(), crypto.get()); + session->onFinished.connect(boost::bind(&ClientSessionTest::handleSessionFinished, this, _1)); + session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::handleSessionNeedCredentials, this)); + session->setAllowPLAINOverNonTLS(true); + return session; + } + + void initializeSession(boost::shared_ptr<ClientSession> session) { + session->start(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithPLAINAuthentication(); + session->sendCredentials(createSafeByteArray("mypass")); + server->receiveAuthRequest("PLAIN"); + server->sendAuthSuccess(); + server->receiveStreamStart(); + server->sendStreamStart(); + server->sendStreamFeaturesWithBindAndStreamManagement(); + server->receiveBind(); + server->sendBindResult(); + server->receiveStreamManagementEnable(); + server->sendStreamManagementEnabled(); + } + + void handleSessionFinished(boost::shared_ptr<Error> error) { + sessionFinishedReceived = true; + sessionFinishedError = error; + } + + void handleSessionNeedCredentials() { + needCredentials = true; + } + + class MockSessionStream : public SessionStream { + public: + struct Event { + Event(boost::shared_ptr<ToplevelElement> element) : element(element), footer(false) {} + Event(const ProtocolHeader& header) : header(header), footer(false) {} + Event() : footer(true) {} + + boost::shared_ptr<ToplevelElement> element; + boost::optional<ProtocolHeader> header; + bool footer; + }; + + MockSessionStream() : available(true), canTLSEncrypt(true), tlsEncrypted(false), compressed(false), whitespacePingEnabled(false), resetCount(0) { + } + + virtual void close() { + onClosed(boost::shared_ptr<Error>()); + } + + virtual bool isOpen() { + return available; + } + + virtual void writeHeader(const ProtocolHeader& header) { + receivedEvents.push_back(Event(header)); + } + + virtual void writeFooter() { + receivedEvents.push_back(Event()); + } + + virtual void writeElement(boost::shared_ptr<ToplevelElement> element) { + receivedEvents.push_back(Event(element)); + } + + virtual void writeData(const std::string&) { + } + + virtual bool supportsTLSEncryption() { + return canTLSEncrypt; + } + + virtual void addTLSEncryption() { + tlsEncrypted = true; + } + + virtual bool isTLSEncrypted() { + return tlsEncrypted; + } + + virtual ByteArray getTLSFinishMessage() const { + return ByteArray(); + } + + virtual Certificate::ref getPeerCertificate() const { + return Certificate::ref(new SimpleCertificate()); + } + + virtual std::vector<Certificate::ref> getPeerCertificateChain() const { + return std::vector<Certificate::ref>(); + } + + virtual boost::shared_ptr<CertificateVerificationError> getPeerCertificateVerificationError() const { + return boost::shared_ptr<CertificateVerificationError>(); + } + + virtual bool supportsZLibCompression() { + return true; + } + + virtual void addZLibCompression() { + compressed = true; + } + + virtual void setWhitespacePingEnabled(bool enabled) { + whitespacePingEnabled = enabled; + } + + virtual void resetXMPPParser() { + resetCount++; + } + + void breakConnection() { + onClosed(boost::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::ConnectionReadError)); + } + + void breakTLS() { + onClosed(boost::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::TLSError)); + } + + + void sendStreamStart() { + ProtocolHeader header; + header.setTo("foo.com"); + return onStreamStartReceived(header); + } + + void sendStreamFeaturesWithStartTLS() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->setHasStartTLS(); + onElementReceived(streamFeatures); + } + + void sendChallenge() { + onElementReceived(boost::make_shared<AuthChallenge>()); + } + + void sendStreamError() { + onElementReceived(boost::make_shared<StreamError>()); + } + + void sendTLSProceed() { + onElementReceived(boost::make_shared<TLSProceed>()); + } + + void sendTLSFailure() { + onElementReceived(boost::make_shared<StartTLSFailure>()); + } + + void sendStreamFeaturesWithMultipleAuthentication() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->addAuthenticationMechanism("PLAIN"); + streamFeatures->addAuthenticationMechanism("DIGEST-MD5"); + streamFeatures->addAuthenticationMechanism("SCRAM-SHA1"); + onElementReceived(streamFeatures); + } + + void sendStreamFeaturesWithPLAINAuthentication() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->addAuthenticationMechanism("PLAIN"); + onElementReceived(streamFeatures); + } + + void sendStreamFeaturesWithEXTERNALAuthentication() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->addAuthenticationMechanism("EXTERNAL"); + onElementReceived(streamFeatures); + } + + void sendStreamFeaturesWithUnknownAuthentication() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->addAuthenticationMechanism("UNKNOWN"); + onElementReceived(streamFeatures); + } + + void sendStreamFeaturesWithBindAndStreamManagement() { + boost::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures()); + streamFeatures->setHasResourceBind(); + streamFeatures->setHasStreamManagement(); + onElementReceived(streamFeatures); + } + + void sendEmptyStreamFeatures() { + onElementReceived(boost::make_shared<StreamFeatures>()); + } + + void sendAuthSuccess() { + onElementReceived(boost::make_shared<AuthSuccess>()); + } + + void sendAuthFailure() { + onElementReceived(boost::make_shared<AuthFailure>()); + } + + void sendStreamManagementEnabled() { + onElementReceived(boost::make_shared<StreamManagementEnabled>()); + } + + void sendStreamManagementFailed() { + onElementReceived(boost::make_shared<StreamManagementFailed>()); + } + + void sendBindResult() { + boost::shared_ptr<ResourceBind> resourceBind(new ResourceBind()); + resourceBind->setJID(JID("foo@bar.com/bla")); + boost::shared_ptr<IQ> iq = IQ::createResult(JID("foo@bar.com"), bindID, resourceBind); + onElementReceived(iq); + } + + void sendMessage() { + boost::shared_ptr<Message> message = boost::make_shared<Message>(); + message->setTo(JID("foo@bar.com/bla")); + onElementReceived(message); + } + + void receiveStreamStart() { + Event event = popEvent(); + CPPUNIT_ASSERT(event.header); + } + + void receiveStartTLS() { + Event event = popEvent(); + CPPUNIT_ASSERT(event.element); + CPPUNIT_ASSERT(boost::dynamic_pointer_cast<StartTLSRequest>(event.element)); + } + + void receiveAuthRequest(const std::string& mech) { + Event event = popEvent(); + CPPUNIT_ASSERT(event.element); + boost::shared_ptr<AuthRequest> request(boost::dynamic_pointer_cast<AuthRequest>(event.element)); + CPPUNIT_ASSERT(request); + CPPUNIT_ASSERT_EQUAL(mech, request->getMechanism()); + } + + void receiveStreamManagementEnable() { + Event event = popEvent(); + CPPUNIT_ASSERT(event.element); + CPPUNIT_ASSERT(boost::dynamic_pointer_cast<EnableStreamManagement>(event.element)); + } + + void receiveBind() { + Event event = popEvent(); + CPPUNIT_ASSERT(event.element); + boost::shared_ptr<IQ> iq = boost::dynamic_pointer_cast<IQ>(event.element); + CPPUNIT_ASSERT(iq); + CPPUNIT_ASSERT(iq->getPayload<ResourceBind>()); + bindID = iq->getID(); + } + + void receiveAck(unsigned int n) { + Event event = popEvent(); + CPPUNIT_ASSERT(event.element); + boost::shared_ptr<StanzaAck> ack = boost::dynamic_pointer_cast<StanzaAck>(event.element); + CPPUNIT_ASSERT(ack); + CPPUNIT_ASSERT_EQUAL(n, ack->getHandledStanzasCount()); + } + + Event popEvent() { + CPPUNIT_ASSERT(!receivedEvents.empty()); + Event event = receivedEvents.front(); + receivedEvents.pop_front(); + return event; + } + + bool available; + bool canTLSEncrypt; + bool tlsEncrypted; + bool compressed; + bool whitespacePingEnabled; + std::string bindID; + int resetCount; + std::deque<Event> receivedEvents; + }; + + boost::shared_ptr<IDNConverter> idnConverter; + boost::shared_ptr<MockSessionStream> server; + bool sessionFinishedReceived; + bool needCredentials; + boost::shared_ptr<Error> sessionFinishedError; + BlindCertificateTrustChecker* blindCertificateTrustChecker; + boost::shared_ptr<CryptoProvider> crypto; }; CPPUNIT_TEST_SUITE_REGISTRATION(ClientSessionTest); #if 0 - void testAuthenticate() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::setNeedCredentials, this)); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithAuthentication(); - session->startSession(); - processEvents(); - CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); - CPPUNIT_ASSERT(needCredentials_); - - getMockServer()->expectAuth("me", "mypass"); - getMockServer()->sendAuthSuccess(); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - session->sendCredentials("mypass"); - CPPUNIT_ASSERT_EQUAL(ClientSession::Authenticating, session->getState()); - processEvents(); - CPPUNIT_ASSERT_EQUAL(ClientSession::Negotiating, session->getState()); - } - - void testAuthenticate_Unauthorized() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithAuthentication(); - session->startSession(); - processEvents(); - - getMockServer()->expectAuth("me", "mypass"); - getMockServer()->sendAuthFailure(); - session->sendCredentials("mypass"); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(ClientSession::AuthenticationFailedError, *session->getError()); - } - - void testAuthenticate_NoValidAuthMechanisms() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithUnsupportedAuthentication(); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(ClientSession::NoSupportedAuthMechanismsError, *session->getError()); - } - - void testResourceBind() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithResourceBind(); - getMockServer()->expectResourceBind("Bar", "session-bind"); - // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::BindingResource, session->getState()); - getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind"); - session->startSession(); - - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); - CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getLocalJID()); - } - - void testResourceBind_ChangeResource() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithResourceBind(); - getMockServer()->expectResourceBind("Bar", "session-bind"); - getMockServer()->sendResourceBindResponse("me@foo.com/Bar123", "session-bind"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); - CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getLocalJID()); - } - - void testResourceBind_EmptyResource() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithResourceBind(); - getMockServer()->expectResourceBind("", "session-bind"); - getMockServer()->sendResourceBindResponse("me@foo.com/NewResource", "session-bind"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); - CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getLocalJID()); - } - - void testResourceBind_Error() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithResourceBind(); - getMockServer()->expectResourceBind("", "session-bind"); - getMockServer()->sendError("session-bind"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(ClientSession::ResourceBindError, *session->getError()); - } - - void testSessionStart() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithSession(); - getMockServer()->expectSessionStart("session-start"); - // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::StartingSession, session->getState()); - getMockServer()->sendSessionStartResponse("session-start"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); - CPPUNIT_ASSERT(sessionStarted_); - } - - void testSessionStart_Error() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithSession(); - getMockServer()->expectSessionStart("session-start"); - getMockServer()->sendError("session-start"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStartError, *session->getError()); - } - - void testSessionStart_AfterResourceBind() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeaturesWithResourceBindAndSession(); - getMockServer()->expectResourceBind("Bar", "session-bind"); - getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind"); - getMockServer()->expectSessionStart("session-start"); - getMockServer()->sendSessionStartResponse("session-start"); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); - CPPUNIT_ASSERT(sessionStarted_); - } - - void testWhitespacePing() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeatures(); - session->startSession(); - processEvents(); - CPPUNIT_ASSERT(session->getWhitespacePingLayer()); - } - - void testReceiveElementAfterSessionStarted() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeatures(); - session->startSession(); - processEvents(); - - getMockServer()->expectMessage(); - session->sendElement(boost::make_shared<Message>())); - } - - void testSendElement() { - boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onElementReceived.connect(boost::bind(&ClientSessionTest::addReceivedElement, this, _1)); - getMockServer()->expectStreamStart(); - getMockServer()->sendStreamStart(); - getMockServer()->sendStreamFeatures(); - getMockServer()->sendMessage(); - session->startSession(); - processEvents(); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedElements_.size())); - CPPUNIT_ASSERT(boost::dynamic_pointer_cast<Message>(receivedElements_[0])); - } + void testAuthenticate() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::setNeedCredentials, this)); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithAuthentication(); + session->startSession(); + processEvents(); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); + CPPUNIT_ASSERT(needCredentials_); + + getMockServer()->expectAuth("me", "mypass"); + getMockServer()->sendAuthSuccess(); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + session->sendCredentials("mypass"); + CPPUNIT_ASSERT_EQUAL(ClientSession::Authenticating, session->getState()); + processEvents(); + CPPUNIT_ASSERT_EQUAL(ClientSession::Negotiating, session->getState()); + } + + void testAuthenticate_Unauthorized() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithAuthentication(); + session->startSession(); + processEvents(); + + getMockServer()->expectAuth("me", "mypass"); + getMockServer()->sendAuthFailure(); + session->sendCredentials("mypass"); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::AuthenticationFailedError, *session->getError()); + } + + void testAuthenticate_NoValidAuthMechanisms() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithUnsupportedAuthentication(); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::NoSupportedAuthMechanismsError, *session->getError()); + } + + void testResourceBind() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithResourceBind(); + getMockServer()->expectResourceBind("Bar", "session-bind"); + // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::BindingResource, session->getState()); + getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind"); + session->startSession(); + + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getLocalJID()); + } + + void testResourceBind_ChangeResource() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithResourceBind(); + getMockServer()->expectResourceBind("Bar", "session-bind"); + getMockServer()->sendResourceBindResponse("me@foo.com/Bar123", "session-bind"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getLocalJID()); + } + + void testResourceBind_EmptyResource() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithResourceBind(); + getMockServer()->expectResourceBind("", "session-bind"); + getMockServer()->sendResourceBindResponse("me@foo.com/NewResource", "session-bind"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getLocalJID()); + } + + void testResourceBind_Error() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithResourceBind(); + getMockServer()->expectResourceBind("", "session-bind"); + getMockServer()->sendError("session-bind"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::ResourceBindError, *session->getError()); + } + + void testSessionStart() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithSession(); + getMockServer()->expectSessionStart("session-start"); + // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::StartingSession, session->getState()); + getMockServer()->sendSessionStartResponse("session-start"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); + CPPUNIT_ASSERT(sessionStarted_); + } + + void testSessionStart_Error() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithSession(); + getMockServer()->expectSessionStart("session-start"); + getMockServer()->sendError("session-start"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStartError, *session->getError()); + } + + void testSessionStart_AfterResourceBind() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeaturesWithResourceBindAndSession(); + getMockServer()->expectResourceBind("Bar", "session-bind"); + getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind"); + getMockServer()->expectSessionStart("session-start"); + getMockServer()->sendSessionStartResponse("session-start"); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); + CPPUNIT_ASSERT(sessionStarted_); + } + + void testWhitespacePing() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeatures(); + session->startSession(); + processEvents(); + CPPUNIT_ASSERT(session->getWhitespacePingLayer()); + } + + void testReceiveElementAfterSessionStarted() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeatures(); + session->startSession(); + processEvents(); + + getMockServer()->expectMessage(); + session->sendElement(boost::make_shared<Message>())); + } + + void testSendElement() { + boost::shared_ptr<MockSession> session(createSession("me@foo.com/Bar")); + session->onElementReceived.connect(boost::bind(&ClientSessionTest::addReceivedElement, this, _1)); + getMockServer()->expectStreamStart(); + getMockServer()->sendStreamStart(); + getMockServer()->sendStreamFeatures(); + getMockServer()->sendMessage(); + session->startSession(); + processEvents(); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedElements_.size())); + CPPUNIT_ASSERT(boost::dynamic_pointer_cast<Message>(receivedElements_[0])); + } #endif diff --git a/Swiften/Client/UnitTest/NickResolverTest.cpp b/Swiften/Client/UnitTest/NickResolverTest.cpp index c7d7a3a..855b15a 100644 --- a/Swiften/Client/UnitTest/NickResolverTest.cpp +++ b/Swiften/Client/UnitTest/NickResolverTest.cpp @@ -20,134 +20,134 @@ using namespace Swift; class NickResolverTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(NickResolverTest); - CPPUNIT_TEST(testNoMatch); - CPPUNIT_TEST(testZeroLengthMatch); - CPPUNIT_TEST(testMatch); - CPPUNIT_TEST(testOverwrittenMatch); - CPPUNIT_TEST(testRemovedMatch); - CPPUNIT_TEST(testMUCNick); - CPPUNIT_TEST(testMUCNoNick); - CPPUNIT_TEST(testRemovedMatch); - CPPUNIT_TEST(testOwnNickFullOnly); - CPPUNIT_TEST(testOwnNickGivenAndFull); - CPPUNIT_TEST(testOwnNickNickEtAl); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); - ownJID_ = JID("kev@wonderland.lit"); - xmppRoster_ = new XMPPRosterImpl(); - stanzaChannel_ = new DummyStanzaChannel(); - iqRouter_ = new IQRouter(stanzaChannel_); - vCardStorage_ = new VCardMemoryStorage(crypto.get()); - vCardManager_ = new VCardManager(ownJID_, iqRouter_, vCardStorage_); - registry_ = new MUCRegistry(); - resolver_ = new NickResolver(ownJID_, xmppRoster_, vCardManager_, registry_); - } - - void tearDown() { - delete resolver_; - delete registry_; - delete vCardManager_; - delete iqRouter_; - delete stanzaChannel_; - delete vCardStorage_; - delete xmppRoster_; - } - - void testMUCNick() { - registry_->addMUC(JID("foo@bar")); - JID testJID("foo@bar/baz"); - - CPPUNIT_ASSERT_EQUAL(std::string("baz"), resolver_->jidToNick(testJID)); - } - - void testMUCNoNick() { - registry_->addMUC(JID("foo@bar")); - JID testJID("foo@bar"); - - CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); - } - - - void testNoMatch() { - JID testJID("foo@bar/baz"); - - CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); - } - - void testZeroLengthMatch() { - JID testJID("foo@bar/baz"); - xmppRoster_->addContact(testJID, "", groups_, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); - } - - void testMatch() { - JID testJID("foo@bar/baz"); - xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(std::string("Test"), resolver_->jidToNick(testJID)); - } - - void testOverwrittenMatch() { - JID testJID("foo@bar/baz"); - xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both); - xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(std::string("Test"), resolver_->jidToNick(testJID)); - } - - void testRemovedMatch() { - JID testJID("foo@bar/baz"); - xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both); - xmppRoster_->removeContact(testJID); - CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); - } - - void testOwnNickFullOnly() { - populateOwnVCard("", "", "Kevin Smith"); - CPPUNIT_ASSERT_EQUAL(std::string("Kevin Smith"), resolver_->jidToNick(ownJID_)); - } - - void testOwnNickGivenAndFull() { - populateOwnVCard("", "Kevin", "Kevin Smith"); - CPPUNIT_ASSERT_EQUAL(std::string("Kevin"), resolver_->jidToNick(ownJID_)); - } - - void testOwnNickNickEtAl() { - populateOwnVCard("Kev", "Kevin", "Kevin Smith"); - CPPUNIT_ASSERT_EQUAL(std::string("Kev"), resolver_->jidToNick(ownJID_)); - } - - void populateOwnVCard(const std::string& nick, const std::string& given, const std::string& full) { - VCard::ref vcard(new VCard()); - if (!nick.empty()) { - vcard->setNickname(nick); - } - if (!given.empty()) { - vcard->setGivenName(given); - } - if (!full.empty()) { - vcard->setFullName(full); - } - vCardManager_->requestVCard(ownJID_); - IQ::ref result(IQ::createResult(JID(), stanzaChannel_->sentStanzas[0]->getID(), vcard)); - stanzaChannel_->onIQReceived(result); - } - - private: - std::vector<std::string> groups_; - XMPPRosterImpl* xmppRoster_; - VCardStorage* vCardStorage_; - IQRouter* iqRouter_; - DummyStanzaChannel* stanzaChannel_; - VCardManager* vCardManager_; - MUCRegistry* registry_; - NickResolver* resolver_; - JID ownJID_; - boost::shared_ptr<CryptoProvider> crypto; + CPPUNIT_TEST_SUITE(NickResolverTest); + CPPUNIT_TEST(testNoMatch); + CPPUNIT_TEST(testZeroLengthMatch); + CPPUNIT_TEST(testMatch); + CPPUNIT_TEST(testOverwrittenMatch); + CPPUNIT_TEST(testRemovedMatch); + CPPUNIT_TEST(testMUCNick); + CPPUNIT_TEST(testMUCNoNick); + CPPUNIT_TEST(testRemovedMatch); + CPPUNIT_TEST(testOwnNickFullOnly); + CPPUNIT_TEST(testOwnNickGivenAndFull); + CPPUNIT_TEST(testOwnNickNickEtAl); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); + ownJID_ = JID("kev@wonderland.lit"); + xmppRoster_ = new XMPPRosterImpl(); + stanzaChannel_ = new DummyStanzaChannel(); + iqRouter_ = new IQRouter(stanzaChannel_); + vCardStorage_ = new VCardMemoryStorage(crypto.get()); + vCardManager_ = new VCardManager(ownJID_, iqRouter_, vCardStorage_); + registry_ = new MUCRegistry(); + resolver_ = new NickResolver(ownJID_, xmppRoster_, vCardManager_, registry_); + } + + void tearDown() { + delete resolver_; + delete registry_; + delete vCardManager_; + delete iqRouter_; + delete stanzaChannel_; + delete vCardStorage_; + delete xmppRoster_; + } + + void testMUCNick() { + registry_->addMUC(JID("foo@bar")); + JID testJID("foo@bar/baz"); + + CPPUNIT_ASSERT_EQUAL(std::string("baz"), resolver_->jidToNick(testJID)); + } + + void testMUCNoNick() { + registry_->addMUC(JID("foo@bar")); + JID testJID("foo@bar"); + + CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); + } + + + void testNoMatch() { + JID testJID("foo@bar/baz"); + + CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); + } + + void testZeroLengthMatch() { + JID testJID("foo@bar/baz"); + xmppRoster_->addContact(testJID, "", groups_, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); + } + + void testMatch() { + JID testJID("foo@bar/baz"); + xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(std::string("Test"), resolver_->jidToNick(testJID)); + } + + void testOverwrittenMatch() { + JID testJID("foo@bar/baz"); + xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both); + xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(std::string("Test"), resolver_->jidToNick(testJID)); + } + + void testRemovedMatch() { + JID testJID("foo@bar/baz"); + xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both); + xmppRoster_->removeContact(testJID); + CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), resolver_->jidToNick(testJID)); + } + + void testOwnNickFullOnly() { + populateOwnVCard("", "", "Kevin Smith"); + CPPUNIT_ASSERT_EQUAL(std::string("Kevin Smith"), resolver_->jidToNick(ownJID_)); + } + + void testOwnNickGivenAndFull() { + populateOwnVCard("", "Kevin", "Kevin Smith"); + CPPUNIT_ASSERT_EQUAL(std::string("Kevin"), resolver_->jidToNick(ownJID_)); + } + + void testOwnNickNickEtAl() { + populateOwnVCard("Kev", "Kevin", "Kevin Smith"); + CPPUNIT_ASSERT_EQUAL(std::string("Kev"), resolver_->jidToNick(ownJID_)); + } + + void populateOwnVCard(const std::string& nick, const std::string& given, const std::string& full) { + VCard::ref vcard(new VCard()); + if (!nick.empty()) { + vcard->setNickname(nick); + } + if (!given.empty()) { + vcard->setGivenName(given); + } + if (!full.empty()) { + vcard->setFullName(full); + } + vCardManager_->requestVCard(ownJID_); + IQ::ref result(IQ::createResult(JID(), stanzaChannel_->sentStanzas[0]->getID(), vcard)); + stanzaChannel_->onIQReceived(result); + } + + private: + std::vector<std::string> groups_; + XMPPRosterImpl* xmppRoster_; + VCardStorage* vCardStorage_; + IQRouter* iqRouter_; + DummyStanzaChannel* stanzaChannel_; + VCardManager* vCardManager_; + MUCRegistry* registry_; + NickResolver* resolver_; + JID ownJID_; + boost::shared_ptr<CryptoProvider> crypto; }; CPPUNIT_TEST_SUITE_REGISTRATION(NickResolverTest); |