Merge pull request #1338 from exarkun/4066.hashtree-test-vectors

Add HashTree-related test vectors
This commit is contained in:
Jean-Paul Calderone 2023-10-09 11:16:10 -04:00 committed by GitHub
commit 47c9a95d42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 15 deletions

0
newsfragments/4066.minor Normal file
View File

View File

@ -1,32 +1,22 @@
"""
Tests for allmydata.hashtree.
Ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import annotations
from future.utils import PY2
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
from twisted.trial import unittest
from .common import SyncTestCase
from base64 import b32encode
from allmydata.util.hashutil import tagged_hash
from allmydata import hashtree
def make_tree(numleaves):
leaves = [b"%d" % i for i in range(numleaves)]
leaf_hashes = [tagged_hash(b"tag", leaf) for leaf in leaves]
ht = hashtree.HashTree(leaf_hashes)
return ht
class Complete(unittest.TestCase):
class Complete(SyncTestCase):
def test_create(self):
# try out various sizes, since we pad to a power of two
ht = make_tree(6)
@ -40,6 +30,18 @@ class Complete(unittest.TestCase):
self.failUnlessRaises(IndexError, ht.parent, 0)
self.failUnlessRaises(IndexError, ht.needed_for, -1)
def test_well_known_tree(self):
self.assertEqual(
[b32encode(s).strip(b"=").lower() for s in make_tree(3)],
[b'vxuqudnucceja4pqkdqy5txapagxubm5moupzqywkbg2jrjkaola',
b'weycjri4jlcaunca2jyx2kr7sbtb7qdriog3f26g5jpc5awfeazq',
b'5ovy3g2wwjnxoqtja4licckxkbqjef4xsjtclk6gxnsl66kvow6a',
b'esd34nbzri75l3j2vwetpk3dvlvsxstkbaktomonrulpks3df3sq',
b'jkxbwa2tppyfax35o72tbjecxvaa4xphma6zbyfbkkku3ed2657a',
b'wfisavaqgab2raihe7dld2qjps4rtxyiubgfs5enziokey2msjwa',
b't3kza5vwx3tlowdemmgdyigp62ju57qduyfh7uulnfkc7mj2ncrq'],
)
def test_needed_hashes(self):
ht = make_tree(8)
self.failUnlessEqual(ht.needed_hashes(0), set([8, 4, 2]))
@ -65,7 +67,7 @@ class Complete(unittest.TestCase):
self.failUnless("\n 8:" in d)
self.failUnless("\n 4:" in d)
class Incomplete(unittest.TestCase):
class Incomplete(SyncTestCase):
def test_create(self):
ht = hashtree.IncompleteHashTree(6)

View File

@ -44,6 +44,34 @@ class HashUtilTests(unittest.TestCase):
self.failUnlessEqual(len(h2), 16)
self.failUnlessEqual(h1, h2)
def test_well_known_tagged_hash(self):
self.assertEqual(
b"yra322btzoqjp4ts2jon5dztgnilcdg6jgztgk7joi6qpjkitg2q",
base32.b2a(hashutil.tagged_hash(b"tag", b"hello world")),
)
self.assertEqual(
b"kfbsfssrv2bvtp3regne6j7gpdjcdjwncewriyfdtt764o5oa7ta",
base32.b2a(hashutil.tagged_hash(b"different", b"hello world")),
)
self.assertEqual(
b"z34pzkgo36chbjz2qykonlxthc4zdqqquapw4bcaoogzvmmcr3zq",
base32.b2a(hashutil.tagged_hash(b"different", b"goodbye world")),
)
def test_well_known_tagged_pair_hash(self):
self.assertEqual(
b"wmto44q3shtezwggku2fxztfkwibvznkfu6clatnvfog527sb6dq",
base32.b2a(hashutil.tagged_pair_hash(b"tag", b"hello", b"world")),
)
self.assertEqual(
b"lzn27njx246jhijpendqrxlk4yb23nznbcrihommbymg5e7quh4a",
base32.b2a(hashutil.tagged_pair_hash(b"different", b"hello", b"world")),
)
self.assertEqual(
b"qnehpoypxxdhjheqq7dayloghtu42yr55uylc776zt23ii73o3oq",
base32.b2a(hashutil.tagged_pair_hash(b"different", b"goodbye", b"world")),
)
def test_chk(self):
h1 = hashutil.convergence_hash(3, 10, 1000, b"data", b"secret")
h2 = hashutil.convergence_hasher(3, 10, 1000, b"secret")