1#!/bin/gawk
2#
3# Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
4# Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. The name of the author may not be used to endorse or promote products
16#    derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29BEGIN {
30  lines = 9
31  fail = 0
32
33  r_i = "[1-9][0-9]*"
34  r_port = "[1-9][0-9][0-9][0-9]+"
35  r_localhost = "127\\.0\\.0\\.1"
36  r_socket = "^socket\\(PF_INET, SOCK_STREAM, IPPROTO_IP\\) += 0<TCP:\\[(" r_i ")\\]>$"
37  r_getsockname = "^getsockname\\(0<TCP:\\[" r_localhost ":(" r_port ")\\]>, \\{sa_family=AF_INET, sin_port=htons\\((" r_port ")\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)\\}, \\[" r_i "\\]\\) += 0$"
38}
39
40NR == 1 {
41  if (match($0, r_socket, a)) {
42    inode = a[1]
43    r_bind = "^bind\\(0<TCP:\\[" inode "\\]>, \\{sa_family=AF_INET, sin_port=htons\\(0\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)\\}, " r_i "\\) += 0$"
44    r_listen = "^listen\\(0<TCP:\\[" inode "\\]>, 5\\) += 0$"
45    next
46  }
47}
48
49NR == 2 {if (r_bind != "" && match($0, r_bind)) next}
50
51NR == 3 {if (r_listen != "" && match($0, r_listen)) next}
52
53NR == 4 {
54  if (match($0, r_getsockname, a) && a[1] == a[2]) {
55    port_l = a[1]
56    r_accept = "^accept\\(0<TCP:\\[" r_localhost ":" port_l "\\]>, \\{sa_family=AF_INET, sin_port=htons\\((" r_port ")\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)\\}, \\[" r_i "\\]\\) += 1<TCP:\\[" r_localhost ":" port_l "->" r_localhost ":(" r_port ")\\]>$"
57    r_close0 = "^close\\(0<TCP:\\[" r_localhost ":" port_l "\\]>) += 0$"
58    next
59  }
60}
61
62NR == 5 {
63  if (r_accept != "" && match($0, r_accept, a) && a[1] == a[2]) {
64    port_r = a[1]
65    r_recv = "^recv\\(1<TCP:\\[" r_localhost ":" port_l "->" r_localhost ":" port_r "\\]>, \"data\", 5, MSG_WAITALL\\) += 4$"
66    r_recvfrom = "^recvfrom\\(1<TCP:\\[" r_localhost ":" port_l "->" r_localhost ":" port_r "\\]>, \"data\", 5, MSG_WAITALL, NULL, NULL\\) += 4$"
67    r_close1 = "^close\\(1<TCP:\\[" r_localhost ":" port_l "->" r_localhost ":" port_r "\\]>) += 0$"
68    next
69  }
70}
71
72NR == 6 {if (r_close0 != "" && match($0, r_close0)) next}
73
74NR == 7 {if (r_recv != "" && (match($0, r_recv) || match($0, r_recvfrom))) next}
75
76NR == 8 {if (r_close1 != "" && match($0, r_close1)) next}
77
78NR == lines && $0 == "+++ exited with 0 +++" {next}
79
80{
81  print "Line " NR " does not match: " $0
82  fail=1
83}
84
85END {
86  if (NR != lines) {
87    print "Expected " lines " lines, found " NR " line(s)."
88    print ""
89    exit 1
90  }
91  if (fail) {
92    print ""
93    exit 1
94  }
95}
96