class MagicHashingTests(unittest.TestCase): """Test magic content hashing.""" def setUp(self): """Set up.""" self.hasher = MagicContentHash() def test_hashing_empty(self): """Test the hashing for no data.""" r = self.hasher.hash_object.hexdigest() s = hashlib.sha1("Ubuntu One").hexdigest() self.assertEqual(r, s) def test_hashing_content_once(self): """Test the hashing for some content sent once.""" self.hasher.update("foobar") r = self.hasher.hash_object.hexdigest() s = hashlib.sha1("Ubuntu Onefoobar").hexdigest() self.assertEqual(r, s) def test_hashing_content_upadting(self): """Test the hashing for some content sent more than once.""" c1 = os.urandom(1000) c2 = os.urandom(1000) self.hasher.update(c1) self.hasher.update(c2) r = self.hasher.hash_object.hexdigest() s = hashlib.sha1("Ubuntu One" + c1 + c2).hexdigest() self.assertEqual(r, s) def test_hexdigest_hiding(self): """Can not access the hex digest.""" self.assertRaises(NotImplementedError, self.hasher.hexdigest) def test_digest_hiding(self): """Can not access the digest.""" self.assertRaises(NotImplementedError, self.hasher.digest) def test_content_hash_hiding(self): """The content hash is not the content hash!""" self.hasher.update("foobar") hexdigest = self.hasher.hash_object.hexdigest() ch = self.hasher.content_hash() # not a string, and never show the content self.assertFalse(isinstance(ch, basestring)) self.assertFalse(hexdigest in str(ch)) self.assertFalse(hexdigest in repr(ch)) # we have the real value hidden in the object self.assertEqual('magic_hash:' + hexdigest, ch._magic_hash) def test_not_pickable(self): """The magic hasher and value can not be pickled""" # the hasher self.assertRaises(NotImplementedError, pickle.dumps, self.hasher) # the value ch = self.hasher.content_hash() self.assertRaises(NotImplementedError, pickle.dumps, ch)
def setUp(self): """Set up.""" self.hasher = MagicContentHash()