Path: blob/main/tests/sys/kern/sonewconn_overflow.py
39536 views
#!/usr/bin/env python1#-2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2020 Netflix, Inc.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#27#2829import socket30import os31import sys32from subprocess import check_output33from time import sleep3435V4HOST = '127.0.0.1'36V6HOST = '::1'37TCPPORT = 6543238UNIXSOCK = '/tmp/testsock'39TYPE = socket.SOCK_STREAM4041class GenericTest(object):42def __init__(self):43raise NotImplementedError("Subclass must override the __init__ method")44def setup(self, af, addr):45self.sockets = []46self.ls = None47self.ls = socket.socket(af, TYPE)48self.ls.bind(addr)49self.ls.listen(2)50self.af = af51self.addr = addr52def doTest(self, cnt):53rv = 054for i in range(0, cnt):55try:56s = socket.socket(self.af, TYPE)57s.connect(self.addr)58except:59continue60self.sockets.append(s)61rv += 162return rv63def __del__(self):64for s in self.sockets:65s.close()66if self.ls is not None:67self.ls.close()6869class IPv4Test(GenericTest):70def __init__(self):71super(IPv4Test, self).setup(socket.AF_INET, (V4HOST, TCPPORT))7273class IPv6Test(GenericTest):74def __init__(self):75super(IPv6Test, self).setup(socket.AF_INET6, (V6HOST, TCPPORT))7677class UnixTest(GenericTest):78def __init__(self):79super(UnixTest, self).setup(socket.AF_UNIX, UNIXSOCK)80def __del__(self):81super(UnixTest, self).__del__()82os.remove(UNIXSOCK)8384class LogChecker():85def __init__(self):86# Clear the dmesg buffer to prevent rotating causes issues87os.system('/sbin/dmesg -c > /dev/null')88# Figure out how big the dmesg buffer is.89self.dmesgOff = len(check_output("/sbin/dmesg"))9091def checkForMsg(self, expected):92newOff = self.dmesgOff93for i in range(0, 3):94dmesg = check_output("/sbin/dmesg")95newOff = len(dmesg)96if newOff >= self.dmesgOff:97dmesg = dmesg[self.dmesgOff:]98for line in dmesg.splitlines():99try:100if str(line).find(expected) >= 0:101self.dmesgOff = newOff102return True103except:104pass105sleep(0.5)106self.dmesgOff = newOff107return False108109def main():110ip4 = IPv4Test()111ip6 = IPv6Test()112lcl = UnixTest()113lc = LogChecker()114failure = False115116STDLOGMSG = "Listen queue overflow: 4 already in queue awaiting acceptance (1 occurrences)"117118V4LOGMSG = "(%s:%d (proto 6)): %s" % (V4HOST, TCPPORT, STDLOGMSG)119ip4.doTest(5)120if not lc.checkForMsg(V4LOGMSG):121failure = True122sys.stderr.write("IPv4 log message not seen\n")123else:124ip4.doTest(1)125if lc.checkForMsg(V4LOGMSG):126failure = True127sys.stderr.write("Subsequent IPv4 log message not suppressed\n")128129V6LOGMSG = "([%s]:%d (proto 6)): %s" % (V6HOST, TCPPORT, STDLOGMSG)130ip6.doTest(5)131if not lc.checkForMsg(V6LOGMSG):132failure = True133sys.stderr.write("IPv6 log message not seen\n")134else:135ip6.doTest(1)136if lc.checkForMsg(V6LOGMSG):137failure = True138sys.stderr.write("Subsequent IPv6 log message not suppressed\n")139140UNIXLOGMSG = "(local:%s): %s" % (UNIXSOCK, STDLOGMSG)141lcl.doTest(5)142if not lc.checkForMsg(UNIXLOGMSG):143failure = True144sys.stderr.write("Unix socket log message not seen\n")145else:146lcl.doTest(1)147if lc.checkForMsg(UNIXLOGMSG):148failure = True149sys.stderr.write("Subsequent Unix socket log message not suppressed\n")150151if failure:152sys.exit(1)153sys.exit(0)154155if __name__ == '__main__':156main()157158159