00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "../TrackerRequestUrlCreator.h"
00025 #include "Imports.cpp"
00026 #include <sstream>
00027 #include <limits>
00028
00029 namespace Protocols {
00030 namespace BitTorrent {
00031 namespace Trackers {
00032 namespace Testing {
00033
00035
00037 class TrackerRequestUrlCreatorTest : public CppUnit::TestFixture
00038 {
00039 CPPUNIT_TEST_SUITE (TrackerRequestUrlCreatorTest);
00040 CPPUNIT_TEST (testCreateRequestUrlMinimalRequest);
00041 CPPUNIT_TEST (testCreateRequestUrlCompleteRequest);
00042 CPPUNIT_TEST (testCreateRequestUrlMinNumbers);
00043 CPPUNIT_TEST (testCreateRequestUrlMaxNumbers);
00044 CPPUNIT_TEST_SUITE_END();
00045
00047
00051 void scenarioCreateRequestUrl (const TrackerRequest &request,
00052 const QByteArray &referenceUrl)
00053 {
00054 const Uri requestUrl = TrackerRequestUrlCreator::createRequestUrl (
00055 request);
00056 CPPUNIT_ASSERT (requestUrl.encoded() == referenceUrl);
00057 }
00058
00060 template <class T>
00061 static QByteArray stringifyNum (T num)
00062 {
00063 std::ostringstream out;
00064 out << num;
00065 return QByteArray (out.str().c_str());
00066 }
00067
00068 const QByteArray ReferenceAnnounceUrl;
00069 const QByteArray ReferenceMinimalRequestUrlQueryPart;
00070 const QByteArray ReferenceMinimalRequestUrl;
00071 const QByteArray ReferenceCompleteRequestUrlQueryPart;
00072 const QByteArray ReferenceCompleteRequestUrl;
00073
00074 const TrackerRequest MinimalRequest;
00075 const TrackerRequest CompleteRequest;
00076
00077 public:
00078 TrackerRequestUrlCreatorTest();
00079
00081 void testCreateRequestUrlMinimalRequest()
00082 {
00083 scenarioCreateRequestUrl (MinimalRequest, ReferenceMinimalRequestUrl);
00084 }
00085
00087 void testCreateRequestUrlCompleteRequest()
00088 {
00089 scenarioCreateRequestUrl (CompleteRequest, ReferenceCompleteRequestUrl);
00090 }
00091
00093 void testCreateRequestUrlMinNumbers()
00094 {
00095 TrackerRequest request (Uri::fromUnencoded (ReferenceAnnounceUrl),
00096 PeerInfo ("10.0.0.0",
00097 std::numeric_limits<quint16>::min(),
00098 PeerInfo::PeerId ("h584hfp70x52ns05jc75")),
00099 Torrent::InfoHash ("klotp50374hx75g36123"),
00100 std::numeric_limits<quint64>::min(),
00101 std::numeric_limits<quint64>::min(),
00102 std::numeric_limits<quint64>::min(),
00103 false,
00104 TrackerRequest::Stopped,
00105 std::numeric_limits<uint>::min(),
00106 "xz42vdlgo60svx64hryz",
00107 "dht73f2caldor058cp08");
00108
00109 QByteArray referenceUrl = ReferenceAnnounceUrl +
00110 "?info_hash=klotp50374hx75g36123"
00111 "&peer_id=h584hfp70x52ns05jc75"
00112 "&port=" + stringifyNum (std::numeric_limits<quint16>::min()) +
00113 "&uploaded=" + stringifyNum (std::numeric_limits<quint64>::min()) +
00114 "&downloaded=" + stringifyNum (std::numeric_limits<quint64>::min()) +
00115 "&left=" + stringifyNum (std::numeric_limits<quint64>::min()) +
00116 "&compact=0"
00117 "&event=stopped"
00118 "&ip=10.0.0.0"
00119 "&numwant=" + stringifyNum (std::numeric_limits<uint>::min()) +
00120 "&key=xz42vdlgo60svx64hryz"
00121 "&trackerid=dht73f2caldor058cp08";
00122
00123 scenarioCreateRequestUrl (request, referenceUrl);
00124 }
00125
00127 void testCreateRequestUrlMaxNumbers()
00128 {
00129 TrackerRequest request (Uri::fromUnencoded (ReferenceAnnounceUrl),
00130 PeerInfo ("255.255.255.255",
00131 std::numeric_limits<quint16>::max(),
00132 PeerInfo::PeerId ("h584hfp70x52ns05jc75")),
00133 Torrent::InfoHash ("klotp50374hx75g36123"),
00134 std::numeric_limits<quint64>::max(),
00135 std::numeric_limits<quint64>::max(),
00136 std::numeric_limits<quint64>::max(),
00137 true,
00138 TrackerRequest::Stopped,
00139 std::numeric_limits<uint>::max(),
00140 "xz42vdlgo60svx64hryz",
00141 "dht73f2caldor058cp08");
00142
00143 QByteArray referenceUrl = ReferenceAnnounceUrl +
00144 "?info_hash=klotp50374hx75g36123"
00145 "&peer_id=h584hfp70x52ns05jc75"
00146 "&port=" + stringifyNum (std::numeric_limits<quint16>::max()) +
00147 "&uploaded=" + stringifyNum (std::numeric_limits<quint64>::max()) +
00148 "&downloaded=" + stringifyNum (std::numeric_limits<quint64>::max()) +
00149 "&left=" + stringifyNum (std::numeric_limits<quint64>::max()) +
00150 "&compact=1"
00151 "&event=stopped"
00152 "&ip=255.255.255.255"
00153 "&numwant=" + stringifyNum (std::numeric_limits<uint>::max()) +
00154 "&key=xz42vdlgo60svx64hryz"
00155 "&trackerid=dht73f2caldor058cp08";
00156
00157 scenarioCreateRequestUrl (request, referenceUrl);
00158 }
00159 };
00160
00161 CPPUNIT_TEST_SUITE_REGISTRATION(TrackerRequestUrlCreatorTest);
00162
00164
00169 TrackerRequestUrlCreatorTest::TrackerRequestUrlCreatorTest()
00170 : ReferenceAnnounceUrl ("http://tracker.com/announce.php"),
00171 ReferenceMinimalRequestUrlQueryPart (
00172 "?info_hash=z53bf64bd05hfn2daqml"
00173 "&peer_id=-CA0610-574938205849"
00174 "&port=6881"
00175 "&uploaded=4096"
00176 "&downloaded=2048"
00177 "&left=63488"
00178 "&compact=0"
00179 "&event=empty"
00180 "&numwant=0"),
00181 ReferenceMinimalRequestUrl (
00182 ReferenceAnnounceUrl + ReferenceMinimalRequestUrlQueryPart),
00183 ReferenceCompleteRequestUrlQueryPart (
00184 "?info_hash=fh37dosi5jv0c91g3654"
00185 "&peer_id=-CA0610-574938205849"
00186 "&port=6882"
00187 "&uploaded=12345"
00188 "&downloaded=120"
00189 "&left=63488"
00190 "&compact=1"
00191 "&event=started"
00192 "&ip=127.0.0.1"
00193 "&numwant=30"
00194 "&key=364h3kd9472mfd03odj3"
00195 "&trackerid=z84d74h39doz7dh47du5"),
00196 ReferenceCompleteRequestUrl (
00197 ReferenceAnnounceUrl + ReferenceCompleteRequestUrlQueryPart),
00198 MinimalRequest (
00199 Uri::fromUnencoded (ReferenceAnnounceUrl),
00200 PeerInfo ("0.0.0.0", 6881, PeerInfo::PeerId ("-CA0610-574938205849")),
00201 Torrent::InfoHash ("z53bf64bd05hfn2daqml"),
00202 4096,
00203 2048,
00204 63488,
00205 false),
00206 CompleteRequest (
00207 Uri::fromUnencoded (ReferenceAnnounceUrl),
00208 PeerInfo ("127.0.0.1", 6882, PeerInfo::PeerId ("-CA0610-574938205849")),
00209 Torrent::InfoHash ("fh37dosi5jv0c91g3654"),
00210 12345,
00211 120,
00212 63488,
00213 true,
00214 TrackerRequest::Started,
00215 30,
00216 QByteArray ("364h3kd9472mfd03odj3"),
00217 QByteArray ("z84d74h39doz7dh47du5"))
00218 {
00219 }
00220
00221 }
00222 }
00223 }
00224 }