From 3e490d0c28c962358f4b62a1ec43bec58ee9f9f6 Mon Sep 17 00:00:00 2001 From: John Evans Date: Mon, 10 Mar 2014 20:44:24 -0400 Subject: [PATCH 1/2] Rewrote the test for a file truncated by 5000 bytes. #180 --- glymur/test/test_conformance.py | 38 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/glymur/test/test_conformance.py b/glymur/test/test_conformance.py index 765f2f6..48db78a 100644 --- a/glymur/test/test_conformance.py +++ b/glymur/test/test_conformance.py @@ -62,6 +62,26 @@ class TestSuiteConformance(unittest.TestCase): with self.assertRaises(OSError): j2k.read(rlevel=-1) + def test_truncated_5000(self): + """File is missing last 5000 bytes.""" + with open(self.j2kfile, 'rb') as ifile: + data = ifile.read() + with tempfile.NamedTemporaryFile(suffix='.j2k') as ofile: + ofile.write(data[:-5000]) + ofile.flush() + + j2k = Jp2k(ofile.name) + with self.assertWarns(UserWarning): + codestream = j2k.get_codestream(header_only=False) + + # The last segment is truncated, so there should not be an EOC + # marker. + self.assertNotEqual(codestream.segment[-1].marker_id, 'EOC') + + # The codestream is not as long as claimed. + with self.assertRaises(OSError): + j2k.read(rlevel=-1) + @unittest.skipIf(FORMAT_CORPUS_DATA_ROOT is None, "FORMAT_CORPUS_DATA_ROOT environment variable not set") @@ -70,24 +90,6 @@ class TestSuiteConformance(unittest.TestCase): class TestSuiteFormatCorpus(unittest.TestCase): """Test suite for files in format corpus repository.""" - @unittest.skipIf(re.match(r"""1\.[01234]""", - glymur.version.openjpeg_version) is not None, - "Needs 1.4+ to catch this.") - def test_balloon_trunc2(self): - """Shortened by 5000 bytes.""" - jfile = os.path.join(FORMAT_CORPUS_DATA_ROOT, - 'jp2k-test/byteCorruption/balloon_trunc2.jp2') - j2k = Jp2k(jfile) - with self.assertWarns(UserWarning): - codestream = j2k.get_codestream(header_only=False) - - # The last segment is truncated, so there should not be an EOC marker. - self.assertNotEqual(codestream.segment[-1].marker_id, 'EOC') - - # The codestream is not as long as claimed. - with self.assertRaises(OSError): - j2k.read(rlevel=-1) - def test_balloon_trunc3(self): """Most of last tile is missing.""" jfile = os.path.join(FORMAT_CORPUS_DATA_ROOT, From 1cefd80a76b3e47e0ac688814eff3dce38161bda Mon Sep 17 00:00:00 2001 From: jevans Date: Tue, 11 Mar 2014 21:02:15 -0400 Subject: [PATCH 2/2] Just remove the conformance tests, they aren't really needed. #180 --- glymur/test/test_conformance.py | 150 -------------------------------- glymur/test/test_opj_suite.py | 13 ++- 2 files changed, 9 insertions(+), 154 deletions(-) delete mode 100644 glymur/test/test_conformance.py diff --git a/glymur/test/test_conformance.py b/glymur/test/test_conformance.py deleted file mode 100644 index 48db78a..0000000 --- a/glymur/test/test_conformance.py +++ /dev/null @@ -1,150 +0,0 @@ -""" -These tests deal with JPX/JP2/J2K images in the format-corpus repository. -""" -# R0904: Not too many methods in unittest. -# pylint: disable=R0904 - -# E1101: assertWarns introduced in python 3.2 -# pylint: disable=E1101 - -import os -from os.path import join -import re -import sys -import tempfile -import unittest - -import glymur -from glymur import Jp2k - -try: - FORMAT_CORPUS_DATA_ROOT = os.environ['FORMAT_CORPUS_DATA_ROOT'] -except KeyError: - FORMAT_CORPUS_DATA_ROOT = None - -try: - OPJ_DATA_ROOT = os.environ['OPJ_DATA_ROOT'] -except KeyError: - OPJ_DATA_ROOT = None - - -@unittest.skipIf(sys.hexversion < 0x03020000, - "Requires features introduced in 3.2 (assertWarns)") -class TestSuiteConformance(unittest.TestCase): - """Test suite for conformance.""" - - def setUp(self): - self.j2kfile = glymur.data.goodstuff() - - def tearDown(self): - pass - - @unittest.skipIf(re.match(r"""1\.[0123]""", - glymur.version.openjpeg_version) is not None, - "Needs 1.3+ to catch this.") - def test_truncated_eoc(self): - """Has one byte shaved off of EOC marker.""" - with open(self.j2kfile, 'rb') as ifile: - data = ifile.read() - with tempfile.NamedTemporaryFile(suffix='.j2k') as ofile: - ofile.write(data[:-1]) - ofile.flush() - - j2k = Jp2k(ofile.name) - with self.assertWarns(UserWarning): - codestream = j2k.get_codestream(header_only=False) - - # The last segment is truncated, so there should not be an EOC - # marker. - self.assertNotEqual(codestream.segment[-1].marker_id, 'EOC') - - # The codestream is not as long as claimed. - with self.assertRaises(OSError): - j2k.read(rlevel=-1) - - def test_truncated_5000(self): - """File is missing last 5000 bytes.""" - with open(self.j2kfile, 'rb') as ifile: - data = ifile.read() - with tempfile.NamedTemporaryFile(suffix='.j2k') as ofile: - ofile.write(data[:-5000]) - ofile.flush() - - j2k = Jp2k(ofile.name) - with self.assertWarns(UserWarning): - codestream = j2k.get_codestream(header_only=False) - - # The last segment is truncated, so there should not be an EOC - # marker. - self.assertNotEqual(codestream.segment[-1].marker_id, 'EOC') - - # The codestream is not as long as claimed. - with self.assertRaises(OSError): - j2k.read(rlevel=-1) - - -@unittest.skipIf(FORMAT_CORPUS_DATA_ROOT is None, - "FORMAT_CORPUS_DATA_ROOT environment variable not set") -@unittest.skipIf(sys.hexversion < 0x03020000, - "Requires features introduced in 3.2 (assertWarns)") -class TestSuiteFormatCorpus(unittest.TestCase): - """Test suite for files in format corpus repository.""" - - def test_balloon_trunc3(self): - """Most of last tile is missing.""" - jfile = os.path.join(FORMAT_CORPUS_DATA_ROOT, - 'jp2k-test/byteCorruption/balloon_trunc3.jp2') - j2k = Jp2k(jfile) - with self.assertWarns(UserWarning): - codestream = j2k.get_codestream(header_only=False) - - # The last segment is truncated, so there should not be an EOC marker. - self.assertNotEqual(codestream.segment[-1].marker_id, 'EOC') - - # Should error out, it does not. - #with self.assertRaises(OSError): - # j2k.read(rlevel=-1) - - def test_jp2_brand_any_icc_profile(self): - """If 'jp2 ', then the method cannot be any icc profile.""" - jfile = os.path.join(FORMAT_CORPUS_DATA_ROOT, - 'jp2k-test', 'icc', - 'balloon_eciRGBv2_ps_adobeplugin.jpf') - with self.assertWarns(UserWarning): - Jp2k(jfile) - - def test_jp2_brand_iccpr_mult_colr(self): - """Has colr box, one that conforms, one that does not.""" - - # Wrong 'brand' field; contains two versions of ICC profile: one - # embedded using "Any ICC" method; other embedded using "Restricted - # ICC" method, with description ("Modified eciRGB v2") and profileClass - # ("Input Device") changed relative to original profile. - jfile = join(FORMAT_CORPUS_DATA_ROOT, 'jp2k-test', 'icc', - 'balloon_eciRGBv2_ps_adobeplugin_jp2compatible.jpf') - with self.assertWarns(UserWarning): - Jp2k(jfile) - - -@unittest.skipIf(OPJ_DATA_ROOT is None, - "OPJ_DATA_ROOT environment variable not set") -@unittest.skipIf(sys.hexversion < 0x03020000, - "Requires features introduced in 3.2 (assertWarns)") -class TestSuiteOpj(unittest.TestCase): - """Test suite for files in openjpeg repository.""" - - def setUp(self): - pass - - def tearDown(self): - pass - - def test_jp2_brand_any_icc_profile(self): - """If 'jp2 ', then the method cannot be any icc profile.""" - filename = os.path.join(OPJ_DATA_ROOT, - 'input/nonregression/text_GBR.jp2') - with self.assertWarns(UserWarning): - Jp2k(filename) - -if __name__ == "__main__": - unittest.main() diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index be85de9..df4cde2 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -6307,11 +6307,16 @@ class TestSuiteDump(unittest.TestCase): [8, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10, 9, 9, 10]) def test_NR_text_GBR_dump(self): + # brand is 'jp2 ', but has any icc profile. + # Verify the warning on python3, but ignore it otherwise. jfile = opj_data_file('input/nonregression/text_GBR.jp2') - with warnings.catch_warnings(): - # brand is 'jp2 ', but has any icc profile. - warnings.simplefilter("ignore") - jp2 = Jp2k(jfile) + if sys.hexversion > 0x03030000: + with self.assertWarns(UserWarning): + jp2 = Jp2k(jfile) + else: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + jp2 = Jp2k(jfile) ids = [box.box_id for box in jp2.box] lst = ['jP ', 'ftyp', 'rreq', 'jp2h',