OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [tests/] [socket.c] - Blame information for rev 825

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):           nickg
43
// Contributors:        nickg
44
// Date:                2000-05-25
45
// Purpose:             Test socket API
46
// Description:         This program tests the socket API. Note that it is only
47
//                      intended to test the API and not the functionality of
48
//                      the underlying network stack. That is assumed to have
49
//                      been established by other tests elsewhere.
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
#include <pkgconf/system.h>
56
#include <pkgconf/isoinfra.h>
57
#ifdef CYGPKG_POSIX
58
#include <pkgconf/posix.h>
59
#endif
60
 
61
#ifndef CYGINT_ISO_PTHREAD_IMPL
62
# define NA_MSG "POSIX threads needed to run test"
63
#elif !defined CYGPKG_NET
64
# define NA_MSG "NET package needed to run test"
65
#elif !defined CYGPKG_POSIX_SIGNALS
66
# define NA_MSG "POSIX signals package needed to run test"
67
#endif
68
 
69
#include <cyg/infra/testcase.h>
70
 
71
#ifndef NA_MSG
72
 
73
#include <pkgconf/hal.h>
74
#include <pkgconf/kernel.h>
75
#include <pkgconf/io_fileio.h>
76
 
77
 
78
#define __ECOS 1
79
 
80
#include <cyg/kernel/ktypes.h>         // base kernel types
81
#include <cyg/infra/cyg_trac.h>        // tracing macros
82
#include <cyg/infra/cyg_ass.h>         // assertion macros
83
 
84
#include <cyg/infra/testcase.h>
85
 
86
#include <unistd.h>
87
#include <fcntl.h>
88
#include <sys/stat.h>
89
#include <errno.h>
90
 
91
#include <network.h>
92
#include <arpa/inet.h>
93
 
94
#include <pthread.h>
95
#include <signal.h>
96
 
97
 
98
#include <cyg/infra/diag.h>            // HAL polled output
99
 
100
//--------------------------------------------------------------------------
101
 
102
#define SHOW_RESULT( _fn, _res ) \
103
diag_printf(#_fn " returned %d %s\n", (int) _res, _res<0?strerror(errno):"");
104
 
105
//--------------------------------------------------------------------------
106
// Thread stack.
107
 
108
char thread1_stack[PTHREAD_STACK_MIN*2];
109
char thread2_stack[PTHREAD_STACK_MIN*2];
110
 
111
//--------------------------------------------------------------------------
112
// Local variables
113
 
114
// Thread IDs
115
pthread_t thread1;
116
pthread_t thread2;
117
 
118
struct sockaddr_in sa;
119
 
120
//--------------------------------------------------------------------------
121
// test buffers
122
 
123
#define TEST_BUFSIZE 512
124
 
125
static char buf1[TEST_BUFSIZE];
126
static char buf2[TEST_BUFSIZE];
127
static char buf3[TEST_BUFSIZE];
128
 
129
//--------------------------------------------------------------------------
130
 
131
void *pthread_entry1( void *arg)
132
{
133
    int fd, fd2;
134
    struct sockaddr_in accsa;
135
    socklen_t accsa_len = sizeof(accsa);
136
    int err;
137
//    int one = 1;
138
//    int so1 = sizeof(one);
139
    fd_set rd;
140
    int i;
141
    ssize_t done;
142
 
143
    CYG_TEST_INFO( "Thread 1 running" );
144
 
145
    CYG_TEST_INFO( "Thread1: calling socket()");
146
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
147
    if( fd < 0 ) SHOW_RESULT( socket, fd );
148
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
149
 
150
//    err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
151
//    if( err < 0 ) SHOW_RESULT( setsockopt, err );
152
//    CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
153
 
154
    CYG_TEST_INFO( "Thread1: calling bind()");
155
    err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
156
    if( err < 0 ) SHOW_RESULT( bind, err );
157
    CYG_TEST_CHECK( err == 0, "bind() returned error");
158
 
159
    CYG_TEST_INFO( "Thread1: calling listen()");
160
    err = listen( fd, 1);
161
    if( err < 0 ) SHOW_RESULT( listen, err );
162
    CYG_TEST_CHECK( err == 0, "listen() returned error");
163
 
164
    FD_ZERO( &rd );
165
    FD_SET( fd, &rd );
166
 
167
    CYG_TEST_INFO( "Thread1: calling select()");
168
    err = select( fd+1, &rd, NULL, NULL, NULL );
169
    if( err < 0 ) SHOW_RESULT( select, err );
170
    CYG_TEST_CHECK( err >= 0, "select() returned error");
171
    CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
172
 
173
    CYG_TEST_INFO( "Thread1: calling accept()");
174
    fd2 = accept( fd, (struct sockaddr *)&accsa, &accsa_len );
175
    if( fd2 < 0 ) SHOW_RESULT( accept, fd2 );
176
    CYG_TEST_CHECK( fd2 >= 0, "accept() returned error");
177
 
178
 
179
    for( i = 0; i < TEST_BUFSIZE; i++ ) buf1[i] = i;
180
 
181
    CYG_TEST_INFO( "Thread1: calling write()");
182
    done = write( fd2, buf1, TEST_BUFSIZE);
183
    if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
184
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
185
 
186
    FD_ZERO( &rd );
187
    FD_SET( fd2, &rd );
188
 
189
    CYG_TEST_INFO( "Thread1: calling select()");
190
    err = select( fd2+1, &rd, NULL, NULL, NULL );
191
    if( err < 0 ) SHOW_RESULT( select, err );
192
    CYG_TEST_CHECK( err >= 0, "select() returned error");
193
    CYG_TEST_CHECK( FD_ISSET( fd2, &rd ), "Fd2 not set in select() result");
194
 
195
    CYG_TEST_INFO( "Thread1: calling read()");
196
    done = read( fd2, buf3, TEST_BUFSIZE);
197
    if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
198
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
199
 
200
    for( i = 0; i < TEST_BUFSIZE; i++ )
201
        if( buf1[i] != buf3[i] )
202
            diag_printf("buf1[%d](%02x) != buf3[%d](%02x)\n",i,buf1[i],i,buf3[i]);
203
 
204
    CYG_TEST_INFO( "Thread1: calling close(fd)");
205
    err = close(fd);
206
    if( err < 0 ) SHOW_RESULT( close, err );
207
    CYG_TEST_CHECK( err == 0, "close() returned error");
208
 
209
    CYG_TEST_INFO( "Thread1: calling close(fd2)");
210
    err = close(fd2);
211
    if( err < 0 ) SHOW_RESULT( close, err );
212
    CYG_TEST_CHECK( err == 0, "close() returned error");
213
 
214
    CYG_TEST_INFO( "Thread1: calling pthread_exit()");
215
    pthread_exit( arg );
216
}
217
 
218
//--------------------------------------------------------------------------
219
 
220
void *pthread_entry2( void *arg)
221
{
222
    int fd;
223
    int err;
224
//    int one = 1;
225
//    int so1 = sizeof(one);
226
    int i;
227
    ssize_t done;
228
    fd_set rd;
229
 
230
    CYG_TEST_INFO( "Thread 2 running" );
231
 
232
    CYG_TEST_INFO( "Thread2: calling socket()");
233
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
234
    if( fd < 0 ) SHOW_RESULT( socket, fd );
235
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
236
 
237
//    err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
238
//    if( err < 0 ) SHOW_RESULT( setsockopt, err );    
239
//    CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
240
 
241
    CYG_TEST_INFO( "Thread2: calling connect()");
242
    err = connect( fd, (struct sockaddr *)&sa, sizeof(sa));
243
    if( err < 0 ) SHOW_RESULT( connect, err );
244
    CYG_TEST_CHECK( err == 0, "connect() returned error");
245
 
246
    FD_ZERO( &rd );
247
    FD_SET( fd, &rd );
248
 
249
    CYG_TEST_INFO( "Thread2: calling select()");
250
    err = select( fd+1, &rd, NULL, NULL, NULL );
251
    if( err < 0 ) SHOW_RESULT( select, err );
252
    CYG_TEST_CHECK( err >= 0, "select() returned error");
253
    CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
254
 
255
    CYG_TEST_INFO( "Thread2: calling read()");
256
    done = read( fd, buf2, TEST_BUFSIZE);
257
    if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
258
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
259
 
260
    for( i = 0; i < TEST_BUFSIZE; i++ )
261
        if( buf1[i] != buf2[i] )
262
            diag_printf("buf1[%d](%02x) != buf2[%d](%02x)\n",i,buf1[i],i,buf2[i]);
263
 
264
    CYG_TEST_INFO( "Thread2: calling write()");
265
    done = write( fd, buf2, TEST_BUFSIZE);
266
    if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
267
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
268
 
269
    CYG_TEST_INFO( "Thread2: calling close(fd)");
270
    err = close(fd);
271
    if( err < 0 ) SHOW_RESULT( close, err );
272
    CYG_TEST_CHECK( err == 0, "close() returned error");
273
 
274
    CYG_TEST_INFO( "Thread2: calling pthread_exit()");
275
    pthread_exit( arg );
276
}
277
 
278
//==========================================================================
279
// main
280
 
281
int main( int argc, char **argv )
282
{
283
    struct in_addr ina;
284
    char *addr1 = "127.0.255.106";
285
    char *addr2;
286
    void *retval;
287
    pthread_attr_t attr;
288
    struct sched_param schedparam;
289
 
290
    sa.sin_family = AF_INET;
291
    sa.sin_len = sizeof(sa);
292
    inet_aton("127.0.0.1", &sa.sin_addr);
293
    sa.sin_port = htons(1234);
294
 
295
    CYG_TEST_INIT();
296
 
297
    init_all_network_interfaces();
298
 
299
    // Test inet_ntoa() and inet_aton()
300
 
301
    inet_aton(addr1, &ina);
302
    addr2 = inet_ntoa(ina);
303
    CYG_TEST_CHECK( strcmp(addr1, addr2) == 0, "Bad inet adderess conversion");
304
 
305
 
306
    // Create test threads
307
 
308
    {
309
        pthread_attr_init( &attr );
310
 
311
        schedparam.sched_priority = 10;
312
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
313
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
314
        pthread_attr_setschedparam( &attr, &schedparam );
315
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
316
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
317
 
318
        pthread_create( &thread1,
319
                        &attr,
320
                        pthread_entry1,
321
                        (void *)0x12345671);
322
    }
323
 
324
    {
325
        pthread_attr_init( &attr );
326
 
327
        schedparam.sched_priority = 5;
328
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
329
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
330
        pthread_attr_setschedparam( &attr, &schedparam );
331
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
332
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
333
 
334
        pthread_create( &thread2,
335
                        &attr,
336
                        pthread_entry2,
337
                        (void *)0x12345672);
338
    }
339
 
340
    // Now join with thread1
341
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
342
    pthread_join( thread1, &retval );
343
 
344
    // And thread 2
345
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
346
    pthread_join( thread2, &retval );
347
 
348
    CYG_TEST_PASS_FINISH("socket");
349
}
350
 
351
#else
352
 
353
//==========================================================================
354
// main
355
 
356
int main( int argc, char **argv )
357
{
358
    CYG_TEST_INIT();
359
 
360
    CYG_TEST_NA(NA_MSG);
361
}
362
 
363
#endif
364
 
365
 
366
// -------------------------------------------------------------------------
367
// EOF socket.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.