PacketSessionTest.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 "../PacketSession.h"
00025 #include "generated/PacketSessionDriver.h"
00026 #include "Imports.cpp"
00027 
00028 namespace Protocols {
00029 namespace BitTorrent {
00030 namespace Transfers {
00031 namespace Testing {
00032 
00034 
00062 class PacketSessionTest : public CppUnit::TestFixture
00063 {
00064     // \todo Maybe derive from a common base that is a REFERENCE_OBJECT?
00065     REFERENCE_OBJECT (PacketSessionTest)
00066 
00067     CPPUNIT_TEST_SUITE (PacketSessionTest);
00068     CPPUNIT_TEST (testReadNothing);
00069     CPPUNIT_TEST (testReadOnePacket);
00070     CPPUNIT_TEST (testReadTwoPackets);
00071     CPPUNIT_TEST (testReadOnePacketAndHalfPacketStillBuffered);
00072     CPPUNIT_TEST (testSendPacketWritesPacketDirectly);
00073     CPPUNIT_TEST (testSendTwoPacketsBuffersThem);
00074     CPPUNIT_TEST (testSendTwoPacketsBuffersThemThenWritesThem);
00075     CPPUNIT_TEST_SUITE_END();
00076 
00077     TransportStub                   *transport;
00078     auto_ptr <SessionStatusMock>    status;
00079     auto_ptr <PacketSession>        sessionReal;
00080     auto_ptr <PacketSessionDriver>  session;
00081 
00082     const Packet                    packet1;
00083     const Packet                    packet2;
00084     const QByteArray                halfPacketBytes;
00085 
00086 public:
00088     PacketSessionTest()
00089     :   transport (0), status(), sessionReal(), session(),
00090         packet1 (Keepalive()), packet2 (Choke()), halfPacketBytes ("12")
00091     {
00092     }
00093 
00094     void setUp()
00095     {
00096         transport = new TransportStub();
00097         auto_ptr <Transport> temp (transport);
00098         status.reset (new SessionStatusMock);
00099         sessionReal.reset (new PacketSession (temp, *status));
00100         session.reset (new PacketSessionDriver (*sessionReal));
00101     }
00102 
00103     void tearDown()
00104     {
00105         session.reset();
00106         sessionReal.reset();
00107         status.reset();
00108         transport = 0; // No need for delete, session owns the object.
00109     }
00110 
00112     void stateOpenedSession()
00113     {
00114         call (session->open())
00115         .returns();
00116     }
00117 
00119     void testReadNothing()
00120     {
00121         stateOpenedSession();
00122 
00123         call (session->transportReadyRead (transport))
00124         .returns();
00125     }
00126 
00128     void testReadOnePacket()
00129     {
00130         stateOpenedSession();
00131 
00132         Packet packet = Keepalive();
00133         transport->readBuffer = packet->rawPacket();
00134 
00135         call (session->transportReadyRead (transport))
00136             .willCall (status->sessionReceivedData (*session, packet))
00137             .returns()
00138         .returns();
00139 
00140         willBeEqual (transport->readBuffer, QByteArray());
00141     }
00142 
00144     void testReadTwoPackets()
00145     {
00146         stateOpenedSession();
00147 
00148         transport->readBuffer = packet1->rawPacket() + packet2->rawPacket();
00149 
00150         call (session->transportReadyRead (transport))
00151             .willCall (status->sessionReceivedData (*session, packet1))
00152             .returns()
00153             .willCall (status->sessionReceivedData (*session, packet2))
00154             .returns()
00155         .returns();
00156 
00157         willBeEqual (transport->readBuffer, QByteArray());
00158     }
00159 
00161     void testReadOnePacketAndHalfPacketStillBuffered()
00162     {
00163         stateOpenedSession();
00164 
00165         transport->readBuffer = packet1->rawPacket() + halfPacketBytes;
00166 
00167         call (session->transportReadyRead (transport))
00168             .willCall (status->sessionReceivedData (*session, packet1))
00169             .returns()
00170         .returns();
00171 
00172         willBeEqual (transport->readBuffer, halfPacketBytes);
00173     }
00174 
00176     void testSendPacketWritesPacketDirectly()
00177     {
00178         stateOpenedSession();
00179 
00180         transport->writeBuffer.reserve (packet1->rawPacket().size());
00181 
00182         call (session->send (packet1))
00183             .willCall (status->sessionSendingData (*session, packet1))
00184             .returns()
00185         .returns();
00186 
00187         willBeEqual (transport->writeBuffer, packet1->rawPacket());
00188     }
00189 
00191     void testSendTwoPacketsBuffersThem()
00192     {
00193         stateOpenedSession();
00194 
00195         transport->writeBuffer.reserve (0); // Disables writing to Transport.
00196 
00197         call (session->send (packet1))
00198             .willCall (status->sessionSendingData (*session, packet1))
00199             .returns()
00200         .returns();
00201         call (session->send (packet2))
00202             .willCall (status->sessionSendingData (*session, packet2))
00203             .returns()
00204         .returns();
00205 
00206         willBeEqual (transport->writeBuffer, QByteArray());
00207     }
00208 
00210     void testSendTwoPacketsBuffersThemThenWritesThem()
00211     {
00212         testSendTwoPacketsBuffersThem();
00213 
00214         // Enable writing to Transport:
00215         transport->writeBuffer.reserve (packet1->rawPacket().size()
00216                                         + packet2->rawPacket().size());
00217 
00218         call (session->transportReadyWrite (transport))
00219         .returns();
00220 
00221         willBeEqual (transport->writeBuffer,
00222                      packet1->rawPacket() + packet2->rawPacket());
00223     }
00224 };
00225 
00226 CPPUNIT_TEST_SUITE_REGISTRATION(PacketSessionTest);
00227 
00228 } // namespace Testing
00229 } // namespace Transfers
00230 } // namespace BitTorrent
00231 } // namespace Protocols