From 6077ad8cc6c7cdcbbb5c13532102b939fdff1168 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Wed, 24 Dec 2014 20:59:32 +0900 Subject: [PATCH] tests: add a test for decoding unix domain socket addresses * tests/unix-yy-accept.awk: New file. * tests/unix-yy-connect.awk: New file. * tests/unix-yy.test: New test. * tests/Makefile.am (TESTS): Add it. (EXTRA_DIST): Add unix-yy-accept.awk and unix-yy-connect.awk. Signed-off-by: Masatake YAMATO Signed-off-by: Dmitry V. Levin --- tests/Makefile.am | 3 ++ tests/unix-yy-accept.awk | 65 +++++++++++++++++++++++++++++++++++++++ tests/unix-yy-connect.awk | 41 ++++++++++++++++++++++++ tests/unix-yy.test | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 tests/unix-yy-accept.awk create mode 100644 tests/unix-yy-connect.awk create mode 100755 tests/unix-yy.test diff --git a/tests/Makefile.am b/tests/Makefile.am index 560648b6..f17e8c0f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -37,6 +37,7 @@ TESTS = \ net.test \ net-fd.test \ net-yy.test \ + unix-yy.test \ uid.test \ uid16.test \ uid32.test \ @@ -59,6 +60,8 @@ EXTRA_DIST = init.sh run.sh \ net-yy-connect.awk \ sigaction.awk \ uid.awk \ + unix-yy-accept.awk \ + unix-yy-connect.awk \ $(TESTS) CLEANFILES = $(TESTS:=.tmp) diff --git a/tests/unix-yy-accept.awk b/tests/unix-yy-accept.awk new file mode 100644 index 00000000..ad1d39ec --- /dev/null +++ b/tests/unix-yy-accept.awk @@ -0,0 +1,65 @@ +BEGIN { + lines = 8 + fail = 0 + + inode_listen = "?" + inode_accepted = "?" + inode_peer = "?" + + r_i = "[1-9][0-9]*" + r_bind = "^bind\\(0, {sa_family=AF_LOCAL, sun_path=\"local-stream\"}, " r_i "\\) += 0$" + r_listen = "^/$" + r_getsockname = "^/$" + r_accept = "^/$" + r_close0 = "^/$" + r_close1 = "^/$" +} + +NR == 1 && /^socket\(PF_LOCAL, SOCK_STREAM, 0\) += 0$/ {next} + +NR == 2 { + if (match($0, r_bind, a)) { + inode_listen = a[1] + r_listen = "^listen\\(0, 5\\) += 0$" + r_getsockname = "^getsockname\\(0, {sa_family=AF_LOCAL, sun_path=\"local-stream\"}, \\[" r_i "\\]\\) += 0$" + r_accept = "^accept\\(0, {sa_family=AF_LOCAL, NULL}, \\[" r_i "\\]\\) += 1(" r_i "),\"local-stream\"\\]>" + next + } +} + +NR == 3 {if (match($0, r_listen)) next} + +NR == 4 {if (match($0, r_getsockname)) next} + +NR == 5 { + if (match($0, r_accept, a)) { + inode_accepted = a[1] + inode_peer = a[2] + print inode_accepted + r_close0 = "^close\\(0\\) += 0$" + r_close1 = "^close\\(1\\) += 0$" + next + } +} + +NR == 6 {if (match($0, r_close0)) next} +NR == 7 {if (match($0, r_close1)) next} + +NR == lines && /^\+\+\+ exited with 0 \+\+\+$/ {next} + +{ + print "Line " NR " does not match: " $0 + fail=1 +} + +END { + if (NR != lines) { + print "Expected " lines " lines, found " NR " line(s)." + print "" + exit 1 + } + if (fail) { + print "" + exit 1 + } +} diff --git a/tests/unix-yy-connect.awk b/tests/unix-yy-connect.awk new file mode 100644 index 00000000..1b41315e --- /dev/null +++ b/tests/unix-yy-connect.awk @@ -0,0 +1,41 @@ +BEGIN { + lines = 5 + fail = 0 + + inode = "?" + + r_i = "[1-9][0-9]*" + r_close0 = "^close\\(0\\) += 0$" + r_connect = "^connect\\(1, {sa_family=AF_LOCAL, sun_path=\"local-stream\"}, " r_i "\\) += 0$" + r_close1 = "^/$" +} + +NR == 1 && /^socket\(PF_LOCAL, SOCK_STREAM, 0\) += 1$/ {next} +NR == 2 {if (match($0, r_close0)) next} +NR == 3 { + if (match($0, r_connect, a)) { + inode = a[1] + r_close1 = "^close\\(1" r_i "\\]>\\) += 0$" + next + } +} +NR == 4 {if (match($0, r_close1, a) && a[1] == inode) {next}} + +NR == lines && /^\+\+\+ exited with 0 \+\+\+$/ {next} + +{ + print "Line " NR " does not match: " $0 + fail=1 +} + +END { + if (NR != lines) { + print "Expected " lines " lines, found " NR " line(s)." + print "" + exit 1 + } + if (fail) { + print "" + exit 1 + } +} diff --git a/tests/unix-yy.test b/tests/unix-yy.test new file mode 100755 index 00000000..6e7bd563 --- /dev/null +++ b/tests/unix-yy.test @@ -0,0 +1,60 @@ +#!/bin/sh + +# Check decoding of address information (inode[->peer][,path]) +# associated with unix domain socket descriptors. + +. "${srcdir=.}/init.sh" + +# strace -yy is implemented using /proc/self/fd +[ -d /proc/self/fd/ ] || + framework_skip_ '/proc/self/fd/ is not available' + +check_prog awk +check_prog sed + +rm -f $LOG.* $LOG-* + +./net-accept-connect || + fail_ 'net-accept-connect failed' + +./netlink_inet_diag || { + if [ $? -eq 77 ]; then + framework_skip_ 'NETLINK_INET_DIAG is not available' + else + fail_ 'netlink_inet_diag failed' + fi +} + +args="-tt -ff -yy -o $LOG -eclose,network ./net-accept-connect" +$STRACE $args || + fail_ "strace $args failed" + +"$srcdir"/../strace-log-merge $LOG > $LOG || { + cat $LOG + fail_ 'strace-log-merge failed' +} +rm -f $LOG.* + +child="$(sed -rn '/SIGCHLD/ s/^.*, si_pid=([1-9][0-9]*), .*/\1/p' $LOG)" +[ -n "$child" ] || { + cat $LOG + fail_ 'failed to find pid of child process' +} + +sed -rn "/^$child"' /!d; / socket\(/,$ s/^[0-9]+ +[^ ]+ (.+)/\1/p' $LOG > $LOG-connect && +sed -rn "/^$child"' /d; /SIGCHLD/d; / socket\(/,$ s/^[0-9]+ +[^ ]+ (.+)/\1/p' $LOG > $LOG-accept || { + cat $LOG + fail_ 'failed to separate logs' +} + +awk -f "$srcdir"/unix-yy-connect.awk $LOG-connect || { + cat $LOG-connect + fail_ "strace $args failed to decode socket descriptors properly" +} + +awk -f "$srcdir"/unix-yy-accept.awk $LOG-accept || { + cat $LOG-accept + fail_ "strace $args failed to decode socket descriptors properly" +} + +exit 0 -- 2.40.0