PacketFactoryTest.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 "../PacketFactory.h"
00025 #include "../BadPacket.h"
00026 #include "PacketStub.h"
00027 #include "cppunit/extensions/HelperMacros.h"
00028 
00029 namespace Protocols {
00030 namespace Generics {
00031 namespace Testing {
00032 
00033 class PacketFactoryStub: public PacketFactory
00034 {
00035 public:
00036     void setDoExtractPacketReturns (bool returnValue, int headerLength,
00037                                     int payloadLength, int trailerLength)
00038     {
00039         doExtractReturnValue    = returnValue;
00040         doExtractHeaderLength   = headerLength;
00041         doExtractPayloadLength  = payloadLength;
00042         doExtractTrailerLength  = trailerLength;
00043     }
00044 
00045     bool doExtractPacket (const QByteArray &,
00046                           int &headerLength,
00047                           int &payloadLength,
00048                           int &trailerLength)
00049     {
00050         if (doExtractReturnValue) {
00051             headerLength = doExtractHeaderLength;
00052             payloadLength = doExtractPayloadLength;
00053             trailerLength = doExtractTrailerLength;
00054         }
00055         return doExtractReturnValue;
00056     }
00057 
00058     void setDoCreatePacketReturns (Packet packet)
00059     {
00060         doCreatePacketPacket = packet;
00061     }
00062 
00063     Packet doCreatePacket (const QByteArray &,
00064                            const QByteArray &,
00065                            const QByteArray &)
00066     {
00067         return doCreatePacketPacket;
00068     }
00069 
00070 private:
00071     bool                    doExtractReturnValue;
00072     int                     doExtractHeaderLength;
00073     int                     doExtractPayloadLength;
00074     int                     doExtractTrailerLength;
00075     Packet                  doCreatePacketPacket;
00076 };
00077 
00078 class PacketFactoryTest : public CppUnit::TestFixture
00079 {
00080     CPPUNIT_TEST_SUITE (PacketFactoryTest);
00081     CPPUNIT_TEST (testExtractPacket);
00082     CPPUNIT_TEST (testCreatePacketOK);
00083     CPPUNIT_TEST (testCreatePacketBad);
00084     CPPUNIT_TEST_SUITE_END();
00085 
00086 public:
00087     void setUp()
00088     {
00089     }
00090 
00091     void tearDown()
00092     {
00093     }
00094 
00095     void testExtractPacket()
00096     {
00097         {
00098             PacketFactoryStub factory;
00099             factory.setDoExtractPacketReturns (true, 2, 0, 0);
00100             QByteArray rawBytes (2, 0);
00101             int headerLength = 0, payloadLength = 0, trailerLength = 0;
00102             bool extracted = factory.extractPacket (rawBytes, headerLength,
00103                                                 payloadLength, trailerLength);
00104             CPPUNIT_ASSERT (true == extracted);
00105         }
00106         {
00107             PacketFactoryStub factory;
00108             factory.setDoExtractPacketReturns (false, 0, 0, 0);
00109             QByteArray rawBytes (0, '\0');
00110             int headerLength = 0, payloadLength = 0, trailerLength = 0;
00111             bool extracted = factory.extractPacket (rawBytes, headerLength,
00112                                                 payloadLength, trailerLength);
00113             CPPUNIT_ASSERT (false == extracted);
00114         }
00115     }
00116 
00117     void testCreatePacketOK()
00118     {
00119         PacketFactoryStub factory;
00120         QByteArray rawHeader (2, 1);
00121         QByteArray rawPayload;
00122         QByteArray rawTrailer;
00123         Packet packet = PacketStub();
00124         factory.setDoCreatePacketReturns (packet); // i.e. return the stub
00125         packet = factory.createPacket (rawHeader, rawPayload, rawTrailer);
00126         CPPUNIT_ASSERT (typeid (*packet) == typeid (PacketStub));
00127     }
00128 
00129     void testCreatePacketBad()
00130     {
00131         {
00132             PacketFactoryStub factory;
00133             QByteArray rawHeader (2, 1);
00134             QByteArray rawPayload (1, 0);
00135             QByteArray rawTrailer;
00136             // Create a packet for which parse() will return false
00137             // (rawPayload will NOT match the value expected by the stub):
00138             Packet packet;
00139             packet = PacketStub (rawHeader, rawTrailer, true);
00140             factory.setDoCreatePacketReturns (packet); // i.e. return the stub
00141             packet = factory.createPacket (rawHeader, rawPayload, rawTrailer);
00142             CPPUNIT_ASSERT (typeid (*packet) == typeid (BadPacket));
00143         }
00144         {
00145             PacketFactoryStub factory;
00146             QByteArray rawHeader (2, 0);
00147             QByteArray rawPayload;
00148             QByteArray rawTrailer;
00149             Packet packet;
00150             factory.setDoCreatePacketReturns (packet); // Packet() is BadPacket.
00151             packet = factory.createPacket (rawHeader, rawPayload, rawTrailer);
00152             CPPUNIT_ASSERT (typeid (*packet) == typeid (BadPacket));
00153         }
00154     }
00155 };
00156 
00157 CPPUNIT_TEST_SUITE_REGISTRATION (PacketFactoryTest);
00158 
00159 } // namespace Testing
00160 } // namespace Generics
00161 } // namespace Protocols