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_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_get_root_user(self): name = "*****@*****.**" encryptor = ["rotate", 5] location = ["local", None, "tom"] ident = Identity(name, encryptor, location) self.compareIdents(name, [ident]) name = "*****@*****.**" encryptor = ["rotate", -5] location = ["local", None, "jerry"] ident = Identity(name, encryptor, location) self.compareIdents(name, [ident])
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)
def test_decode(self): from ejtp.identity import Identity, IdentityCache cache = IdentityCache() cache[['testing']] = Identity('joe', ['rotate', 1], ['testing']) self.assertEqual( frame.encrypted.EncryptedFrame('r["testing"]\x00gpp').decode( cache), RawData('foo'))
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"]'))
def test_get_subbranch_user(self): # Branch with many users # And they have a plan name = "*****@*****.**" classes = ['centurion', 'humanoid', 'raider', 'hybrid'] idents = [] for i in range(len(classes)): encryptor = ['rotate', i + 1] location = ['local', None, classes[i]] idents.append(Identity(name, encryptor, location)) self.compareIdents(name, idents)
def get_identity(): print(NOTICE_EMAIL) name = confirm(get_name, 'The name you chose: %r') loc = confirm(get_location, 'Your location is:\n%r') print("\nNow we generate your encryptor.") enc = None while enc == None: try: enc = get_encryptor() except ValueError: pass return Identity(name, enc, loc)
def setUp(self): self.ident = Identity('joe', ['rotate', 8], None)
def mockup(name="mitzi"): return Identity(*mockups[name])
def setUp(self): self.cache = IdentityCache() self.joe_ident = Identity('joe', ['rotate', 3], ['local', None, 'joe']) self.mitzi_ident = mockup('mitzi') self.atlas_ident = mockup('atlas') self.victor_ident = mockup('victor')
class TestIdentityCache(unittest.TestCase): def setUp(self): self.cache = IdentityCache() self.joe_ident = Identity('joe', ['rotate', 3], ['local', None, 'joe']) self.mitzi_ident = mockup('mitzi') self.atlas_ident = mockup('atlas') self.victor_ident = mockup('victor') def test_setitem_wrong_type(self): self.assertRaisesRegexp(TypeError, "Expected ejtp.identity.core.Identity", self.cache.__setitem__, self.joe_ident, []) def test_setitem_wrong_location(self): self.assertRaisesRegexp(ValueError, "Trying to cache ident in the wrong location", self.cache.__setitem__, ['x', 'y', 'z'], self.joe_ident) def test_update_idents(self): idents = [self.joe_ident, self.victor_ident] self.cache.update_idents(idents) self.assertEquals(sorted(self.cache.all(), key=lambda x: x.key), sorted(idents, key=lambda x: x.key)) for i in idents: self.assertEquals(self.cache[i.location], i) def test_filter_by_name(self): # Create alternate-universe Joe alt_joe = self.joe_ident.clone() alt_joe.location = ['local', None, 'alt_joe'] idents = [self.mitzi_ident, self.joe_ident, alt_joe] self.cache.update_idents(idents) self.assertEqual(self.cache.filter_by_name(self.mitzi_ident.name), [self.mitzi_ident]) joe_filtered = self.cache.filter_by_name(self.joe_ident.name) self.assertEqual( sorted(joe_filtered, key=lambda x: x.key), sorted([self.joe_ident, alt_joe], key=lambda x: x.key)) def test_serialize(self): self.cache.update_ident(self.mitzi_ident) serialized_ident = self.cache.serialize() self.assertIn('["local",null,"mitzi"]', serialized_ident) def test_encrypt_capable(self): self.cache.update_ident(self.mitzi_ident) self.cache.update_ident(self.atlas_ident.public()) self.cache.update_ident(self.joe_ident) capable = [ident.name for ident in self.cache.encrypt_capable()] self.assertIn('joe', capable) self.assertIn('*****@*****.**', capable) self.assertNotIn('*****@*****.**', capable) def _assert_caches(self, cache1, cache2): for item in zip(sorted(cache1.cache.keys()), sorted(cache2.cache.keys())): self.assertEqual(*item) def test_sync(self): mitzi_cache = IdentityCache() atlas_cache = IdentityCache() mitzi_cache.update_ident(self.mitzi_ident) atlas_cache.update_ident(self.atlas_ident) mitzi_cache.sync(atlas_cache) self._assert_caches(mitzi_cache, atlas_cache) def test_deserialize(self): self.cache.update_ident(self.mitzi_ident) self.cache.update_ident(self.atlas_ident) serialization = self.cache.serialize() new_cache = IdentityCache() new_cache.deserialize(serialization) self._assert_caches(self.cache, new_cache) def test_load_from_without_args(self): self.assertRaisesRegexp( ValueError, 'Must provide either file_path or file_object', self.cache.load_from) def test_load(self): self.cache.load_from(testing_path('examplecache.json')) atlas_location = self.cache.find_by_name( "*****@*****.**").location self.assertListEqual(self.atlas_ident.location, atlas_location) def test_save_to_without_args(self): self.assertRaisesRegexp( ValueError, 'Must provide either file_path or file_object', self.cache.save_to) def test_save_to(self): filename = 'temp.json' self.cache.update_ident(self.mitzi_ident) self.cache.save_to(filename, indent=4) os.remove(filename)
def test_get_identity(self): ''' Tries to overlap minimally with more specific tests, except for the encryptor retry mechanism, which is specific to get_identity and must not kill the process during encryptor creation failure. ''' with self.io: self.io.extend([ '*****@*****.**', 'y', 'local', 'athena', 'y', 'y', 'rsa', '120', 'y', 'rotate', '120', 'y', ]) result = get_identity() expected_ident = Identity('*****@*****.**', ['rotate', 120], ['local', None, 'athena']) self.assertEqual(result, expected_ident) lines = self.io.get_lines() expected_beginning = [ 'NOTE: If you intend to host this identity in the DJDNS', 'registry, the email domain needs to match your branch', 'regex to be accessible.', 'http://roaming-initiative.com/blog/blog/djdns-ident-registration.html', 'Your identity name, in email form:', '$ [email protected]', "The name you chose: '*****@*****.**'", 'Is this correct? [y/n]', '$ y', 'The following types are available:', 'local : Can only communicate within a single OS process', 'tcp : IPv6 address, accessed over TCP', 'tcp4 : IPv4 address, accessed over TCP', 'udp : IPv6 address, accessed over UDP', 'udp4 : IPv4 address, accessed over UDP', 'Which type do you want?', '$ local', 'Your callsign:', '$ athena', 'Is this correct? [y/n]', '$ y', 'Your location is:', "['local', None, 'athena']", 'Is this correct? [y/n]', '$ y', 'Now we generate your encryptor.', 'The following types are available:', 'rotate : Only for trivial demos, not recommended!', 'rsa : RSA Public-Key encryption (recommended)', 'Which type do you want?', '$ rsa', 'How many bits?', '$ 120', 'Is this correct? [y/n]', '$ y', 'Generating... if it takes awhile, wiggle your mouse.', ] # Intentionally ignore the env-specific Crypto traceback expected_end = [ "AttributeError: 'NoneType' object has no attribute 'exportKey'", 'The following types are available:', 'rotate : Only for trivial demos, not recommended!', 'rsa : RSA Public-Key encryption (recommended)', 'Which type do you want?', '$ rotate', 'How much to rotate?', '$ 120', 'Is this correct? [y/n]', '$ y', ] self.assertEqual(lines[:len(expected_beginning)], expected_beginning) self.assertEqual(lines[-len(expected_end):], expected_end)
def test_construct(self): from ejtp.identity import Identity ident = Identity('joe', ['rotate', 1], ['testing']) self.assertEqual(frame.encrypted.construct(ident, 'foo'), frame.encrypted.EncryptedFrame('r["testing"]\x00gpp'))