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_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 = "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 = [] for fragment in dimse_msg.encode_msg(1, 16382): pdvs.append(fragment) cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] assert cs_pdv == c_find_rsp_cmd # print(len(ds_pdv), len(c_find_rsp_ds)) # print(' '.join([f"{b:02X}" for b in ds_pdv])) # print(' '.join([f"{b:02X}" for b in c_find_rsp_ds])) assert ds_pdv == c_find_rsp_ds
def test_bad_req_identifier(self): """Test SCP handles a bad request identifier""" def handle(event): yield 0xFF00, self.query handlers = [(evt.EVT_C_FIND, handle)] self.ae = ae = AE() ae.add_supported_context(GeneralRelevantPatientInformationQuery) ae.add_requested_context(GeneralRelevantPatientInformationQuery, ExplicitVRLittleEndian) scp = ae.start_server(('', 11112), block=False, evt_handlers=handlers) 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 = GeneralRelevantPatientInformationQuery req.Priority = 2 req.Identifier = BytesIO( b'\x08\x00\x01\x00\x40\x40\x00\x00\x00\x00\x00\x08\x00\x49') assoc._reactor_checkpoint.clear() assoc.dimse.send_msg(req, 1) cx_id, rsp = assoc.dimse.get_msg(True) assoc._reactor_checkpoint.set() assert rsp.Status == 0xC310 assoc.release() scp.shutdown()
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 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_bad_req_identifier(self): """Test SCP handles a bad request identifier""" def handle(event): try: ds = event.identifier for elem in ds.iterall(): pass except NotImplementedError: yield 0xC310, None return yield 0x0000, None handlers = [(evt.EVT_C_FIND, handle)] self.ae = ae = AE() ae.add_supported_context( ProductCharacteristicsQueryInformationModelFind) ae.add_requested_context( ProductCharacteristicsQueryInformationModelFind, ExplicitVRLittleEndian) scp = ae.start_server(('', 11112), block=False, evt_handlers=handlers) assoc = ae.associate('localhost', 11112) assert assoc.is_established req = C_FIND() req.MessageID = 1 req.AffectedSOPClassUID = ProductCharacteristicsQueryInformationModelFind req.Priority = 2 req.Identifier = BytesIO( b'\x08\x00\x01\x00\x40\x40\x00\x00\x00\x00\x00\x08\x00\x49') assoc._reactor_checkpoint.clear() assoc.dimse.send_msg(req, 1) cx_id, rsp = assoc.dimse.get_msg(True) assoc._reactor_checkpoint.set() assert rsp.Status == 0xC310 assoc.release() assert assoc.is_released scp.shutdown()
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
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 = [] for fragment in dimse_msg.encode_msg(1, 16382): pdvs.append(fragment) cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] assert cs_pdv == c_find_rq_cmd assert ds_pdv == c_find_rq_ds
def test_exceptions(self): """ Check incorrect types/values for properties raise exceptions """ primitive = C_FIND() # MessageID with pytest.raises(TypeError): primitive.MessageID = 'halp' with pytest.raises(TypeError): primitive.MessageID = 1.111 with pytest.raises(ValueError): primitive.MessageID = 65536 with pytest.raises(ValueError): primitive.MessageID = -1 # MessageIDBeingRespondedTo with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 'halp' with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 1.111 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = 65536 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = -1 # AffectedSOPClassUID with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 45.2 with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 100 # Priority with pytest.raises(ValueError): primitive.Priority = 45.2 with pytest.raises(ValueError): primitive.Priority = 'abc' with pytest.raises(ValueError): primitive.Priority = -1 with pytest.raises(ValueError): primitive.Priority = 3 # Identifier msg = r"'Identifier' parameter must be a BytesIO object" with pytest.raises(TypeError, match=msg): primitive.Identifier = 'halp' with pytest.raises(TypeError): primitive.Identifier = 1.111 with pytest.raises(TypeError): primitive.Identifier = 50 with pytest.raises(TypeError): primitive.Identifier = [30, 10] # Status with pytest.raises(TypeError): primitive.Status = 19.4