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 - dict objects are ordered by key. seed_hashes.append(sha512('baz').hexdigest()) seed_hashes.append(_get_hash(child_dict).hexdigest()) seed_hashes.append(sha512('foo').hexdigest()) seed_hashes.append(_get_hash(child_list).hexdigest()) seed = ''.join(seed_hashes) expected = sha512(seed) actual = _get_hash(to_hash) self.assertEqual(expected.hexdigest(), actual.hexdigest())
def test_get_hash_unicode(self): """ Strings are hashed correctly """ expected = sha512(u'foo') actual = _get_hash(u'foo') self.assertEqual(expected.hexdigest(), actual.hexdigest())
def test_get_hash_float(self): """ Ensure float values are hashed correctly. """ expected = sha512('12345.6789') actual = _get_hash(12345.6789) self.assertEqual(expected.hexdigest(), actual.hexdigest())
def test_get_hash_long(self): """ Ensure long values are hashed correctly. """ expected = sha512('1234567890987654321234567890987654321') actual = _get_hash(1234567890987654321234567890987654321) self.assertEqual(expected.hexdigest(), actual.hexdigest())
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('false') actual = _get_hash(False) self.assertEqual(expected.hexdigest(), actual.hexdigest())
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('true') actual = _get_hash(True) self.assertEqual(expected.hexdigest(), actual.hexdigest())
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('null') actual = _get_hash(None) self.assertEqual(expected.hexdigest(), actual.hexdigest())
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).hexdigest()) seed = ''.join(seed_hashes) expected = sha512(seed) actual = _get_hash(to_hash) self.assertEqual(expected.hexdigest(), actual.hexdigest())
def test_get_hash_dict(self): """ Ensures that the dict is hashed in such a way that the keys are sorted so the hashes 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).hexdigest()) seed_hashes.append(sha512(v).hexdigest()) seed = ''.join(seed_hashes) expected = sha512(seed) actual = _get_hash(to_hash) self.assertEqual(expected.hexdigest(), actual.hexdigest())