#!/usr/bin/env python3 # Copyright: 2026, Maximiliano Curia # # License: ISC # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # . # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. """Image metadata fields reaching the output (Debian bug #1022330).""" import shutil import struct import subprocess import tempfile import unittest import zlib from pathlib import Path import xdg.Mime from decopy.fstree import parsers def _png() -> bytes: """A valid 1x1 PNG, so a test can write metadata onto a real image.""" def chunk(tag: bytes, data: bytes) -> bytes: return ( struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data)) ) return ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(b"\x00\x00\x00\x00")) + chunk(b"IEND", b"") ) def _exif(*fields: str) -> str: """Text shaped like exiftool's own output, one 'Label : value' per line.""" pairs = (f.split("=", 1) for f in fields) return "".join(f"{label:32}: {value}\n" for label, value in pairs) class TestExiftoolCopyrightField(unittest.TestCase): """exiftool's Copyright label fires parse_holders whatever the value holds.""" @staticmethod def _parse(text: str) -> tuple[list[str], list[str]]: holders, licenses = parsers.process_exiftool_output(text) return [str(h) for h in holders], list(licenses) def test_a_licence_in_the_copyright_field_is_not_a_holder(self): text = _exif( "Copyright=CC Attribution-ShareAlike " "http://creativecommons.org/licenses/by-sa/3.0/", ) holders, licenses = self._parse(text) self.assertEqual(holders, []) self.assertIn("CC-BY-SA-3", licenses) def test_every_licence_shaped_value_is_suppressed(self): values = ( "CC BY-SA 4.0", "Public Domain http://creativecommons.org/licenses/publicdomain/", "CC0 Public Domain Dedication " "http://creativecommons.org/publicdomain/zero/1.0/", "CC Attribution http://creativecommons.org/licenses/by/4.0/", ) for value in values: with self.subTest(value=value): holders, licenses = self._parse(_exif(f"Copyright={value}")) self.assertEqual(holders, []) self.assertNotEqual(licenses, []) def test_a_real_copyright_is_kept(self): holders, _ = self._parse(_exif("Copyright=Copyright 2011 Lapo Calamandrei")) self.assertEqual(holders, ["2011, Lapo Calamandrei"]) def test_a_dated_name_is_kept(self): holders, _ = self._parse(_exif("Copyright=2011 Jakub Steiner")) self.assertEqual(holders, ["2011, Jakub Steiner"]) def test_a_plain_name_is_kept(self): holders, _ = self._parse(_exif("Copyright=Fonticons, Inc.")) self.assertEqual(holders, ["Fonticons, Inc"]) def test_suppression_is_per_line(self): # Only the line whose own value is a licence goes; a second Copyright # line naming a holder is untouched. text = _exif( "Copyright=CC Attribution-ShareAlike " "http://creativecommons.org/licenses/by-sa/3.0/", "Copyright=2011 Jakub Steiner", ) holders, licenses = self._parse(text) self.assertEqual(holders, ["2011, Jakub Steiner"]) self.assertIn("CC-BY-SA-3", licenses) def test_a_rights_field_still_self_filters(self): kept, _ = self._parse(_exif("Rights=Copyright 2007 Niko Kiirala")) self.assertEqual(kept, ["2007, Niko Kiirala"]) dropped, _ = self._parse(_exif("Rights=CC BY-SA 4.0")) self.assertEqual(dropped, []) def test_other_fields_still_yield_their_licences(self): text = _exif( "Usage Terms=This work is licensed under a Creative Commons " "Attribution 4.0 International License.", ) _, licenses = self._parse(text) self.assertIn("CC-BY-4", licenses) class TestExiftoolAuthorFields(unittest.TestCase): """Author/Artist/Creator name a holder that parse_holders cannot see.""" @staticmethod def _parse(text: str) -> tuple[list[str], list[str]]: holders, licenses = parsers.process_exiftool_output(text) return [str(h) for h in holders], list(licenses) def test_author_becomes_a_holder(self): holders, _ = self._parse(_exif("Author=Lapo Calamandrei")) self.assertEqual(holders, ["Lapo Calamandrei"]) def test_every_author_field_is_read(self): for label in ("Author", "Artist", "Creator"): with self.subTest(label=label): holders, _ = self._parse(_exif(f"{label}=Jakub Steiner")) self.assertEqual(holders, ["Jakub Steiner"]) def test_a_real_notice_is_not_overridden(self): text = _exif("Copyright=2011 Sam Hewitt", "Author=Lapo Calamandrei") holders, _ = self._parse(text) self.assertEqual(holders, ["2011, Sam Hewitt"]) def test_a_licence_copyright_no_longer_blocks_the_author(self): # The 97 corpus files carrying both: the suppressed Copyright tag must # leave the gap open for the author to fill. text = _exif( "Copyright=CC Attribution-NonCommercial-ShareAlike " "http://creativecommons.org/licenses/by-nc-sa/3.0/", "Author=Lapo Calamandrei", ) holders, licenses = self._parse(text) self.assertEqual(holders, ["Lapo Calamandrei"]) self.assertIn("CC-BY-NC-SA-3", licenses) def test_an_email_is_kept(self): holders, _ = self._parse(_exif("Author=Luca Ferretti ")) self.assertEqual(holders, ["Luca Ferretti "]) def test_junk_values_are_rejected(self): for value in ( "(doofus-01)", "HASH(0x86d2470)", "x-default", "www.tonytextures.com", "Inkscape", "$committer", ): with self.subTest(value=value): holders, _ = self._parse(_exif(f"Author={value}")) self.assertEqual(holders, []) def test_the_software_field_is_not_an_author(self): holders, _ = self._parse(_exif("Software=www.inkscape.org")) self.assertEqual(holders, []) def test_a_value_holding_a_colon_survives(self): # Labels never contain a colon, values often do. holders, _ = self._parse(_exif("Author=Ell", "Web Statement=http://x.invalid/")) self.assertEqual(holders, ["Ell"]) class TestExiftoolParserEndToEnd(unittest.TestCase): """The suppression reaches a real image through the exiftool parser.""" @unittest.skipUnless(shutil.which("exiftool"), "exiftool not installed") def test_png_with_a_licence_in_its_copyright_tag(self): exiftool = shutil.which("exiftool") assert exiftool is not None with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "icon.png" path.write_bytes(_png()) subprocess.run( # noqa: S603 [ exiftool, "-overwrite_original", "-Copyright=CC Attribution-ShareAlike " "http://creativecommons.org/licenses/by-sa/3.0/", str(path), ], check=True, capture_output=True, ) parser = parsers.lookup_mimetypes(xdg.Mime.get_type(str(path))) holders, licenses = parser(str(path)) self.assertEqual([str(h) for h in holders], []) self.assertIn("CC-BY-SA-3", list(licenses)) if __name__ == "__main__": unittest.main()