QueryHitsTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-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 "../QueryHits.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace Gnutella {
00029 namespace Packets {
00030 namespace Testing {
00031 
00033 
00051 class QueryHitsTest : public CppUnit::TestFixture
00052 {
00053     CPPUNIT_TEST_SUITE(QueryHitsTest);
00054     CPPUNIT_TEST(testPacketProperties);
00055     CPPUNIT_TEST(testReadPacketOK);
00056     CPPUNIT_TEST(testReadPacketBAD);
00057     CPPUNIT_TEST(testWritePacketOK);
00058     CPPUNIT_TEST(testReadSetWritePacketOK);
00059     CPPUNIT_TEST_SUITE_END();
00060 
00061     Result                  simpleResult;
00062     QueryHitsData           simpleQueryHitsData;
00063     auto_ptr <QueryHits>    packet;
00064 
00065 public:
00066     QueryHitsTest() : simpleResult(), simpleQueryHitsData(), packet()
00067     {
00068     }
00069 
00070     void setUp()
00071     {
00072         simpleResult.fileIndex = 0x12345678;
00073         simpleResult.fileSize = 0x23456789;
00074         simpleResult.fullFileName = QString("TAOCP");
00075 
00076         simpleQueryHitsData.serventId = QUuid (0x01020304, 0x0005, 0x0607,
00077             '\x8', '\x9', '\xa', '\xb', '\xc', '\xd', '\xe', '\xf');
00078         packet.reset (new QueryHits());
00079     }
00080 
00081     void tearDown()
00082     {
00083         packet.reset();
00084     }
00085 
00087 
00091     void scenarioReadPacket (const QByteArray &rawHeader,
00092                              const QByteArray &rawPayload,
00093                              bool parsedOk,
00094                              quint16 port,
00095                              const QHostAddress &ipAddress,
00096                              quint32 speed,
00097                              const ResultSet &resultSet,
00098                              const QueryHitsData &queryHitsData) {
00099         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00100 
00101         if (parsedOk) {
00102             //CPPUNIT_ASSERT (packet->numberOfHits() == numberOfHits);
00103             CPPUNIT_ASSERT (packet->port() == port);
00104             CPPUNIT_ASSERT (packet->ipAddress() == ipAddress);
00105             CPPUNIT_ASSERT (packet->speed() == speed);
00106             CPPUNIT_ASSERT (packet->resultSet() == resultSet);
00107             CPPUNIT_ASSERT (packet->queryHitsData() == queryHitsData);
00108         }
00109     }
00110 
00112 
00116     void scenarioWritePacket (//uchar numberOfHits,
00117                               quint16 port,
00118                               const QHostAddress &ipAddress,
00119                               quint32 speed,
00120                               const ResultSet &resultSet,
00121                               const QueryHitsData &queryHitsData,
00122                               const QByteArray &rawHeader,
00123                               const QByteArray &rawPayload) {
00124         //packet->setNumberOfHits (numberOfHits);
00125         packet->setPort (port);
00126         packet->setIpAddress (ipAddress);
00127         packet->setSpeed (speed);
00128         packet->setResultSet (resultSet);
00129         packet->setQueryHitsData (queryHitsData);
00130         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00131         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00132     }
00133 
00135 
00143     void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00144                                      const QByteArray &rawPayload,
00145                                      //uchar numberOfHits,
00146                                      quint16 port,
00147                                      const QHostAddress &ipAddress,
00148                                      quint32 speed,
00149                                      const ResultSet &resultSet,
00150                                      const QueryHitsData &queryHitsData,
00151                                      const QByteArray &rawHeader2,
00152                                      const QByteArray &rawPayload2) {
00153         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00154 
00155         //packet->setNumberOfHits (numberOfHits);
00156         packet->setPort (port);
00157         packet->setIpAddress (ipAddress);
00158         packet->setSpeed (speed);
00159         packet->setResultSet (resultSet);
00160         packet->setQueryHitsData (queryHitsData);
00161 
00162         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00163         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00164     }
00165 
00167     void testPacketProperties()
00168     {
00169         CPPUNIT_ASSERT (QueryHitsPacket == packet->packetType());
00170         CPPUNIT_ASSERT (QString ("QueryHits") == packet->name());
00171     }
00172 
00173     void testReadPacketOK() {
00174         scenarioReadPacket(
00175             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x81\x7\x8\x2a\0\0\0", 23),
00176             QByteArray ("\x01" "\x67\x45" "\x55\xd6\x3c\xb9" "\x70\x56\x34\x12" "\x78\x56\x34\x12" "\x89\x67\x45\x23" "TAOCP\0" "\0" "UNKN\x02\x3c\x01" "\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf", 42),
00177             true,
00178             //1,
00179             0x4567,
00180             QHostAddress (0x55d63cb9),
00181             0x12345670,
00182             ResultSet() += simpleResult,
00183             simpleQueryHitsData);
00184     }
00185 
00186     void testReadPacketBAD() {
00187         scenarioReadPacket(
00188             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x81\x7\x8\x29\0\0\0", 23),
00189             QByteArray ("\x01" "\x67\x15" "\x55\xd6\x3c\xb9" "\x70\x56\x34\x12" "\x78\x56\x24\x12" "\x83\x67\x45\x23" "TAOCP\0" "\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf", 41),
00190             false,
00191             //1,
00192             0x4567,
00193             QHostAddress (0x55d63cb9),
00194             0x12345670,
00195             ResultSet() += simpleResult,
00196             simpleQueryHitsData);
00197     }
00198 
00199     void testWritePacketOK() {
00200         scenarioWritePacket(
00201             //1,
00202             0x4567,
00203             QHostAddress(0x55d63cb9),
00204             0x12345670,
00205             ResultSet() += simpleResult,
00206             simpleQueryHitsData,
00207             packet->rawHeader().replace(19, 4, QByteArray("\x31\0\0\0",4)),
00208             QByteArray ("\x01" "\x67\x45" "\x55\xd6\x3c\xb9" "\x70\x56\x34\x12" "\x78\x56\x34\x12" "\x89\x67\x45\x23" "TAOCP\0" "\0" "UNKN\x02\x3c\x01" "\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf", 49));
00209     }
00210 
00211     void testReadSetWritePacketOK() {
00212         scenarioReadSetWritePacket(
00213             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x81\x7\x8\x29\0\0\0", 23),
00214             QByteArray ("\x1" "\x07\x45" "\x55\xd2\x3c\xb9" "\x70\x55\x34\x12" "\x78\x56\x24\x12" "\x89\x62\x35\x23" "TAOP\0" "\0" "\x1\x2\x3\x4\x0\x5\x6\x3\x8\x9\xa\xb\x3\xd\xf\xf", 41),
00215             //1,
00216             0x4567,
00217             QHostAddress (0x55d63cb9),
00218             0x12345670,
00219             ResultSet() += simpleResult,
00220             simpleQueryHitsData,
00221             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x81\x7\x8\x31\0\0\0", 23),
00222             QByteArray ("\x1" "\x67\x45" "\x55\xd6\x3c\xb9" "\x70\x56\x34\x12" "\x78\x56\x34\x12" "\x89\x67\x45\x23" "TAOCP\0" "\0" "UNKN\x02\x3c\x01" "\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf", 49));
00223     }
00224 };
00225 
00226 CPPUNIT_TEST_SUITE_REGISTRATION(QueryHitsTest);
00227 
00228 } // namespace Testing
00229 } // namespace Packets
00230 } // namespace Gnutella
00231 } // namespace Protocols