#!/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. """Embedded metadata fields to copyright notices (Debian bug #1022330).""" import unittest from decopy.fstree.metadata import metadata_notices, rdf_fields from decopy.licensing.holders import parse_holders class TestMetadataNotices(unittest.TestCase): """metadata_notices() maps a field mapping to synthesised notice lines.""" @staticmethod def _holders(fields: dict[str, str]) -> list[str]: return [str(h) for h in parse_holders(metadata_notices(fields))] def test_creator_becomes_a_yearless_notice(self): fields = {"dc:creator": "Ramon Miranda"} self.assertEqual(self._holders(fields), ["Ramon Miranda"]) def test_creator_keeps_an_email(self): fields = {"Author": "Luca Ferretti "} self.assertEqual(self._holders(fields), ["Luca Ferretti "]) def test_every_creator_field_is_read(self): for field in ("dc:creator", "Author", "Artist", "Creator"): with self.subTest(field=field): holders = self._holders({field: "Jakub Steiner"}) self.assertEqual(holders, ["Jakub Steiner"]) def test_an_organisation_is_a_holder(self): fields = {"Author": "GNOME Design Team"} self.assertEqual(self._holders(fields), ["GNOME Design Team"]) def test_rights_is_emitted_raw_so_it_self_filters(self): # A real notice passes; a licence string or placeholder does not. self.assertEqual( self._holders({"dc:rights": "Copyright 2007 Niko Kiirala"}), ["2007, Niko Kiirala"], ) licencey = ("see the license tab", "CC BY-SA 4.0", "GPL version 2") for value in licencey: with self.subTest(value=value): self.assertEqual(self._holders({"dc:rights": value}), []) def test_unknown_fields_are_ignored(self): fields = {"dc:date": "13 Jan 2019", "dc:publisher": "Inkscape"} self.assertEqual(metadata_notices(fields), "") def test_authoring_tools_are_rejected(self): tools = ("Inkscape", "Sodipodi", "Matplotlib v3.5.1, https://matplotlib.org/") for tool in tools: with self.subTest(tool=tool): self.assertEqual(self._holders({"Author": tool}), []) def test_template_markers_are_rejected(self): for junk in ("$committer", "%USERNAME%", "{{author}}"): with self.subTest(junk=junk): self.assertEqual(self._holders({"dc:creator": junk}), []) def test_machine_artefacts_are_rejected(self): for junk in ("HASH(0x86d2470)", "x-default", "www.tonytextures.com"): with self.subTest(junk=junk): self.assertEqual(self._holders({"Artist": junk}), []) def test_a_value_that_would_fabricate_a_year_is_rejected(self): # "(doofus-01)" parses as the year 2001 plus a truncated name; no year # is ever synthesised, so one appearing means the value is not a name. for junk in ("(doofus-01)", "doofus-01"): with self.subTest(junk=junk): self.assertEqual(self._holders({"Creator": junk}), []) def test_bracketed_handles_are_kept(self): self.assertEqual(self._holders({"dc:creator": "[maxwxyz]"}), ["maxwxyz"]) def test_no_fields_gives_no_notices(self): self.assertEqual(metadata_notices({}), "") def test_repeated_names_are_emitted_once(self): fields = {"Author": "Jakub Steiner", "Creator": "Jakub Steiner"} self.assertEqual(self._holders(fields), ["Jakub Steiner"]) class TestRdfFields(unittest.TestCase): """rdf_fields() reads the RDF metadata block Inkscape and friends write.""" @staticmethod def _svg(work: str) -> str: return ( '\n' '\n' ' \n' " image/svg+xml\n" f" {work}\n" " \n" ' \n' "\n" ) @staticmethod def _agent(field: str, name: str) -> str: return ( f"{name}" f"" ) def test_creator(self): svg = self._svg(self._agent("creator", "GNOME Design Team")) self.assertEqual(rdf_fields(svg), {"dc:creator": "GNOME Design Team"}) def test_rights(self): svg = self._svg(self._agent("rights", "Copyright 2007 Niko Kiirala")) self.assertEqual(rdf_fields(svg), {"dc:rights": "Copyright 2007 Niko Kiirala"}) def test_both_fields(self): svg = self._svg( self._agent("creator", "Andy Fitzsimon") + self._agent("rights", "Andrew Michael Fitzsimon"), ) self.assertEqual( rdf_fields(svg), {"dc:creator": "Andy Fitzsimon", "dc:rights": "Andrew Michael Fitzsimon"}, ) def test_entities_are_unescaped(self): svg = self._svg( self._agent("creator", "Luca Ferretti <elle.uca@libero.it>"), ) self.assertEqual( rdf_fields(svg), {"dc:creator": "Luca Ferretti "}, ) def test_truncated_entity_is_read_as_written(self): # An upstream value that never closed its angle bracket. svg = self._svg(self._agent("creator", "Luca Ferretti <elle.uca@libero.it")) self.assertEqual( rdf_fields(svg), {"dc:creator": "Luca Ferretti Application Exit") self.assertEqual(rdf_fields(svg), {}) def test_wrapped_attributes(self): # Inkscape splits an element name from its attributes across lines. svg = self._svg( '\n \n' " Jakub Steiner\n" " \n ", ) self.assertEqual(rdf_fields(svg), {"dc:creator": "Jakub Steiner"}) def test_direct_text_content(self): # Dublin Core allows the value without a cc:Agent wrapper. svg = self._svg("Ramon Miranda") self.assertEqual(rdf_fields(svg), {"dc:creator": "Ramon Miranda"}) def test_empty_value_is_dropped(self): svg = self._svg(self._agent("creator", "") + self._agent("rights", " ")) self.assertEqual(rdf_fields(svg), {}) def test_no_metadata(self): self.assertEqual(rdf_fields(''), {}) def test_malformed_xml_does_not_raise(self): broken = "" self.assertEqual(rdf_fields(broken), {}) def test_empty_text(self): self.assertEqual(rdf_fields(""), {}) if __name__ == "__main__": unittest.main()