ee3cdbdf404dbadb78f1a957e2c8b7fcdbf0b6bb
[gnulib.git] / tests / test-net_if.c
1 /* Test of <net/if.h> functions.
2    Copyright (C) 2010-2012 Free Software Foundation, Inc.
3
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.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2010.  */
18
19 #include <config.h>
20
21 #include <net/if.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (if_freenameindex, void, (struct if_nameindex *));
25 SIGNATURE_CHECK (if_indextoname, char *, (unsigned int, char *));
26 SIGNATURE_CHECK (if_nameindex, struct if_nameindex *, (void));
27 SIGNATURE_CHECK (if_nametoindex, unsigned int, (const char *));
28
29 #include <stddef.h> /* NULL */
30 #include <stdio.h> /* fprintf */
31
32 int
33 main (int argc, char *argv[])
34 {
35   struct if_nameindex *ifnp, *p;
36
37   p = ifnp = if_nameindex ();
38   if (ifnp == NULL)
39     {
40       fputs ("if_nameindex returned NULL\n", stderr);
41       return 1;
42     }
43
44   while (p->if_index)
45     {
46       unsigned int idx;
47       char buf[IF_NAMESIZE];
48       char *q;
49
50       if (argc > 1)
51         printf ("index %d name %s\n", p->if_index, p->if_name);
52
53       idx = if_nametoindex (p->if_name);
54       if (idx != p->if_index)
55         {
56           fprintf (stderr, "if_nametoindex (%s) = %d != %d\n",
57                    p->if_name, idx, p->if_index);
58           return 1;
59         }
60
61       q = if_indextoname (p->if_index, buf);
62       if (q == NULL)
63         {
64           fprintf (stderr, "if_indextoname (%d) returned NULL\n", p->if_index);
65           return 1;
66         }
67       if (q != buf)
68         {
69           fprintf (stderr, "if_indextoname (%d) buffer mismatch?\n",
70                    p->if_index);
71           return 1;
72         }
73       if (strcmp (p->if_name, q) != 0)
74         {
75           fprintf (stderr, "if_indextoname (%s) = %s ?!\n", p->if_name, q);
76           return 1;
77         }
78
79       p++;
80     }
81
82   if_freenameindex (ifnp);
83
84   return 0;
85 }