From 93bf3ea918d3bf8372df85c216d8a1ab7af3aca6 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 25 Jul 2014 12:24:22 +0200 Subject: [PATCH] mpd: Fix crash on wrong number of command args Fixes #789 --- mopidy/mpd/protocol/__init__.py | 9 ++++++++- tests/mpd/protocol/test_authentication.py | 6 ++++++ tests/mpd/test_commands.py | 6 +++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/mopidy/mpd/protocol/__init__.py b/mopidy/mpd/protocol/__init__.py index 3c501bbb..f0ae814b 100644 --- a/mopidy/mpd/protocol/__init__.py +++ b/mopidy/mpd/protocol/__init__.py @@ -138,7 +138,13 @@ class Commands(object): def validate(*args, **kwargs): if varargs: return func(*args, **kwargs) - callargs = inspect.getcallargs(func, *args, **kwargs) + + try: + callargs = inspect.getcallargs(func, *args, **kwargs) + except TypeError: + raise exceptions.MpdArgError( + 'wrong number of arguments for "%s"' % name) + for key, value in callargs.items(): default = defaults.get(key, object()) if key in validators and value != default: @@ -146,6 +152,7 @@ class Commands(object): callargs[key] = validators[key](value) except ValueError: raise exceptions.MpdArgError('incorrect arguments') + return func(**callargs) validate.auth_required = auth_required diff --git a/tests/mpd/protocol/test_authentication.py b/tests/mpd/protocol/test_authentication.py index 6a39ba81..4937c04f 100644 --- a/tests/mpd/protocol/test_authentication.py +++ b/tests/mpd/protocol/test_authentication.py @@ -19,6 +19,12 @@ class AuthenticationActiveTest(protocol.BaseTestCase): self.assertFalse(self.dispatcher.authenticated) self.assertEqualResponse('ACK [3@0] {password} incorrect password') + def test_authentication_without_password_fails(self): + self.sendRequest('password') + self.assertFalse(self.dispatcher.authenticated) + self.assertEqualResponse( + 'ACK [2@0] {password} wrong number of arguments for "password"') + def test_anything_when_not_authenticated_should_fail(self): self.sendRequest('any request at all') self.assertFalse(self.dispatcher.authenticated) diff --git a/tests/mpd/test_commands.py b/tests/mpd/test_commands.py index bb4effb8..2b4205fe 100644 --- a/tests/mpd/test_commands.py +++ b/tests/mpd/test_commands.py @@ -169,15 +169,15 @@ class TestCommands(unittest.TestCase): def test_call_incorrect_args(self): self.commands.add('foo')(lambda context: context) - with self.assertRaises(TypeError): + with self.assertRaises(exceptions.MpdArgError): self.commands.call(['foo', 'bar']) self.commands.add('bar')(lambda context, required: context) - with self.assertRaises(TypeError): + with self.assertRaises(exceptions.MpdArgError): self.commands.call(['bar', 'bar', 'baz']) self.commands.add('baz')(lambda context, optional=None: context) - with self.assertRaises(TypeError): + with self.assertRaises(exceptions.MpdArgError): self.commands.call(['baz', 'bar', 'baz']) def test_validator_gets_applied_to_required_arg(self):