/* * 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 using namespace Swift; class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(JingleParserTest); CPPUNIT_TEST(testParse_Xep0166_Example3); CPPUNIT_TEST(testParse_Xep0166_Example8); CPPUNIT_TEST(testParse_Xep0261_Example1); CPPUNIT_TEST(testParse_Xep0261_Example3); CPPUNIT_TEST(testParse_Xep0261_Example9); CPPUNIT_TEST(testParse_Xep0261_Example13); CPPUNIT_TEST(testParse_Xep0234_Example1); CPPUNIT_TEST(testParse_Xep0234_Example3); CPPUNIT_TEST(testParse_Xep0234_Example5); CPPUNIT_TEST(testParse_Xep0234_Example8); CPPUNIT_TEST(testParse_Xep0234_Example10); CPPUNIT_TEST(testParse_Xep0260_Example1); CPPUNIT_TEST(testParse_Xep0260_Example3); CPPUNIT_TEST_SUITE_END(); public: //http://xmpp.org/extensions/xep-0166.html#example-3 void testParse_Xep0166_Example3() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionTerminate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); CPPUNIT_ASSERT_EQUAL(JinglePayload::Reason::Success, jingle->getReason().get_value_or(JinglePayload::Reason(JinglePayload::Reason::UnknownType, "")).type); } //http://xmpp.org/extensions/xep-0166.html#example-8 void testParse_Xep0166_Example8() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " Sorry, gotta go!\n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionTerminate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); CPPUNIT_ASSERT_EQUAL(JinglePayload::Reason::Success, jingle->getReason().get_value_or(JinglePayload::Reason(JinglePayload::Reason::UnknownType, "")).type); CPPUNIT_ASSERT_EQUAL(std::string("Sorry, gotta go!"), jingle->getReason().get_value_or(JinglePayload::Reason(JinglePayload::Reason::UnknownType, "")).text); } // IBB Transport Method Examples // http://xmpp.org/extensions/xep-0261.html#example-1 void testParse_Xep0261_Example1() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionInitiate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); std::vector payloads = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), payloads.size()); JingleContentPayload::ref payload = payloads[0]; CPPUNIT_ASSERT_EQUAL(JingleContentPayload::InitiatorCreator, payload->getCreator()); CPPUNIT_ASSERT_EQUAL(std::string("ex"), payload->getName()); CPPUNIT_ASSERT_EQUAL(static_cast(1), payload->getTransports().size()); JingleIBBTransportPayload::ref transportPaylod = payload->getTransport(); CPPUNIT_ASSERT(transportPaylod); CPPUNIT_ASSERT_EQUAL(4096U, *transportPaylod->getBlockSize()); CPPUNIT_ASSERT_EQUAL(std::string("ch3d9s71"), transportPaylod->getSessionID()); } // http://xmpp.org/extensions/xep-0261.html#example-1 void testParse_Xep0261_Example3() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " \n" " \n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionAccept, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), jingle->getResponder()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); std::vector payloads = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), payloads.size()); JingleContentPayload::ref payload = payloads[0]; CPPUNIT_ASSERT_EQUAL(JingleContentPayload::InitiatorCreator, payload->getCreator()); CPPUNIT_ASSERT_EQUAL(std::string("ex"), payload->getName()); CPPUNIT_ASSERT_EQUAL(static_cast(1), payload->getTransports().size()); JingleIBBTransportPayload::ref transportPaylod = payload->getTransport(); CPPUNIT_ASSERT(transportPaylod); CPPUNIT_ASSERT_EQUAL(2048U, *transportPaylod->getBlockSize()); CPPUNIT_ASSERT_EQUAL(std::string("ch3d9s71"), transportPaylod->getSessionID()); } // http://xmpp.org/extensions/xep-0261.html#example-9 void testParse_Xep0261_Example9() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::TransportInfo, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); std::vector payloads = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), payloads.size()); JingleContentPayload::ref payload = payloads[0]; CPPUNIT_ASSERT_EQUAL(JingleContentPayload::InitiatorCreator, payload->getCreator()); CPPUNIT_ASSERT_EQUAL(std::string("ex"), payload->getName()); JingleIBBTransportPayload::ref transportPaylod = payload->getTransport(); CPPUNIT_ASSERT(transportPaylod); CPPUNIT_ASSERT_EQUAL(2048U, *transportPaylod->getBlockSize()); CPPUNIT_ASSERT_EQUAL(std::string("bt8a71h6"), transportPaylod->getSessionID()); } // http://xmpp.org/extensions/xep-0261.html#example-13 void testParse_Xep0261_Example13() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionTerminate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); CPPUNIT_ASSERT_EQUAL(JinglePayload::Reason::Success, jingle->getReason().get_value_or(JinglePayload::Reason()).type); } // Jingle File Transfer Examples // http://xmpp.org/extensions/xep-0234.html#example-1 void testParse_Xep0234_Example1() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" "\n" "\n" "\n" "1969-07-21T02:56:15Z\n" "This is a test. If this were a real file...\n" "text/plain\n" "test.txt\n" "\n" "1022\n" "VS2nSZMIUsaa5dIUHTdmsQ==\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionInitiate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("851ba2"), jingle->getSessionID()); std::vector contents = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), contents.size()); JingleFileTransferDescription::ref description = contents[0]->getDescription(); CPPUNIT_ASSERT(description); JingleFileTransferFileInfo fileInfo = description->getFileInfo(); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), fileInfo.getName()); CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), fileInfo.getHashes().begin()->first); CPPUNIT_ASSERT_EQUAL(std::string("VS2nSZMIUsaa5dIUHTdmsQ=="), Base64::encode(fileInfo.getHashes().begin()->second)); CPPUNIT_ASSERT(1022 == fileInfo.getSize()); CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), fileInfo.getDescription()); CPPUNIT_ASSERT_EQUAL(true, fileInfo.getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == fileInfo.getDate()); } // http://xmpp.org/extensions/xep-0234.html#example-3 void testParse_Xep0234_Example3() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" "\n" "\n" "\n" "1969-07-21T02:56:15Z\n" "This is a test. If this were a real file...\n" "text/plain\n" "test.txt\n" "\n" "1022\n" "VS2nSZMIUsaa5dIUHTdmsQ==\n" "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionAccept, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("851ba2"), jingle->getSessionID()); std::vector contents = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), contents.size()); JingleFileTransferDescription::ref description = contents[0]->getDescription(); CPPUNIT_ASSERT(description); JingleFileTransferFileInfo fileInfo = description->getFileInfo(); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), fileInfo.getName()); CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), fileInfo.getHashes().begin()->first); CPPUNIT_ASSERT_EQUAL(std::string("VS2nSZMIUsaa5dIUHTdmsQ=="), Base64::encode(fileInfo.getHashes().begin()->second)); CPPUNIT_ASSERT(1022 == fileInfo.getSize()); CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), fileInfo.getDescription()); CPPUNIT_ASSERT_EQUAL(true, fileInfo.getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == fileInfo.getDate()); } // http://xmpp.org/extensions/xep-0234.html#example-5 void testParse_Xep0234_Example5() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::TransportInfo, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); std::vector contents = jingle->getContents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), contents.size()); JingleS5BTransportPayload::ref transport = contents[0]->getTransport(); CPPUNIT_ASSERT(transport); CPPUNIT_ASSERT_EQUAL(std::string("vj3hs98y"), transport->getSessionID()); CPPUNIT_ASSERT_EQUAL(std::string("hr65dqyd"), transport->getCandidateUsed()); } // http://xmpp.org/extensions/xep-0234.html#example-8 void testParse_Xep0234_Example8() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " VS2nSZMIUsaa5dIUHTdmsQ==\n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionInfo, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); JingleFileTransferHash::ref hash = jingle->getPayload(); CPPUNIT_ASSERT(hash); CPPUNIT_ASSERT_EQUAL(std::string("VS2nSZMIUsaa5dIUHTdmsQ=="), Base64::encode(hash->getFileInfo().getHash("sha-1").get())); } // http://xmpp.org/extensions/xep-0234.html#example-10 void testParse_Xep0234_Example10() { Log::setLogLevel(Log::debug); PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " VS2nSZMIUsaa5dIUHTdmsQ==\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionInitiate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("uj3b2"), jingle->getSessionID()); JingleContentPayload::ref content = jingle->getPayload(); CPPUNIT_ASSERT(content); JingleFileTransferFileInfo file = content->getDescription()->getFileInfo(); CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), file.getHashes().begin()->first); CPPUNIT_ASSERT_EQUAL(std::string("VS2nSZMIUsaa5dIUHTdmsQ=="), Base64::encode(file.getHashes().begin()->second)); CPPUNIT_ASSERT_EQUAL(static_cast(270336), file.getRangeOffset()); CPPUNIT_ASSERT_EQUAL(true, file.getSupportsRangeRequests()); } // http://xmpp.org/extensions/xep-0260.html#example-1 void testParse_Xep0260_Example1() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionInitiate, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); JingleContentPayload::ref content = jingle->getPayload(); CPPUNIT_ASSERT(content); JingleS5BTransportPayload::ref s5bPayload = content->getTransport(); CPPUNIT_ASSERT(s5bPayload); CPPUNIT_ASSERT_EQUAL(std::string("vj3hs98y"), s5bPayload->getSessionID()); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::TCPMode, s5bPayload->getMode()); CPPUNIT_ASSERT_EQUAL(false, s5bPayload->hasCandidateError()); CPPUNIT_ASSERT_EQUAL(false, s5bPayload->hasProxyError()); CPPUNIT_ASSERT_EQUAL(std::string(), s5bPayload->getActivated()); CPPUNIT_ASSERT_EQUAL(std::string(), s5bPayload->getCandidateUsed()); CPPUNIT_ASSERT_EQUAL(static_cast(2), s5bPayload->getCandidates().size()); JingleS5BTransportPayload::Candidate candidate; candidate = s5bPayload->getCandidates()[0]; CPPUNIT_ASSERT_EQUAL(std::string("hft54dqy"), candidate.cid); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), candidate.jid); CPPUNIT_ASSERT(HostAddressPort(HostAddress("192.168.4.1"), 5086) == candidate.hostPort); CPPUNIT_ASSERT_EQUAL(8257636, candidate.priority); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::Candidate::DirectType, candidate.type); candidate = s5bPayload->getCandidates()[1]; CPPUNIT_ASSERT_EQUAL(std::string("hutr46fe"), candidate.cid); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), candidate.jid); CPPUNIT_ASSERT(HostAddressPort(HostAddress("24.24.24.1"), 5087) == candidate.hostPort); CPPUNIT_ASSERT_EQUAL(8258636, candidate.priority); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::Candidate::DirectType, candidate.type); } // http://xmpp.org/extensions/xep-0260.html#example-3 void testParse_Xep0260_Example3() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n" )); JinglePayload::ref jingle = parser.getPayload(); CPPUNIT_ASSERT(jingle); CPPUNIT_ASSERT_EQUAL(JinglePayload::SessionAccept, jingle->getAction()); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), jingle->getInitiator()); CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); JingleContentPayload::ref content = jingle->getPayload(); CPPUNIT_ASSERT(content); JingleS5BTransportPayload::ref s5bPayload = content->getTransport(); CPPUNIT_ASSERT(s5bPayload); CPPUNIT_ASSERT_EQUAL(std::string("vj3hs98y"), s5bPayload->getSessionID()); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::TCPMode, s5bPayload->getMode()); CPPUNIT_ASSERT_EQUAL(false, s5bPayload->hasCandidateError()); CPPUNIT_ASSERT_EQUAL(false, s5bPayload->hasProxyError()); CPPUNIT_ASSERT_EQUAL(std::string(), s5bPayload->getActivated()); CPPUNIT_ASSERT_EQUAL(std::string(), s5bPayload->getCandidateUsed()); CPPUNIT_ASSERT_EQUAL(static_cast(3), s5bPayload->getCandidates().size()); JingleS5BTransportPayload::Candidate candidate; candidate = s5bPayload->getCandidates()[0]; CPPUNIT_ASSERT_EQUAL(std::string("ht567dq"), candidate.cid); CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), candidate.jid); CPPUNIT_ASSERT(HostAddressPort(HostAddress("192.169.1.10"), 6539) == candidate.hostPort); CPPUNIT_ASSERT_EQUAL(8257636, candidate.priority); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::Candidate::DirectType, candidate.type); candidate = s5bPayload->getCandidates()[1]; CPPUNIT_ASSERT_EQUAL(std::string("hr65dqyd"), candidate.cid); CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), candidate.jid); CPPUNIT_ASSERT(HostAddressPort(HostAddress("134.102.201.180"), 16453) == candidate.hostPort); CPPUNIT_ASSERT_EQUAL(7929856, candidate.priority); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::Candidate::AssistedType, candidate.type); candidate = s5bPayload->getCandidates()[2]; CPPUNIT_ASSERT_EQUAL(std::string("grt654q2"), candidate.cid); CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), candidate.jid); CPPUNIT_ASSERT(HostAddressPort(HostAddress("2001:638:708:30c9:219:d1ff:fea4:a17d"), 6539) == candidate.hostPort); CPPUNIT_ASSERT_EQUAL(8257606, candidate.priority); CPPUNIT_ASSERT_EQUAL(JingleS5BTransportPayload::Candidate::DirectType, candidate.type); } }; CPPUNIT_TEST_SUITE_REGISTRATION(JingleParserTest);