From 97a6b51cecd276c061cd5f6098cbb97d499d5172 Mon Sep 17 00:00:00 2001 From: Florian Bach Date: Sun, 9 Oct 2022 17:13:54 +0200 Subject: [PATCH] Add test case for XML nodes >32k --- calibre-plugin/libadobe.py | 4 +--- tests/main.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/calibre-plugin/libadobe.py b/calibre-plugin/libadobe.py index 92400cf..6829bf5 100644 --- a/calibre-plugin/libadobe.py +++ b/calibre-plugin/libadobe.py @@ -609,9 +609,7 @@ def hash_node_ctx(node, hash_ctx): while True: remaining = textlen - done if remaining > 0x7fff: - print("Warning: Hashing text node larger than 32k.") - print("This usually doesn't happen, and I'm not sure if this is implemented correctly.") - print("If you run into issues, please open a bug report.") + #print("Warning: Why are we hashing a node larger than 32k?") remaining = 0x7fff hash_do_append_tag(hash_ctx, ASN_TEXT) diff --git a/tests/main.py b/tests/main.py index e98592b..3ca8a6d 100755 --- a/tests/main.py +++ b/tests/main.py @@ -131,7 +131,7 @@ class TestAdobe(unittest.TestCase): def test_hash_node(self): - '''Check if the XML hash (needed for the signature) is correct''' + '''Check if XML hashing (needed for the signature) works''' # This XML is an anonymized (all IDs replaced with random UUIDs) ACSM file. @@ -173,7 +173,7 @@ class TestAdobe(unittest.TestCase): def test_hash_node_returnbugfix(self): - '''Check if the XML hash is correct when returning a book ...''' + '''Check if XML hashing works for book returns ...''' # I don't think there's ever a case where the hashing algorithm is different, # but I needed this test during debugging and thought, hey, why not leave it in. @@ -198,9 +198,39 @@ class TestAdobe(unittest.TestCase): mock_xml_obj = etree.fromstring(mock_xml_str) sha_hash = libadobe.hash_node(mock_xml_obj).hexdigest().lower() - self.assertEqual(sha_hash, "8b0a24ba37c4333d93650c6ce52f8ee779f21533", "Invalid SHA hash for node signing") + self.assertEqual(sha_hash, "8b0a24ba37c4333d93650c6ce52f8ee779f21533", "Invalid SHA hash for node signing (return)") + def test_hash_node_ultralong(self): + '''Check if XML hashing works with long values''' + + # If an XML text element is longer than 32k bytes, the hashing works differently. + # Make sure that that works, even though it's not going to show up in practice anywhere ... + + mock_xml_str = """ + + urn:uuid:6e5393e0-ff13-4ae8-8f6c-6654182ac7d5 + urn:uuid:51abfbaf-f0e8-474d-b031-626c5224f90f + {} + eVr2pi26AAAAAAAA + 2022-08-03T09:16:22Z + + 6ccfbc7a-349b-40ad-82d8-d7a4c717ca13-00000271 + 237493726-1749302749327354-Wed Aug 03 09:16:22 UTC 2022 + urn:uuid:6e5393e0-ff13-4ae8-8f6c-6654182ac7d5 + true + true + CB3Ql1FAJD957t5n749q5ZO8IzU= + + + """.format("A"*70000) + + + mock_xml_obj = etree.fromstring(mock_xml_str) + sha_hash = libadobe.hash_node(mock_xml_obj).hexdigest().lower() + + self.assertEqual(sha_hash, "7f62c1c1db2e1c965fd8403a4e768735a5848689", "Invalid SHA hash for node signing (ultralong)") + def test_sign_node_old(self):