HaveAllTest.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 "../HaveAll.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace BitTorrent {
00029 namespace Packets {
00030 namespace Testing {
00031 
00033 
00051 class HaveAllTest : public CppUnit::TestFixture
00052 {
00053     CPPUNIT_TEST_SUITE(HaveAllTest);
00054     CPPUNIT_TEST(testPacketProperties);
00055     CPPUNIT_TEST(testReadPacketOK);
00056     CPPUNIT_TEST(testReadPacketFails);
00057     CPPUNIT_TEST(testWritePacketOK);
00058     CPPUNIT_TEST_SUITE_END();
00059 
00060     auto_ptr <HaveAll>  packet;
00061 
00062 public:
00063     void setUp()
00064     {
00065         packet.reset (new HaveAll());
00066     }
00067 
00068     void tearDown()
00069     {
00070         packet.reset();
00071     }
00072 
00074 
00078     void scenarioReadPacket (const QByteArray &rawHeader,
00079                              const QByteArray &rawPayload,
00080                              bool parsedOk) {
00081         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00082     }
00083 
00085 
00089     void scenarioWritePacket (const QByteArray &rawHeader,
00090                               const QByteArray &rawPayload) {
00091         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00092         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00093     }
00094 
00096 
00104     void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00105                                      const QByteArray &rawPayload,
00106                                      const QByteArray &rawHeader2,
00107                                      const QByteArray &rawPayload2) {
00108         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00109 
00110         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00111         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00112     }
00113 
00115     void testPacketProperties()
00116     {
00117         CPPUNIT_ASSERT (HaveAllPacket == packet->packetType());
00118         CPPUNIT_ASSERT (QString ("HaveAll") == packet->name());
00119     }
00120 
00121     void testReadPacketOK() {
00122         scenarioReadPacket(
00123             QByteArray ("\0\0\0\1", 4),
00124             QByteArray ("\xe", 1),
00125             true);
00126     }
00127 
00128     void testReadPacketFails() {
00129         scenarioReadPacket(
00130             QByteArray ("\0\0\0\2", 4),
00131             QByteArray ("\xe\0", 2),
00132             false);
00133     }
00134 
00135     void testWritePacketOK() {
00136         scenarioWritePacket(
00137             QByteArray ("\0\0\0\1", 4),
00138             QByteArray ("\xe", 1));
00139     }
00140 };
00141 
00142 CPPUNIT_TEST_SUITE_REGISTRATION(HaveAllTest);
00143 
00144 } // namespace Testing
00145 } // namespace Packets
00146 } // namespace BitTorrent
00147 } // namespace Protocols