File: connector2_test.cpp

package info (click to toggle)
libassa 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,084 kB
  • ctags: 2,323
  • sloc: cpp: 15,641; sh: 8,704; makefile: 372; perl: 51
file content (352 lines) | stat: -rw-r--r-- 10,472 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: connector2_test.cpp,v 1.10 2006/07/20 02:30:55 vlg Exp $
//------------------------------------------------------------------------------
//                            ConnectCancelTest.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2002,2005 by Vladislav Grinchenko 
//
//  This program is free software; you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License   
//  as published by the Free Software Foundation; either version  
//  2 of the License, or (at your option) any later version.      
//------------------------------------------------------------------------------
//
// Date   : Wed Dec 11 14:06:16 2002
//
//------------------------------------------------------------------------------

static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"                                                                            \n"
"   connector2_test                                                          \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"   This is the test for Bug # 651712 (Connector sync mode bug):             \n"
"                                                                            \n"
"   When Connector connects successfully with remote service and             \n"
"   ServiceHandler returns -1 from its open() call, the return value         \n"
"   is ignored.                                                              \n"
"   Instead, Connector::connect() should return -1 to the application-level  \n"
"   calling code.                                                            \n"
"                                                                            \n"
"   First, the test spans off 'echos' server and then connects to it twice.  \n"
"   First time 'echos' ServiceHandler returns -1 from its open().            \n"
"   The return value is tested to see if indeed -1 is returned to the        \n"
"   application code. Second time around, the connection establishment       \n"
"   is successful and we write a greeting message to 'echos' server,         \n"
"   read and validate the reply and exit.                                    \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  connector2_test [OPTIONS]                                        \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \n"
" -p, --port NAME         - Port number for echos to listen to requests.     \n"
" -D, --log-file NAME     - Write debug to NAME file                         \n"
" -d, --log-stdout        - Write debug to standard output                   \n"
" -z, --log-size NUM      - Maximum size debug file can reach (dfl: is 10Mb) \n"
" -m, --mask MASK         - Mask (default: ALL = 0x7fffffff)                 \n"
" -h, --help              - Print this messag                                \n"
" -v, --version           - Print version number                            \n";
//------------------------------------------------------------------------------

#if !defined (WIN32)

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

#include <iostream>
#include <string>
using std::string;

#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/CommonUtils.h>
#include <assa/ServiceHandler.h>
#include <assa/INETAddress.h>
#include <assa/IPv4Socket.h>
#include <assa/Connector.h>
#include <assa/Fork.h>

using namespace ASSA;

//------------------------------------------------------------------------------
//                            Echo
//------------------------------------------------------------------------------
class Echo : public ServiceHandler<IPv4Socket>
{
public:
	Echo () : m_msg ("Privet, Vlad") { trace("Echo::Echo");	}

	virtual int open ();
	virtual int handle_read (int fd_);
	virtual int handle_close (int fd_);

private:
	static u_int m_invocation;
	std::string m_msg;
};

u_int Echo::m_invocation = 0;

//------------------------------------------------------------------------------
//                         ConnectCancelTest
//------------------------------------------------------------------------------
class ConnectCancelTest :
    public ASSA::GenServer,
    public ASSA::Singleton<ConnectCancelTest>
{
public:
    ConnectCancelTest ();
    ~ConnectCancelTest ();

    virtual void init_service ();
    virtual void process_events ();

	std::string get_build_dir () const { return m_build_dir; }
	void test_failed () { set_exit_value (1); }

private:
	std::string  m_build_dir;
	Echo*        m_echo;
	INETAddress* m_address;
	pid_t        m_echo_pid;

	Connector<Echo, IPv4Socket> m_connector;
};

/* Useful definitions */

#define CONNECTCANCELTEST  ConnectCancelTest::get_instance()
#define REACTOR CONNECTCANCELTEST->get_reactor()

// Static declarations mandated by Singleton class

ASSA_DECL_SINGLETON(ConnectCancelTest);

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

int
Echo::
open () 
{
	trace("Echo::open");

	if (m_invocation++ == 0) {
		DL((APP,"First attempt = return -1\n"));
		return -1;
	}
	DL((APP,"Second attempt = return 0\n"));
	
	IPv4Socket& s = *this;
	Assure_exit (s.write (m_msg.c_str (), m_msg.size ()) == m_msg.size ());
	s << flush;
	REACTOR->registerIOHandler (this, s.getHandler (), READ_EVENT);
	return 0;
}

int
Echo::
handle_close (int)
{
	trace("Echo::close");
	CONNECTCANCELTEST->stop_service ();
}

int
Echo::
handle_read (int fd_) 
{
	trace("Echo::handle_read");

	IPv4Socket& s = *this;
	char* buf = new char [m_msg.size () +1];
	u_int expected = m_msg.size ();
	char* nextbyte = buf;
	int ret = 0;
	while ((ret = s.read (nextbyte, expected)) > 0) {
		nextbyte += ret;
		expected -= ret;
		if (expected == 0) {
			*nextbyte = '\0';
			break;
		}
	}
	if (buf == m_msg) {
		std::cout << "Test 2 passed" << std::endl;
		DL((APP,"Test 2 passed\n"));
		delete [] buf;
		return -1;
	}
	else {
		std::cout << "Test 2 failed" << std::endl;

		DL ((APP,"Test 2 failed\n"));
		DL ((APP,"Expected = \"%s\"\n", m_msg.c_str ()));
		DL ((APP,"Received = \"%s\"\n", buf));

		CONNECTCANCELTEST->test_failed ();
	}
	return -1;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

ConnectCancelTest::
ConnectCancelTest () : 
	m_address (NULL),
	m_echo_pid (0)
{
    // ---Configuration---
    rm_opt ('f', "config-file"  );
    rm_opt ('n', "instance"     );

    // ---Process bookkeeping---
    rm_opt ('b', "daemon"       );
    rm_opt ('l', "pidfile"      );
    rm_opt ('L', "ommit-pidfile");

    /*---
     * By defauil disable all debugging
     *---*/
    m_mask = ASSA::APP;
    m_log_file = "connector2_test.log";

	add_opt (0, "build-dir", &m_build_dir);
}

ConnectCancelTest::
~ConnectCancelTest ()
{
	trace("ConnectCancelTest::~ConnectCancelTest");

	if (m_echo) {
		delete m_echo;
		m_echo = NULL;
	}
	if (m_address) {
		delete m_address;
		m_address = NULL;
	}
}


void
ConnectCancelTest::
init_service ()
{
    trace("ConnectCancelTest::init_service");

	if (m_build_dir.length () == 0) {
        m_build_dir = ASSA::Utils::get_cwd_name ();
    }
    DL((APP,"build-dir = \"%s\"\n", m_build_dir.c_str ()));

	m_address = new INETAddress (get_port ().c_str ());
	if (m_address->bad ()) {
		std::cerr << "Missing --port=NUM option\n";
		exit (1);
	}
	
	std::string exec_name (get_build_dir () + "/echos");
	Fork ss (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS);

	if (ss.isChild ()) {
		::execlp (exec_name.c_str (), exec_name.c_str (),
				  "--port", get_port ().c_str (),
				  "--mask=0x7fffffff",
				  NULL);
		std::cerr << "Failed to execlp(" << exec_name << std::endl;
		exit (1);
	}
	else if (ss.isParent ()) {
		m_echo_pid = ss.getChildPID ();
		ASSA::Utils::sleep_for_seconds (2);
	}
    DL((ASSA::APP,"Service has been initialized\n"));
}

void
ConnectCancelTest::
process_events ()
{
    trace("ConnectCancelTest::process_events");

	m_echo = new Echo;
	TimeVal timeout (5.0);
	
	if (m_connector.open (timeout) < 0) {
		std::cout << "open (" << get_port () << "@" 
				  << m_address->getHostName ()
				  << " failed" << std::endl;
		set_exit_value (1);
		return;
	}

	/** Test 1: connection ok, but Echo rejects it.
	 */
	if (m_connector.connect (m_echo, *m_address) != -1) {
		std::cout << "Test 1 failed" << std::endl;
		DL((APP,"Test 1 failed\n"));
		set_exit_value (1);
		return;
	}
	std::cout << "Test 1 passed" << std::endl;
	DL((APP,"Test 1 passed\n"));
	/** Test 2: connection ok, and Echo accepts it.
	 */
	if (m_connector.connect (m_echo, *m_address) < 0) {
		std::cerr << "Test 2 failed" << std::endl;
		DL((APP,"Test 2 failed\n"));
		set_exit_value (1);
		return;
	}
	
    while (service_is_active ()) {
        m_reactor.waitForEvents ();
    }

    // Shut the service down
    m_reactor.stopReactor ();
    DL((ASSA::APP,"Service stopped!\n"));
}

#endif /* !defined WIN32 */

int
main (int argc, char* argv[])
{
#if !defined (WIN32)

    static const char release[] = "VERSION";
    int patch_level = 0;

	std::cout << "= Running connector2_test Test =\n\n";

    CONNECTCANCELTEST->set_version (release, patch_level);
    CONNECTCANCELTEST->set_author  ("Vladislav Grinchenko");
    CONNECTCANCELTEST->set_flags   (ASSA::GenServer::RMLOG);

    CONNECTCANCELTEST->init (&argc, argv, help_msg);
 
    CONNECTCANCELTEST->init_service ();
    CONNECTCANCELTEST->process_events ();

    return CONNECTCANCELTEST->get_exit_value ();

#endif /* !defined WIN32 */

	return 0;
}