From patchwork Tue Mar 19 18:52:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Miller X-Patchwork-Id: 2303761 Return-Path: X-Original-To: patchwork-linux-wireless@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 77E0CDFB79 for ; Tue, 19 Mar 2013 18:53:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756755Ab3CSSxE (ORCPT ); Tue, 19 Mar 2013 14:53:04 -0400 Received: from shards.monkeyblade.net ([149.20.54.216]:47078 "EHLO shards.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755440Ab3CSSxD (ORCPT ); Tue, 19 Mar 2013 14:53:03 -0400 Received: from localhost (cpe-66-108-118-205.nyc.res.rr.com [66.108.118.205]) (Authenticated sender: davem-davemloft) by shards.monkeyblade.net (Postfix) with ESMTPSA id A290F5805ED; Tue, 19 Mar 2013 11:53:04 -0700 (PDT) Date: Tue, 19 Mar 2013 14:52:58 -0400 (EDT) Message-Id: <20130319.145258.1891905092469394685.davem@davemloft.net> To: netdev@vger.kernel.org CC: netfilter-devel@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH] net: Add socket() system call self test. From: David Miller X-Mailer: Mew version 6.5 on Emacs 24.1 / Mule 6.0 (HANACHIRUSATO) Mime-Version: 1.0 X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (shards.monkeyblade.net [0.0.0.0]); Tue, 19 Mar 2013 11:53:05 -0700 (PDT) Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Signed-off-by: David S. Miller --- As mentioned during the netfilter workshop, we will be adding all sorts of networking tests now that 3.9.x has a selftest framework in place. The first test I'm adding to net-next does some very simple testing of the socket() system call. Feel free to send patches for more tests, making the run script more powerful (perhaps by taking a whitespace seperated list of tests to run on the command line), and adding more checks to the socket.c test. Thanks. tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net-socket/Makefile | 16 ++++ .../testing/selftests/net-socket/run_netsocktests | 12 +++ tools/testing/selftests/net-socket/socket.c | 92 ++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 tools/testing/selftests/net-socket/Makefile create mode 100644 tools/testing/selftests/net-socket/run_netsocktests create mode 100644 tools/testing/selftests/net-socket/socket.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 3cc0ad7..7c6280f 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -5,6 +5,7 @@ TARGETS += vm TARGETS += cpu-hotplug TARGETS += memory-hotplug TARGETS += efivarfs +TARGETS += net-socket all: for TARGET in $(TARGETS); do \ diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile new file mode 100644 index 0000000..f27ee10 --- /dev/null +++ b/tools/testing/selftests/net-socket/Makefile @@ -0,0 +1,16 @@ +# Makefile for net-socket selftests + +CC = $(CROSS_COMPILE)gcc +CFLAGS = -Wall + +NET_SOCK_PROGS = socket + +all: $(NET_SOCK_PROGS) +%: %.c + $(CC) $(CFLAGS) -o $@ $^ + +run_tests: all + @/bin/sh ./run_netsocktests || echo "vmtests: [FAIL]" + +clean: + $(RM) $(NET_SOCK_PROGS) diff --git a/tools/testing/selftests/net-socket/run_netsocktests b/tools/testing/selftests/net-socket/run_netsocktests new file mode 100644 index 0000000..c09a682 --- /dev/null +++ b/tools/testing/selftests/net-socket/run_netsocktests @@ -0,0 +1,12 @@ +#!/bin/bash + +echo "--------------------" +echo "running socket test" +echo "--------------------" +./socket +if [ $? -ne 0 ]; then + echo "[FAIL]" +else + echo "[PASS]" +fi + diff --git a/tools/testing/selftests/net-socket/socket.c b/tools/testing/selftests/net-socket/socket.c new file mode 100644 index 0000000..0f227f2 --- /dev/null +++ b/tools/testing/selftests/net-socket/socket.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +#include + +struct socket_testcase { + int domain; + int type; + int protocol; + + /* 0 = valid file descriptor + * -foo = error foo + */ + int expect; + + /* If non-zero, accept EAFNOSUPPORT to handle the case + * of the protocol not being configured into the kernel. + */ + int nosupport_ok; +}; + +static struct socket_testcase tests[] = { + { AF_MAX, 0, 0, -EAFNOSUPPORT, 0 }, + { AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 1 }, + { AF_INET, SOCK_DGRAM, IPPROTO_TCP, -EPROTONOSUPPORT, 1 }, + { AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 1 }, + { AF_INET, SOCK_STREAM, IPPROTO_UDP, -EPROTONOSUPPORT, 1 }, +}; + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#define ERR_STRING_SZ 64 + +static int run_tests(void) +{ + char err_string1[ERR_STRING_SZ]; + char err_string2[ERR_STRING_SZ]; + int i, err; + + err = 0; + for (i = 0; i < ARRAY_SIZE(tests); i++) { + struct socket_testcase *s = &tests[i]; + int fd; + + fd = socket(s->domain, s->type, s->protocol); + if (fd < 0) { + if (s->nosupport_ok && + errno == EAFNOSUPPORT) + continue; + + if (s->expect < 0 && + errno == -s->expect) + continue; + + strerror_r(-s->expect, err_string1, ERR_STRING_SZ); + strerror_r(errno, err_string2, ERR_STRING_SZ); + + fprintf(stderr, "socket(%d, %d, %d) expected " + "err (%s) got (%s)\n", + s->domain, s->type, s->protocol, + err_string1, err_string2); + + err = -1; + break; + } else { + close(fd); + + if (s->expect < 0) { + strerror_r(errno, err_string1, ERR_STRING_SZ); + + fprintf(stderr, "socket(%d, %d, %d) expected " + "success got err (%s)\n", + s->domain, s->type, s->protocol, + err_string1); + + err = -1; + break; + } + } + } + + return err; +} + +int main(void) +{ + int err = run_tests(); + + return err; +}