def test_1_operation(self): sign_object(self.public_keys[0], self.private_keys[0], self.operation[0]) self.operation[1] = Operation(OperationRev.from_obj(self.operation[0]), 'http://illegal.example.com/', [self.public_keys[2]]) with self.assertRaises(Operation.OwnershipError): sign_object(self.public_keys[0], self.private_keys[0], self.operation[1]) self.operation[1].verify() self.operation[1] = Operation(OperationRev.from_obj(self.operation[0]), 'http://new.example.com/', [self.public_keys[2]]) sign_object(self.public_keys[2], self.private_keys[2], self.operation[1]) self.assertTrue(self.operation[1].verify())
def test_2_operation(self): sign_object(self.public_keys[1], self.private_keys[1], self.operation[0]) self.operation[1] = Operation(OperationRev.from_obj(self.operation[0]), 'http://new.example.com/', [self.public_keys[2]]) sign_object(self.public_keys[2], self.private_keys[2], self.operation[1]) self.operation[2] = Operation._construct_with_uuid(OperationRev.from_obj(self.operation[1]), uuid4(), 'http://new2.example.com', []) with self.assertRaisesRegex(Operation.UUIDError, "UUID mismatch"): sign_object(self.public_keys[2], self.private_keys[2], self.operation[2]) self.operation[2].verify() self.operation[2] = Operation(OperationRev.from_obj(self.operation[1]), 'http://new2.example.com', []) sign_object(self.public_keys[2], self.private_keys[2], self.operation[2]) self.operation[2].verify()
def test_put_operation_2(self): with self.assertRaisesRegex(Operation.ChainError, "previous_operation_rev does not exist"): self.operation[2] = Operation(OperationRev.from_id( sha256(b'wrong hash').digest()), self.operation[2].address, self.operation[2].owners) sign_object(self.public_keys[0], self.private_keys[0], self.operation[1]) self.operation[2] = Operation._construct_with_uuid(OperationRev.from_obj(self.operation[1]), self.operation[2].uuid, self.operation[2].address, self.operation[2].owners) sign_object(self.public_keys[1], self.private_keys[1], self.operation[2]) with self.assertRaisesRegex(Operation.UUIDError, "UUID mismatch"): self.operation[2].put() self.operation[2] = Operation._construct_with_uuid(OperationRev(), self.operation[2].uuid, self.operation[2].address, self.operation[2].owners) sign_object(self.public_keys[1], self.private_keys[1], self.operation[2]) self.operation[2].put() with self.assertRaisesRegex(Operation.DuplicationError, "object id already in the database"): self.operation[2].put()
def test_put_operation1(self): sign_object(self.public_keys[0], self.private_keys[0], self.operation[0]) self.operation[0].put() self.operation[1] = Operation._construct_with_uuid(OperationRev(), self.operation[0].uuid, self.operation[1].address, self.operation[1].owners) sign_object(self.public_keys[0], self.private_keys[0], self.operation[1]) with self.assertRaisesRegex(Operation.UUIDError, "UUID of the minting operation does not fulfill the requirements"): self.operation[1].put() self.operation[1] = Operation(OperationRev.from_obj(self.operation[0]), self.operation[1].address, self.operation[1].owners) sign_object(self.public_keys[0], self.private_keys[0], self.operation[1]) with self.assertRaises(Operation.OwnershipError): self.operation[1].put() sign_object(self.public_keys[1], self.private_keys[1], self.operation[1]) self.operation[1].put()
def test_3_get_and_remove(self): sign_object(self.public_key, self.private_key, self.operations[0]) self.operations[1] = Operation(OperationRev.from_obj(self.operations[0]), self.operations[1].address, self.operations[1].owners) for op in self.operations: sign_object(self.public_key, self.private_key, op) op.put() for op in self.operations: new_op = Operation.get(op.id) self.assertEqual(new_op.id, op.id) self.assertCountEqual(Operation.get_ids_list(), [op.id for op in self.operations]) self.operations[1].remove() self.operations[0].remove() self.assertCountEqual(Operation.get_ids_list(), [self.operations[2].id]) self.operations[2].remove() for op in self.operations: with self.assertRaises(Operation.DoesNotExist): op.remove() self.assertEqual(Operation.get_ids_list(), [])
def add_operations(uuids, private_keys, public_keys): """ create operations: for uuid[0]: op[0] -> op[2] -> op[6] -> op[8]; for uuid[1]: op[1] -> op[3] -> op[4]; for uuid[2]: op[5] -> op[7] -> op[9] """ ops = [ Operation(OperationRev(), uuids[0], 'http://example1.com/', [public_keys[0]]), Operation(OperationRev(), uuids[1], 'http://example2.com/', [public_keys[1]]), ] sign_object(public_keys[0], private_keys[0], ops[0]) sign_object(public_keys[0], private_keys[0], ops[1]) ops.extend([ Operation(OperationRev.from_obj(ops[0]), uuids[0], 'http://example1.com/v2/', [public_keys[0]]), Operation(OperationRev.from_obj(ops[1]), uuids[1], 'http://example2.com/v2/', [public_keys[1]]) ]) sign_object(public_keys[0], private_keys[0], ops[2]) sign_object(public_keys[1], private_keys[1], ops[3]) ops.append( Operation(OperationRev.from_obj(ops[3]), uuids[1], 'http://example2.com/v3/', [public_keys[1]]) ) sign_object(public_keys[1], private_keys[1], ops[4]) ops.extend([ Operation(OperationRev(), uuids[2], 'http://example3.com/', [public_keys[1], public_keys[2]]), Operation(OperationRev.from_obj(ops[2]), uuids[0], 'http://example1.com/v3/', [public_keys[0], public_keys[2]]) ]) sign_object(public_keys[2], private_keys[2], ops[5]) sign_object(public_keys[0], private_keys[0], ops[6]) ops.extend([ Operation(OperationRev.from_obj(ops[5]), uuids[2], 'http://example3.com/v2/', [public_keys[2]]), Operation(OperationRev.from_obj(ops[6]), uuids[0], 'http://example1.com/v4/', [public_keys[2]]) ]) sign_object(public_keys[1], private_keys[1], ops[7]) sign_object(public_keys[2], private_keys[2], ops[8]) ops.append( Operation(OperationRev.from_obj(ops[7]), uuids[2], 'http://example3.com/v3/', [public_keys[2]]) ) sign_object(public_keys[2], private_keys[2], ops[9]) return ops
def setUp(self): initialise_database('test_database_file') self.private_key = SigningKey.generate() self.public_key = PublicKey.from_signing_key(self.private_key) # self.uuids = [uuid4() for _ in range(3)] self.operations = [[ Operation(OperationRev(),'http://example0.com/v0/', [self.public_key]), Operation(OperationRev(),'http://example1.com/v0/', [self.public_key]) ]] for op in self.operations[0]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[0][0]), 'http://example0.com/v1/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[0][1]), 'http://example1.com/v1/', [self.public_key]), Operation(OperationRev(), 'http://example2.com/v0/', [self.public_key]) ]) for op in self.operations[1]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[1][0]),'http://example0.com/v2/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[1][1]), 'http://example1.com/v2/', [self.public_key]) ]) for op in self.operations[2]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[1][1]), 'http://alternative1.com/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[1][2]), 'http://alternative2.com/', [self.public_key]) ]) for op in self.operations[3]: sign_object(self.public_key, self.private_key, op) timestamp = int(time.time()) - 100 self.blocks = [Block.from_operations_list(BlockRev(), timestamp, self.operations[0])] self.blocks[0].mine() sign_object(self.public_key, self.private_key, self.blocks[0]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[0]), timestamp + 20, self.operations[1])) self.blocks[1].mine() sign_object(self.public_key, self.private_key, self.blocks[1]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[1]), timestamp + 40, self.operations[2])) self.blocks[2].mine() sign_object(self.public_key, self.private_key, self.blocks[2]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[1]), timestamp + 60, self.operations[3])) self.blocks[3].mine() sign_object(self.public_key, self.private_key, self.blocks[3])
def test_2_put(self): sign_object(self.public_key, self.private_key, self.operations[0]) self.operations[0].put() with self.assertRaisesRegex(Operation.DuplicationError, "object id already in the database"): self.operations[0].put() self.operations[1] = Operation(OperationRev.from_obj(self.operations[0]), self.operations[1].address, self.operations[1].owners) sign_object(self.public_key, self.private_key, self.operations[1]) self.operations[1].put() sign_object(self.public_key, self.private_key, self.operations[2]) self.operations[2].put() revision_id_list = Operation.get_ids_list() self.assertEqual(len(revision_id_list), 3) self.assertCountEqual(revision_id_list, [op.id for op in self.operations])
def test_fields(self): self.assertEqual(self.identifier.uuid, self.uuid) self.assertEqual(self.identifier.operation_rev, OperationRev.from_obj(self.operation))
def test(): try: os.remove('test_database_file') except OSError: pass initialise_database('test_database_file') obj_private_key = SigningKey.generate() obj_public_key = PublicKey.from_signing_key(obj_private_key) obj_uuids = [uuid4() for _ in range(3)] obj_operations = [[ Operation(OperationRev(), 'http://example0.com/v0/', [obj_public_key]), Operation(OperationRev(), 'http://example1.com/v0/', [obj_public_key]) ]] for op in obj_operations[0]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[0][0]), 'http://example0.com/v1/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[0][1]), 'http://example1.com/v1/', [obj_public_key]), Operation(OperationRev(), 'http://example2.com/v0/', [obj_public_key]) ]) for op in obj_operations[1]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][0]), 'http://example0.com/v2/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://example1.com/v2/', [obj_public_key]) ]) for op in obj_operations[2]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://alternative1.com/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][2]), 'http://alternative2.com/', [obj_public_key]) ]) for op in obj_operations[3]: sign_object(obj_public_key, obj_private_key, op) timestamp = int(time.time()) - 100 obj_blocks = [Block.from_operations_list(BlockRev(), timestamp, obj_operations[0])] obj_blocks[0].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[0]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[0]), timestamp + 20, obj_operations[1])) obj_blocks[1].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[1]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 40, obj_operations[2])) obj_blocks[2].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[2]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 60, obj_operations[3])) obj_blocks[3].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[3]) for block in obj_blocks: block.put() for block in obj_blocks: new_block = Block.get(block.id) assert new_block.id == block.id for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass assert sorted(Block.get_ids_list()) == sorted([block.id for block in obj_blocks]) obj_blocks[2].remove() for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[3].remove() try: obj_blocks[0].remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[1].remove() obj_blocks[0].remove() for block in obj_blocks: try: block.remove() raise AssertionError except Block.DoesNotExist: pass assert Block.get_ids_list() == [] close_database() os.remove('test_database_file')