libreplace: add extended getifaddrs test that prints out the interfaces.
authorMichael Adam <obnox@samba.org>
Thu, 28 Feb 2008 20:43:06 +0000 (21:43 +0100)
committerMichael Adam <obnox@samba.org>
Thu, 28 Feb 2008 22:01:15 +0000 (23:01 +0100)
Michael

source/lib/replace/Makefile.in
source/lib/replace/test/getifaddrs.c [new file with mode: 0644]
source/lib/replace/test/testsuite.c
source/torture/local/config.mk

index af9522f3a6bce57ccc383c071b2238e46b504028..c989835a8d37b29cee7e63451be470f2a4dd6194 100644 (file)
@@ -40,7 +40,7 @@ test: all
 
 installcheck: install test
 
-TEST_OBJS = test/testsuite.o test/os2_delete.o test/strptime.o
+TEST_OBJS = test/testsuite.o test/os2_delete.o test/strptime.o test/getifaddrs.o
 
 testsuite: libreplace.a $(TEST_OBJS)
        $(CC) -o testsuite $(TEST_OBJS) -L. -lreplace $(LDFLAGS) $(LIBS)
diff --git a/source/lib/replace/test/getifaddrs.c b/source/lib/replace/test/getifaddrs.c
new file mode 100644 (file)
index 0000000..66eed70
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Unix SMB/CIFS implementation.
+ *
+ * libreplace getifaddrs test
+ *
+ * Copyright (C) Michael Adam <obnox@samba.org> 2008
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef AUTOCONF_TEST
+#include "replace.h"
+#include "system/network.h"
+#endif
+
+#ifdef HAVE_INET_NTOP
+#define rep_inet_ntop inet_ntop
+#endif
+
+static const char *format_sockaddr(struct sockaddr *addr,
+                                  char *addrstring,
+                                  socklen_t addrlen)
+{
+       const char *result = NULL;
+
+       if (addr->sa_family == AF_INET) {
+               result = rep_inet_ntop(AF_INET,
+                                      &((struct sockaddr_in *)addr)->sin_addr,
+                                      addrstring,
+                                      addrlen);
+       } else if (addr->sa_family == AF_INET6) {
+               result = rep_inet_ntop(AF_INET6,
+                                      &((struct sockaddr_in6 *)addr)->sin6_addr,
+                                      addrstring,
+                                      addrlen);
+       }
+       return result;
+}
+
+int getifaddrs_test(void)
+{
+       struct ifaddrs *ifs = NULL;
+       int ret;
+
+       ret = getifaddrs(&ifs);
+       if (ret != 0) {
+               fprintf(stderr, "getifaddrs() failed: %s", strerror(errno));
+               return 1;
+       }
+
+       while (ifs) {
+               printf("%-10s ", ifs->ifa_name);
+               if (ifs->ifa_addr != NULL) {
+                       char addrstring[INET6_ADDRSTRLEN];
+                       const char *result;
+
+                       result = format_sockaddr(ifs->ifa_addr,
+                                                addrstring,
+                                                sizeof(addrstring));
+                       if (result != NULL) {
+                               printf("IP=%s ", addrstring);
+                       }
+
+                       if (ifs->ifa_netmask != NULL) {
+                               result = format_sockaddr(ifs->ifa_netmask,
+                                                        addrstring,
+                                                        sizeof(addrstring));
+                               if (result != NULL) {
+                                       printf("NETMASK=%s", addrstring);
+                               }
+                       } else {
+                               printf("AF=%d ", ifs->ifa_addr->sa_family);
+                       }
+               } else {
+                       printf("<no address>");
+               }
+
+               printf("\n");
+               ifs = ifs->ifa_next;
+       }
+
+       freeifaddrs(ifs);
+
+       return 0;
+}
index c9f330100555d39bbe205948d4cfea21357a768d..b53836036562b1df0ab99928184226108ff271e3 100644 (file)
@@ -856,21 +856,18 @@ static int test_strptime(void)
        return libreplace_test_strptime();
 }
 
+extern int getifaddrs_test(void);
+
 static int test_getifaddrs(void)
 {
-       struct ifaddrs *ifa;
-       int ret;
 
        printf("test: getifaddrs\n");
 
-       ret = getifaddrs(&ifa);
-       if (ret != 0) {
+       if (getifaddrs_test() != 0) {
                printf("failure: getifaddrs\n");
                return false;
        }
 
-       freeifaddrs(ifa);
-
        printf("success: getifaddrs\n");
        return true;
 }
index d0ba1f2782e72080f5349e83e2b75d5dbf085305..4143d415cff72018fb9fc0a90b8274e09f17a089 100644 (file)
@@ -8,6 +8,7 @@ PRIVATE_PROTO_HEADER = \
 OBJ_FILES = \
                ../../lib/charset/tests/iconv.o \
                ../../lib/talloc/testsuite.o \
+               ../../lib/replace/test/getifaddrs.o \
                ../../lib/replace/test/os2_delete.o \
                ../../lib/replace/test/strptime.o \
                ../../lib/replace/test/testsuite.o \