Beispiel #1
0
 def test_get_hash_nested_structure(self):
     """
     Ensure a tree like object is recursively hashed to produce a root-hash
     value.
     """
     child_list = ['bar', 1, 1.234, ]
     child_dict = {
         'b': False,
         'a': None,
         'c': True
     }
     to_hash = {
         'foo': child_list,
         'baz': child_dict
     }
     seed_hashes = []
     # REMEMBER - in this algorithm the keys to a dict object are ordered.
     seed_hashes.append(sha512(b'baz').hexdigest())
     seed_hashes.append(_get_hash(child_dict).hexdigest())
     seed_hashes.append(sha512(b'foo').hexdigest())
     seed_hashes.append(_get_hash(child_list).hexdigest())
     seed = ''.join(seed_hashes)
     expected = sha512(seed.encode('utf-8'))
     actual = _get_hash(to_hash)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #2
0
 def test_get_hash_str(self):
     """
     Strings are hashed correctly
     """
     expected = sha512(b'foo')
     actual = _get_hash('foo')
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #3
0
 def test_get_hash_float(self):
     """
     Ensure float values are hashed correctly.
     """
     expected = sha512(b'12345.6789')
     actual = _get_hash(12345.6789)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #4
0
 def test_get_hash_int(self):
     """
     Ensure integer values are hashed correctly.
     """
     expected = sha512(b'1234567890987654321234567890987654321')
     actual = _get_hash(1234567890987654321234567890987654321)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #5
0
 def test_get_hash_boolean_false(self):
     """
     Ensure hash of Python's False boolean value is a hash of 'false'
     (since this is the false value in JSON).
     """
     expected = sha512(b'false')
     actual = _get_hash(False)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #6
0
 def test_get_hash_boolean_true(self):
     """
     Ensure hash of Python's True boolean value is a hash of 'true' (since
     this is the true value in JSON).
     """
     expected = sha512(b'true')
     actual = _get_hash(True)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #7
0
 def test_get_hash_none(self):
     """
     Ensure the hash of Python's None is actually a hash of 'null' (since
     this is the null value for JSON).
     """
     expected = sha512(b'null')
     actual = _get_hash(None)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #8
0
    def test_get_hash_list(self):
        """
        Ensure all the items in a list are hashed in the correct order.
        """
        to_hash = []
        for i in range(5):
            to_hash.append(str(uuid.uuid4()))

        seed_hashes = []
        for item in to_hash:
            seed_hashes.append(sha512(item.encode('utf-8')).hexdigest())
        seed = ''.join(seed_hashes)
        expected = sha512(seed.encode('utf-8'))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())
Beispiel #9
0
    def test_get_hash_dict(self):
        """
        Ensures that the dict is hashed in such a way that the keys are
        sorted so the resulting leaf hashes are used in the correct order.
        """
        to_hash = {}
        for i in range(5):
            k = str(uuid.uuid4())
            v = str(uuid.uuid4())
            to_hash[k] = v

        seed_hashes = []
        for k in sorted(to_hash):
            v = to_hash[k]
            seed_hashes.append(sha512(k.encode('utf-8')).hexdigest())
            seed_hashes.append(sha512(v.encode('utf-8')).hexdigest())
        seed = ''.join(seed_hashes)
        expected = sha512(seed.encode('utf-8'))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())