linkchecker/tests/checker/telnetserver.py
Chris Mayo 87039913b2 Fix remaining flake8 violations in tests/
tests/test_clamav.py:58:89: E501 line too long (90 > 88 characters)
tests/test_containers.py:38:9: F841 local variable 'dummy' is assigned to but never used
tests/test_dummy.py:35:9: F841 local variable 'dummy' is assigned to but never used
tests/test_ftpparse.py:94:89: E501 line too long (96 > 88 characters)
tests/test_url.py:128:89: E501 line too long (130 > 88 characters)
tests/test_strformat.py:62:9: E741 ambiguous variable name 'l'
tests/test_strformat.py:136:9: E731 do not assign a lambda expression, use a def
tests/checker/ftpserver.py:94:9: E722 do not use bare 'except'
tests/checker/httpserver.py:55:39: E231 missing whitespace after ','
tests/checker/httpserver.py:224:9: E722 do not use bare 'except'
tests/checker/telnetserver.py:84:9: E722 do not use bare 'except'
tests/checker/__init__.py:71:89: E501 line too long (119 > 88 characters)
tests/checker/__init__.py:292:13: E741 ambiguous variable name 'l'
tests/checker/test_http_misc.py:30:1: W293 blank line contains whitespace
tests/checker/test_https.py:21:1: F401 'tests.need_network' imported but unused
tests/checker/test_news.py:35:1: E302 expected 2 blank lines, found 1
2020-05-28 20:29:13 +01:00

106 lines
3.3 KiB
Python

# Copyright (C) 2012 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Define http test support classes for LinkChecker tests.
"""
import time
import threading
import telnetlib
import miniboa
from . import LinkCheckTest
TIMEOUT = 5
class TelnetServerTest(LinkCheckTest):
"""Start/stop a Telnet server that can be used for testing."""
def __init__(self, methodName="runTest"):
"""Init test class and store default ftp server port."""
super(TelnetServerTest, self).__init__(methodName=methodName)
self.host = "localhost"
self.port = None
self.stop_event = threading.Event()
self.server_thread = None
def get_url(self, user=None, password=None):
if user is not None:
if password is not None:
netloc = "%s:%s@%s" % (user, password, self.host)
else:
netloc = "%s@%s" % (user, self.host)
else:
netloc = self.host
return "telnet://%s:%d" % (netloc, self.port)
def setUp(self):
"""Start a new Telnet server in a new thread."""
self.port, self.server_thread = start_server(self.host, 0, self.stop_event)
self.assertFalse(self.port is None)
def tearDown(self):
"""Send QUIT request to telnet server."""
self.stop_event.set()
if self.server_thread is not None:
self.server_thread.join(10)
assert not self.server_thread.is_alive()
def start_server(host, port, stop_event):
# Instantiate Telnet server class and listen to host:port
clients = []
def on_connect(client):
clients.append(client)
client.send("Telnet test server\nlogin: ")
server = miniboa.TelnetServer(port=port, address=host, on_connect=on_connect)
port = server.server_socket.getsockname()[1]
t = threading.Thread(None, serve_forever, args=(server, clients, stop_event))
t.start()
# wait for server to start up
tries = 0
while tries < 5:
tries += 1
try:
client = telnetlib.Telnet(timeout=TIMEOUT)
client.open(host, port)
client.write(b"exit\n")
break
except Exception:
time.sleep(0.5)
return port, t
def serve_forever(server, clients, stop_event):
"""Run poll loop for server."""
while True:
if stop_event.is_set():
return
server.poll()
for client in clients:
if client.active and client.cmd_ready:
handle_cmd(client)
def handle_cmd(client):
"""Handle telnet clients."""
msg = client.get_command().lower()
if msg == "exit":
client.active = False
else:
client.send("Password: ")