GgepTest.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 "../Ggep.h"
00025 #include "Utils/Encodings/Cobs.h"
00026 #include "Utils/Encodings/Deflate.h"
00027 #include "Imports.cpp"
00028 
00029 namespace Protocols {
00030 namespace Gnutella {
00031 namespace Packets {
00032 namespace Extensions {
00033 namespace Testing {
00034 
00036 
00040 class GgepTest : public CppUnit::TestFixture
00041 {
00042     CPPUNIT_TEST_SUITE(GgepTest);
00043     CPPUNIT_TEST(testCtorEncodingFlagSet);
00044     CPPUNIT_TEST(testCtorCompressionFlagSet);
00045     CPPUNIT_TEST(testCtorReservedFlagSet);
00046     CPPUNIT_TEST(testCtorNoFlagSet);
00047     CPPUNIT_TEST(testCtorCompressionAndEncodingFlagsSet);
00048     CPPUNIT_TEST(testParseNoCompressionNoEncoding);
00049     CPPUNIT_TEST(testParseCompressionNoEncoding);
00050     CPPUNIT_TEST(testParseNoCompressionEncoding);
00051     CPPUNIT_TEST(testParseCompressionEncoding);
00052     CPPUNIT_TEST(testParseCompressionEncodingNullData);
00053     CPPUNIT_TEST(testRawDataCompressionNoEncoding);
00054     CPPUNIT_TEST(testRawDataNoCompressionNoEncoding);
00055     CPPUNIT_TEST(testRawDataCompressionEncoding);
00056     CPPUNIT_TEST(testRawDataNoCompressionNoEncoding);
00057     CPPUNIT_TEST(testRawDataCompressionEncodingNullData);
00058     CPPUNIT_TEST(testFromId);
00059     CPPUNIT_TEST(testOperatorEqualIdDifferent);
00060     CPPUNIT_TEST(testOperatorEqualDataDifferent);
00061     CPPUNIT_TEST(testOperatorEqualFlagDifferent);
00062     CPPUNIT_TEST(testOperatorEqualRealEqual);
00063     CPPUNIT_TEST_SUITE_END();
00064 
00065 public:
00067     void scenarioCtor (int flags, bool encoding,
00068                        bool compression, bool reserved)
00069     {
00070         Ggeps::Unknown ggep ("unknown", "fortest", flags);
00071         CPPUNIT_ASSERT (ggep.isFlagSet (Ggep::Encoding) == encoding);
00072         CPPUNIT_ASSERT (ggep.isFlagSet (Ggep::Compression) == compression);
00073         CPPUNIT_ASSERT (ggep.isFlagSet (Ggep::Reserved) == reserved);
00074     }
00075 
00076     void testCtorEncodingFlagSet()
00077     {
00078         scenarioCtor (0x40, true, false, false);
00079     }
00080 
00081     void testCtorCompressionFlagSet()
00082     {
00083         scenarioCtor (0x20, false, true, false);
00084     }
00085 
00086     void testCtorReservedFlagSet()
00087     {
00088         scenarioCtor (0x10, false, false, true);
00089     }
00090 
00091     void testCtorNoFlagSet()
00092     {
00093         scenarioCtor (0x00, false, false, false);
00094     }
00095 
00096     void testCtorCompressionAndEncodingFlagsSet()
00097     {
00098         scenarioCtor (0x60, true, true, false);
00099     }
00100 
00102 
00108     void scenarioParse (const QByteArray &parsedData,
00109                         bool compress, bool encode)
00110     {
00111         QByteArray rawData = parsedData;
00112         int flags = 0;
00113         if (compress) {
00114             rawData = Utils::Encodings::Deflate::compress (rawData);
00115             flags |= Ggep::Compression;
00116         }
00117         if (encode) {
00118             rawData = Utils::Encodings::Cobs::encode (rawData);
00119             flags |= Ggep::Encoding;
00120         }
00121         Ggeps::Unknown ggep ("un", QByteArray(), flags);
00122         CPPUNIT_ASSERT (ggep.parse (rawData) == true);
00123         CPPUNIT_ASSERT (ggep.data() == parsedData);
00124     }
00125 
00126     void testParseNoCompressionNoEncoding()
00127     {
00128         scenarioParse ("fortest", false, false);
00129     }
00130 
00131     void testParseCompressionNoEncoding()
00132     {
00133         scenarioParse ("fortest", true, false);
00134     }
00135 
00136     void testParseNoCompressionEncoding()
00137     {
00138         scenarioParse ("fortest", false, true);
00139     }
00140 
00141     void testParseCompressionEncoding()
00142     {
00143         scenarioParse ("fortest", true, true);
00144     }
00145 
00146     void testParseCompressionEncodingNullData()
00147     {
00148         scenarioParse ("", true, true);
00149     }
00150 
00151 
00153 
00158     void scenarioRawData (const QByteArray& rawData,
00159                           bool compress, bool encode)
00160     {
00161         QByteArray encodeData = rawData;
00162         int flags = 0;
00163         if (compress) {
00164             encodeData = Utils::Encodings::Deflate::compress (encodeData);
00165             flags |= Ggep::Compression;
00166         }
00167         if (encode) {
00168             encodeData = Utils::Encodings::Cobs::encode (encodeData);
00169             flags |= Ggep::Encoding;
00170         }
00171         Ggeps::Unknown ggep ("un", rawData, flags);
00172         CPPUNIT_ASSERT (ggep.rawData() == encodeData);
00173     }
00174 
00175     void testRawDataCompressionNoEncoding()
00176     {
00177         scenarioRawData ("fortest", true, false);
00178     }
00179 
00180     void testRawDataNoCompressionNoEncoding()
00181     {
00182         scenarioRawData ("fortest", false, false);
00183     }
00184 
00185     void testRawDataCompressionEncoding()
00186     {
00187         scenarioRawData ("fortest", true, true);
00188     }
00189 
00190     void testRawDataNoCompressionEncoding()
00191     {
00192         scenarioRawData ("fortest", false, true);
00193     }
00194 
00195     void testRawDataCompressionEncodingNullData()
00196     {
00197         scenarioRawData ("", true, true);
00198     }
00199 
00200     void testFromId()
00201     {
00202         // the data test maybe placed in the rawData() test.
00203         // \todo for each Id test it.
00204         Ggep* ggep = Ggep::fromId("un", 0x00, "rawdata");
00205         CPPUNIT_ASSERT ( ggep->id() == "un" );
00206         delete ggep;
00207     }
00208     void scenarioOperatorEqual(const QByteArray &lid, const QByteArray &lrawData, \
00209         int lflags, const QByteArray &rid, const QByteArray &rrawData, int rflags)
00210     {
00211         Ggeps::Unknown lggep (lid, lrawData, lflags);
00212         Ggeps::Unknown rggep (rid, rrawData, rflags);
00213         bool result;
00214         result = lid == rid && lrawData == rrawData && lflags == rflags;
00215         CPPUNIT_ASSERT ( (lggep == rggep) == result );
00216     }
00217     void testOperatorEqualIdDifferent()
00218     {
00219         scenarioOperatorEqual ( "un",   "fortest",      0x80,\
00220                         "hehe",     "fortest",      0x80);
00221     }
00222     void testOperatorEqualDataDifferent()
00223     {
00224         scenarioOperatorEqual ( "un",   "fortest",      0x80,\
00225                         "un",   "fortest2",     0x80);
00226     }
00227     void testOperatorEqualFlagDifferent()
00228     {
00229         scenarioOperatorEqual ( "un",   "fortest",      0x80,\
00230                         "un",   "fortest",      0x40);
00231     }
00232     void testOperatorEqualRealEqual()
00233     {
00234         scenarioOperatorEqual ( "un",   "fortest",      0x80,\
00235                         "un",   "fortest",      0x80);
00236     }
00237 };
00238 
00239 CPPUNIT_TEST_SUITE_REGISTRATION(GgepTest);
00240 
00241 } // namespace Testing
00242 } // namespace Extensions
00243 } // namespace Packets
00244 } // namespace Gnutella
00245 } // namespace Protocols