Skip to content

Commit

Permalink
validate put parameters
Browse files Browse the repository at this point in the history
We should at least check against common usage errors
  • Loading branch information
JonnyJD committed Apr 25, 2013
1 parent fb03281 commit 08d1cde
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion discid/disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
12 changes: 6 additions & 6 deletions test_discid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 08d1cde

Please sign in to comment.