From 08d1cdebf2b500ca2e79bcac1e3c3df8b0c45037 Mon Sep 17 00:00:00 2001 From: Johannes Dewender Date: Fri, 26 Apr 2013 00:28:26 +0200 Subject: [PATCH] validate put parameters We should at least check against common usage errors --- discid/disc.py | 6 +++++- test_discid.py | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/discid/disc.py b/discid/disc.py index 83abe45..5046c35 100644 --- a/discid/disc.py +++ b/discid/disc.py @@ -146,12 +146,16 @@ def read(self, device=None, features=[]): _LIB.discid_put.argtypes = (c_void_p, c_int, c_int, c_void_p) _LIB.discid_put.restype = c_int - # TODO: test if input is valid (int rather than string, ...) def put(self, first, last, disc_sectors, track_offsets): """Creates a TOC based on the input given. The user is supposed to use :func:`discid.put`. """ + # check for common usage errors + if (len(track_offsets) != last - first + 1 + or False in [disc_sectors >= off for off in track_offsets]): + raise DiscError("invalid parameters given") + # only the "read" (= TOC) feature is supported by put self._requested_features = ["read"] diff --git a/test_discid.py b/test_discid.py index de196be..49a1d3c 100755 --- a/test_discid.py +++ b/test_discid.py @@ -72,13 +72,13 @@ def test_device_encoding(self): self.assertRaises(discid.DiscError, discid.read, devicebytes) def test_put_fail(self): - # it will only fail because first > last - first = 15 - last = 1 - sectors = 258725 - offsets = [150, 17510, 33275, 45910] # also wrong + # not enough offsets + self.assertRaises(discid.DiscError, discid.put, 1, 2, 150, [150]) + # too many offsets self.assertRaises(discid.DiscError, - discid.put, first, last, sectors, offsets) + discid.put, 1, 2, 1000, [150, 500, 750]) + # total sectors / offset mismatch + self.assertRaises(discid.DiscError, discid.put, 1, 2, 150, [150, 500]) def test_put_success(self): test_disc = test_discs[0]