Skip to contentSkip to conversation

lib6: Remove runcc.* and the testcases that use it.

PATCH Superseded Zeng Linggang 3 participants 38 messages Oct 7, 2015 – Oct 22, 2015 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/7 lib6: Remove runcc.* and the testcases that use it.

---
 runtest/ipv6_lib                  |   4 -
 testcases/network/.gitignore      |   4 -
 testcases/network/lib6/Makefile   |   4 -
 testcases/network/lib6/asapi_01.c | 251 ----------------------------
 testcases/network/lib6/asapi_02.c | 338 --------------------------------------
 testcases/network/lib6/asapi_03.c | 234 --------------------------
 testcases/network/lib6/asapi_05.c | 119 +-------------
 testcases/network/lib6/asapi_07.c | 144 ----------------
 testcases/network/lib6/runcc.c    | 209 -----------------------
 testcases/network/lib6/runcc.h    |  30 ----
 10 files changed, 1 insertion(+), 1336 deletions(-)
 delete mode 100644 testcases/network/lib6/asapi_01.c
 delete mode 100644 testcases/network/lib6/asapi_02.c
 delete mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_07.c
 delete mode 100644 testcases/network/lib6/runcc.c
 delete mode 100644 testcases/network/lib6/runcc.h
runtest/ipv6_lib +0 -4
index 75614fb..203e275 100644
@@ -2,10 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_01 asapi_01
-asapi_02 asapi_02
-asapi_03 asapi_03
 asapi_04 asapi_04
 asapi_05 asapi_05
 asapi_06 asapi_06
-asapi_07 asapi_07
testcases/network/.gitignore +0 -4
index aa6d4da..18b0203 100644
@@ -1,13 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_01
-/lib6/asapi_02
-/lib6/asapi_03
 /lib6/asapi_04
 /lib6/asapi_05
 /lib6/asapi_06
-/lib6/asapi_07
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/Makefile +0 -4
index e337938..e9fde3b 100644
@@ -31,8 +31,4 @@ LDLIBS				+= -lpthread
 
 CLEAN_TARGETS			+= $(LIB)
 
-FILTER_OUT_MAKE_TARGETS		:= runcc
-
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
-
-$(MAKE_TARGETS): %: %.o runcc.o
testcases/network/lib6/asapi_01.c deleted +0 -251

251 lines not on this page · raw message

testcases/network/lib6/asapi_02.c deleted +0 -338

338 lines not on this page · raw message

testcases/network/lib6/asapi_03.c deleted +0 -234

234 lines not on this page · raw message

testcases/network/lib6/asapi_05.c +1 -118

146 lines not on this page · raw message

testcases/network/lib6/asapi_07.c deleted +0 -144

144 lines not on this page · raw message

testcases/network/lib6/runcc.c deleted +0 -209

209 lines not on this page · raw message

testcases/network/lib6/runcc.h deleted +0 -30
deleted file mode 100644
index 4e2a0d5..0000000
@@ -1,30 +0,0 @@
-/*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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 2 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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-int aliascheck(char *tname, char *incl, char *structure, char *field,
-	char *dname);
-int funccheck(char *tname, char *incl, char *fname);
-int structcheck(char *tname, char *incl, char *structure, char *field,
-	char *offset, char *size);
-int valuecheck(char *tname, char *incl, char *dname, char *dval);
-
-#define IP6_H "#include <netinet/ip6.h>\n"
-#define IN_H "#include <netinet/in.h>\n"
-#define ICMP6_H "#include <netinet/icmp6.h>\n"
-#define SOCKET_H "#include <sys/types.h>\n#include <sys/socket.h>\n"
Signature · 1 line
1.9.3
#1 lore raw

PATCH v7 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

---
 include/safe_macros.h |   1 +
 include/safe_net.h    |  60 +++++++++++++++++
 lib/safe_net.c        | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 244 insertions(+)
 create mode 100644 include/safe_net.h
 create mode 100644 lib/safe_net.c
include/safe_macros.h +1 -0
index cd86931..e938f17 100644
@@ -28,6 +28,7 @@
 #include <dirent.h>
 
 #include "safe_stdio.h"
+#include "safe_net.h"
 
 char*	safe_basename(const char *file, const int lineno,
 	    void (*cleanup_fn)(void), char *path);
include/safe_net.h new file +60 -0

60 lines not on this page · raw message

lib/safe_net.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
1.9.3
#2 lore raw

PATCH v7 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

Hi!

include/safe_net.h line 29

Quoted 2 lines
include/safe_net.h new file +2 -0
+char *ltp_sock_addr(const struct sockaddr *sa, socklen_t salen);
+#define LTP_SOCK_ADDR(sa, salen)	ltp_sock_addr(sa, salen)

Why do we define the LTP_SOCK_ADDR() as a macro?

As far as I can see it's does not add any value over calling the function.

Also we tend to prefix LTP internal funciton with tst_ rather than ltp_ but that is minor.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH v7 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

Hi,

Quoted 6 lines · Cyril Hrubis

Hi!

include/safe_net.h line 29

Quoted 2 lines
include/safe_net.h new file +2 -0
+char *ltp_sock_addr(const struct sockaddr *sa, socklen_t salen);
+#define LTP_SOCK_ADDR(sa, salen)	ltp_sock_addr(sa, salen)

Why do we define the LTP_SOCK_ADDR() as a macro?

Indeed, it is not necessary.

Quoted 5 lines

As far as I can see it's does not add any value over calling the function.

Also we tend to prefix LTP internal funciton with tst_ rather than ltp_ but that is minor.

OK, I got it.

Thanks for your review.

Best regards,
Zeng

Quoted 1 line
#4 lore raw

PATCH v7 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

In reply to Zeng Linggang

Hi!

lib/safe_net.c line 26

Quoted 4 lines
lib/safe_net.c new file +4 -0
+char *ltp_sock_addr(const struct sockaddr *sa, socklen_t salen)
+{
+	char portstr[8];
+	static char str[128];

Also this is not thread safe and the testcases that use these fucntions run in several threads. Even if the race condition where two threads write to the str buffer at the same time is unlikely we should rather change the interface to print the result into user supplied buffer.

So it reads:

char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen,

                    char *res, size_t res_len);

And the caller would just define array and pass it to the function as:

char buf[128];

...

	tst_resm(..., tst_sock_addr(sa, salen, buf, sizeof(buf)), ...);
Signature · 2 lines
Cyril Hrubis
[email protected]
#5 lore raw

PATCH v8 1/7 lib6: Remove runcc.* and the testcases that use it.

---
 runtest/ipv6_lib                  |   4 -
 testcases/network/.gitignore      |   4 -
 testcases/network/lib6/Makefile   |   4 -
 testcases/network/lib6/asapi_01.c | 251 ----------------------------
 testcases/network/lib6/asapi_02.c | 338 --------------------------------------
 testcases/network/lib6/asapi_03.c | 234 --------------------------
 testcases/network/lib6/asapi_05.c | 119 +-------------
 testcases/network/lib6/asapi_07.c | 144 ----------------
 testcases/network/lib6/runcc.c    | 209 -----------------------
 testcases/network/lib6/runcc.h    |  30 ----
 10 files changed, 1 insertion(+), 1336 deletions(-)
 delete mode 100644 testcases/network/lib6/asapi_01.c
 delete mode 100644 testcases/network/lib6/asapi_02.c
 delete mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_07.c
 delete mode 100644 testcases/network/lib6/runcc.c
 delete mode 100644 testcases/network/lib6/runcc.h
runtest/ipv6_lib +0 -4
index 75614fb..203e275 100644
@@ -2,10 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_01 asapi_01
-asapi_02 asapi_02
-asapi_03 asapi_03
 asapi_04 asapi_04
 asapi_05 asapi_05
 asapi_06 asapi_06
-asapi_07 asapi_07
testcases/network/.gitignore +0 -4
index aa6d4da..18b0203 100644
@@ -1,13 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_01
-/lib6/asapi_02
-/lib6/asapi_03
 /lib6/asapi_04
 /lib6/asapi_05
 /lib6/asapi_06
-/lib6/asapi_07
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/Makefile +0 -4
index e337938..e9fde3b 100644
@@ -31,8 +31,4 @@ LDLIBS				+= -lpthread
 
 CLEAN_TARGETS			+= $(LIB)
 
-FILTER_OUT_MAKE_TARGETS		:= runcc
-
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
-
-$(MAKE_TARGETS): %: %.o runcc.o
testcases/network/lib6/asapi_01.c deleted +0 -251

251 lines not on this page · raw message

testcases/network/lib6/asapi_02.c deleted +0 -338

338 lines not on this page · raw message

testcases/network/lib6/asapi_03.c deleted +0 -234

234 lines not on this page · raw message

testcases/network/lib6/asapi_05.c +1 -118

146 lines not on this page · raw message

testcases/network/lib6/asapi_07.c deleted +0 -144

144 lines not on this page · raw message

testcases/network/lib6/runcc.c deleted +0 -209

209 lines not on this page · raw message

testcases/network/lib6/runcc.h deleted +0 -30
deleted file mode 100644
index 4e2a0d5..0000000
@@ -1,30 +0,0 @@
-/*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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 2 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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-int aliascheck(char *tname, char *incl, char *structure, char *field,
-	char *dname);
-int funccheck(char *tname, char *incl, char *fname);
-int structcheck(char *tname, char *incl, char *structure, char *field,
-	char *offset, char *size);
-int valuecheck(char *tname, char *incl, char *dname, char *dval);
-
-#define IP6_H "#include <netinet/ip6.h>\n"
-#define IN_H "#include <netinet/in.h>\n"
-#define ICMP6_H "#include <netinet/icmp6.h>\n"
-#define SOCKET_H "#include <sys/types.h>\n#include <sys/socket.h>\n"
Signature · 1 line
1.9.3
#6 lore raw

PATCH v8 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

---
 include/safe_macros.h |   1 +
 include/safe_net.h    |  60 ++++++++++++++++
 lib/safe_net.c        | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 250 insertions(+)
 create mode 100644 include/safe_net.h
 create mode 100644 lib/safe_net.c
include/safe_macros.h +1 -0
index cd86931..e938f17 100644
@@ -28,6 +28,7 @@
 #include <dirent.h>
 
 #include "safe_stdio.h"
+#include "safe_net.h"
 
 char*	safe_basename(const char *file, const int lineno,
 	    void (*cleanup_fn)(void), char *path);
include/safe_net.h new file +60 -0

60 lines not on this page · raw message

lib/safe_net.c new file +189 -0

189 lines not on this page · raw message

Signature · 1 line
1.9.3
#7 lore raw

PATCH v8 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

Hi,

Quoted 55 lines · Zeng Linggang
lib/safe_net.c new file +55 -0
+#include <errno.h>
+#include "test.h"
+#include "safe_net.h"
+
+char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res,
+		    size_t len)
+{
+	char portstr[8];
+
+	switch (sa->sa_family) {
+
+	case AF_INET: {
+		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+
+		if (!inet_ntop(AF_INET, &sin->sin_addr, res, len))
+			return NULL;
+
+		if (ntohs(sin->sin_port) != 0) {
+			snprintf(portstr, sizeof(portstr), ":%d",
+				 ntohs(sin->sin_port));
+			strcat(res, portstr);
+		}
+
+		return res;
+	}
+
+	case AF_INET6: {
+		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+
+		res[0] = '[';
+		if (!inet_ntop(AF_INET6, &sin6->sin6_addr, res + 1, len - 1))
+			return NULL;
+
+		if (ntohs(sin6->sin6_port) != 0) {
+			snprintf(portstr, sizeof(portstr), "]:%d",
+				 ntohs(sin6->sin6_port));
+			strcat(res, portstr);
+			return res;
+		}
+
+		return res + 1;
+	}
+
+	case AF_UNIX: {
+		struct sockaddr_un *unp = (struct sockaddr_un *)sa;
+
+		if (unp->sun_path[0] == '\0')
+			strcpy(res, "(no pathname bound)");
+		else
+			snprintf(res, len, "%s", unp->sun_path);
+
+		return res;
+	}
+
+	default: {

lib/safe_net.c line 83

Quoted 10 lines
lib/safe_net.c new file +10 -0
+		snprintf(res, len,
+			 "sock_ntop: unknown AF_xxx: %d, len: %d",
+			 sa->sa_family, salen);
+
+		return res;
+	}
+
+	}
+
+	return NULL;

The last return is redundant here.

Quoted 18 lines
lib/safe_net.c new file +18 -0
+}
+
+int safe_socket(const char *file, const int lineno, void (cleanup_fn)(void),
+		int domain, int type, int protocol)
+{
+	int rval;
+
+	rval = socket(domain, type, protocol);
+
+	if (rval < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: socket(%d, %d, %d) failed", file, lineno,
+			 domain, type, protocol);
+	}
+
+	return rval;
+}
+

lib/safe_net.c line 111

Quoted 10 lines
lib/safe_net.c new file +10 -0
+int safe_bind(const char *file, const int lineno, void (cleanup_fn)(void),
+	      int socket, const struct sockaddr *address,
+	      socklen_t address_len)
+{
+	int i;
+	char buf[128];
+
+	for (i = 0; i < 120; i++) {
+		if (!bind(socket, address, address_len)) {
+			tst_resm(TINFO, "bind is OK now");

I doubt that we need to print "bind is OK now"... the other library functions don't print anything on success.

Otherwise it looks good.

Also, could you please send a patch that renames asapi_04/05/06 to asapi_01/02/03?

Best regards,
Alexey

#8 lore raw

PATCH v8 2/7 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

Hi,

Quoted 61 lines · Alexey Kodanev

Hi,

Quoted 59 lines · Zeng Linggang
lib/safe_net.c new file +59 -0
+#include <errno.h>
+#include "test.h"
+#include "safe_net.h"
+
+char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res,
+		    size_t len)
+{
+	char portstr[8];
+
+	switch (sa->sa_family) {
+
+	case AF_INET: {
+		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+
+		if (!inet_ntop(AF_INET, &sin->sin_addr, res, len))
+			return NULL;
+
+		if (ntohs(sin->sin_port) != 0) {
+			snprintf(portstr, sizeof(portstr), ":%d",
+				 ntohs(sin->sin_port));
+			strcat(res, portstr);
+		}
+
+		return res;
+	}
+
+	case AF_INET6: {
+		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+
+		res[0] = '[';
+		if (!inet_ntop(AF_INET6, &sin6->sin6_addr, res + 1, len - 1))
+			return NULL;
+
+		if (ntohs(sin6->sin6_port) != 0) {
+			snprintf(portstr, sizeof(portstr), "]:%d",
+				 ntohs(sin6->sin6_port));
+			strcat(res, portstr);
+			return res;
+		}
+
+		return res + 1;
+	}
+
+	case AF_UNIX: {
+		struct sockaddr_un *unp = (struct sockaddr_un *)sa;
+
+		if (unp->sun_path[0] == '\0')
+			strcpy(res, "(no pathname bound)");
+		else
+			snprintf(res, len, "%s", unp->sun_path);
+
+		return res;
+	}
+
+	default: {
+		snprintf(res, len,
+			 "sock_ntop: unknown AF_xxx: %d, len: %d",
+			 sa->sa_family, salen);
+
Quoted 9 lines

lib/safe_net.c line 83

Quoted 6 lines
lib/safe_net.c new file +6 -0
+		return res;
+	}
+
+	}
+
+	return NULL;

The last return is redundant here.

Yep.

Quoted 23 lines
Quoted 23 lines
lib/safe_net.c new file +23 -0
+}
+
+int safe_socket(const char *file, const int lineno, void (cleanup_fn)(void),
+		int domain, int type, int protocol)
+{
+	int rval;
+
+	rval = socket(domain, type, protocol);
+
+	if (rval < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: socket(%d, %d, %d) failed", file, lineno,
+			 domain, type, protocol);
+	}
+
+	return rval;
+}
+
+int safe_bind(const char *file, const int lineno, void (cleanup_fn)(void),
+	      int socket, const struct sockaddr *address,
+	      socklen_t address_len)
+{
+	int i;
Quoted 8 lines

lib/safe_net.c line 111

Quoted 5 lines
lib/safe_net.c new file +5 -0
+	char buf[128];
+
+	for (i = 0; i < 120; i++) {
+		if (!bind(socket, address, address_len)) {
+			tst_resm(TINFO, "bind is OK now");

I doubt that we need to print "bind is OK now"... the other library functions don't print anything on success.

"Silence is golden" :)

Quoted 5 lines

Otherwise it looks good.

Also, could you please send a patch that renames asapi_04/05/06 to asapi_01/02/03?

Hmm, OK. I will rename them in the next version(V9).

Thank you very much.

Best regards,
Zeng

Quoted 4 lines

Best regards,
Alexey

#9 lore raw

PATCH v9 1/8 lib6: Remove runcc.* and the testcases that use it.

In reply to Alexey Kodanev
---
 runtest/ipv6_lib                  |   4 -
 testcases/network/.gitignore      |   4 -
 testcases/network/lib6/Makefile   |   4 -
 testcases/network/lib6/asapi_01.c | 251 ----------------------------
 testcases/network/lib6/asapi_02.c | 338 --------------------------------------
 testcases/network/lib6/asapi_03.c | 234 --------------------------
 testcases/network/lib6/asapi_05.c | 119 +-------------
 testcases/network/lib6/asapi_07.c | 144 ----------------
 testcases/network/lib6/runcc.c    | 209 -----------------------
 testcases/network/lib6/runcc.h    |  30 ----
 10 files changed, 1 insertion(+), 1336 deletions(-)
 delete mode 100644 testcases/network/lib6/asapi_01.c
 delete mode 100644 testcases/network/lib6/asapi_02.c
 delete mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_07.c
 delete mode 100644 testcases/network/lib6/runcc.c
 delete mode 100644 testcases/network/lib6/runcc.h
runtest/ipv6_lib +0 -4
index 75614fb..203e275 100644
@@ -2,10 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_01 asapi_01
-asapi_02 asapi_02
-asapi_03 asapi_03
 asapi_04 asapi_04
 asapi_05 asapi_05
 asapi_06 asapi_06
-asapi_07 asapi_07
testcases/network/.gitignore +0 -4
index aa6d4da..18b0203 100644
@@ -1,13 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_01
-/lib6/asapi_02
-/lib6/asapi_03
 /lib6/asapi_04
 /lib6/asapi_05
 /lib6/asapi_06
-/lib6/asapi_07
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/Makefile +0 -4
index e337938..e9fde3b 100644
@@ -31,8 +31,4 @@ LDLIBS				+= -lpthread
 
 CLEAN_TARGETS			+= $(LIB)
 
-FILTER_OUT_MAKE_TARGETS		:= runcc
-
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
-
-$(MAKE_TARGETS): %: %.o runcc.o
testcases/network/lib6/asapi_01.c deleted +0 -251

251 lines not on this page · raw message

testcases/network/lib6/asapi_02.c deleted +0 -338

338 lines not on this page · raw message

testcases/network/lib6/asapi_03.c deleted +0 -234

234 lines not on this page · raw message

testcases/network/lib6/asapi_05.c +1 -118

146 lines not on this page · raw message

testcases/network/lib6/asapi_07.c deleted +0 -144

144 lines not on this page · raw message

testcases/network/lib6/runcc.c deleted +0 -209

209 lines not on this page · raw message

testcases/network/lib6/runcc.h deleted +0 -30
deleted file mode 100644
index 4e2a0d5..0000000
@@ -1,30 +0,0 @@
-/*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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 2 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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-int aliascheck(char *tname, char *incl, char *structure, char *field,
-	char *dname);
-int funccheck(char *tname, char *incl, char *fname);
-int structcheck(char *tname, char *incl, char *structure, char *field,
-	char *offset, char *size);
-int valuecheck(char *tname, char *incl, char *dname, char *dval);
-
-#define IP6_H "#include <netinet/ip6.h>\n"
-#define IN_H "#include <netinet/in.h>\n"
-#define ICMP6_H "#include <netinet/icmp6.h>\n"
-#define SOCKET_H "#include <sys/types.h>\n#include <sys/socket.h>\n"
Signature · 1 line
1.9.3
#10 lore raw

PATCH v9 2/8 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

---
 include/safe_macros.h |   1 +
 include/safe_net.h    |  60 ++++++++++++++++
 lib/safe_net.c        | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 246 insertions(+)
 create mode 100644 include/safe_net.h
 create mode 100644 lib/safe_net.c
include/safe_macros.h +1 -0
index cd86931..e938f17 100644
@@ -28,6 +28,7 @@
 #include <dirent.h>
 
 #include "safe_stdio.h"
+#include "safe_net.h"
 
 char*	safe_basename(const char *file, const int lineno,
 	    void (*cleanup_fn)(void), char *path);
include/safe_net.h new file +60 -0

60 lines not on this page · raw message

lib/safe_net.c new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
1.9.3
#11 lore raw

PATCH v9 3/8 lib6/asapi_04.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of ptab[] and cstab[].
* Use 'pthread_join()' instead of 'sem_*'.
* Rewrite 'csum_test()' and 'isocketpair()'.
* Remove cleanup().
* Some cleanup.

---
 testcases/network/lib6/asapi_04.c | 301 +++++++++++++++-----------------------
 1 file changed, 117 insertions(+), 184 deletions(-)
testcases/network/lib6/asapi_04.c +117 -184
index 76de26b..df26494 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_04
- *
- * Test Description:
- *  Verify that in6 and sockaddr fields are present. Most of these are
- *  "PASS" if they just compile.
- *
- * Usage:  <for command-line>
- *  asapi_04
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -48,33 +31,33 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_04";	/* Test program identifier.    */
+char *TCID = "asapi_04";
 
-pid_t pid;
+static pid_t pid;
 
-struct {
+static struct {
 	char *prt_name;
 	int prt_value;
 } ptab[] = {
-	{
-	"hopopt", 0}, {
-	"ipv6", 41}, {
-	"ipv6-route", 43}, {
-	"ipv6-frag", 44}, {
-	"esp", 50}, {
-	"ah", 51}, {
-	"ipv6-icmp", 58}, {
-	"ipv6-nonxt", 59}, {
-"ipv6-opts", 60},};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
+	{"hopopt", 0},
+	{"ipv6", 41},
+	{"ipv6-route", 43},
+	{"ipv6-frag", 44},
+	{"esp", 50},
+	{"ah", 51},
+	{"ipv6-icmp", 58},
+	{"ipv6-nonxt", 59},
+	{"ipv6-opts", 60},
+};
 
-#define READ_TIMEOUT	5	/* secs */
+#define PTCOUNT		ARRAY_SIZE(ptab)
+#define READ_TIMEOUT	5
 
-void do_tests(void);
-void setup(void), cleanup(void);
-int csum_test(char *rhost);
+static void do_tests(void);
+static void setup(void);
+static void csum_test(void);
 
 int main(int argc, char *argv[])
 {
@@ -82,52 +65,48 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, 0, 0);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
-
 	tst_exit();
 }
 
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 /* RFC 3542, Section 2.3 */
 #ifndef IN6_ARE_ADDR_EQUAL
-	tst_resm(TBROK, "IN6_ARE_ADDR_EQUAL not present");
+	tst_resm(TCONF, "IN6_ARE_ADDR_EQUAL not present");
 #else /* IN6_ARE_ADDR_EQUAL */
 	/*
 	 * set each bit in an address and check for unequal; then set
 	 * in the second address and check for equal. Covers all bits, all
 	 * combinations.
 	 */
-	{
-		struct in6_addr a1, a2;
-		int word, bit;
-		int rv = 1;
+	struct in6_addr a1, a2;
+	int word, bit;
+	int rv = 1;
 
-		memset(&a1, 0, sizeof(a1));
-		memset(&a2, 0, sizeof(a2));
+	memset(&a1, 0, sizeof(a1));
+	memset(&a2, 0, sizeof(a2));
 
-		rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
+	rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
 
-		for (word = 0; word < 4; ++word)
-			for (bit = 0; bit < 32; ++bit) {
-				uint32_t newbit = 1 << bit;
+	for (word = 0; word < 4; ++word) {
+		for (bit = 0; bit < 32; ++bit) {
+			uint32_t newbit = 1U << bit;
 
-				a1.s6_addr32[word] |= newbit;	/* unequal */
-				rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
-				a2.s6_addr32[word] |= newbit;	/* equal */
-				rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
-			}
-		tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
+			a1.s6_addr32[word] |= newbit;
+			rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
+			a2.s6_addr32[word] |= newbit;
+			rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
+		}
 	}
+
+	tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
 #endif /* IN6_ARE_ADDR_EQUAL */
 
 /* RFC 3542, Section 2.4 */
@@ -141,7 +120,7 @@ void do_tests(void)
 			 ptab[i].prt_name);
 	}
 /* RFC 3542, Section 3.1 */
-	csum_test("::1");
+	csum_test();
 }
 
 /*
@@ -162,10 +141,10 @@ struct tprot {
 	unsigned char tp_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct tprot) + 2048];
-unsigned char rpbuf[sizeof(struct tprot) + 2048];
+static unsigned char tpbuf[sizeof(struct tprot) + 2048];
+static unsigned char rpbuf[sizeof(struct tprot) + 2048];
 
-struct csent {
+static struct csent {
 	int cs_offset;
 	int cs_dlen;
 	int cs_setresult;	/* setsockopt expected result */
@@ -173,25 +152,24 @@ struct csent {
 	int cs_sndresult;	/* send expected result */
 	int cs_snderrno;	/* send expected errno */
 } cstab[] = {
-	{
-	0, 5, 0, 0, 0, 0}, {
-	6, 30, 0, 0, 0, 0}, {
-	3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
-	{
-	4, 5, 0, 0, -1, EINVAL},	/* not enough space */
-	{
-	50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
-	{
-	22, 30, 0, 0, 0, 0}, {
-	2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
+	{0, 5, 0, 0, 0, 0},
+	{6, 30, 0, 0, 0, 0},
+	{3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
+	{4, 5, 0, 0, -1, EINVAL},	/* not enough space */
+	{50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
+	{22, 30, 0, 0, 0, 0},
+	{2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
 };
 
-#define CSCOUNT	(sizeof(cstab)/sizeof(cstab[0]))
+#define CSCOUNT	ARRAY_SIZE(cstab)
 
-static int recvtprot(int sd, unsigned char *packet, int psize)
+int TST_TOTAL = PTCOUNT + CSCOUNT;
+
+static int recvtprot(int sd, unsigned char *packet)
 {
 	struct tprot *tpt;
-	int cc, total, expected;
+	int cc;
+	unsigned int total, expected;
 	int gothead;
 
 	tpt = (struct tprot *)packet;
@@ -210,15 +188,17 @@ static int recvtprot(int sd, unsigned char *packet, int psize)
 				gothead = 1;
 				expected += ntohl(tpt->tp_dlen);
 			}
-		} else
+		} else {
 			break;
+		}
 	}
 	if (cc < 0)
 		return cc;
 	return total;
 }
 
-unsigned short csum(unsigned short partial, unsigned char *packet, int len)
+static unsigned short csum(unsigned short partial, unsigned char *packet,
+			   int len)
 {
 	unsigned long sum = partial;
 	unsigned short *ps;
@@ -234,7 +214,7 @@ unsigned short csum(unsigned short partial, unsigned char *packet, int len)
 	return ~sum;
 }
 
-struct ph {
+static struct ph {
 	struct in6_addr ph_sa;
 	struct in6_addr ph_da;
 	uint32_t ph_len;
@@ -242,13 +222,14 @@ struct ph {
 	uint8_t ph_nh;
 } ph;
 
-static int client(int prot, int sfd)
+static int client(int sfd)
 {
 	struct tprot *pttp = (struct tprot *)tpbuf;
 	struct tprot *prtp = (struct tprot *)rpbuf;
 	struct sockaddr_in6 rsin6;
 	static int seq;
-	int i, sd, cc, cs;
+	unsigned int i;
+	int sd, cc, cs;
 
 	memset(&rsin6, 0, sizeof(rsin6));
 	rsin6.sin6_family = AF_INET6;
@@ -259,11 +240,8 @@ static int client(int prot, int sfd)
 	ph.ph_da = rsin6.sin6_addr;
 	ph.ph_nh = NH_TEST;
 
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		tst_resm(TBROK, "can't create raw socket: %s", strerror(errno));
-		return -1;
-	}
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	for (i = 0; i < CSCOUNT; ++i) {
 		int offset, len, xlen;
 		int rv;
@@ -322,9 +300,9 @@ static int client(int prot, int sfd)
 				 offset, len);
 			continue;
 		}
-		while ((cc = recvtprot(sfd, rpbuf, sizeof(rpbuf)))) {
-			if (htonl(prtp->tp_pid) == pid &&
-			    htonl(prtp->tp_seq) == seq)
+		while ((cc = recvtprot(sfd, rpbuf))) {
+			if (htonl(prtp->tp_pid) == (uint32_t)pid &&
+			    htonl(prtp->tp_seq) == (uint32_t)seq)
 				break;
 		}
 		rv = 1;
@@ -334,7 +312,7 @@ static int client(int prot, int sfd)
 				continue;
 			if (*p != 0xa5) {
 				tst_resm(TFAIL, "IPV6_CHECKSUM corrupt data "
-					 "0x%02x != 0xa5 at offset %d in packet",
+					 "0x%02x != 0xa5 at offset %zd in packet",
 					 *p, p - rpbuf);
 				rv = 0;
 				break;
@@ -356,99 +334,60 @@ static int client(int prot, int sfd)
 }
 
 static int listen_fd, connect_fd;
-sem_t ilsem;
 
-void *ilistener(void *arg)
+static void *ilistener(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	connect_fd = accept(listen_fd, 0, 0);
 	close(listen_fd);
-	sem_post(&ilsem);
 	return NULL;
 }
 
-int isocketpair(int pf, int type, int proto, int fd[2])
+static void isocketpair(int pf, int type, int proto, int fd[2])
 {
 	pthread_t thid;
 	struct sockaddr_in sin4;
 	socklen_t namelen;
 
-/* restrict to PF_INET for now */
-	if (pf != PF_INET) {
-		errno = EOPNOTSUPP;
-		return -1;
-	}
-	sem_init(&ilsem, 0, 0);
-	listen_fd = socket(pf, type, proto);
-	if (listen_fd < 0) {
-		perror("socket");
-		return -1;
-	}
+	listen_fd = SAFE_SOCKET(NULL, pf, type, proto);
+
 	memset(&sin4, 0, sizeof(sin4));
-	if (bind(listen_fd, (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("bind");
-		return -1;
-	}
-	if (listen(listen_fd, 10) < 0) {
-		perror("listen");
-		return -1;
-	}
+
+	SAFE_BIND(NULL, listen_fd, (struct sockaddr *)&sin4, sizeof(sin4));
+
+	SAFE_LISTEN(NULL, listen_fd, 10);
+
 	namelen = sizeof(sin4);
-	if (getsockname(listen_fd, (struct sockaddr *)&sin4, &namelen) < 0) {
-		perror("getsockname");
-		return -1;
-	}
-	if (pthread_create(&thid, 0, ilistener, 0) < 0) {
-		perror("pthread_create");
-		return -1;
-	}
+	SAFE_GETSOCKNAME(NULL, listen_fd, (struct sockaddr *)&sin4, &namelen);
+
+	if (pthread_create(&thid, 0, ilistener, 0) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "pthread_create error");
+
+	fd[0] = SAFE_SOCKET(NULL, pf, type, proto);
 
-	fd[0] = socket(pf, type, proto);
-	if (fd[0] < 0) {
-		perror("socket");
-		return -1;
-	}
 	sin4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	if (connect(fd[0], (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("connect");
-		return -1;
-	}
-	sem_wait(&ilsem);
+
+	SAFE_CONNECT(NULL, fd[0], (struct sockaddr *)&sin4, sizeof(sin4));
+
+	pthread_join(thid, NULL);
+
 	fd[1] = connect_fd;
-	sem_destroy(&ilsem);
-	return 0;
 }
 
 #ifndef MAX
 #define MAX(a, b) (@ >= (b) ? (a) : (b))
 #endif /* MAX */
 
-int csum_test(char *rhost)
+static void csum_test(void)
 {
 	fd_set rset, rset_save;
 	int csd[2];		/* control sockets */
 	int sd, nfds, maxfd, cc;
 	struct timeval tv;
 
-/* rhost == loopback, for now */
-	if (strcmp(rhost, "::1")) {
-		tst_resm(TBROK, "invalid rhost \"%s\"", rhost);
-		return -1;
-	}
-	if (isocketpair(PF_INET, SOCK_STREAM, 0, csd) < 0) {
-		tst_resm(TBROK, "socketpair: %s", strerror(errno));
-		return -1;
-	}
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		int saved_errno = errno;
-
-		if (errno == EPERM && geteuid())
-			tst_resm(TBROK, "IPV6_CHECKSUM tests must run as root");
-		else
-			tst_resm(TBROK, "All IPv6_CHECKSUM tests broken: "
-				 "socket: %s", strerror(saved_errno));
-		return -1;
-	}
+	isocketpair(PF_INET, SOCK_STREAM, 0, csd);
+
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	FD_ZERO(&rset_save);
 	FD_SET(sd, &rset_save);
 	FD_SET(csd[1], &rset_save);
@@ -457,16 +396,16 @@ int csum_test(char *rhost)
 
 	/* server socket set; now start the client */
 	switch (fork()) {
-	case 0:		/* child */
+	case 0:
 		close(csd[0]);
 		break;
 	case -1:
-		tst_resm(TBROK, "can't fork rserver");
-		return -1;
-	default:		/* parent */
+		tst_brkm(TBROK, NULL, "can't fork rserver");
+	default:
 		close(sd);
 		close(csd[1]);
-		return client(pid, csd[0]);
+		client(csd[0]);
+		return;
 	}
 
 	tv.tv_sec = READ_TIMEOUT;
@@ -478,7 +417,7 @@ int csum_test(char *rhost)
 			exit(0);
 		} else if (nfds == 0) {
 			fprintf(stderr, "server read timed out");
-			return -1;
+			return;
 		}
 		if (FD_ISSET(sd, &rset)) {
 			static char packet[2048];
@@ -499,9 +438,8 @@ int csum_test(char *rhost)
 			char buf[2048];
 
 			cc = read(csd[1], buf, sizeof(buf));
-			if (cc == 0) {
+			if (cc == 0)
 				exit(0);
-			}
 			if (cc < 0) {
 				perror("server read");
 				exit(1);
@@ -512,16 +450,11 @@ int csum_test(char *rhost)
 		tv.tv_sec = READ_TIMEOUT;
 		tv.tv_usec = 0;
 	}
-	return 0;
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
-}
+	TEST_PAUSE;
 
-void cleanup(void)
-{
+	pid = getpid();
 }
-
-int TST_TOTAL = PTCOUNT + CSCOUNT;
Signature · 1 line
1.9.3
#12 lore raw

PATCH v9 4/8 lib6/asapi_05.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of etab[] and ftab[].
* Some cleanup.

---
 testcases/network/lib6/asapi_05.c | 159 ++++++++++++++------------------------
 1 file changed, 60 insertions(+), 99 deletions(-)
testcases/network/lib6/asapi_05.c +60 -99
index e251eee..f8c30f1 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_05
- *
- * Test Description:
- *  These tests are for the "Advanced Sockets API" (RFC 3542)
- *  Verify that in6 and sockaddr fields are present.
- *
- * Usage:  <for command-line>
- *  asapi_05
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -47,86 +30,74 @@
 #include <netinet/icmp6.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_05";	/* Test program identifier.    */
+char *TCID = "asapi_05";
 
-void setup(void);
-void cleanup(void);
+static void setup(void);
 
-void icmp6_ft(void);
+static void icmp6_ft(void);
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		icmp6_ft();
-	}
-
-	cleanup();
 
 	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
+	TEST_PAUSE;
 	tst_require_root();
 }
 
-void cleanup(void)
-{
-}
-
-/*
- * This is for old, broken glibc-header icmp6_filter structure definitions.
- * If icmp6.h has struct icmp6_filter with field named "data" instead
- * of the standard "icmp_filt", uncomment this line.
- */
-/*#define icmp_filt	data */
-
-enum tt { T_WILLPASS, T_WILLBLOCK, T_SETPASS, T_SETBLOCK, T_SETPASSALL,
+enum tt {
+	T_WILLPASS,
+	T_WILLBLOCK,
+	T_SETPASS,
+	T_SETBLOCK,
+	T_SETPASSALL,
 	T_SETBLOCKALL
 };
-struct ftent {
-	char *ft_tname;		/* test name, for logging */
+
+static struct ftent {
+	char *ft_tname;			/* test name, for logging */
 	unsigned char ft_sndtype;	/* send type field */
 	unsigned char ft_flttype;	/* filter type field */
-	enum tt ft_test;	/* what macro to test */
-	int ft_expected;	/* packet should pass? */
+	enum tt ft_test;		/* what macro to test */
+	int ft_expected;		/* packet should pass? */
 } ftab[] = {
-	{
-	"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1}, {
-	"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1}, {
-	"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0}, {
-"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},};
-
-#define FTCOUNT	(sizeof(ftab)/sizeof(ftab[0]))
-
-int ic6_send1(char *tname, unsigned char type)
+	{"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1},
+	{"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1},
+	{"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0},
+	{"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},
+};
+
+#define FTCOUNT	ARRAY_SIZE(ftab)
+
+static int ic6_send1(char *tname, unsigned char type)
 {
 	struct sockaddr_in6 sin6;
 	struct icmp6_hdr ic6;
 	int s;
 
-	s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (s == -1) {
-		tst_resm(TBROK, "%s: socket failed", tname);
-		return 1;
-	}
+	s = SAFE_SOCKET(NULL, AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+
 	memset(&ic6, 0, sizeof(ic6));
 	ic6.icmp6_type = type;
 	ic6.icmp6_data32[0] = htonl(getpid());
@@ -142,7 +113,7 @@ int ic6_send1(char *tname, unsigned char type)
 	return 0;
 }
 
-int ic6_recv1(char *tname, int sall, int sf)
+static int ic6_recv1(char *tname, int sall, int sf)
 {
 	fd_set readfds, readfds_saved;
 	struct timeval tv;
@@ -185,7 +156,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotall = 1;
 		}
 		if (FD_ISSET(sf, &readfds)) {
@@ -196,7 +167,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotone = 1;
 		}
 		memcpy(&readfds, &readfds_saved, sizeof(readfds));
@@ -211,18 +182,14 @@ int ic6_recv1(char *tname, int sall, int sf)
 }
 
 /* functional tests */
-void icmp6_ft(void)
+static void icmp6_ft(void)
 {
 	struct icmp6_filter i6f;
 	int sall, sf;
-	int i;
+	unsigned int i;
+
+	sall = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
-	sall = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sall < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create sall socket");
-		return;
-	}
 	ICMP6_FILTER_SETPASSALL(&i6f);
 	if (setsockopt(sall, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f,
 		       sizeof(i6f)) < 0) {
@@ -230,12 +197,7 @@ void icmp6_ft(void)
 			 "setsockopt pass all ICMP6_FILTER failed");
 	}
 
-	sf = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sf < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create test socket");
-		return;
-	}
+	sf = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
 	int rv;
 
@@ -284,8 +246,9 @@ void icmp6_ft(void)
 			if (ic6_send1(ftab[i].ft_tname, ftab[i].ft_sndtype))
 				continue;
 			rv = ic6_recv1(ftab[i].ft_tname, sall, sf);
-		} else
+		} else {
 			rv = -1;
+		}
 
 		if (rv < 0)
 			continue;
@@ -296,5 +259,3 @@ void icmp6_ft(void)
 			tst_resm(TPASS, "%s", ftab[i].ft_tname);
 	}
 }
-
-int TST_TOTAL = FTCOUNT;
Signature · 1 line
1.9.3
#13 lore raw

PATCH v9 5/8 lib6/asapi_06.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Remove unused variables.
* Modify the format of sotab[] and cmtab[].
* Modify the initialization of sotab[].
* Remove IPV6_PKTINFO test. IPV6_PKTINFO is only implemented by setsockopt() not by getsockopt(). see:
https://github.com/torvalds/linux/commit/b24a2516d10751d7ed5afb58420df25370c9dffb
* Some cleanup.

---
 testcases/network/lib6/asapi_06.c | 473 ++++++++------------------------------
 1 file changed, 99 insertions(+), 374 deletions(-)
testcases/network/lib6/asapi_06.c +99 -374
index 960ebb5..c6e2c8e 100644
@@ -1,37 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_06
- *
- * Test Description:
- *  Tests for RFC 3542 section 4 socket options and ancillary data
- *
- * Usage:  <for command-line>
- *  asapi_06
- *
- * HISTORY
- *	05/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include "config.h"
@@ -57,25 +41,16 @@
 #include <arpa/inet.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_06";	/* Test program identifier.    */
+char *TCID = "asapi_06";
 
 int TST_TOTAL = 1;
 
-pid_t pid;
-
-struct {
-	char *prt_name;
-	int prt_value;
-} ptab[] = {
-};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
-
 #define READ_TIMEOUT	5	/* secs */
 
-void do_tests(void);
-void setup(void), cleanup(void);
+static void do_tests(void);
+static void setup(void);
 
 int main(int argc, char *argv[])
 {
@@ -83,14 +58,11 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
 	tst_exit();
 }
 
@@ -150,7 +122,7 @@ union soval {
 #define IN6_ANY		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 
 /* so_clrval and so_setval members are initilized in the body */
-struct soent {
+static struct soent {
 	char *so_tname;
 	int so_opt;
 	int so_dorecv;		/* do receive test? */
@@ -160,73 +132,52 @@ struct soent {
 	union soval so_setval;
 	socklen_t so_valsize;
 } sotab[] = {
-/* RFC 3542, Section 4 */
-	{
-		"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* make sure TCLASS stays when setting another opt */
-	{
-		"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* OLD values */
-	{
-		"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-0}}}}}, sizeof(int)},};
-
-#define SOCOUNT	(sizeof(sotab)/sizeof(sotab[0]))
+	/* RFC 3542, Section 4 */
+	{"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* make sure TCLASS stays when setting another opt */
+	{"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* OLD values */
+	{"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+};
+
+#define SOCOUNT	ARRAY_SIZE(sotab)
 
 struct soprot {
-	int sop_pid;		/* sender PID */
-	int sop_seq;		/* sequence # */
-	int sop_dlen;		/* tp_dat length */
+	int sop_pid;			/* sender PID */
+	int sop_seq;			/* sequence # */
+	int sop_dlen;			/* tp_dat length */
 	unsigned char sop_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct soprot) + 2048];
-unsigned char rpbuf[sizeof(struct soprot) + 2048];
+static unsigned char tpbuf[sizeof(struct soprot) + 2048];
+static unsigned char rpbuf[sizeof(struct soprot) + 2048];
 
-unsigned char control[2048];
-int clen;
+static unsigned char control[2048];
 
-int seq;
+static int seq;
 
-int setupso(void)
-{
-/* add routing headers, other ancillary data here */
-	return 0;
-}
-
-struct cme {
+static struct cme {
 	int cm_len;
 	int cm_level;
 	int cm_type;
@@ -235,26 +186,21 @@ struct cme {
 		uint32_t cmu_hops;
 	} cmu;
 } cmtab[] = {
-	{
-		sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {
-		0x12}
-	}
-	, {
-		sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {
-		0x21}
-	}
-,};
+	{sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {0x12} },
+	{sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {0x21} },
+};
 
-#define CMCOUNT	(sizeof(cmtab)/sizeof(cmtab[0]))
+#define CMCOUNT	ARRAY_SIZE(cmtab)
 
-ssize_t sendall(int st)
+static ssize_t sendall(int st)
 {
 	struct sockaddr_in6 sin6;
 	struct msghdr msg;
 	struct iovec iov;
 	struct soprot *psop;
 	unsigned char *pd;
-	int i, ctotal;
+	unsigned int i;
+	int ctotal;
 
 	psop = (struct soprot *)tpbuf;
 	psop->sop_pid = htonl(getpid());
@@ -291,7 +237,7 @@ ssize_t sendall(int st)
 	return sendmsg(st, &msg, 0);
 }
 
-void so_test(struct soent *psoe)
+static void so_test(struct soent *psoe)
 {
 	struct sockaddr_in6 sin6;
 	union soval sobuf;
@@ -300,63 +246,51 @@ void so_test(struct soent *psoe)
 	int st;
 
 	if (psoe->so_opt == -1) {
-		tst_resm(TBROK, "%s not present at compile time",
+		tst_brkm(TBROK | TERRNO, NULL, "%s not present at compile time",
 			 psoe->so_tname);
-		return;
 	}
 	if (psoe->so_clear || sr < 0) {
 		if (sr < 0)
 			close(sr);
-		sr = socket(PF_INET6, SOCK_RAW, NH_TEST);
-		if (sr < 0) {
-			tst_resm(TBROK, "%s socket: %s", psoe->so_tname,
-				 strerror(errno));
-			return;
-		}
+		sr = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
 	}
 	memset(&sin6, 0, sizeof(sin6));
 	sin6.sin6_family = AF_INET6;
 	sin6.sin6_addr = in6addr_loopback;
-	if (bind(sr, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
-		tst_resm(TBROK, "%s: bind: %s", psoe->so_tname,
-			 strerror(errno));
-	}
+
+	SAFE_BIND(NULL, sr, (struct sockaddr *)&sin6, sizeof(sin6));
+
 	if (setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_clrval,
 		       psoe->so_valsize) < 0) {
-		tst_resm(TBROK, "%s: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
+		tst_brkm(TBROK | TERRNO, NULL, "%s: setsockopt",
+			 psoe->so_tname);
 	}
+
 	TEST(setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_setval,
 			psoe->so_valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL, "%s set-get: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
+		tst_resm(TFAIL | TERRNO, "%s set-get: setsockopt",
+			 psoe->so_tname);
 		return;
 	}
+
 	valsize = psoe->so_valsize;
 	TEST(getsockopt(sr, SOL_IPV6, psoe->so_opt, &sobuf, &valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TBROK, "%s set-get: getsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize))
+		tst_brkm(TBROK | TERRNO, NULL, "%s set-get: getsockopt",
+			 psoe->so_tname);
+	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize)) {
 		tst_resm(TFAIL, "%s set-get optval != setval", psoe->so_tname);
-	else
+	} else {
 		tst_resm(TPASS, "%s set-get", psoe->so_tname);
-
-	st = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (st < 0) {
-		tst_resm(TBROK, "%s transmit socket: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	}
-	if (sendall(st) < 0) {
-		tst_resm(TBROK, "%s transmit sendto: %s", psoe->so_tname,
-			 strerror(errno));
-		close(st);
-		return;
 	}
+
+	st = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
+	if (sendall(st) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "%s transmit sendto",
+			 psoe->so_tname);
+
 	close(st);
 
 	/* receiver processing */
@@ -382,12 +316,11 @@ void so_test(struct soent *psoe)
 			if (nfds < 0) {
 				if (errno == EINTR)
 					continue;
-				tst_resm(TBROK, "%s select: %s", psoe->so_tname,
-					 strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s select",
+					 psoe->so_tname);
 			}
 			if (nfds == 0) {
-				tst_resm(TBROK, "%s recvmsg timed out",
+				tst_brkm(TBROK, NULL, "%s recvmsg timed out",
 					 psoe->so_tname);
 				return;
 			}
@@ -405,11 +338,10 @@ void so_test(struct soent *psoe)
 
 			cc = recvmsg(sr, &msg, 0);
 			if (cc < 0) {
-				tst_resm(TBROK, "%s recvmsg: %s",
-					 psoe->so_tname, strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s recvmsg",
+					 psoe->so_tname);
 			}
-/* check pid & seq here */
+			/* check pid & seq here */
 			break;
 		}
 		gotone = 0;
@@ -419,9 +351,9 @@ void so_test(struct soent *psoe)
 				break;
 			gotone = pcmsg->cmsg_level == SOL_IPV6 &&
 			    pcmsg->cmsg_type == psoe->so_cmtype;
-			if (gotone)
+			if (gotone) {
 				break;
-			else if (psoe->so_clear) {
+			} else if (psoe->so_clear) {
 				tst_resm(TFAIL, "%s receive: extraneous data "
 					 "in control: level %d type %d len %zu",
 					 psoe->so_tname, pcmsg->cmsg_level,
@@ -429,232 +361,25 @@ void so_test(struct soent *psoe)
 				return;
 			}
 		}
-/* check contents here */
+		/* check contents here */
 		if (psoe->so_dorecv)
 			tst_resm(gotone ? TPASS : TFAIL, "%s receive",
 				 psoe->so_tname);
 	}
 }
 
-#define IPV6_ADDR_NODE		1
-#define IPV6_ADDR_LINK		2
-#define IPV6_ADDR_GLOBAL	3
-
-#ifdef HAVE_IFADDRS_H
-static int ipv6_addr_scope(struct in6_addr *pin6)
-{
-	if ((ntohl(pin6->s6_addr32[0]) & 0xFFC00000) == 0xFE800000)
-		return IPV6_ADDR_LINK;
-	if (memcmp(pin6, &in6addr_loopback, sizeof(*pin6)) == 0)
-		return IPV6_ADDR_NODE;
-	return IPV6_ADDR_GLOBAL;
-}
-#endif /* HAVE_IFADDRS_H */
-
-int getsock(char *tname, struct sockaddr_in6 *psin6_arg, int scope)
-{
-#ifdef HAVE_IFADDRS_H
-	static struct ifaddrs *pifa_head;
-	struct ifaddrs *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	int ifindex = 0;
-	int s;
-
-	if (!pifa_head && getifaddrs(&pifa_head)) {
-		tst_resm(TBROK, "%s: getifaddrs failed", tname);
-		return -1;
-	}
-	if (psin6_arg)
-		ifindex = psin6_arg->sin6_scope_id;
-
-	/* first, find a global address */
-	for (pifa = pifa_head; pifa; pifa = pifa->ifa_next) {
-		int this_scope;
-
-		if (!(pifa->ifa_flags & IFF_UP))
-			continue;
-		if (pifa->ifa_addr->sa_family != AF_INET6)
-			continue;
-		psin6 = (struct sockaddr_in6 *)pifa->ifa_addr;
-		this_scope = ipv6_addr_scope(&psin6->sin6_addr);
-		if (this_scope &&
-		    ((this_scope < 0 && -this_scope == scope) ||
-		     (this_scope > 0 && this_scope != scope)))
-			continue;
-		psin6->sin6_scope_id = if_nametoindex(pifa->ifa_name);
-		if ((ifindex < 0 && -ifindex == psin6->sin6_scope_id) ||
-		    (ifindex > 0 && ifindex != psin6->sin6_scope_id))
-			continue;
-		s = socket(PF_INET6, SOCK_DGRAM, 0);
-		if (s < 0) {
-			tst_resm(TBROK, "%s: socket %s", tname,
-				 strerror(errno));
-			return -1;
-		}
-		if (bind(s, pifa->ifa_addr, sizeof(struct sockaddr_in6)) < 0) {
-			tst_resm(TBROK, "%s: bind \"%s\": %s", tname,
-				 inet_ntop(AF_INET6, &psin6->sin6_addr, strbuf,
-					   sizeof(strbuf)), strerror(errno));
-			return -1;
-		}
-		if (psin6_arg) {
-			*psin6_arg = *psin6;
-			psin6_arg->sin6_scope_id =
-			    if_nametoindex(pifa->ifa_name);
-		}
-		return s;
-	}
-	{
-		char *scopestr, *intfstr;
-
-		switch (scope) {
-		case IPV6_ADDR_NODE:
-			scopestr = " node-local";
-			break;
-		case IPV6_ADDR_LINK:
-			scopestr = " link-local";
-			break;
-		case IPV6_ADDR_GLOBAL:
-			scopestr = " global";
-			break;
-		default:
-			scopestr = "";
-			break;
-		}
-		if (ifindex < 0) {
-			intfstr = " not on ifindex";
-			ifindex = -ifindex;
-		} else if (ifindex)
-			intfstr = " on ifindex";
-		else
-			intfstr = 0;
-
-		if (intfstr)
-			tst_resm(TBROK, "%s: getsock : no%s addresses%s %d",
-				 tname, scopestr, intfstr, ifindex);
-		else
-			tst_resm(TBROK, "%s: getsock : no%s addresses",
-				 tname, scopestr);
-	}
-	return -1;
-#else /* HAVE_IFADDRS_H */
-	return -1;
-#endif
-}
-
-#ifdef notyet
-/*
- * RFC 3542 IPV6_PKTINFO not in mainline yet (as of 2.6.15). The get/set
- * tests are below, and comments for some further tests to be added later
- */
-void test_pktinfo(void)
-{
-	int s_snd, s_rcv[3] = { -1, -1, -1 };
-	struct sockaddr_in6 sa_rcv[3];
-	int s, i;
-	struct ifaddrs *pifa_head, *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	char *tname = "IPV6_PKTINFO";
-	struct in6_pktinfo pi, pi_tmp;
-	int sinlen;
-	int optlen;
-
-	s_snd = getsock(tname, 0, IPV6_ADDR_GLOBAL);
-	if (s_snd < 0) {
-		tst_resm(TBROK, "%s: can't create send socket", tname);
-		return;
-	}
-	/* global-scope address, interface X */
-	sa_rcv[0].sin6_scope_id = 0;
-	s_rcv[0] = getsock(tname, &sa_rcv[0], IPV6_ADDR_GLOBAL);
-	if (s_rcv[0] == -1) {
-		tst_resm(TBROK, "%s: only link-scope addresses", tname);
-		return;
-	}
-	/* link-local-scope address, interface X */
-	sa_rcv[1].sin6_scope_id = sa_rcv[0].sin6_scope_id;
-	s_rcv[1] = getsock(tname, &sa_rcv[1], IPV6_ADDR_LINK);
-	if (s_rcv[1] < 0) {
-		tst_resm(TBROK, "%s: no link-local address on ifindex %d",
-			 tname, sa_rcv[0].sin6_scope_id);
-		return;
-	}
-	/* link-local-scope address, interface Y */
-	sa_rcv[2].sin6_scope_id = -sa_rcv[0].sin6_scope_id;
-	s_rcv[2] = getsock(tname, &sa_rcv[2], IPV6_ADDR_LINK);
-	if (s_rcv[2] < 0) {
-		tst_resm(TBROK, "%s: only one interface?", tname);
-		return;
-	}
-	/* send to rcv1 to verify communication */
-	/* force to rcv2 w/ PKTINFO */
-/* TESTS: */
-/* sticky set-get */
-	tname = "IPV6_PKTINFO set";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[1].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else
-		tst_resm(TPASS, "%s", tname);
-
-	tname = "IPV6_PKTINFO get";
-	optlen = sizeof(pi_tmp);
-	TEST(getsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi_tmp, &optlen));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else if (memcmp(&pi, &pi_tmp, sizeof(pi)) != 0) {
-		char strbuf2[64];
-		tst_resm(TFAIL, "%s: {\"%s\",%d} != {\"%s\",%d}", tname,
-			 inet_ntop(AF_INET6, &pi_tmp.ipi6_addr, strbuf,
-				   sizeof(strbuf)), pi_tmp.ipi6_ifindex,
-			 inet_ntop(AF_INET6, &pi.ipi6_addr, strbuf2,
-				   sizeof(strbuf2)), pi.ipi6_ifindex);
-	} else
-		tst_resm(TPASS, "%s", tname);
-/* ancillary data override */
-/* link-local, wrong interface */
-	tname = "IPV6_PKTINFO invalid {lladdr, intf}";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[2].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN == 0)
-		tst_resm(TFAIL, "%s returns success, should be -1, EINVAL",
-			 tname);
-	else if (TEST_ERRNO != EINVAL)
-		tst_resm(TFAIL, "%s errno %d != %d", tname, TEST_ERRNO, EINVAL);
-	else
-		tst_resm(TPASS, "%s", tname);
-/* nonexistent interface */
-/* non-local address */
-/* clear address */
-/* clear interface */
-/* sendmsg() sin6_scope differs with ancillary data interface */
-}
-#endif /* notyet */
-
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 	for (i = 0; i < SOCOUNT; ++i) {
 		sotab[i].so_clrval.sou_bool = 0;
 		sotab[i].so_setval.sou_bool = 1;
 		so_test(&sotab[i]);
 	}
-#ifdef notyet
-	test_pktinfo();
-#endif /* notyet - see test_pktinfo() comment above */
-}
-
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
 }
 
-void cleanup(void)
+static void setup(void)
 {
+	TEST_PAUSE;
 }
Signature · 1 line
1.9.3
#14 lore raw

PATCH v9 6/8 lib6/getaddrinfo_01.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* In every test function, using TINFO instead of TPASS marks the success of each inner test.
* Some cleanup.

---
 testcases/network/lib6/getaddrinfo_01.c | 503 ++++++++++++++++++--------------
 1 file changed, 281 insertions(+), 222 deletions(-)
testcases/network/lib6/getaddrinfo_01.c +281 -222
index ec29b5d..a812e77 100644
@@ -1,43 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: getaddrinfo_01
- *
- * Test Description:
- *  Tests for getaddrinfo library function
- *
- * Usage:  <for command-line>
- *  getaddrinfo_01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2004 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <unistd.h>
@@ -53,40 +31,38 @@
 #define AI_V4MAPPED    0x0008	/* IPv4 mapped addresses are acceptable.  */
 #endif
 
-char *TCID = "getaddrinfo_01";	/* Test program identifier.    */
-int testno;
+static void setup(void);
+static void gaiv4(void);
+static void gaiv6(void);
 
-void setup(void), cleanup(void);
-
-int TST_TOTAL = 1;
-
-/* a host that isn't where LTP is running */
-#define REMOTEHOSTNAME	"www.ibm.com"
-
-void gaiv4(void), gaiv6(void);
-void dumpres(struct addrinfo *);
+char *TCID = "getaddrinfo_01";
+int TST_TOTAL = 2;
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+
 		gaiv4();
 		gaiv6();
 	}
-	cleanup();
 
-	return (0);
+	tst_exit();
 }
 
-/* getaddrinfo tests (v4) */
+static void setup(void)
+{
+	TEST_PAUSE;
+}
 
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -132,13 +108,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 2, IPv4 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -152,20 +133,26 @@ void gaiv4(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 3, IPv4 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -200,13 +187,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 4, IPv4 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -244,18 +236,22 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv4 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 5, IPv4 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -266,8 +262,11 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -306,17 +305,28 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service,"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 0+service,i"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 7, IPv4 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -350,19 +360,30 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 8, IPv4 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -376,15 +397,18 @@ void gaiv4(void)
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
-	} else
-		tst_resm(TPASS,
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	} else {
+		tst_resm(TINFO,
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV");
+	}
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 9, IPv4 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -396,12 +420,13 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 10, IPv4 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = 0;
@@ -439,20 +464,31 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv4 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 11, IPv4 AI_V4MAPPED */
-
 	/* AI_V4MAPPED should be ignored because family != AF_INET6 */
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
@@ -482,18 +518,25 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
+
+
+	tst_resm(TPASS, "getaddrinfo tests (v4) succeed");
 }
 
 /* getaddrinfo tests (v6) */
-
-void gaiv6(void)
+static void gaiv6(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -539,13 +582,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 13, IPv6 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -559,20 +607,26 @@ void gaiv6(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 14, IPv6 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -607,13 +661,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 15, IPv6 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -652,18 +711,22 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv6 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 16, IPv6 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -674,8 +737,11 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -715,17 +781,26 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 18, IPv6 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -760,19 +835,28 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 19, IPv6 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -786,15 +870,17 @@ void gaiv6(void)
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS,
+		tst_resm(TINFO,
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 20, IPv6 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -806,12 +892,13 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 21, IPv6 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = 0;
@@ -850,20 +937,31 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv6 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 22, IPv6 AI_V4MAPPED */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_flags = AI_V4MAPPED;
@@ -892,57 +990,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
-}
-
-/* this prints an addrinfo list; useful for debugging */
-void dumpres(struct addrinfo *pai)
-{
-	int count = 1;
-	for (; pai; pai = pai->ai_next, count++) {
-		printf("result %d [0x%p]\n", count, pai);
-		printf("\tai_flags %x\n", pai->ai_flags);
-		printf("\tai_family %d\n", pai->ai_family);
-		printf("\tai_socktype %d\n", pai->ai_socktype);
-		printf("\tai_protocol %d\n", pai->ai_protocol);
-		printf("\tai_addrlen %d\n", pai->ai_addrlen);
-		printf("\tai_canonname \"%s\"\n", pai->ai_canonname);
-		printf("\tai_addr.sa_family %x\n", pai->ai_addr->sa_family);
-		if (pai->ai_addr->sa_family == AF_INET) {
-			char buf[1024];
-			struct sockaddr_in *psin =
-			    (struct sockaddr_in *)pai->ai_addr;
-
-			if (!inet_ntop(AF_INET, &psin->sin_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin_addr \"%s\"\n", buf);
-		} else if (pai->ai_addr->sa_family == AF_INET6) {
-			char buf[1024];
-
-			struct sockaddr_in6 *psin6 =
-			    (struct sockaddr_in6 *)pai->ai_addr;
-			if (!inet_ntop(AF_INET6, &psin6->sin6_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin6_addr \"%s\"\n", buf);
-
-		}
-		printf("\tai_next %p\n", pai->ai_next);
+		return;
 	}
-}
 
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
-}
-
-void cleanup(void)
-{
+	tst_resm(TPASS, "getaddrinfo tests (v6) succeed");
 }
Signature · 1 line
1.9.3
#15 lore raw

PATCH v9 6/8 lib6/getaddrinfo_01.c: Cleanup

Hi,

Quoted 6 lines · Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* In every test function, using TINFO instead of TPASS marks the success of each inner test.
* Some cleanup.

Sorry for delay, I was going to push the patch-set but encountered with a lot of warnings produced by checkpatch utility. Basically, they are all because of either "else is not generally usefull after break or return" or "quoted string split across lines". Then I noted that test-cases there have wrong structure, comments are below.

Also,
asapi_06.c: "commit description ... possible unwrapped" - you can trim commit id a little, it will still be accessable.

Quoted 19 lines
testcases/network/lib6/getaddrinfo_01.c +5 -6
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
  {
  	struct addrinfo *aires, hints, *pai;
  	char hostname[MAXHOSTNAMELEN + 1];
@@ -132,13 +108,18 @@ void gaiv4(void)
  				 psin ? psin->sin_family : 0,
  				 psin ? psin->sin_port : 0,
  				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {

testcases/network/lib6/getaddrinfo_01.c void gaiv4(void) · line 123

Quoted 10 lines
testcases/network/lib6/getaddrinfo_01.c +7 -0
+			tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
  			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
  			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 

If you write the description of the particular test-case before the test, you don't need to repeat the same description in each TPASS/TFAIL messages after. Test-case should end with either TPASS or TFAIL, not TINFO. Therefore all test-cases below could be something like this:

tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
TEST(...);

if (!TEST_RETURN) {
     if (error) {
         tst_resm(TFAIL, "");
         return;
     }

     tst_resm(TPASS, "test finished");
} else {
     tst_resm(TFAIL, "...");
}

Best regards,
Alexey

#16 lore raw

PATCH v9 6/8 lib6/getaddrinfo_01.c: Cleanup

Hello,

Quoted 15 lines · Alexey Kodanev

Hi,

Quoted 6 lines · Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* In every test function, using TINFO instead of TPASS marks the success of each inner test.
* Some cleanup.

Sorry for delay, I was going to push the patch-set but encountered with a lot of warnings produced by checkpatch utility. Basically, they are all because of either "else is not generally usefull after break or return" or "quoted string split across lines". Then I noted that test-cases there have wrong structure, comments are below.

Quoted 4 lines

Also,
asapi_06.c: "commit description ... possible unwrapped" - you can trim commit id a little, it will still be accessable.

Hmm, I found the 'checkpatch.pl' which I used is a little out of date, it did not output this ERROR.

Quoted 38 lines
Quoted 20 lines
testcases/network/lib6/getaddrinfo_01.c +5 -6
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
  {
  	struct addrinfo *aires, hints, *pai;
  	char hostname[MAXHOSTNAMELEN + 1];
@@ -132,13 +108,18 @@ void gaiv4(void)
  				 psin ? psin->sin_family : 0,
  				 psin ? psin->sin_port : 0,
  				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {

testcases/network/lib6/getaddrinfo_01.c void gaiv4(void) · line 123

Quoted 10 lines
testcases/network/lib6/getaddrinfo_01.c +7 -0
+			tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
  			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
  			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 

If you write the description of the particular test-case before the test, you don't need to repeat the same description in each TPASS/TFAIL messages after. Test-case should end with either TPASS or TFAIL, not TINFO. Therefore all test-cases below could be something like this:

tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
TEST(...);

Quoted 11 lines
if (!TEST_RETURN) {
     if (error) {
         tst_resm(TFAIL, "");
         return;
     }

     tst_resm(TPASS, "test finished");
} else {
     tst_resm(TFAIL, "...");
}

This is OK.

Thanks and best regards,
Zeng

Quoted 3 lines

Best regards,
Alexey

#17 lore raw

PATCH v10 1/8 lib6: Remove runcc.* and the testcases that use it.

In reply to Alexey Kodanev
---
 runtest/ipv6_lib                  |   4 -
 testcases/network/.gitignore      |   4 -
 testcases/network/lib6/Makefile   |   4 -
 testcases/network/lib6/asapi_01.c | 251 ----------------------------
 testcases/network/lib6/asapi_02.c | 338 --------------------------------------
 testcases/network/lib6/asapi_03.c | 234 --------------------------
 testcases/network/lib6/asapi_05.c | 119 +-------------
 testcases/network/lib6/asapi_07.c | 144 ----------------
 testcases/network/lib6/runcc.c    | 209 -----------------------
 testcases/network/lib6/runcc.h    |  30 ----
 10 files changed, 1 insertion(+), 1336 deletions(-)
 delete mode 100644 testcases/network/lib6/asapi_01.c
 delete mode 100644 testcases/network/lib6/asapi_02.c
 delete mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_07.c
 delete mode 100644 testcases/network/lib6/runcc.c
 delete mode 100644 testcases/network/lib6/runcc.h
runtest/ipv6_lib +0 -4
index 75614fb..203e275 100644
@@ -2,10 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_01 asapi_01
-asapi_02 asapi_02
-asapi_03 asapi_03
 asapi_04 asapi_04
 asapi_05 asapi_05
 asapi_06 asapi_06
-asapi_07 asapi_07
testcases/network/.gitignore +0 -4
index aa6d4da..18b0203 100644
@@ -1,13 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_01
-/lib6/asapi_02
-/lib6/asapi_03
 /lib6/asapi_04
 /lib6/asapi_05
 /lib6/asapi_06
-/lib6/asapi_07
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/Makefile +0 -4
index e337938..e9fde3b 100644
@@ -31,8 +31,4 @@ LDLIBS				+= -lpthread
 
 CLEAN_TARGETS			+= $(LIB)
 
-FILTER_OUT_MAKE_TARGETS		:= runcc
-
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
-
-$(MAKE_TARGETS): %: %.o runcc.o
testcases/network/lib6/asapi_01.c deleted +0 -251

251 lines not on this page · raw message

testcases/network/lib6/asapi_02.c deleted +0 -338

338 lines not on this page · raw message

testcases/network/lib6/asapi_03.c deleted +0 -234

234 lines not on this page · raw message

testcases/network/lib6/asapi_05.c +1 -118

146 lines not on this page · raw message

testcases/network/lib6/asapi_07.c deleted +0 -144

144 lines not on this page · raw message

testcases/network/lib6/runcc.c deleted +0 -209

209 lines not on this page · raw message

testcases/network/lib6/runcc.h deleted +0 -30
deleted file mode 100644
index 4e2a0d5..0000000
@@ -1,30 +0,0 @@
-/*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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 2 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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-int aliascheck(char *tname, char *incl, char *structure, char *field,
-	char *dname);
-int funccheck(char *tname, char *incl, char *fname);
-int structcheck(char *tname, char *incl, char *structure, char *field,
-	char *offset, char *size);
-int valuecheck(char *tname, char *incl, char *dname, char *dval);
-
-#define IP6_H "#include <netinet/ip6.h>\n"
-#define IN_H "#include <netinet/in.h>\n"
-#define ICMP6_H "#include <netinet/icmp6.h>\n"
-#define SOCKET_H "#include <sys/types.h>\n#include <sys/socket.h>\n"
Signature · 1 line
1.9.3
#18 lore raw

PATCH v10 2/8 SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()

---
 include/safe_macros.h |   1 +
 include/safe_net.h    |  60 ++++++++++++++++
 lib/safe_net.c        | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 246 insertions(+)
 create mode 100644 include/safe_net.h
 create mode 100644 lib/safe_net.c
include/safe_macros.h +1 -0
index cd86931..e938f17 100644
@@ -28,6 +28,7 @@
 #include <dirent.h>
 
 #include "safe_stdio.h"
+#include "safe_net.h"
 
 char*	safe_basename(const char *file, const int lineno,
 	    void (*cleanup_fn)(void), char *path);
include/safe_net.h new file +60 -0

60 lines not on this page · raw message

lib/safe_net.c new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
1.9.3
#19 lore raw

PATCH v10 3/8 lib6/asapi_04.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of ptab[] and cstab[].
* Use 'pthread_join()' instead of 'sem_*'.
* Rewrite 'csum_test()' and 'isocketpair()'.
* Remove cleanup().
* Some cleanup.

---
 testcases/network/lib6/asapi_04.c | 301 +++++++++++++++-----------------------
 1 file changed, 117 insertions(+), 184 deletions(-)
testcases/network/lib6/asapi_04.c +117 -184
index 76de26b..ff310ea 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_04
- *
- * Test Description:
- *  Verify that in6 and sockaddr fields are present. Most of these are
- *  "PASS" if they just compile.
- *
- * Usage:  <for command-line>
- *  asapi_04
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -48,33 +31,33 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_04";	/* Test program identifier.    */
+char *TCID = "asapi_04";
 
-pid_t pid;
+static pid_t pid;
 
-struct {
+static struct {
 	char *prt_name;
 	int prt_value;
 } ptab[] = {
-	{
-	"hopopt", 0}, {
-	"ipv6", 41}, {
-	"ipv6-route", 43}, {
-	"ipv6-frag", 44}, {
-	"esp", 50}, {
-	"ah", 51}, {
-	"ipv6-icmp", 58}, {
-	"ipv6-nonxt", 59}, {
-"ipv6-opts", 60},};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
+	{"hopopt", 0},
+	{"ipv6", 41},
+	{"ipv6-route", 43},
+	{"ipv6-frag", 44},
+	{"esp", 50},
+	{"ah", 51},
+	{"ipv6-icmp", 58},
+	{"ipv6-nonxt", 59},
+	{"ipv6-opts", 60},
+};
 
-#define READ_TIMEOUT	5	/* secs */
+#define PTCOUNT		ARRAY_SIZE(ptab)
+#define READ_TIMEOUT	5
 
-void do_tests(void);
-void setup(void), cleanup(void);
-int csum_test(char *rhost);
+static void do_tests(void);
+static void setup(void);
+static void csum_test(void);
 
 int main(int argc, char *argv[])
 {
@@ -82,52 +65,48 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, 0, 0);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
-
 	tst_exit();
 }
 
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 /* RFC 3542, Section 2.3 */
 #ifndef IN6_ARE_ADDR_EQUAL
-	tst_resm(TBROK, "IN6_ARE_ADDR_EQUAL not present");
+	tst_resm(TCONF, "IN6_ARE_ADDR_EQUAL not present");
 #else /* IN6_ARE_ADDR_EQUAL */
 	/*
 	 * set each bit in an address and check for unequal; then set
 	 * in the second address and check for equal. Covers all bits, all
 	 * combinations.
 	 */
-	{
-		struct in6_addr a1, a2;
-		int word, bit;
-		int rv = 1;
+	struct in6_addr a1, a2;
+	int word, bit;
+	int rv = 1;
 
-		memset(&a1, 0, sizeof(a1));
-		memset(&a2, 0, sizeof(a2));
+	memset(&a1, 0, sizeof(a1));
+	memset(&a2, 0, sizeof(a2));
 
-		rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
+	rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
 
-		for (word = 0; word < 4; ++word)
-			for (bit = 0; bit < 32; ++bit) {
-				uint32_t newbit = 1 << bit;
+	for (word = 0; word < 4; ++word) {
+		for (bit = 0; bit < 32; ++bit) {
+			uint32_t newbit = 1U << bit;
 
-				a1.s6_addr32[word] |= newbit;	/* unequal */
-				rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
-				a2.s6_addr32[word] |= newbit;	/* equal */
-				rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
-			}
-		tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
+			a1.s6_addr32[word] |= newbit;
+			rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
+			a2.s6_addr32[word] |= newbit;
+			rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
+		}
 	}
+
+	tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
 #endif /* IN6_ARE_ADDR_EQUAL */
 
 /* RFC 3542, Section 2.4 */
@@ -141,7 +120,7 @@ void do_tests(void)
 			 ptab[i].prt_name);
 	}
 /* RFC 3542, Section 3.1 */
-	csum_test("::1");
+	csum_test();
 }
 
 /*
@@ -162,10 +141,10 @@ struct tprot {
 	unsigned char tp_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct tprot) + 2048];
-unsigned char rpbuf[sizeof(struct tprot) + 2048];
+static unsigned char tpbuf[sizeof(struct tprot) + 2048];
+static unsigned char rpbuf[sizeof(struct tprot) + 2048];
 
-struct csent {
+static struct csent {
 	int cs_offset;
 	int cs_dlen;
 	int cs_setresult;	/* setsockopt expected result */
@@ -173,25 +152,24 @@ struct csent {
 	int cs_sndresult;	/* send expected result */
 	int cs_snderrno;	/* send expected errno */
 } cstab[] = {
-	{
-	0, 5, 0, 0, 0, 0}, {
-	6, 30, 0, 0, 0, 0}, {
-	3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
-	{
-	4, 5, 0, 0, -1, EINVAL},	/* not enough space */
-	{
-	50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
-	{
-	22, 30, 0, 0, 0, 0}, {
-	2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
+	{0, 5, 0, 0, 0, 0},
+	{6, 30, 0, 0, 0, 0},
+	{3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
+	{4, 5, 0, 0, -1, EINVAL},	/* not enough space */
+	{50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
+	{22, 30, 0, 0, 0, 0},
+	{2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
 };
 
-#define CSCOUNT	(sizeof(cstab)/sizeof(cstab[0]))
+#define CSCOUNT	ARRAY_SIZE(cstab)
 
-static int recvtprot(int sd, unsigned char *packet, int psize)
+int TST_TOTAL = PTCOUNT + CSCOUNT;
+
+static int recvtprot(int sd, unsigned char *packet)
 {
 	struct tprot *tpt;
-	int cc, total, expected;
+	int cc;
+	unsigned int total, expected;
 	int gothead;
 
 	tpt = (struct tprot *)packet;
@@ -210,15 +188,17 @@ static int recvtprot(int sd, unsigned char *packet, int psize)
 				gothead = 1;
 				expected += ntohl(tpt->tp_dlen);
 			}
-		} else
+		} else {
 			break;
+		}
 	}
 	if (cc < 0)
 		return cc;
 	return total;
 }
 
-unsigned short csum(unsigned short partial, unsigned char *packet, int len)
+static unsigned short csum(unsigned short partial, unsigned char *packet,
+			   int len)
 {
 	unsigned long sum = partial;
 	unsigned short *ps;
@@ -234,7 +214,7 @@ unsigned short csum(unsigned short partial, unsigned char *packet, int len)
 	return ~sum;
 }
 
-struct ph {
+static struct ph {
 	struct in6_addr ph_sa;
 	struct in6_addr ph_da;
 	uint32_t ph_len;
@@ -242,13 +222,14 @@ struct ph {
 	uint8_t ph_nh;
 } ph;
 
-static int client(int prot, int sfd)
+static int client(int sfd)
 {
 	struct tprot *pttp = (struct tprot *)tpbuf;
 	struct tprot *prtp = (struct tprot *)rpbuf;
 	struct sockaddr_in6 rsin6;
 	static int seq;
-	int i, sd, cc, cs;
+	unsigned int i;
+	int sd, cc, cs;
 
 	memset(&rsin6, 0, sizeof(rsin6));
 	rsin6.sin6_family = AF_INET6;
@@ -259,11 +240,8 @@ static int client(int prot, int sfd)
 	ph.ph_da = rsin6.sin6_addr;
 	ph.ph_nh = NH_TEST;
 
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		tst_resm(TBROK, "can't create raw socket: %s", strerror(errno));
-		return -1;
-	}
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	for (i = 0; i < CSCOUNT; ++i) {
 		int offset, len, xlen;
 		int rv;
@@ -322,9 +300,9 @@ static int client(int prot, int sfd)
 				 offset, len);
 			continue;
 		}
-		while ((cc = recvtprot(sfd, rpbuf, sizeof(rpbuf)))) {
-			if (htonl(prtp->tp_pid) == pid &&
-			    htonl(prtp->tp_seq) == seq)
+		while ((cc = recvtprot(sfd, rpbuf))) {
+			if (htonl(prtp->tp_pid) == (uint32_t)pid &&
+			    htonl(prtp->tp_seq) == (uint32_t)seq)
 				break;
 		}
 		rv = 1;
@@ -334,7 +312,7 @@ static int client(int prot, int sfd)
 				continue;
 			if (*p != 0xa5) {
 				tst_resm(TFAIL, "IPV6_CHECKSUM corrupt data "
-					 "0x%02x != 0xa5 at offset %d in packet",
+					 "0x%02x != 0xa5, offset %zd in packet",
 					 *p, p - rpbuf);
 				rv = 0;
 				break;
@@ -356,99 +334,60 @@ static int client(int prot, int sfd)
 }
 
 static int listen_fd, connect_fd;
-sem_t ilsem;
 
-void *ilistener(void *arg)
+static void *ilistener(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	connect_fd = accept(listen_fd, 0, 0);
 	close(listen_fd);
-	sem_post(&ilsem);
 	return NULL;
 }
 
-int isocketpair(int pf, int type, int proto, int fd[2])
+static void isocketpair(int pf, int type, int proto, int fd[2])
 {
 	pthread_t thid;
 	struct sockaddr_in sin4;
 	socklen_t namelen;
 
-/* restrict to PF_INET for now */
-	if (pf != PF_INET) {
-		errno = EOPNOTSUPP;
-		return -1;
-	}
-	sem_init(&ilsem, 0, 0);
-	listen_fd = socket(pf, type, proto);
-	if (listen_fd < 0) {
-		perror("socket");
-		return -1;
-	}
+	listen_fd = SAFE_SOCKET(NULL, pf, type, proto);
+
 	memset(&sin4, 0, sizeof(sin4));
-	if (bind(listen_fd, (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("bind");
-		return -1;
-	}
-	if (listen(listen_fd, 10) < 0) {
-		perror("listen");
-		return -1;
-	}
+
+	SAFE_BIND(NULL, listen_fd, (struct sockaddr *)&sin4, sizeof(sin4));
+
+	SAFE_LISTEN(NULL, listen_fd, 10);
+
 	namelen = sizeof(sin4);
-	if (getsockname(listen_fd, (struct sockaddr *)&sin4, &namelen) < 0) {
-		perror("getsockname");
-		return -1;
-	}
-	if (pthread_create(&thid, 0, ilistener, 0) < 0) {
-		perror("pthread_create");
-		return -1;
-	}
+	SAFE_GETSOCKNAME(NULL, listen_fd, (struct sockaddr *)&sin4, &namelen);
+
+	if (pthread_create(&thid, 0, ilistener, 0) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "pthread_create error");
+
+	fd[0] = SAFE_SOCKET(NULL, pf, type, proto);
 
-	fd[0] = socket(pf, type, proto);
-	if (fd[0] < 0) {
-		perror("socket");
-		return -1;
-	}
 	sin4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	if (connect(fd[0], (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("connect");
-		return -1;
-	}
-	sem_wait(&ilsem);
+
+	SAFE_CONNECT(NULL, fd[0], (struct sockaddr *)&sin4, sizeof(sin4));
+
+	pthread_join(thid, NULL);
+
 	fd[1] = connect_fd;
-	sem_destroy(&ilsem);
-	return 0;
 }
 
 #ifndef MAX
 #define MAX(a, b) (@ >= (b) ? (a) : (b))
 #endif /* MAX */
 
-int csum_test(char *rhost)
+static void csum_test(void)
 {
 	fd_set rset, rset_save;
 	int csd[2];		/* control sockets */
 	int sd, nfds, maxfd, cc;
 	struct timeval tv;
 
-/* rhost == loopback, for now */
-	if (strcmp(rhost, "::1")) {
-		tst_resm(TBROK, "invalid rhost \"%s\"", rhost);
-		return -1;
-	}
-	if (isocketpair(PF_INET, SOCK_STREAM, 0, csd) < 0) {
-		tst_resm(TBROK, "socketpair: %s", strerror(errno));
-		return -1;
-	}
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		int saved_errno = errno;
-
-		if (errno == EPERM && geteuid())
-			tst_resm(TBROK, "IPV6_CHECKSUM tests must run as root");
-		else
-			tst_resm(TBROK, "All IPv6_CHECKSUM tests broken: "
-				 "socket: %s", strerror(saved_errno));
-		return -1;
-	}
+	isocketpair(PF_INET, SOCK_STREAM, 0, csd);
+
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	FD_ZERO(&rset_save);
 	FD_SET(sd, &rset_save);
 	FD_SET(csd[1], &rset_save);
@@ -457,16 +396,16 @@ int csum_test(char *rhost)
 
 	/* server socket set; now start the client */
 	switch (fork()) {
-	case 0:		/* child */
+	case 0:
 		close(csd[0]);
 		break;
 	case -1:
-		tst_resm(TBROK, "can't fork rserver");
-		return -1;
-	default:		/* parent */
+		tst_brkm(TBROK, NULL, "can't fork rserver");
+	default:
 		close(sd);
 		close(csd[1]);
-		return client(pid, csd[0]);
+		client(csd[0]);
+		return;
 	}
 
 	tv.tv_sec = READ_TIMEOUT;
@@ -478,7 +417,7 @@ int csum_test(char *rhost)
 			exit(0);
 		} else if (nfds == 0) {
 			fprintf(stderr, "server read timed out");
-			return -1;
+			return;
 		}
 		if (FD_ISSET(sd, &rset)) {
 			static char packet[2048];
@@ -499,9 +438,8 @@ int csum_test(char *rhost)
 			char buf[2048];
 
 			cc = read(csd[1], buf, sizeof(buf));
-			if (cc == 0) {
+			if (cc == 0)
 				exit(0);
-			}
 			if (cc < 0) {
 				perror("server read");
 				exit(1);
@@ -512,16 +450,11 @@ int csum_test(char *rhost)
 		tv.tv_sec = READ_TIMEOUT;
 		tv.tv_usec = 0;
 	}
-	return 0;
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
-}
+	TEST_PAUSE;
 
-void cleanup(void)
-{
+	pid = getpid();
 }
-
-int TST_TOTAL = PTCOUNT + CSCOUNT;
Signature · 1 line
1.9.3
#20 lore raw

PATCH v10 4/8 lib6/asapi_05.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of etab[] and ftab[].
* Some cleanup.

---
 testcases/network/lib6/asapi_05.c | 159 ++++++++++++++------------------------
 1 file changed, 60 insertions(+), 99 deletions(-)
testcases/network/lib6/asapi_05.c +60 -99
index e251eee..f8c30f1 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_05
- *
- * Test Description:
- *  These tests are for the "Advanced Sockets API" (RFC 3542)
- *  Verify that in6 and sockaddr fields are present.
- *
- * Usage:  <for command-line>
- *  asapi_05
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -47,86 +30,74 @@
 #include <netinet/icmp6.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_05";	/* Test program identifier.    */
+char *TCID = "asapi_05";
 
-void setup(void);
-void cleanup(void);
+static void setup(void);
 
-void icmp6_ft(void);
+static void icmp6_ft(void);
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		icmp6_ft();
-	}
-
-	cleanup();
 
 	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
+	TEST_PAUSE;
 	tst_require_root();
 }
 
-void cleanup(void)
-{
-}
-
-/*
- * This is for old, broken glibc-header icmp6_filter structure definitions.
- * If icmp6.h has struct icmp6_filter with field named "data" instead
- * of the standard "icmp_filt", uncomment this line.
- */
-/*#define icmp_filt	data */
-
-enum tt { T_WILLPASS, T_WILLBLOCK, T_SETPASS, T_SETBLOCK, T_SETPASSALL,
+enum tt {
+	T_WILLPASS,
+	T_WILLBLOCK,
+	T_SETPASS,
+	T_SETBLOCK,
+	T_SETPASSALL,
 	T_SETBLOCKALL
 };
-struct ftent {
-	char *ft_tname;		/* test name, for logging */
+
+static struct ftent {
+	char *ft_tname;			/* test name, for logging */
 	unsigned char ft_sndtype;	/* send type field */
 	unsigned char ft_flttype;	/* filter type field */
-	enum tt ft_test;	/* what macro to test */
-	int ft_expected;	/* packet should pass? */
+	enum tt ft_test;		/* what macro to test */
+	int ft_expected;		/* packet should pass? */
 } ftab[] = {
-	{
-	"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1}, {
-	"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1}, {
-	"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0}, {
-"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},};
-
-#define FTCOUNT	(sizeof(ftab)/sizeof(ftab[0]))
-
-int ic6_send1(char *tname, unsigned char type)
+	{"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1},
+	{"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1},
+	{"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0},
+	{"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},
+};
+
+#define FTCOUNT	ARRAY_SIZE(ftab)
+
+static int ic6_send1(char *tname, unsigned char type)
 {
 	struct sockaddr_in6 sin6;
 	struct icmp6_hdr ic6;
 	int s;
 
-	s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (s == -1) {
-		tst_resm(TBROK, "%s: socket failed", tname);
-		return 1;
-	}
+	s = SAFE_SOCKET(NULL, AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+
 	memset(&ic6, 0, sizeof(ic6));
 	ic6.icmp6_type = type;
 	ic6.icmp6_data32[0] = htonl(getpid());
@@ -142,7 +113,7 @@ int ic6_send1(char *tname, unsigned char type)
 	return 0;
 }
 
-int ic6_recv1(char *tname, int sall, int sf)
+static int ic6_recv1(char *tname, int sall, int sf)
 {
 	fd_set readfds, readfds_saved;
 	struct timeval tv;
@@ -185,7 +156,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotall = 1;
 		}
 		if (FD_ISSET(sf, &readfds)) {
@@ -196,7 +167,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotone = 1;
 		}
 		memcpy(&readfds, &readfds_saved, sizeof(readfds));
@@ -211,18 +182,14 @@ int ic6_recv1(char *tname, int sall, int sf)
 }
 
 /* functional tests */
-void icmp6_ft(void)
+static void icmp6_ft(void)
 {
 	struct icmp6_filter i6f;
 	int sall, sf;
-	int i;
+	unsigned int i;
+
+	sall = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
-	sall = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sall < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create sall socket");
-		return;
-	}
 	ICMP6_FILTER_SETPASSALL(&i6f);
 	if (setsockopt(sall, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f,
 		       sizeof(i6f)) < 0) {
@@ -230,12 +197,7 @@ void icmp6_ft(void)
 			 "setsockopt pass all ICMP6_FILTER failed");
 	}
 
-	sf = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sf < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create test socket");
-		return;
-	}
+	sf = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
 	int rv;
 
@@ -284,8 +246,9 @@ void icmp6_ft(void)
 			if (ic6_send1(ftab[i].ft_tname, ftab[i].ft_sndtype))
 				continue;
 			rv = ic6_recv1(ftab[i].ft_tname, sall, sf);
-		} else
+		} else {
 			rv = -1;
+		}
 
 		if (rv < 0)
 			continue;
@@ -296,5 +259,3 @@ void icmp6_ft(void)
 			tst_resm(TPASS, "%s", ftab[i].ft_tname);
 	}
 }
-
-int TST_TOTAL = FTCOUNT;
Signature · 1 line
1.9.3
#21 lore raw

PATCH v10 5/8 lib6/asapi_06.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Remove unused variables.
* Modify the format of sotab[] and cmtab[].
* Modify the initialization of sotab[].
* Remove IPV6_PKTINFO test. IPV6_PKTINFO is only implemented by setsockopt() not by getsockopt(). see:
'commit b24a2516d107
("ipv6: Add IPV6_PKTINFO sticky option support to setsockopt()")' in Linux kernel.
* Some cleanup.

---
 testcases/network/lib6/asapi_06.c | 473 ++++++++------------------------------
 1 file changed, 99 insertions(+), 374 deletions(-)
testcases/network/lib6/asapi_06.c +99 -374
index 960ebb5..c6e2c8e 100644
@@ -1,37 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_06
- *
- * Test Description:
- *  Tests for RFC 3542 section 4 socket options and ancillary data
- *
- * Usage:  <for command-line>
- *  asapi_06
- *
- * HISTORY
- *	05/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include "config.h"
@@ -57,25 +41,16 @@
 #include <arpa/inet.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_06";	/* Test program identifier.    */
+char *TCID = "asapi_06";
 
 int TST_TOTAL = 1;
 
-pid_t pid;
-
-struct {
-	char *prt_name;
-	int prt_value;
-} ptab[] = {
-};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
-
 #define READ_TIMEOUT	5	/* secs */
 
-void do_tests(void);
-void setup(void), cleanup(void);
+static void do_tests(void);
+static void setup(void);
 
 int main(int argc, char *argv[])
 {
@@ -83,14 +58,11 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
 	tst_exit();
 }
 
@@ -150,7 +122,7 @@ union soval {
 #define IN6_ANY		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 
 /* so_clrval and so_setval members are initilized in the body */
-struct soent {
+static struct soent {
 	char *so_tname;
 	int so_opt;
 	int so_dorecv;		/* do receive test? */
@@ -160,73 +132,52 @@ struct soent {
 	union soval so_setval;
 	socklen_t so_valsize;
 } sotab[] = {
-/* RFC 3542, Section 4 */
-	{
-		"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* make sure TCLASS stays when setting another opt */
-	{
-		"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* OLD values */
-	{
-		"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-0}}}}}, sizeof(int)},};
-
-#define SOCOUNT	(sizeof(sotab)/sizeof(sotab[0]))
+	/* RFC 3542, Section 4 */
+	{"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* make sure TCLASS stays when setting another opt */
+	{"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* OLD values */
+	{"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+};
+
+#define SOCOUNT	ARRAY_SIZE(sotab)
 
 struct soprot {
-	int sop_pid;		/* sender PID */
-	int sop_seq;		/* sequence # */
-	int sop_dlen;		/* tp_dat length */
+	int sop_pid;			/* sender PID */
+	int sop_seq;			/* sequence # */
+	int sop_dlen;			/* tp_dat length */
 	unsigned char sop_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct soprot) + 2048];
-unsigned char rpbuf[sizeof(struct soprot) + 2048];
+static unsigned char tpbuf[sizeof(struct soprot) + 2048];
+static unsigned char rpbuf[sizeof(struct soprot) + 2048];
 
-unsigned char control[2048];
-int clen;
+static unsigned char control[2048];
 
-int seq;
+static int seq;
 
-int setupso(void)
-{
-/* add routing headers, other ancillary data here */
-	return 0;
-}
-
-struct cme {
+static struct cme {
 	int cm_len;
 	int cm_level;
 	int cm_type;
@@ -235,26 +186,21 @@ struct cme {
 		uint32_t cmu_hops;
 	} cmu;
 } cmtab[] = {
-	{
-		sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {
-		0x12}
-	}
-	, {
-		sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {
-		0x21}
-	}
-,};
+	{sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {0x12} },
+	{sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {0x21} },
+};
 
-#define CMCOUNT	(sizeof(cmtab)/sizeof(cmtab[0]))
+#define CMCOUNT	ARRAY_SIZE(cmtab)
 
-ssize_t sendall(int st)
+static ssize_t sendall(int st)
 {
 	struct sockaddr_in6 sin6;
 	struct msghdr msg;
 	struct iovec iov;
 	struct soprot *psop;
 	unsigned char *pd;
-	int i, ctotal;
+	unsigned int i;
+	int ctotal;
 
 	psop = (struct soprot *)tpbuf;
 	psop->sop_pid = htonl(getpid());
@@ -291,7 +237,7 @@ ssize_t sendall(int st)
 	return sendmsg(st, &msg, 0);
 }
 
-void so_test(struct soent *psoe)
+static void so_test(struct soent *psoe)
 {
 	struct sockaddr_in6 sin6;
 	union soval sobuf;
@@ -300,63 +246,51 @@ void so_test(struct soent *psoe)
 	int st;
 
 	if (psoe->so_opt == -1) {
-		tst_resm(TBROK, "%s not present at compile time",
+		tst_brkm(TBROK | TERRNO, NULL, "%s not present at compile time",
 			 psoe->so_tname);
-		return;
 	}
 	if (psoe->so_clear || sr < 0) {
 		if (sr < 0)
 			close(sr);
-		sr = socket(PF_INET6, SOCK_RAW, NH_TEST);
-		if (sr < 0) {
-			tst_resm(TBROK, "%s socket: %s", psoe->so_tname,
-				 strerror(errno));
-			return;
-		}
+		sr = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
 	}
 	memset(&sin6, 0, sizeof(sin6));
 	sin6.sin6_family = AF_INET6;
 	sin6.sin6_addr = in6addr_loopback;
-	if (bind(sr, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
-		tst_resm(TBROK, "%s: bind: %s", psoe->so_tname,
-			 strerror(errno));
-	}
+
+	SAFE_BIND(NULL, sr, (struct sockaddr *)&sin6, sizeof(sin6));
+
 	if (setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_clrval,
 		       psoe->so_valsize) < 0) {
-		tst_resm(TBROK, "%s: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
+		tst_brkm(TBROK | TERRNO, NULL, "%s: setsockopt",
+			 psoe->so_tname);
 	}
+
 	TEST(setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_setval,
 			psoe->so_valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL, "%s set-get: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
+		tst_resm(TFAIL | TERRNO, "%s set-get: setsockopt",
+			 psoe->so_tname);
 		return;
 	}
+
 	valsize = psoe->so_valsize;
 	TEST(getsockopt(sr, SOL_IPV6, psoe->so_opt, &sobuf, &valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TBROK, "%s set-get: getsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize))
+		tst_brkm(TBROK | TERRNO, NULL, "%s set-get: getsockopt",
+			 psoe->so_tname);
+	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize)) {
 		tst_resm(TFAIL, "%s set-get optval != setval", psoe->so_tname);
-	else
+	} else {
 		tst_resm(TPASS, "%s set-get", psoe->so_tname);
-
-	st = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (st < 0) {
-		tst_resm(TBROK, "%s transmit socket: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	}
-	if (sendall(st) < 0) {
-		tst_resm(TBROK, "%s transmit sendto: %s", psoe->so_tname,
-			 strerror(errno));
-		close(st);
-		return;
 	}
+
+	st = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
+	if (sendall(st) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "%s transmit sendto",
+			 psoe->so_tname);
+
 	close(st);
 
 	/* receiver processing */
@@ -382,12 +316,11 @@ void so_test(struct soent *psoe)
 			if (nfds < 0) {
 				if (errno == EINTR)
 					continue;
-				tst_resm(TBROK, "%s select: %s", psoe->so_tname,
-					 strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s select",
+					 psoe->so_tname);
 			}
 			if (nfds == 0) {
-				tst_resm(TBROK, "%s recvmsg timed out",
+				tst_brkm(TBROK, NULL, "%s recvmsg timed out",
 					 psoe->so_tname);
 				return;
 			}
@@ -405,11 +338,10 @@ void so_test(struct soent *psoe)
 
 			cc = recvmsg(sr, &msg, 0);
 			if (cc < 0) {
-				tst_resm(TBROK, "%s recvmsg: %s",
-					 psoe->so_tname, strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s recvmsg",
+					 psoe->so_tname);
 			}
-/* check pid & seq here */
+			/* check pid & seq here */
 			break;
 		}
 		gotone = 0;
@@ -419,9 +351,9 @@ void so_test(struct soent *psoe)
 				break;
 			gotone = pcmsg->cmsg_level == SOL_IPV6 &&
 			    pcmsg->cmsg_type == psoe->so_cmtype;
-			if (gotone)
+			if (gotone) {
 				break;
-			else if (psoe->so_clear) {
+			} else if (psoe->so_clear) {
 				tst_resm(TFAIL, "%s receive: extraneous data "
 					 "in control: level %d type %d len %zu",
 					 psoe->so_tname, pcmsg->cmsg_level,
@@ -429,232 +361,25 @@ void so_test(struct soent *psoe)
 				return;
 			}
 		}
-/* check contents here */
+		/* check contents here */
 		if (psoe->so_dorecv)
 			tst_resm(gotone ? TPASS : TFAIL, "%s receive",
 				 psoe->so_tname);
 	}
 }
 
-#define IPV6_ADDR_NODE		1
-#define IPV6_ADDR_LINK		2
-#define IPV6_ADDR_GLOBAL	3
-
-#ifdef HAVE_IFADDRS_H
-static int ipv6_addr_scope(struct in6_addr *pin6)
-{
-	if ((ntohl(pin6->s6_addr32[0]) & 0xFFC00000) == 0xFE800000)
-		return IPV6_ADDR_LINK;
-	if (memcmp(pin6, &in6addr_loopback, sizeof(*pin6)) == 0)
-		return IPV6_ADDR_NODE;
-	return IPV6_ADDR_GLOBAL;
-}
-#endif /* HAVE_IFADDRS_H */
-
-int getsock(char *tname, struct sockaddr_in6 *psin6_arg, int scope)
-{
-#ifdef HAVE_IFADDRS_H
-	static struct ifaddrs *pifa_head;
-	struct ifaddrs *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	int ifindex = 0;
-	int s;
-
-	if (!pifa_head && getifaddrs(&pifa_head)) {
-		tst_resm(TBROK, "%s: getifaddrs failed", tname);
-		return -1;
-	}
-	if (psin6_arg)
-		ifindex = psin6_arg->sin6_scope_id;
-
-	/* first, find a global address */
-	for (pifa = pifa_head; pifa; pifa = pifa->ifa_next) {
-		int this_scope;
-
-		if (!(pifa->ifa_flags & IFF_UP))
-			continue;
-		if (pifa->ifa_addr->sa_family != AF_INET6)
-			continue;
-		psin6 = (struct sockaddr_in6 *)pifa->ifa_addr;
-		this_scope = ipv6_addr_scope(&psin6->sin6_addr);
-		if (this_scope &&
-		    ((this_scope < 0 && -this_scope == scope) ||
-		     (this_scope > 0 && this_scope != scope)))
-			continue;
-		psin6->sin6_scope_id = if_nametoindex(pifa->ifa_name);
-		if ((ifindex < 0 && -ifindex == psin6->sin6_scope_id) ||
-		    (ifindex > 0 && ifindex != psin6->sin6_scope_id))
-			continue;
-		s = socket(PF_INET6, SOCK_DGRAM, 0);
-		if (s < 0) {
-			tst_resm(TBROK, "%s: socket %s", tname,
-				 strerror(errno));
-			return -1;
-		}
-		if (bind(s, pifa->ifa_addr, sizeof(struct sockaddr_in6)) < 0) {
-			tst_resm(TBROK, "%s: bind \"%s\": %s", tname,
-				 inet_ntop(AF_INET6, &psin6->sin6_addr, strbuf,
-					   sizeof(strbuf)), strerror(errno));
-			return -1;
-		}
-		if (psin6_arg) {
-			*psin6_arg = *psin6;
-			psin6_arg->sin6_scope_id =
-			    if_nametoindex(pifa->ifa_name);
-		}
-		return s;
-	}
-	{
-		char *scopestr, *intfstr;
-
-		switch (scope) {
-		case IPV6_ADDR_NODE:
-			scopestr = " node-local";
-			break;
-		case IPV6_ADDR_LINK:
-			scopestr = " link-local";
-			break;
-		case IPV6_ADDR_GLOBAL:
-			scopestr = " global";
-			break;
-		default:
-			scopestr = "";
-			break;
-		}
-		if (ifindex < 0) {
-			intfstr = " not on ifindex";
-			ifindex = -ifindex;
-		} else if (ifindex)
-			intfstr = " on ifindex";
-		else
-			intfstr = 0;
-
-		if (intfstr)
-			tst_resm(TBROK, "%s: getsock : no%s addresses%s %d",
-				 tname, scopestr, intfstr, ifindex);
-		else
-			tst_resm(TBROK, "%s: getsock : no%s addresses",
-				 tname, scopestr);
-	}
-	return -1;
-#else /* HAVE_IFADDRS_H */
-	return -1;
-#endif
-}
-
-#ifdef notyet
-/*
- * RFC 3542 IPV6_PKTINFO not in mainline yet (as of 2.6.15). The get/set
- * tests are below, and comments for some further tests to be added later
- */
-void test_pktinfo(void)
-{
-	int s_snd, s_rcv[3] = { -1, -1, -1 };
-	struct sockaddr_in6 sa_rcv[3];
-	int s, i;
-	struct ifaddrs *pifa_head, *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	char *tname = "IPV6_PKTINFO";
-	struct in6_pktinfo pi, pi_tmp;
-	int sinlen;
-	int optlen;
-
-	s_snd = getsock(tname, 0, IPV6_ADDR_GLOBAL);
-	if (s_snd < 0) {
-		tst_resm(TBROK, "%s: can't create send socket", tname);
-		return;
-	}
-	/* global-scope address, interface X */
-	sa_rcv[0].sin6_scope_id = 0;
-	s_rcv[0] = getsock(tname, &sa_rcv[0], IPV6_ADDR_GLOBAL);
-	if (s_rcv[0] == -1) {
-		tst_resm(TBROK, "%s: only link-scope addresses", tname);
-		return;
-	}
-	/* link-local-scope address, interface X */
-	sa_rcv[1].sin6_scope_id = sa_rcv[0].sin6_scope_id;
-	s_rcv[1] = getsock(tname, &sa_rcv[1], IPV6_ADDR_LINK);
-	if (s_rcv[1] < 0) {
-		tst_resm(TBROK, "%s: no link-local address on ifindex %d",
-			 tname, sa_rcv[0].sin6_scope_id);
-		return;
-	}
-	/* link-local-scope address, interface Y */
-	sa_rcv[2].sin6_scope_id = -sa_rcv[0].sin6_scope_id;
-	s_rcv[2] = getsock(tname, &sa_rcv[2], IPV6_ADDR_LINK);
-	if (s_rcv[2] < 0) {
-		tst_resm(TBROK, "%s: only one interface?", tname);
-		return;
-	}
-	/* send to rcv1 to verify communication */
-	/* force to rcv2 w/ PKTINFO */
-/* TESTS: */
-/* sticky set-get */
-	tname = "IPV6_PKTINFO set";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[1].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else
-		tst_resm(TPASS, "%s", tname);
-
-	tname = "IPV6_PKTINFO get";
-	optlen = sizeof(pi_tmp);
-	TEST(getsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi_tmp, &optlen));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else if (memcmp(&pi, &pi_tmp, sizeof(pi)) != 0) {
-		char strbuf2[64];
-		tst_resm(TFAIL, "%s: {\"%s\",%d} != {\"%s\",%d}", tname,
-			 inet_ntop(AF_INET6, &pi_tmp.ipi6_addr, strbuf,
-				   sizeof(strbuf)), pi_tmp.ipi6_ifindex,
-			 inet_ntop(AF_INET6, &pi.ipi6_addr, strbuf2,
-				   sizeof(strbuf2)), pi.ipi6_ifindex);
-	} else
-		tst_resm(TPASS, "%s", tname);
-/* ancillary data override */
-/* link-local, wrong interface */
-	tname = "IPV6_PKTINFO invalid {lladdr, intf}";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[2].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN == 0)
-		tst_resm(TFAIL, "%s returns success, should be -1, EINVAL",
-			 tname);
-	else if (TEST_ERRNO != EINVAL)
-		tst_resm(TFAIL, "%s errno %d != %d", tname, TEST_ERRNO, EINVAL);
-	else
-		tst_resm(TPASS, "%s", tname);
-/* nonexistent interface */
-/* non-local address */
-/* clear address */
-/* clear interface */
-/* sendmsg() sin6_scope differs with ancillary data interface */
-}
-#endif /* notyet */
-
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 	for (i = 0; i < SOCOUNT; ++i) {
 		sotab[i].so_clrval.sou_bool = 0;
 		sotab[i].so_setval.sou_bool = 1;
 		so_test(&sotab[i]);
 	}
-#ifdef notyet
-	test_pktinfo();
-#endif /* notyet - see test_pktinfo() comment above */
-}
-
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
 }
 
-void cleanup(void)
+static void setup(void)
 {
+	TEST_PAUSE;
 }
Signature · 1 line
1.9.3
#22 lore raw

PATCH v10 6/8 lib6/getaddrinfo_01.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* Some cleanup.

---
 testcases/network/lib6/getaddrinfo_01.c | 466 +++++++++++++++++---------------
 1 file changed, 248 insertions(+), 218 deletions(-)
testcases/network/lib6/getaddrinfo_01.c +248 -218
index ec29b5d..bbf8f77 100644
@@ -1,43 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: getaddrinfo_01
- *
- * Test Description:
- *  Tests for getaddrinfo library function
- *
- * Usage:  <for command-line>
- *  getaddrinfo_01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2004 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <unistd.h>
@@ -53,40 +31,38 @@
 #define AI_V4MAPPED    0x0008	/* IPv4 mapped addresses are acceptable.  */
 #endif
 
-char *TCID = "getaddrinfo_01";	/* Test program identifier.    */
-int testno;
-
-void setup(void), cleanup(void);
+static void setup(void);
+static void gaiv4(void);
+static void gaiv6(void);
 
-int TST_TOTAL = 1;
-
-/* a host that isn't where LTP is running */
-#define REMOTEHOSTNAME	"www.ibm.com"
-
-void gaiv4(void), gaiv6(void);
-void dumpres(struct addrinfo *);
+char *TCID = "getaddrinfo_01";
+int TST_TOTAL = 22;
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+
 		gaiv4();
 		gaiv6();
 	}
-	cleanup();
 
-	return (0);
+	tst_exit();
 }
 
-/* getaddrinfo tests (v4) */
+static void setup(void)
+{
+	TEST_PAUSE;
+}
 
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -96,8 +72,7 @@ void gaiv4(void)
 	char *p;
 
 	if (gethostname(hostname, sizeof(hostname)) < 0)
-		tst_brkm(TBROK, NULL, "gethostname failed - %s",
-			 strerror(errno));
+		tst_brkm(TBROK | TERRNO, NULL, "gethostname failed");
 	strncpy(shortname, hostname, MAXHOSTNAMELEN);
 	shortname[MAXHOSTNAMELEN] = '\0';
 	p = strchr(shortname, '.');
@@ -132,13 +107,17 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 2, IPv4 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -152,20 +131,25 @@ void gaiv4(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 canonical name");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 canonical name");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 3, IPv4 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -200,13 +184,17 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 host+service");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 host+service");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 4, IPv4 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -244,18 +232,20 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS,
-				 "getaddrinfo IPv4 host+service PASSIVE");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 host+service PASSIVE");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 5, IPv4 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -266,8 +256,11 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -306,17 +299,27 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service, PASSIVE");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 0+service, PASSIVE");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv4 0+service,"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 0+service,"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 7, IPv4 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -350,19 +353,29 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 0+service");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 8, IPv4 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -376,15 +389,16 @@ void gaiv4(void)
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
-	} else
-		tst_resm(TPASS,
-			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV");
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv4 host+service, AI_NUMERICSERV");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 9, IPv4 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -396,12 +410,11 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
-			 "hints");
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP hints");
 
 	/* test 10, IPv4 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = 0;
@@ -439,20 +452,31 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else if (got_tcp && got_udp)
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
 			tst_resm(TPASS, "getaddrinfo IPv4 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 11, IPv4 AI_V4MAPPED */
-
 	/* AI_V4MAPPED should be ignored because family != AF_INET6 */
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
@@ -482,18 +506,21 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 AI_V4MAPPED");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv4 AI_V4MAPPED");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 }
 
 /* getaddrinfo tests (v6) */
-
-void gaiv6(void)
+static void gaiv6(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -539,13 +566,17 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 basic lookup");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 basic lookup");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 basic "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 13, IPv6 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -559,20 +590,25 @@ void gaiv6(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 canonical name");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 canonical name");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 14, IPv6 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -607,13 +643,17 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 host+service");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 host+service");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 15, IPv6 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -652,18 +692,20 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS,
-				 "getaddrinfo IPv6 host+service PASSIVE");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 host+service PASSIVE");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 16, IPv6 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -674,8 +716,11 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -715,17 +760,25 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 18, IPv6 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -760,19 +813,27 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 0+service");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 19, IPv6 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -786,15 +847,16 @@ void gaiv6(void)
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
-	} else
-		tst_resm(TPASS,
-			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV");
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv6 host+service, AI_NUMERICSERV");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 20, IPv6 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -806,12 +868,11 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
-			 "hints");
+		return;
+	}
+	tst_resm(TPASS, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP hints");
 
 	/* test 21, IPv6 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = 0;
@@ -850,20 +911,31 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else if (got_tcp && got_udp)
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
 			tst_resm(TPASS, "getaddrinfo IPv6 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TPASS, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 22, IPv6 AI_V4MAPPED */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_flags = AI_V4MAPPED;
@@ -892,57 +964,15 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 AI_V4MAPPED");
+			freeaddrinfo(aires);
+			return;
+		}
+		tst_resm(TPASS, "getaddrinfo IPv6 AI_V4MAPPED");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
-}
-
-/* this prints an addrinfo list; useful for debugging */
-void dumpres(struct addrinfo *pai)
-{
-	int count = 1;
-	for (; pai; pai = pai->ai_next, count++) {
-		printf("result %d [0x%p]\n", count, pai);
-		printf("\tai_flags %x\n", pai->ai_flags);
-		printf("\tai_family %d\n", pai->ai_family);
-		printf("\tai_socktype %d\n", pai->ai_socktype);
-		printf("\tai_protocol %d\n", pai->ai_protocol);
-		printf("\tai_addrlen %d\n", pai->ai_addrlen);
-		printf("\tai_canonname \"%s\"\n", pai->ai_canonname);
-		printf("\tai_addr.sa_family %x\n", pai->ai_addr->sa_family);
-		if (pai->ai_addr->sa_family == AF_INET) {
-			char buf[1024];
-			struct sockaddr_in *psin =
-			    (struct sockaddr_in *)pai->ai_addr;
-
-			if (!inet_ntop(AF_INET, &psin->sin_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin_addr \"%s\"\n", buf);
-		} else if (pai->ai_addr->sa_family == AF_INET6) {
-			char buf[1024];
-
-			struct sockaddr_in6 *psin6 =
-			    (struct sockaddr_in6 *)pai->ai_addr;
-			if (!inet_ntop(AF_INET6, &psin6->sin6_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin6_addr \"%s\"\n", buf);
-
-		}
-		printf("\tai_next %p\n", pai->ai_next);
+		return;
 	}
 }
-
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
-}
-
-void cleanup(void)
-{
-}
Signature · 1 line
1.9.3
#23 lore raw

PATCH v10 7/8 lib6: Rename

In reply to Zeng Linggang
rename:    asapi_04.c -> asapi_01.c
rename:    asapi_05.c -> asapi_02.c
rename:    asapi_06.c -> asapi_03.c
---
 runtest/ipv6_lib                  |   6 +-
 testcases/network/.gitignore      |   6 +-
 testcases/network/lib6/asapi_01.c | 460 ++++++++++++++++++++++++++++++++++++++
 testcases/network/lib6/asapi_02.c | 261 +++++++++++++++++++++
 testcases/network/lib6/asapi_03.c | 385 +++++++++++++++++++++++++++++++
 testcases/network/lib6/asapi_04.c | 460 --------------------------------------
 testcases/network/lib6/asapi_05.c | 261 ---------------------
 testcases/network/lib6/asapi_06.c | 385 -------------------------------
 8 files changed, 1112 insertions(+), 1112 deletions(-)
 create mode 100644 testcases/network/lib6/asapi_01.c
 create mode 100644 testcases/network/lib6/asapi_02.c
 create mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_04.c
 delete mode 100644 testcases/network/lib6/asapi_05.c
 delete mode 100644 testcases/network/lib6/asapi_06.c
runtest/ipv6_lib +3 -3
index 203e275..636232b 100644
@@ -2,6 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_04 asapi_04
-asapi_05 asapi_05
-asapi_06 asapi_06
+asapi_01 asapi_01
+asapi_02 asapi_02
+asapi_03 asapi_03
testcases/network/.gitignore +3 -3
index 18b0203..77a350b 100644
@@ -1,9 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_04
-/lib6/asapi_05
-/lib6/asapi_06
+/lib6/asapi_01
+/lib6/asapi_02
+/lib6/asapi_03
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/asapi_01.c new file +460 -0

460 lines not on this page · raw message

testcases/network/lib6/asapi_02.c new file +261 -0

261 lines not on this page · raw message

testcases/network/lib6/asapi_03.c new file +385 -0

385 lines not on this page · raw message

testcases/network/lib6/asapi_04.c deleted +0 -460

460 lines not on this page · raw message

testcases/network/lib6/asapi_05.c deleted +0 -261

261 lines not on this page · raw message

testcases/network/lib6/asapi_06.c deleted +0 -385

385 lines not on this page · raw message

Signature · 1 line
1.9.3
#26 lore raw

PATCH v9 7/8 lib6: Rename

In reply to Zeng Linggang
rename:    asapi_04.c -> asapi_01.c
rename:    asapi_05.c -> asapi_02.c
rename:    asapi_06.c -> asapi_03.c
---
 runtest/ipv6_lib                  |   6 +-
 testcases/network/.gitignore      |   6 +-
 testcases/network/lib6/asapi_01.c | 460 ++++++++++++++++++++++++++++++++++++++
 testcases/network/lib6/asapi_02.c | 261 +++++++++++++++++++++
 testcases/network/lib6/asapi_03.c | 385 +++++++++++++++++++++++++++++++
 testcases/network/lib6/asapi_04.c | 460 --------------------------------------
 testcases/network/lib6/asapi_05.c | 261 ---------------------
 testcases/network/lib6/asapi_06.c | 385 -------------------------------
 8 files changed, 1112 insertions(+), 1112 deletions(-)
 create mode 100644 testcases/network/lib6/asapi_01.c
 create mode 100644 testcases/network/lib6/asapi_02.c
 create mode 100644 testcases/network/lib6/asapi_03.c
 delete mode 100644 testcases/network/lib6/asapi_04.c
 delete mode 100644 testcases/network/lib6/asapi_05.c
 delete mode 100644 testcases/network/lib6/asapi_06.c
runtest/ipv6_lib +3 -3
index 203e275..636232b 100644
@@ -2,6 +2,6 @@
 in6_01 in6_01
 in6_02 in6_02
 getaddrinfo_01 getaddrinfo_01
-asapi_04 asapi_04
-asapi_05 asapi_05
-asapi_06 asapi_06
+asapi_01 asapi_01
+asapi_02 asapi_02
+asapi_03 asapi_03
testcases/network/.gitignore +3 -3
index 18b0203..77a350b 100644
@@ -1,9 +1,9 @@
 /can/filter-tests/can_filter
 /can/filter-tests/can_rcv_own_msgs
 /datafiles/
-/lib6/asapi_04
-/lib6/asapi_05
-/lib6/asapi_06
+/lib6/asapi_01
+/lib6/asapi_02
+/lib6/asapi_03
 /lib6/getaddrinfo_01
 /lib6/in6_01
 /lib6/in6_02
testcases/network/lib6/asapi_01.c new file +460 -0

460 lines not on this page · raw message

testcases/network/lib6/asapi_02.c new file +261 -0

261 lines not on this page · raw message

testcases/network/lib6/asapi_03.c new file +385 -0

385 lines not on this page · raw message

testcases/network/lib6/asapi_04.c deleted +0 -460

460 lines not on this page · raw message

testcases/network/lib6/asapi_05.c deleted +0 -261

261 lines not on this page · raw message

testcases/network/lib6/asapi_06.c deleted +0 -385

385 lines not on this page · raw message

Signature · 1 line
1.9.3
#28 lore raw

PATCH v8 3/7 lib6/asapi_04.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of ptab[] and cstab[].
* Use 'pthread_join()' instead of 'sem_*'.
* Rewrite 'csum_test()' and 'isocketpair()'.
* Remove cleanup().
* Some cleanup.

---
 testcases/network/lib6/asapi_04.c | 301 +++++++++++++++-----------------------
 1 file changed, 117 insertions(+), 184 deletions(-)
testcases/network/lib6/asapi_04.c +117 -184
index 76de26b..df26494 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_04
- *
- * Test Description:
- *  Verify that in6 and sockaddr fields are present. Most of these are
- *  "PASS" if they just compile.
- *
- * Usage:  <for command-line>
- *  asapi_04
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -48,33 +31,33 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_04";	/* Test program identifier.    */
+char *TCID = "asapi_04";
 
-pid_t pid;
+static pid_t pid;
 
-struct {
+static struct {
 	char *prt_name;
 	int prt_value;
 } ptab[] = {
-	{
-	"hopopt", 0}, {
-	"ipv6", 41}, {
-	"ipv6-route", 43}, {
-	"ipv6-frag", 44}, {
-	"esp", 50}, {
-	"ah", 51}, {
-	"ipv6-icmp", 58}, {
-	"ipv6-nonxt", 59}, {
-"ipv6-opts", 60},};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
+	{"hopopt", 0},
+	{"ipv6", 41},
+	{"ipv6-route", 43},
+	{"ipv6-frag", 44},
+	{"esp", 50},
+	{"ah", 51},
+	{"ipv6-icmp", 58},
+	{"ipv6-nonxt", 59},
+	{"ipv6-opts", 60},
+};
 
-#define READ_TIMEOUT	5	/* secs */
+#define PTCOUNT		ARRAY_SIZE(ptab)
+#define READ_TIMEOUT	5
 
-void do_tests(void);
-void setup(void), cleanup(void);
-int csum_test(char *rhost);
+static void do_tests(void);
+static void setup(void);
+static void csum_test(void);
 
 int main(int argc, char *argv[])
 {
@@ -82,52 +65,48 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, 0, 0);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
-
 	tst_exit();
 }
 
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 /* RFC 3542, Section 2.3 */
 #ifndef IN6_ARE_ADDR_EQUAL
-	tst_resm(TBROK, "IN6_ARE_ADDR_EQUAL not present");
+	tst_resm(TCONF, "IN6_ARE_ADDR_EQUAL not present");
 #else /* IN6_ARE_ADDR_EQUAL */
 	/*
 	 * set each bit in an address and check for unequal; then set
 	 * in the second address and check for equal. Covers all bits, all
 	 * combinations.
 	 */
-	{
-		struct in6_addr a1, a2;
-		int word, bit;
-		int rv = 1;
+	struct in6_addr a1, a2;
+	int word, bit;
+	int rv = 1;
 
-		memset(&a1, 0, sizeof(a1));
-		memset(&a2, 0, sizeof(a2));
+	memset(&a1, 0, sizeof(a1));
+	memset(&a2, 0, sizeof(a2));
 
-		rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
+	rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
 
-		for (word = 0; word < 4; ++word)
-			for (bit = 0; bit < 32; ++bit) {
-				uint32_t newbit = 1 << bit;
+	for (word = 0; word < 4; ++word) {
+		for (bit = 0; bit < 32; ++bit) {
+			uint32_t newbit = 1U << bit;
 
-				a1.s6_addr32[word] |= newbit;	/* unequal */
-				rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
-				a2.s6_addr32[word] |= newbit;	/* equal */
-				rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
-			}
-		tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
+			a1.s6_addr32[word] |= newbit;
+			rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
+			a2.s6_addr32[word] |= newbit;
+			rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
+		}
 	}
+
+	tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
 #endif /* IN6_ARE_ADDR_EQUAL */
 
 /* RFC 3542, Section 2.4 */
@@ -141,7 +120,7 @@ void do_tests(void)
 			 ptab[i].prt_name);
 	}
 /* RFC 3542, Section 3.1 */
-	csum_test("::1");
+	csum_test();
 }
 
 /*
@@ -162,10 +141,10 @@ struct tprot {
 	unsigned char tp_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct tprot) + 2048];
-unsigned char rpbuf[sizeof(struct tprot) + 2048];
+static unsigned char tpbuf[sizeof(struct tprot) + 2048];
+static unsigned char rpbuf[sizeof(struct tprot) + 2048];
 
-struct csent {
+static struct csent {
 	int cs_offset;
 	int cs_dlen;
 	int cs_setresult;	/* setsockopt expected result */
@@ -173,25 +152,24 @@ struct csent {
 	int cs_sndresult;	/* send expected result */
 	int cs_snderrno;	/* send expected errno */
 } cstab[] = {
-	{
-	0, 5, 0, 0, 0, 0}, {
-	6, 30, 0, 0, 0, 0}, {
-	3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
-	{
-	4, 5, 0, 0, -1, EINVAL},	/* not enough space */
-	{
-	50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
-	{
-	22, 30, 0, 0, 0, 0}, {
-	2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
+	{0, 5, 0, 0, 0, 0},
+	{6, 30, 0, 0, 0, 0},
+	{3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
+	{4, 5, 0, 0, -1, EINVAL},	/* not enough space */
+	{50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
+	{22, 30, 0, 0, 0, 0},
+	{2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
 };
 
-#define CSCOUNT	(sizeof(cstab)/sizeof(cstab[0]))
+#define CSCOUNT	ARRAY_SIZE(cstab)
 
-static int recvtprot(int sd, unsigned char *packet, int psize)
+int TST_TOTAL = PTCOUNT + CSCOUNT;
+
+static int recvtprot(int sd, unsigned char *packet)
 {
 	struct tprot *tpt;
-	int cc, total, expected;
+	int cc;
+	unsigned int total, expected;
 	int gothead;
 
 	tpt = (struct tprot *)packet;
@@ -210,15 +188,17 @@ static int recvtprot(int sd, unsigned char *packet, int psize)
 				gothead = 1;
 				expected += ntohl(tpt->tp_dlen);
 			}
-		} else
+		} else {
 			break;
+		}
 	}
 	if (cc < 0)
 		return cc;
 	return total;
 }
 
-unsigned short csum(unsigned short partial, unsigned char *packet, int len)
+static unsigned short csum(unsigned short partial, unsigned char *packet,
+			   int len)
 {
 	unsigned long sum = partial;
 	unsigned short *ps;
@@ -234,7 +214,7 @@ unsigned short csum(unsigned short partial, unsigned char *packet, int len)
 	return ~sum;
 }
 
-struct ph {
+static struct ph {
 	struct in6_addr ph_sa;
 	struct in6_addr ph_da;
 	uint32_t ph_len;
@@ -242,13 +222,14 @@ struct ph {
 	uint8_t ph_nh;
 } ph;
 
-static int client(int prot, int sfd)
+static int client(int sfd)
 {
 	struct tprot *pttp = (struct tprot *)tpbuf;
 	struct tprot *prtp = (struct tprot *)rpbuf;
 	struct sockaddr_in6 rsin6;
 	static int seq;
-	int i, sd, cc, cs;
+	unsigned int i;
+	int sd, cc, cs;
 
 	memset(&rsin6, 0, sizeof(rsin6));
 	rsin6.sin6_family = AF_INET6;
@@ -259,11 +240,8 @@ static int client(int prot, int sfd)
 	ph.ph_da = rsin6.sin6_addr;
 	ph.ph_nh = NH_TEST;
 
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		tst_resm(TBROK, "can't create raw socket: %s", strerror(errno));
-		return -1;
-	}
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	for (i = 0; i < CSCOUNT; ++i) {
 		int offset, len, xlen;
 		int rv;
@@ -322,9 +300,9 @@ static int client(int prot, int sfd)
 				 offset, len);
 			continue;
 		}
-		while ((cc = recvtprot(sfd, rpbuf, sizeof(rpbuf)))) {
-			if (htonl(prtp->tp_pid) == pid &&
-			    htonl(prtp->tp_seq) == seq)
+		while ((cc = recvtprot(sfd, rpbuf))) {
+			if (htonl(prtp->tp_pid) == (uint32_t)pid &&
+			    htonl(prtp->tp_seq) == (uint32_t)seq)
 				break;
 		}
 		rv = 1;
@@ -334,7 +312,7 @@ static int client(int prot, int sfd)
 				continue;
 			if (*p != 0xa5) {
 				tst_resm(TFAIL, "IPV6_CHECKSUM corrupt data "
-					 "0x%02x != 0xa5 at offset %d in packet",
+					 "0x%02x != 0xa5 at offset %zd in packet",
 					 *p, p - rpbuf);
 				rv = 0;
 				break;
@@ -356,99 +334,60 @@ static int client(int prot, int sfd)
 }
 
 static int listen_fd, connect_fd;
-sem_t ilsem;
 
-void *ilistener(void *arg)
+static void *ilistener(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	connect_fd = accept(listen_fd, 0, 0);
 	close(listen_fd);
-	sem_post(&ilsem);
 	return NULL;
 }
 
-int isocketpair(int pf, int type, int proto, int fd[2])
+static void isocketpair(int pf, int type, int proto, int fd[2])
 {
 	pthread_t thid;
 	struct sockaddr_in sin4;
 	socklen_t namelen;
 
-/* restrict to PF_INET for now */
-	if (pf != PF_INET) {
-		errno = EOPNOTSUPP;
-		return -1;
-	}
-	sem_init(&ilsem, 0, 0);
-	listen_fd = socket(pf, type, proto);
-	if (listen_fd < 0) {
-		perror("socket");
-		return -1;
-	}
+	listen_fd = SAFE_SOCKET(NULL, pf, type, proto);
+
 	memset(&sin4, 0, sizeof(sin4));
-	if (bind(listen_fd, (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("bind");
-		return -1;
-	}
-	if (listen(listen_fd, 10) < 0) {
-		perror("listen");
-		return -1;
-	}
+
+	SAFE_BIND(NULL, listen_fd, (struct sockaddr *)&sin4, sizeof(sin4));
+
+	SAFE_LISTEN(NULL, listen_fd, 10);
+
 	namelen = sizeof(sin4);
-	if (getsockname(listen_fd, (struct sockaddr *)&sin4, &namelen) < 0) {
-		perror("getsockname");
-		return -1;
-	}
-	if (pthread_create(&thid, 0, ilistener, 0) < 0) {
-		perror("pthread_create");
-		return -1;
-	}
+	SAFE_GETSOCKNAME(NULL, listen_fd, (struct sockaddr *)&sin4, &namelen);
+
+	if (pthread_create(&thid, 0, ilistener, 0) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "pthread_create error");
+
+	fd[0] = SAFE_SOCKET(NULL, pf, type, proto);
 
-	fd[0] = socket(pf, type, proto);
-	if (fd[0] < 0) {
-		perror("socket");
-		return -1;
-	}
 	sin4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	if (connect(fd[0], (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("connect");
-		return -1;
-	}
-	sem_wait(&ilsem);
+
+	SAFE_CONNECT(NULL, fd[0], (struct sockaddr *)&sin4, sizeof(sin4));
+
+	pthread_join(thid, NULL);
+
 	fd[1] = connect_fd;
-	sem_destroy(&ilsem);
-	return 0;
 }
 
 #ifndef MAX
 #define MAX(a, b) (@ >= (b) ? (a) : (b))
 #endif /* MAX */
 
-int csum_test(char *rhost)
+static void csum_test(void)
 {
 	fd_set rset, rset_save;
 	int csd[2];		/* control sockets */
 	int sd, nfds, maxfd, cc;
 	struct timeval tv;
 
-/* rhost == loopback, for now */
-	if (strcmp(rhost, "::1")) {
-		tst_resm(TBROK, "invalid rhost \"%s\"", rhost);
-		return -1;
-	}
-	if (isocketpair(PF_INET, SOCK_STREAM, 0, csd) < 0) {
-		tst_resm(TBROK, "socketpair: %s", strerror(errno));
-		return -1;
-	}
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		int saved_errno = errno;
-
-		if (errno == EPERM && geteuid())
-			tst_resm(TBROK, "IPV6_CHECKSUM tests must run as root");
-		else
-			tst_resm(TBROK, "All IPv6_CHECKSUM tests broken: "
-				 "socket: %s", strerror(saved_errno));
-		return -1;
-	}
+	isocketpair(PF_INET, SOCK_STREAM, 0, csd);
+
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	FD_ZERO(&rset_save);
 	FD_SET(sd, &rset_save);
 	FD_SET(csd[1], &rset_save);
@@ -457,16 +396,16 @@ int csum_test(char *rhost)
 
 	/* server socket set; now start the client */
 	switch (fork()) {
-	case 0:		/* child */
+	case 0:
 		close(csd[0]);
 		break;
 	case -1:
-		tst_resm(TBROK, "can't fork rserver");
-		return -1;
-	default:		/* parent */
+		tst_brkm(TBROK, NULL, "can't fork rserver");
+	default:
 		close(sd);
 		close(csd[1]);
-		return client(pid, csd[0]);
+		client(csd[0]);
+		return;
 	}
 
 	tv.tv_sec = READ_TIMEOUT;
@@ -478,7 +417,7 @@ int csum_test(char *rhost)
 			exit(0);
 		} else if (nfds == 0) {
 			fprintf(stderr, "server read timed out");
-			return -1;
+			return;
 		}
 		if (FD_ISSET(sd, &rset)) {
 			static char packet[2048];
@@ -499,9 +438,8 @@ int csum_test(char *rhost)
 			char buf[2048];
 
 			cc = read(csd[1], buf, sizeof(buf));
-			if (cc == 0) {
+			if (cc == 0)
 				exit(0);
-			}
 			if (cc < 0) {
 				perror("server read");
 				exit(1);
@@ -512,16 +450,11 @@ int csum_test(char *rhost)
 		tv.tv_sec = READ_TIMEOUT;
 		tv.tv_usec = 0;
 	}
-	return 0;
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
-}
+	TEST_PAUSE;
 
-void cleanup(void)
-{
+	pid = getpid();
 }
-
-int TST_TOTAL = PTCOUNT + CSCOUNT;
Signature · 1 line
1.9.3
#29 lore raw

PATCH v8 4/7 lib6/asapi_05.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of etab[] and ftab[].
* Some cleanup.

---
 testcases/network/lib6/asapi_05.c | 159 ++++++++++++++------------------------
 1 file changed, 60 insertions(+), 99 deletions(-)
testcases/network/lib6/asapi_05.c +60 -99
index e251eee..f8c30f1 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_05
- *
- * Test Description:
- *  These tests are for the "Advanced Sockets API" (RFC 3542)
- *  Verify that in6 and sockaddr fields are present.
- *
- * Usage:  <for command-line>
- *  asapi_05
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -47,86 +30,74 @@
 #include <netinet/icmp6.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_05";	/* Test program identifier.    */
+char *TCID = "asapi_05";
 
-void setup(void);
-void cleanup(void);
+static void setup(void);
 
-void icmp6_ft(void);
+static void icmp6_ft(void);
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		icmp6_ft();
-	}
-
-	cleanup();
 
 	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
+	TEST_PAUSE;
 	tst_require_root();
 }
 
-void cleanup(void)
-{
-}
-
-/*
- * This is for old, broken glibc-header icmp6_filter structure definitions.
- * If icmp6.h has struct icmp6_filter with field named "data" instead
- * of the standard "icmp_filt", uncomment this line.
- */
-/*#define icmp_filt	data */
-
-enum tt { T_WILLPASS, T_WILLBLOCK, T_SETPASS, T_SETBLOCK, T_SETPASSALL,
+enum tt {
+	T_WILLPASS,
+	T_WILLBLOCK,
+	T_SETPASS,
+	T_SETBLOCK,
+	T_SETPASSALL,
 	T_SETBLOCKALL
 };
-struct ftent {
-	char *ft_tname;		/* test name, for logging */
+
+static struct ftent {
+	char *ft_tname;			/* test name, for logging */
 	unsigned char ft_sndtype;	/* send type field */
 	unsigned char ft_flttype;	/* filter type field */
-	enum tt ft_test;	/* what macro to test */
-	int ft_expected;	/* packet should pass? */
+	enum tt ft_test;		/* what macro to test */
+	int ft_expected;		/* packet should pass? */
 } ftab[] = {
-	{
-	"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1}, {
-	"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1}, {
-	"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0}, {
-"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},};
-
-#define FTCOUNT	(sizeof(ftab)/sizeof(ftab[0]))
-
-int ic6_send1(char *tname, unsigned char type)
+	{"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1},
+	{"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1},
+	{"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0},
+	{"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},
+};
+
+#define FTCOUNT	ARRAY_SIZE(ftab)
+
+static int ic6_send1(char *tname, unsigned char type)
 {
 	struct sockaddr_in6 sin6;
 	struct icmp6_hdr ic6;
 	int s;
 
-	s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (s == -1) {
-		tst_resm(TBROK, "%s: socket failed", tname);
-		return 1;
-	}
+	s = SAFE_SOCKET(NULL, AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+
 	memset(&ic6, 0, sizeof(ic6));
 	ic6.icmp6_type = type;
 	ic6.icmp6_data32[0] = htonl(getpid());
@@ -142,7 +113,7 @@ int ic6_send1(char *tname, unsigned char type)
 	return 0;
 }
 
-int ic6_recv1(char *tname, int sall, int sf)
+static int ic6_recv1(char *tname, int sall, int sf)
 {
 	fd_set readfds, readfds_saved;
 	struct timeval tv;
@@ -185,7 +156,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotall = 1;
 		}
 		if (FD_ISSET(sf, &readfds)) {
@@ -196,7 +167,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotone = 1;
 		}
 		memcpy(&readfds, &readfds_saved, sizeof(readfds));
@@ -211,18 +182,14 @@ int ic6_recv1(char *tname, int sall, int sf)
 }
 
 /* functional tests */
-void icmp6_ft(void)
+static void icmp6_ft(void)
 {
 	struct icmp6_filter i6f;
 	int sall, sf;
-	int i;
+	unsigned int i;
+
+	sall = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
-	sall = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sall < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create sall socket");
-		return;
-	}
 	ICMP6_FILTER_SETPASSALL(&i6f);
 	if (setsockopt(sall, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f,
 		       sizeof(i6f)) < 0) {
@@ -230,12 +197,7 @@ void icmp6_ft(void)
 			 "setsockopt pass all ICMP6_FILTER failed");
 	}
 
-	sf = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sf < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create test socket");
-		return;
-	}
+	sf = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
 	int rv;
 
@@ -284,8 +246,9 @@ void icmp6_ft(void)
 			if (ic6_send1(ftab[i].ft_tname, ftab[i].ft_sndtype))
 				continue;
 			rv = ic6_recv1(ftab[i].ft_tname, sall, sf);
-		} else
+		} else {
 			rv = -1;
+		}
 
 		if (rv < 0)
 			continue;
@@ -296,5 +259,3 @@ void icmp6_ft(void)
 			tst_resm(TPASS, "%s", ftab[i].ft_tname);
 	}
 }
-
-int TST_TOTAL = FTCOUNT;
Signature · 1 line
1.9.3
#30 lore raw

PATCH v8 5/7 lib6/asapi_06.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Remove unused variables.
* Modify the format of sotab[] and cmtab[].
* Modify the initialization of sotab[].
* Remove IPV6_PKTINFO test. IPV6_PKTINFO is only implemented by setsockopt() not by getsockopt(). see:
https://github.com/torvalds/linux/commit/b24a2516d10751d7ed5afb58420df25370c9dffb
* Some cleanup.

---
 testcases/network/lib6/asapi_06.c | 473 ++++++++------------------------------
 1 file changed, 99 insertions(+), 374 deletions(-)
testcases/network/lib6/asapi_06.c +99 -374
index 960ebb5..c6e2c8e 100644
@@ -1,37 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_06
- *
- * Test Description:
- *  Tests for RFC 3542 section 4 socket options and ancillary data
- *
- * Usage:  <for command-line>
- *  asapi_06
- *
- * HISTORY
- *	05/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include "config.h"
@@ -57,25 +41,16 @@
 #include <arpa/inet.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_06";	/* Test program identifier.    */
+char *TCID = "asapi_06";
 
 int TST_TOTAL = 1;
 
-pid_t pid;
-
-struct {
-	char *prt_name;
-	int prt_value;
-} ptab[] = {
-};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
-
 #define READ_TIMEOUT	5	/* secs */
 
-void do_tests(void);
-void setup(void), cleanup(void);
+static void do_tests(void);
+static void setup(void);
 
 int main(int argc, char *argv[])
 {
@@ -83,14 +58,11 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
 	tst_exit();
 }
 
@@ -150,7 +122,7 @@ union soval {
 #define IN6_ANY		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 
 /* so_clrval and so_setval members are initilized in the body */
-struct soent {
+static struct soent {
 	char *so_tname;
 	int so_opt;
 	int so_dorecv;		/* do receive test? */
@@ -160,73 +132,52 @@ struct soent {
 	union soval so_setval;
 	socklen_t so_valsize;
 } sotab[] = {
-/* RFC 3542, Section 4 */
-	{
-		"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* make sure TCLASS stays when setting another opt */
-	{
-		"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* OLD values */
-	{
-		"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-0}}}}}, sizeof(int)},};
-
-#define SOCOUNT	(sizeof(sotab)/sizeof(sotab[0]))
+	/* RFC 3542, Section 4 */
+	{"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* make sure TCLASS stays when setting another opt */
+	{"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* OLD values */
+	{"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+};
+
+#define SOCOUNT	ARRAY_SIZE(sotab)
 
 struct soprot {
-	int sop_pid;		/* sender PID */
-	int sop_seq;		/* sequence # */
-	int sop_dlen;		/* tp_dat length */
+	int sop_pid;			/* sender PID */
+	int sop_seq;			/* sequence # */
+	int sop_dlen;			/* tp_dat length */
 	unsigned char sop_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct soprot) + 2048];
-unsigned char rpbuf[sizeof(struct soprot) + 2048];
+static unsigned char tpbuf[sizeof(struct soprot) + 2048];
+static unsigned char rpbuf[sizeof(struct soprot) + 2048];
 
-unsigned char control[2048];
-int clen;
+static unsigned char control[2048];
 
-int seq;
+static int seq;
 
-int setupso(void)
-{
-/* add routing headers, other ancillary data here */
-	return 0;
-}
-
-struct cme {
+static struct cme {
 	int cm_len;
 	int cm_level;
 	int cm_type;
@@ -235,26 +186,21 @@ struct cme {
 		uint32_t cmu_hops;
 	} cmu;
 } cmtab[] = {
-	{
-		sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {
-		0x12}
-	}
-	, {
-		sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {
-		0x21}
-	}
-,};
+	{sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {0x12} },
+	{sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {0x21} },
+};
 
-#define CMCOUNT	(sizeof(cmtab)/sizeof(cmtab[0]))
+#define CMCOUNT	ARRAY_SIZE(cmtab)
 
-ssize_t sendall(int st)
+static ssize_t sendall(int st)
 {
 	struct sockaddr_in6 sin6;
 	struct msghdr msg;
 	struct iovec iov;
 	struct soprot *psop;
 	unsigned char *pd;
-	int i, ctotal;
+	unsigned int i;
+	int ctotal;
 
 	psop = (struct soprot *)tpbuf;
 	psop->sop_pid = htonl(getpid());
@@ -291,7 +237,7 @@ ssize_t sendall(int st)
 	return sendmsg(st, &msg, 0);
 }
 
-void so_test(struct soent *psoe)
+static void so_test(struct soent *psoe)
 {
 	struct sockaddr_in6 sin6;
 	union soval sobuf;
@@ -300,63 +246,51 @@ void so_test(struct soent *psoe)
 	int st;
 
 	if (psoe->so_opt == -1) {
-		tst_resm(TBROK, "%s not present at compile time",
+		tst_brkm(TBROK | TERRNO, NULL, "%s not present at compile time",
 			 psoe->so_tname);
-		return;
 	}
 	if (psoe->so_clear || sr < 0) {
 		if (sr < 0)
 			close(sr);
-		sr = socket(PF_INET6, SOCK_RAW, NH_TEST);
-		if (sr < 0) {
-			tst_resm(TBROK, "%s socket: %s", psoe->so_tname,
-				 strerror(errno));
-			return;
-		}
+		sr = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
 	}
 	memset(&sin6, 0, sizeof(sin6));
 	sin6.sin6_family = AF_INET6;
 	sin6.sin6_addr = in6addr_loopback;
-	if (bind(sr, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
-		tst_resm(TBROK, "%s: bind: %s", psoe->so_tname,
-			 strerror(errno));
-	}
+
+	SAFE_BIND(NULL, sr, (struct sockaddr *)&sin6, sizeof(sin6));
+
 	if (setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_clrval,
 		       psoe->so_valsize) < 0) {
-		tst_resm(TBROK, "%s: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
+		tst_brkm(TBROK | TERRNO, NULL, "%s: setsockopt",
+			 psoe->so_tname);
 	}
+
 	TEST(setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_setval,
 			psoe->so_valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL, "%s set-get: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
+		tst_resm(TFAIL | TERRNO, "%s set-get: setsockopt",
+			 psoe->so_tname);
 		return;
 	}
+
 	valsize = psoe->so_valsize;
 	TEST(getsockopt(sr, SOL_IPV6, psoe->so_opt, &sobuf, &valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TBROK, "%s set-get: getsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize))
+		tst_brkm(TBROK | TERRNO, NULL, "%s set-get: getsockopt",
+			 psoe->so_tname);
+	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize)) {
 		tst_resm(TFAIL, "%s set-get optval != setval", psoe->so_tname);
-	else
+	} else {
 		tst_resm(TPASS, "%s set-get", psoe->so_tname);
-
-	st = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (st < 0) {
-		tst_resm(TBROK, "%s transmit socket: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	}
-	if (sendall(st) < 0) {
-		tst_resm(TBROK, "%s transmit sendto: %s", psoe->so_tname,
-			 strerror(errno));
-		close(st);
-		return;
 	}
+
+	st = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
+	if (sendall(st) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "%s transmit sendto",
+			 psoe->so_tname);
+
 	close(st);
 
 	/* receiver processing */
@@ -382,12 +316,11 @@ void so_test(struct soent *psoe)
 			if (nfds < 0) {
 				if (errno == EINTR)
 					continue;
-				tst_resm(TBROK, "%s select: %s", psoe->so_tname,
-					 strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s select",
+					 psoe->so_tname);
 			}
 			if (nfds == 0) {
-				tst_resm(TBROK, "%s recvmsg timed out",
+				tst_brkm(TBROK, NULL, "%s recvmsg timed out",
 					 psoe->so_tname);
 				return;
 			}
@@ -405,11 +338,10 @@ void so_test(struct soent *psoe)
 
 			cc = recvmsg(sr, &msg, 0);
 			if (cc < 0) {
-				tst_resm(TBROK, "%s recvmsg: %s",
-					 psoe->so_tname, strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s recvmsg",
+					 psoe->so_tname);
 			}
-/* check pid & seq here */
+			/* check pid & seq here */
 			break;
 		}
 		gotone = 0;
@@ -419,9 +351,9 @@ void so_test(struct soent *psoe)
 				break;
 			gotone = pcmsg->cmsg_level == SOL_IPV6 &&
 			    pcmsg->cmsg_type == psoe->so_cmtype;
-			if (gotone)
+			if (gotone) {
 				break;
-			else if (psoe->so_clear) {
+			} else if (psoe->so_clear) {
 				tst_resm(TFAIL, "%s receive: extraneous data "
 					 "in control: level %d type %d len %zu",
 					 psoe->so_tname, pcmsg->cmsg_level,
@@ -429,232 +361,25 @@ void so_test(struct soent *psoe)
 				return;
 			}
 		}
-/* check contents here */
+		/* check contents here */
 		if (psoe->so_dorecv)
 			tst_resm(gotone ? TPASS : TFAIL, "%s receive",
 				 psoe->so_tname);
 	}
 }
 
-#define IPV6_ADDR_NODE		1
-#define IPV6_ADDR_LINK		2
-#define IPV6_ADDR_GLOBAL	3
-
-#ifdef HAVE_IFADDRS_H
-static int ipv6_addr_scope(struct in6_addr *pin6)
-{
-	if ((ntohl(pin6->s6_addr32[0]) & 0xFFC00000) == 0xFE800000)
-		return IPV6_ADDR_LINK;
-	if (memcmp(pin6, &in6addr_loopback, sizeof(*pin6)) == 0)
-		return IPV6_ADDR_NODE;
-	return IPV6_ADDR_GLOBAL;
-}
-#endif /* HAVE_IFADDRS_H */
-
-int getsock(char *tname, struct sockaddr_in6 *psin6_arg, int scope)
-{
-#ifdef HAVE_IFADDRS_H
-	static struct ifaddrs *pifa_head;
-	struct ifaddrs *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	int ifindex = 0;
-	int s;
-
-	if (!pifa_head && getifaddrs(&pifa_head)) {
-		tst_resm(TBROK, "%s: getifaddrs failed", tname);
-		return -1;
-	}
-	if (psin6_arg)
-		ifindex = psin6_arg->sin6_scope_id;
-
-	/* first, find a global address */
-	for (pifa = pifa_head; pifa; pifa = pifa->ifa_next) {
-		int this_scope;
-
-		if (!(pifa->ifa_flags & IFF_UP))
-			continue;
-		if (pifa->ifa_addr->sa_family != AF_INET6)
-			continue;
-		psin6 = (struct sockaddr_in6 *)pifa->ifa_addr;
-		this_scope = ipv6_addr_scope(&psin6->sin6_addr);
-		if (this_scope &&
-		    ((this_scope < 0 && -this_scope == scope) ||
-		     (this_scope > 0 && this_scope != scope)))
-			continue;
-		psin6->sin6_scope_id = if_nametoindex(pifa->ifa_name);
-		if ((ifindex < 0 && -ifindex == psin6->sin6_scope_id) ||
-		    (ifindex > 0 && ifindex != psin6->sin6_scope_id))
-			continue;
-		s = socket(PF_INET6, SOCK_DGRAM, 0);
-		if (s < 0) {
-			tst_resm(TBROK, "%s: socket %s", tname,
-				 strerror(errno));
-			return -1;
-		}
-		if (bind(s, pifa->ifa_addr, sizeof(struct sockaddr_in6)) < 0) {
-			tst_resm(TBROK, "%s: bind \"%s\": %s", tname,
-				 inet_ntop(AF_INET6, &psin6->sin6_addr, strbuf,
-					   sizeof(strbuf)), strerror(errno));
-			return -1;
-		}
-		if (psin6_arg) {
-			*psin6_arg = *psin6;
-			psin6_arg->sin6_scope_id =
-			    if_nametoindex(pifa->ifa_name);
-		}
-		return s;
-	}
-	{
-		char *scopestr, *intfstr;
-
-		switch (scope) {
-		case IPV6_ADDR_NODE:
-			scopestr = " node-local";
-			break;
-		case IPV6_ADDR_LINK:
-			scopestr = " link-local";
-			break;
-		case IPV6_ADDR_GLOBAL:
-			scopestr = " global";
-			break;
-		default:
-			scopestr = "";
-			break;
-		}
-		if (ifindex < 0) {
-			intfstr = " not on ifindex";
-			ifindex = -ifindex;
-		} else if (ifindex)
-			intfstr = " on ifindex";
-		else
-			intfstr = 0;
-
-		if (intfstr)
-			tst_resm(TBROK, "%s: getsock : no%s addresses%s %d",
-				 tname, scopestr, intfstr, ifindex);
-		else
-			tst_resm(TBROK, "%s: getsock : no%s addresses",
-				 tname, scopestr);
-	}
-	return -1;
-#else /* HAVE_IFADDRS_H */
-	return -1;
-#endif
-}
-
-#ifdef notyet
-/*
- * RFC 3542 IPV6_PKTINFO not in mainline yet (as of 2.6.15). The get/set
- * tests are below, and comments for some further tests to be added later
- */
-void test_pktinfo(void)
-{
-	int s_snd, s_rcv[3] = { -1, -1, -1 };
-	struct sockaddr_in6 sa_rcv[3];
-	int s, i;
-	struct ifaddrs *pifa_head, *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	char *tname = "IPV6_PKTINFO";
-	struct in6_pktinfo pi, pi_tmp;
-	int sinlen;
-	int optlen;
-
-	s_snd = getsock(tname, 0, IPV6_ADDR_GLOBAL);
-	if (s_snd < 0) {
-		tst_resm(TBROK, "%s: can't create send socket", tname);
-		return;
-	}
-	/* global-scope address, interface X */
-	sa_rcv[0].sin6_scope_id = 0;
-	s_rcv[0] = getsock(tname, &sa_rcv[0], IPV6_ADDR_GLOBAL);
-	if (s_rcv[0] == -1) {
-		tst_resm(TBROK, "%s: only link-scope addresses", tname);
-		return;
-	}
-	/* link-local-scope address, interface X */
-	sa_rcv[1].sin6_scope_id = sa_rcv[0].sin6_scope_id;
-	s_rcv[1] = getsock(tname, &sa_rcv[1], IPV6_ADDR_LINK);
-	if (s_rcv[1] < 0) {
-		tst_resm(TBROK, "%s: no link-local address on ifindex %d",
-			 tname, sa_rcv[0].sin6_scope_id);
-		return;
-	}
-	/* link-local-scope address, interface Y */
-	sa_rcv[2].sin6_scope_id = -sa_rcv[0].sin6_scope_id;
-	s_rcv[2] = getsock(tname, &sa_rcv[2], IPV6_ADDR_LINK);
-	if (s_rcv[2] < 0) {
-		tst_resm(TBROK, "%s: only one interface?", tname);
-		return;
-	}
-	/* send to rcv1 to verify communication */
-	/* force to rcv2 w/ PKTINFO */
-/* TESTS: */
-/* sticky set-get */
-	tname = "IPV6_PKTINFO set";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[1].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else
-		tst_resm(TPASS, "%s", tname);
-
-	tname = "IPV6_PKTINFO get";
-	optlen = sizeof(pi_tmp);
-	TEST(getsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi_tmp, &optlen));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else if (memcmp(&pi, &pi_tmp, sizeof(pi)) != 0) {
-		char strbuf2[64];
-		tst_resm(TFAIL, "%s: {\"%s\",%d} != {\"%s\",%d}", tname,
-			 inet_ntop(AF_INET6, &pi_tmp.ipi6_addr, strbuf,
-				   sizeof(strbuf)), pi_tmp.ipi6_ifindex,
-			 inet_ntop(AF_INET6, &pi.ipi6_addr, strbuf2,
-				   sizeof(strbuf2)), pi.ipi6_ifindex);
-	} else
-		tst_resm(TPASS, "%s", tname);
-/* ancillary data override */
-/* link-local, wrong interface */
-	tname = "IPV6_PKTINFO invalid {lladdr, intf}";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[2].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN == 0)
-		tst_resm(TFAIL, "%s returns success, should be -1, EINVAL",
-			 tname);
-	else if (TEST_ERRNO != EINVAL)
-		tst_resm(TFAIL, "%s errno %d != %d", tname, TEST_ERRNO, EINVAL);
-	else
-		tst_resm(TPASS, "%s", tname);
-/* nonexistent interface */
-/* non-local address */
-/* clear address */
-/* clear interface */
-/* sendmsg() sin6_scope differs with ancillary data interface */
-}
-#endif /* notyet */
-
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 	for (i = 0; i < SOCOUNT; ++i) {
 		sotab[i].so_clrval.sou_bool = 0;
 		sotab[i].so_setval.sou_bool = 1;
 		so_test(&sotab[i]);
 	}
-#ifdef notyet
-	test_pktinfo();
-#endif /* notyet - see test_pktinfo() comment above */
-}
-
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
 }
 
-void cleanup(void)
+static void setup(void)
 {
+	TEST_PAUSE;
 }
Signature · 1 line
1.9.3
#31 lore raw

PATCH v8 6/7 lib6/getaddrinfo_01.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* In every test function, using TINFO instead of TPASS marks the success of each inner test.
* Some cleanup.

---
 testcases/network/lib6/getaddrinfo_01.c | 503 ++++++++++++++++++--------------
 1 file changed, 281 insertions(+), 222 deletions(-)
testcases/network/lib6/getaddrinfo_01.c +281 -222
index ec29b5d..a812e77 100644
@@ -1,43 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: getaddrinfo_01
- *
- * Test Description:
- *  Tests for getaddrinfo library function
- *
- * Usage:  <for command-line>
- *  getaddrinfo_01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2004 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <unistd.h>
@@ -53,40 +31,38 @@
 #define AI_V4MAPPED    0x0008	/* IPv4 mapped addresses are acceptable.  */
 #endif
 
-char *TCID = "getaddrinfo_01";	/* Test program identifier.    */
-int testno;
+static void setup(void);
+static void gaiv4(void);
+static void gaiv6(void);
 
-void setup(void), cleanup(void);
-
-int TST_TOTAL = 1;
-
-/* a host that isn't where LTP is running */
-#define REMOTEHOSTNAME	"www.ibm.com"
-
-void gaiv4(void), gaiv6(void);
-void dumpres(struct addrinfo *);
+char *TCID = "getaddrinfo_01";
+int TST_TOTAL = 2;
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+
 		gaiv4();
 		gaiv6();
 	}
-	cleanup();
 
-	return (0);
+	tst_exit();
 }
 
-/* getaddrinfo tests (v4) */
+static void setup(void)
+{
+	TEST_PAUSE;
+}
 
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -132,13 +108,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 2, IPv4 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -152,20 +133,26 @@ void gaiv4(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 3, IPv4 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -200,13 +187,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 4, IPv4 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -244,18 +236,22 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv4 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 5, IPv4 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -266,8 +262,11 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -306,17 +305,28 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service,"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 0+service,i"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 7, IPv4 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -350,19 +360,30 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 8, IPv4 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -376,15 +397,18 @@ void gaiv4(void)
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
-	} else
-		tst_resm(TPASS,
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	} else {
+		tst_resm(TINFO,
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV");
+	}
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 9, IPv4 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -396,12 +420,13 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 10, IPv4 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = 0;
@@ -439,20 +464,31 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv4 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 11, IPv4 AI_V4MAPPED */
-
 	/* AI_V4MAPPED should be ignored because family != AF_INET6 */
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
@@ -482,18 +518,25 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
+
+
+	tst_resm(TPASS, "getaddrinfo tests (v4) succeed");
 }
 
 /* getaddrinfo tests (v6) */
-
-void gaiv6(void)
+static void gaiv6(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -539,13 +582,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 13, IPv6 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -559,20 +607,26 @@ void gaiv6(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 14, IPv6 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -607,13 +661,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 15, IPv6 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -652,18 +711,22 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv6 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 16, IPv6 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -674,8 +737,11 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -715,17 +781,26 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 18, IPv6 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -760,19 +835,28 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 19, IPv6 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -786,15 +870,17 @@ void gaiv6(void)
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS,
+		tst_resm(TINFO,
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 20, IPv6 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -806,12 +892,13 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 21, IPv6 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = 0;
@@ -850,20 +937,31 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv6 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 22, IPv6 AI_V4MAPPED */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_flags = AI_V4MAPPED;
@@ -892,57 +990,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
-}
-
-/* this prints an addrinfo list; useful for debugging */
-void dumpres(struct addrinfo *pai)
-{
-	int count = 1;
-	for (; pai; pai = pai->ai_next, count++) {
-		printf("result %d [0x%p]\n", count, pai);
-		printf("\tai_flags %x\n", pai->ai_flags);
-		printf("\tai_family %d\n", pai->ai_family);
-		printf("\tai_socktype %d\n", pai->ai_socktype);
-		printf("\tai_protocol %d\n", pai->ai_protocol);
-		printf("\tai_addrlen %d\n", pai->ai_addrlen);
-		printf("\tai_canonname \"%s\"\n", pai->ai_canonname);
-		printf("\tai_addr.sa_family %x\n", pai->ai_addr->sa_family);
-		if (pai->ai_addr->sa_family == AF_INET) {
-			char buf[1024];
-			struct sockaddr_in *psin =
-			    (struct sockaddr_in *)pai->ai_addr;
-
-			if (!inet_ntop(AF_INET, &psin->sin_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin_addr \"%s\"\n", buf);
-		} else if (pai->ai_addr->sa_family == AF_INET6) {
-			char buf[1024];
-
-			struct sockaddr_in6 *psin6 =
-			    (struct sockaddr_in6 *)pai->ai_addr;
-			if (!inet_ntop(AF_INET6, &psin6->sin6_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin6_addr \"%s\"\n", buf);
-
-		}
-		printf("\tai_next %p\n", pai->ai_next);
+		return;
 	}
-}
 
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
-}
-
-void cleanup(void)
-{
+	tst_resm(TPASS, "getaddrinfo tests (v6) succeed");
 }
Signature · 1 line
1.9.3
#33 lore raw

PATCH v7 3/7 lib6/asapi_04.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of ptab[] and cstab[].
* Use 'pthread_join()' instead of 'sem_*'.
* Rewrite 'csum_test()' and 'isocketpair()'.
* Remove cleanup().
* Some cleanup.

---
 testcases/network/lib6/asapi_04.c | 301 +++++++++++++++-----------------------
 1 file changed, 117 insertions(+), 184 deletions(-)
testcases/network/lib6/asapi_04.c +117 -184
index 76de26b..df26494 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_04
- *
- * Test Description:
- *  Verify that in6 and sockaddr fields are present. Most of these are
- *  "PASS" if they just compile.
- *
- * Usage:  <for command-line>
- *  asapi_04
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -48,33 +31,33 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_04";	/* Test program identifier.    */
+char *TCID = "asapi_04";
 
-pid_t pid;
+static pid_t pid;
 
-struct {
+static struct {
 	char *prt_name;
 	int prt_value;
 } ptab[] = {
-	{
-	"hopopt", 0}, {
-	"ipv6", 41}, {
-	"ipv6-route", 43}, {
-	"ipv6-frag", 44}, {
-	"esp", 50}, {
-	"ah", 51}, {
-	"ipv6-icmp", 58}, {
-	"ipv6-nonxt", 59}, {
-"ipv6-opts", 60},};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
+	{"hopopt", 0},
+	{"ipv6", 41},
+	{"ipv6-route", 43},
+	{"ipv6-frag", 44},
+	{"esp", 50},
+	{"ah", 51},
+	{"ipv6-icmp", 58},
+	{"ipv6-nonxt", 59},
+	{"ipv6-opts", 60},
+};
 
-#define READ_TIMEOUT	5	/* secs */
+#define PTCOUNT		ARRAY_SIZE(ptab)
+#define READ_TIMEOUT	5
 
-void do_tests(void);
-void setup(void), cleanup(void);
-int csum_test(char *rhost);
+static void do_tests(void);
+static void setup(void);
+static void csum_test(void);
 
 int main(int argc, char *argv[])
 {
@@ -82,52 +65,48 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, 0, 0);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
-
 	tst_exit();
 }
 
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 /* RFC 3542, Section 2.3 */
 #ifndef IN6_ARE_ADDR_EQUAL
-	tst_resm(TBROK, "IN6_ARE_ADDR_EQUAL not present");
+	tst_resm(TCONF, "IN6_ARE_ADDR_EQUAL not present");
 #else /* IN6_ARE_ADDR_EQUAL */
 	/*
 	 * set each bit in an address and check for unequal; then set
 	 * in the second address and check for equal. Covers all bits, all
 	 * combinations.
 	 */
-	{
-		struct in6_addr a1, a2;
-		int word, bit;
-		int rv = 1;
+	struct in6_addr a1, a2;
+	int word, bit;
+	int rv = 1;
 
-		memset(&a1, 0, sizeof(a1));
-		memset(&a2, 0, sizeof(a2));
+	memset(&a1, 0, sizeof(a1));
+	memset(&a2, 0, sizeof(a2));
 
-		rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
+	rv = IN6_ARE_ADDR_EQUAL(&a1, &a2);
 
-		for (word = 0; word < 4; ++word)
-			for (bit = 0; bit < 32; ++bit) {
-				uint32_t newbit = 1 << bit;
+	for (word = 0; word < 4; ++word) {
+		for (bit = 0; bit < 32; ++bit) {
+			uint32_t newbit = 1U << bit;
 
-				a1.s6_addr32[word] |= newbit;	/* unequal */
-				rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
-				a2.s6_addr32[word] |= newbit;	/* equal */
-				rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
-			}
-		tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
+			a1.s6_addr32[word] |= newbit;
+			rv &= !IN6_ARE_ADDR_EQUAL(&a1, &a2);
+			a2.s6_addr32[word] |= newbit;
+			rv &= IN6_ARE_ADDR_EQUAL(&a1, &a2);
+		}
 	}
+
+	tst_resm(rv ? TPASS : TFAIL, "IN6_ARE_ADDR_EQUAL");
 #endif /* IN6_ARE_ADDR_EQUAL */
 
 /* RFC 3542, Section 2.4 */
@@ -141,7 +120,7 @@ void do_tests(void)
 			 ptab[i].prt_name);
 	}
 /* RFC 3542, Section 3.1 */
-	csum_test("::1");
+	csum_test();
 }
 
 /*
@@ -162,10 +141,10 @@ struct tprot {
 	unsigned char tp_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct tprot) + 2048];
-unsigned char rpbuf[sizeof(struct tprot) + 2048];
+static unsigned char tpbuf[sizeof(struct tprot) + 2048];
+static unsigned char rpbuf[sizeof(struct tprot) + 2048];
 
-struct csent {
+static struct csent {
 	int cs_offset;
 	int cs_dlen;
 	int cs_setresult;	/* setsockopt expected result */
@@ -173,25 +152,24 @@ struct csent {
 	int cs_sndresult;	/* send expected result */
 	int cs_snderrno;	/* send expected errno */
 } cstab[] = {
-	{
-	0, 5, 0, 0, 0, 0}, {
-	6, 30, 0, 0, 0, 0}, {
-	3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
-	{
-	4, 5, 0, 0, -1, EINVAL},	/* not enough space */
-	{
-	50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
-	{
-	22, 30, 0, 0, 0, 0}, {
-	2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
+	{0, 5, 0, 0, 0, 0},
+	{6, 30, 0, 0, 0, 0},
+	{3, 20, -1, EINVAL, -1, -1},	/* non-aligned offset */
+	{4, 5, 0, 0, -1, EINVAL},	/* not enough space */
+	{50, 5, 0, 0, -1, EINVAL},	/* outside of packet */
+	{22, 30, 0, 0, 0, 0},
+	{2000, 2004, 0, 0, 0, 0},	/* in a fragment (over Ethernet) */
 };
 
-#define CSCOUNT	(sizeof(cstab)/sizeof(cstab[0]))
+#define CSCOUNT	ARRAY_SIZE(cstab)
 
-static int recvtprot(int sd, unsigned char *packet, int psize)
+int TST_TOTAL = PTCOUNT + CSCOUNT;
+
+static int recvtprot(int sd, unsigned char *packet)
 {
 	struct tprot *tpt;
-	int cc, total, expected;
+	int cc;
+	unsigned int total, expected;
 	int gothead;
 
 	tpt = (struct tprot *)packet;
@@ -210,15 +188,17 @@ static int recvtprot(int sd, unsigned char *packet, int psize)
 				gothead = 1;
 				expected += ntohl(tpt->tp_dlen);
 			}
-		} else
+		} else {
 			break;
+		}
 	}
 	if (cc < 0)
 		return cc;
 	return total;
 }
 
-unsigned short csum(unsigned short partial, unsigned char *packet, int len)
+static unsigned short csum(unsigned short partial, unsigned char *packet,
+			   int len)
 {
 	unsigned long sum = partial;
 	unsigned short *ps;
@@ -234,7 +214,7 @@ unsigned short csum(unsigned short partial, unsigned char *packet, int len)
 	return ~sum;
 }
 
-struct ph {
+static struct ph {
 	struct in6_addr ph_sa;
 	struct in6_addr ph_da;
 	uint32_t ph_len;
@@ -242,13 +222,14 @@ struct ph {
 	uint8_t ph_nh;
 } ph;
 
-static int client(int prot, int sfd)
+static int client(int sfd)
 {
 	struct tprot *pttp = (struct tprot *)tpbuf;
 	struct tprot *prtp = (struct tprot *)rpbuf;
 	struct sockaddr_in6 rsin6;
 	static int seq;
-	int i, sd, cc, cs;
+	unsigned int i;
+	int sd, cc, cs;
 
 	memset(&rsin6, 0, sizeof(rsin6));
 	rsin6.sin6_family = AF_INET6;
@@ -259,11 +240,8 @@ static int client(int prot, int sfd)
 	ph.ph_da = rsin6.sin6_addr;
 	ph.ph_nh = NH_TEST;
 
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		tst_resm(TBROK, "can't create raw socket: %s", strerror(errno));
-		return -1;
-	}
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	for (i = 0; i < CSCOUNT; ++i) {
 		int offset, len, xlen;
 		int rv;
@@ -322,9 +300,9 @@ static int client(int prot, int sfd)
 				 offset, len);
 			continue;
 		}
-		while ((cc = recvtprot(sfd, rpbuf, sizeof(rpbuf)))) {
-			if (htonl(prtp->tp_pid) == pid &&
-			    htonl(prtp->tp_seq) == seq)
+		while ((cc = recvtprot(sfd, rpbuf))) {
+			if (htonl(prtp->tp_pid) == (uint32_t)pid &&
+			    htonl(prtp->tp_seq) == (uint32_t)seq)
 				break;
 		}
 		rv = 1;
@@ -334,7 +312,7 @@ static int client(int prot, int sfd)
 				continue;
 			if (*p != 0xa5) {
 				tst_resm(TFAIL, "IPV6_CHECKSUM corrupt data "
-					 "0x%02x != 0xa5 at offset %d in packet",
+					 "0x%02x != 0xa5 at offset %zd in packet",
 					 *p, p - rpbuf);
 				rv = 0;
 				break;
@@ -356,99 +334,60 @@ static int client(int prot, int sfd)
 }
 
 static int listen_fd, connect_fd;
-sem_t ilsem;
 
-void *ilistener(void *arg)
+static void *ilistener(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	connect_fd = accept(listen_fd, 0, 0);
 	close(listen_fd);
-	sem_post(&ilsem);
 	return NULL;
 }
 
-int isocketpair(int pf, int type, int proto, int fd[2])
+static void isocketpair(int pf, int type, int proto, int fd[2])
 {
 	pthread_t thid;
 	struct sockaddr_in sin4;
 	socklen_t namelen;
 
-/* restrict to PF_INET for now */
-	if (pf != PF_INET) {
-		errno = EOPNOTSUPP;
-		return -1;
-	}
-	sem_init(&ilsem, 0, 0);
-	listen_fd = socket(pf, type, proto);
-	if (listen_fd < 0) {
-		perror("socket");
-		return -1;
-	}
+	listen_fd = SAFE_SOCKET(NULL, pf, type, proto);
+
 	memset(&sin4, 0, sizeof(sin4));
-	if (bind(listen_fd, (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("bind");
-		return -1;
-	}
-	if (listen(listen_fd, 10) < 0) {
-		perror("listen");
-		return -1;
-	}
+
+	SAFE_BIND(NULL, listen_fd, (struct sockaddr *)&sin4, sizeof(sin4));
+
+	SAFE_LISTEN(NULL, listen_fd, 10);
+
 	namelen = sizeof(sin4);
-	if (getsockname(listen_fd, (struct sockaddr *)&sin4, &namelen) < 0) {
-		perror("getsockname");
-		return -1;
-	}
-	if (pthread_create(&thid, 0, ilistener, 0) < 0) {
-		perror("pthread_create");
-		return -1;
-	}
+	SAFE_GETSOCKNAME(NULL, listen_fd, (struct sockaddr *)&sin4, &namelen);
+
+	if (pthread_create(&thid, 0, ilistener, 0) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "pthread_create error");
+
+	fd[0] = SAFE_SOCKET(NULL, pf, type, proto);
 
-	fd[0] = socket(pf, type, proto);
-	if (fd[0] < 0) {
-		perror("socket");
-		return -1;
-	}
 	sin4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	if (connect(fd[0], (struct sockaddr *)&sin4, sizeof(sin4)) < 0) {
-		perror("connect");
-		return -1;
-	}
-	sem_wait(&ilsem);
+
+	SAFE_CONNECT(NULL, fd[0], (struct sockaddr *)&sin4, sizeof(sin4));
+
+	pthread_join(thid, NULL);
+
 	fd[1] = connect_fd;
-	sem_destroy(&ilsem);
-	return 0;
 }
 
 #ifndef MAX
 #define MAX(a, b) (@ >= (b) ? (a) : (b))
 #endif /* MAX */
 
-int csum_test(char *rhost)
+static void csum_test(void)
 {
 	fd_set rset, rset_save;
 	int csd[2];		/* control sockets */
 	int sd, nfds, maxfd, cc;
 	struct timeval tv;
 
-/* rhost == loopback, for now */
-	if (strcmp(rhost, "::1")) {
-		tst_resm(TBROK, "invalid rhost \"%s\"", rhost);
-		return -1;
-	}
-	if (isocketpair(PF_INET, SOCK_STREAM, 0, csd) < 0) {
-		tst_resm(TBROK, "socketpair: %s", strerror(errno));
-		return -1;
-	}
-	sd = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (sd < 0) {
-		int saved_errno = errno;
-
-		if (errno == EPERM && geteuid())
-			tst_resm(TBROK, "IPV6_CHECKSUM tests must run as root");
-		else
-			tst_resm(TBROK, "All IPv6_CHECKSUM tests broken: "
-				 "socket: %s", strerror(saved_errno));
-		return -1;
-	}
+	isocketpair(PF_INET, SOCK_STREAM, 0, csd);
+
+	sd = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
 	FD_ZERO(&rset_save);
 	FD_SET(sd, &rset_save);
 	FD_SET(csd[1], &rset_save);
@@ -457,16 +396,16 @@ int csum_test(char *rhost)
 
 	/* server socket set; now start the client */
 	switch (fork()) {
-	case 0:		/* child */
+	case 0:
 		close(csd[0]);
 		break;
 	case -1:
-		tst_resm(TBROK, "can't fork rserver");
-		return -1;
-	default:		/* parent */
+		tst_brkm(TBROK, NULL, "can't fork rserver");
+	default:
 		close(sd);
 		close(csd[1]);
-		return client(pid, csd[0]);
+		client(csd[0]);
+		return;
 	}
 
 	tv.tv_sec = READ_TIMEOUT;
@@ -478,7 +417,7 @@ int csum_test(char *rhost)
 			exit(0);
 		} else if (nfds == 0) {
 			fprintf(stderr, "server read timed out");
-			return -1;
+			return;
 		}
 		if (FD_ISSET(sd, &rset)) {
 			static char packet[2048];
@@ -499,9 +438,8 @@ int csum_test(char *rhost)
 			char buf[2048];
 
 			cc = read(csd[1], buf, sizeof(buf));
-			if (cc == 0) {
+			if (cc == 0)
 				exit(0);
-			}
 			if (cc < 0) {
 				perror("server read");
 				exit(1);
@@ -512,16 +450,11 @@ int csum_test(char *rhost)
 		tv.tv_sec = READ_TIMEOUT;
 		tv.tv_usec = 0;
 	}
-	return 0;
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
-}
+	TEST_PAUSE;
 
-void cleanup(void)
-{
+	pid = getpid();
 }
-
-int TST_TOTAL = PTCOUNT + CSCOUNT;
Signature · 1 line
1.9.3
#34 lore raw

PATCH v7 4/7 lib6/asapi_05.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Modify the format of etab[] and ftab[].
* Some cleanup.

---
 testcases/network/lib6/asapi_05.c | 159 ++++++++++++++------------------------
 1 file changed, 60 insertions(+), 99 deletions(-)
testcases/network/lib6/asapi_05.c +60 -99
index e251eee..f8c30f1 100644
@@ -1,38 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_05
- *
- * Test Description:
- *  These tests are for the "Advanced Sockets API" (RFC 3542)
- *  Verify that in6 and sockaddr fields are present.
- *
- * Usage:  <for command-line>
- *  asapi_05
- *
- * HISTORY
- *	04/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <stdio.h>
@@ -47,86 +30,74 @@
 #include <netinet/icmp6.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_05";	/* Test program identifier.    */
+char *TCID = "asapi_05";
 
-void setup(void);
-void cleanup(void);
+static void setup(void);
 
-void icmp6_ft(void);
+static void icmp6_ft(void);
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		icmp6_ft();
-	}
-
-	cleanup();
 
 	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;		/* if -P option specified */
+	TEST_PAUSE;
 	tst_require_root();
 }
 
-void cleanup(void)
-{
-}
-
-/*
- * This is for old, broken glibc-header icmp6_filter structure definitions.
- * If icmp6.h has struct icmp6_filter with field named "data" instead
- * of the standard "icmp_filt", uncomment this line.
- */
-/*#define icmp_filt	data */
-
-enum tt { T_WILLPASS, T_WILLBLOCK, T_SETPASS, T_SETBLOCK, T_SETPASSALL,
+enum tt {
+	T_WILLPASS,
+	T_WILLBLOCK,
+	T_SETPASS,
+	T_SETBLOCK,
+	T_SETPASSALL,
 	T_SETBLOCKALL
 };
-struct ftent {
-	char *ft_tname;		/* test name, for logging */
+
+static struct ftent {
+	char *ft_tname;			/* test name, for logging */
 	unsigned char ft_sndtype;	/* send type field */
 	unsigned char ft_flttype;	/* filter type field */
-	enum tt ft_test;	/* what macro to test */
-	int ft_expected;	/* packet should pass? */
+	enum tt ft_test;		/* what macro to test */
+	int ft_expected;		/* packet should pass? */
 } ftab[] = {
-	{
-	"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1}, {
-	"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0}, {
-	"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0}, {
-	"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1}, {
-	"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0}, {
-"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},};
-
-#define FTCOUNT	(sizeof(ftab)/sizeof(ftab[0]))
-
-int ic6_send1(char *tname, unsigned char type)
+	{"ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1},
+	{"ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0},
+	{"ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1},
+	{"ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0},
+	{"ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1},
+	{"ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0},
+	{"ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},
+};
+
+#define FTCOUNT	ARRAY_SIZE(ftab)
+
+static int ic6_send1(char *tname, unsigned char type)
 {
 	struct sockaddr_in6 sin6;
 	struct icmp6_hdr ic6;
 	int s;
 
-	s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (s == -1) {
-		tst_resm(TBROK, "%s: socket failed", tname);
-		return 1;
-	}
+	s = SAFE_SOCKET(NULL, AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+
 	memset(&ic6, 0, sizeof(ic6));
 	ic6.icmp6_type = type;
 	ic6.icmp6_data32[0] = htonl(getpid());
@@ -142,7 +113,7 @@ int ic6_send1(char *tname, unsigned char type)
 	return 0;
 }
 
-int ic6_recv1(char *tname, int sall, int sf)
+static int ic6_recv1(char *tname, int sall, int sf)
 {
 	fd_set readfds, readfds_saved;
 	struct timeval tv;
@@ -185,7 +156,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotall = 1;
 		}
 		if (FD_ISSET(sf, &readfds)) {
@@ -196,7 +167,7 @@ int ic6_recv1(char *tname, int sall, int sf)
 				return -1;
 			}
 			/* if packet check succeeds... */
-			if (htonl(pic6->icmp6_data32[0]) == getpid())
+			if (htonl(pic6->icmp6_data32[0]) == (uint32_t)getpid())
 				gotone = 1;
 		}
 		memcpy(&readfds, &readfds_saved, sizeof(readfds));
@@ -211,18 +182,14 @@ int ic6_recv1(char *tname, int sall, int sf)
 }
 
 /* functional tests */
-void icmp6_ft(void)
+static void icmp6_ft(void)
 {
 	struct icmp6_filter i6f;
 	int sall, sf;
-	int i;
+	unsigned int i;
+
+	sall = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
-	sall = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sall < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create sall socket");
-		return;
-	}
 	ICMP6_FILTER_SETPASSALL(&i6f);
 	if (setsockopt(sall, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f,
 		       sizeof(i6f)) < 0) {
@@ -230,12 +197,7 @@ void icmp6_ft(void)
 			 "setsockopt pass all ICMP6_FILTER failed");
 	}
 
-	sf = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-	if (sf < 0) {
-		tst_resm(TBROK | TERRNO,
-			 "icmp6_ft socket: can't create test socket");
-		return;
-	}
+	sf = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
 	int rv;
 
@@ -284,8 +246,9 @@ void icmp6_ft(void)
 			if (ic6_send1(ftab[i].ft_tname, ftab[i].ft_sndtype))
 				continue;
 			rv = ic6_recv1(ftab[i].ft_tname, sall, sf);
-		} else
+		} else {
 			rv = -1;
+		}
 
 		if (rv < 0)
 			continue;
@@ -296,5 +259,3 @@ void icmp6_ft(void)
 			tst_resm(TPASS, "%s", ftab[i].ft_tname);
 	}
 }
-
-int TST_TOTAL = FTCOUNT;
Signature · 1 line
1.9.3
#35 lore raw

PATCH v7 5/7 lib6/asapi_06.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup().
* Remove useless comments.
* Use SAFE_XXX().
* Remove unused variables.
* Modify the format of sotab[] and cmtab[].
* Modify the initialization of sotab[].
* Remove IPV6_PKTINFO test. IPV6_PKTINFO is only implemented by setsockopt() not by getsockopt(). see:
https://github.com/torvalds/linux/commit/b24a2516d10751d7ed5afb58420df25370c9dffb
* Some cleanup.

---
 testcases/network/lib6/asapi_06.c | 473 ++++++++------------------------------
 1 file changed, 99 insertions(+), 374 deletions(-)
testcases/network/lib6/asapi_06.c +99 -374
index 960ebb5..c6e2c8e 100644
@@ -1,37 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: asapi_06
- *
- * Test Description:
- *  Tests for RFC 3542 section 4 socket options and ancillary data
- *
- * Usage:  <for command-line>
- *  asapi_06
- *
- * HISTORY
- *	05/2005 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include "config.h"
@@ -57,25 +41,16 @@
 #include <arpa/inet.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
-char *TCID = "asapi_06";	/* Test program identifier.    */
+char *TCID = "asapi_06";
 
 int TST_TOTAL = 1;
 
-pid_t pid;
-
-struct {
-	char *prt_name;
-	int prt_value;
-} ptab[] = {
-};
-
-#define PTCOUNT	(sizeof(ptab)/sizeof(ptab[0]))
-
 #define READ_TIMEOUT	5	/* secs */
 
-void do_tests(void);
-void setup(void), cleanup(void);
+static void do_tests(void);
+static void setup(void);
 
 int main(int argc, char *argv[])
 {
@@ -83,14 +58,11 @@ int main(int argc, char *argv[])
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 
-	pid = getpid();
-
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc)
 		do_tests();
 
-	cleanup();
 	tst_exit();
 }
 
@@ -150,7 +122,7 @@ union soval {
 #define IN6_ANY		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 
 /* so_clrval and so_setval members are initilized in the body */
-struct soent {
+static struct soent {
 	char *so_tname;
 	int so_opt;
 	int so_dorecv;		/* do receive test? */
@@ -160,73 +132,52 @@ struct soent {
 	union soval so_setval;
 	socklen_t so_valsize;
 } sotab[] = {
-/* RFC 3542, Section 4 */
-	{
-		"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* make sure TCLASS stays when setting another opt */
-	{
-		"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)},
-/* OLD values */
-	{
-		"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-	0}}}}}, sizeof(int)}, {
-		"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1, { { { { {
-		0}}}}}, { { { { {
-0}}}}}, sizeof(int)},};
-
-#define SOCOUNT	(sizeof(sotab)/sizeof(sotab[0]))
+	/* RFC 3542, Section 4 */
+	{"IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, 1, IPV6_PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT, 1, IPV6_HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVRTHDR", IPV6_RECVRTHDR, 0, IPV6_RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS, 0, IPV6_HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS, 0, IPV6_DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_RECVTCLASS", IPV6_RECVTCLASS, 1, IPV6_TCLASS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* make sure TCLASS stays when setting another opt */
+	{"IPV6_RECVTCLASS (2)", IPV6_RECVHOPLIMIT, 1, IPV6_TCLASS, 0,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	/* OLD values */
+	{"IPV6_2292PKTINFO", IPV6_2292PKTINFO, 1, IPV6_2292PKTINFO, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPLIMIT", IPV6_2292HOPLIMIT, 1, IPV6_2292HOPLIMIT, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292RTHDR", IPV6_2292RTHDR, 0, IPV6_2292RTHDR, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292HOPOPTS", IPV6_2292HOPOPTS, 0, IPV6_2292HOPOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+	{"IPV6_2292DSTOPTS", IPV6_2292DSTOPTS, 0, IPV6_2292DSTOPTS, 1,
+	 {{{{{0} } }, 0} }, {{{{{0} } }, 0} }, sizeof(int)},
+};
+
+#define SOCOUNT	ARRAY_SIZE(sotab)
 
 struct soprot {
-	int sop_pid;		/* sender PID */
-	int sop_seq;		/* sequence # */
-	int sop_dlen;		/* tp_dat length */
+	int sop_pid;			/* sender PID */
+	int sop_seq;			/* sequence # */
+	int sop_dlen;			/* tp_dat length */
 	unsigned char sop_dat[0];	/* user data */
 };
 
-unsigned char tpbuf[sizeof(struct soprot) + 2048];
-unsigned char rpbuf[sizeof(struct soprot) + 2048];
+static unsigned char tpbuf[sizeof(struct soprot) + 2048];
+static unsigned char rpbuf[sizeof(struct soprot) + 2048];
 
-unsigned char control[2048];
-int clen;
+static unsigned char control[2048];
 
-int seq;
+static int seq;
 
-int setupso(void)
-{
-/* add routing headers, other ancillary data here */
-	return 0;
-}
-
-struct cme {
+static struct cme {
 	int cm_len;
 	int cm_level;
 	int cm_type;
@@ -235,26 +186,21 @@ struct cme {
 		uint32_t cmu_hops;
 	} cmu;
 } cmtab[] = {
-	{
-		sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {
-		0x12}
-	}
-	, {
-		sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {
-		0x21}
-	}
-,};
+	{sizeof(uint32_t), SOL_IPV6, IPV6_TCLASS, {0x12} },
+	{sizeof(uint32_t), SOL_IPV6, IPV6_HOPLIMIT, {0x21} },
+};
 
-#define CMCOUNT	(sizeof(cmtab)/sizeof(cmtab[0]))
+#define CMCOUNT	ARRAY_SIZE(cmtab)
 
-ssize_t sendall(int st)
+static ssize_t sendall(int st)
 {
 	struct sockaddr_in6 sin6;
 	struct msghdr msg;
 	struct iovec iov;
 	struct soprot *psop;
 	unsigned char *pd;
-	int i, ctotal;
+	unsigned int i;
+	int ctotal;
 
 	psop = (struct soprot *)tpbuf;
 	psop->sop_pid = htonl(getpid());
@@ -291,7 +237,7 @@ ssize_t sendall(int st)
 	return sendmsg(st, &msg, 0);
 }
 
-void so_test(struct soent *psoe)
+static void so_test(struct soent *psoe)
 {
 	struct sockaddr_in6 sin6;
 	union soval sobuf;
@@ -300,63 +246,51 @@ void so_test(struct soent *psoe)
 	int st;
 
 	if (psoe->so_opt == -1) {
-		tst_resm(TBROK, "%s not present at compile time",
+		tst_brkm(TBROK | TERRNO, NULL, "%s not present at compile time",
 			 psoe->so_tname);
-		return;
 	}
 	if (psoe->so_clear || sr < 0) {
 		if (sr < 0)
 			close(sr);
-		sr = socket(PF_INET6, SOCK_RAW, NH_TEST);
-		if (sr < 0) {
-			tst_resm(TBROK, "%s socket: %s", psoe->so_tname,
-				 strerror(errno));
-			return;
-		}
+		sr = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
 	}
 	memset(&sin6, 0, sizeof(sin6));
 	sin6.sin6_family = AF_INET6;
 	sin6.sin6_addr = in6addr_loopback;
-	if (bind(sr, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
-		tst_resm(TBROK, "%s: bind: %s", psoe->so_tname,
-			 strerror(errno));
-	}
+
+	SAFE_BIND(NULL, sr, (struct sockaddr *)&sin6, sizeof(sin6));
+
 	if (setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_clrval,
 		       psoe->so_valsize) < 0) {
-		tst_resm(TBROK, "%s: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
+		tst_brkm(TBROK | TERRNO, NULL, "%s: setsockopt",
+			 psoe->so_tname);
 	}
+
 	TEST(setsockopt(sr, SOL_IPV6, psoe->so_opt, &psoe->so_setval,
 			psoe->so_valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL, "%s set-get: setsockopt: %s", psoe->so_tname,
-			 strerror(errno));
+		tst_resm(TFAIL | TERRNO, "%s set-get: setsockopt",
+			 psoe->so_tname);
 		return;
 	}
+
 	valsize = psoe->so_valsize;
 	TEST(getsockopt(sr, SOL_IPV6, psoe->so_opt, &sobuf, &valsize));
 	if (TEST_RETURN != 0) {
-		tst_resm(TBROK, "%s set-get: getsockopt: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize))
+		tst_brkm(TBROK | TERRNO, NULL, "%s set-get: getsockopt",
+			 psoe->so_tname);
+	} else if (memcmp(&psoe->so_setval, &sobuf, psoe->so_valsize)) {
 		tst_resm(TFAIL, "%s set-get optval != setval", psoe->so_tname);
-	else
+	} else {
 		tst_resm(TPASS, "%s set-get", psoe->so_tname);
-
-	st = socket(PF_INET6, SOCK_RAW, NH_TEST);
-	if (st < 0) {
-		tst_resm(TBROK, "%s transmit socket: %s", psoe->so_tname,
-			 strerror(errno));
-		return;
-	}
-	if (sendall(st) < 0) {
-		tst_resm(TBROK, "%s transmit sendto: %s", psoe->so_tname,
-			 strerror(errno));
-		close(st);
-		return;
 	}
+
+	st = SAFE_SOCKET(NULL, PF_INET6, SOCK_RAW, NH_TEST);
+
+	if (sendall(st) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "%s transmit sendto",
+			 psoe->so_tname);
+
 	close(st);
 
 	/* receiver processing */
@@ -382,12 +316,11 @@ void so_test(struct soent *psoe)
 			if (nfds < 0) {
 				if (errno == EINTR)
 					continue;
-				tst_resm(TBROK, "%s select: %s", psoe->so_tname,
-					 strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s select",
+					 psoe->so_tname);
 			}
 			if (nfds == 0) {
-				tst_resm(TBROK, "%s recvmsg timed out",
+				tst_brkm(TBROK, NULL, "%s recvmsg timed out",
 					 psoe->so_tname);
 				return;
 			}
@@ -405,11 +338,10 @@ void so_test(struct soent *psoe)
 
 			cc = recvmsg(sr, &msg, 0);
 			if (cc < 0) {
-				tst_resm(TBROK, "%s recvmsg: %s",
-					 psoe->so_tname, strerror(errno));
-				return;
+				tst_brkm(TBROK | TERRNO, NULL, "%s recvmsg",
+					 psoe->so_tname);
 			}
-/* check pid & seq here */
+			/* check pid & seq here */
 			break;
 		}
 		gotone = 0;
@@ -419,9 +351,9 @@ void so_test(struct soent *psoe)
 				break;
 			gotone = pcmsg->cmsg_level == SOL_IPV6 &&
 			    pcmsg->cmsg_type == psoe->so_cmtype;
-			if (gotone)
+			if (gotone) {
 				break;
-			else if (psoe->so_clear) {
+			} else if (psoe->so_clear) {
 				tst_resm(TFAIL, "%s receive: extraneous data "
 					 "in control: level %d type %d len %zu",
 					 psoe->so_tname, pcmsg->cmsg_level,
@@ -429,232 +361,25 @@ void so_test(struct soent *psoe)
 				return;
 			}
 		}
-/* check contents here */
+		/* check contents here */
 		if (psoe->so_dorecv)
 			tst_resm(gotone ? TPASS : TFAIL, "%s receive",
 				 psoe->so_tname);
 	}
 }
 
-#define IPV6_ADDR_NODE		1
-#define IPV6_ADDR_LINK		2
-#define IPV6_ADDR_GLOBAL	3
-
-#ifdef HAVE_IFADDRS_H
-static int ipv6_addr_scope(struct in6_addr *pin6)
-{
-	if ((ntohl(pin6->s6_addr32[0]) & 0xFFC00000) == 0xFE800000)
-		return IPV6_ADDR_LINK;
-	if (memcmp(pin6, &in6addr_loopback, sizeof(*pin6)) == 0)
-		return IPV6_ADDR_NODE;
-	return IPV6_ADDR_GLOBAL;
-}
-#endif /* HAVE_IFADDRS_H */
-
-int getsock(char *tname, struct sockaddr_in6 *psin6_arg, int scope)
-{
-#ifdef HAVE_IFADDRS_H
-	static struct ifaddrs *pifa_head;
-	struct ifaddrs *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	int ifindex = 0;
-	int s;
-
-	if (!pifa_head && getifaddrs(&pifa_head)) {
-		tst_resm(TBROK, "%s: getifaddrs failed", tname);
-		return -1;
-	}
-	if (psin6_arg)
-		ifindex = psin6_arg->sin6_scope_id;
-
-	/* first, find a global address */
-	for (pifa = pifa_head; pifa; pifa = pifa->ifa_next) {
-		int this_scope;
-
-		if (!(pifa->ifa_flags & IFF_UP))
-			continue;
-		if (pifa->ifa_addr->sa_family != AF_INET6)
-			continue;
-		psin6 = (struct sockaddr_in6 *)pifa->ifa_addr;
-		this_scope = ipv6_addr_scope(&psin6->sin6_addr);
-		if (this_scope &&
-		    ((this_scope < 0 && -this_scope == scope) ||
-		     (this_scope > 0 && this_scope != scope)))
-			continue;
-		psin6->sin6_scope_id = if_nametoindex(pifa->ifa_name);
-		if ((ifindex < 0 && -ifindex == psin6->sin6_scope_id) ||
-		    (ifindex > 0 && ifindex != psin6->sin6_scope_id))
-			continue;
-		s = socket(PF_INET6, SOCK_DGRAM, 0);
-		if (s < 0) {
-			tst_resm(TBROK, "%s: socket %s", tname,
-				 strerror(errno));
-			return -1;
-		}
-		if (bind(s, pifa->ifa_addr, sizeof(struct sockaddr_in6)) < 0) {
-			tst_resm(TBROK, "%s: bind \"%s\": %s", tname,
-				 inet_ntop(AF_INET6, &psin6->sin6_addr, strbuf,
-					   sizeof(strbuf)), strerror(errno));
-			return -1;
-		}
-		if (psin6_arg) {
-			*psin6_arg = *psin6;
-			psin6_arg->sin6_scope_id =
-			    if_nametoindex(pifa->ifa_name);
-		}
-		return s;
-	}
-	{
-		char *scopestr, *intfstr;
-
-		switch (scope) {
-		case IPV6_ADDR_NODE:
-			scopestr = " node-local";
-			break;
-		case IPV6_ADDR_LINK:
-			scopestr = " link-local";
-			break;
-		case IPV6_ADDR_GLOBAL:
-			scopestr = " global";
-			break;
-		default:
-			scopestr = "";
-			break;
-		}
-		if (ifindex < 0) {
-			intfstr = " not on ifindex";
-			ifindex = -ifindex;
-		} else if (ifindex)
-			intfstr = " on ifindex";
-		else
-			intfstr = 0;
-
-		if (intfstr)
-			tst_resm(TBROK, "%s: getsock : no%s addresses%s %d",
-				 tname, scopestr, intfstr, ifindex);
-		else
-			tst_resm(TBROK, "%s: getsock : no%s addresses",
-				 tname, scopestr);
-	}
-	return -1;
-#else /* HAVE_IFADDRS_H */
-	return -1;
-#endif
-}
-
-#ifdef notyet
-/*
- * RFC 3542 IPV6_PKTINFO not in mainline yet (as of 2.6.15). The get/set
- * tests are below, and comments for some further tests to be added later
- */
-void test_pktinfo(void)
-{
-	int s_snd, s_rcv[3] = { -1, -1, -1 };
-	struct sockaddr_in6 sa_rcv[3];
-	int s, i;
-	struct ifaddrs *pifa_head, *pifa;
-	struct sockaddr_in6 *psin6;
-	char strbuf[128];
-	char *tname = "IPV6_PKTINFO";
-	struct in6_pktinfo pi, pi_tmp;
-	int sinlen;
-	int optlen;
-
-	s_snd = getsock(tname, 0, IPV6_ADDR_GLOBAL);
-	if (s_snd < 0) {
-		tst_resm(TBROK, "%s: can't create send socket", tname);
-		return;
-	}
-	/* global-scope address, interface X */
-	sa_rcv[0].sin6_scope_id = 0;
-	s_rcv[0] = getsock(tname, &sa_rcv[0], IPV6_ADDR_GLOBAL);
-	if (s_rcv[0] == -1) {
-		tst_resm(TBROK, "%s: only link-scope addresses", tname);
-		return;
-	}
-	/* link-local-scope address, interface X */
-	sa_rcv[1].sin6_scope_id = sa_rcv[0].sin6_scope_id;
-	s_rcv[1] = getsock(tname, &sa_rcv[1], IPV6_ADDR_LINK);
-	if (s_rcv[1] < 0) {
-		tst_resm(TBROK, "%s: no link-local address on ifindex %d",
-			 tname, sa_rcv[0].sin6_scope_id);
-		return;
-	}
-	/* link-local-scope address, interface Y */
-	sa_rcv[2].sin6_scope_id = -sa_rcv[0].sin6_scope_id;
-	s_rcv[2] = getsock(tname, &sa_rcv[2], IPV6_ADDR_LINK);
-	if (s_rcv[2] < 0) {
-		tst_resm(TBROK, "%s: only one interface?", tname);
-		return;
-	}
-	/* send to rcv1 to verify communication */
-	/* force to rcv2 w/ PKTINFO */
-/* TESTS: */
-/* sticky set-get */
-	tname = "IPV6_PKTINFO set";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[1].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else
-		tst_resm(TPASS, "%s", tname);
-
-	tname = "IPV6_PKTINFO get";
-	optlen = sizeof(pi_tmp);
-	TEST(getsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi_tmp, &optlen));
-	if (TEST_RETURN != 0)
-		tst_resm(TFAIL, "%s: %s", tname, strerror(errno));
-	else if (memcmp(&pi, &pi_tmp, sizeof(pi)) != 0) {
-		char strbuf2[64];
-		tst_resm(TFAIL, "%s: {\"%s\",%d} != {\"%s\",%d}", tname,
-			 inet_ntop(AF_INET6, &pi_tmp.ipi6_addr, strbuf,
-				   sizeof(strbuf)), pi_tmp.ipi6_ifindex,
-			 inet_ntop(AF_INET6, &pi.ipi6_addr, strbuf2,
-				   sizeof(strbuf2)), pi.ipi6_ifindex);
-	} else
-		tst_resm(TPASS, "%s", tname);
-/* ancillary data override */
-/* link-local, wrong interface */
-	tname = "IPV6_PKTINFO invalid {lladdr, intf}";
-	pi.ipi6_addr = sa_rcv[1].sin6_addr;
-	pi.ipi6_ifindex = sa_rcv[2].sin6_scope_id;
-	TEST(setsockopt(s_snd, SOL_IPV6, IPV6_PKTINFO, &pi, sizeof(pi)));
-	if (TEST_RETURN == 0)
-		tst_resm(TFAIL, "%s returns success, should be -1, EINVAL",
-			 tname);
-	else if (TEST_ERRNO != EINVAL)
-		tst_resm(TFAIL, "%s errno %d != %d", tname, TEST_ERRNO, EINVAL);
-	else
-		tst_resm(TPASS, "%s", tname);
-/* nonexistent interface */
-/* non-local address */
-/* clear address */
-/* clear interface */
-/* sendmsg() sin6_scope differs with ancillary data interface */
-}
-#endif /* notyet */
-
-void do_tests(void)
+static void do_tests(void)
 {
-	int i;
+	unsigned int i;
 
 	for (i = 0; i < SOCOUNT; ++i) {
 		sotab[i].so_clrval.sou_bool = 0;
 		sotab[i].so_setval.sou_bool = 1;
 		so_test(&sotab[i]);
 	}
-#ifdef notyet
-	test_pktinfo();
-#endif /* notyet - see test_pktinfo() comment above */
-}
-
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
 }
 
-void cleanup(void)
+static void setup(void)
 {
+	TEST_PAUSE;
 }
Signature · 1 line
1.9.3
#36 lore raw

PATCH v7 6/7 lib6/getaddrinfo_01.c: Cleanup

In reply to Zeng Linggang

* Add 'static' before some functions and global variables.
* Remove cleanup() function.
* Remove useless comments.
* In every test function, using TINFO instead of TPASS marks the success of each inner test.
* Some cleanup.

---
 testcases/network/lib6/getaddrinfo_01.c | 503 ++++++++++++++++++--------------
 1 file changed, 281 insertions(+), 222 deletions(-)
testcases/network/lib6/getaddrinfo_01.c +281 -222
index ec29b5d..a812e77 100644
@@ -1,43 +1,21 @@
 /*
+ * Copyright (c) 2015 Fujitsu Ltd.
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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 2 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.
  *
- *   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, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Name: getaddrinfo_01
- *
- * Test Description:
- *  Tests for getaddrinfo library function
- *
- * Usage:  <for command-line>
- *  getaddrinfo_01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2004 written by David L Stevens
- *
- * RESTRICTIONS:
- *  None.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
+ * Author: David L Stevens
  */
 
 #include <unistd.h>
@@ -53,40 +31,38 @@
 #define AI_V4MAPPED    0x0008	/* IPv4 mapped addresses are acceptable.  */
 #endif
 
-char *TCID = "getaddrinfo_01";	/* Test program identifier.    */
-int testno;
+static void setup(void);
+static void gaiv4(void);
+static void gaiv6(void);
 
-void setup(void), cleanup(void);
-
-int TST_TOTAL = 1;
-
-/* a host that isn't where LTP is running */
-#define REMOTEHOSTNAME	"www.ibm.com"
-
-void gaiv4(void), gaiv6(void);
-void dumpres(struct addrinfo *);
+char *TCID = "getaddrinfo_01";
+int TST_TOTAL = 2;
 
 int main(int argc, char *argv[])
 {
 	int lc;
 
-	/* Parse standard options given to run the test. */
 	tst_parse_opts(argc, argv, NULL, NULL);
 
 	setup();
 
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+
 		gaiv4();
 		gaiv6();
 	}
-	cleanup();
 
-	return (0);
+	tst_exit();
 }
 
-/* getaddrinfo tests (v4) */
+static void setup(void)
+{
+	TEST_PAUSE;
+}
 
-void gaiv4(void)
+/* getaddrinfo tests (v4) */
+static void gaiv4(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -132,13 +108,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 2, IPv4 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -152,20 +133,26 @@ void gaiv4(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 3, IPv4 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -200,13 +187,18 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 4, IPv4 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -244,18 +236,22 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv4 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 5, IPv4 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -266,8 +262,11 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv4 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -306,17 +305,28 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service,"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 0+service,i"
+				" PASSIVE (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 7, IPv4 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -350,19 +360,30 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 "
+				"0+service (\"\", \"%s\") returns %ld (\"%s\")",
+				service, TEST_RETURN,
+				gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 8, IPv4 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv4 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -376,15 +397,18 @@ void gaiv4(void)
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
-	} else
-		tst_resm(TPASS,
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
+	} else {
+		tst_resm(TINFO,
 			 "getaddrinfo IPv4 host+service, AI_NUMERICSERV");
+	}
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 9, IPv4 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = SOCK_STREAM;
@@ -396,12 +420,13 @@ void gaiv4(void)
 		tst_resm(TFAIL, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv4 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 10, IPv4 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
 	hints.ai_socktype = 0;
@@ -439,20 +464,31 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv4 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv4 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv4 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 11, IPv4 AI_V4MAPPED */
-
 	/* AI_V4MAPPED should be ignored because family != AF_INET6 */
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET;
@@ -482,18 +518,25 @@ void gaiv4(void)
 				 psin ? psin->sin_family : 0,
 				 psin ? psin->sin_port : 0,
 				 psin ? htons(psin->sin_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv4 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv4 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv4 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv4 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
+
+
+	tst_resm(TPASS, "getaddrinfo tests (v4) succeed");
 }
 
 /* getaddrinfo tests (v6) */
-
-void gaiv6(void)
+static void gaiv6(void)
 {
 	struct addrinfo *aires, hints, *pai;
 	char hostname[MAXHOSTNAMELEN + 1];
@@ -539,13 +582,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 basic lookup");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 basic "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 basic lookup");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 basic "
 			 "lookup (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 13, IPv6 canonical name */
 	memset(&hints, 0, sizeof(hints));
@@ -559,20 +607,26 @@ void gaiv6(void)
 		if (!pai) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name: no "
 				 "entries with canonical name set");
+			freeaddrinfo(aires);
+			return;
 		} else if (strcasecmp(hostname, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
 				 pai->ai_canonname, hostname);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 canonical name");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 canonical name");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "canonical name (\"%s\") returns %ld (\"%s\")",
 			 shortname, TEST_RETURN, gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 14, IPv6 host+service name */
-
 	memset(&hints, 0, sizeof(hints));
 	/*
 	 * These are hard-coded for echo/7 to avoid using getservbyname(),
@@ -607,13 +661,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 host+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 host+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service returns %ld (\"%s\")", TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 15, IPv6 hostname+service, AI_PASSIVE */
 	memset(&hints, 0, sizeof(hints));
@@ -652,18 +711,22 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS,
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO,
 				 "getaddrinfo IPv6 host+service PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 host+"
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 host+"
 			 "service, PASSIVE (\"%s\", \"%s\") returns %ld (\"%s\")",
 			 hostname, service, TEST_RETURN,
 			 gai_strerror(TEST_RETURN));
+		return;
+	}
 
 	/* test 16, IPv6 host+service w/ AI_NUMERICHOST */
-
 	memset(&hints, 0, sizeof(hints));
 	strcpy(service, "echo");
 	servnum = 7;
@@ -674,8 +737,11 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname: "
 			 "returns %ld expected %d (EAI_NONAME)",
 			 TEST_RETURN, EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
+		tst_resm(TINFO, "getaddrinfo IPv6 AI_NUMERICHOST w/ hostname");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 
@@ -715,17 +781,26 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service, PASSIVE");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service, PASSIVE (\"\", \"%s\") "
-			 "returns %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service, PASSIVE"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 18, IPv6 0+service */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -760,19 +835,28 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 0+service");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 0+service (\"\", \"%s\") returns %ld "
-			 "(\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service");
+			freeaddrinfo(aires);
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 0+service"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 19, IPv6 host+service, AI_NUMERICSERV */
-
 #ifndef AI_NUMERICSERV
-	tst_resm(TFAIL, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
+	tst_resm(TCONF, "getaddrinfo IPv6 host+service, AI_NUMERICSERV: flag "
 		 "not implemented");
 #else
 	memset(&hints, 0, sizeof(hints));
@@ -786,15 +870,17 @@ void gaiv6(void)
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV: "
 			 "returns %ld (\"%s\") expected %d (EAI_NONAME)",
 			 TEST_RETURN, gai_strerror(TEST_RETURN), EAI_NONAME);
+		if (!TEST_RETURN)
+			freeaddrinfo(aires);
+		return;
 	} else
-		tst_resm(TPASS,
+		tst_resm(TINFO,
 			 "getaddrinfo IPv6 host+service, AI_NUMERICSERV");
 	if (!TEST_RETURN)
 		freeaddrinfo(aires);
 #endif /* AI_NUMERICSERV */
 
 	/* test 20, IPv6 SOCK_STREAM/IPPROTO_UDP hints */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = SOCK_STREAM;
@@ -806,12 +892,13 @@ void gaiv6(void)
 		tst_resm(TFAIL, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
 		freeaddrinfo(aires);
-	} else
-		tst_resm(TPASS, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
+		return;
+	} else {
+		tst_resm(TINFO, "getaddrinfo IPv6 SOCK_STREAM/IPPROTO_UDP "
 			 "hints");
+	}
 
 	/* test 21, IPv6 socktype 0, 513 */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_socktype = 0;
@@ -850,20 +937,31 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else if (got_tcp && got_udp)
-			tst_resm(TPASS, "getaddrinfo IPv6 socktype 0,513");
-		else
+			freeaddrinfo(aires);
+			return;
+		} else if (got_tcp && got_udp) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513");
+			freeaddrinfo(aires);
+		} else {
 			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513 TCP %d"
 				 " UDP %d", got_tcp, got_udp);
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN == EAI_BADFLAGS ? TPASS : TFAIL,
-			 "getaddrinfo IPv6 socktype 0,513 (\"\", \"%s\") returns"
-			 " %ld (\"%s\")", service, TEST_RETURN,
-			 gai_strerror(TEST_RETURN));
+			freeaddrinfo(aires);
+			return;
+		}
+	} else {
+		if (TEST_RETURN == EAI_BADFLAGS) {
+			tst_resm(TINFO, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+		} else {
+			tst_resm(TFAIL, "getaddrinfo IPv6 socktype 0,513"
+				" (\"\", \"%s\") returns %ld (\"%s\")", service,
+				TEST_RETURN, gai_strerror(TEST_RETURN));
+			return;
+		}
+	}
 
 	/* test 22, IPv6 AI_V4MAPPED */
-
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_INET6;
 	hints.ai_flags = AI_V4MAPPED;
@@ -892,57 +990,18 @@ void gaiv6(void)
 				 psin6 ? psin6->sin6_family : 0,
 				 psin6 ? psin6->sin6_port : 0,
 				 psin6 ? htons(psin6->sin6_port) : 0);
-		} else
-			tst_resm(TPASS, "getaddrinfo IPv6 AI_V4MAPPED");
-		freeaddrinfo(aires);
-	} else
-		tst_resm(TEST_RETURN ? TFAIL : TPASS, "getaddrinfo IPv6 "
+			freeaddrinfo(aires);
+			return;
+		} else {
+			tst_resm(TINFO, "getaddrinfo IPv6 AI_V4MAPPED");
+			freeaddrinfo(aires);
+		}
+	} else {
+		tst_resm(TFAIL, "getaddrinfo IPv6 "
 			 "AI_V4MAPPED (\"%s\") returns %ld (\"%s\")", hostname,
 			 TEST_RETURN, gai_strerror(TEST_RETURN));
-}
-
-/* this prints an addrinfo list; useful for debugging */
-void dumpres(struct addrinfo *pai)
-{
-	int count = 1;
-	for (; pai; pai = pai->ai_next, count++) {
-		printf("result %d [0x%p]\n", count, pai);
-		printf("\tai_flags %x\n", pai->ai_flags);
-		printf("\tai_family %d\n", pai->ai_family);
-		printf("\tai_socktype %d\n", pai->ai_socktype);
-		printf("\tai_protocol %d\n", pai->ai_protocol);
-		printf("\tai_addrlen %d\n", pai->ai_addrlen);
-		printf("\tai_canonname \"%s\"\n", pai->ai_canonname);
-		printf("\tai_addr.sa_family %x\n", pai->ai_addr->sa_family);
-		if (pai->ai_addr->sa_family == AF_INET) {
-			char buf[1024];
-			struct sockaddr_in *psin =
-			    (struct sockaddr_in *)pai->ai_addr;
-
-			if (!inet_ntop(AF_INET, &psin->sin_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin_addr \"%s\"\n", buf);
-		} else if (pai->ai_addr->sa_family == AF_INET6) {
-			char buf[1024];
-
-			struct sockaddr_in6 *psin6 =
-			    (struct sockaddr_in6 *)pai->ai_addr;
-			if (!inet_ntop(AF_INET6, &psin6->sin6_addr, buf,
-				       sizeof(buf)))
-				buf[0] = '\0';
-			printf("\tai_addr.sin6_addr \"%s\"\n", buf);
-
-		}
-		printf("\tai_next %p\n", pai->ai_next);
+		return;
 	}
-}
 
-void setup(void)
-{
-	TEST_PAUSE;		/* if -P option specified */
-}
-
-void cleanup(void)
-{
+	tst_resm(TPASS, "getaddrinfo tests (v6) succeed");
 }
Signature · 1 line
1.9.3