irctest/irctest/cases.py

431 lines
16 KiB
Python
Raw Normal View History

import ssl
2015-12-20 12:12:54 +00:00
import time
2015-12-19 00:11:57 +00:00
import socket
import tempfile
2015-12-19 00:11:57 +00:00
import unittest
2015-12-20 14:11:56 +00:00
import functools
2015-12-19 00:11:57 +00:00
import supybot.utils
from . import runner
2015-12-21 19:13:16 +00:00
from . import client_mock
2015-12-20 14:11:56 +00:00
from .irc_utils import capabilities
from .irc_utils import message_parser
from .exceptions import ConnectionClosed
from .specifications import Specifications
2015-12-19 00:11:57 +00:00
class _IrcTestCase(unittest.TestCase):
2015-12-20 12:47:30 +00:00
"""Base class for test cases."""
2015-12-19 00:11:57 +00:00
controllerClass = None # Will be set by __main__.py
def description(self):
method_doc = self._testMethodDoc
if not method_doc:
return ''
return '\t'+supybot.utils.str.normalizeWhitespace(
method_doc,
removeNewline=False,
).strip().replace('\n ', '\n\t')
2015-12-20 00:48:56 +00:00
def setUp(self):
super().setUp()
self.controller = self.controllerClass()
self.inbuffer = []
2015-12-20 00:48:56 +00:00
if self.show_io:
print('---- new test ----')
2015-12-19 23:47:06 +00:00
def assertMessageEqual(self, msg, subcommand=None, subparams=None,
2015-12-22 21:33:23 +00:00
target=None, nick=None, fail_msg=None, extra_format=(),
2015-12-25 21:47:11 +00:00
strip_first_param=False, **kwargs):
2015-12-20 12:47:30 +00:00
"""Helper for partially comparing a message.
Takes the message as first arguments, and comparisons to be made
as keyword arguments.
Deals with subcommands (eg. `CAP`) if any of `subcommand`,
`subparams`, and `target` are given."""
2015-12-22 04:06:51 +00:00
fail_msg = fail_msg or '{msg}'
2015-12-19 23:47:06 +00:00
for (key, value) in kwargs.items():
2015-12-25 21:47:11 +00:00
if strip_first_param and key == 'params':
value = value[1:]
2015-12-22 21:33:23 +00:00
self.assertEqual(getattr(msg, key), value, msg, fail_msg,
extra_format=extra_format)
if nick:
self.assertNotEqual(msg.prefix, None, msg, fail_msg)
self.assertEqual(msg.prefix.split('!')[0], nick, msg, fail_msg)
2015-12-19 23:47:06 +00:00
if subcommand is not None or subparams is not None:
self.assertGreater(len(msg.params), 2, fail_msg)
2019-02-17 20:39:35 +00:00
#msg_target = msg.params[0]
2015-12-19 23:47:06 +00:00
msg_subcommand = msg.params[1]
msg_subparams = msg.params[2:]
if subcommand:
self.assertEqual(msg_subcommand, subcommand, msg, fail_msg,
extra_format=extra_format)
2015-12-19 23:47:06 +00:00
if subparams is not None:
self.assertEqual(msg_subparams, subparams, msg, fail_msg,
extra_format=extra_format)
def assertIn(self, item, list_, msg=None, fail_msg=None, extra_format=()):
if fail_msg:
fail_msg = fail_msg.format(*extra_format,
item=item, list=list_, msg=msg)
super().assertIn(item, list_, fail_msg)
2015-12-22 21:33:23 +00:00
def assertNotIn(self, item, list_, msg=None, fail_msg=None, extra_format=()):
if fail_msg:
fail_msg = fail_msg.format(*extra_format,
item=item, list=list_, msg=msg)
super().assertNotIn(item, list_, fail_msg)
def assertEqual(self, got, expects, msg=None, fail_msg=None, extra_format=()):
if fail_msg:
fail_msg = fail_msg.format(*extra_format,
got=got, expects=expects, msg=msg)
super().assertEqual(got, expects, fail_msg)
def assertNotEqual(self, got, expects, msg=None, fail_msg=None, extra_format=()):
if fail_msg:
fail_msg = fail_msg.format(*extra_format,
got=got, expects=expects, msg=msg)
super().assertNotEqual(got, expects, fail_msg)
class BaseClientTestCase(_IrcTestCase):
2015-12-19 22:09:06 +00:00
"""Basic class for client tests. Handles spawning a client and exchanging
messages with it."""
2015-12-19 16:52:38 +00:00
nick = None
user = None
2015-12-19 00:11:57 +00:00
def setUp(self):
2015-12-20 00:48:56 +00:00
super().setUp()
2015-12-20 14:11:56 +00:00
self.conn = None
2015-12-19 00:11:57 +00:00
self._setUpServer()
def tearDown(self):
2015-12-20 14:11:56 +00:00
if self.conn:
self.conn.sendall(b'QUIT :end of test.')
self.controller.kill()
2015-12-20 14:11:56 +00:00
if self.conn:
self.conn_file.close()
self.conn.close()
2015-12-19 00:11:57 +00:00
self.server.close()
def _setUpServer(self):
"""Creates the server and make it listen."""
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('', 0)) # Bind any free port
self.server.listen(1)
def acceptClient(self, tls_cert=None, tls_key=None):
2015-12-19 00:11:57 +00:00
"""Make the server accept a client connection. Blocking."""
(self.conn, addr) = self.server.accept()
if tls_cert is None and tls_key is None:
pass
else:
assert tls_cert and tls_key, \
'tls_cert must be provided if and only if tls_key is.'
with tempfile.NamedTemporaryFile('at') as certfile, \
tempfile.NamedTemporaryFile('at') as keyfile:
certfile.write(tls_cert)
certfile.seek(0)
keyfile.write(tls_key)
keyfile.seek(0)
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=certfile.name, keyfile=keyfile.name)
self.conn = context.wrap_socket(self.conn, server_side=True)
self.conn_file = self.conn.makefile(newline='\r\n',
encoding='utf8')
2015-12-19 00:11:57 +00:00
def getLine(self):
line = self.conn_file.readline()
if self.show_io:
2015-12-20 23:23:32 +00:00
print('{:.3f} C: {}'.format(time.time(), line.strip()))
return line
def getMessages(self, *args):
lines = self.getLines(*args)
return map(message_parser.parse_message, lines)
def getMessage(self, *args, filter_pred=None):
"""Gets a message and returns it. If a filter predicate is given,
fetches messages until the predicate returns a False on a message,
and returns this message."""
while True:
line = self.getLine(*args)
if not line:
raise ConnectionClosed()
msg = message_parser.parse_message(line)
if not filter_pred or filter_pred(msg):
return msg
def sendLine(self, line):
2015-12-19 22:09:06 +00:00
ret = self.conn.sendall(line.encode())
assert ret is None
if not line.endswith('\r\n'):
2015-12-19 22:09:06 +00:00
ret = self.conn.sendall(b'\r\n')
assert ret is None
2015-12-19 16:52:38 +00:00
if self.show_io:
2015-12-20 23:23:32 +00:00
print('{:.3f} S: {}'.format(time.time(), line.strip()))
class ClientNegociationHelper:
"""Helper class for tests handling capabilities negociation."""
def readCapLs(self, auth=None, tls_config=None):
(hostname, port) = self.server.getsockname()
self.controller.run(
hostname=hostname,
port=port,
2015-12-19 16:52:38 +00:00
auth=auth,
tls_config=tls_config,
)
self.acceptClient()
m = self.getMessage()
self.assertEqual(m.command, 'CAP',
'First message is not CAP LS.')
if m.params == ['LS']:
self.protocol_version = 301
elif m.params == ['LS', '302']:
self.protocol_version = 302
elif m.params == ['END']:
self.protocol_version = None
else:
raise AssertionError('Unknown CAP params: {}'
.format(m.params))
def userNickPredicate(self, msg):
"""Predicate to be used with getMessage to handle NICK/USER
transparently."""
if msg.command == 'NICK':
self.assertEqual(len(msg.params), 1, msg)
self.nick = msg.params[0]
return False
elif msg.command == 'USER':
self.assertEqual(len(msg.params), 4, msg)
2015-12-19 16:52:38 +00:00
self.user = msg.params
return False
else:
return True
2015-12-20 14:11:56 +00:00
def negotiateCapabilities(self, caps, cap_ls=True, auth=None):
2015-12-20 12:47:30 +00:00
"""Performes a complete capability negociation process, without
ending it, so the caller can continue the negociation."""
2015-12-19 16:52:38 +00:00
if cap_ls:
self.readCapLs(auth)
if not self.protocol_version:
# No negotiation.
return
2015-12-20 14:11:56 +00:00
self.sendLine('CAP * LS :{}'.format(' '.join(caps)))
capability_names = frozenset(capabilities.cap_list_to_dict(caps))
2015-12-19 20:17:06 +00:00
self.acked_capabilities = set()
while True:
m = self.getMessage(filter_pred=self.userNickPredicate)
2015-12-19 16:52:38 +00:00
if m.command != 'CAP':
return m
self.assertGreater(len(m.params), 0, m)
if m.params[0] == 'REQ':
self.assertEqual(len(m.params), 2, m)
requested = frozenset(m.params[1].split())
2015-12-19 20:17:06 +00:00
if not requested.issubset(capability_names):
2015-12-19 16:52:38 +00:00
self.sendLine('CAP {} NAK :{}'.format(
self.nick or '*',
m.params[1][0:100]))
else:
self.sendLine('CAP {} ACK :{}'.format(
self.nick or '*',
m.params[1]))
2015-12-19 20:17:06 +00:00
self.acked_capabilities.update(requested)
else:
return m
2015-12-19 22:09:06 +00:00
class BaseServerTestCase(_IrcTestCase):
"""Basic class for server tests. Handles spawning a server and exchanging
messages with it."""
password = None
2015-12-25 14:45:06 +00:00
ssl = False
2015-12-22 21:33:23 +00:00
valid_metadata_keys = frozenset()
invalid_metadata_keys = frozenset()
2015-12-19 22:09:06 +00:00
def setUp(self):
2015-12-20 00:48:56 +00:00
super().setUp()
2015-12-25 21:47:11 +00:00
self.server_support = {}
2015-12-19 22:09:06 +00:00
self.find_hostname_and_port()
2015-12-22 21:33:23 +00:00
self.controller.run(self.hostname, self.port, password=self.password,
valid_metadata_keys=self.valid_metadata_keys,
2015-12-25 14:45:06 +00:00
invalid_metadata_keys=self.invalid_metadata_keys,
ssl=self.ssl)
2015-12-19 22:09:06 +00:00
self.clients = {}
def tearDown(self):
self.controller.kill()
for client in list(self.clients):
self.removeClient(client)
def find_hostname_and_port(self):
"""Find available hostname/port to listen on."""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("",0))
(self.hostname, self.port) = s.getsockname()
s.close()
def addClient(self, name=None, show_io=None):
2015-12-19 22:09:06 +00:00
"""Connects a client to the server and adds it to the dict.
If 'name' is not given, uses the lowest unused non-negative integer."""
self.controller.wait_for_port()
2015-12-19 22:09:06 +00:00
if not name:
name = max(map(int, list(self.clients)+[0]))+1
show_io = show_io if show_io is not None else self.show_io
2015-12-21 19:13:16 +00:00
self.clients[name] = client_mock.ClientMock(name=name,
show_io=show_io)
self.clients[name].connect(self.hostname, self.port)
2015-12-20 12:12:54 +00:00
return name
2015-12-19 22:09:06 +00:00
2015-12-19 22:09:06 +00:00
def removeClient(self, name):
2015-12-20 12:47:30 +00:00
"""Disconnects the client, without QUIT."""
2015-12-19 22:09:06 +00:00
assert name in self.clients
self.clients[name].disconnect()
2015-12-19 22:09:06 +00:00
del self.clients[name]
def getMessages(self, client, **kwargs):
return self.clients[client].getMessages(**kwargs)
def getMessage(self, client, **kwargs):
return self.clients[client].getMessage(**kwargs)
def getRegistrationMessage(self, client):
"""Filter notices, do not send pings."""
return self.getMessage(client, synchronize=False,
filter_pred=lambda m:m.command != 'NOTICE')
2015-12-19 22:09:06 +00:00
def sendLine(self, client, line):
return self.clients[client].sendLine(line)
2015-12-20 00:48:56 +00:00
2015-12-20 14:11:56 +00:00
def getCapLs(self, client, as_list=False):
2015-12-20 12:47:30 +00:00
"""Waits for a CAP LS block, parses all CAP LS messages, and return
2015-12-20 14:11:56 +00:00
the dict capabilities, with their values.
If as_list is given, returns the raw list (ie. key/value not split)
in case the order matters (but it shouldn't)."""
caps = []
2015-12-20 00:48:56 +00:00
while True:
m = self.getRegistrationMessage(client)
2015-12-20 00:48:56 +00:00
self.assertMessageEqual(m, command='CAP', subcommand='LS')
if m.params[2] == '*':
2015-12-20 14:11:56 +00:00
caps.extend(m.params[3].split())
2015-12-20 00:48:56 +00:00
else:
2015-12-20 14:11:56 +00:00
caps.extend(m.params[2].split())
if not as_list:
caps = capabilities.cap_list_to_dict(caps)
return caps
def assertDisconnected(self, client):
try:
2018-12-31 00:05:13 +00:00
self.getMessages(client)
self.getMessages(client)
except (socket.error, ConnectionClosed):
del self.clients[client]
return
else:
raise AssertionError('Client not disconnected.')
def skipToWelcome(self, client):
"""Skip to the point where we are registered
<https://tools.ietf.org/html/rfc2812#section-3.1>
"""
2018-12-28 18:42:47 +00:00
result = []
while True:
m = self.getMessage(client, synchronize=False)
2018-12-28 18:42:47 +00:00
result.append(m)
if m.command == '001':
2018-12-28 18:42:47 +00:00
return result
2015-12-29 11:54:09 +00:00
def connectClient(self, nick, name=None, capabilities=None,
2018-12-28 18:42:47 +00:00
skip_if_cap_nak=False, show_io=None):
client = self.addClient(name, show_io=show_io)
2017-11-01 23:33:43 +00:00
if capabilities is not None and 0 < len(capabilities):
self.sendLine(client, 'CAP REQ :{}'.format(' '.join(capabilities)))
m = self.getRegistrationMessage(client)
2015-12-29 11:54:09 +00:00
try:
self.assertMessageEqual(m, command='CAP',
fail_msg='Expected CAP ACK, got: {msg}')
self.assertEqual(m.params[1], 'ACK', m,
fail_msg='Expected CAP ACK, got: {msg}')
except AssertionError:
if skip_if_cap_nak:
raise runner.NotImplementedByController(
', '.join(capabilities))
else:
raise
self.sendLine(client, 'CAP END')
self.sendLine(client, 'NICK {}'.format(nick))
self.sendLine(client, 'USER username * * :Realname')
2018-12-28 18:42:47 +00:00
welcome = self.skipToWelcome(client)
self.sendLine(client, 'PING foo')
# Skip all that happy welcoming stuff
while True:
m = self.getMessage(client)
if m.command == 'PONG':
break
2015-12-25 21:47:11 +00:00
elif m.command == '005':
for param in m.params[1:-1]:
if '=' in param:
(key, value) = param.split('=')
else:
(key, value) = (param, None)
self.server_support[key] = value
2018-12-28 18:42:47 +00:00
welcome.append(m)
return welcome
def joinClient(self, client, channel):
self.sendLine(client, 'JOIN {}'.format(channel))
received = {m.command for m in self.getMessages(client)}
self.assertIn('366', received,
fail_msg='Join to {} failed, {item} is not in the set of '
'received responses: {list}',
extra_format=(channel,))
def joinChannel(self, client, channel):
self.sendLine(client, 'JOIN {}'.format(channel))
# wait until we see them join the channel
joined = False
while not joined:
for msg in self.getMessages(client):
# todo: also respond to cannot join channel numeric
if msg.command.upper() == 'JOIN' and 0 < len(msg.params) and msg.params[0].lower() == channel.lower():
joined = True
break
2015-12-20 14:11:56 +00:00
class OptionalityHelper:
def checkSaslSupport(self):
if self.controller.supported_sasl_mechanisms:
return
raise runner.NotImplementedByController('SASL')
2015-12-20 14:11:56 +00:00
def checkMechanismSupport(self, mechanism):
if mechanism in self.controller.supported_sasl_mechanisms:
return
raise runner.OptionalSaslMechanismNotSupported(mechanism)
2015-12-20 14:11:56 +00:00
def skipUnlessHasMechanism(mech):
def decorator(f):
@functools.wraps(f)
def newf(self):
self.checkMechanismSupport(mech)
return f(self)
return newf
return decorator
def skipUnlessHasSasl(f):
@functools.wraps(f)
def newf(self):
self.checkSaslSupport()
return f(self)
return newf
class SpecificationSelector:
2015-12-23 18:39:34 +00:00
def requiredBySpecification(*specifications, strict=False):
specifications = frozenset(
Specifications.of_name(s) if isinstance(s, str) else s
for s in specifications)
if None in specifications:
raise ValueError('Invalid set of specifications: {}'
.format(specifications))
def decorator(f):
@functools.wraps(f)
def newf(self):
if specifications.isdisjoint(self.testedSpecifications):
raise runner.NotRequiredBySpecifications()
2015-12-23 18:39:34 +00:00
if strict and not self.strictTests:
raise runner.SkipStrictTest()
return f(self)
return newf
return decorator