mpd: Fix crash on wrong number of command args

Fixes #789
This commit is contained in:
Stein Magnus Jodal 2014-07-25 12:24:22 +02:00
parent a650cc08c0
commit 93bf3ea918
3 changed files with 17 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -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):