def test_hashing_not_equal(self): entity1 = Entity() entity2 = Entity() addr1 = Address(entity1) addr2 = Address(entity2) self.assertNotEqual(hash(addr1), hash(addr2))
def test_construction_from_bytes(self): # create a reference and a copy of the entity reference = Entity() other = Entity(reference.private_key_bytes) self.assertEqual(reference.private_key_bytes, other.private_key_bytes) self.assertEqual(reference.private_key, other.private_key) self.assertEqual(reference.private_key_hex, other.private_key_hex) self.assertEqual(reference.public_key_bytes, other.public_key_bytes) self.assertEqual(reference.public_key, other.public_key) self.assertEqual(reference.public_key_hex, other.public_key_hex)
def test_dumps(self): ref = Entity() stream = StringIO() ref.dump(stream, SUPER_SECURE_PASSWORD) stream.seek(0) other = Entity.load(stream, SUPER_SECURE_PASSWORD) self.assertEqual(ref.private_key, other.private_key)
def test_generation(self): entity = Entity() self.assertEqual(32, len(entity.private_key_bytes)) # check the other binary representations self.assertEqual( base64.b64encode(entity.private_key_bytes).decode(), entity.private_key) self.assertEqual(entity.private_key_bytes.hex(), entity.private_key_hex) signing_key = ecdsa.SigningKey.from_string( entity.private_key_bytes, curve=Entity.curve, hashfunc=Entity.hash_function) verifying_key = signing_key.get_verifying_key() self.assertEqual(verifying_key.to_string(), entity.public_key_bytes) # check the other binary representations self.assertEqual( base64.b64encode(entity.public_key_bytes).decode(), entity.public_key) self.assertEqual(entity.public_key_bytes.hex(), entity.public_key_hex)
def test_hex_display(self): entity = Entity() address = Address(entity) address_bytes = bytes(address) hex_address = binascii.hexlify(address_bytes).decode() self.assertEqual(hex_address, address.to_hex())
def test_construction_from_strings(self): ref = Entity() test1 = Identity.from_hex(ref.public_key_hex) self.assertEqual(ref, test1) test2 = Identity.from_base64(ref.public_key) self.assertEqual(ref, test2)
def test_hex_display(self): entity = Entity() address = Address(entity) address_bytes = bytes(address) hex_address = address_bytes.hex() self.assertEqual(hex_address, address.to_hex())
def test_construction_from_verifying_key(self): entity = Entity() # create a private / public key pair # create the identity from the identity (copy) identity = Identity(entity.verifying_key) # ensure the they are the same self.assertEqual(identity.public_key_bytes, entity.public_key_bytes)
def test_invalid_display(self): entity = Entity() address = Address(entity) address_bytes = bytes(address) invalid_checksum = bytes([0] * Address.CHECKSUM_SIZE) invalid_display = base58.b58encode(address_bytes + invalid_checksum).decode() with self.assertRaises(ValueError): _ = Address(invalid_display)
def test_construction_from_identity(self): entity = Entity() address = Address(entity) # manually compute the address value expected_address_bytes, expected_display = _calc_address(entity.public_key_bytes) self.assertEqual(expected_address_bytes, bytes(address)) self.assertEqual(expected_display, str(address))
def test_construction_from_string(self): entity = Entity() # manually compute the address value expected_address_bytes, expected_display = _calc_address(entity.public_key_bytes) # re-create the address from the display string address = Address(expected_display) self.assertEqual(bytes(address), expected_address_bytes) self.assertEqual(str(address), expected_display)
def test_prompt_dump(self): ref = Entity() stream = StringIO() with patch('getpass.getpass') as mock_getpass: mock_getpass.side_effect = [ WEAK_PASSWORD, SUPER_SECURE_PASSWORD, WEAK_PASSWORD, SUPER_SECURE_PASSWORD ] ref.prompt_dump(stream) stream.seek(0)
def test_prompt_load(self): ref = Entity() stream = StringIO() # write the entity to the stream ref.dump(stream, SUPER_SECURE_PASSWORD) # reset the stream stream.seek(0) with patch('getpass.getpass') as mock_getpass: mock_getpass.side_effect = [SUPER_SECURE_PASSWORD] other = Entity.prompt_load(stream) self.assertEqual(other.private_key, ref.private_key)
def test_signing_verifying_cycle(self): entity = Entity() payload = 'foo bar is a baz' # sign the payload signature = entity.sign(payload.encode()) # verify the payload self.assertTrue(entity.verify(payload.encode(), signature)) # modify the signature slightly bad_signature = bytes([(signature[0] + 1) & 0xff]) + signature[1:] self.assertFalse(entity.verify(payload.encode(), bad_signature)) # also ensure a different payload is not verifiable self.assertFalse( entity.verify('foo bar is not a baz'.encode(), signature))
def test_supports_collections(self): entity = Entity() # create a series of identities identity1 = Identity(entity) identity2 = Identity(entity) identity3 = Identity(identity2) identity_list = [identity1, identity2, identity3] self.assertEqual(len(identity_list), 3) self.assertIn(identity1, identity_list) self.assertIn(identity2, identity_list) self.assertIn(identity3, identity_list) # due to the fact that each identity is the name, the first element will always be matched self.assertEqual(identity_list.index(identity1), 0) self.assertEqual(identity_list.index(identity2), 0) self.assertEqual(identity_list.index(identity3), 0) identity_set = set(identity_list) self.assertEqual(len(identity_set), 1) self.assertIn(identity1, identity_set) self.assertIn(identity2, identity_set) self.assertIn(identity3, identity_set)
def test_not_equal(self): entity1 = Entity() entity2 = Entity() self.assertNotEqual(entity1, entity2)
def test_construction_from_identity(self): entity = Entity() # create a private / public key pair identity = Identity(entity) self.assertEqual(entity.public_key_bytes, identity.public_key_bytes)
def test_hashing_equal(self): entity = Entity() addr1 = Address(entity) addr2 = Address(entity) self.assertEqual(hash(addr1), hash(addr2))
def test_signing_key(self): entity = Entity() self.assertIsInstance(entity.signing_key, ecdsa.SigningKey)
def test_invalid_construction(self): with self.assertRaises(RuntimeError): _ = Entity(str())
def test_dump_failure_on_weak_password(self): stream = StringIO() with self.assertRaises(RuntimeError): Entity().dump(stream, WEAK_PASSWORD)
def test_dumps_failure_on_weak_password(self): with self.assertRaises(RuntimeError): Entity().dumps(WEAK_PASSWORD)
def test_construction_from_address(self): entity = Entity() address1 = Address(entity) address2 = Address(address1) self.assertEqual(bytes(address1), bytes(address2))
def test_equality(self): entity = Entity() addr1 = Address(entity) addr2 = Address(entity) self.assertEqual(addr1, addr2)
def test_construction_from_base64(self): ref = Entity() ref_key = ref.private_key other = Entity.from_base64(ref_key) self.assertEqual(ref.private_key_bytes, other.private_key_bytes)
def test_loads(self): ref = Entity() value = ref.dumps(SUPER_SECURE_PASSWORD) other = Entity.loads(value, SUPER_SECURE_PASSWORD) self.assertEqual(ref.private_key, other.private_key)
def test_conversion_from_hex(self): ref = Entity() other = Entity.from_hex(ref.private_key_hex) self.assertEqual(ref.private_key, other.private_key)