3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2020 Netflix, Inc.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 from subprocess import check_output
35 from time import sleep
40 UNIXSOCK = '/tmp/testsock'
41 TYPE = socket.SOCK_STREAM
43 class GenericTest(object):
45 raise NotImplementedError("Subclass must override the __init__ method")
46 def setup(self, af, addr):
49 self.ls = socket.socket(af, TYPE)
54 def doTest(self, cnt):
56 for i in range(0, cnt):
58 s = socket.socket(self.af, TYPE)
62 self.sockets.append(s)
66 for s in self.sockets:
68 if self.ls is not None:
71 class IPv4Test(GenericTest):
73 super(IPv4Test, self).setup(socket.AF_INET, (V4HOST, TCPPORT))
75 class IPv6Test(GenericTest):
77 super(IPv6Test, self).setup(socket.AF_INET6, (V6HOST, TCPPORT))
79 class UnixTest(GenericTest):
81 super(UnixTest, self).setup(socket.AF_UNIX, UNIXSOCK)
83 super(UnixTest, self).__del__()
88 # Clear the dmesg buffer to prevent rotating causes issues
89 os.system('/sbin/dmesg -c > /dev/null')
90 # Figure out how big the dmesg buffer is.
91 self.dmesgOff = len(check_output("/sbin/dmesg"))
93 def checkForMsg(self, expected):
94 newOff = self.dmesgOff
96 dmesg = check_output("/sbin/dmesg")
98 if newOff >= self.dmesgOff:
99 dmesg = dmesg[self.dmesgOff:]
100 for line in dmesg.splitlines():
102 if str(line).find(expected) >= 0:
103 self.dmesgOff = newOff
108 self.dmesgOff = newOff
118 STDLOGMSG = "Listen queue overflow: 4 already in queue awaiting acceptance (1 occurrences)"
120 V4LOGMSG = "(%s:%d (proto 6)): %s" % (V4HOST, TCPPORT, STDLOGMSG)
122 if not lc.checkForMsg(V4LOGMSG):
124 sys.stderr.write("IPv4 log message not seen\n")
127 if lc.checkForMsg(V4LOGMSG):
129 sys.stderr.write("Subsequent IPv4 log message not suppressed\n")
131 V6LOGMSG = "([%s]:%d (proto 6)): %s" % (V6HOST, TCPPORT, STDLOGMSG)
133 if not lc.checkForMsg(V6LOGMSG):
135 sys.stderr.write("IPv6 log message not seen\n")
138 if lc.checkForMsg(V6LOGMSG):
140 sys.stderr.write("Subsequent IPv6 log message not suppressed\n")
142 UNIXLOGMSG = "(local:%s): %s" % (UNIXSOCK, STDLOGMSG)
144 if not lc.checkForMsg(UNIXLOGMSG):
146 sys.stderr.write("Unix socket log message not seen\n")
149 if lc.checkForMsg(UNIXLOGMSG):
151 sys.stderr.write("Subsequent Unix socket log message not suppressed\n")
157 if __name__ == '__main__':