Beispiel #1
0
    def test_callback_send_c_move(self):
        """Check callback for sending DIMSE C-MOVE messages."""
        # C-MOVE-RQ
        primitive = C_MOVE()
        primitive.MessageID = 1
        primitive.AffectedSOPClassUID = '1.1.1'
        primitive.MoveDestination = b'TESTSCP'
        # No dataset
        primitive.Identifier = BytesIO()
        self.dimse.send_msg(primitive, 1)
        # Dataset
        bytestream = BytesIO()
        bytestream.write(c_store_ds)
        primitive.Identifier = bytestream
        self.dimse.send_msg(primitive, 1)

        # C-MOVE-RSP
        primitive = C_MOVE()
        primitive.MessageIDBeingRespondedTo = 1
        primitive.Status = 0x0000
        # No dataset
        primitive.Identifier = BytesIO()
        self.dimse.send_msg(primitive, 1)
        # Dataset
        bytestream = BytesIO()
        bytestream.write(c_store_ds)
        primitive.Identifier = bytestream
        self.dimse.send_msg(primitive, 1)
    def test_uid_exceptions_true(self):
        """Test ValueError raised with ENFORCE_UID_CONFORMANCE = True."""
        primitive = C_MOVE()
        _config.ENFORCE_UID_CONFORMANCE = True

        with pytest.raises(ValueError):
            primitive.AffectedSOPClassUID = 'abc'
    def message_to_primitive(self):
        """Convert the ``DIMSEMessage`` class to a DIMSE primitive.

        Returns
        -------
        DIMSEPrimitive sub-class
            One of the DIMSE message primitives from
            :ref:`pynetdicom.dimse_primitives<api_dimse_primitives>` generated
            from the current ``DIMSEMessage`` sub-class object.
        """
        # 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.context_id

        return primitive
    def test_conversion_rsp(self):
        """ Check conversion to a -RSP PDU produces the correct output """
        primitive = C_MOVE()
        primitive.MessageIDBeingRespondedTo = 5
        primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2'
        primitive.Status = 0xFF00
        primitive.NumberOfRemainingSuboperations = 3
        primitive.NumberOfCompletedSuboperations = 1
        primitive.NumberOfFailedSuboperations = 2
        primitive.NumberOfWarningSuboperations = 4

        ref_identifier = Dataset()
        ref_identifier.QueryRetrieveLevel = "PATIENT"
        ref_identifier.PatientID = "*"

        primitive.Identifier = BytesIO(encode(ref_identifier, True, True))

        dimse_msg = C_MOVE_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_move_rsp_cmd
        assert ds_pdv == c_move_rsp_ds
    def test_conversion_rq(self):
        """ Check conversion to a -RQ PDU produces the correct output """
        primitive = C_MOVE()
        primitive.MessageID = 7
        primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2'
        primitive.Priority = 0x02
        primitive.MoveDestination = validate_ae_title("MOVE_SCP")

        ref_identifier = Dataset()
        ref_identifier.PatientID = '*'
        ref_identifier.QueryRetrieveLevel = "PATIENT"

        primitive.Identifier = BytesIO(encode(ref_identifier, True, True))

        dimse_msg = C_MOVE_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_move_rq_cmd
        assert ds_pdv == c_move_rq_ds
 def test_is_valid_resposne(self):
     """Test C_MOVE.is_valid_response."""
     primitive = C_MOVE()
     assert not primitive.is_valid_response
     primitive.MessageIDBeingRespondedTo = 1
     assert not primitive.is_valid_response
     primitive.Status = 0x0000
     assert primitive.is_valid_response
Beispiel #7
0
    def test_aet_short_false(self):
        """Test using long AE titles."""
        primitive = C_MOVE()

        _config.USE_SHORT_DIMSE_AET = False

        primitive.MoveDestination = "A"
        assert "A" == primitive.MoveDestination
Beispiel #8
0
    def test_aet_deprecation(self):
        """Test deprecation warning for Move Destination from bytes."""
        primitive = C_MOVE()

        msg = ("The use of bytes with 'Move Destination' is deprecated, "
               "use an ASCII str instead")
        with pytest.warns(DeprecationWarning, match=msg):
            primitive.MoveDestination = b'TEST'

        assert primitive.MoveDestination == 'TEST'
    def test_uid_exceptions_false(self):
        """Test ValueError raised with ENFORCE_UID_CONFORMANCE = False."""
        primitive = C_MOVE()

        _config.ENFORCE_UID_CONFORMANCE = False

        primitive.AffectedSOPClassUID = 'abc'
        assert primitive.AffectedSOPClassUID == 'abc'

        # Can't have more than 64 characters
        with pytest.raises(ValueError):
            primitive.AffectedSOPClassUID = 'abc' * 22
Beispiel #10
0
 def test_is_valid_request(self):
     """Test C_MOVE.is_valid_request"""
     primitive = C_MOVE()
     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.MoveDestination = b'1234567890123456'
     assert not primitive.is_valid_request
     primitive.Identifier = BytesIO()
     assert primitive.is_valid_request
Beispiel #11
0
    def test_aet_short_true(self):
        """Test using short AE titles."""
        primitive = C_MOVE()

        _config.USE_SHORT_DIMSE_AET = True

        primitive.MoveDestination = b'A'
        aet = primitive.MoveDestination
        assert b'A ' == primitive.MoveDestination

        primitive.MoveDestination = b'ABCDEFGHIJKLMNO'
        assert b'ABCDEFGHIJKLMNO ' == primitive.MoveDestination

        primitive.MoveDestination = b'ABCDEFGHIJKLMNOP'
        assert b'ABCDEFGHIJKLMNOP' == primitive.MoveDestination

        primitive.MoveDestination = b'ABCDEFGHIJKLMNOPQ'
        assert b'ABCDEFGHIJKLMNOP' == primitive.MoveDestination
Beispiel #12
0
    def test_assignment(self):
        """ Check assignment works correctly """
        primitive = C_MOVE()

        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

        primitive.MoveDestination = 'UNITTEST_SCP'
        assert primitive.MoveDestination == b'UNITTEST_SCP    '

        ref_ds = Dataset()
        ref_ds.PatientID = 1234567

        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
Beispiel #13
0
    def test_callback_receive_c_move(self):
        """Check callback for receiving DIMSE C-MOVE messages."""
        # C-MOVE-RQ
        msg = C_MOVE_RQ()
        self.dimse.debug_receive_c_move_rq(msg)

        # C-MOVE-RSP
        primitive = C_MOVE()
        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()
        primitive.NumberOfCompletedSuboperations = 1
        primitive.NumberOfWarningSuboperations = 3
        primitive.NumberOfFailedSuboperations = 4

        # No dataset, remaining subops
        primitive.Status = 0x0000  # Must be for pending
        msg = C_MOVE_RSP()
        msg.primitive_to_message(primitive)
        self.dimse.debug_receive_c_move_rsp(msg)

        # Dataset
        ref_ds = Dataset()
        ref_ds.PatientID = 'Test1101'
        ref_ds.PatientName = "Tube HeNe"
        primitive.Identifier = BytesIO(encode(ref_ds, True, True))
        primitive.NumberOfRemainingSuboperations = 2

        msg = C_GET_RSP()

        msg.primitive_to_message(primitive)

        # Dataset
        self.dimse.debug_receive_c_move_rsp(msg)

        # C-CANCEL-MOVE-RQ
        self.dimse.debug_receive_c_cancel_rq(msg)
Beispiel #14
0
    def test_send_c_move_rsp_no_affected_sop(self):
        """Test the handler for C-MOVE rsp with no Affected SOP Class UID."""
        self.ae = ae = AE()
        ae.add_supported_context('1.2.840.10008.1.1')
        ae.add_requested_context('1.2.840.10008.1.1')
        scp = ae.start_server(('', 11112), block=False)

        assoc = ae.associate('localhost', 11112)
        assert assoc.is_established

        msg = C_MOVE()
        msg.MessageIDBeingRespondedTo = 1
        msg.Status = 0x0000
        msg.NumberOfRemainingSuboperations = 0
        msg.NumberOfCompletedSuboperations = 0
        msg.NumberOfFailedSuboperations = 0
        msg.NumberOfWarningSuboperations = 0

        assoc.dimse.send_msg(msg, 1)

        assoc.release()
        scp.shutdown()
Beispiel #15
0
    def test_aet_short_true(self):
        """Test using short AE titles."""
        primitive = C_MOVE()

        _config.USE_SHORT_DIMSE_AET = True

        primitive.MoveDestination = "A"
        aet = primitive.MoveDestination
        assert "A" == primitive.MoveDestination

        primitive.MoveDestination = "  ABCD FG  "
        assert primitive.MoveDestination == "  ABCD FG  "
        primitive.MoveDestination = "  ABCD FG  "
        assert primitive.MoveDestination == "  ABCD FG  "
        primitive.MoveDestination = "ABCDEFGHIJKLMNO"
        assert "ABCDEFGHIJKLMNO" == primitive.MoveDestination
        primitive.MoveDestination = "ABCDEFGHIJKLMNOP"
        assert "ABCDEFGHIJKLMNOP" == primitive.MoveDestination

        msg = "Invalid 'Move Destination' value 'ABCDEFGHIJKLMNOPQ'"
        with pytest.raises(ValueError, match=msg):
            primitive.MoveDestination = "ABCDEFGHIJKLMNOPQ"

        assert "ABCDEFGHIJKLMNOP" == primitive.MoveDestination
    @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:
    """Test DIMSE service provider operations."""
    def setup(self):
        """Set up"""
        self.dimse = DIMSEServiceProvider(DummyAssociation())
Beispiel #17
0
    @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):
        """Set up"""
        self.dimse = DIMSEServiceProvider(DummyDUL(), 1)
Beispiel #18
0
    def test_exceptions(self):
        """ Check incorrect types/values for properties raise exceptions """
        primitive = C_MOVE()

        # 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

        # NumberOfRemainingSuboperations
        with pytest.raises(TypeError):
            primitive.NumberOfRemainingSuboperations = 'halp'
        with pytest.raises(TypeError):
            primitive.NumberOfRemainingSuboperations = 1.111
        with pytest.raises(ValueError):
            primitive.NumberOfRemainingSuboperations = -1

        # NumberOfCompletedSuboperations
        with pytest.raises(TypeError):
            primitive.NumberOfCompletedSuboperations = 'halp'
        with pytest.raises(TypeError):
            primitive.NumberOfCompletedSuboperations = 1.111
        with pytest.raises(ValueError):
            primitive.NumberOfCompletedSuboperations = -1

        # NumberOfFailedSuboperations
        with pytest.raises(TypeError):
            primitive.NumberOfFailedSuboperations = 'halp'
        with pytest.raises(TypeError):
            primitive.NumberOfFailedSuboperations = 1.111
        with pytest.raises(ValueError):
            primitive.NumberOfFailedSuboperations = -1

        # NumberOfWarningSuboperations
        with pytest.raises(TypeError):
            primitive.NumberOfWarningSuboperations = 'halp'
        with pytest.raises(TypeError):
            primitive.NumberOfWarningSuboperations = 1.111
        with pytest.raises(ValueError):
            primitive.NumberOfWarningSuboperations = -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

        # MoveDestination
        with pytest.raises(TypeError):
            primitive.MoveDestination = 45.2

        with pytest.raises(TypeError):
            primitive.MoveDestination = 100

        with pytest.raises(ValueError):
            primitive.MoveDestination = ''

        with pytest.raises(ValueError):
            primitive.MoveDestination = '    '

        # 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