Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 368317
b: refs/heads/master
c: b0aa73b
h: refs/heads/master
i:
  368315: 40168c3
v: v3
  • Loading branch information
David S. Miller committed Mar 19, 2013
1 parent b207e5d commit 33db807
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: b5fb82c48b5898c50a9cf75fc957911b56fe1dc5
refs/heads/master: b0aa73bf081da6810dacd750b9f8186640e172db
1 change: 1 addition & 0 deletions trunk/tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TARGETS += vm
TARGETS += cpu-hotplug
TARGETS += memory-hotplug
TARGETS += efivarfs
TARGETS += net-socket

all:
for TARGET in $(TARGETS); do \
Expand Down
16 changes: 16 additions & 0 deletions trunk/tools/testing/selftests/net-socket/Makefile
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions trunk/tools/testing/selftests/net-socket/run_netsocktests
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

echo "--------------------"
echo "running socket test"
echo "--------------------"
./socket
if [ $? -ne 0 ]; then
echo "[FAIL]"
else
echo "[PASS]"
fi

92 changes: 92 additions & 0 deletions trunk/tools/testing/selftests/net-socket/socket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

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;
}

0 comments on commit 33db807

Please sign in to comment.