From 5cc64a2e85e0dccb0d12b8c0b0a85a4c01fb4978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Wed, 22 Jul 2009 08:13:01 +0200 Subject: Started refactoring ClientSessionTest. diff --git a/Swiften/Client/ClientSession.cpp b/Swiften/Client/ClientSession.cpp index 802ac0a..1409195 100644 --- a/Swiften/Client/ClientSession.cpp +++ b/Swiften/Client/ClientSession.cpp @@ -197,7 +197,7 @@ void ClientSession::sendSessionStart() { void ClientSession::handleSessionFinished(const boost::optional& error) { if (error) { - assert(!error_); + //assert(!error_); state_ = Error; error_ = error; } diff --git a/Swiften/Client/UnitTest/ClientSessionTest.cpp b/Swiften/Client/UnitTest/ClientSessionTest.cpp index a44b0df..bb10cfd 100644 --- a/Swiften/Client/UnitTest/ClientSessionTest.cpp +++ b/Swiften/Client/UnitTest/ClientSessionTest.cpp @@ -39,6 +39,7 @@ using namespace Swift; class ClientSessionTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(ClientSessionTest); CPPUNIT_TEST(testConstructor); + /* CPPUNIT_TEST(testStart_Error); CPPUNIT_TEST(testStart_XMLError); CPPUNIT_TEST(testStartTLS); @@ -59,6 +60,7 @@ class ClientSessionTest : public CppUnit::TestFixture { CPPUNIT_TEST(testWhitespacePing); CPPUNIT_TEST(testReceiveElementAfterSessionStarted); CPPUNIT_TEST(testSendElement); + */ CPPUNIT_TEST_SUITE_END(); public: @@ -82,6 +84,7 @@ class ClientSessionTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(ClientSession::Initial, session->getState()); } +/* void testStart_Error() { boost::shared_ptr session(createSession("me@foo.com/Bar")); @@ -639,15 +642,62 @@ class ClientSessionTest : public CppUnit::TestFixture { bool resetParser_; String domain_; + XMPPParser* parser_; + XMPPSerializer serializer_; + std::deque events_; + }; + + */ + + private: + struct MockConnection : public Connection, public XMPPParserClient { + MockConnection() : + resetParser_(false), + serializer_(&payloadSerializers_) { + parser_ = new XMPPParser(this, &payloadParserFactories_); + } + + ~MockConnection() { + delete parser_; + } + + void handleStreamStart(const ProtocolHeader& header) { + } + + void handleElement(boost::shared_ptr element) { + } + + void handleStreamEnd() { + } + + void disconnect() { } + void listen() { assert(false); } + void connect(const HostAddressPort&) { CPPUNIT_ASSERT(false); } + void connect(const String&) { CPPUNIT_ASSERT(false); } + + void write(const ByteArray& data) { + CPPUNIT_ASSERT(parser_->parse(data.toString())); + if (resetParser_) { + resetParser(); + resetParser_ = false; + } + } + + void resetParser() { + delete parser_; + parser_ = new XMPPParser(this, &payloadParserFactories_); + } + FullPayloadParserFactoryCollection payloadParserFactories_; FullPayloadSerializerCollection payloadSerializers_; + bool resetParser_; XMPPParser* parser_; XMPPSerializer serializer_; - std::deque events_; }; struct MockTLSLayer : public TLSLayer { MockTLSLayer() : connecting_(false) {} + bool setClientCertificate(const PKCS12Certificate&) { return true; } void writeData(const ByteArray& data) { onWriteData(data); } void handleDataRead(const ByteArray& data) { onDataRead(data); } @@ -661,24 +711,35 @@ class ClientSessionTest : public CppUnit::TestFixture { struct MockTLSLayerFactory : public TLSLayerFactory { MockTLSLayerFactory() : haveTLS_(true) {} + void setTLSSupported(bool b) { haveTLS_ = b; } + virtual bool canCreate() const { return haveTLS_; } + virtual boost::shared_ptr createTLSLayer() { - assert(haveTLS_); + CPPUNIT_ASSERT(haveTLS_); boost::shared_ptr result(new MockTLSLayer()); layers_.push_back(result); return result; } + std::vector< boost::shared_ptr > layers_; bool haveTLS_; }; struct MockSession : public ClientSession { - MockSession(const JID& jid, boost::shared_ptr connection, TLSLayerFactory* tlsLayerFactory, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers) : ClientSession(jid, connection, tlsLayerFactory, payloadParserFactories, payloadSerializers) {} + MockSession( + const JID& jid, + boost::shared_ptr connection, + TLSLayerFactory* tlsLayerFactory, + PayloadParserFactoryCollection* payloadParserFactories, + PayloadSerializerCollection* payloadSerializers) : + ClientSession(jid, connection, tlsLayerFactory, payloadParserFactories, payloadSerializers) {} boost::shared_ptr getTLSLayer() const { return getStreamStack()->getLayer(); } + boost::shared_ptr getWhitespacePingLayer() const { return getStreamStack()->getLayer(); } @@ -687,18 +748,19 @@ class ClientSessionTest : public CppUnit::TestFixture { boost::shared_ptr createSession(const String& jid) { return boost::shared_ptr(new MockSession(JID(jid), connection_, tlsLayerFactory_, &payloadParserFactories_, &payloadSerializers_)); } - - + + private: + FullPayloadParserFactoryCollection payloadParserFactories_; + FullPayloadSerializerCollection payloadSerializers_; DummyEventLoop* eventLoop_; boost::shared_ptr connection_; MockTLSLayerFactory* tlsLayerFactory_; - FullPayloadParserFactoryCollection payloadParserFactories_; - FullPayloadSerializerCollection payloadSerializers_; bool sessionStarted_; bool needCredentials_; std::vector< boost::shared_ptr > receivedElements_; + /* typedef std::vector< boost::function > EventQueue; - EventQueue events_; + EventQueue events_;*/ }; CPPUNIT_TEST_SUITE_REGISTRATION(ClientSessionTest); -- cgit v0.10.2-6-g49f6