socketUnitTests.cpp

Go to the documentation of this file.
00001 #ifdef UNITTESTS
00002 
00003 #include "MiniCppUnit.hxx"
00004 #include "storage.h"
00005 #include "socket.h"
00006 #include <math.h>
00007 #include <iostream>
00008 #include <fstream>
00009 #include <cstdlib>
00010 #include <string>
00011 #include <limits.h>
00012 #include <float.h>
00013 
00014 #define ADDRINUSE 
00015 #define PORT 8888
00016 
00017 #define TEST_CHAR UCHAR_MAX
00018 #define TEST_BYTE UCAHR_MAX
00019 #define TEST_STRING "Test-String(ÄÖÜäöü?ß&%$§!+#~*')"
00020 #define TEST_SHORT SHRT_MAX
00021 #define TEST_INT INT_MAX
00022 #define TEST_FLOAT FLT_MAX
00023 #define TEST_DOUBLE DBL_MAX
00024 
00025 
00026 class socketUnitTests : public TestFixture<socketUnitTests>
00027 {
00028 public:
00029     TEST_FIXTURE( socketUnitTests )
00030    , currentMode_(undefined),
00031    server_socket_( PORT ) ,
00032      client_socket_( "localhost" , PORT )
00033                                       
00034     {
00035         TEST_CASE( openConnection );
00036 
00037         TEST_CASE( receiveStorage );
00038         TEST_CASE( sendStorage );
00039 
00040         TEST_CASE( closeConnection );
00041     }
00042 
00043     void openConnection()
00044     {
00045         try {
00046       server_socket_.set_blocking(false);
00047             server_socket_.accept();
00048             currentMode_ = server;
00049         }
00050         catch(tcpip::SocketException e) {
00051             if (e.what() == "tcpip::Socket::accept() Unable to create listening socket: Address already in use"){
00052                 currentMode_ = client;
00053             }else{
00054                 FAIL( "Server socket accept() failed!" );
00055             }
00056         }
00057         
00058         if (currentMode_ == server)
00059         {
00060       server_socket_.set_blocking(true);
00061       std::cout << Assert::yellow() << "Listening on port " << PORT << ". Please start another instance of this program on host.\n" << Assert::normal();
00062       server_socket_.accept();
00063         }
00064         if (currentMode_ == client){
00065             try {
00066                 client_socket_.connect();
00067             } 
00068             catch (tcpip::SocketException e){
00069                 FAIL( "Client socket.connect() failed!" );
00070             }
00071         }
00072     }
00073 
00074     void closeConnection()
00075     {
00076         try {
00077             if (currentMode_ == client) client_socket_.close();
00078         }
00079         catch (tcpip::SocketException e){
00080                 FAIL( "Client socket close() failed!" );
00081         }
00082 
00083         try {
00084             if (currentMode_ == server) server_socket_.close();
00085         }
00086         catch (tcpip::SocketException e){
00087                 FAIL( "Server socket close() failed!" );
00088         }
00089     }
00090 
00091     void receiveStorage()
00092     {
00093         if (currentMode_ == client) return;
00094 
00095         tcpip::Storage received_message;
00096 
00097         try {
00098             server_socket_.receiveExact(received_message);
00099 
00100             ASSERT_EQUALS( static_cast<int>(TEST_CHAR), 
00101                            static_cast<int>(received_message.readChar()) );
00102             ASSERT_EQUALS( TEST_STRING, received_message.readString() );
00103             ASSERT_EQUALS( TEST_SHORT, received_message.readShort() );
00104             ASSERT_EQUALS( TEST_INT, received_message.readInt() );
00105             ASSERT_EQUALS( TEST_FLOAT, received_message.readFloat() );
00106             ASSERT_EQUALS( TEST_DOUBLE, received_message.readDouble() );
00107         }
00108         catch (tcpip::SocketException e) {
00109             FAIL( "Server socket receiveExact() failed!" );
00110         }
00111     }
00112 
00113     void sendStorage()
00114     {
00115         if (currentMode_ == server) return;
00116 
00117         tcpip::Storage send_message;
00118         
00119         try {
00120             send_message.writeChar( TEST_CHAR );
00121             send_message.writeString( TEST_STRING );
00122             send_message.writeShort( TEST_SHORT );
00123             send_message.writeInt( TEST_INT );
00124             send_message.writeFloat( TEST_FLOAT );
00125             send_message.writeDouble( TEST_DOUBLE );
00126 
00127             client_socket_.sendExact(send_message);
00128         }
00129         catch (tcpip::SocketException e) {
00130             FAIL( "Client socket sendExact() failed!" );
00131         }
00132     }
00133 
00134     //...
00135 private:
00136     enum mode{undefined = -1, client, server};
00137     mode currentMode_;
00138     tcpip::Socket server_socket_;
00139     tcpip::Socket client_socket_;
00140 };
00141 
00142 //Register fixture
00143 REGISTER_FIXTURE( socketUnitTests );
00144 
00145 #endif

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6