SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
socketUnitTests.cpp
Go to the documentation of this file.
1 #ifdef UNITTESTS
2 
3 #include "MiniCppUnit.hxx"
4 #include "storage.h"
5 #include "socket.h"
6 #include <math.h>
7 #include <iostream>
8 #include <fstream>
9 #include <cstdlib>
10 #include <string>
11 #include <limits.h>
12 #include <float.h>
13 
14 #define ADDRINUSE
15 #define PORT 8888
16 
17 #define TEST_CHAR UCHAR_MAX
18 #define TEST_BYTE UCAHR_MAX
19 #define TEST_STRING "Test-String(ÄÖÜäöü?ß&%$§!+#~*')"
20 #define TEST_SHORT SHRT_MAX
21 #define TEST_INT INT_MAX
22 #define TEST_FLOAT FLT_MAX
23 #define TEST_DOUBLE DBL_MAX
24 
25 
26 class socketUnitTests : public TestFixture<socketUnitTests>
27 {
28 public:
29  TEST_FIXTURE( socketUnitTests )
30  , currentMode_(undefined),
31  server_socket_( PORT ) ,
32  client_socket_( "localhost" , PORT )
33 
34  {
35  TEST_CASE( openConnection );
36 
37  TEST_CASE( receiveStorage );
38  TEST_CASE( sendStorage );
39 
40  TEST_CASE( closeConnection );
41  }
42 
43  void openConnection()
44  {
45  try {
46  server_socket_.set_blocking(false);
47  server_socket_.accept();
48  currentMode_ = server;
49  }
50  catch(tcpip::SocketException e) {
51  if (e.what() == "tcpip::Socket::accept() Unable to create listening socket: Address already in use"){
52  currentMode_ = client;
53  }else{
54  FAIL( "Server socket accept() failed!" );
55  }
56  }
57 
58  if (currentMode_ == server)
59  {
60  server_socket_.set_blocking(true);
61  std::cout << Assert::yellow() << "Listening on port " << PORT << ". Please start another instance of this program on host.\n" << Assert::normal();
62  server_socket_.accept();
63  }
64  if (currentMode_ == client){
65  try {
66  client_socket_.connect();
67  }
68  catch (tcpip::SocketException e){
69  FAIL( "Client socket.connect() failed!" );
70  }
71  }
72  }
73 
74  void closeConnection()
75  {
76  try {
77  if (currentMode_ == client) client_socket_.close();
78  }
79  catch (tcpip::SocketException e){
80  FAIL( "Client socket close() failed!" );
81  }
82 
83  try {
84  if (currentMode_ == server) server_socket_.close();
85  }
86  catch (tcpip::SocketException e){
87  FAIL( "Server socket close() failed!" );
88  }
89  }
90 
91  void receiveStorage()
92  {
93  if (currentMode_ == client) return;
94 
95  tcpip::Storage received_message;
96 
97  try {
98  server_socket_.receiveExact(received_message);
99 
100  ASSERT_EQUALS( static_cast<int>(TEST_CHAR),
101  static_cast<int>(received_message.readChar()) );
102  ASSERT_EQUALS( TEST_STRING, received_message.readString() );
103  ASSERT_EQUALS( TEST_SHORT, received_message.readShort() );
104  ASSERT_EQUALS( TEST_INT, received_message.readInt() );
105  ASSERT_EQUALS( TEST_FLOAT, received_message.readFloat() );
106  ASSERT_EQUALS( TEST_DOUBLE, received_message.readDouble() );
107  }
108  catch (tcpip::SocketException e) {
109  FAIL( "Server socket receiveExact() failed!" );
110  }
111  }
112 
113  void sendStorage()
114  {
115  if (currentMode_ == server) return;
116 
117  tcpip::Storage send_message;
118 
119  try {
120  send_message.writeChar( TEST_CHAR );
121  send_message.writeString( TEST_STRING );
122  send_message.writeShort( TEST_SHORT );
123  send_message.writeInt( TEST_INT );
124  send_message.writeFloat( TEST_FLOAT );
125  send_message.writeDouble( TEST_DOUBLE );
126 
127  client_socket_.sendExact(send_message);
128  }
129  catch (tcpip::SocketException e) {
130  FAIL( "Client socket sendExact() failed!" );
131  }
132  }
133 
134  //...
135 private:
136  enum mode{undefined = -1, client, server};
137  mode currentMode_;
138  tcpip::Socket server_socket_;
139  tcpip::Socket client_socket_;
140 };
141 
142 //Register fixture
143 REGISTER_FIXTURE( socketUnitTests );
144 
145 #endif