Add test case for XML nodes >32k

This commit is contained in:
Florian Bach 2022-10-09 17:13:54 +02:00
parent 294f600ebd
commit 97a6b51cec
2 changed files with 34 additions and 6 deletions

View file

@ -609,9 +609,7 @@ def hash_node_ctx(node, hash_ctx):
while True: while True:
remaining = textlen - done remaining = textlen - done
if remaining > 0x7fff: if remaining > 0x7fff:
print("Warning: Hashing text node larger than 32k.") #print("Warning: Why are we hashing a 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.")
remaining = 0x7fff remaining = 0x7fff
hash_do_append_tag(hash_ctx, ASN_TEXT) hash_do_append_tag(hash_ctx, ASN_TEXT)

View file

@ -131,7 +131,7 @@ class TestAdobe(unittest.TestCase):
def test_hash_node(self): 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. # 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): 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, # 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. # 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) mock_xml_obj = etree.fromstring(mock_xml_str)
sha_hash = libadobe.hash_node(mock_xml_obj).hexdigest().lower() 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 = """
<adept:notification xmlns:adept="http://ns.adobe.com/adept">
<adept:user>urn:uuid:6e5393e0-ff13-4ae8-8f6c-6654182ac7d5</adept:user>
<adept:device>urn:uuid:51abfbaf-f0e8-474d-b031-626c5224f90f</adept:device>
<adept:testCodeForACSMInput>{}</adept:testCodeForACSMInput>
<adept:nonce>eVr2pi26AAAAAAAA</adept:nonce>
<adept:expiration>2022-08-03T09:16:22Z</adept:expiration>
<body xmlns="http://ns.adobe.com/adept">
<fulfillment>6ccfbc7a-349b-40ad-82d8-d7a4c717ca13-00000271</fulfillment>
<transaction>237493726-1749302749327354-Wed Aug 03 09:16:22 UTC 2022</transaction>
<user>urn:uuid:6e5393e0-ff13-4ae8-8f6c-6654182ac7d5</user>
<fulfilled>true</fulfilled>
<returned>true</returned>
<hmac>CB3Ql1FAJD957t5n749q5ZO8IzU=</hmac>
</body>
</adept:notification>
""".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): def test_sign_node_old(self):