PingTest.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 "../Ping.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Protocols {
00028 namespace Gnutella {
00029 namespace Packets {
00030 namespace Testing {
00031 
00033 
00051 class PingTest : public CppUnit::TestFixture
00052 {
00053     CPPUNIT_TEST_SUITE(PingTest);
00054     CPPUNIT_TEST(testPacketProperties);
00055     CPPUNIT_TEST(testReadPacketOneSingleGgepBlock);
00056     CPPUNIT_TEST(testReadPacketNoPayload);
00057     CPPUNIT_TEST(testReadPacketOneInvalidGgepBlock);
00058     CPPUNIT_TEST(testWritePacketOneGgepBlock);
00059     CPPUNIT_TEST(testReadSetWritePacketOK);
00060     CPPUNIT_TEST_SUITE_END();
00061 
00062     auto_ptr <Ping> packet;
00063 
00064 public:
00065     void setUp()
00066     {
00067         packet.reset (new Ping());
00068     }
00069 
00070     void tearDown()
00071     {
00072         packet.reset();
00073     }
00074 
00076 
00080     void scenarioReadPacket (const QByteArray &rawHeader,
00081                              const QByteArray &rawPayload,
00082                              bool parsedOk,const GgepBlock &ggepBlock) {
00083         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00084 
00085         if (parsedOk) {
00086             CPPUNIT_ASSERT (packet->ggepBlock() == ggepBlock);
00087         }
00088     }
00089 
00091 
00095     void scenarioWritePacket (const GgepBlock &ggepBlock,
00096                               const QByteArray &rawHeader,
00097                               const QByteArray &rawPayload) {
00098         packet->setGgepBlock (ggepBlock);
00099 
00100         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00101         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00102     }
00103 
00105 
00113     void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00114                                      const QByteArray &rawPayload,
00115                                      const GgepBlock &ggepBlock,
00116                                      const QByteArray &rawHeader2,
00117                                      const QByteArray &rawPayload2) {
00118         CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00119 
00120         packet->setGgepBlock (ggepBlock);
00121 
00122         CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00123         CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00124     }
00125 
00127     void testPacketProperties()
00128     {
00129         CPPUNIT_ASSERT (PingPacket == packet->packetType());
00130         CPPUNIT_ASSERT (QString ("Ping") == packet->name());
00131     }
00132 
00133     void testReadPacketOneSingleGgepBlock() {
00134         scenarioReadPacket(
00135             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\0\x7\x8\xE\0\0\0", 23),
00136             QByteArray ("\xC3\x84UNKN\107fortest", 14),
00137             true,
00138             GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest")));
00139     }
00140 
00141     void testReadPacketNoPayload() {
00142         scenarioReadPacket(
00143             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\0\x7\x8\0\0\0\0", 23),
00144             QByteArray(),
00145             true,
00146             GgepBlock());
00147     }
00148 
00149     void testReadPacketOneInvalidGgepBlock() {
00150         scenarioReadPacket(
00151             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\0\x7\x8\x2\0\0\0", 23),
00152             QByteArray ("\195\100", 2),
00153             false,
00154             GgepBlock());
00155     }
00156 
00157     void testWritePacketOneGgepBlock() {
00158         scenarioWritePacket(
00159             GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest")),
00160             packet->rawHeader().replace(19, 4, QByteArray("\xE\0\0\0",4)),
00161             QByteArray ("\xC3\x84UNKN\107fortest", 14));
00162     }
00163 
00164     void testReadSetWritePacketOK() {
00165         scenarioReadSetWritePacket(
00166             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\0\x7\x8\xE\0\0\0", 23),
00167             QByteArray ("\xC3\x84UNKN\107fortest", 14),
00168             GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest2")),
00169             QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\0\x7\x8\xF\0\0\0", 23),
00170             QByteArray ("\xC3\x84UNKN\110fortest2", 15));
00171     }
00172 };
00173 
00174 CPPUNIT_TEST_SUITE_REGISTRATION(PingTest);
00175 
00176 } // namespace Testing
00177 } // namespace Packets
00178 } // namespace Gnutella
00179 } // namespace Protocols