usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-net_if.c
bloba339a57d04dfce14d051d4cc024d5b1b9ff02e99
1 /* Test of <net/if.h> functions.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2010. */
19 #include <config.h>
21 #include <net/if.h>
23 /* We do not yet have replacements for if_* functions on systems that
24 lack a native <net/if.h>. */
25 #if HAVE_NET_IF_H && HAVE_IF_NAMEINDEX
26 # include "signature.h"
27 SIGNATURE_CHECK (if_freenameindex, void, (struct if_nameindex *));
28 SIGNATURE_CHECK (if_indextoname, char *, (unsigned int, char *));
29 SIGNATURE_CHECK (if_nameindex, struct if_nameindex *, (void));
30 SIGNATURE_CHECK (if_nametoindex, unsigned int, (const char *));
31 #endif
33 #include <stddef.h> /* NULL */
34 #include <stdio.h> /* fprintf */
35 #include <string.h> /* strcmp */
37 int
38 main (int argc, char *argv[])
40 #if HAVE_NET_IF_H && HAVE_IF_NAMEINDEX
41 struct if_nameindex *ifnp, *p;
43 p = ifnp = if_nameindex ();
44 if (ifnp == NULL)
46 fputs ("if_nameindex returned NULL\n", stderr);
47 return 1;
50 while (p->if_index)
52 unsigned int idx;
53 char buf[IF_NAMESIZE];
54 char *q;
56 if (argc > 1)
57 printf ("index %d name %s\n", p->if_index, p->if_name);
59 idx = if_nametoindex (p->if_name);
60 if (idx != p->if_index)
62 fprintf (stderr, "if_nametoindex (%s) = %d != %d\n",
63 p->if_name, idx, p->if_index);
64 return 1;
67 q = if_indextoname (p->if_index, buf);
68 if (q == NULL)
70 fprintf (stderr, "if_indextoname (%d) returned NULL\n", p->if_index);
71 return 1;
73 if (q != buf)
75 fprintf (stderr, "if_indextoname (%d) buffer mismatch?\n",
76 p->if_index);
77 return 1;
79 if (strcmp (p->if_name, q) != 0)
81 fprintf (stderr, "if_indextoname (%s) = %s ?!\n", p->if_name, q);
82 return 1;
85 p++;
88 if_freenameindex (ifnp);
89 #endif /* HAVE_NET_IF_H */
91 #if !HAVE_NET_IF_H || HAVE_IF_NAMEINDEX
93 static struct if_nameindex ni;
94 return !IF_NAMESIZE + ni.if_index + !!ni.if_name;
96 #else
97 return 0;
98 #endif