1 //==========================================================================
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: nickg
46 // Purpose: Test socket API
47 // Description: This program tests the socket API. Note that it is only
48 // intended to test the API and not the functionality of
49 // the underlying network stack. That is assumed to have
50 // been established by other tests elsewhere.
52 //####DESCRIPTIONEND####
54 //==========================================================================
56 #include <pkgconf/system.h>
57 #include <pkgconf/isoinfra.h>
59 #include <pkgconf/posix.h>
62 #ifndef CYGINT_ISO_PTHREAD_IMPL
63 # define NA_MSG "POSIX threads needed to run test"
64 #elif !defined CYGPKG_NET
65 # define NA_MSG "NET package needed to run test"
66 #elif !defined CYGPKG_POSIX_SIGNALS
67 # define NA_MSG "POSIX signals package needed to run test"
70 #include <cyg/infra/testcase.h>
74 #include <pkgconf/hal.h>
75 #include <pkgconf/kernel.h>
76 #include <pkgconf/io_fileio.h>
81 #include <cyg/kernel/ktypes.h> // base kernel types
82 #include <cyg/infra/cyg_trac.h> // tracing macros
83 #include <cyg/infra/cyg_ass.h> // assertion macros
85 #include <cyg/infra/testcase.h>
93 #include <arpa/inet.h>
99 #include <cyg/infra/diag.h> // HAL polled output
101 //--------------------------------------------------------------------------
103 #define SHOW_RESULT( _fn, _res ) \
104 diag_printf(#_fn " returned %d %s\n", (int) _res, _res<0?strerror(errno):"");
106 //--------------------------------------------------------------------------
109 char thread1_stack[PTHREAD_STACK_MIN*2];
110 char thread2_stack[PTHREAD_STACK_MIN*2];
112 //--------------------------------------------------------------------------
119 struct sockaddr_in sa;
121 //--------------------------------------------------------------------------
124 #define TEST_BUFSIZE 512
126 static char buf1[TEST_BUFSIZE];
127 static char buf2[TEST_BUFSIZE];
128 static char buf3[TEST_BUFSIZE];
130 //--------------------------------------------------------------------------
132 void *pthread_entry1( void *arg)
135 struct sockaddr_in accsa;
136 socklen_t accsa_len = sizeof(accsa);
139 // int so1 = sizeof(one);
144 CYG_TEST_INFO( "Thread 1 running" );
146 CYG_TEST_INFO( "Thread1: calling socket()");
147 fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
148 if( fd < 0 ) SHOW_RESULT( socket, fd );
149 CYG_TEST_CHECK( fd >= 0, "socket() returned error");
151 // err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
152 // if( err < 0 ) SHOW_RESULT( setsockopt, err );
153 // CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
155 CYG_TEST_INFO( "Thread1: calling bind()");
156 err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
157 if( err < 0 ) SHOW_RESULT( bind, err );
158 CYG_TEST_CHECK( err == 0, "bind() returned error");
160 CYG_TEST_INFO( "Thread1: calling listen()");
161 err = listen( fd, 1);
162 if( err < 0 ) SHOW_RESULT( listen, err );
163 CYG_TEST_CHECK( err == 0, "listen() returned error");
168 CYG_TEST_INFO( "Thread1: calling select()");
169 err = select( fd+1, &rd, NULL, NULL, NULL );
170 if( err < 0 ) SHOW_RESULT( select, err );
171 CYG_TEST_CHECK( err >= 0, "select() returned error");
172 CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
174 CYG_TEST_INFO( "Thread1: calling accept()");
175 fd2 = accept( fd, (struct sockaddr *)&accsa, &accsa_len );
176 if( fd2 < 0 ) SHOW_RESULT( accept, fd2 );
177 CYG_TEST_CHECK( fd2 >= 0, "accept() returned error");
180 for( i = 0; i < TEST_BUFSIZE; i++ ) buf1[i] = i;
182 CYG_TEST_INFO( "Thread1: calling write()");
183 done = write( fd2, buf1, TEST_BUFSIZE);
184 if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
185 CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
190 CYG_TEST_INFO( "Thread1: calling select()");
191 err = select( fd2+1, &rd, NULL, NULL, NULL );
192 if( err < 0 ) SHOW_RESULT( select, err );
193 CYG_TEST_CHECK( err >= 0, "select() returned error");
194 CYG_TEST_CHECK( FD_ISSET( fd2, &rd ), "Fd2 not set in select() result");
196 CYG_TEST_INFO( "Thread1: calling read()");
197 done = read( fd2, buf3, TEST_BUFSIZE);
198 if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
199 CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
201 for( i = 0; i < TEST_BUFSIZE; i++ )
202 if( buf1[i] != buf3[i] )
203 diag_printf("buf1[%d](%02x) != buf3[%d](%02x)\n",i,buf1[i],i,buf3[i]);
205 CYG_TEST_INFO( "Thread1: calling close(fd)");
207 if( err < 0 ) SHOW_RESULT( close, err );
208 CYG_TEST_CHECK( err == 0, "close() returned error");
210 CYG_TEST_INFO( "Thread1: calling close(fd2)");
212 if( err < 0 ) SHOW_RESULT( close, err );
213 CYG_TEST_CHECK( err == 0, "close() returned error");
215 CYG_TEST_INFO( "Thread1: calling pthread_exit()");
219 //--------------------------------------------------------------------------
221 void *pthread_entry2( void *arg)
226 // int so1 = sizeof(one);
231 CYG_TEST_INFO( "Thread 2 running" );
233 CYG_TEST_INFO( "Thread2: calling socket()");
234 fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
235 if( fd < 0 ) SHOW_RESULT( socket, fd );
236 CYG_TEST_CHECK( fd >= 0, "socket() returned error");
238 // err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
239 // if( err < 0 ) SHOW_RESULT( setsockopt, err );
240 // CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
242 CYG_TEST_INFO( "Thread2: calling connect()");
243 err = connect( fd, (struct sockaddr *)&sa, sizeof(sa));
244 if( err < 0 ) SHOW_RESULT( connect, err );
245 CYG_TEST_CHECK( err == 0, "connect() returned error");
250 CYG_TEST_INFO( "Thread2: calling select()");
251 err = select( fd+1, &rd, NULL, NULL, NULL );
252 if( err < 0 ) SHOW_RESULT( select, err );
253 CYG_TEST_CHECK( err >= 0, "select() returned error");
254 CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
256 CYG_TEST_INFO( "Thread2: calling read()");
257 done = read( fd, buf2, TEST_BUFSIZE);
258 if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
259 CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
261 for( i = 0; i < TEST_BUFSIZE; i++ )
262 if( buf1[i] != buf2[i] )
263 diag_printf("buf1[%d](%02x) != buf2[%d](%02x)\n",i,buf1[i],i,buf2[i]);
265 CYG_TEST_INFO( "Thread2: calling write()");
266 done = write( fd, buf2, TEST_BUFSIZE);
267 if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
268 CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
270 CYG_TEST_INFO( "Thread2: calling close(fd)");
272 if( err < 0 ) SHOW_RESULT( close, err );
273 CYG_TEST_CHECK( err == 0, "close() returned error");
275 CYG_TEST_INFO( "Thread2: calling pthread_exit()");
279 //==========================================================================
282 int main( int argc, char **argv )
285 char *addr1 = "127.0.255.106";
289 struct sched_param schedparam;
291 sa.sin_family = AF_INET;
292 sa.sin_len = sizeof(sa);
293 inet_aton("127.0.0.1", &sa.sin_addr);
294 sa.sin_port = htons(1234);
298 init_all_network_interfaces();
300 // Test inet_ntoa() and inet_aton()
302 inet_aton(addr1, &ina);
303 addr2 = inet_ntoa(ina);
304 CYG_TEST_CHECK( strcmp(addr1, addr2) == 0, "Bad inet adderess conversion");
307 // Create test threads
310 pthread_attr_init( &attr );
312 schedparam.sched_priority = 10;
313 pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
314 pthread_attr_setschedpolicy( &attr, SCHED_RR );
315 pthread_attr_setschedparam( &attr, &schedparam );
316 pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
317 pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
319 pthread_create( &thread1,
326 pthread_attr_init( &attr );
328 schedparam.sched_priority = 5;
329 pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
330 pthread_attr_setschedpolicy( &attr, SCHED_RR );
331 pthread_attr_setschedparam( &attr, &schedparam );
332 pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
333 pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
335 pthread_create( &thread2,
341 // Now join with thread1
342 CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
343 pthread_join( thread1, &retval );
346 CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
347 pthread_join( thread2, &retval );
349 CYG_TEST_PASS_FINISH("socket");
354 //==========================================================================
357 int main( int argc, char **argv )
367 // -------------------------------------------------------------------------