| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""SVG metadata reaching the output (Debian bug #1022330).""" |
| 18 |
|
| 19 |
import tempfile |
| 20 |
import unittest |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
import xdg.Mime |
| 24 |
|
| 25 |
from decopy.fstree import parsers |
| 26 |
|
| 27 |
|
| 28 |
def _svg(work: str = "", extra: str = "") -> str: |
| 29 |
return ( |
| 30 |
'<?xml version="1.0"?>\n' |
| 31 |
f"{extra}" |
| 32 |
'<svg xmlns:dc="http://purl.org/dc/elements/1.1/"\n' |
| 33 |
' xmlns:cc="http://creativecommons.org/ns#"\n' |
| 34 |
' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n' |
| 35 |
' xmlns="http://www.w3.org/2000/svg" width="8" height="8">\n' |
| 36 |
' <metadata><rdf:RDF><cc:Work rdf:about="">\n' |
| 37 |
" <dc:format>image/svg+xml</dc:format>\n" |
| 38 |
f" {work}\n" |
| 39 |
" </cc:Work></rdf:RDF></metadata>\n" |
| 40 |
' <rect width="8" height="8"/>\n' |
| 41 |
"</svg>\n" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def _agent(field: str, name: str) -> str: |
| 46 |
return ( |
| 47 |
f"<dc:{field}><cc:Agent><dc:title>{name}</dc:title>" |
| 48 |
f"</cc:Agent></dc:{field}>" |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
class TestSvgMetadataParser(unittest.TestCase): |
| 53 |
"""image/svg+xml gets its own parser, filling holders it would otherwise lose.""" |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
def _parse(text: str) -> tuple[list[str], list[str]]: |
| 57 |
with tempfile.NamedTemporaryFile("w", suffix=".svg", delete=False) as handle: |
| 58 |
handle.write(text) |
| 59 |
path = handle.name |
| 60 |
try: |
| 61 |
parser = parsers.lookup_mimetypes(xdg.Mime.get_type(path)) |
| 62 |
holders, licenses = parser(path) |
| 63 |
finally: |
| 64 |
Path(path).unlink(missing_ok=True) |
| 65 |
return [str(h) for h in holders], list(licenses) |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
def _parse_bytes(text: str) -> tuple[list[str], list[str]]: |
| 69 |
holders, licenses = parsers.svg_content_parser("icon.svg", text.encode()) |
| 70 |
return [str(h) for h in holders], list(licenses) |
| 71 |
|
| 72 |
def test_creator_becomes_a_holder(self): |
| 73 |
holders, _ = self._parse(_svg(_agent("creator", "GNOME Design Team"))) |
| 74 |
self.assertEqual(holders, ["GNOME Design Team"]) |
| 75 |
|
| 76 |
def test_creator_is_read_from_archive_bytes_too(self): |
| 77 |
holders, _ = self._parse_bytes(_svg(_agent("creator", "Jakub Steiner"))) |
| 78 |
self.assertEqual(holders, ["Jakub Steiner"]) |
| 79 |
|
| 80 |
def test_a_real_notice_is_not_overridden(self): |
| 81 |
# Gap-fill only: metadata never displaces a copyright notice. |
| 82 |
text = _svg( |
| 83 |
_agent("creator", "GNOME Design Team"), |
| 84 |
extra="<!-- Copyright 2011 Jakub Steiner -->\n", |
| 85 |
) |
| 86 |
holders, _ = self._parse(text) |
| 87 |
self.assertEqual(holders, ["2011, Jakub Steiner"]) |
| 88 |
|
| 89 |
def test_licence_valued_rights_yields_no_holder(self): |
| 90 |
holders, _ = self._parse(_svg(_agent("rights", "CC BY-SA 4.0"))) |
| 91 |
self.assertEqual(holders, []) |
| 92 |
|
| 93 |
def test_rights_holding_a_real_notice_is_used(self): |
| 94 |
text = _svg(_agent("rights", "Copyright 2007 Niko Kiirala")) |
| 95 |
holders, _ = self._parse(text) |
| 96 |
self.assertEqual(holders, ["2007, Niko Kiirala"]) |
| 97 |
|
| 98 |
def test_licences_are_unchanged(self): |
| 99 |
work = ( |
| 100 |
_agent("creator", "Sam Hewitt") |
| 101 |
+ '<cc:license rdf:resource="' |
| 102 |
+ "http://creativecommons.org/licenses/by-sa/3.0/" |
| 103 |
+ '" />' |
| 104 |
) |
| 105 |
holders, licenses = self._parse(_svg(work)) |
| 106 |
self.assertEqual(holders, ["Sam Hewitt"]) |
| 107 |
self.assertIn("CC-BY-SA-3", licenses) |
| 108 |
|
| 109 |
def test_namespace_declaration_alone_yields_nothing(self): |
| 110 |
# The reporter's case: xmlns:cc is not a licence and not a holder. |
| 111 |
holders, licenses = self._parse(_svg("<dc:title/>")) |
| 112 |
self.assertEqual(holders, []) |
| 113 |
self.assertEqual(licenses, []) |
| 114 |
|
| 115 |
def test_malformed_metadata_does_not_raise(self): |
| 116 |
text = "<svg><metadata><rdf:RDF><cc:Work><dc:creator><cc:Agent>\n" |
| 117 |
holders, licenses = self._parse(text) |
| 118 |
self.assertEqual(holders, []) |
| 119 |
self.assertEqual(licenses, []) |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == "__main__": |
| 123 |
unittest.main() |