RequestTest.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 "../Request.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace BitTorrent {
00029 namespace Packets {
00030 namespace Testing {
00031 
00033 
00051 class RequestTest : public CppUnit::TestFixture
00052 {
00053     CPPUNIT_TEST_SUITE(RequestTest);
00054     CPPUNIT_TEST(testPacketProperties);
00055     CPPUNIT_TEST(testReadPacketOK);
00056     CPPUNIT_TEST(testReadPacketFail);
00057     CPPUNIT_TEST(testWritePacketOK);
00058     CPPUNIT_TEST(testReadSetWritePacketOK);
00059     CPPUNIT_TEST_SUITE_END();
00060 
00061     auto_ptr <Request>  packet;
00062 
00063 public:
00064     void setUp()
00065     {
00066         packet.reset (new Request());
00067     }
00068 
00069     void tearDown()
00070     {
00071         packet.reset();
00072     }
00073 
00075 
00079     void scenarioReadPacket (const QByteArray &rawHeader,
00080                              const QByteArray &rawPayload,
00081                              bool parsedOk,quint32 pieceIndex,
00082                              quint32 startOffset,
00083                              quint32 bytesCount) {
00084         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00085 
00086         if (parsedOk) {
00087             CPPUNIT_ASSERT (packet->pieceIndex() == pieceIndex);
00088             CPPUNIT_ASSERT (packet->startOffset() == startOffset);
00089             CPPUNIT_ASSERT (packet->bytesCount() == bytesCount);
00090         }
00091     }
00092 
00094 
00098     void scenarioWritePacket (quint32 pieceIndex,
00099                               quint32 startOffset,
00100                               quint32 bytesCount,
00101                               const QByteArray &rawHeader,
00102                               const QByteArray &rawPayload) {
00103         packet->setPieceIndex (pieceIndex);
00104         packet->setStartOffset (startOffset);
00105         packet->setBytesCount (bytesCount);
00106 
00107         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00108         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00109     }
00110 
00112 
00120     void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00121                                      const QByteArray &rawPayload,
00122                                      quint32 pieceIndex,
00123                                      quint32 startOffset,
00124                                      quint32 bytesCount,
00125                                      const QByteArray &rawHeader2,
00126                                      const QByteArray &rawPayload2) {
00127         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00128 
00129         packet->setPieceIndex (pieceIndex);
00130         packet->setStartOffset (startOffset);
00131         packet->setBytesCount (bytesCount);
00132 
00133         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00134         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00135     }
00136 
00138     void testPacketProperties()
00139     {
00140         CPPUNIT_ASSERT (RequestPacket == packet->packetType());
00141         CPPUNIT_ASSERT (QString ("Request") == packet->name());
00142     }
00143 
00144     void testReadPacketOK() {
00145         scenarioReadPacket(
00146             QByteArray ("\0\0\0\15", 4),
00147             QByteArray ("\6\1\2\3\4\5\6\7\10\11\12\13\14", 13),
00148             true,
00149             0x01020304,
00150             0x05060708,
00151             0x090A0B0C);
00152     }
00153 
00154     void testReadPacketFail() {
00155         scenarioReadPacket(
00156             QByteArray ("\0\0\0\14", 4),
00157             QByteArray ("\6\0\0\0\0\0\0\0\0\0\0\0", 12),
00158             false,
00159             0,
00160             0,
00161             0);
00162     }
00163 
00164     void testWritePacketOK() {
00165         scenarioWritePacket(
00166             0x01020304,
00167             0x05060708,
00168             0x090A0B0C,
00169             QByteArray ("\0\0\0\15", 4),
00170             QByteArray ("\6\1\2\3\4\5\6\7\10\11\12\13\14", 13));
00171     }
00172 
00173     void testReadSetWritePacketOK() {
00174         scenarioReadSetWritePacket(
00175             QByteArray ("\0\0\0\15", 4),
00176             QByteArray ("\6\1\2\3\4\5\6\7\0\1\2\3\4", 13),
00177             0x01020304,
00178             0x05060708,
00179             0x090A0B0C,
00180             QByteArray ("\0\0\0\15", 4),
00181             QByteArray ("\6\1\2\3\4\5\6\7\10\11\12\13\14", 13));
00182     }
00183 };
00184 
00185 CPPUNIT_TEST_SUITE_REGISTRATION(RequestTest);
00186 
00187 } // namespace Testing
00188 } // namespace Packets
00189 } // namespace BitTorrent
00190 } // namespace Protocols