def test_construct(self): from ejtp.identity import Identity ident = Identity('joe', ['rotate', 1], ['testing']) signature = ident.sign('foo') siglen = len(signature) self.assertEqual( frame.signed.construct(ident, 'foo'), frame.signed.SignedFrame( RawData('s["testing"]\x00') + (siglen // 256, siglen % 256) + signature + 'foo'))
def test_decode(self): from ejtp.identity import Identity, IdentityCache ident = Identity('joe', ['rotate', 1], ['testing']) cache = IdentityCache() cache[['testing']] = ident signature = ident.sign('foo') siglen = len(signature) signed_content = RawData('s["testing"]\x00') + ( siglen // 256, siglen % 256) + signature + 'foo' self.assertEqual( frame.signed.SignedFrame(signed_content).decode(cache), RawData('foo')) self.assertRaises( ValueError, frame.signed.SignedFrame( 's["testing"]\x00\x00\x07invalidfoo').decode, cache)
class TestIdentity(unittest.TestCase): def setUp(self): self.ident = Identity('joe', ['rotate', 8], None) def test_name(self): self.assertEqual('joe', self.ident.name) def test_encryptor(self): self.assertIsInstance(self.ident.encryptor, RotateEncryptor) def test_encryptor_cache(self): e = self.ident.encryptor self.assertEqual(e, self.ident.encryptor) def test_signature(self): plaintext = 'example' sig = self.ident.sign(plaintext) self.assertTrue(self.ident.verify_signature(sig, plaintext)) def test_public(self): ident = mockup() proto = ident.encryptor.proto() self.assertIn('PRIVATE', str(proto[1])) self.assertNotIn('PUBLIC', str(proto[1])) public_ident = ident.public() public_proto = public_ident.encryptor.proto() self.assertNotIn('PRIVATE', str(public_proto[1])) self.assertIn('PUBLIC', str(public_proto[1])) def test_equality(self): self.assertTrue(self.ident == Identity('joe', ['rotate', 8], None)) self.assertFalse(self.ident == Identity('joke', ['rotate', 8], None)) self.assertFalse(self.ident == Identity('joe', ['rotate', 9], None)) self.assertFalse( self.ident == Identity('joe', ['rotate', 8], "tangerine")) self.assertFalse(self.ident == Identity( 'joe', ['rotate', 8], None, arbitrary="hula")) def test_key(self): # Location is None self.assertRaises(TypeError, lambda: self.ident.key) # Set location to legitimate data self.ident.location = ['local', None, 'joey'] self.assertEqual(self.ident.key, String('["local",null,"joey"]'))