]> git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/check_ifaddrs.c
tests: Simplify Makefile.am
[thirdparty/lldpd.git] / tests / check_ifaddrs.c
1 #include <stdio.h>
2 #include <arpa/inet.h>
3 #include <check.h>
4 #include "../src/lldpd.h"
5
6 #define DUMP "ifdump.txt"
7
8 /* This is not a real test. It should dump into a file the list of interfaces */
9
10 static const char *
11 addr_string (struct sockaddr *sa) {
12 static char buf[64];
13 const char *res;
14 if (sa == NULL)
15 return "NULL";
16 switch (sa->sa_family) {
17 case AF_INET:
18 res = inet_ntop(AF_INET,
19 &((struct sockaddr_in *)sa)->sin_addr,
20 buf, sizeof(buf));
21 break;
22 case AF_INET6:
23 res = inet_ntop(AF_INET6,
24 &((struct sockaddr_in6 *)sa)->sin6_addr,
25 buf, sizeof(buf));
26 break;
27 case AF_UNSPEC:
28 return "<--->";
29 case AF_PACKET:
30 return "<pkt>";
31 default:
32 snprintf(buf, 64, "<%4d>", sa->sa_family);
33 return buf;
34 }
35 strcpy(buf, res);
36 if (strlen(buf) > 26)
37 memcpy(buf + 21, "[...]", strlen("[...]") + 1);
38 return buf;
39 }
40
41 START_TEST (test_ifaddrs)
42 {
43 struct ifaddrs *ifap, *ifa;
44 FILE* dump;
45
46 if (getifaddrs(&ifap) < 0) {
47 fail("unable to get interface list");
48 return;
49 }
50 dump = fopen(DUMP, "w+");
51 if (dump == NULL) {
52 fail("unable to open dump file " DUMP);
53 return;
54 }
55 fprintf(dump,
56 "Name Flags Address Netmask Broadcast/Destination\n");
57 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
58 fprintf(dump, "%-15s%#.5x ",
59 ifa->ifa_name, ifa->ifa_flags);
60 fprintf(dump, "%-26s ",
61 addr_string(ifa->ifa_addr));
62 fprintf(dump, "%-26s ",
63 addr_string(ifa->ifa_netmask));
64 fprintf(dump, "%-26s\n",
65 addr_string(ifa->ifa_broadaddr));
66 }
67 fclose(dump);
68 }
69 END_TEST
70
71 Suite *
72 ifaddrs_suite(void)
73 {
74 Suite *s = suite_create("getifaddrs");
75
76 /* Single objects packing/unpacking */
77 TCase *tc_core = tcase_create("getifaddrs");
78 tcase_add_test(tc_core, test_ifaddrs);
79 suite_add_tcase(s, tc_core);
80
81 return s;
82 }
83
84 int
85 main()
86 {
87 int number_failed;
88 Suite *s = ifaddrs_suite ();
89 SRunner *sr = srunner_create (s);
90 srunner_run_all (sr, CK_ENV);
91 number_failed = srunner_ntests_failed (sr);
92 srunner_free (sr);
93 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
94 }