FileType box sanity checks. #175

This commit is contained in:
jevans 2014-02-19 20:25:08 -05:00
commit c82d525dd2
2 changed files with 36 additions and 0 deletions

View file

@ -1046,9 +1046,22 @@ class FileTypeBox(Jp2kBox):
return msg
def _validate(self):
"""Validate the box before writing to file."""
if self.brand not in ['jp2 ', 'jpx ']:
msg = "The file type brand must be either 'jp2 ' or 'jpx '."
raise IOError(msg)
valid_cls = ['jp2 ', 'jpx ', 'jpxb']
for item in self.compatibility_list:
if item not in valid_cls:
msg = "The file type compatibility list item '{0}' is not "
msg += "valid: valid entries are {1}"
raise IOError(msg.format(item, valid_cls))
def write(self, fptr):
"""Write a File Type box to file.
"""
self._validate()
length = 16 + 4*len(self.compatibility_list)
fptr.write(struct.pack('>I', length))
fptr.write('ftyp'.encode())

View file

@ -340,6 +340,29 @@ class TestChannelDefinition(unittest.TestCase):
association=association)
class TestFileTypeBox(unittest.TestCase):
"""Test suite for ftyp box issues."""
def setUp(self):
pass
def tearDown(self):
pass
def test_brand_unknown(self):
"""A ftyp box brand must be 'jp2 ' or 'jpx '."""
ftyp = glymur.jp2box.FileTypeBox(brand='jp3')
with self.assertRaises(IOError):
with tempfile.TemporaryFile() as tfile:
ftyp.write(tfile)
def test_cl_entry_unknown(self):
"""A ftyp box cl list can only contain 'jp2 ', 'jpx ', or 'jpxb'."""
ftyp = glymur.jp2box.FileTypeBox(compatibility_list=['jp3'])
with self.assertRaises(IOError):
with tempfile.TemporaryFile() as tfile:
ftyp.write(tfile)
class TestColourSpecificationBox(unittest.TestCase):
"""Test suite for colr box instantiation."""