New module to test <net/if.h> interfaces.
[gnulib.git] / tests / test-net_if.c
1 /* Test of <net/if.h> functions.
2    Copyright (C) 2010 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 #include <net/if.h>
21 #include <stddef.h> /* NULL */
22 #include <stdio.h> /* printf */
23
24 int
25 main (int argc, char *argv[])
26 {
27   struct if_nameindex ifn, *ifnp, *p;
28   void (*if_fni) (struct if_nameindex *) = if_freenameindex;
29   char *(*if_itn)(unsigned, char *) = if_indextoname;
30   struct if_nameindex  *(*if_ni)(void) = if_nameindex;
31   unsigned (*if_nti)(const char *) = if_nametoindex;
32
33   ifn.if_index = 42;
34   ifn.if_name = "foo";
35
36   p = ifnp = if_nameindex ();
37   if (ifnp == NULL)
38     {
39       printf ("if_nameindex returned NULL\n");
40       return 1;
41     }
42
43   while (p->if_index)
44     {
45       unsigned idx;
46       char buf[IF_NAMESIZE];
47       char *q;
48
49       if (argc > 1)
50         printf ("index %d name %s\n", p->if_index, p->if_name);
51
52       idx = if_nametoindex (p->if_name);
53       if (idx != p->if_index)
54         {
55           printf ("if_nametoindex (%s) = %d != %d\n",
56                   p->if_name, idx, p->if_index);
57           return 1;
58         }
59
60       q = if_indextoname (p->if_index, buf);
61       if (q == NULL)
62         {
63           printf ("if_indextoname (%d) returned NULL\n", p->if_index);
64           return 1;
65         }
66       if (q != buf)
67         {
68           printf ("if_indextoname (%d) buffer mismatch?\n", p->if_index);
69           return 1;
70         }
71       if (strcmp (p->if_name, q) != 0)
72         {
73           printf ("if_indextoname (%s) = %s ?!\n", p->if_name, q);
74           return 1;
75         }
76
77       p++;
78     }
79
80   if_freenameindex (ifnp);
81
82   return 0;
83 }