]> granicus.if.org Git - strace/commitdiff
tests: add a test for basic network syscalls
authorDmitry V. Levin <ldv@altlinux.org>
Tue, 7 May 2013 23:32:01 +0000 (23:32 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 8 May 2013 02:06:39 +0000 (02:06 +0000)
* tests/.gitignore: Add net-accept-connect, *.o and *.log.*.
* tests/Makefile.am (AM_CFLAGS): New variable.
(check_PROGRAMS): Add net-accept-connect.
(TESTS): Add net.
* tests/net-accept-connect.c: New file.
* tests/net: New file.

tests/.gitignore
tests/Makefile.am
tests/net [new file with mode: 0755]
tests/net-accept-connect.c [new file with mode: 0644]

index 7e563b8b3023b7b9cc879c0052dfcc26b9f82505..947455d2820b4c16c9174788540a369608b71920 100644 (file)
@@ -1,2 +1,5 @@
+net-accept-connect
 *.log
+*.log.*
+*.o
 *.trs
index aaf103a27c82aed42af1d74c535abe57df212e00..a7f584d362fc966b67cd97ce324f2be5cd2bc65c 100644 (file)
@@ -1,6 +1,10 @@
 # Automake input for strace tests.
 
-TESTS = ptrace_setoptions strace-f qual_syscall stat
+AM_CFLAGS = $(WARN_CFLAGS)
+
+check_PROGRAMS = net-accept-connect
+
+TESTS = ptrace_setoptions strace-f qual_syscall stat net
 
 EXTRA_DIST = init.sh $(TESTS)
 
diff --git a/tests/net b/tests/net
new file mode 100755 (executable)
index 0000000..3fb10ba
--- /dev/null
+++ b/tests/net
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+# Check how network syscalls are traced.
+
+. "${srcdir=.}/init.sh"
+
+check_strace
+check_timeout
+check_prog grep
+check_prog rm
+
+rm -f check.log.*
+
+$TIMEOUT ./net-accept-connect ||
+       fail_ 'net-accept-connect failed'
+
+args='-tt -ff -o check.log -enetwork ./net-accept-connect'
+$TIMEOUT $STRACE $args ||
+       fail_ "strace $args failed"
+
+"$srcdir"/../strace-log-merge check.log > check.log || {
+       cat check.log
+       fail_ 'strace-log-merge failed'
+}
+
+rm -f check.log.*
+
+grep_log()
+{
+       local syscall="$1"; shift
+       local prefix='[1-9][0-9]* +[0-9]+:[0-9]+:[0-9]+\.[0-9]+ +'
+
+       LC_ALL=C grep -E -x "$prefix$syscall$@" check.log > /dev/null || {
+               cat check.log
+               fail_ "strace -enetwork failed to trace \"$syscall\" properly"
+       }
+}
+
+grep_log socket '\(PF_(LOCAL|UNIX|FILE), SOCK_STREAM, 0\) += 0'
+grep_log socket '\(PF_(LOCAL|UNIX|FILE), SOCK_STREAM, 0\) += 1'
+grep_log bind '\(0, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, 110\) += 0'
+grep_log listen '\(0, 5\) += 0'
+grep_log getsockname '\(0, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, \[15\]\) += 0'
+grep_log accept '\(0, \{sa_family=AF_(LOCAL|UNIX|FILE), NULL\}, \[2\]\) += 1'
+grep_log connect '\(1, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, 110\) += 0'
+
+exit 0
diff --git a/tests/net-accept-connect.c b/tests/net-accept-connect.c
new file mode 100644 (file)
index 0000000..9d296f7
--- /dev/null
@@ -0,0 +1,48 @@
+#include <assert.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#define SUN_PATH "local-stream"
+int main(void)
+{
+       struct sockaddr_un addr = {
+               .sun_family = AF_UNIX,
+               .sun_path = SUN_PATH
+       };
+       socklen_t len = sizeof addr;
+
+       unlink(SUN_PATH);
+       close(0);
+       close(1);
+
+       assert(socket(PF_LOCAL, SOCK_STREAM, 0) == 0);
+       assert(bind(0, (struct sockaddr *) &addr, len) == 0);
+       assert(listen(0, 5) == 0);
+
+       memset(&addr, 0, sizeof addr);
+       assert(getsockname(0, (struct sockaddr *) &addr, &len) == 0);
+
+       pid_t pid = fork();
+       assert(pid >= 0);
+
+       if (pid) {
+               assert(accept(0, (struct sockaddr *) &addr, &len) == 1);
+               assert(close(0) == 0);
+               int status;
+               assert(waitpid(pid, &status, 0) == pid);
+               assert(status == 0);
+               assert(close(1) == 0);
+       } else {
+               assert(socket(PF_LOCAL, SOCK_STREAM, 0) == 1);
+               assert(close(0) == 0);
+               assert(connect(1, (struct sockaddr *) &addr, sizeof addr) == 0);
+               assert(close(1) == 0);
+               return 0;
+       }
+
+       unlink(SUN_PATH);
+       return 0;
+}