summaryrefslogtreecommitdiff
path: root/test/sockstreambuf_test.cpp
blob: 7f992f15f462f1361a6f76bbeec62522fa252412 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
*  psocksxx - A C++ wrapper for POSIX sockets
*  Copyright (C) 2013 Uditha Atukorala
*
*  This software library is free software; you can redistribute it and/or modify
*  it under the terms of the GNU Lesser General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  This software library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public License
*  along with this software library. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "sockstreambuf_test.h"
#include "lecho.h"

#include <psocksxx/sockstreambuf.h>
#include <psocksxx/lsockaddr.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdlib>


// register the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( sockstreambuf_test );

// use namespace psocksxx
using namespace psocksxx;


sockstreambuf_test::sockstreambuf_test() { }
sockstreambuf_test::~sockstreambuf_test() { }


void sockstreambuf_test::connect_local() throw() {

	int csock;
	sockaddr_un saddr;

	const char * path = LOCAL_SOCK_PATH;
	bzero( (void *) &saddr, sizeof( saddr ) );
	saddr.sun_family = AF_LOCAL;
	strcpy( saddr.sun_path, path );

	// not worth having error handling here so we hope for the best
	csock = socket( AF_LOCAL, SOCK_STREAM, 0 );
	connect( csock, (::sockaddr *) &saddr, sizeof( sockaddr_un ) );

}


void sockstreambuf_test::setUp() {

	// initialise locals
	_sockaddr.sa_family = AF_LOCAL;

}


void sockstreambuf_test::tearDown() {

}


void sockstreambuf_test::test_constructors() {

	const sockstreambuf ssb;
	const sockstreambuf ssb_s( -1 );

	// check the default constructor
	CPPUNIT_ASSERT( ssb.socket() == ssb_s.socket() );

}


void sockstreambuf_test::test_open_close_local_ip() {

	sockstreambuf ssb;

	CPPUNIT_ASSERT_NO_THROW_MESSAGE( "Failed to open socket communication end-point",
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::ipproto_ip ) );

	// close the opened socket end-point
	ssb.close();

}


void sockstreambuf_test::test_flush_empty() {

	sockstreambuf ssb( -1 );
	CPPUNIT_ASSERT( sockstreambuf::eof == ssb.flush() );

}


void sockstreambuf_test::test_bad_flush() {

	// socket stream buffer
	sockstreambuf ssb( -1 );

	// add a char into the buffer
	ssb.sputc( 'c' );

	// flush buffer
	CPPUNIT_ASSERT( sockstreambuf::eof == ssb.flush() );

}


void sockstreambuf_test::test_bad_connect_failure() {

	// socket stream buffer
	sockstreambuf ssb( -1 );

	// this should throw a bad file descriptor error
	CPPUNIT_ASSERT_THROW( ssb.connect( &_sockaddr ), sockexception );

}


void sockstreambuf_test::test_bad_bind_failure() {

	// socket stream buffer
	sockstreambuf ssb( -1 );

	// this should throw a bad file descriptor error
	CPPUNIT_ASSERT_THROW( ssb.bind( &_sockaddr ), sockexception );

}


void sockstreambuf_test::test_bad_listen_failure() {

	// socket stream buffer
	sockstreambuf ssb( -1 );

	// this should throw a bad file descriptor error
	CPPUNIT_ASSERT_THROW( ssb.listen(), sockexception );

}


void sockstreambuf_test::test_bad_accept_failure() {

	// socket stream buffer
	sockstreambuf ssb( -1 );

	// this should throw a bad file descriptor error
	CPPUNIT_ASSERT_THROW( ssb.accept(), sockexception );

}


void sockstreambuf_test::test_local_bind() {

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_SOCK_PATH;
	lsockaddr saddr( path );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// bind to address
	CPPUNIT_ASSERT_NO_THROW( ssb.bind( &saddr ) );


	// close socket
	ssb.close();

	// unlink
	if ( unlink( path ) !=0 ) {
		CPPUNIT_FAIL( std::string( "failed to unlink socket: " ).append( path ) );
	}

}


void sockstreambuf_test::test_local_listen() {

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_SOCK_PATH;
	lsockaddr saddr( path );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// bind to address
	try {
		ssb.bind( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
	}

	// listen
	CPPUNIT_ASSERT_NO_THROW( ssb.listen() );


	// close socket
	ssb.close();

	// unlink
	if ( unlink( path ) !=0 ) {
		CPPUNIT_FAIL( std::string( "failed to unlink socket: " ).append( path ) );
	}

}


void sockstreambuf_test::test_local_connect() {

	// socket steam buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );

	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	CPPUNIT_ASSERT_NO_THROW( ssb.connect( &saddr ) );

	// close socket
	ssb.close();

}


void sockstreambuf_test::test_local_connect_timeout() {

	// socket steam buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );

	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	CPPUNIT_ASSERT_NO_THROW( ssb.connect( &saddr, 1 ) );

	// close socket
	ssb.close();

}


void sockstreambuf_test::test_local_accept() {

	// fork variables
	pid_t  cpid, wpid;
	int    wpid_status;

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_SOCK_PATH;
	lsockaddr saddr( path );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// bind to address
	try {
		ssb.bind( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
	}

	// listen
	try {
		ssb.listen();
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
	}


	// fork
	cpid = fork();

	if ( cpid == -1 ) {
		CPPUNIT_FAIL( "failed to fork" );
	} else if ( cpid == 0 ) {

		// child - connect to the local socket created by the parent
		connect_local();

		// exit
		exit( 0 );

	} else {

		// parent - accept a connection from the child
		CPPUNIT_ASSERT( ssb.accept() != -1 );

		// wait for child to exit
		if ( ( wpid = waitpid( cpid, &wpid_status, 0 ) ) == -1 ) {
			CPPUNIT_FAIL( "failed waiting for the child process to terminate" );
		}

	}

	// close socket
	ssb.close();

	// unlink
	if ( unlink( path ) !=0 ) {
		CPPUNIT_FAIL( std::string( "failed to unlink socket: " ).append( path ) );
	}

}


void sockstreambuf_test::test_local_flush() {

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	try {
		ssb.connect( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// put a char into the buffer and flush
	ssb.sputc( 'c' );
	CPPUNIT_ASSERT( 1 == ssb.flush() );


	// close socket
	ssb.close();

}


void sockstreambuf_test::test_local_flush_read() {

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	try {
		ssb.connect( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// put a char into the buffer
	ssb.sputc( 'c' );

	// flush
	if ( ssb.flush() == 1 ) {

		// read back
		CPPUNIT_ASSERT( 'c' == ssb.sgetc() );

	} else {
		CPPUNIT_FAIL( "failed to flush buffer" );
	}


	// close socket
	ssb.close();

}


void sockstreambuf_test::test_local_ostream() {

	// socket stream buffer
	sockstreambuf ssb;

	// ostream
	std::ostream output( &ssb );

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	try {
		ssb.connect( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// use the output stream to send a char
	output << 'c' << std::endl;

	// read back using the socket stream buffer
	CPPUNIT_ASSERT( 'c' ==  ssb.sgetc() );


	// close socket
	ssb.close();

}


void sockstreambuf_test::test_local_istream() {

	// socket stream buffer
	sockstreambuf ssb;

	// istream
	std::istream input( &ssb );

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	try {
		ssb.connect( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}


	// put a char into the buffer
	ssb.sputc( 'c' );

	// flush
	if ( ssb.flush() == 1 ) {

		// read back using istream
		char c;
		input >> c;

		// assert
		CPPUNIT_ASSERT( 'c' == c );

	} else {
		CPPUNIT_FAIL( "failed to flush buffer" );
	}


	// close socket
	ssb.close();

}


void sockstreambuf_test::test_set_timeout() {

	// socket stream buffer
	sockstreambuf ssb;

	time_t sec = 1;
	suseconds_t usec = 500;

	// set timeout
	const timeval * t = ssb.timeout( sec, usec );

	// validate
	CPPUNIT_ASSERT( sec == t->tv_sec );
	CPPUNIT_ASSERT( usec == t->tv_usec );

}


void sockstreambuf_test::test_clear_timeout() {

	// socket stream buffer
	sockstreambuf ssb;

	// clear the timeout before a timeout is set
	CPPUNIT_ASSERT( 0 == ssb.clear_timeout() );


	time_t sec = 1;
	suseconds_t usec = 500;

	// set timeout
	const timeval * t = ssb.timeout( sec, usec );

	// clear the timeout after a timeout is set
	CPPUNIT_ASSERT( 0 == ssb.clear_timeout() );

}


void sockstreambuf_test::test_local_read_timeout() {

	// socket stream buffer
	sockstreambuf ssb;

	// local (unix) socket address
	const char * path = LOCAL_LISTENER_SOCK_PATH;
	lsockaddr saddr( path );

	// local echo server
	lecho echo( LOCAL_LISTENER_SOCK_PATH );


	// prepare the socket
	try {
		ssb.open( sockstreambuf::pf_local, sockstreambuf::sock_stream, sockstreambuf::proto_unspec );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// connect
	try {
		ssb.connect( &saddr );
	} catch ( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// set timeout
	ssb.timeout( 0, 200 );

	// read - this should timeout and throw a timeout exception
	CPPUNIT_ASSERT_THROW( ssb.sgetc(), socktimeoutexception );

	// should've set the timed-out flag as well
	CPPUNIT_ASSERT( true == ssb.timedout() );


	// close socket
	ssb.close();

}