From bdefa32d3a7dad19a1565c484b9a586f6a22d876 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 22 Jun 2020 15:48:56 -0400 Subject: [PATCH] add a test for utf8 enforcement --- irctest/client_mock.py | 11 ++++++++--- irctest/controllers/oragono.py | 1 + irctest/server_tests/test_utf8.py | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 irctest/server_tests/test_utf8.py diff --git a/irctest/client_mock.py b/irctest/client_mock.py index f99cc3c..ca99779 100644 --- a/irctest/client_mock.py +++ b/irctest/client_mock.py @@ -89,9 +89,14 @@ class ClientMock: if not filter_pred or filter_pred(message): return message def sendLine(self, line): - if not line.endswith('\r\n'): - line += '\r\n' - encoded_line = line.encode() + if isinstance(line, str): + encoded_line = line.encode() + elif isinstance(line, bytes): + encoded_line = line + else: + raise ValueError(line) + if not encoded_line.endswith(b'\r\n'): + encoded_line += b'\r\n' try: ret = self.conn.sendall(encoded_line) except BrokenPipeError: diff --git a/irctest/controllers/oragono.py b/irctest/controllers/oragono.py index 1827218..69757df 100644 --- a/irctest/controllers/oragono.py +++ b/irctest/controllers/oragono.py @@ -35,6 +35,7 @@ BASE_CONFIG = { "ban-message": "Try again later", "exempted": ["localhost"], }, + 'enforce-utf8': True, }, 'accounts': { diff --git a/irctest/server_tests/test_utf8.py b/irctest/server_tests/test_utf8.py new file mode 100644 index 0000000..b5afd5b --- /dev/null +++ b/irctest/server_tests/test_utf8.py @@ -0,0 +1,22 @@ +from irctest import cases +from irctest.numerics import ERR_UNKNOWNERROR + +class Utf8TestCase(cases.BaseServerTestCase, cases.OptionalityHelper): + @cases.SpecificationSelector.requiredBySpecification('Oragono') + def testUtf8Validation(self): + self.connectClient('bar', capabilities=['batch', 'echo-message', 'labeled-response', 'message-tags']) + self.joinChannel(1, '#qux') + self.sendLine(1, 'PRIVMSG #qux hi') + ms = self.getMessages(1) + self.assertMessageEqual([m for m in ms if m.command == 'PRIVMSG'][0], params=['#qux', 'hi']) + + self.sendLine(1, b'PRIVMSG #qux hi\xaa') + ms = self.getMessages(1) + self.assertEqual(len(ms), 1) + self.assertEqual(ms[0].command, ERR_UNKNOWNERROR) + + self.sendLine(1, b'@label=xyz PRIVMSG #qux hi\xaa') + ms = self.getMessages(1) + self.assertEqual(len(ms), 1) + self.assertEqual(ms[0].command, ERR_UNKNOWNERROR) + self.assertEqual(ms[0].tags.get('label'), 'xyz')