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 # Figure out how big the dmesg buffer is.
89 self.dmesgOff = len(check_output("/sbin/dmesg"))
91 def checkForMsg(self, expected):
92 newOff = self.dmesgOff
94 dmesg = check_output("/sbin/dmesg")
96 if newOff >= self.dmesgOff:
97 dmesg = dmesg[self.dmesgOff:]
98 for line in dmesg.splitlines():
100 if str(line).find(expected) >= 0:
101 self.dmesgOff = newOff
106 self.dmesgOff = newOff
116 STDLOGMSG = "Listen queue overflow: 4 already in queue awaiting acceptance (1 occurrences)"
118 V4LOGMSG = "(%s:%d (proto 6)): %s" % (V4HOST, TCPPORT, STDLOGMSG)
120 if not lc.checkForMsg(V4LOGMSG):
122 sys.stderr.write("IPv4 log message not seen\n")
125 if lc.checkForMsg(V4LOGMSG):
127 sys.stderr.write("Subsequent IPv4 log message not suppressed\n")
129 V6LOGMSG = "([%s]:%d (proto 6)): %s" % (V6HOST, TCPPORT, STDLOGMSG)
131 if not lc.checkForMsg(V6LOGMSG):
133 sys.stderr.write("IPv6 log message not seen\n")
136 if lc.checkForMsg(V6LOGMSG):
138 sys.stderr.write("Subsequent IPv6 log message not suppressed\n")
140 UNIXLOGMSG = "(local:%s): %s" % (UNIXSOCK, STDLOGMSG)
142 if not lc.checkForMsg(UNIXLOGMSG):
144 sys.stderr.write("Unix socket log message not seen\n")
147 if lc.checkForMsg(UNIXLOGMSG):
149 sys.stderr.write("Subsequent Unix socket log message not suppressed\n")
155 if __name__ == '__main__':