TrackerRequestTest.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 "../TrackerRequest.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace BitTorrent {
00029 namespace Trackers {
00030 namespace Testing {
00031 
00033 
00035 class TrackerRequestTest : public CppUnit::TestFixture
00036 {
00037     CPPUNIT_TEST_SUITE (TrackerRequestTest);
00038     CPPUNIT_TEST (testMinimalRequestCorrectness);
00039     CPPUNIT_TEST (testMaximalRequestCorrectness);
00040     CPPUNIT_TEST (testCompareTwoEqualRequests);
00041     CPPUNIT_TEST (testCopyCtor);
00042     CPPUNIT_TEST (testAssignment);
00043     CPPUNIT_TEST (testModifyingACopyDoesNotChangeTheOriginal);
00044     CPPUNIT_TEST_SUITE_END();
00045 
00046 public:
00048     void testMinimalRequestCorrectness()
00049     {
00050         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00051             "http://tracker.com");
00052         PeerInfo peerInfo ("0.0.0.0", 6881,
00053             PeerInfo::PeerId ("-CA0610-574938205849"));
00054         TrackerRequest request (ReferenceAnnounceUrl,
00055                                 peerInfo,
00056                                 Torrent::InfoHash ("z53bf64bd05hfn2daqml"),
00057                                 4096,
00058                                 2048,
00059                                 63488,
00060                                 true);
00061 
00062         CPPUNIT_ASSERT (request.announceUrl() == ReferenceAnnounceUrl);
00063         CPPUNIT_ASSERT (request.peerInfo() == peerInfo);
00064         CPPUNIT_ASSERT (request.infoHash() == Torrent::InfoHash ("z53bf64bd05hfn2daqml"));
00065         CPPUNIT_ASSERT (request.uploaded() == 4096);
00066         CPPUNIT_ASSERT (request.downloaded() == 2048);
00067         CPPUNIT_ASSERT (request.left() == 63488);
00068         CPPUNIT_ASSERT (request.compact() == true);
00069         CPPUNIT_ASSERT (request.event() == TrackerRequest::Empty);
00070         CPPUNIT_ASSERT (request.numWant() == 0);
00071         CPPUNIT_ASSERT (request.key() == QByteArray());
00072         CPPUNIT_ASSERT (request.trackerId() == QByteArray());
00073     }
00074 
00076     void testMaximalRequestCorrectness()
00077     {
00078         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00079             "http://tracker.com");
00080         PeerInfo peerInfo ("127.0.0.1", 6881,
00081             PeerInfo::PeerId ("-CA0610-574938205849"));
00082         TrackerRequest request (ReferenceAnnounceUrl,
00083                                 peerInfo,
00084                                 Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00085                                 2048,
00086                                 1024,
00087                                 63488,
00088                                 true,
00089                                 TrackerRequest::Started,
00090                                 30,
00091                                 QByteArray ("364h3kd9472mfd03odj3"),
00092                                 QByteArray ("z84d74h39doz7dh47du5"));
00093 
00094         CPPUNIT_ASSERT (request.announceUrl() == ReferenceAnnounceUrl);
00095         CPPUNIT_ASSERT (request.peerInfo() == peerInfo);
00096         CPPUNIT_ASSERT (request.infoHash() ==Torrent::InfoHash ("fh37dosi5jv0c91g3654"));
00097         CPPUNIT_ASSERT (request.uploaded() == 2048);
00098         CPPUNIT_ASSERT (request.downloaded() == 1024);
00099         CPPUNIT_ASSERT (request.left() == 63488);
00100         CPPUNIT_ASSERT (request.compact() == true);
00101         CPPUNIT_ASSERT (request.event() == TrackerRequest::Started);
00102         CPPUNIT_ASSERT (request.numWant() == 30);
00103         CPPUNIT_ASSERT (request.key() == QByteArray ("364h3kd9472mfd03odj3"));
00104         CPPUNIT_ASSERT (request.trackerId() == QByteArray ("z84d74h39doz7dh47du5"));
00105     }
00106 
00108     void testCompareTwoEqualRequests()
00109     {
00110         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00111             "http://tracker.com");
00112         PeerInfo peerInfo ("127.0.0.1", 6881,
00113             PeerInfo::PeerId ("-CA0610-574938205849"));
00114         TrackerRequest request1 (ReferenceAnnounceUrl,
00115                                  peerInfo,
00116                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00117                                  2048,
00118                                  1024,
00119                                  63488,
00120                                  true,
00121                                  TrackerRequest::Started,
00122                                  30,
00123                                  QByteArray ("364h3kd9472mfd03odj3"),
00124                                  QByteArray ("z84d74h39doz7dh47du5"));
00125         TrackerRequest request2 (ReferenceAnnounceUrl,
00126                                  peerInfo,
00127                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00128                                  2048,
00129                                  1024,
00130                                  63488,
00131                                  true,
00132                                  TrackerRequest::Started,
00133                                  30,
00134                                  QByteArray ("364h3kd9472mfd03odj3"),
00135                                  QByteArray ("z84d74h39doz7dh47du5"));
00136 
00137         CPPUNIT_ASSERT (request1 == request2);
00138         CPPUNIT_ASSERT (!(request1 != request2));
00139     }
00140 
00142     void testCopyCtor()
00143     {
00144         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00145             "http://tracker.com");
00146         PeerInfo peerInfo ("127.0.0.1", 6881,
00147             PeerInfo::PeerId ("-CA0610-574938205849"));
00148         TrackerRequest request1 (ReferenceAnnounceUrl,
00149                                  peerInfo,
00150                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00151                                  2048,
00152                                  1024,
00153                                  63488,
00154                                  true,
00155                                  TrackerRequest::Started,
00156                                  30,
00157                                  QByteArray ("364h3kd9472mfd03odj3"),
00158                                  QByteArray ("z84d74h39doz7dh47du5"));
00159         TrackerRequest request2 (request1);
00160 
00161         CPPUNIT_ASSERT (request1 == request2);
00162     }
00163 
00165     void testAssignment()
00166     {
00167         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00168             "http://tracker.com");
00169         PeerInfo peerInfo ("127.0.0.1", 6881,
00170             PeerInfo::PeerId ("-CA0610-574938205849"));
00171         TrackerRequest request1 (ReferenceAnnounceUrl,
00172                                  peerInfo,
00173                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00174                                  2048,
00175                                  1024,
00176                                  63488,
00177                                  true,
00178                                  TrackerRequest::Started,
00179                                  30,
00180                                  QByteArray ("364h3kd9472mfd03odj3"),
00181                                  QByteArray ("z84d74h39doz7dh47du5"));
00182         TrackerRequest request2 (ReferenceAnnounceUrl,
00183                                  peerInfo,
00184                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00185                                  2048,
00186                                  1024,
00187                                  63488,
00188                                  true);
00189 
00190         request2 = request1;
00191         CPPUNIT_ASSERT (request1 == request2);
00192     }
00193 
00195     void testModifyingACopyDoesNotChangeTheOriginal()
00196     {
00197         const Uri ReferenceAnnounceUrl = Uri::fromUnencoded (
00198             "http://tracker.com");
00199         PeerInfo peerInfo ("127.0.0.1", 6881,
00200             PeerInfo::PeerId ("-CA0610-574938205849"));
00201         TrackerRequest request1 (ReferenceAnnounceUrl,
00202                                  peerInfo,
00203                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00204                                  2048,
00205                                  1024,
00206                                  63488,
00207                                  true,
00208                                  TrackerRequest::Started,
00209                                  30,
00210                                  QByteArray ("364h3kd9472mfd03odj3"),
00211                                  QByteArray ("z84d74h39doz7dh47du5"));
00212         TrackerRequest request2 (ReferenceAnnounceUrl,
00213                                  peerInfo,
00214                                  Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00215                                  2048,
00216                                  1024,
00217                                  63488,
00218                                  true);
00219 
00220         request2 = request1;
00221         // After modifying one object the two objects should be different
00222         request2 = TrackerRequest (ReferenceAnnounceUrl,
00223                                    peerInfo,
00224                                    Torrent::InfoHash ("f7453fd52bd94os82h7f"),
00225                                    1234,
00226                                    5678,
00227                                    34970,
00228                                    false,
00229                                    TrackerRequest::Empty,
00230                                    40,
00231                                    QByteArray ("25dg48glnmf0tu68703g"),
00232                                    QByteArray ("8dh1sdfh3dhd53vxnfld"));
00233 
00234         CPPUNIT_ASSERT (request1 != request2);
00235     }
00236 };
00237 
00238 CPPUNIT_TEST_SUITE_REGISTRATION(TrackerRequestTest);
00239 
00240 } // namespace Testing
00241 } // namespace Trackers
00242 } // namespace BitTorrent
00243 } // namespace Protocols