def test_is_valid_resposne(self): """Test C_FIND.is_valid_response.""" primitive = C_FIND() assert not primitive.is_valid_response primitive.MessageIDBeingRespondedTo = 1 assert not primitive.is_valid_response primitive.Status = 0x0000 assert primitive.is_valid_response
def test_send_c_find(self): """Check sending DIMSE C-FIND messages.""" # C-FIND-RQ primitive = C_FIND() primitive.MessageID = 1 primitive.AffectedSOPClassUID = '1.1.1' def test_callback(msg): """Callback""" self.assertEqual(msg.__class__.__name__, 'C_FIND_RQ') self.dimse.on_send_dimse_message = test_callback self.dimse.send_msg(primitive, 1) # C-FIND-RSP primitive = C_FIND() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 0x0000 def test_callback(msg): """Callback""" self.assertEqual(msg.__class__.__name__, 'C_FIND_RSP') self.dimse.on_send_dimse_message = test_callback self.dimse.send_msg(primitive, 1)
def message_to_primitive(self): """Convert the DIMSEMessage class to a DIMSE primitive. Returns ------- DIMSE message primitive One of the DIMSE message primitives from pynetdicom3.dimse_primitives generated from the current DIMSEMessage. """ # pylint: disable=too-many-branches cls_type_name = self.__class__.__name__ if 'C_ECHO' in cls_type_name: primitive = C_ECHO() elif 'C_STORE' in cls_type_name: primitive = C_STORE() elif 'C_FIND' in cls_type_name: primitive = C_FIND() elif 'C_GET' in cls_type_name: primitive = C_GET() elif 'C_MOVE' in cls_type_name: primitive = C_MOVE() elif 'C_CANCEL' in cls_type_name: primitive = C_CANCEL() elif 'N_EVENT' in cls_type_name: primitive = N_EVENT_REPORT() elif 'N_GET' in cls_type_name: primitive = N_GET() elif 'N_SET' in cls_type_name: primitive = N_SET() elif 'N_ACTION' in cls_type_name: primitive = N_ACTION() elif 'N_CREATE' in cls_type_name: primitive = N_CREATE() elif 'N_DELETE' in cls_type_name: primitive = N_DELETE() # Command Set # For each parameter in the primitive, set the appropriate value # from the Message's Command Set elements for elem in self.command_set: if hasattr(primitive, elem.keyword): setattr(primitive, elem.keyword, self.command_set.__getattr__(elem.keyword)) # Datasets # Set the primitive's DataSet/Identifier/etc attribute try: dataset_keyword = _DATASET_KEYWORDS[cls_type_name] setattr(primitive, dataset_keyword, self.data_set) except KeyError: pass # Set the presentation context ID the message was set under primitive._context_id = self.ID return primitive
def test_bad_req_identifier(self): """Test SCP handles a bad request identifier""" self.scp = DummyFindSCP() self.scp.statuses = [0xFF00] self.scp.identifiers = [self.query] self.scp.start() ae = AE() ae.add_requested_context( ProductCharacteristicsQueryInformationModelFind, ExplicitVRLittleEndian) ae.acse_timeout = 5 ae.dimse_timeout = 5 assoc = ae.associate('localhost', 11112) assert assoc.is_established req = C_FIND() req.MessageID = 1 req.AffectedSOPClassUID = ProductCharacteristicsQueryInformationModelFind.uid req.Priority = 2 req.Identifier = BytesIO( b'\x08\x00\x01\x00\x04\x00\x00\x00\x00\x08\x00\x49') assoc.dimse.send_msg(req, 1) rsp, _ = assoc.dimse.receive_msg(True) assert rsp.Status == 0xC310 assoc.release() self.scp.stop()
def test_is_valid_request(self): """Test C_FIND.is_valid_request""" primitive = C_FIND() assert not primitive.is_valid_request primitive.MessageID = 1 assert not primitive.is_valid_request primitive.AffectedSOPClassUID = '1.2' assert not primitive.is_valid_request primitive.Priority = 2 assert not primitive.is_valid_request primitive.Identifier = BytesIO() assert primitive.is_valid_request
def test_conversion_rq(self): """ Check conversion to a -RQ PDU produces the correct output """ primitive = C_FIND() primitive.MessageID = 7 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.Priority = 0x02 ref_identifier = Dataset() ref_identifier.PatientID = '*' ref_identifier.QueryRetrieveLevel = "PATIENT" primitive.Identifier = BytesIO(encode(ref_identifier, True, True)) dimse_msg = C_FIND_RQ() dimse_msg.primitive_to_message(primitive) pdvs = dimse_msg.encode_msg(1, 16382) cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] self.assertEqual(cs_pdv, c_find_rq_cmd) self.assertEqual(ds_pdv, c_find_rq_ds)
def test_conversion_rsp(self): """ Check conversion to a -RSP PDU produces the correct output """ primitive = C_FIND() primitive.MessageIDBeingRespondedTo = 5 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.Status = 0xFF00 ref_identifier = Dataset() ref_identifier.QueryRetrieveLevel = "PATIENT" ref_identifier.RetrieveAETitle = validate_ae_title("FINDSCP") ref_identifier.PatientName = "ANON^A^B^C^D" primitive.Identifier = BytesIO(encode(ref_identifier, True, True)) dimse_msg = C_FIND_RSP() dimse_msg.primitive_to_message(primitive) pdvs = dimse_msg.encode_msg(1, 16382) cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] self.assertEqual(cs_pdv, c_find_rsp_cmd) self.assertEqual(ds_pdv, c_find_rsp_ds)
def message_to_primitive(self): """Convert the DIMSEMessage class to a DIMSE primitive. Returns ------- primitive One of the pynetdicom3.dimse_primitives primitives generated from the current DIMSEMessage. """ # pylint: disable=too-many-branches cls_type_name = self.__class__.__name__ if 'C_ECHO' in cls_type_name: primitive = C_ECHO() elif 'C_STORE' in cls_type_name: primitive = C_STORE() elif 'C_FIND' in cls_type_name: primitive = C_FIND() elif 'C_GET' in cls_type_name: primitive = C_GET() elif 'C_MOVE' in cls_type_name: primitive = C_MOVE() elif 'C_CANCEL' in cls_type_name: primitive = C_CANCEL() elif 'N_EVENT' in cls_type_name: primitive = N_EVENT_REPORT() elif 'N_GET' in cls_type_name: primitive = N_GET() elif 'N_SET' in cls_type_name: primitive = N_SET() elif 'N_ACTION' in cls_type_name: primitive = N_ACTION() elif 'N_CREATE' in cls_type_name: primitive = N_CREATE() elif 'N_DELETE' in cls_type_name: primitive = N_DELETE() # elif 'C_CANCEL' in cls_type_name: # primitive = C_CANCEL() # Command Set # For each parameter in the primitive, set the appropriate value # from the Message's Command Set elements for elem in self.command_set: if hasattr(primitive, elem.keyword): setattr(primitive, elem.keyword, self.command_set.__getattr__(elem.keyword)) # Datasets # Set the primitive's DataSet/Identifier/etc attribute if cls_type_name == 'C_STORE_RQ': setattr(primitive, 'DataSet', self.data_set) elif cls_type_name in [ 'C_FIND_RQ', 'C_FIND_RSP', 'C_GET_RQ', 'C_GET_RSP', 'C_MOVE_RQ', 'C_MOVE_RSP' ]: setattr(primitive, 'Identifier', self.data_set) elif cls_type_name == 'N_EVENT_REPORT_RQ': setattr(primitive, 'EventInformation', self.data_set) elif cls_type_name == 'N_EVENT_REPORT_RSP': setattr(primitive, 'EventReply', self.data_set) elif cls_type_name in [ 'N_GET_RSP', 'N_SET_RSP', 'N_CREATE_RQ', 'N_CREATE_RSP' ]: setattr(primitive, 'AttributeList', self.data_set) elif cls_type_name == 'N_SET_RQ': setattr(primitive, 'ModificationList', self.data_set) elif cls_type_name == 'N_ACTION_RQ': setattr(primitive, 'ActionInformation', self.data_set) elif cls_type_name == 'N_ACTION_RSP': setattr(primitive, 'ActionReply', self.data_set) return primitive
def test_callback_receive_c_find(self): """Check callback for receiving DIMSE C-FIND messages.""" # C-FIND-RQ primitive = C_FIND() primitive.MessageID = 7 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.AffectedSOPInstanceUID = '1.2.392.200036.9116.2.6.1.48.' \ '1215709044.1459316254.522441' primitive.Priority = 0x02 primitive.MoveOriginatorApplicationEntityTitle = 'UNITTEST_SCP' primitive.MoveOriginatorMessageID = 3 primitive.Identifier = BytesIO() # No dataset msg = C_FIND_RQ() msg.primitive_to_message(primitive) self.dimse.debug_receive_c_find_rq(msg) # Dataset ref_ds = Dataset() ref_ds.PatientID = 'Test1101' ref_ds.PatientName = "Tube HeNe" primitive.Identifier = BytesIO(encode(ref_ds, True, True)) msg = C_FIND_RQ() msg.primitive_to_message(primitive) # Dataset self.dimse.debug_receive_c_find_rq(msg) # C-FIND-RSP primitive = C_FIND() primitive.MessageIDBeingRespondedTo = 7 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.AffectedSOPInstanceUID = '1.2.392.200036.9116.2.6.1.48.' \ '1215709044.1459316254.522441' primitive.MoveOriginatorApplicationEntityTitle = 'UNITTEST_SCP' primitive.MoveOriginatorMessageID = 3 primitive.Identifier = BytesIO() # No dataset primitive.Status = 0x0000 # Must be for pending msg = C_FIND_RSP() msg.primitive_to_message(primitive) self.dimse.debug_receive_c_find_rsp(msg) primitive.Identifier = BytesIO(encode(ref_ds, True, True)) msg = C_FIND_RSP() msg.primitive_to_message(primitive) # Dataset msg.data_set = BytesIO(encode(ref_ds, True, True)) self.dimse.debug_receive_c_find_rsp(msg) # Non-pending status msg.data_set.Status = 0x0001 self.dimse.debug_receive_c_find_rsp(msg) # C-CANCEL-FIND-RQ self.dimse.debug_receive_c_cancel_rq(msg)
def test_callback_send_c_find(self): """Check callback for sending DIMSE C-FIND messages.""" # C-FIND-RQ primitive = C_FIND() primitive.MessageID = 1 primitive.AffectedSOPClassUID = '1.1.1' primitive.Priority = 0x02 primitive.Identifier = BytesIO() # No dataset self.dimse.send_msg(primitive, 1) # Dataset bytestream = BytesIO() bytestream.write(c_store_ds) primitive.Identifier = bytestream self.dimse.send_msg(primitive, 1) # C-FIND-RSP primitive = C_FIND() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 0x0000 primitive.AffectedSOPClassUID = '1.1.1' # No dataset self.dimse.send_msg(primitive, 1) # Dataset bytestream = BytesIO() bytestream.write(c_store_ds) primitive.Identifier = bytestream primitive.Status = 0xFF00 # Only has dataset when 0xFF00 or 0xFF01 self.dimse.send_msg(primitive, 1)
def test_exceptions(self): """ Check incorrect types/values for properties raise exceptions """ primitive = C_FIND() # MessageID with self.assertRaises(TypeError): primitive.MessageID = 'halp' with self.assertRaises(TypeError): primitive.MessageID = 1.111 with self.assertRaises(ValueError): primitive.MessageID = 65536 with self.assertRaises(ValueError): primitive.MessageID = -1 # MessageIDBeingRespondedTo with self.assertRaises(TypeError): primitive.MessageIDBeingRespondedTo = 'halp' with self.assertRaises(TypeError): primitive.MessageIDBeingRespondedTo = 1.111 with self.assertRaises(ValueError): primitive.MessageIDBeingRespondedTo = 65536 with self.assertRaises(ValueError): primitive.MessageIDBeingRespondedTo = -1 # AffectedSOPClassUID with self.assertRaises(TypeError): primitive.AffectedSOPClassUID = 45.2 with self.assertRaises(TypeError): primitive.AffectedSOPClassUID = 100 with self.assertRaises(ValueError): primitive.AffectedSOPClassUID = 'abc' # Priority with self.assertRaises(ValueError): primitive.Priority = 45.2 with self.assertRaises(ValueError): primitive.Priority = 'abc' with self.assertRaises(ValueError): primitive.Priority = -1 with self.assertRaises(ValueError): primitive.Priority = 3 # Identifier with self.assertRaises(TypeError): primitive.Identifier = 'halp' with self.assertRaises(TypeError): primitive.Identifier = 1.111 with self.assertRaises(TypeError): primitive.Identifier = 50 with self.assertRaises(TypeError): primitive.Identifier = [30, 10] # Status with self.assertRaises(TypeError): primitive.Status = 19.4
def test_assignment(self): """ Check assignment works correctly """ primitive = C_FIND() primitive.MessageID = 11 self.assertEqual(primitive.MessageID, 11) primitive.MessageIDBeingRespondedTo = 13 self.assertEqual(primitive.MessageIDBeingRespondedTo, 13) # AffectedSOPClassUID primitive.AffectedSOPClassUID = '1.1.1' self.assertEqual(primitive.AffectedSOPClassUID, UID('1.1.1')) self.assertTrue(isinstance(primitive.AffectedSOPClassUID, UID)) primitive.AffectedSOPClassUID = UID('1.1.2') self.assertEqual(primitive.AffectedSOPClassUID, UID('1.1.2')) self.assertTrue(isinstance(primitive.AffectedSOPClassUID, UID)) primitive.AffectedSOPClassUID = b'1.1.3' self.assertEqual(primitive.AffectedSOPClassUID, UID('1.1.3')) self.assertTrue(isinstance(primitive.AffectedSOPClassUID, UID)) primitive.Priority = 0x02 self.assertEqual(primitive.Priority, 0x02) ref_ds = Dataset() ref_ds.PatientID = '*' ref_ds.QueryRetrieveLevel = "PATIENT" primitive.Identifier = BytesIO(encode(ref_ds, True, True)) #self.assertEqual(primitive.DataSet, ref_ds) primitive.Status = 0x0000 self.assertEqual(primitive.Status, 0x0000) primitive.Status = 0xC123 self.assertEqual(primitive.Status, 0xC123) primitive.Status = 0xEE01 self.assertEqual(primitive.Status, 0xEE01)
def test_assignment(self): """ Check assignment works correctly """ primitive = C_FIND() primitive.MessageID = 11 assert primitive.MessageID == 11 primitive.MessageIDBeingRespondedTo = 13 assert primitive.MessageIDBeingRespondedTo == 13 # AffectedSOPClassUID primitive.AffectedSOPClassUID = '1.1.1' assert primitive.AffectedSOPClassUID == UID('1.1.1') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = UID('1.1.2') assert primitive.AffectedSOPClassUID == UID('1.1.2') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = b'1.1.3' assert primitive.AffectedSOPClassUID == UID('1.1.3') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.Priority = 0x02 assert primitive.Priority == 0x02 ref_ds = Dataset() ref_ds.PatientID = '*' ref_ds.QueryRetrieveLevel = "PATIENT" primitive.Identifier = BytesIO(encode(ref_ds, True, True)) #assert primitive.DataSet, ref_ds) primitive.Status = 0x0000 assert primitive.Status == 0x0000 primitive.Status = 0xC123 assert primitive.Status == 0xC123 primitive.Status = 0xEE01 assert primitive.Status == 0xEE01
pass @staticmethod def receive_pdu(): """Dummy Receive method to test DIMSEServiceProvider.Receive""" pass @staticmethod def peek_next_pdu(): return 0x01 REFERENCE_MSG = [ (C_ECHO(), ('C_ECHO_RQ', 'C_ECHO_RSP')), (C_STORE(), ('C_STORE_RQ', 'C_STORE_RSP')), (C_FIND(), ('C_FIND_RQ', 'C_FIND_RSP')), (C_GET(), ('C_GET_RQ', 'C_GET_RSP')), (C_MOVE(), ('C_MOVE_RQ', 'C_MOVE_RSP')), (C_CANCEL(), (None, 'C_CANCEL_RQ')), (N_EVENT_REPORT(), ('N_EVENT_REPORT_RQ', 'N_EVENT_REPORT_RSP')), (N_GET(), ('N_GET_RQ', 'N_GET_RSP')), (N_SET(), ('N_SET_RQ', 'N_SET_RSP')), (N_ACTION(), ('N_ACTION_RQ', 'N_ACTION_RSP')), (N_CREATE(), ('N_CREATE_RQ', 'N_CREATE_RSP')), (N_DELETE(), ('N_DELETE_RQ', 'N_DELETE_RSP')), ] class TestDIMSEProvider(object): """Test DIMSE service provider operations.""" def setup(self):