TrackerRequestSessionFactoryImplTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../MultiTrackerRequestSession.h"
00025 #include "../SingleTrackerRequestSession.h"
00026 #include "../TrackerRequestSession.h"
00027 #include "../TrackerRequestSessionFactory.h"
00028 #include "generated/TrackerRequestSessionFactoryImplDriver.h"
00029 #include "generated/TrackerRequestSessionFactoryMock.h"
00030 #include "Imports.cpp"
00031 
00032 namespace Protocols {
00033 namespace BitTorrent {
00034 namespace Trackers {
00035 namespace Testing {
00036 
00038 
00040 class TrackerRequestSessionFactoryImplTest : public CppUnit::TestFixture
00041 {
00042     CPPUNIT_TEST_SUITE (TrackerRequestSessionFactoryImplTest);
00043     CPPUNIT_TEST (testCreateSessionTorrentWithAnnounceUrlWillReturnSingleTrackerRequestSession);
00044     CPPUNIT_TEST (testCreateSessionTorrentWithAnnounceListWillReturnMultiTrackerRequestSession);
00045     CPPUNIT_TEST (testDestroySessionOnlyOneSessionCreated);
00046     CPPUNIT_TEST (testDestroySessionTwoSessionsCreated);
00047     CPPUNIT_TEST (testDestroyAllCreatedSessionsThreeSessionsCreated);
00048     CPPUNIT_TEST (testTrackerRequestSessionEstablishedNotificationWillBeForwarded);
00049     CPPUNIT_TEST (trackerRequestSessionErrorNotificationWillBeForwarded);
00050     CPPUNIT_TEST (trackerRequestSessionResponseRecievedNotificationWillBeForwarded);
00051     CPPUNIT_TEST (trackerRequestSessionClosingNotificationWillBeForwarded);
00052     CPPUNIT_TEST (trackerRequestSessionClosedNotificationWillBeForwarded);
00053     CPPUNIT_TEST_SUITE_END();
00054 
00055     auto_ptr <TrackerRequestSessionFactoryStatusMock>   factoryStatus;
00056     auto_ptr <TrackerRequestSessionFactoryImpl>         factoryReal;
00057     auto_ptr <TrackerRequestSessionFactoryImplDriver>   factory;
00058 
00059     Torrent                                             torrentWithAnnounceUrl;
00060     Torrent                                             torrentWithAnnounceList;
00061     TrackerResponse                                     trackerResponse;
00062     QString                                             errorMessage;
00063 
00064 public:
00065     TrackerRequestSessionFactoryImplTest();
00066 
00067     void setUp()
00068     {
00069         factoryStatus.reset (new TrackerRequestSessionFactoryStatusMock());
00070         factoryReal.reset (new TrackerRequestSessionFactoryImpl (
00071             &factoryStatus->imp));
00072         factory.reset (new TrackerRequestSessionFactoryImplDriver (
00073             *factoryReal.get()));
00074     }
00075 
00076     void tearDown()
00077     {
00078         factory.reset();
00079         factoryReal.reset();
00080         factoryStatus.reset();
00081     }
00082 
00084     void testCreateSessionTorrentWithAnnounceUrlWillReturnSingleTrackerRequestSession()
00085     {
00086         TrackerRequestSession *session = factoryReal->createSession (
00087             torrentWithAnnounceUrl);
00088         CPPUNIT_ASSERT (typeid (*session) == typeid (SingleTrackerRequestSession));
00089     }
00090 
00092     void testCreateSessionTorrentWithAnnounceListWillReturnMultiTrackerRequestSession()
00093     {
00094         TrackerRequestSession *session = factoryReal->createSession (
00095             torrentWithAnnounceList);
00096         CPPUNIT_ASSERT (typeid (*session) == typeid (MultiTrackerRequestSession));
00097     }
00098 
00100     void testDestroySessionOnlyOneSessionCreated()
00101     {
00102         TrackerRequestSession *session = factoryReal->createSession (
00103             torrentWithAnnounceUrl);
00104         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 1);
00105         factoryReal->destroySession (session);
00106         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 0);
00107     }
00108 
00113     void testDestroySessionTwoSessionsCreated()
00114     {
00115         TrackerRequestSession *session1 = factoryReal->createSession (
00116             torrentWithAnnounceUrl);
00117         /*TrackerRequestSession *session2 =*/ factoryReal->createSession (
00118             torrentWithAnnounceList);
00119         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 2);
00120         factoryReal->destroySession (session1);
00121         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 1);
00122     }
00123 
00125     void testDestroyAllCreatedSessionsThreeSessionsCreated()
00126     {
00127         TrackerRequestSession *session1 = factoryReal->createSession (
00128             torrentWithAnnounceUrl);
00129         TrackerRequestSession *session2 = factoryReal->createSession (
00130             torrentWithAnnounceList);
00131         TrackerRequestSession *session3 = factoryReal->createSession (
00132             torrentWithAnnounceList);
00133         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 3);
00134         factoryReal->destroySession (session1);
00135         factoryReal->destroySession (session2);
00136         factoryReal->destroySession (session3);
00137         CPPUNIT_ASSERT (factoryReal->holdedSessionsCount() == 0);
00138     }
00139 
00145     void testTrackerRequestSessionEstablishedNotificationWillBeForwarded()
00146     {
00147         TrackerRequestSession *session = factoryReal->createSession (
00148             torrentWithAnnounceUrl);
00149 
00150         call (factory->trackerRequestSessionEstablished (session))
00151             .willCall (factoryStatus->trackerRequestSessionFactorySessionEstablished (
00152                 session))
00153             .returns()
00154         .returns();
00155     }
00156 
00162     void trackerRequestSessionErrorNotificationWillBeForwarded()
00163     {
00164         TrackerRequestSession *session = factoryReal->createSession (
00165             torrentWithAnnounceUrl);
00166 
00167         call (factory->trackerRequestSessionError (session, errorMessage))
00168             .willCall (factoryStatus->trackerRequestSessionFactorySessionError (
00169                 session, errorMessage))
00170             .returns()
00171         .returns();
00172     }
00173 
00179     void trackerRequestSessionResponseRecievedNotificationWillBeForwarded()
00180     {
00181         TrackerRequestSession *session = factoryReal->createSession (
00182             torrentWithAnnounceUrl);
00183 
00184         call (factory->trackerRequestSessionResponseRecieved (session,
00185             trackerResponse))
00186             .willCall (factoryStatus->trackerRequestSessionFactoryResponseRecieved (
00187                 session, trackerResponse))
00188             .returns()
00189         .returns();
00190     }
00191 
00197     void trackerRequestSessionClosingNotificationWillBeForwarded()
00198     {
00199         TrackerRequestSession *session = factoryReal->createSession (
00200             torrentWithAnnounceUrl);
00201 
00202         call (factory->trackerRequestSessionClosing (session))
00203             .willCall (factoryStatus->trackerRequestSessionFactorySessionClosing (
00204                 session))
00205             .returns()
00206         .returns();
00207     }
00208 
00214     void trackerRequestSessionClosedNotificationWillBeForwarded()
00215     {
00216         TrackerRequestSession *session = factoryReal->createSession (
00217             torrentWithAnnounceUrl);
00218 
00219         call (factory->trackerRequestSessionClosed (session))
00220             .willCall (factoryStatus->trackerRequestSessionFactorySessionClosed (
00221                 session))
00222             .returns()
00223         .returns();
00224     }
00225 };
00226 
00227 CPPUNIT_TEST_SUITE_REGISTRATION(TrackerRequestSessionFactoryImplTest);
00228 
00230 TrackerRequestSessionFactoryImplTest::TrackerRequestSessionFactoryImplTest()
00231     : factoryStatus(),
00232       factoryReal(),
00233       factory(),
00234       torrentWithAnnounceUrl(),
00235       torrentWithAnnounceList(),
00236       trackerResponse(),
00237       errorMessage (QString ("Error"))
00238 {
00239     const QByteArray rawTorrentWithAnnounceUrlData (
00240         "d8:announce18:http://tracker.com"
00241         "4:infod6:lengthi6e4:name8:file.txt"
00242         "12:piece lengthi32768e6:pieces20:f64hx6dr3cz940fgd52see");
00243     bool torrentWithAnnounceUrloadedOk = TorrentParser::parseAndLoadTorrent (
00244         rawTorrentWithAnnounceUrlData, torrentWithAnnounceUrl);
00245     CPPUNIT_ASSERT (torrentWithAnnounceUrloadedOk);
00246 
00247     const QByteArray rawTorrentWithAnnounceListData (
00248         "d8:announce18:http://tracker.com"
00249         "13:announce-listll19:http://tracker1.com19:http://tracker2.comee"
00250         "4:infod6:lengthi6e4:name8:file.txt"
00251         "12:piece lengthi32768e6:pieces20:cx33lza06nvgseqwt3b9ee");
00252     bool torrentWithAnnounceListLoadedOk = TorrentParser::parseAndLoadTorrent (
00253         rawTorrentWithAnnounceListData, torrentWithAnnounceList);
00254     CPPUNIT_ASSERT (torrentWithAnnounceListLoadedOk);
00255 }
00256 
00257 } // namespace Testing
00258 } // namespace Trackers
00259 } // namespace BitTorrent
00260 } // namespace Protocols