Ejemplo n.º 1
0
    def test_conversion_rsp(self):
        """ Check conversion to a -RSP PDU produces the correct output """
        primitive = C_STORE()
        primitive.MessageIDBeingRespondedTo = 5
        primitive.AffectedSOPClassUID = '1.2.4.10'
        primitive.AffectedSOPInstanceUID = '1.2.4.5.7.8'
        primitive.Status = 0x0000

        dimse_msg = C_STORE_RSP()
        dimse_msg.primitive_to_message(primitive)

        pdvs = dimse_msg.encode_msg(1, 16382)
        cs_pdv = pdvs[0].presentation_data_value_list[0][1]
        self.assertEqual(cs_pdv, c_store_rsp_cmd)
Ejemplo n.º 2
0
 def test_is_valid_request(self):
     """Test C_STORE.is_valid_request"""
     primitive = C_STORE()
     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.AffectedSOPInstanceUID = '1.2.1'
     assert not primitive.is_valid_request
     primitive.DataSet = BytesIO()
     assert primitive.is_valid_request
Ejemplo n.º 3
0
    def test_encode(self):
        """Test encoding of a DIMSE message."""
        primitive = C_STORE()
        primitive.MessageID = 7
        primitive.AffectedSOPClassUID = '1.1.1'
        primitive.AffectedSOPInstanceUID = '1.2.1'
        primitive.Priority = 0x02
        primitive.MoveOriginatorApplicationEntityTitle = 'UNITTEST'
        primitive.MoveOriginatorMessageID = 3

        # Test encode without dataset
        dimse_msg = C_STORE_RQ()
        dimse_msg.primitive_to_message(primitive)
        p_data_list = []
        for pdata in dimse_msg.encode_msg(12, 16):
            p_data_list.append(pdata)
        assert p_data_list[0].presentation_data_value_list[0][1][
            0:1] == b'\x01'
        assert p_data_list[-1].presentation_data_value_list[0][1][
            0:1] == b'\x03'
        assert dimse_msg.ID == 12

        # Test encode with dataset
        ds = Dataset()
        ds.PatientID = 'Test1101'
        ds.PatientName = 'Tube^HeNe'
        primitive.DataSet = BytesIO(encode(ds, True, True))

        dimse_msg = C_STORE_RQ()
        dimse_msg.primitive_to_message(primitive)
        p_data_list = []
        for pdata in dimse_msg.encode_msg(13, 10):
            p_data_list.append(pdata)

        assert p_data_list[0].presentation_data_value_list[0][1][
            0:1] == b'\x01'
        assert p_data_list[-1].presentation_data_value_list[0][1][
            0:1] == b'\x02'
        assert p_data_list[-2].presentation_data_value_list[0][1][
            0:1] == b'\x00'
        assert p_data_list[-10].presentation_data_value_list[0][1][
            0:1] == b'\x03'
        assert dimse_msg.ID == 13

        p_data_list = []
        for pdata in dimse_msg.encode_msg(1, 31682):
            p_data_list.append(pdata)
        assert p_data_list[0].presentation_data_value_list[0][
            1] == c_store_rq_cmd
        assert p_data_list[1].presentation_data_value_list[0][1] == c_store_ds
Ejemplo n.º 4
0
    def test_decode(self):
        """Test decoding of a DIMSE message."""
        primitive = C_STORE()
        primitive.MessageID = 7
        primitive.AffectedSOPClassUID = '1.1.1'
        primitive.AffectedSOPInstanceUID = '1.2.1'
        primitive.Priority = 0x02
        primitive.MoveOriginatorApplicationEntityTitle = b'UNITTEST'
        primitive.MoveOriginatorMessageID = 3
        ds = Dataset()
        ds.PatientID = 'Test1101'
        ds.PatientName = 'Tube^HeNe'
        primitive.DataSet = BytesIO(encode(ds, True, True))
        dimse_msg = C_STORE_RQ()
        dimse_msg.primitive_to_message(primitive)

        # CMD: (1x4, 3x1), DS: (0x1, 2x1)
        p_data = dimse_msg.encode_msg(12, 24)

        # Command set decoding
        dimse_msg.decode_msg(next(p_data))  # MCHB 1
        dimse_msg.decode_msg(next(p_data))  # MCHB 1
        dimse_msg.decode_msg(next(p_data))  # MCHB 1
        dimse_msg.decode_msg(next(p_data))  # MCHB 1
        dimse_msg.decode_msg(next(p_data))  # MCHB 3 - end of command set
        assert dimse_msg.__class__ == C_STORE_RQ

        # Test decoded command set
        cs = dimse_msg.command_set
        assert cs.CommandGroupLength == 102
        assert cs.AffectedSOPClassUID == UID('1.1.1')
        assert cs.AffectedSOPInstanceUID == UID('1.2.1')
        assert cs.Priority == 2
        assert cs.CommandDataSetType == 1
        assert cs.CommandField == 1
        assert cs.MoveOriginatorApplicationEntityTitle == 'UNITTEST        '
        assert cs.MoveOriginatorMessageID == 3

        # Test decoded dataset
        dimse_msg.decode_msg(next(p_data))  # MCHB 1 # MCHB 0
        dimse_msg.decode_msg(next(p_data))  # MCHB 1 # MCHB 2
        assert dimse_msg.data_set.getvalue() == c_store_ds[1:]

        # Test returns false
        msg = C_STORE_RSP()
        assert not msg.decode_msg(c_store_rsp_cmd)
Ejemplo n.º 5
0
    def test_decode(self):
        """Test decoding of a DIMSE message."""
        primitive = C_STORE()
        primitive.MessageID = 7
        primitive.AffectedSOPClassUID = '1.1.1'
        primitive.AffectedSOPInstanceUID = '1.2.1'
        primitive.Priority = 0x02
        primitive.MoveOriginatorApplicationEntityTitle = b'UNITTEST'
        primitive.MoveOriginatorMessageID = 3
        ds = Dataset()
        ds.PatientID = 'Test1101'
        ds.PatientName = 'Tube^HeNe'
        primitive.DataSet = BytesIO(encode(ds, True, True))
        dimse_msg = C_STORE_RQ()
        dimse_msg.primitive_to_message(primitive)

        # CMD: (1x4, 3x1), DS: (0x1, 2x1)
        p_data_list = dimse_msg.encode_msg(12, 24)

        # Command set decoding
        for pdv in p_data_list[:4]:
            dimse_msg.decode_msg(pdv)  # MCHB 1
        dimse_msg.decode_msg(p_data_list[4])  # MCHB 3 - end of command set
        self.assertEqual(dimse_msg.__class__, C_STORE_RQ)

        # Test decoded command set
        cs = dimse_msg.command_set
        self.assertTrue(cs.CommandGroupLength == 102)
        self.assertTrue(cs.AffectedSOPClassUID == UID('1.1.1'))
        self.assertTrue(cs.AffectedSOPInstanceUID == UID('1.2.1'))
        self.assertTrue(cs.Priority == 2)
        self.assertTrue(cs.CommandDataSetType == 1)
        self.assertTrue(cs.CommandField == 1)
        # Bug in pydicom -> AEs not stripped of trailing spaces
        self.assertTrue(
            cs.MoveOriginatorApplicationEntityTitle == b'UNITTEST        ')
        self.assertTrue(cs.MoveOriginatorMessageID == 3)

        # Test decoded dataset
        dimse_msg.decode_msg(p_data_list[5])  # MCHB 0
        dimse_msg.decode_msg(p_data_list[6])  # MCHB 2
        self.assertTrue(dimse_msg.data_set.getvalue() == c_store_ds[1:])

        # Test returns false
        msg = C_STORE_RSP()
        self.assertFalse(msg.decode_msg(c_store_rsp_cmd))
Ejemplo n.º 6
0
    def test_primitive_to_message(self):
        """Test converting a DIMSE primitive to a DIMSE message."""
        primitive = C_STORE()
        primitive.MessageID = 7
        primitive.AffectedSOPClassUID = '1.1.1'
        primitive.AffectedSOPInstanceUID = '1.2.1'
        primitive.Priority = 0x02
        msg = C_STORE_RQ()
        msg.primitive_to_message(primitive)
        # Test unused command set elements are deleted
        assert not 'MoveOriginatorApplicationEntityTitle' in msg.command_set

        # Test raise error for unknown DIMSE message type
        msg.__class__.__name__ = 'TestClass'
        with pytest.raises(ValueError):
            msg.primitive_to_message(primitive)

        # Reset name to avoid errors in other tests
        msg.__class__.__name__ = 'C_STORE_RQ'
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
 def test_validate_status_unknown(self):
     """Test return unknown status"""
     sop = StorageServiceClass()
     rsp = C_STORE()
     rsp = sop.validate_status(0xD011, rsp)
     assert rsp.Status == 0xD011
Ejemplo n.º 9
0
 def test_validate_status_invalid(self):
     """Test exception raised if invalid status value"""
     sop = StorageServiceClass()
     rsp = C_STORE()
     rsp = sop.validate_status('test', rsp)
     assert rsp.Status == 0xC002
Ejemplo n.º 10
0
 def test_validate_status_int(self):
     """Test that validate_status works correctly with int"""
     sop = StorageServiceClass()
     rsp = C_STORE()
     rsp = sop.validate_status(0x0000, rsp)
     assert rsp.Status == 0x0000
Ejemplo n.º 11
0
    def test_exceptions(self):
        """ Check incorrect types/values for properties raise exceptions """
        primitive = C_STORE()

        # 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'

        # AffectedSOPInstanceUID
        with self.assertRaises(TypeError):
            primitive.AffectedSOPInstanceUID = 45.2

        with self.assertRaises(TypeError):
            primitive.AffectedSOPInstanceUID = 100

        with self.assertRaises(ValueError):
            primitive.AffectedSOPInstanceUID = '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

        # MoveOriginatorApplicationEntityTitle
        with self.assertRaises(TypeError):
            primitive.MoveOriginatorApplicationEntityTitle = 45.2

        with self.assertRaises(TypeError):
            primitive.MoveOriginatorApplicationEntityTitle = 100

        with self.assertRaises(ValueError):
            primitive.MoveOriginatorApplicationEntityTitle = ''

        with self.assertRaises(ValueError):
            primitive.MoveOriginatorApplicationEntityTitle = '    '

        # MoveOriginatorMessageID
        with self.assertRaises(TypeError):
            primitive.MoveOriginatorMessageID = 'halp'

        with self.assertRaises(TypeError):
            primitive.MoveOriginatorMessageID = 1.111

        with self.assertRaises(ValueError):
            primitive.MoveOriginatorMessageID = 65536

        with self.assertRaises(ValueError):
            primitive.MoveOriginatorMessageID = -1

        # DataSet
        with self.assertRaises(TypeError):
            primitive.DataSet = 'halp'

        with self.assertRaises(TypeError):
            primitive.DataSet = 1.111

        with self.assertRaises(TypeError):
            primitive.DataSet = 50

        with self.assertRaises(TypeError):
            primitive.DataSet = [30, 10]

        # Status
        with self.assertRaises(TypeError):
            primitive.Status = 19.4
Ejemplo n.º 12
0
        """Dummy Send method to test DIMSEServiceProvider.Send"""
        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."""