From 244660ec9e19429d2e8856ad497c4cee220d4bb6 Mon Sep 17 00:00:00 2001 From: jevans Date: Tue, 28 May 2013 20:04:23 -0400 Subject: [PATCH] Can brute-force write a file such that exiv2 can read it. --- glymur/test/__init__.py | 1 + glymur/test/test_xmp.py | 84 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 glymur/test/test_xmp.py diff --git a/glymur/test/__init__.py b/glymur/test/__init__.py index 9677af5..f396e49 100644 --- a/glymur/test/__init__.py +++ b/glymur/test/__init__.py @@ -5,3 +5,4 @@ from .test_printing import TestPrinting as printing from .test_opj_suite import TestSuite as suite from .test_opj_suite_write import TestSuiteWrite as suitew from .test_opj_suite_neg import TestSuiteNegative as suiteneg +from .test_xmp import TestSuite as xmp diff --git a/glymur/test/test_xmp.py b/glymur/test/test_xmp.py new file mode 100644 index 0000000..14b3b6f --- /dev/null +++ b/glymur/test/test_xmp.py @@ -0,0 +1,84 @@ +import os +import struct +import sys +import tempfile +import uuid +import unittest +if sys.hexversion <= 0x03030000: + from mock import patch +else: + from unittest.mock import patch +import warnings +from xml.etree import cElementTree as ET + +import pkg_resources + +from glymur import Jp2k +from glymur.lib import openjp2 as opj2 +import glymur + +lines = [' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' Rubbertree' + + ' ' + + ' ' + + ' ' + + ' \n' + + ''] +packet = '\n'.join(lines) + +class TestSuite(unittest.TestCase): + + @classmethod + def setUpClass(cls): + jp2file = pkg_resources.resource_filename(glymur.__name__, + "data/nemo.jp2") + with tempfile.NamedTemporaryFile(suffix='.jp2', delete=False) as tfile: + cls._xmp_file = tfile.name + with open(jp2file, 'rb') as ifile: + # Everything up until the jp2c box. + buffer = ifile.read(77) + tfile.write(buffer) + + # Write the uuid box with bad xml + # Length = 4 + 4 + 16 + length of packet, id is 'xml '. + L = 4 + 4 + 16 + len(packet.encode()) + buffer = struct.pack('>I4s', int(L), b'uuid') + tfile.write(buffer) + tfile.write(b'\xbe\x7a\xcf\xcb\x97\xa9\x42\xe8') + tfile.write(b'\x9c\x71\x99\x94\x91\xe3\xaf\xac') + tfile.write(packet.encode()) + + # Get the rest of the input file. + buffer = ifile.read() + tfile.write(buffer) + tfile.flush() + + @classmethod + def tearDownClass(cls): + os.unlink(cls._xmp_file) + + def setUp(self): + self.jp2file = pkg_resources.resource_filename(glymur.__name__, + "data/nemo.jp2") + + def tearDown(self): + pass + + def test_basic_xmp(self): + b = b'\xbe\x7a\xcf\xcb\x97\xa9\x42\xe8\x9c\x71\x99\x94\x91\xe3\xaf\xac' + xmp_uuid = uuid.UUID(bytes=b) + jp2 = Jp2k(self._xmp_file) + self.assertEqual(jp2.box[3].uuid, xmp_uuid) + + +if __name__ == "__main__": + unittest.main()