def setUp(self): super().setUp() deadline_offset = 10 message_timestamp = get_current_utc_timestamp() + deadline_offset compute_task_def = message.ComputeTaskDef() compute_task_def['task_id'] = '8' compute_task_def['subtask_id'] = '8' compute_task_def['deadline'] = message_timestamp task_to_compute = tasks.TaskToComputeFactory( compute_task_def=compute_task_def, requestor_public_key=encode_hex(REQUESTOR_PUBLIC_KEY), provider_public_key=encode_hex(PROVIDER_PUBLIC_KEY), price=0, ) task_to_compute = load( dump( task_to_compute, REQUESTOR_PRIVATE_KEY, settings.CONCENT_PUBLIC_KEY, ), settings.CONCENT_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY, check_time=False, ) report_computed_task = tasks.ReportComputedTaskFactory( task_to_compute=task_to_compute) self.force_report_computed_task = message.ForceReportComputedTask( report_computed_task=report_computed_task)
def test_send_should_return_http_400_if_data_is_incorrect(self): compute_task_def = message.ComputeTaskDef() task_to_compute = message.TaskToCompute( compute_task_def = compute_task_def, provider_public_key = PROVIDER_PUBLIC_KEY, requestor_public_key = REQUESTOR_PUBLIC_KEY, price=0, ) report_computed_task = message.tasks.ReportComputedTask( task_to_compute = task_to_compute ) with freeze_time("2017-11-17 9:56:00"): force_report_computed_task = message.ForceReportComputedTask( report_computed_task = report_computed_task ) response = self.client.post( reverse('core:send'), data = dump( force_report_computed_task, PROVIDER_PRIVATE_KEY, CONCENT_PUBLIC_KEY, ), content_type = 'application/octet-stream', ) self._test_400_response( response, error_code=ErrorCode.MESSAGE_VALUE_BLANK ) data = message.ForceReportComputedTask() data.report_computed_task = message.tasks.ReportComputedTask() compute_task_def['deadline'] = self.message_timestamp - 3600 data.report_computed_task.task_to_compute = message.TaskToCompute(compute_task_def = compute_task_def) response = self.client.post( reverse('core:send'), data = dump( data, PROVIDER_PRIVATE_KEY, CONCENT_PUBLIC_KEY, ), content_type = 'application/octet-stream', ) self._test_400_response(response)
def test_message_sig(self): """Signed message inside a signed message""" concent_keys = cryptography.ECCx(None) provider_keys = cryptography.ECCx(None) requestor_keys = cryptography.ECCx(None) report_computed_task = factories.tasks.ReportComputedTaskFactory() # Dump TaskToCompute to make it signed s_rct = shortcuts.dump( report_computed_task, requestor_keys.raw_privkey, provider_keys.raw_pubkey, ) # Load TaskToCompute back to its original format # Task TaskToCompute is verified correctly report_computed_task = shortcuts.load( s_rct, provider_keys.raw_privkey, requestor_keys.raw_pubkey, ) first_sig = report_computed_task.sig first_hash = report_computed_task.get_short_hash() force_report = message.ForceReportComputedTask() force_report.report_computed_task = report_computed_task s_force_report = shortcuts.dump( force_report, provider_keys.raw_privkey, concent_keys.raw_pubkey, ) force_report = shortcuts.load( s_force_report, concent_keys.raw_privkey, provider_keys.raw_pubkey, ) second_sig = force_report.report_computed_task.sig second_hash = force_report.report_computed_task.get_short_hash() self.assertEqual(first_sig, second_sig) self.assertEqual(first_hash, second_hash) # Now, attached TaskToCompute should still be verified using # original key. ecdsa_verify will raise InvalidSignature on # failure. cryptography.ecdsa_verify( requestor_keys.raw_pubkey, force_report.report_computed_task.sig, force_report.report_computed_task.get_short_hash(), )
def _send_force_report_computed_task(self): report_computed_task = message.tasks.ReportComputedTask( task_to_compute=self.task_to_compute # pylint: disable=no-member ) force_report_computed_task = message.ForceReportComputedTask( report_computed_task=report_computed_task, ) return self.client.post( reverse('core:send'), data=dump(force_report_computed_task, self.PROVIDER_PRIVATE_KEY, settings.CONCENT_PUBLIC_KEY), content_type='application/octet-stream', )
def setUp(self): self.request_factory = RequestFactory() self.want_to_compute = message.WantToComputeTask( node_name=1, task_id=2, perf_index=3, price=4, max_resource_size=5, max_memory_size=6, num_cores=7, ) self.message_to_view = { "node_name": self.want_to_compute.node_name, # pylint: disable=no-member "task_id": self.want_to_compute.task_id, # pylint: disable=no-member "perf_index": self.want_to_compute.perf_index, # pylint: disable=no-member "price": self.want_to_compute.price, # pylint: disable=no-member "max_resource_size": self.want_to_compute.max_resource_size, # pylint: disable=no-member "max_memory_size": self.want_to_compute.max_memory_size, # pylint: disable=no-member "num_cores": self.want_to_compute.num_cores, # pylint: disable=no-member } deadline_offset = 10 message_timestamp = get_current_utc_timestamp() + deadline_offset compute_task_def = tasks.ComputeTaskDefFactory( task_id='8', subtask_id='8', deadline=message_timestamp, ) task_to_compute = tasks.TaskToComputeFactory( compute_task_def=compute_task_def, requestor_public_key=encode_hex(REQUESTOR_PUBLIC_KEY), provider_public_key=encode_hex(PROVIDER_PUBLIC_KEY), price=0, ) task_to_compute = load( dump( task_to_compute, REQUESTOR_PRIVATE_KEY, settings.CONCENT_PUBLIC_KEY, ), settings.CONCENT_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY, check_time=False, ) self.force_report_computed_task = message.ForceReportComputedTask() self.force_report_computed_task.report_computed_task = message.tasks.ReportComputedTask( ) self.force_report_computed_task.report_computed_task.task_to_compute = task_to_compute
def test_receive_should_return_ack_if_the_receive_queue_contains_only_force_report_and_its_past_deadline( self): with freeze_time("2017-11-17 10:00:00"): self.compute_task_def = message.ComputeTaskDef() self.compute_task_def['task_id'] = '2' self.compute_task_def['subtask_id'] = '2' self.compute_task_def['deadline'] = get_current_utc_timestamp() self.task_to_compute = message.TaskToCompute( compute_task_def=self.compute_task_def, provider_public_key=PROVIDER_PUBLIC_KEY, requestor_public_key=REQUESTOR_PUBLIC_KEY, price=0, ) self.force_golem_data = message.ForceReportComputedTask( report_computed_task=message.tasks.ReportComputedTask( task_to_compute=self.task_to_compute))
def test_send_should_return_http_400_if_task_to_compute_deadline_is_not_an_integer( self): compute_task_def = message.ComputeTaskDef() compute_task_def['task_id'] = '8' compute_task_def['subtask_id'] = '8' invalid_values = [ -11, 'a11b', {}, [], (1, 2, 3), None, ] for deadline in invalid_values: StoredMessage.objects.all().delete() compute_task_def['deadline'] = deadline task_to_compute = tasks.TaskToComputeFactory( compute_task_def=compute_task_def, provider_public_key=PROVIDER_PUBLIC_KEY, requestor_public_key=REQUESTOR_PUBLIC_KEY, price=0, ) serialized_task_to_compute = dump(task_to_compute, REQUESTOR_PRIVATE_KEY, PROVIDER_PUBLIC_KEY) deserialized_task_to_compute = load(serialized_task_to_compute, PROVIDER_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY, check_time=False) with freeze_time("2017-11-17 10:00:00"): force_report_computed_task = message.ForceReportComputedTask( report_computed_task=message.tasks.ReportComputedTask( task_to_compute=deserialized_task_to_compute)) response_400 = self.client.post( reverse('core:send'), data=dump(force_report_computed_task, PROVIDER_PRIVATE_KEY, CONCENT_PUBLIC_KEY), content_type='application/octet-stream', ) self._test_400_response(response_400)
def setUp(self): with freeze_time("2017-11-17 10:00:00"): super().setUp() self.compute_task_def = message.ComputeTaskDef() self.compute_task_def['task_id'] = '1' self.compute_task_def['subtask_id'] = '1' self.compute_task_def['deadline'] = get_current_utc_timestamp() + ( 60 * 37) self.task_to_compute = tasks.TaskToComputeFactory( compute_task_def=self.compute_task_def, provider_public_key=PROVIDER_PUBLIC_KEY, requestor_public_key=REQUESTOR_PUBLIC_KEY, price=0, ) self.size = 58 self.force_golem_data = message.ForceReportComputedTask( report_computed_task=message.tasks.ReportComputedTask( task_to_compute=self.task_to_compute, size=self.size))
def setUp(self): super().setUp() deadline_offset = 10 message_timestamp = get_current_utc_timestamp() + deadline_offset compute_task_def = message.ComputeTaskDef() compute_task_def['task_id'] = '8' compute_task_def['subtask_id'] = '8' compute_task_def['deadline'] = message_timestamp compute_task_def['extra_data'] = { 'frames': [1], 'output_format': 'PNG', 'scene_file': 'kitten.blend', } task_to_compute = tasks.TaskToComputeFactory( compute_task_def=compute_task_def, requestor_public_key=encode_hex(REQUESTOR_PUBLIC_KEY), provider_public_key=encode_hex(PROVIDER_PUBLIC_KEY), price=0, ) task_to_compute = load( dump( task_to_compute, REQUESTOR_PRIVATE_KEY, settings.CONCENT_PUBLIC_KEY, ), settings.CONCENT_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY, check_time=False, ) report_computed_task = tasks.ReportComputedTaskFactory( task_to_compute=task_to_compute) report_computed_task = load( dump( report_computed_task, PROVIDER_PRIVATE_KEY, settings.CONCENT_PUBLIC_KEY, ), settings.CONCENT_PRIVATE_KEY, PROVIDER_PUBLIC_KEY, check_time=False, ) self.force_report_computed_task = message.ForceReportComputedTask( report_computed_task=report_computed_task)
def setUp(self): super().setUp() self.compute_task_def = message.ComputeTaskDef() self.compute_task_def['task_id'] = '1' self.compute_task_def['subtask_id'] = '1' self.compute_task_def['deadline'] = get_current_utc_timestamp() - 60 self.task_to_compute = tasks.TaskToComputeFactory( compute_task_def=self.compute_task_def, provider_public_key=PROVIDER_PUBLIC_KEY, requestor_public_key=REQUESTOR_PUBLIC_KEY, price=0, ) self.size = 58 with freeze_time("2017-11-17 10:00:00"): self.force_golem_data = message.ForceReportComputedTask( report_computed_task=message.tasks.ReportComputedTask( task_to_compute=self.task_to_compute, size=self.size)) message_timestamp = datetime.datetime.now(timezone.utc) new_message = StoredMessage( type=self.force_golem_data.report_computed_task.TYPE, timestamp=message_timestamp, data=self.force_golem_data.report_computed_task.serialize(), task_id=self.compute_task_def['task_id'], # pylint: disable=no-member subtask_id=self.compute_task_def['subtask_id'], # pylint: disable=no-member ) new_message.full_clean() new_message.save() task_to_compute_message = StoredMessage( type=self.task_to_compute.TYPE, timestamp=message_timestamp, data=self.task_to_compute.serialize(), task_id=self.compute_task_def['task_id'], # pylint: disable=no-member subtask_id=self.compute_task_def['subtask_id'], # pylint: disable=no-member ) task_to_compute_message.full_clean() task_to_compute_message.save() ack_report_computed_task = message.AckReportComputedTask( report_computed_task=message.tasks.ReportComputedTask( task_to_compute=self.task_to_compute, )) stored_ack_report_computed_task = StoredMessage( type=ack_report_computed_task.TYPE, timestamp=message_timestamp, data=ack_report_computed_task.serialize(), task_id=self.compute_task_def['task_id'], # pylint: disable=no-member subtask_id=self.compute_task_def['subtask_id'], # pylint: disable=no-member ) stored_ack_report_computed_task.full_clean() stored_ack_report_computed_task.save() client_provider = Client(public_key_bytes=PROVIDER_PUBLIC_KEY) client_provider.full_clean() client_provider.save() client_requestor = Client(public_key_bytes=REQUESTOR_PUBLIC_KEY) client_requestor.full_clean() client_requestor.save() subtask = Subtask( task_id=self.compute_task_def['task_id'], subtask_id=self.compute_task_def['subtask_id'], report_computed_task=new_message, task_to_compute=task_to_compute_message, ack_report_computed_task=stored_ack_report_computed_task, state=Subtask.SubtaskState.REPORTED.name, # pylint: disable=no-member provider=client_provider, requestor=client_requestor, result_package_size=self.size, computation_deadline=parse_timestamp_to_utc_datetime( self.compute_task_def['deadline'])) subtask.full_clean() subtask.save() new_message_inbox = PendingResponse( response_type=PendingResponse.ResponseType.ForceReportComputedTask. name, # pylint: disable=no-member client=client_requestor, queue=PendingResponse.Queue.ReceiveOutOfBand.name, # pylint: disable=no-member subtask=subtask, ) new_message_inbox.full_clean() new_message_inbox.save()
def test_message_sig(self): """Signed message inside a signed message""" concent_keys = cryptography.ECCx(None) provider_keys = cryptography.ECCx(None) requestor_keys = cryptography.ECCx(None) task_to_compute = message.TaskToCompute() ctd = message.ComputeTaskDef({ 'task_id': 20, }) task_to_compute.compute_task_def = ctd # Dump TaskToCompute to make it signed s_task_to_compute = shortcuts.dump( task_to_compute, requestor_keys.raw_privkey, provider_keys.raw_pubkey, ) # Load TaskToCompute back to its original format task_to_compute = shortcuts.load( s_task_to_compute, provider_keys.raw_privkey, requestor_keys.raw_pubkey, ) first_sig = task_to_compute.sig first_hash = task_to_compute.get_short_hash() # Task TaskToCompute is verified correctly cryptography.ecdsa_verify( requestor_keys.raw_pubkey, task_to_compute.sig, task_to_compute.get_short_hash(), ) force_report = message.ForceReportComputedTask() force_report.task_to_compute = task_to_compute s_force_report = shortcuts.dump( force_report, provider_keys.raw_privkey, concent_keys.raw_pubkey, ) force_report = shortcuts.load( s_force_report, concent_keys.raw_privkey, provider_keys.raw_pubkey, ) second_sig = force_report.task_to_compute.sig second_hash = force_report.task_to_compute.get_short_hash() self.assertEqual(first_sig, second_sig) self.assertEqual(first_hash, second_hash) # Now, attached TaskToCompute should still be verified using # original key. ecdsa_verify will raise InvalidSignature on # failure. cryptography.ecdsa_verify( requestor_keys.raw_pubkey, force_report.task_to_compute.sig, force_report.task_to_compute.get_short_hash(), )