def print_nice_bytes(bytestream):
    """Nice output for bytestream."""
    str_list = pretty_bytes(bytestream,
                            prefix="b'\\x",
                            delimiter='\\x',
                            items_per_line=10)
    for string in str_list:
        print(string)
Esempio n. 2
0
 def test_bytesio(self):
     """Test wrap list using bytesio"""
     bytestream = BytesIO()
     bytestream.write(a_associate_rq)
     result = pretty_bytes(bytestream,
                           prefix='',
                           delimiter='',
                           items_per_line=10)
     self.assertTrue(isinstance(result[0], str))
Esempio n. 3
0
    def test_parameters(self):
        """Test parameters are correct."""
        # Default
        bytestream = a_associate_rq
        result = pretty_bytes(bytestream)
        self.assertEqual(len(result), 14)
        self.assertTrue(isinstance(result[0], str))

        # prefix
        result = pretty_bytes(bytestream, prefix='\\x')
        for line in result:
            self.assertTrue(line[:2] == '\\x')

        # delimiter
        result = pretty_bytes(bytestream, prefix='', delimiter=',')
        for line in result:
            self.assertTrue(line[2] == ',')

        # items_per_line
        result = pretty_bytes(bytestream,
                              prefix='',
                              delimiter='',
                              items_per_line=10)
        self.assertEqual(len(result[0]), 20)

        # max_size
        result = pretty_bytes(bytestream,
                              prefix='',
                              delimiter='',
                              items_per_line=10,
                              max_size=100)
        self.assertEqual(len(result), 11)  # 10 plus the cutoff line
        result = pretty_bytes(bytestream, max_size=None)

        # suffix
        result = pretty_bytes(bytestream, suffix='xxx')
        for line in result:
            self.assertTrue(line[-3:] == 'xxx')
Esempio n. 4
0
    def test_parameters(self):
        """Test parameters are correct."""
        # Default
        bytestream = a_associate_rq
        result = pretty_bytes(bytestream)
        assert len(result) == 14
        assert isinstance(result[0], str)

        # prefix
        result = pretty_bytes(bytestream, prefix='\\x')
        for line in result:
            assert line[:2] == '\\x'

        # delimiter
        result = pretty_bytes(bytestream, prefix='', delimiter=',')
        for line in result:
            assert line[2] == ','

        # items_per_line
        result = pretty_bytes(bytestream,
                              prefix='',
                              delimiter='',
                              items_per_line=10)
        assert len(result[0]) == 20

        # max_size
        result = pretty_bytes(bytestream,
                              prefix='',
                              delimiter='',
                              items_per_line=10,
                              max_size=100)
        assert len(result) == 11  # 10 plus the cutoff line
        result = pretty_bytes(bytestream, max_size=None)

        # suffix
        result = pretty_bytes(bytestream, suffix='xxx')
        for line in result:
            assert line[-3:] == 'xxx'
Esempio n. 5
0
    def debug_receive_associate_rq(a_associate_rq):
        """
        Placeholder for a function callback. Function will be called
        immediately after receiving and decoding an A-ASSOCIATE-RQ

        Parameters
        ----------
        a_associate_rq : pynetdicom3.pdu.A_ASSOCIATE_RQ
            The A-ASSOCIATE-RQ PDU instance
        """
        LOGGER.info("Association Received")

        # Shorthand
        pdu = a_associate_rq

        app_context = pdu.application_context_name.title()
        pres_contexts = pdu.presentation_context
        user_info = pdu.user_information

        #responding_ae = 'resp. AP Title'
        their_class_uid = 'unknown'
        their_version = 'unknown'

        if user_info.implementation_class_uid:
            their_class_uid = user_info.implementation_class_uid
        if user_info.implementation_version_name:
            their_version = user_info.implementation_version_name

        s = ['Request Parameters:']
        s.append('====================== BEGIN A-ASSOCIATE-RQ ================'
                 '=====')
        s.append(
            'Their Implementation Class UID:    {0!s}'.format(their_class_uid))
        s.append('Their Implementation Version Name: {0!s}'.format(
            their_version.decode('ascii')))
        s.append('Application Context Name:    {0!s}'.format(app_context))
        s.append('Calling Application Name:    {0!s}'.format(
            pdu.calling_ae_title.decode('ascii')))
        s.append('Called Application Name:     {0!s}'.format(
            pdu.called_ae_title.decode('ascii')))
        s.append('Their Max PDU Receive Size:  {0!s}'.format(
            user_info.maximum_length))

        ## Presentation Contexts
        s.append('Presentation Contexts:')
        for item in pres_contexts:
            s.append('  Context ID:        {0!s} (Proposed)'.format(
                item.context_id))
            s.append('    Abstract Syntax: ={0!s}'.format(
                item.abstract_syntax.name))

            # Add SCP/SCU Role Selection Negotiation
            # Roles are: SCU, SCP/SCU, SCP, Default
            if pdu.user_information.role_selection:
                try:
                    role = pdu.user_information.role_selection[
                        item.abstract_syntax]
                    roles = []
                    if role.scp_role:
                        roles.append('SCP')
                    if role.scu_role:
                        roles.append('SCU')

                    scp_scu_role = '/'.join(roles)
                except KeyError:
                    scp_scu_role = 'Default'
            else:
                scp_scu_role = 'Default'

            s.append('    Proposed SCP/SCU Role: {0!s}'.format(scp_scu_role))
            s.append('    Proposed Transfer Syntax(es):')
            for ts in item.transfer_syntax:
                s.append('      ={0!s}'.format(ts.name))

        ## Extended Negotiation
        if pdu.user_information.ext_neg is not None:
            s.append('Requested Extended Negotiation:')

            for item in pdu.user_information.ext_neg:
                s.append('  Abstract Syntax: ={0!s}'.format(item.uid))
                #s.append('    Application Information, length: %d bytes'
                #                                       %len(item.app_info))

                app_info = pretty_bytes(item.app_info)
                app_info[0] = '[' + app_info[0][1:]
                app_info[-1] = app_info[-1] + ' ]'
                for line in app_info:
                    s.append('    {0!s}'.format(line))
        else:
            s.append('Requested Extended Negotiation: None')

        ## Common Extended Negotiation
        if pdu.user_information.common_ext_neg is not None:
            s.append('Requested Common Extended Negotiation:')

            for item in pdu.user_information.common_ext_neg:

                s.append('  Abstract Syntax: ={0!s}'.format(
                    item.sop_class_uid))
                s.append('  Service Class:   ={0!s}'.format(
                    item.service_class_uid))

                if item.related_general_sop_class_identification != []:
                    s.append('  Related General SOP Class(es):')
                    for sub_field in \
                                item.related_general_sop_class_identification:
                        s.append('    ={0!s}'.format(sub_field))
                else:
                    s.append('  Related General SOP Classes: None')
        else:
            s.append('Requested Common Extended Negotiation: None')

        ## Asynchronous Operations Window Negotiation
        #async_neg = 'None'
        if pdu.user_information.async_ops_window is not None:
            s.append('Requested Asynchronous Operations Window Negotiation:')
            # FIXME
        else:
            s.append('Requested Asynchronous Operations Window ' \
                     'Negotiation: None')

        ## User Identity
        if user_info.user_identity is not None:
            usid = user_info.user_identity
            s.append('Requested User Identity Negotiation:')
            s.append('  Authentication Mode: {0:d} - {1!s}'.format(
                usid.id_type, usid.id_type_str))
            if usid.id_type == 1:
                s.append('  Username: [{0!s}]'.format(
                    usid.primary.decode('utf-8')))
            elif usid.id_type == 2:
                s.append('  Username: [{0!s}]'.format(
                    usid.primary.decode('utf-8')))
                s.append('  Password: [{0!s}]'.format(
                    usid.secondary.decode('utf-8')))
            elif usid.id_type == 3:
                s.append('  Kerberos Service Ticket (not dumped) length: '
                         '{0:d}'.format(len(usid.primary)))
            elif usid.id_type == 4:
                s.append('  SAML Assertion (not dumped) length: '
                         '{0:d}'.format(len(usid.primary)))

            if usid.response_requested:
                s.append('  Positive Response requested: Yes')
            else:
                s.append('  Positive Response requested: None')
        else:
            s.append('Requested User Identity Negotiation: None')

        s.append(
            '======================= END A-ASSOCIATE-RQ =================='
            '====')

        for line in s:
            LOGGER.debug(line)
Esempio n. 6
0
    def debug_send_associate_rq(a_associate_rq):
        """
        Placeholder for a function callback. Function will be called
        immediately prior to encoding and sending an A-ASSOCIATE-RQ to
        a peer AE

        The default implementation is used for logging debugging information

        Parameters
        ----------
        a_associate_rq : pynetdicom3.pdu.A_ASSOCIATE_RQ
            The A-ASSOCIATE-RQ PDU instance to be encoded and sent
        """
        # Shorthand
        pdu = a_associate_rq

        app_context = pdu.application_context_name.title()
        pres_contexts = pdu.presentation_context
        user_info = pdu.user_information

        s = ['Request Parameters:']
        s.append('====================== BEGIN A-ASSOCIATE-RQ ================'
                 '=====')

        s.append('Our Implementation Class UID:      '
                 '{0!s}'.format(user_info.implementation_class_uid))
        s.append('Our Implementation Version Name:   '
                 '{0!s}'.format(user_info.implementation_version_name))
        s.append('Application Context Name:    {0!s}'.format(app_context))
        s.append('Calling Application Name:    '
                 '{0!s}'.format(pdu.calling_ae_title.decode('utf-8')))
        s.append('Called Application Name:     '
                 '{0!s}'.format(pdu.called_ae_title.decode('utf-8')))
        s.append('Our Max PDU Receive Size:    '
                 '{0!s}'.format(user_info.maximum_length))

        ## Presentation Contexts
        if len(pres_contexts) == 1:
            s.append('Presentation Context:')
        else:
            s.append('Presentation Contexts:')

        for context in pres_contexts:
            s.append('  Context ID:        {0!s} '
                     '(Proposed)'.format((context.ID)))
            s.append('    Abstract Syntax: ='
                     '{0!s}'.format(context.abstract_syntax))

            if 'SCU' in context.__dict__.keys():
                scp_scu_role = '{0!s}/{1!s}'.format(context.SCP, context.SCU)
            else:
                scp_scu_role = 'Default'
            s.append('    Proposed SCP/SCU Role: {0!s}'.format(scp_scu_role))

            # Transfer Syntaxes
            if len(context.transfer_syntax) == 1:
                s.append('    Proposed Transfer Syntax:')
            else:
                s.append('    Proposed Transfer Syntaxes:')

            for ts in context.transfer_syntax:
                s.append('      ={0!s}'.format(ts.name))

        ## Extended Negotiation
        if pdu.user_information.ext_neg is not None:
            s.append('Requested Extended Negotiation:')

            for item in pdu.user_information.ext_neg:
                s.append('  Abstract Syntax: ={0!s}'.format(item.UID))
                #s.append('    Application Information, length: %d bytes'
                #                                       %len(item.app_info))

                app_info = pretty_bytes(item.app_info)
                app_info[0] = '[' + app_info[0][1:]
                app_info[-1] = app_info[-1] + ' ]'
                for line in app_info:
                    s.append('    {0!s}'.format(line))
        else:
            s.append('Requested Extended Negotiation: None')

        ## Common Extended Negotiation
        if pdu.user_information.common_ext_neg is not None:
            s.append('Requested Common Extended Negotiation:')

            for item in pdu.user_information.common_ext_neg:

                s.append('  Abstract Syntax: ={0!s}'.format(item.sop_class_uid))
                s.append('  Service Class:   ='
                         '{0!s}'.format(item.service_class_uid))

                if item.related_general_sop_class_identification != []:
                    s.append('  Related General SOP Class(es):')
                    for sub_field in \
                            item.related_general_sop_class_identification:
                        s.append('    ={0!s}'.format(sub_field))
                else:
                    s.append('  Related General SOP Classes: None')
        else:
            s.append('Requested Common Extended Negotiation: None')

        ## User Identity
        if user_info.user_identity is not None:
            usid = user_info.user_identity
            s.append('Requested User Identity Negotiation:')
            s.append('  Authentication Mode: {0:d} - '
                     '{1!s}'.format(usid.id_type, usid.id_type_str))
            if usid.id_type == 1:
                s.append('  Username: '******'[{0!s}]'.format(usid.primary.decode('utf-8')))
            elif usid.id_type == 2:
                s.append('  Username: '******'[{0!s}]'.format(usid.primary.decode('utf-8')))
                s.append('  Password: '******'[{0!s}]'.format(usid.secondary.decode('utf-8')))
            elif usid.id_type == 3:
                s.append('  Kerberos Service Ticket (not dumped) length: '
                         '{0:d}'.format(len(usid.primary)))
            elif usid.id_type == 4:
                s.append('  SAML Assertion (not dumped) length: '
                         '{0:d}'.format(len(usid.primary)))

            if usid.response_requested:
                s.append('  Positive Response requested: Yes')
            else:
                s.append('  Positive Response requested: None')
        else:
            s.append('Requested User Identity Negotiation: None')

        s.append('======================= END A-ASSOCIATE-RQ =================='
                 '====')

        for line in s:
            LOGGER.debug(line)