def test_good_init(self): """Test the presentation context class init""" pc = PresentationContext(1) self.assertEqual(pc.ID, 1) self.assertEqual(pc.AbstractSyntax, None) self.assertEqual(pc.TransferSyntax, []) pc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) self.assertEqual(pc.ID, 1) self.assertEqual(pc.AbstractSyntax, '1.1.1') self.assertTrue(isinstance(pc.AbstractSyntax, UID)) self.assertEqual(pc.TransferSyntax, ['1.2.840.10008.1.2']) self.assertTrue(isinstance(pc.TransferSyntax[0], UID)) pc = PresentationContext(1, transfer_syntaxes=['1.2.840.10008.1.2']) self.assertEqual(pc.ID, 1) self.assertEqual(pc.AbstractSyntax, None) self.assertEqual(pc.TransferSyntax, ['1.2.840.10008.1.2']) self.assertTrue(isinstance(pc.TransferSyntax[0], UID)) pc = PresentationContext(1, abstract_syntax='1.1.1') self.assertEqual(pc.ID, 1) self.assertEqual(pc.AbstractSyntax, '1.1.1') self.assertTrue(isinstance(pc.AbstractSyntax, UID)) self.assertEqual(pc.TransferSyntax, [])
def test_add_private_transfer_syntax(self): """Test adding private transfer syntaxes""" pc = PresentationContext(1) pc.add_transfer_syntax('2.16.840.1.113709.1.2.2') self.assertTrue('2.16.840.1.113709.1.2.2' in pc._transfer_syntax) pc.TransferSyntax = ['2.16.840.1.113709.1.2.1'] self.assertTrue('2.16.840.1.113709.1.2.1' in pc._transfer_syntax)
def test_status(self): """Test presentation context status""" pc = PresentationContext(1) statuses = [None, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05] results = ['Pending', 'Accepted', 'User Rejected', 'Provider Rejected', 'Abstract Syntax Not Supported', 'Transfer Syntax(es) Not Supported', 'Unknown'] for status, result in zip(statuses, results): pc.Result = status self.assertEqual(pc.status, result)
def test_bad_init(self): """Test breaking the presentation context manager init""" req = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) acc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) pcm = PresentationContextManager() self.assertEqual(pcm.requestor_contexts, []) self.assertEqual(pcm.acceptor_contexts, []) with self.assertRaises(TypeError): pcm = PresentationContextManager(req, [acc])
def test_good_init(self): """Test the presentation context manager init""" req = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) acc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) pcm = PresentationContextManager() self.assertEqual(pcm.requestor_contexts, []) self.assertEqual(pcm.acceptor_contexts, []) pcm = PresentationContextManager([req], [acc]) self.assertEqual(pcm.requestor_contexts, [req]) self.assertEqual(pcm.acceptor_contexts, [acc])
def test_string_output(self): """Test string output""" pc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) pc.SCP = True pc.SCU = False pc.Result = 0x0002 self.assertTrue('1.1.1' in pc.__str__()) self.assertTrue('Implicit' in pc.__str__()) self.assertTrue('Provider Rejected' in pc.__str__())
def test_bad_init(self): """Test breaking the presentation context manager init""" req = PresentationContext() req.context_id = 1 req.abstract_syntax = '1.1.1' req.transfer_syntax = ['1.2.840.10008.1.2'] acc = PresentationContext() acc.context_id = 1 acc.abstract_syntax = '1.1.1' acc.transfer_syntax = ['1.2.840.10008.1.2'] pcm = PresentationContextManager() self.assertEqual(pcm.requestor_contexts, []) self.assertEqual(pcm.acceptor_contexts, []) with self.assertRaises(TypeError): pcm = PresentationContextManager(req, [acc])
def test_good_init(self): """Test the presentation context manager init""" req = PresentationContext() req.context_id = 1 req.abstract_syntax = '1.1.1' req.transfer_syntax = ['1.2.840.10008.1.2'] acc = PresentationContext() acc.context_id = 1 acc.abstract_syntax = '1.1.1' acc.transfer_syntax = ['1.2.840.10008.1.2'] pcm = PresentationContextManager() self.assertEqual(pcm.requestor_contexts, []) self.assertEqual(pcm.acceptor_contexts, []) pcm = PresentationContextManager([req], [acc]) self.assertEqual(pcm.requestor_contexts, [req]) self.assertEqual(pcm.acceptor_contexts, [acc])
def test_property_setters(self): """Test the property setters""" # Requestor req = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) acc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2.1']) # No matching abstract syntax pcm = PresentationContextManager([req]) acc = PresentationContext(1, '1.1.2', ['1.2.840.10008.1.2']) pcm.acceptor_contexts = [acc] self.assertEqual(pcm.accepted, []) acc.Result = 0x03 #print(acc, pcm.rejected) self.assertEqual(pcm.rejected, [acc]) pcm = PresentationContextManager() with self.assertRaises(RuntimeError): pcm.acceptor_contexts = [acc] pcm.requestor_contexts = [req] self.assertEqual(pcm.requestor_contexts, [req]) with self.assertRaises(TypeError): pcm.requestor_contexts = req # Acceptor # No matching transfer syntax pcm.requestor_contexts = [req] pcm.acceptor_contexts = [acc] self.assertEqual(pcm.accepted, []) acc.Result = 0x04 self.assertEqual(pcm.rejected, [acc]) # Accepted pcm = PresentationContextManager() pcm.requestor_contexts = [req] acc = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) pcm.acceptor_contexts = [acc] self.assertEqual(pcm.rejected, []) acc.Result = 0x01 self.assertEqual(pcm.accepted, [acc])
def test_equality(self): """Test presentation context equality""" pc_a = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) pc_b = PresentationContext(1, '1.1.1', ['1.2.840.10008.1.2']) self.assertTrue(pc_a == pc_a) self.assertTrue(pc_a == pc_b) self.assertFalse(pc_a != pc_b) self.assertFalse(pc_a != pc_a) pc_a.SCP = True self.assertFalse(pc_a == pc_b) pc_b.SCP = True self.assertTrue(pc_a == pc_b) pc_a.SCU = True self.assertFalse(pc_a == pc_b) pc_b.SCU = True self.assertTrue(pc_a == pc_b) self.assertFalse('a' == pc_b)
def test_bad_init(self): """Test the presentation context class init""" with self.assertRaises(ValueError): PresentationContext(0) with self.assertRaises(ValueError): PresentationContext(256) with self.assertRaises(TypeError): PresentationContext(1, transfer_syntaxes='1.1.1') with self.assertRaises(ValueError): PresentationContext(1, transfer_syntaxes=[1234]) with self.assertRaises(TypeError): PresentationContext(1, abstract_syntax=['1.1.1.']) with self.assertRaises(TypeError): PresentationContext(1, abstract_syntax=1234)
def test_transfer_syntax(self): """Test transfer syntax setter""" pc = PresentationContext(1) pc.TransferSyntax = ['1.2.840.10008.1.2'] self.assertEqual(pc.TransferSyntax[0], UID('1.2.840.10008.1.2')) self.assertTrue(isinstance(pc.TransferSyntax[0], UID)) pc.TransferSyntax = [b'1.2.840.10008.1.2.1'] self.assertEqual(pc.TransferSyntax[0], UID('1.2.840.10008.1.2.1')) self.assertTrue(isinstance(pc.TransferSyntax[0], UID)) pc.TransferSyntax = [UID('1.2.840.10008.1.2.2')] self.assertEqual(pc.TransferSyntax[0], UID('1.2.840.10008.1.2.2')) self.assertTrue(isinstance(pc.TransferSyntax[0], UID)) with self.assertRaises(TypeError): pc.TransferSyntax = UID('1.4.1') pc.TransferSyntax = ['1.4.1.', '1.2.840.10008.1.2'] self.assertEqual(pc.TransferSyntax[0], UID('1.2.840.10008.1.2'))
def test_abstract_syntax(self): """Test abstract syntax setter""" pc = PresentationContext(1) pc.AbstractSyntax = '1.1.1' self.assertEqual(pc.AbstractSyntax, UID('1.1.1')) self.assertTrue(isinstance(pc.AbstractSyntax, UID)) pc.AbstractSyntax = b'1.2.1' self.assertEqual(pc.AbstractSyntax, UID('1.2.1')) self.assertTrue(isinstance(pc.AbstractSyntax, UID)) pc.AbstractSyntax = UID('1.3.1') self.assertEqual(pc.AbstractSyntax, UID('1.3.1')) self.assertTrue(isinstance(pc.AbstractSyntax, UID)) pc.AbstractSyntax = UID('1.4.1.') self.assertEqual(pc.AbstractSyntax, UID('1.3.1')) self.assertTrue(isinstance(pc.AbstractSyntax, UID))
def _build_presentation_contexts(self): """Build the presentation context list. Build presentation context list to be: * sent to remote AE when requesting association (presentation_contexts_scu) * used to decide whether to accept or reject when remote AE requests association (presentation_contexts_scp) See PS3.8 Sections 7.1.1.13 and 9.3.2.2 """ self.presentation_contexts_scu = [] self.presentation_contexts_scp = [] for [pc_output, sop_input] in \ [[self.presentation_contexts_scu, self.scu_supported_sop], [self.presentation_contexts_scp, self.scp_supported_sop]]: for ii, sop_class in enumerate(sop_input): # Must be an odd integer between 1 and 255 presentation_context_id = ii * 2 + 1 abstract_syntax = sop_class # Add the Presentation Context Definition Item # If we have too many Items, warn and skip the rest if presentation_context_id < 255: pc_item = PresentationContext(presentation_context_id, abstract_syntax, self.transfer_syntaxes[:]) pc_output.append(pc_item) else: LOGGER.warning( "More than 126 supported SOP Classes have " "been supplied to the Application Entity, " "but the Presentation Context Definition ID " "can only be an odd integer between 1 and " "255. The remaining SOP Classes will not be " "included") break
def test_conversion(self): """ Check conversion to a PDU produces the correct output """ assoc = A_ASSOCIATE() assoc.application_context_name = "1.2.840.10008.3.1.1.1" assoc.calling_ae_title = 'ECHOSCU' assoc.called_ae_title = 'ANY-SCP' assoc.maximum_length_received = 16382 assoc.implementation_class_uid = '1.2.826.0.1.3680043.9.3811.0.9.0' imp_ver_name = ImplementationVersionNameNotification() imp_ver_name.implementation_version_name = 'PYNETDICOM_090' assoc.user_information.append(imp_ver_name) assoc.presentation_context_definition_list = \ [PresentationContext(1, '1.2.840.10008.1.1', ['1.2.840.10008.1.2'])] pdu = A_ASSOCIATE_RQ() pdu.FromParams(assoc) data = pdu.encode() ref = b"\x01\x00\x00\x00\x00\xd1\x00\x01\x00\x00\x41\x4e\x59\x2d\x53\x43" \ b"\x50\x20\x20\x20\x20\x20\x20\x20\x20\x20\x45\x43\x48\x4f\x53\x43" \ b"\x55\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x00\x00" \ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x15\x31\x2e" \ b"\x32\x2e\x38\x34\x30\x2e\x31\x30\x30\x30\x38\x2e\x33\x2e\x31\x2e" \ b"\x31\x2e\x31\x20\x00\x00\x2e\x01\x00\x00\x00\x30\x00\x00\x11\x31" \ b"\x2e\x32\x2e\x38\x34\x30\x2e\x31\x30\x30\x30\x38\x2e\x31\x2e\x31" \ b"\x40\x00\x00\x11\x31\x2e\x32\x2e\x38\x34\x30\x2e\x31\x30\x30\x30" \ b"\x38\x2e\x31\x2e\x32\x50\x00\x00\x3e\x51\x00\x00\x04\x00\x00\x3f" \ b"\xfe\x52\x00\x00\x20\x31\x2e\x32\x2e\x38\x32\x36\x2e\x30\x2e\x31" \ b"\x2e\x33\x36\x38\x30\x30\x34\x33\x2e\x39\x2e\x33\x38\x31\x31\x2e" \ b"\x30\x2e\x39\x2e\x30\x55\x00\x00\x0e\x50\x59\x4e\x45\x54\x44\x49" \ b"\x43\x4f\x4d\x5f\x30\x39\x30" self.assertTrue(data == ref)
def test_assignment(self): """ Check assignment works correctly """ assoc = A_ASSOCIATE() def test_mode(): assoc.mode = "test value" self.assertRaises(AttributeError, test_mode) def test_preq(): assoc.presentation_requirements = "test value2" self.assertRaises(AttributeError, test_preq) def test_sreq(): assoc.session_requirements = "test value3" self.assertRaises(AttributeError, test_sreq) assoc.application_context_name = "1.2.840.10008.3.1.1.1" self.assertTrue(assoc.application_context_name == UID('1.2.840.10008.3.1.1.1')) assoc.application_context_name = b"1.2.840.10008.3.1.1.1" self.assertTrue(assoc.application_context_name == UID('1.2.840.10008.3.1.1.1')) assoc.application_context_name = UID("1.2.840.10008.3.1.1.1") self.assertTrue(assoc.application_context_name == UID('1.2.840.10008.3.1.1.1')) assoc.calling_ae_title = 'ABCD1234ABCD12345' self.assertTrue(assoc.calling_ae_title == b'ABCD1234ABCD1234') assoc.called_ae_title = 'ABCD1234ABCD12345' self.assertTrue(assoc.called_ae_title == b'ABCD1234ABCD1234') self.assertTrue(assoc.responding_ae_title == b'ABCD1234ABCD1234') max_length = MaximumLengthNegotiation() max_length.maximum_length_received = 31222 assoc.user_information.append(max_length) self.assertTrue(assoc.user_information[0].maximum_length_received == 31222) assoc.user_information = ['a', max_length] self.assertEqual(assoc.user_information, [max_length]) assoc.result = 0 self.assertTrue(assoc.result == 0) assoc.result = 1 self.assertTrue(assoc.result == 1) assoc.result = 2 self.assertTrue(assoc.result == 2) assoc.result_source = 1 self.assertTrue(assoc.result_source == 1) assoc.result_source = 2 self.assertTrue(assoc.result_source == 2) assoc.result_source = 3 self.assertTrue(assoc.result_source == 3) assoc.diagnostic = 1 self.assertTrue(assoc.diagnostic == 1) assoc.diagnostic = 2 self.assertTrue(assoc.diagnostic == 2) assoc.diagnostic = 3 self.assertTrue(assoc.diagnostic == 3) assoc.diagnostic = 7 self.assertTrue(assoc.diagnostic == 7) assoc.calling_presentation_address = ('10.40.94.43', 105) self.assertTrue(assoc.calling_presentation_address == ('10.40.94.43', 105)) assoc.called_presentation_address = ('10.40.94.44', 106) self.assertTrue(assoc.called_presentation_address == ('10.40.94.44', 106)) assoc.presentation_context_definition_list = [PresentationContext(1)] self.assertTrue(assoc.presentation_context_definition_list == [PresentationContext(1)]) assoc.presentation_context_definition_list = ['a', PresentationContext(1)] self.assertTrue(assoc.presentation_context_definition_list == [PresentationContext(1)]) assoc.presentation_context_definition_results_list = [PresentationContext(1)] self.assertTrue(assoc.presentation_context_definition_results_list == [PresentationContext(1)]) assoc.presentation_context_definition_results_list = ['a', PresentationContext(1)] self.assertTrue(assoc.presentation_context_definition_results_list == [PresentationContext(1)]) assoc = A_ASSOCIATE() # No maximum_length_received set self.assertEqual(assoc.maximum_length_received, None) # No MaximumLengthNegotiation present assoc.maximum_length_received = 31223 self.assertTrue(assoc.user_information[0].maximum_length_received == 31223) self.assertTrue(assoc.maximum_length_received == 31223) # MaximumLengthNegotiation already present assoc.maximum_length_received = 31224 self.assertTrue(assoc.maximum_length_received == 31224) # No ImplementationClassUIDNegotiation present assoc.implementation_class_uid = '1.1.2.3.4' self.assertTrue(assoc.user_information[1].implementation_class_uid == UID('1.1.2.3.4')) self.assertTrue(assoc.implementation_class_uid == UID('1.1.2.3.4')) # ImplementationClassUIDNegotiation already present assoc.implementation_class_uid = '1.1.2.3.4' self.assertTrue(assoc.implementation_class_uid == UID('1.1.2.3.4'))
def test_add_transfer_syntax(self): """Test adding transfer syntaxes""" pc = PresentationContext(1) pc.add_transfer_syntax('1.2.840.10008.1.2') pc.add_transfer_syntax(b'1.2.840.10008.1.2.1') pc.add_transfer_syntax(UID('1.2.840.10008.1.2.2')) pc.add_transfer_syntax(UID('')) with self.assertRaises(TypeError): pc.add_transfer_syntax([]) with self.assertRaises(ValueError): pc.add_transfer_syntax('1.2.3.') with self.assertRaises(ValueError): pc.add_transfer_syntax('1.2.840.10008.1.1')