~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to gnulib-tests/test-select.h

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- buffer-read-only: t -*- vi: set ro: */
2
 
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
1
/* Test of select() substitute.
4
 
   Copyright (C) 2008-2011 Free Software Foundation, Inc.
 
2
   Copyright (C) 2008-2012 Free Software Foundation, Inc.
5
3
 
6
4
   This program is free software: you can redistribute it and/or modify
7
5
   it under the terms of the GNU General Public License as published by
32
30
#include "macros.h"
33
31
 
34
32
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
35
 
# define WIN32_NATIVE
 
33
# define WINDOWS_NATIVE
36
34
#endif
37
35
 
38
36
#ifdef HAVE_SYS_WAIT_H
86
84
 
87
85
  s = socket (AF_INET, SOCK_STREAM, 0);
88
86
 
 
87
  x = 1;
 
88
  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
 
89
 
89
90
  memset (&ia, 0, sizeof (ia));
90
91
  ia.sin_family = AF_INET;
91
92
  inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
96
97
      exit (77);
97
98
    }
98
99
 
99
 
  x = 1;
100
 
  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
101
 
 
102
100
  if (listen (s, 1) < 0)
103
101
    {
104
102
      perror ("listen");
123
121
 
124
122
  if (!blocking)
125
123
    {
126
 
#ifdef WIN32_NATIVE
 
124
#ifdef WINDOWS_NATIVE
127
125
      unsigned long iMode = 1;
128
126
      ioctl (s, FIONBIO, (char *) &iMode);
129
127
 
229
227
#endif
230
228
 
231
229
 
 
230
static int
 
231
do_select_bad_nfd_nowait (int nfd, select_fn my_select)
 
232
{
 
233
  struct timeval tv0;
 
234
  tv0.tv_sec = 0;
 
235
  tv0.tv_usec = 0;
 
236
  errno = 0;
 
237
  return my_select (nfd, NULL, NULL, NULL, &tv0);
 
238
}
 
239
 
 
240
static void
 
241
test_bad_nfd (select_fn my_select)
 
242
{
 
243
  if (do_select_bad_nfd_nowait (-1, my_select) != -1 || errno != EINVAL)
 
244
    failed ("invalid errno after negative nfds");
 
245
  /* Can't test FD_SETSIZE + 1 for EINVAL, since some systems allow
 
246
     dynamically larger set size by redefining FD_SETSIZE anywhere up
 
247
     to the actual maximum fd.  */
 
248
  /* if (do_select_bad_nfd_nowait (FD_SETSIZE + 1, my_select) != -1 */
 
249
  /*     || errno != EINVAL) */
 
250
  /*   failed ("invalid errno after bogus nfds"); */
 
251
}
 
252
 
 
253
/* Test select(2) on invalid file descriptors.  */
 
254
 
 
255
static int
 
256
do_select_bad_fd (int fd, int ev, struct timeval *timeout, select_fn my_select)
 
257
{
 
258
  fd_set rfds, wfds, xfds;
 
259
 
 
260
  FD_ZERO (&rfds);
 
261
  FD_ZERO (&wfds);
 
262
  FD_ZERO (&xfds);
 
263
  if (ev & SEL_IN)
 
264
    FD_SET (fd, &rfds);
 
265
  if (ev & SEL_OUT)
 
266
    FD_SET (fd, &wfds);
 
267
  if (ev & SEL_EXC)
 
268
    FD_SET (fd, &xfds);
 
269
  errno = 0;
 
270
  return my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
 
271
  /* In this case, when fd is invalid, on some platforms, the bit for fd
 
272
     is left alone in the fd_set, whereas on other platforms it is cleared.
 
273
     So, don't check the bit for fd here.  */
 
274
}
 
275
 
 
276
static int
 
277
do_select_bad_fd_nowait (int fd, int ev, select_fn my_select)
 
278
{
 
279
  struct timeval tv0;
 
280
  tv0.tv_sec = 0;
 
281
  tv0.tv_usec = 0;
 
282
  return do_select_bad_fd (fd, ev, &tv0, my_select);
 
283
}
 
284
 
 
285
static void
 
286
test_bad_fd (select_fn my_select)
 
287
{
 
288
  /* This tests fails on OSF/1 and native Windows, even with fd = 16.  */
 
289
#if !(defined __osf__ || defined WINDOWS_NATIVE)
 
290
  int fd;
 
291
 
 
292
  /* On Linux, Mac OS X, *BSD, values of fd like 99 or 399 are discarded
 
293
     by the kernel early and therefore do *not* lead to EBADF, as required
 
294
     by POSIX.  */
 
295
# if defined __linux__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
 
296
  fd = 16;
 
297
# else
 
298
  fd = 99;
 
299
# endif
 
300
 
 
301
  if (do_select_bad_fd_nowait (fd, SEL_IN, my_select) == 0 || errno != EBADF)
 
302
    failed ("invalid fd among rfds");
 
303
  if (do_select_bad_fd_nowait (fd, SEL_OUT, my_select) == 0 || errno != EBADF)
 
304
    failed ("invalid fd among wfds");
 
305
  if (do_select_bad_fd_nowait (fd, SEL_EXC, my_select) == 0 || errno != EBADF)
 
306
    failed ("invalid fd among xfds");
 
307
#endif
 
308
}
 
309
 
 
310
 
232
311
/* Test select(2) for unconnected nonblocking sockets.  */
233
312
 
234
313
static void
263
342
static void
264
343
test_accept_first (select_fn my_select)
265
344
{
266
 
#ifndef WIN32_NATIVE
 
345
#ifndef WINDOWS_NATIVE
267
346
  int s = open_server_socket ();
268
347
  struct sockaddr_in ia;
269
348
  socklen_t addrlen;
364
443
static int
365
444
test_function (select_fn my_select)
366
445
{
367
 
  int result;
 
446
  int result = 0;
368
447
 
369
448
#ifdef INTERACTIVE
370
449
  printf ("Please press Enter\n");
371
450
  test (test_tty, "TTY", my_select);
372
451
#endif
373
452
 
374
 
  result = test (test_connect_first, my_select, "Unconnected socket test");
 
453
  result += test (test_bad_nfd, my_select, "Invalid nfd test");
 
454
  result += test (test_bad_fd, my_select, "Invalid fd test");
 
455
  result += test (test_connect_first, my_select, "Unconnected socket test");
375
456
  result += test (test_socket_pair, my_select, "Connected sockets test");
376
457
  result += test (test_accept_first, my_select, "General socket test with fork");
377
458
  result += test (test_pipe, my_select, "Pipe test");