summaryrefslogtreecommitdiff
path: root/test/tcpnsockstream_test.cpp
blob: 028832f16f967961af37b4b930f6dd2e3a16e227 (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
/*
*  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 "tcpnsockstream_test.h"
#include "necho.h"

#include <psocksxx/tcpnsockstream.h>
#include <cstdlib>
#include <sys/wait.h>


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

// use namespace psocksxx
using namespace psocksxx;


void tcpnsockstream_test::connect_naddr( nsockaddr * naddr ) throw() {

	int csock;

	// not worth having error handling here so we hope for the best
	csock = socket( AF_INET, SOCK_STREAM, 0 );
	connect( csock, naddr->psockaddr(), naddr->size() );

}


void tcpnsockstream_test::test_constructors() {

	// default constructor
	CPPUNIT_ASSERT_NO_THROW( tcpnsockstream ss );

}


void tcpnsockstream_test::test_connect_addr() {

	// tcp socket stream
	tcpnsockstream ss;

	// network echo server
	necho n( NSOCK_NODE, NSOCK_SERVICE );

	// network address to connect to
	nsockaddr saddr( NSOCK_NODE, NSOCK_SERVICE );

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

}


void tcpnsockstream_test::test_connect_host_port() {

	// tcp socket stream
	tcpnsockstream ss;

	// network echo server
	necho n( NSOCK_NODE, NSOCK_SERVICE_2 );

	// connect
	CPPUNIT_ASSERT_NO_THROW( ss.connect( NSOCK_NODE, atoi( NSOCK_SERVICE_2 ) ) );

}


void tcpnsockstream_test::test_bind_addr() {

	// tcp socket stream
	tcpnsockstream ss;

	// network address to bind to
	nsockaddr naddr( NSOCK_NODE, NSOCK_BIND_SERVICE );

	// bind
	CPPUNIT_ASSERT_NO_THROW( ss.bind( &naddr, true ) );

}


void tcpnsockstream_test::test_listen_addr() {

	// tcp socket stream
	tcpnsockstream ss;

	// network address to bind to
	nsockaddr naddr( NSOCK_NODE, NSOCK_BIND_SERVICE );

	// bind
	try {
		ss.bind( &naddr, true );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// listen
	CPPUNIT_ASSERT_NO_THROW( ss.listen( 1 ) );

}


void tcpnsockstream_test::test_accept_addr() {

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

	// tcp socket stream
	tcpnsockstream ss;

	// network socket pointer
	nsockstream * nsock = 0;

	// network address to bind to
	nsockaddr naddr( NSOCK_NODE, NSOCK_BIND_SERVICE );

	// bind
	try {
		ss.bind( &naddr, true );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}

	// listen
	try {
		ss.listen( 1 );
	} catch( sockexception &e ) {
		CPPUNIT_FAIL( e.what() );
		return;
	}


	// fork
	cpid = fork();

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

		// child - connect to the network socket created by the parent
		connect_naddr( &naddr );

		// exit
		exit( 0 );

	} else {

		// parent - accept a connection from the child
		CPPUNIT_ASSERT_NO_THROW( nsock = ss.accept() );
		CPPUNIT_ASSERT( nsock != 0 );

		// cleanup
		if ( nsock != 0 ) {
			delete nsock;
		}

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

	}

}


void tcpnsockstream_test::test_bind_addr_fail() {

	// tcp socket stream
	tcpnsockstream ss;

	// network address to bind to
	nsockaddr naddr( NSOCK_NODE, NSOCK_BIND_SERVICE );

	// bind - this should fail with address already in use error
	// because of test_accept_addr()
	CPPUNIT_ASSERT_THROW( ss.bind( &naddr ), sockexception );

}


void tcpnsockstream_test::test_io_addr() {

	// tcp socket stream
	tcpnsockstream ss;

	// network echo server
	necho n( NSOCK_NODE, NSOCK_SERVICE );

	// network address to connect to
	nsockaddr saddr( NSOCK_NODE, NSOCK_SERVICE );

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

	// write
	ss << 'c' << std::endl;

	// read
	char c;
	ss >> c;

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

}