00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "../Push.h"
00025 #include "Imports.cpp"
00026
00027 namespace Protocols {
00028 namespace Gnutella {
00029 namespace Packets {
00030 namespace Testing {
00031
00033
00051 class PushTest : public CppUnit::TestFixture
00052 {
00053 CPPUNIT_TEST_SUITE(PushTest);
00054 CPPUNIT_TEST(testPacketProperties);
00055 CPPUNIT_TEST(testReadPacketOK);
00056 CPPUNIT_TEST(testReadPacketNoPayload);
00057 CPPUNIT_TEST(testWritePacketOK);
00058 CPPUNIT_TEST(testReadSetWritePacketOK);
00059 CPPUNIT_TEST_SUITE_END();
00060
00061 auto_ptr <Push> packet;
00062
00063 public:
00064 void setUp()
00065 {
00066 packet.reset (new Push());
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,QUuid serventId,
00082 quint32 fileIndex,
00083 const QHostAddress &ipAddress,
00084 quint16 port,
00085 const GgepBlock &ggepBlock) {
00086 CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload) == parsedOk);
00087
00088 if (parsedOk) {
00089 CPPUNIT_ASSERT (packet->serventId() == serventId);
00090 CPPUNIT_ASSERT (packet->fileIndex() == fileIndex);
00091 CPPUNIT_ASSERT (packet->ipAddress() == ipAddress);
00092 CPPUNIT_ASSERT (packet->port() == port);
00093 CPPUNIT_ASSERT (packet->ggepBlock() == ggepBlock);
00094 }
00095 }
00096
00098
00102 void scenarioWritePacket (QUuid serventId,
00103 quint32 fileIndex,
00104 const QHostAddress &ipAddress,
00105 quint16 port,
00106 const GgepBlock &ggepBlock,
00107 const QByteArray &rawHeader,
00108 const QByteArray &rawPayload) {
00109 packet->setServentId (serventId);
00110 packet->setFileIndex (fileIndex);
00111 packet->setIpAddress (ipAddress);
00112 packet->setPort (port);
00113 packet->setGgepBlock (ggepBlock);
00114
00115 CPPUNIT_ASSERT (packet->rawHeader() == rawHeader);
00116 CPPUNIT_ASSERT (packet->rawPayload() == rawPayload);
00117 }
00118
00120
00128 void scenarioReadSetWritePacket (const QByteArray &rawHeader,
00129 const QByteArray &rawPayload,
00130 QUuid serventId,
00131 quint32 fileIndex,
00132 const QHostAddress &ipAddress,
00133 quint16 port,
00134 const GgepBlock &ggepBlock,
00135 const QByteArray &rawHeader2,
00136 const QByteArray &rawPayload2) {
00137 CPPUNIT_ASSERT (packet->parse (rawHeader, rawPayload));
00138
00139 packet->setServentId (serventId);
00140 packet->setFileIndex (fileIndex);
00141 packet->setIpAddress (ipAddress);
00142 packet->setPort (port);
00143 packet->setGgepBlock (ggepBlock);
00144
00145 CPPUNIT_ASSERT (packet->rawHeader() == rawHeader2);
00146 CPPUNIT_ASSERT (packet->rawPayload() == rawPayload2);
00147 }
00148
00150 void testPacketProperties()
00151 {
00152 CPPUNIT_ASSERT (PushPacket == packet->packetType());
00153 CPPUNIT_ASSERT (QString ("Push") == packet->name());
00154 }
00155
00156 void testReadPacketOK() {
00157 scenarioReadPacket(
00158 QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x40\x7\x8\x28\0\0\0", 23),
00159 QByteArray ("\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x70\x56\x34\x12\x55\xd6\x3c\xb9\x67\x45\xC3\x84UNKN\107fortest", 40),
00160 true,
00161 QUuid(0x01020304, 0x0005, 0x0607, '\x8', '\x9', '\xa', '\xb', '\xc', '\xd', '\xe', '\xf'),
00162 0x12345670,
00163 QHostAddress(0x55d63cb9),
00164 0x4567,
00165 GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest")));
00166 }
00167
00168 void testReadPacketNoPayload() {
00169 scenarioReadPacket(
00170 QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x40\x7\x8\0\0\0\0", 23),
00171 QByteArray(),
00172 false,
00173 QUuid(),
00174 0,
00175 QHostAddress(quint32(0)),
00176 0,
00177 GgepBlock());
00178 }
00179
00180 void testWritePacketOK() {
00181 scenarioWritePacket(
00182 QUuid(0x01020304, 0x0005, 0x0607, '\x8', '\x9', '\xa', '\xb', '\xc', '\xd', '\xe', '\xf'),
00183 0x12345670,
00184 QHostAddress(0x55d63cb9),
00185 0x4567,
00186 GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest")),
00187 packet->rawHeader().replace(19, 4, QByteArray("\x28\0\0\0",4)),
00188 QByteArray ("\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x70\x56\x34\x12\x55\xd6\x3c\xb9\x67\x45\xC3\x84UNKN\107fortest", 40));
00189 }
00190
00191 void testReadSetWritePacketOK() {
00192 scenarioReadSetWritePacket(
00193 QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x40\x7\x8\x28\0\0\0", 23),
00194 QByteArray ("\x5\x2\x3\x4\x0\x5\x3\x7\x8\xa\xa\x0\xc\xd\xf\x7\x30\x07\x33\x11\x7f\x00\x00\x01\x33\x33\xC3\x84UNKN\107fortest", 40),
00195 QUuid(0x01020304, 0x0005, 0x0607, '\x8', '\x9', '\xa', '\xb', '\xc', '\xd', '\xe', '\xf'),
00196 0x12345670,
00197 QHostAddress(0x55d63cb9),
00198 0x4567,
00199 GgepBlock().addExtension(Ggeps::Unknown("UNKN","fortest2")),
00200 QByteArray ("\x1\x2\x3\x4\x5\x6\x7\x8\x9\0\x1\x2\x3\x4\x5\x6\x40\x7\x8\x29\0\0\0", 23),
00201 QByteArray ("\x1\x2\x3\x4\x0\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x70\x56\x34\x12\x55\xd6\x3c\xb9\x67\x45\xC3\x84UNKN\110fortest2", 41));
00202 }
00203 };
00204
00205 CPPUNIT_TEST_SUITE_REGISTRATION(PushTest);
00206
00207 }
00208 }
00209 }
00210 }