PacketProtocolTest.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 "../PacketProtocol.h"
00025 #include "../BadPacket.h"
00026 #include "../BitField.h"
00027 #include "../Cancel.h"
00028 #include "../Choke.h"
00029 #include "../Have.h"
00030 #include "../Interested.h"
00031 #include "../Keepalive.h"
00032 #include "../NotInterested.h"
00033 #include "../Piece.h"
00034 #include "../Request.h"
00035 #include "../Unchoke.h"
00036 #include "Imports.cpp"
00037 
00038 namespace Protocols {
00039 namespace BitTorrent {
00040 namespace Packets {
00041 namespace Testing {
00042 
00043 // \todo test HaveAll, HaveNone, AllowedFast, SuggestPiece, RejectRequest
00044 // \todo rename to PacketProtocolTest !!!
00045 class PacketFactoryTest : public CppUnit::TestFixture
00046 {
00047     CPPUNIT_TEST_SUITE (PacketFactoryTest);
00048     CPPUNIT_TEST (testHeaderLengthReturns4);
00049     CPPUNIT_TEST (testPayloadLengthIs0);
00050     CPPUNIT_TEST (testPayloadLengthIs1);
00051     CPPUNIT_TEST (testPayloadLengthIs12345);
00052     CPPUNIT_TEST (testCreatePacketKeepalive);
00053     CPPUNIT_TEST (testCreatePacketChoke);
00054     CPPUNIT_TEST (testCreatePacketUnchoke);
00055     CPPUNIT_TEST (testCreatePacketInterested);
00056     CPPUNIT_TEST (testCreatePacketNotInterested);
00057     CPPUNIT_TEST (testCreatePacketHave);
00058     CPPUNIT_TEST (testCreatePacketBitField);
00059     CPPUNIT_TEST (testCreatePacketRequest);
00060     CPPUNIT_TEST (testCreatePacketPiece);
00061     CPPUNIT_TEST (testCreatePacketCancel);
00062     CPPUNIT_TEST (testCreateBadPacketUnknownType);
00063     CPPUNIT_TEST (testCreateBadPacketChoke);
00064     CPPUNIT_TEST (testCreateBadPacketUnchoke);
00065     CPPUNIT_TEST_SUITE_END();
00066 
00067     auto_ptr <PacketProtocol>   protocol;
00068 
00069 public:
00070     PacketFactoryTest() : protocol()
00071     {
00072     }
00073 
00074     void setUp()
00075     {
00076         protocol.reset (new PacketProtocol());
00077     }
00078 
00079     void tearDown()
00080     {
00081         protocol.reset();
00082     }
00083 
00084     void testHeaderLengthReturns4()
00085     {
00086         willBeEqual (protocol->headerLength(), 4);
00087     }
00088 
00089     void testPayloadLengthIs0()
00090     {
00091         QByteArray header ("\0\0\0\0", 4);
00092         willBeEqual (protocol->payloadLength (header), 0);
00093     }
00094 
00095     void testPayloadLengthIs1()
00096     {
00097         QByteArray header ("\0\0\0\x1", 4);
00098         willBeEqual (protocol->payloadLength (header), 1);
00099     }
00100 
00101     void testPayloadLengthIs12345()
00102     {
00103         QByteArray header ("\0\0\x30\x39", 4);
00104         willBeEqual (protocol->payloadLength (header), 12345);
00105     }
00106 
00107     void scenarioCreatePacket (const QByteArray &header,
00108                                const QByteArray &payload,
00109                                const PacketBase &packet)
00110     {
00111         willBeEqual (protocol->createPacket (header, payload),
00112                      GenericsPacket (packet));
00113     }
00114 
00115     void testCreatePacketKeepalive()
00116     {
00117         scenarioCreatePacket (QByteArray ("\0\0\0\0", 4),
00118                               QByteArray (""),
00119                               Keepalive());
00120     }
00121 
00122     void testCreatePacketChoke()
00123     {
00124         scenarioCreatePacket (QByteArray ("\0\0\0\x1", 4),
00125                               QByteArray ("\x0", 1),
00126                               Choke());
00127     }
00128 
00129     void testCreatePacketUnchoke()
00130     {
00131         scenarioCreatePacket (QByteArray ("\0\0\0\x1", 4),
00132                               QByteArray ("\x1", 1),
00133                               Unchoke());
00134     }
00135 
00136     void testCreatePacketInterested()
00137     {
00138         scenarioCreatePacket (QByteArray ("\0\0\0\x1", 4),
00139                               QByteArray ("\x2", 1),
00140                               Interested());
00141     }
00142 
00143     void testCreatePacketNotInterested()
00144     {
00145         scenarioCreatePacket (QByteArray ("\0\0\0\x1", 4),
00146                               QByteArray ("\x3", 1),
00147                               NotInterested());
00148     }
00149 
00150     void testCreatePacketHave()
00151     {
00152         scenarioCreatePacket (QByteArray ("\0\0\0\x5", 4),
00153                               QByteArray ("\x4\x1\x2\x3\x4", 5),
00154                               Have (16909060));
00155     }
00156 
00157     void testCreatePacketBitField()
00158     {
00159         scenarioCreatePacket (QByteArray ("\0\0\0\x5", 4),
00160                               QByteArray ("\x5\x1\x2\x3\x4", 5),
00161                               BitField (QByteArray ("\x1\x2\x3\x4", 4)));
00162     }
00163 
00164     void testCreatePacketRequest()
00165     {
00166         scenarioCreatePacket (QByteArray ("\0\0\0\xd", 4),
00167                               QByteArray ("\x6\0\0\0\x1\0\0\0\x2\0\0\0\x3", 13),
00168                               Request (1,2, 3));
00169     }
00170 
00171     void testCreatePacketPiece()
00172     {
00173         scenarioCreatePacket (QByteArray ("\0\0\0\xf", 4),
00174                               QByteArray ("\x7\0\0\0\x1\0\0\0\x2\0\0\0\x3\0\0", 15),
00175                               Piece (1,2, QByteArray ("\0\0\0\x3\0\0", 6)));
00176     }
00177 
00178     void testCreatePacketCancel()
00179     {
00180         scenarioCreatePacket (QByteArray ("\0\0\0\xd", 4),
00181                               QByteArray ("\x8\0\0\0\x1\0\0\0\x2\0\0\0\x3", 13),
00182                               Cancel (1,2, 3));
00183     }
00184 
00185     void scenarioCreateBadPacket (const QByteArray &header,
00186                                   const QByteArray &payload)
00187     {
00188         PacketType type = static_cast <PacketType> (payload.at(0));
00189         scenarioCreatePacket (header, payload,
00190                               BadPacket (type, header, payload));
00191     }
00192 
00193     void testCreateBadPacketUnknownType()
00194     {
00195         scenarioCreateBadPacket (QByteArray ("\0\0\0\xa", 4),
00196                                  QByteArray ("\xef\0\0\0\x1\0\0\0\x2\0", 10));
00197     }
00198 
00199     void testCreateBadPacketChoke()
00200     {
00201         scenarioCreateBadPacket (QByteArray ("\0\0\0\xa", 4),
00202                                  QByteArray ("\x0\0\0\0\x1\0\0\0\x2\0", 10));
00203     }
00204 
00205     void testCreateBadPacketUnchoke()
00206     {
00207         scenarioCreateBadPacket (QByteArray ("\0\0\0\xa", 4),
00208                                  QByteArray ("\x1\0\0\0\x1\0\0\0\x2\0", 10));
00209     }
00210 };
00211 
00212 CPPUNIT_TEST_SUITE_REGISTRATION (PacketFactoryTest);
00213 
00214 } // namespace Testing
00215 } // namespace Packets
00216 } // namespace BitTorrent
00217 } // namespace Protocols