From c82d525dd2286d95811a078b3237cf58641563ab Mon Sep 17 00:00:00 2001 From: jevans Date: Wed, 19 Feb 2014 20:25:08 -0500 Subject: [PATCH] FileType box sanity checks. #175 --- glymur/jp2box.py | 13 +++++++++++++ glymur/test/test_jp2box.py | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 4ce47ff..0a370f9 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -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()) diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index feffec9..426bf66 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -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."""