HaveTest.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 "../Have.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace BitTorrent {
00029 namespace Packets {
00030 namespace Testing {
00031 
00033 
00051 class HaveTest : public CppUnit::TestFixture
00052 {
00053     CPPUNIT_TEST_SUITE(HaveTest);
00054     CPPUNIT_TEST(testPacketProperties);
00055     CPPUNIT_TEST(testReadPacketOK);
00056     CPPUNIT_TEST(testReadPacketFails);
00057     CPPUNIT_TEST(testWritePacketOK);
00058     CPPUNIT_TEST(testReadSetWritePacketOK);
00059     CPPUNIT_TEST_SUITE_END();
00060 
00061     auto_ptr <Have> packet;
00062 
00063 public:
00064     void setUp()
00065     {
00066         packet.reset (new Have());
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         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00083 
00084         if (parsedOk) {
00085             CPPUNIT_ASSERT (packet->pieceIndex() == pieceIndex);
00086         }
00087     }
00088 
00090 
00094     void scenarioWritePacket (quint32 pieceIndex,
00095                               const QByteArray &rawHeader,
00096                               const QByteArray &rawPayload) {
00097         packet->setPieceIndex (pieceIndex);
00098 
00099         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00100         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00101     }
00102 
00104 
00112     void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00113                                      const QByteArray &rawPayload,
00114                                      quint32 pieceIndex,
00115                                      const QByteArray &rawHeader2,
00116                                      const QByteArray &rawPayload2) {
00117         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00118 
00119         packet->setPieceIndex (pieceIndex);
00120 
00121         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00122         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00123     }
00124 
00126     void testPacketProperties()
00127     {
00128         CPPUNIT_ASSERT (HavePacket == packet->packetType());
00129         CPPUNIT_ASSERT (QString ("Have") == packet->name());
00130     }
00131 
00132     void testReadPacketOK() {
00133         scenarioReadPacket(
00134             QByteArray ("\0\0\0\5", 4),
00135             QByteArray ("\4\1\2\3\4", 5),
00136             true,
00137             0x01020304);
00138     }
00139 
00140     void testReadPacketFails() {
00141         scenarioReadPacket(
00142             QByteArray ("\0\0\0\2", 4),
00143             QByteArray ("\4\0", 2),
00144             false,
00145             0);
00146     }
00147 
00148     void testWritePacketOK() {
00149         scenarioWritePacket(
00150             0x01020304,
00151             QByteArray ("\0\0\0\5", 4),
00152             QByteArray ("\4\1\2\3\4", 5));
00153     }
00154 
00155     void testReadSetWritePacketOK() {
00156         scenarioReadSetWritePacket(
00157             QByteArray ("\0\0\0\5", 4),
00158             QByteArray ("\4\1\2\3\4", 5),
00159             0x04030201,
00160             QByteArray ("\0\0\0\5", 4),
00161             QByteArray ("\4\4\3\2\1", 5));
00162     }
00163 };
00164 
00165 CPPUNIT_TEST_SUITE_REGISTRATION(HaveTest);
00166 
00167 } // namespace Testing
00168 } // namespace Packets
00169 } // namespace BitTorrent
00170 } // namespace Protocols