From 10b6f8d6da834c9293c90b6e95eaa329f69a92c8 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 12 Apr 2022 18:48:03 +0200 Subject: [PATCH] Remove useless 'OptionalityHelper'. --- irctest/cases.py | 58 +++++++++-------------- irctest/client_tests/sasl.py | 20 ++++---- irctest/client_tests/tls.py | 2 +- irctest/server_tests/account_tag.py | 6 +-- irctest/server_tests/away_notify.py | 2 +- irctest/server_tests/cap.py | 2 +- irctest/server_tests/extended_join.py | 4 +- irctest/server_tests/labeled_responses.py | 2 +- irctest/server_tests/message_tags.py | 2 +- irctest/server_tests/multiline.py | 2 +- irctest/server_tests/sasl.py | 16 +++---- irctest/server_tests/utf8.py | 2 +- irctest/server_tests/who.py | 6 +-- irctest/server_tests/whois.py | 10 ++-- 14 files changed, 59 insertions(+), 75 deletions(-) diff --git a/irctest/cases.py b/irctest/cases.py index 0a64c1a..e782e11 100644 --- a/irctest/cases.py +++ b/irctest/cases.py @@ -732,50 +732,38 @@ class BaseServerTestCase( raise ChannelJoinException(msg.command, msg.params) -_TSelf = TypeVar("_TSelf", bound="OptionalityHelper") +_TSelf = TypeVar("_TSelf", bound="_IrcTestCase") _TReturn = TypeVar("_TReturn") -class OptionalityHelper(Generic[TController]): - controller: TController - - def checkSaslSupport(self) -> None: - if self.controller.supported_sasl_mechanisms: - return - raise runner.NotImplementedByController("SASL") - - def checkMechanismSupport(self, mechanism: str) -> None: - if mechanism in self.controller.supported_sasl_mechanisms: - return - raise runner.OptionalSaslMechanismNotSupported(mechanism) - - @staticmethod - def skipUnlessHasMechanism( - mech: str, - ) -> Callable[[Callable[..., _TReturn]], Callable[..., _TReturn]]: - # Just a function returning a function that takes functions and - # returns functions, nothing to see here. - # If Python didn't have such an awful syntax for callables, it would be: - # str -> ((TSelf -> TReturn) -> (TSelf -> TReturn)) - def decorator(f: Callable[..., _TReturn]) -> Callable[..., _TReturn]: - @functools.wraps(f) - def newf(self: _TSelf, *args: Any, **kwargs: Any) -> _TReturn: - self.checkMechanismSupport(mech) - return f(self, *args, **kwargs) - - return newf - - return decorator - - @staticmethod - def skipUnlessHasSasl(f: Callable[..., _TReturn]) -> Callable[..., _TReturn]: +def skipUnlessHasMechanism( + mech: str, +) -> Callable[[Callable[..., _TReturn]], Callable[..., _TReturn]]: + # Just a function returning a function that takes functions and + # returns functions, nothing to see here. + # If Python didn't have such an awful syntax for callables, it would be: + # str -> ((TSelf -> TReturn) -> (TSelf -> TReturn)) + def decorator(f: Callable[..., _TReturn]) -> Callable[..., _TReturn]: @functools.wraps(f) def newf(self: _TSelf, *args: Any, **kwargs: Any) -> _TReturn: - self.checkSaslSupport() + if mech not in self.controller.supported_sasl_mechanisms: + raise runner.OptionalSaslMechanismNotSupported(mech) return f(self, *args, **kwargs) return newf + return decorator + + +def skipUnlessHasSasl(f: Callable[..., _TReturn]) -> Callable[..., _TReturn]: + @functools.wraps(f) + def newf(self: _TSelf, *args: Any, **kwargs: Any) -> _TReturn: + if not self.controller.supported_sasl_mechanisms: + raise runner.NotImplementedByController("SASL") + return f(self, *args, **kwargs) + + return newf + def mark_services(cls: TClass) -> TClass: cls.run_services = True diff --git a/irctest/client_tests/sasl.py b/irctest/client_tests/sasl.py index bf766f9..ca771b7 100644 --- a/irctest/client_tests/sasl.py +++ b/irctest/client_tests/sasl.py @@ -39,8 +39,8 @@ class IdentityHash: return self._data -class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") +class SaslTestCase(cases.BaseClientTestCase): + @cases.skipUnlessHasMechanism("PLAIN") def testPlain(self): """Test PLAIN authentication with correct username/password.""" auth = authentication.Authentication( @@ -60,7 +60,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): m = self.negotiateCapabilities(["sasl"], False) self.assertEqual(m, Message({}, None, "CAP", ["END"])) - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainNotAvailable(self): """`sasl=EXTERNAL` is advertized, whereas the client is configured to use PLAIN. @@ -90,7 +90,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): self.assertMessageMatch(m, command="CAP") @pytest.mark.parametrize("pattern", ["barbaz", "éèà"]) - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainLarge(self, pattern): """Test the client splits large AUTHENTICATE messages whose payload is not a multiple of 400. @@ -119,7 +119,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): m = self.negotiateCapabilities(["sasl"], False) self.assertEqual(m, Message({}, None, "CAP", ["END"])) - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") @pytest.mark.parametrize("pattern", ["quux", "éè"]) def testPlainLargeMultiple(self, pattern): """Test the client splits large AUTHENTICATE messages whose payload @@ -150,7 +150,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): self.assertEqual(m, Message({}, None, "CAP", ["END"])) @pytest.mark.skipif(ecdsa is None, reason="python3-ecdsa is not available") - @cases.OptionalityHelper.skipUnlessHasMechanism("ECDSA-NIST256P-CHALLENGE") + @cases.skipUnlessHasMechanism("ECDSA-NIST256P-CHALLENGE") def testEcdsa(self): """Test ECDSA authentication.""" auth = authentication.Authentication( @@ -184,7 +184,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): m = self.negotiateCapabilities(["sasl"], False) self.assertEqual(m, Message({}, None, "CAP", ["END"])) - @cases.OptionalityHelper.skipUnlessHasMechanism("SCRAM-SHA-256") + @cases.skipUnlessHasMechanism("SCRAM-SHA-256") def testScram(self): """Test SCRAM-SHA-256 authentication.""" auth = authentication.Authentication( @@ -226,7 +226,7 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): self.assertEqual(m.command, "AUTHENTICATE", m) self.assertEqual(m.params, ["+"], m) - @cases.OptionalityHelper.skipUnlessHasMechanism("SCRAM-SHA-256") + @cases.skipUnlessHasMechanism("SCRAM-SHA-256") def testScramBadPassword(self): """Test SCRAM-SHA-256 authentication with a bad password.""" auth = authentication.Authentication( @@ -261,8 +261,8 @@ class SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): authenticator.response(msg) -class Irc302SaslTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") +class Irc302SaslTestCase(cases.BaseClientTestCase): + @cases.skipUnlessHasMechanism("PLAIN") def testPlainNotAvailable(self): """Test the client does not try to authenticate using a mechanism the server does not advertise. diff --git a/irctest/client_tests/tls.py b/irctest/client_tests/tls.py index 285bd36..7172a96 100644 --- a/irctest/client_tests/tls.py +++ b/irctest/client_tests/tls.py @@ -140,7 +140,7 @@ class TlsTestCase(cases.BaseClientTestCase): self.getMessage() -class StsTestCase(cases.BaseClientTestCase, cases.OptionalityHelper): +class StsTestCase(cases.BaseClientTestCase): def setUp(self): super().setUp() self.insecure_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/irctest/server_tests/account_tag.py b/irctest/server_tests/account_tag.py index f5d2e87..6a19755 100644 --- a/irctest/server_tests/account_tag.py +++ b/irctest/server_tests/account_tag.py @@ -6,7 +6,7 @@ from irctest import cases @cases.mark_services -class AccountTagTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class AccountTagTestCase(cases.BaseServerTestCase): def connectRegisteredClient(self, nick): self.addClient() self.sendLine(2, "CAP LS 302") @@ -40,7 +40,7 @@ class AccountTagTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.skipToWelcome(2) @cases.mark_capabilities("account-tag") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPrivmsg(self): self.connectClient("foo", capabilities=["account-tag"], skip_if_cap_nak=True) self.getMessages(1) @@ -54,7 +54,7 @@ class AccountTagTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): ) @cases.mark_capabilities("account-tag") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testInvite(self): self.connectClient("foo", capabilities=["account-tag"], skip_if_cap_nak=True) self.getMessages(1) diff --git a/irctest/server_tests/away_notify.py b/irctest/server_tests/away_notify.py index 283c007..3e9034e 100644 --- a/irctest/server_tests/away_notify.py +++ b/irctest/server_tests/away_notify.py @@ -5,7 +5,7 @@ from irctest import cases -class AwayNotifyTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class AwayNotifyTestCase(cases.BaseServerTestCase): @cases.mark_capabilities("away-notify") def testAwayNotify(self): """Basic away-notify test.""" diff --git a/irctest/server_tests/cap.py b/irctest/server_tests/cap.py index fac6e68..9b74447 100644 --- a/irctest/server_tests/cap.py +++ b/irctest/server_tests/cap.py @@ -8,7 +8,7 @@ from irctest.patma import ANYSTR from irctest.runner import CapabilityNotSupported, ImplementationChoice -class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class CapTestCase(cases.BaseServerTestCase): @cases.mark_specifications("IRCv3") def testNoReq(self): """Test the server handles gracefully clients which do not send diff --git a/irctest/server_tests/extended_join.py b/irctest/server_tests/extended_join.py index 13dc975..2438f87 100644 --- a/irctest/server_tests/extended_join.py +++ b/irctest/server_tests/extended_join.py @@ -6,7 +6,7 @@ from irctest import cases @cases.mark_services -class MetadataTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class MetadataTestCase(cases.BaseServerTestCase): def connectRegisteredClient(self, nick): self.addClient() self.sendLine(2, "CAP LS 302") @@ -50,7 +50,7 @@ class MetadataTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): ) @cases.mark_capabilities("extended-join") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testLoggedIn(self): self.connectClient("foo", capabilities=["extended-join"], skip_if_cap_nak=True) self.joinChannel(1, "#chan") diff --git a/irctest/server_tests/labeled_responses.py b/irctest/server_tests/labeled_responses.py index 03f9dfd..76aa324 100644 --- a/irctest/server_tests/labeled_responses.py +++ b/irctest/server_tests/labeled_responses.py @@ -14,7 +14,7 @@ from irctest.numerics import ERR_UNKNOWNCOMMAND from irctest.patma import ANYDICT, ANYOPTSTR, NotStrRe, RemainingKeys, StrRe -class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class LabeledResponsesTestCase(cases.BaseServerTestCase): @cases.mark_capabilities("echo-message", "batch", "labeled-response") def testLabeledPrivmsgResponsesToMultipleClients(self): self.connectClient( diff --git a/irctest/server_tests/message_tags.py b/irctest/server_tests/message_tags.py index e19a2d9..b5e8071 100644 --- a/irctest/server_tests/message_tags.py +++ b/irctest/server_tests/message_tags.py @@ -10,7 +10,7 @@ from irctest.numerics import ERR_INPUTTOOLONG from irctest.patma import ANYDICT, ANYSTR, StrRe -class MessageTagsTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class MessageTagsTestCase(cases.BaseServerTestCase): @pytest.mark.arbitrary_client_tags @cases.mark_capabilities("message-tags") def testBasic(self): diff --git a/irctest/server_tests/multiline.py b/irctest/server_tests/multiline.py index ac4b7d5..8c397ea 100644 --- a/irctest/server_tests/multiline.py +++ b/irctest/server_tests/multiline.py @@ -12,7 +12,7 @@ CONCAT_TAG = "draft/multiline-concat" base_caps = ["message-tags", "batch", "echo-message", "server-time", "labeled-response"] -class MultilineTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class MultilineTestCase(cases.BaseServerTestCase): @cases.mark_capabilities("draft/multiline") def testBasic(self): self.connectClient( diff --git a/irctest/server_tests/sasl.py b/irctest/server_tests/sasl.py index f289ca5..d680491 100644 --- a/irctest/server_tests/sasl.py +++ b/irctest/server_tests/sasl.py @@ -12,9 +12,9 @@ class RegistrationTestCase(cases.BaseServerTestCase): @cases.mark_services -class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class SaslTestCase(cases.BaseServerTestCase): @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlain(self): """PLAIN authentication with correct username/password.""" self.controller.registerUser(self, "foo", "sesame") @@ -54,7 +54,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): ) @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainNonAscii(self): password = "é" * 100 authstring = base64.b64encode( @@ -82,7 +82,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): ) @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainNoAuthzid(self): """“message = [authzid] UTF8NUL authcid UTF8NUL passwd @@ -170,7 +170,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): ) @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainLarge(self): """Test the client splits large AUTHENTICATE messages whose payload is not a multiple of 400. @@ -232,7 +232,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): # message's length too big for it to be valid. @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") def testPlainLargeEquals400(self): """Test the client splits large AUTHENTICATE messages whose payload is not a multiple of 400. @@ -277,7 +277,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): # message's length too big for it to be valid. @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("SCRAM-SHA-256") + @cases.skipUnlessHasMechanism("SCRAM-SHA-256") def testScramSha256Success(self): self.controller.registerUser(self, "Scramtest", "sesame") @@ -333,7 +333,7 @@ class SaslTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.confirmSuccessfulAuth() @cases.mark_specifications("IRCv3") - @cases.OptionalityHelper.skipUnlessHasMechanism("SCRAM-SHA-256") + @cases.skipUnlessHasMechanism("SCRAM-SHA-256") def testScramSha256Failure(self): self.controller.registerUser(self, "Scramtest", "sesame") diff --git a/irctest/server_tests/utf8.py b/irctest/server_tests/utf8.py index 32ff563..ccd8156 100644 --- a/irctest/server_tests/utf8.py +++ b/irctest/server_tests/utf8.py @@ -9,7 +9,7 @@ from irctest import cases from irctest.patma import ANYSTR -class Utf8TestCase(cases.BaseServerTestCase, cases.OptionalityHelper): +class Utf8TestCase(cases.BaseServerTestCase): @cases.mark_specifications("Ergo") def testUtf8Validation(self): self.connectClient( diff --git a/irctest/server_tests/who.py b/irctest/server_tests/who.py index e320cb1..8510e69 100644 --- a/irctest/server_tests/who.py +++ b/irctest/server_tests/who.py @@ -84,7 +84,7 @@ class BaseWhoTestCase: ) -class WhoTestCase(BaseWhoTestCase, cases.BaseServerTestCase, cases.OptionalityHelper): +class WhoTestCase(BaseWhoTestCase, cases.BaseServerTestCase): @cases.mark_specifications("Modern") def testWhoStar(self): self._init() @@ -422,9 +422,7 @@ class WhoTestCase(BaseWhoTestCase, cases.BaseServerTestCase, cases.OptionalityHe @cases.mark_services -class WhoServicesTestCase( - BaseWhoTestCase, cases.BaseServerTestCase, cases.OptionalityHelper -): +class WhoServicesTestCase(BaseWhoTestCase, cases.BaseServerTestCase): @cases.mark_specifications("IRCv3") @cases.mark_isupport("WHOX") def testWhoxAccount(self): diff --git a/irctest/server_tests/whois.py b/irctest/server_tests/whois.py index a23ee66..ec432a5 100644 --- a/irctest/server_tests/whois.py +++ b/irctest/server_tests/whois.py @@ -164,7 +164,7 @@ class _WhoisTestMixin(cases.BaseServerTestCase): ) -class WhoisTestCase(_WhoisTestMixin, cases.BaseServerTestCase, cases.OptionalityHelper): +class WhoisTestCase(_WhoisTestMixin, cases.BaseServerTestCase): @pytest.mark.parametrize( "server", ["", "My.Little.Server", "coolNick"], @@ -210,11 +210,9 @@ class WhoisTestCase(_WhoisTestMixin, cases.BaseServerTestCase, cases.Optionality @cases.mark_services -class ServicesWhoisTestCase( - _WhoisTestMixin, cases.BaseServerTestCase, cases.OptionalityHelper -): +class ServicesWhoisTestCase(_WhoisTestMixin, cases.BaseServerTestCase): @pytest.mark.parametrize("oper", [False, True], ids=["normal", "oper"]) - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") @cases.mark_specifications("Modern") def testWhoisNumerics(self, oper): """Tests all numerics are in the exhaustive list defined in the Modern spec, @@ -297,7 +295,7 @@ class ServicesWhoisTestCase( "RPL_WHOISCHANNELS should be sent for a non-invisible nick", ) - @cases.OptionalityHelper.skipUnlessHasMechanism("PLAIN") + @cases.skipUnlessHasMechanism("PLAIN") @cases.mark_specifications("ircdocs") def testWhoisAccount(self): """Test numeric 330, RPL_WHOISACCOUNT.