]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/io/fileio/v2_0/tests/socket.c
Initial revision
[karo-tx-redboot.git] / packages / io / fileio / v2_0 / tests / socket.c
1 //==========================================================================
2 //
3 //      socket.c
4 //
5 //      Test socket API
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):           nickg
44 // Contributors:        nickg
45 // Date:                2000-05-25
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.
51 //
52 //####DESCRIPTIONEND####
53 //
54 //==========================================================================
55
56 #include <pkgconf/system.h>
57 #include <pkgconf/isoinfra.h>
58 #ifdef CYGPKG_POSIX
59 #include <pkgconf/posix.h>
60 #endif
61
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"
68 #endif
69
70 #include <cyg/infra/testcase.h>
71
72 #ifndef NA_MSG
73
74 #include <pkgconf/hal.h>
75 #include <pkgconf/kernel.h>
76 #include <pkgconf/io_fileio.h>
77
78
79 #define __ECOS 1
80
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
84
85 #include <cyg/infra/testcase.h>
86
87 #include <unistd.h>
88 #include <fcntl.h>
89 #include <sys/stat.h>
90 #include <errno.h>
91
92 #include <network.h>
93 #include <arpa/inet.h>
94
95 #include <pthread.h>
96 #include <signal.h>
97
98
99 #include <cyg/infra/diag.h>            // HAL polled output
100
101 //--------------------------------------------------------------------------
102
103 #define SHOW_RESULT( _fn, _res ) \
104 diag_printf(#_fn " returned %d %s\n", (int) _res, _res<0?strerror(errno):"");
105
106 //--------------------------------------------------------------------------
107 // Thread stack.
108
109 char thread1_stack[PTHREAD_STACK_MIN*2];
110 char thread2_stack[PTHREAD_STACK_MIN*2];
111
112 //--------------------------------------------------------------------------
113 // Local variables
114
115 // Thread IDs
116 pthread_t thread1;
117 pthread_t thread2;
118
119 struct sockaddr_in sa;
120
121 //--------------------------------------------------------------------------
122 // test buffers
123
124 #define TEST_BUFSIZE 512
125
126 static char buf1[TEST_BUFSIZE];
127 static char buf2[TEST_BUFSIZE];
128 static char buf3[TEST_BUFSIZE];
129
130 //--------------------------------------------------------------------------
131
132 void *pthread_entry1( void *arg)
133 {
134     int fd, fd2;
135     struct sockaddr_in accsa;
136     socklen_t accsa_len = sizeof(accsa);
137     int err;
138 //    int one = 1;
139 //    int so1 = sizeof(one);
140     fd_set rd;
141     int i;
142     ssize_t done;
143     
144     CYG_TEST_INFO( "Thread 1 running" );
145
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");
150
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");
154
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");
159
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");
164
165     FD_ZERO( &rd );
166     FD_SET( fd, &rd );
167
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");
173
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");
178
179
180     for( i = 0; i < TEST_BUFSIZE; i++ ) buf1[i] = i;
181
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");    
186
187     FD_ZERO( &rd );
188     FD_SET( fd2, &rd );
189
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");
195     
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");    
200
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]);
204
205     CYG_TEST_INFO( "Thread1: calling close(fd)");
206     err = close(fd);
207     if( err < 0 ) SHOW_RESULT( close, err );    
208     CYG_TEST_CHECK( err == 0, "close() returned error");    
209
210     CYG_TEST_INFO( "Thread1: calling close(fd2)");
211     err = close(fd2);
212     if( err < 0 ) SHOW_RESULT( close, err );    
213     CYG_TEST_CHECK( err == 0, "close() returned error");        
214     
215     CYG_TEST_INFO( "Thread1: calling pthread_exit()");    
216     pthread_exit( arg );
217 }
218
219 //--------------------------------------------------------------------------
220
221 void *pthread_entry2( void *arg)
222 {
223     int fd;
224     int err;
225 //    int one = 1;
226 //    int so1 = sizeof(one);
227     int i;
228     ssize_t done;
229     fd_set rd;
230     
231     CYG_TEST_INFO( "Thread 2 running" );
232
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");
237
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");
241
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");
246
247     FD_ZERO( &rd );
248     FD_SET( fd, &rd );
249
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");
255
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");    
260
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]);
264
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");    
269
270     CYG_TEST_INFO( "Thread2: calling close(fd)");
271     err = close(fd);
272     if( err < 0 ) SHOW_RESULT( close, err );    
273     CYG_TEST_CHECK( err == 0, "close() returned error");    
274     
275     CYG_TEST_INFO( "Thread2: calling pthread_exit()");    
276     pthread_exit( arg );
277 }
278
279 //==========================================================================
280 // main
281
282 int main( int argc, char **argv )
283 {
284     struct in_addr ina;
285     char *addr1 = "127.0.255.106";
286     char *addr2;
287     void *retval;
288     pthread_attr_t attr;
289     struct sched_param schedparam;
290     
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);
295     
296     CYG_TEST_INIT();
297
298     init_all_network_interfaces();
299     
300     // Test inet_ntoa() and inet_aton()
301
302     inet_aton(addr1, &ina);
303     addr2 = inet_ntoa(ina);
304     CYG_TEST_CHECK( strcmp(addr1, addr2) == 0, "Bad inet adderess conversion");
305
306
307     // Create test threads
308
309     {
310         pthread_attr_init( &attr );
311
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) );
318
319         pthread_create( &thread1,
320                         &attr,
321                         pthread_entry1,
322                         (void *)0x12345671);
323     }
324
325     {
326         pthread_attr_init( &attr );
327
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) );
334
335         pthread_create( &thread2,
336                         &attr,
337                         pthread_entry2,
338                         (void *)0x12345672);
339     }
340     
341     // Now join with thread1
342     CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
343     pthread_join( thread1, &retval );
344
345     // And thread 2
346     CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
347     pthread_join( thread2, &retval );
348
349     CYG_TEST_PASS_FINISH("socket");
350 }
351
352 #else
353
354 //==========================================================================
355 // main
356
357 int main( int argc, char **argv )
358 {
359     CYG_TEST_INIT();
360
361     CYG_TEST_NA(NA_MSG);
362 }
363
364 #endif
365
366
367 // -------------------------------------------------------------------------
368 // EOF socket.c