OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [tests/] [socket.c] - Blame information for rev 27

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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/hal.h>
57
#include <pkgconf/kernel.h>
58
#include <pkgconf/io_fileio.h>
59
 
60
 
61
#define __ECOS 1
62
 
63
#include <cyg/kernel/ktypes.h>         // base kernel types
64
#include <cyg/infra/cyg_trac.h>        // tracing macros
65
#include <cyg/infra/cyg_ass.h>         // assertion macros
66
 
67
#include <cyg/infra/testcase.h>
68
 
69
#if defined(CYGPKG_NET) && defined(CYGPKG_POSIX)
70
 
71
#include <unistd.h>
72
#include <fcntl.h>
73
#include <sys/stat.h>
74
#include <errno.h>
75
 
76
#include <network.h>
77
#include <arpa/inet.h>
78
 
79
#include <pthread.h>
80
#include <signal.h>
81
 
82
 
83
#include <cyg/infra/diag.h>            // HAL polled output
84
 
85
//--------------------------------------------------------------------------
86
 
87
#define SHOW_RESULT( _fn, _res ) \
88
diag_printf(#_fn " returned %d %s\n", _res, _res<0?strerror(errno):"");
89
 
90
//--------------------------------------------------------------------------
91
// Thread stack.
92
 
93
char thread1_stack[PTHREAD_STACK_MIN*2];
94
char thread2_stack[PTHREAD_STACK_MIN*2];
95
 
96
//--------------------------------------------------------------------------
97
// Local variables
98
 
99
// Thread IDs
100
pthread_t thread1;
101
pthread_t thread2;
102
 
103
struct sockaddr_in sa;
104
 
105
//--------------------------------------------------------------------------
106
// test buffers
107
 
108
#define TEST_BUFSIZE 512
109
 
110
static char buf1[TEST_BUFSIZE];
111
static char buf2[TEST_BUFSIZE];
112
static char buf3[TEST_BUFSIZE];
113
 
114
//--------------------------------------------------------------------------
115
 
116
void *pthread_entry1( void *arg)
117
{
118
    int fd, fd2;
119
    struct sockaddr_in accsa;
120
    socklen_t accsa_len = sizeof(accsa);
121
    int err;
122
//    int one = 1;
123
//    int so1 = sizeof(one);
124
    fd_set rd;
125
    int i;
126
    ssize_t done;
127
 
128
    CYG_TEST_INFO( "Thread 1 running" );
129
 
130
    CYG_TEST_INFO( "Thread1: calling socket()");
131
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
132
    if( fd < 0 ) SHOW_RESULT( socket, fd );
133
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
134
 
135
//    err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
136
//    if( err < 0 ) SHOW_RESULT( setsockopt, err );
137
//    CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
138
 
139
    CYG_TEST_INFO( "Thread1: calling bind()");
140
    err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
141
    if( err < 0 ) SHOW_RESULT( bind, err );
142
    CYG_TEST_CHECK( err == 0, "bind() returned error");
143
 
144
    CYG_TEST_INFO( "Thread1: calling listen()");
145
    err = listen( fd, 1);
146
    if( err < 0 ) SHOW_RESULT( listen, err );
147
    CYG_TEST_CHECK( err == 0, "listen() returned error");
148
 
149
    FD_ZERO( &rd );
150
    FD_SET( fd, &rd );
151
 
152
    CYG_TEST_INFO( "Thread1: calling select()");
153
    err = select( fd+1, &rd, NULL, NULL, NULL );
154
    if( err < 0 ) SHOW_RESULT( select, err );
155
    CYG_TEST_CHECK( err >= 0, "select() returned error");
156
    CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
157
 
158
    CYG_TEST_INFO( "Thread1: calling accept()");
159
    fd2 = accept( fd, (struct sockaddr *)&accsa, &accsa_len );
160
    if( fd2 < 0 ) SHOW_RESULT( accept, fd2 );
161
    CYG_TEST_CHECK( fd2 >= 0, "accept() returned error");
162
 
163
 
164
    for( i = 0; i < TEST_BUFSIZE; i++ ) buf1[i] = i;
165
 
166
    CYG_TEST_INFO( "Thread1: calling write()");
167
    done = write( fd2, buf1, TEST_BUFSIZE);
168
    if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
169
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
170
 
171
    FD_ZERO( &rd );
172
    FD_SET( fd2, &rd );
173
 
174
    CYG_TEST_INFO( "Thread1: calling select()");
175
    err = select( fd2+1, &rd, NULL, NULL, NULL );
176
    if( err < 0 ) SHOW_RESULT( select, err );
177
    CYG_TEST_CHECK( err >= 0, "select() returned error");
178
    CYG_TEST_CHECK( FD_ISSET( fd2, &rd ), "Fd2 not set in select() result");
179
 
180
    CYG_TEST_INFO( "Thread1: calling read()");
181
    done = read( fd2, buf3, TEST_BUFSIZE);
182
    if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
183
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
184
 
185
    for( i = 0; i < TEST_BUFSIZE; i++ )
186
        if( buf1[i] != buf3[i] )
187
            diag_printf("buf1[%d](%02x) != buf3[%d](%02x)\n",i,buf1[i],i,buf3[i]);
188
 
189
    CYG_TEST_INFO( "Thread1: calling close(fd)");
190
    err = close(fd);
191
    if( err < 0 ) SHOW_RESULT( close, err );
192
    CYG_TEST_CHECK( err == 0, "close() returned error");
193
 
194
    CYG_TEST_INFO( "Thread1: calling close(fd2)");
195
    err = close(fd2);
196
    if( err < 0 ) SHOW_RESULT( close, err );
197
    CYG_TEST_CHECK( err == 0, "close() returned error");
198
 
199
    CYG_TEST_INFO( "Thread1: calling pthread_exit()");
200
    pthread_exit( arg );
201
}
202
 
203
//--------------------------------------------------------------------------
204
 
205
void *pthread_entry2( void *arg)
206
{
207
    int fd;
208
    int err;
209
//    int one = 1;
210
//    int so1 = sizeof(one);
211
    int i;
212
    ssize_t done;
213
    fd_set rd;
214
 
215
    CYG_TEST_INFO( "Thread 2 running" );
216
 
217
    CYG_TEST_INFO( "Thread2: calling socket()");
218
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
219
    if( fd < 0 ) SHOW_RESULT( socket, fd );
220
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
221
 
222
//    err = setsockopt( fd, SOL_SOCKET, SO_DONTROUTE, (void *)&one, so1);
223
//    if( err < 0 ) SHOW_RESULT( setsockopt, err );    
224
//    CYG_TEST_CHECK( err == 0, "setsockopt() returned error");
225
 
226
    CYG_TEST_INFO( "Thread2: calling connect()");
227
    err = connect( fd, (struct sockaddr *)&sa, sizeof(sa));
228
    if( err < 0 ) SHOW_RESULT( connect, err );
229
    CYG_TEST_CHECK( err == 0, "connect() returned error");
230
 
231
    FD_ZERO( &rd );
232
    FD_SET( fd, &rd );
233
 
234
    CYG_TEST_INFO( "Thread2: calling select()");
235
    err = select( fd+1, &rd, NULL, NULL, NULL );
236
    if( err < 0 ) SHOW_RESULT( select, err );
237
    CYG_TEST_CHECK( err >= 0, "select() returned error");
238
    CYG_TEST_CHECK( FD_ISSET( fd, &rd ), "Fd not set in select() result");
239
 
240
    CYG_TEST_INFO( "Thread2: calling read()");
241
    done = read( fd, buf2, TEST_BUFSIZE);
242
    if( done != TEST_BUFSIZE ) SHOW_RESULT( read, done );
243
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "read() returned bad size");
244
 
245
    for( i = 0; i < TEST_BUFSIZE; i++ )
246
        if( buf1[i] != buf2[i] )
247
            diag_printf("buf1[%d](%02x) != buf2[%d](%02x)\n",i,buf1[i],i,buf2[i]);
248
 
249
    CYG_TEST_INFO( "Thread2: calling write()");
250
    done = write( fd, buf2, TEST_BUFSIZE);
251
    if( done != TEST_BUFSIZE ) SHOW_RESULT( write, done );
252
    CYG_TEST_CHECK( done == TEST_BUFSIZE, "write() returned bad size");
253
 
254
    CYG_TEST_INFO( "Thread2: calling close(fd)");
255
    err = close(fd);
256
    if( err < 0 ) SHOW_RESULT( close, err );
257
    CYG_TEST_CHECK( err == 0, "close() returned error");
258
 
259
    CYG_TEST_INFO( "Thread2: calling pthread_exit()");
260
    pthread_exit( arg );
261
}
262
 
263
//==========================================================================
264
// main
265
 
266
int main( int argc, char **argv )
267
{
268
    struct in_addr ina;
269
    char *addr1 = "127.0.255.106";
270
    char *addr2;
271
    void *retval;
272
    pthread_attr_t attr;
273
    struct sched_param schedparam;
274
 
275
    sa.sin_family = AF_INET;
276
    sa.sin_len = sizeof(sa);
277
    inet_aton("127.0.0.1", &sa.sin_addr);
278
    sa.sin_port = htons(1234);
279
 
280
    CYG_TEST_INIT();
281
 
282
    init_all_network_interfaces();
283
 
284
    // Test inet_ntoa() and inet_aton()
285
 
286
    inet_aton(addr1, &ina);
287
    addr2 = inet_ntoa(ina);
288
    CYG_TEST_CHECK( strcmp(addr1, addr2) == 0, "Bad inet adderess conversion");
289
 
290
 
291
    // Create test threads
292
 
293
    {
294
        pthread_attr_init( &attr );
295
 
296
        schedparam.sched_priority = 10;
297
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
298
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
299
        pthread_attr_setschedparam( &attr, &schedparam );
300
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
301
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
302
 
303
        pthread_create( &thread1,
304
                        &attr,
305
                        pthread_entry1,
306
                        (void *)0x12345671);
307
    }
308
 
309
    {
310
        pthread_attr_init( &attr );
311
 
312
        schedparam.sched_priority = 5;
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 *)&thread2_stack[sizeof(thread2_stack)] );
317
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
318
 
319
        pthread_create( &thread2,
320
                        &attr,
321
                        pthread_entry2,
322
                        (void *)0x12345672);
323
    }
324
 
325
    // Now join with thread1
326
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
327
    pthread_join( thread1, &retval );
328
 
329
    // And thread 2
330
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
331
    pthread_join( thread2, &retval );
332
 
333
    CYG_TEST_PASS_FINISH("socket");
334
}
335
 
336
#else
337
 
338
//==========================================================================
339
// main
340
 
341
int main( int argc, char **argv )
342
{
343
    CYG_TEST_INIT();
344
 
345
    CYG_TEST_NA("socket");
346
}
347
 
348
#endif
349
 
350
 
351
// -------------------------------------------------------------------------
352
// EOF socket.c

powered by: WebSVN 2.1.0

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