def test_stream_encode(self): """ Check encoding an assoc_ac produces the correct output """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) s = pdu.Encode() self.assertEqual(s, a_associate_ac)
def test_new_encode(self): """ Check encoding using new generic method """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) s = pdu.encode() self.assertEqual(s, a_associate_ac)
def test_decode_properties(self): """ Check decoding the assoc_ac stream produces the correct properties """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) # Check AE titles self.assertEqual(pdu.reserved_aec.decode('utf-8'), 'ECHOSCU ') self.assertEqual(pdu.reserved_aet.decode('utf-8'), 'ANY-SCP ') self.assertTrue(isinstance(pdu.reserved_aec, bytes)) self.assertTrue(isinstance(pdu.reserved_aet, bytes)) # Check application_context_name property app_name = pdu.application_context_name self.assertTrue(isinstance(app_name, UID)) self.assertEqual(app_name, '1.2.840.10008.3.1.1.1') # Check presentation_context property contexts = pdu.presentation_context self.assertTrue(isinstance(contexts, list)) for context in contexts: self.assertTrue(isinstance(context, PresentationContextItemAC)) # Check user_information property user_info = pdu.user_information self.assertTrue(isinstance(user_info, UserInformationItem))
def test_decode_properties(self): """Check decoding produces the correct property values.""" pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) # Check AE titles assert pdu._reserved_aec == b'ECHOSCU ' assert pdu._reserved_aet == b'ANY-SCP ' assert pdu.calling_ae_title == b'ECHOSCU ' assert pdu.called_ae_title == b'ANY-SCP ' # Check application_context_name property assert isinstance(pdu.application_context_name, UID) assert pdu.application_context_name == '1.2.840.10008.3.1.1.1' # Check presentation_context property contexts = pdu.presentation_context assert isinstance(contexts, list) assert len(contexts) == 1 for context in contexts: assert isinstance(context, PresentationContextItemAC) # Check user_information property user_info = pdu.user_information assert isinstance(user_info, UserInformationItem)
def test_generic_encode(self): """ Check using the new pdu.encode produces the correct output """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) s = pdu.Encode() t = pdu.encode() self.assertEqual(s, t)
def test_decode(self): """ Check decoding an assoc_ac produces the correct transfer syntax """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) syntax = pdu.presentation_context[0].transfer_syntax assert isinstance(syntax, UID) assert syntax == UID('1.2.840.10008.1.2')
def test_to_primitive(self): """ Check converting PDU to primitive """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) primitive = pdu.to_primitive() assert primitive.application_context_name == UID( '1.2.840.10008.3.1.1.1') assert primitive.calling_ae_title == b'ECHOSCU ' assert primitive.called_ae_title == b'ANY-SCP ' # Test User Information for item in primitive.user_information: # Maximum PDU Length (required) if isinstance(item, MaximumLengthNegotiation): assert item.maximum_length_received == 16384 assert isinstance(item.maximum_length_received, int) # Implementation Class UID (required) elif isinstance(item, ImplementationClassUIDNotification): assert item.implementation_class_uid == UID( '1.2.276.0.7230010.3.0.3.6.0') assert isinstance(item.implementation_class_uid, UID) # Implementation Version Name (optional) elif isinstance(item, ImplementationVersionNameNotification): assert item.implementation_version_name == b'OFFIS_DCMTK_360' assert isinstance(item.implementation_version_name, bytes) # Test Presentation Contexts for context in primitive.presentation_context_definition_list: assert context.context_id == 1 assert context.transfer_syntax[0] == UID('1.2.840.10008.1.2') assert isinstance(primitive.application_context_name, UID) assert isinstance(primitive.calling_ae_title, bytes) assert isinstance(primitive.called_ae_title, bytes) assert isinstance(primitive.user_information, list) assert primitive.result == 0 assert len(primitive.presentation_context_definition_results_list) == 1 # Not used by A-ASSOCIATE-AC or fixed value assert primitive.mode == "normal" assert primitive.responding_ae_title == primitive.called_ae_title assert primitive.result_source is None assert primitive.diagnostic is None assert primitive.calling_presentation_address is None assert primitive.called_presentation_address is None assert primitive.responding_presentation_address == ( primitive.called_presentation_address) assert primitive.presentation_context_definition_list == [] assert primitive.presentation_requirements == "Presentation Kernel" assert primitive.session_requirements == ""
def test_decode_value_type(self): """ Check decoding an assoc_ac produces the correct transfer syntax """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) context = pdu.presentation_context[0] syntax = context.transfer_syntax self.assertTrue(isinstance(syntax, UID)) self.assertTrue(syntax.is_transfer_syntax) self.assertEqual(syntax, UID('1.2.840.10008.1.2'))
def test_decode(self): """Check decoding produces the correct application context.""" pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) app_context = pdu.variable_items[0] assert app_context.item_type == 0x10 assert app_context.item_length == 21 assert app_context.application_context_name == '1.2.840.10008.3.1.1.1' assert isinstance(app_context.application_context_name, UID)
def test_property_setters(self): """Test the property setters""" # presentation_context pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) role_selection = SCP_SCU_RoleSelectionSubItem() role_selection.sop_class_uid = '1.2.840.10008.1.1' role_selection.scu_role = 1 role_selection.scp_role = 1 pdu.user_information.user_data.append(role_selection) context = pdu.presentation_context[0] self.assertTrue(context.transfer_syntax == '1.2.840.10008.1.2')
def test_from_primitive(self): """ Check converting PDU to primitive """ orig = A_ASSOCIATE_AC() orig.decode(a_associate_ac) primitive = orig.to_primitive() new = A_ASSOCIATE_AC() new.from_primitive(primitive) assert new == orig
def test_stream_decode_values_types(self): """ Check decoding an assoc_ac produces the correct presentation context """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) # Check PresentationContextItemRQ attributes presentation_context = pdu.variable_items[1] self.assertEqual(presentation_context.item_type, 0x21) self.assertEqual(presentation_context.presentation_context_id, 0x0001) self.assertEqual(presentation_context.item_length, 25) self.assertEqual(presentation_context.result_reason, 0) self.assertTrue(isinstance(presentation_context.item_type, int)) self.assertTrue( isinstance(presentation_context.presentation_context_id, int)) self.assertTrue(isinstance(presentation_context.item_length, int))
def AE_7(dul): """Association establishment action AE-7. On receiving association request acceptance, issue A-ASSOCIATE-AC State-event triggers: Sta3 + Evt7 .. [1] DICOM Standard 2015b, PS3.8, Table 9-7, "Associate Establishment Related Actions" Parameters ---------- dul : pynetdicom3.dul.DULServiceProvider The DICOM Upper Layer Service instance for the local AE Returns ------- str Sta6, the next state of the state machine """ # Send A-ASSOCIATE-AC PDU dul.pdu = A_ASSOCIATE_AC() dul.pdu.FromParams(dul.primitive) # Callback dul.assoc.acse.debug_send_associate_ac(dul.pdu) bytestream = dul.pdu.Encode() dul.scu_socket.send(bytestream) return 'Sta6'
def test_decode(self): """Check decoding produces the correct presentation context.""" pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) # Check PresentationContextItemRQ attributes context = pdu.variable_items[1] assert context.item_type == 0x21 assert context.item_length == 25 assert len(context) == 29 assert context.presentation_context_id == 0x01 assert context.result_reason == 0 assert len(context.transfer_syntax_sub_item) == 1 syntax = context.transfer_syntax_sub_item[0] assert isinstance(syntax, TransferSyntaxSubItem) assert isinstance(syntax.transfer_syntax_name, UID) assert syntax.transfer_syntax_name == UID('1.2.840.10008.1.2')
def test_decode(self): """ Check decoding the assoc_ac stream produces the correct objects """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) assert pdu.pdu_type == 0x02 assert pdu.pdu_length == 184 assert pdu.protocol_version == 0x0001 assert isinstance(pdu.pdu_type, int) assert isinstance(pdu.pdu_length, int) assert isinstance(pdu.protocol_version, int) # Check VariableItems # The actual items will be tested separately assert isinstance(pdu.variable_items[0], ApplicationContextItem) assert isinstance(pdu.variable_items[1], PresentationContextItemAC) assert isinstance(pdu.variable_items[2], UserInformationItem)
def test_stream_decode_values_types(self): """ Check decoding an assoc_ac produces the correct application context """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) app_context = pdu.variable_items[0] self.assertEqual(app_context.item_type, 0x10) self.assertEqual(app_context.item_length, 21) self.assertEqual(app_context.application_context_name, '1.2.840.10008.3.1.1.1') self.assertTrue(isinstance(app_context.item_type, int)) self.assertTrue(isinstance(app_context.item_length, int)) self.assertTrue(isinstance(app_context.application_context_name, UID)) self.assertEqual(app_context.application_context_name, '1.2.840.10008.3.1.1.1') self.assertTrue(isinstance(app_context.application_context_name, UID))
def test_update_data(self): """ Check that updating the PDU data works correctly """ original = A_ASSOCIATE_AC() original.Decode(a_associate_ac) original.user_information.user_data = [ original.user_information.user_data[1] ] original.get_length() primitive = original.ToParams() new = A_ASSOCIATE_AC() new.FromParams(primitive) self.assertEqual(original, new)
def test_stream_decode_values_types(self): """ Check decoding the assoc_ac stream produces the correct objects """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) self.assertEqual(pdu.pdu_type, 0x02) self.assertEqual(pdu.pdu_length, 184) self.assertEqual(pdu.protocol_version, 0x0001) self.assertTrue(isinstance(pdu.pdu_type, int)) self.assertTrue(isinstance(pdu.pdu_length, int)) self.assertTrue(isinstance(pdu.protocol_version, int)) # Check VariableItems # The actual items will be tested separately self.assertTrue( isinstance(pdu.variable_items[0], ApplicationContextItem)) self.assertTrue( isinstance(pdu.variable_items[1], PresentationContextItemAC)) self.assertTrue(isinstance(pdu.variable_items[2], UserInformationItem))
def test_decode_value_type(self): """ Check decoding an assoc_rq produces the correct user information """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) user_info = pdu.variable_items[2] self.assertEqual(user_info.item_type, 0x50) self.assertEqual(user_info.item_length, 58) self.assertTrue(isinstance(user_info.item_type, int)) self.assertTrue(isinstance(user_info.item_length, int)) self.assertTrue(isinstance(user_info.user_data, list)) # Test user items for item in user_info.user_data: # Maximum PDU Length (required) if isinstance(item, MaximumLengthSubItem): self.assertEqual(item.maximum_length_received, 16384) self.assertEqual(user_info.maximum_length, 16384) self.assertTrue(isinstance(item.maximum_length_received, int)) self.assertTrue(isinstance(user_info.maximum_length, int)) # Implementation Class UID (required) elif isinstance(item, ImplementationClassUIDSubItem): self.assertEqual(item.item_type, 0x52) self.assertEqual(item.item_length, 27) self.assertEqual(item.implementation_class_uid, UID('1.2.276.0.7230010.3.0.3.6.0')) self.assertTrue(isinstance(item.item_type, int)) self.assertTrue(isinstance(item.item_length, int)) self.assertTrue(isinstance(item.implementation_class_uid, UID)) # Implementation Version Name (optional) elif isinstance(item, ImplementationVersionNameSubItem): self.assertEqual(item.item_type, 0x55) self.assertEqual(item.item_length, 15) self.assertEqual(item.implementation_version_name, b'OFFIS_DCMTK_360') self.assertTrue(isinstance(item.item_type, int)) self.assertTrue(isinstance(item.item_length, int)) self.assertTrue( isinstance(item.implementation_version_name, bytes))
def test_decode_properties(self): """ Check decoding the produces the correct properties """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) context = pdu.presentation_context[0] # Check context_id property assert context.context_id == 1 # Check result assert context.result == 0 assert context.result == context.result_reason # Check transfer syntax assert isinstance(context.transfer_syntax, UID) assert context.transfer_syntax == UID('1.2.840.10008.1.2') # result_str assert context.result_str == 'Accepted'
def test_from_primitive(self): """ Check converting PDU to primitive """ orig = A_ASSOCIATE_AC() orig.Decode(a_associate_ac) primitive = orig.ToParams() new = A_ASSOCIATE_AC() new.FromParams(primitive) self.assertEqual(new, orig)
def test_decode_properties(self): """ Check decoding the stream produces the correct properties """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) context = pdu.presentation_context[0] # Check ID property context_id = context.ID self.assertTrue(isinstance(context_id, int)) self.assertEqual(context_id, 1) # Check Result result = pdu.presentation_context[0].result_reason self.assertEqual(result, 0) self.assertTrue(isinstance(result, int)) # Check transfer syntax syntax = pdu.presentation_context[0].transfer_syntax self.assertTrue(syntax.is_transfer_syntax) self.assertTrue(isinstance(syntax, UID)) self.assertEqual(syntax, UID('1.2.840.10008.1.2'))
def test_decode(self): """ Check decoding an assoc_rq produces the correct user information """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) user_info = pdu.variable_items[2] assert user_info.item_type == 0x50 assert user_info.item_length == 58 assert len(user_info) == 62 assert isinstance(user_info.user_data, list) # Test user items for item in user_info.user_data: # Maximum PDU Length (required) if isinstance(item, MaximumLengthSubItem): assert item.maximum_length_received == 16384 assert user_info.maximum_length == 16384 # Implementation Class UID (required) elif isinstance(item, ImplementationClassUIDSubItem): assert item.item_type == 0x52 assert item.item_length == 27 assert item.implementation_class_uid == UID( '1.2.276.0.7230010.3.0.3.6.0') assert isinstance(item.item_type, int) assert isinstance(item.item_length, int) assert isinstance(item.implementation_class_uid, UID) # Implementation Version Name (optional) elif isinstance(item, ImplementationVersionNameSubItem): assert item.item_type == 0x55 assert item.item_length == 15 assert item.implementation_version_name == b'OFFIS_DCMTK_360' assert isinstance(item.item_type, int) assert isinstance(item.item_length, int) assert isinstance(item.implementation_version_name, bytes)
def test_init(self): """Test a new A_ASSOCIATE_AC PDU.""" pdu = A_ASSOCIATE_AC() assert pdu.protocol_version == 0x01 assert pdu._reserved_aet is None assert pdu._reserved_aec is None assert pdu.variable_items == [] assert pdu.pdu_type == 0x02 assert pdu.pdu_length == 68 assert len(pdu) == 74 assert pdu.application_context_name is None assert pdu.called_ae_title is None assert pdu.calling_ae_title is None assert pdu.presentation_context == [] assert pdu.user_information is None
def test_property_getters(self): """Test the property getters""" # called_ae_title pdu = A_ASSOCIATE_AC() pdu.reserved_aet = b'TESTA' self.assertEqual(pdu.called_ae_title, b'TESTA') self.assertTrue(isinstance(pdu.called_ae_title, bytes)) pdu.reserved_aet = 'TESTB' self.assertEqual(pdu.called_ae_title, b'TESTB') self.assertTrue(isinstance(pdu.called_ae_title, bytes)) # calling_ae_title pdu = A_ASSOCIATE_AC() pdu.reserved_aec = b'TESTA' self.assertEqual(pdu.calling_ae_title, b'TESTA') self.assertTrue(isinstance(pdu.calling_ae_title, bytes)) pdu.reserved_aec = 'TESTB' self.assertEqual(pdu.calling_ae_title, b'TESTB') self.assertTrue(isinstance(pdu.calling_ae_title, bytes))
def test__pdu_to_event(self): """Test that good PDU paramters return expected results""" dul = DummyDUL() p2e = dul._pdu_to_event pdu_types = [ A_ASSOCIATE_RQ(), A_ASSOCIATE_AC(), A_ASSOCIATE_RJ(), P_DATA_TF(), A_RELEASE_RQ(), A_RELEASE_RP(), A_ABORT_RQ(), 'TEST' ] event_str = [ 'Evt6', 'Evt3', 'Evt4', 'Evt10', 'Evt12', 'Evt13', 'Evt16', 'Evt19' ] for pdu, evt in zip(pdu_types, event_str): self.assertEqual(p2e(pdu), evt)
def _socket_to_pdu(self, data): """Returns the PDU object associated with an incoming data stream. Parameters ---------- data : bytes The incoming data stream Returns ------- pdu : pynetdicom3.pdu.PDU The decoded data as a PDU object """ pdutype = unpack('B', data[0:1])[0] acse = self.assoc.acse pdu_types = { 0x01: (A_ASSOCIATE_RQ(), acse.debug_receive_associate_rq), 0x02: (A_ASSOCIATE_AC(), acse.debug_receive_associate_ac), 0x03: (A_ASSOCIATE_RJ(), acse.debug_receive_associate_rj), 0x04: (P_DATA_TF(), acse.debug_receive_data_tf), 0x05: (A_RELEASE_RQ(), acse.debug_receive_release_rq), 0x06: (A_RELEASE_RP(), acse.debug_receive_release_rp), 0x07: (A_ABORT_RQ(), acse.debug_receive_abort) } if pdutype in pdu_types: pdu = pdu_types[pdutype][0] pdu.Decode(data) # ACSE callbacks pdu_types[pdutype][1](pdu) return pdu return None
def test_to_primitive(self): """ Check converting PDU to primitive """ pdu = A_ASSOCIATE_AC() pdu.Decode(a_associate_ac) primitive = pdu.ToParams() self.assertEqual(primitive.application_context_name, UID('1.2.840.10008.3.1.1.1')) self.assertEqual(primitive.calling_ae_title, b'ECHOSCU ') self.assertEqual(primitive.called_ae_title, b'ANY-SCP ') # Test User Information for item in primitive.user_information: # Maximum PDU Length (required) if isinstance(item, MaximumLengthNegotiation): self.assertEqual(item.maximum_length_received, 16384) self.assertTrue(isinstance(item.maximum_length_received, int)) # Implementation Class UID (required) elif isinstance(item, ImplementationClassUIDNotification): self.assertEqual(item.implementation_class_uid, UID('1.2.276.0.7230010.3.0.3.6.0')) self.assertTrue(isinstance(item.implementation_class_uid, UID)) # Implementation Version Name (optional) elif isinstance(item, ImplementationVersionNameNotification): self.assertEqual(item.implementation_version_name, b'OFFIS_DCMTK_360') self.assertTrue( isinstance(item.implementation_version_name, bytes)) # Test Presentation Contexts for context in primitive.presentation_context_definition_list: self.assertEqual(context.ID, 1) self.assertEqual(context.TransferSyntax[0], UID('1.2.840.10008.1.2')) self.assertTrue(isinstance(primitive.application_context_name, UID)) self.assertTrue(isinstance(primitive.calling_ae_title, bytes)) self.assertTrue(isinstance(primitive.called_ae_title, bytes)) self.assertTrue(isinstance(primitive.user_information, list)) self.assertEqual(primitive.result, 0) self.assertEqual( len(primitive.presentation_context_definition_results_list), 1) # Not used by A-ASSOCIATE-AC or fixed value self.assertEqual(primitive.mode, "normal") self.assertEqual(primitive.responding_ae_title, primitive.called_ae_title) self.assertEqual(primitive.result_source, None) self.assertEqual(primitive.diagnostic, None) self.assertEqual(primitive.calling_presentation_address, None) self.assertEqual(primitive.called_presentation_address, None) self.assertEqual(primitive.responding_presentation_address, primitive.called_presentation_address) self.assertEqual(primitive.presentation_context_definition_list, []) self.assertEqual(primitive.presentation_requirements, "Presentation Kernel") self.assertEqual(primitive.session_requirements, "")
def test_string_output(self): """Test the string output""" pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) assert "Implicit VR Little Endian" in pdu.__str__() assert "1.2.276.0.7230010" in pdu.__str__()
def test_stream_encode(self): """ Check encoding an assoc_ac produces the correct output """ pdu = A_ASSOCIATE_AC() pdu.decode(a_associate_ac) assert pdu.encode() == a_associate_ac