1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19
20 #ifdef _MSC_VER
21 #pragma warning( disable : 4503 4355 4786 )
22 #include "stdafx.h"
23 #else
24 #include "config.h"
25 #endif
26
27 #include <UnitTest++.h>
28 #include <TestHelper.h>
29 #include <SocketServer.h>
30 #ifdef _MSC_VER
31 #include <stdlib.h>
32 #endif
33
34 using namespace FIX;
35
36 class socketServerFixture : public SocketServer::Strategy
37 {
38 public:
socketServerFixture()39 socketServerFixture()
40 : connect( 0 ), write( 0 ), data( 0 ), disconnect( 0 ),
41 connectSocket( 0 ), writeSocket( 0 ),
42 dataSocket( 0 ), disconnectSocket( 0 ),
43 bufLen( 0 ) {}
44
onConnect(SocketServer &,int accept,int socket)45 void onConnect( SocketServer&, int accept, int socket )
46 { connect++; connectSocket = socket; }
onWrite(SocketServer &,int socket)47 void onWrite( SocketServer&, int socket )
48 { write++; writeSocket = socket; }
onData(SocketServer & server,int socket)49 bool onData( SocketServer& server, int socket )
50 {
51 data++; dataSocket = socket;
52 bufLen = recv( socket, buf, 1, 0 );
53 return bufLen > 0;
54 }
onDisconnect(SocketServer &,int socket)55 void onDisconnect( SocketServer&, int socket )
56 { disconnect++; disconnectSocket = socket; }
onError(SocketServer &)57 void onError( SocketServer& )
58 {}
59
60 int connect, write, data, disconnect;
61 int connectSocket, writeSocket;
62 int dataSocket, disconnectSocket;
63 char buf[ 1 ]; size_t bufLen;
64 };
65
SUITE(SocketServerTests)66 SUITE(SocketServerTests)
67 {
68
69 TEST_FIXTURE(socketServerFixture, accept)
70 {
71 SocketServer object( 0 );
72 int serverS1 = object.add( TestSettings::port, true, true );
73 int clientS1 = createSocket( TestSettings::port, "127.0.0.1" );
74 CHECK( clientS1 > 0 );
75 process_sleep(0.1);
76 int s1 = object.accept( serverS1 );
77 CHECK( s1 >= 0 );
78 object.block( *this );
79 CHECK( object.numConnections() == 1 );
80
81 int clientS2 = createSocket( TestSettings::port, "127.0.0.1" );
82 CHECK( clientS2 > 0 );
83 process_sleep(0.1);
84 int s2 = object.accept( serverS1 );
85 CHECK( s2 >= 0 );
86 object.block( *this );
87 CHECK( object.numConnections() == 2 );
88
89 int clientS3 = createSocket( TestSettings::port, "127.0.0.1" );
90 CHECK( clientS3 > 0 );
91 process_sleep(0.1);
92 int s3 = object.accept( serverS1 );
93 CHECK( s3 >= 0 );
94 object.block( *this );
95 CHECK( object.numConnections() == 3 );
96
97 SocketMonitor& monitor = object.getMonitor();
98 CHECK( !monitor.drop( -1 ) );
99 CHECK( object.numConnections() == 3 );
100 CHECK( monitor.drop( s1 ) );
101 CHECK( object.numConnections() == 2 );
102 CHECK( monitor.drop( s2 ) );
103 CHECK( object.numConnections() == 1 );
104 CHECK( monitor.drop( s3 ) );
105 CHECK( object.numConnections() == 0 );
106
107 destroySocket( clientS1 );
108 destroySocket( clientS2 );
109 destroySocket( clientS3 );
110 }
111
112 TEST_FIXTURE(socketServerFixture, block)
113 {
114 SocketServer object( 0 );
115 object.add( TestSettings::port, true, true );
116 int clientS = createSocket( TestSettings::port, "127.0.0.1" );
117 CHECK( clientS >= 0 );
118
119 object.block( *this );
120 CHECK_EQUAL( 1, connect );
121 CHECK( connectSocket > 0 );
122
123 send( clientS, "1", 1, 0 );
124 object.block( *this );
125 object.block( *this );
126 CHECK_EQUAL( 1, data );
127 CHECK_EQUAL( 1U, bufLen );
128 CHECK_EQUAL( '1', *buf );
129 CHECK( dataSocket > 0 );
130
131 destroySocket( clientS );
132 object.block( *this );
133 CHECK_EQUAL( 1, disconnect );
134 CHECK( disconnectSocket > 0 );
135 }
136
137 TEST_FIXTURE(socketServerFixture, close)
138 {
139 SocketServer object( 0 );
140 object.add( TestSettings::port, true, true );
141 object.close();
142 object.block( *this );
143 }
144
145 }
146