def test_good_ae_title(self): """Test good ae titles are set correctly.""" # Check str AE good_ae = ['a', 'a b', 'a b', ' b'] ref = [ 'a ', 'a b', 'a b ', 'b ' ] for ae, ref_ae in zip(good_ae, ref): new_ae = validate_ae_title(ae) self.assertEqual(new_ae, ref_ae) self.assertTrue(isinstance(new_ae, str)) # Check bytes AE good_ae = [b'a', b'a b', b'a b', b' b'] ref = [ b'a ', b'a b', b'a b ', b'b ' ] for ae, ref_ae in zip(good_ae, ref): new_ae = validate_ae_title(ae) self.assertEqual(new_ae, ref_ae) self.assertTrue(isinstance(new_ae, bytes))
def test_bad_ae_title(self): """Test bad AE titles raise exceptions.""" # Bad str AE bad_ae = [ ' ', # empty, 16 chars 0x20 '', # empty 'AE\\TITLE', # backslash 'AE\tTITLE', # control char, tab 'AE\rTITLE', # control char, line feed 'AE\nTITLE' ] # control char, newline for ae in bad_ae: with self.assertRaises(ValueError): validate_ae_title(ae) # Bad bytes AE bad_ae = [ b' ', # empty, 16 chars 0x20 b'', # empty b'AE\\TITLE', # backslash b'AE\tTITLE', # control char, tab b'AE\rTITLE', # control char, line feed b'AE\nTITLE' ] # control char, newline for ae in bad_ae: with self.assertRaises(ValueError): validate_ae_title(ae)
def ae_title(self, value): """Get the AE title.""" # pylint: disable=attribute-defined-outside-init try: self._ae_title = validate_ae_title(value) except: raise
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] self.assertEqual(cs_pdv, c_move_rq_cmd) self.assertEqual(ds_pdv, c_move_rq_ds)
def associate(self, addr, port, ae_title='ANY-SCP', max_pdu=16382, ext_neg=None): """Attempts to associate with a remote application entity When requesting an association the local AE is acting as an SCU. The Association thread is returned whether or not the association is accepted and should be checked using Association.is_established before sending any messages. Parameters ---------- addr : str The peer AE's TCP/IP address (IPv4) port : int The peer AE's listen port number ae_title : str, optional The peer AE's title max_pdu : int, optional The maximum PDV receive size in bytes to use when negotiating the association ext_neg : List of UserInformation objects, optional Used if extended association negotiation is required Returns ------- assoc : pynetdicom3.association.Association The Association thread """ if not isinstance(addr, str): raise TypeError("ip_address must be a valid IPv4 string") if not isinstance(port, int): raise TypeError("port must be a valid port number") peer_ae = {'AET' : validate_ae_title(ae_title), 'Address' : addr, 'Port' : port} # Associate assoc = Association(local_ae=self, peer_ae=peer_ae, acse_timeout=self.acse_timeout, dimse_timeout=self.dimse_timeout, max_pdu=max_pdu, ext_neg=ext_neg) assoc.start() # Endlessly loops while the Association negotiation is taking place while (not assoc.is_established and not assoc.is_rejected and not assoc.is_aborted and not assoc.dul._kill_thread): # Program loops here endlessly sometimes time.sleep(0.1) # If the Association was established if assoc.is_established: self.active_associations.append(assoc) return assoc
def test_conversion_rsp(self): """ Check conversion to a -RSP PDU produces the correct output """ primitive = C_FIND() primitive.MessageIDBeingRespondedTo = 5 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.Status = 0xFF00 ref_identifier = Dataset() ref_identifier.QueryRetrieveLevel = "PATIENT" ref_identifier.RetrieveAETitle = validate_ae_title("FINDSCP") ref_identifier.PatientName = "ANON^A^B^C^D" primitive.Identifier = BytesIO(encode(ref_identifier, True, True)) dimse_msg = C_FIND_RSP() dimse_msg.primitive_to_message(primitive) pdvs = dimse_msg.encode_msg(1, 16382) cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] self.assertEqual(cs_pdv, c_find_rsp_cmd) self.assertEqual(ds_pdv, c_find_rsp_ds)
def test_bad_parameters(self): "Test exception raised if ae is not str or bytes." with self.assertRaises(TypeError): validate_ae_title(1234) with self.assertRaises(TypeError): validate_ae_title(['aetitle'])
def test_invalid_ae_title_raises(self): """Test invalid AE title value raises exception.""" with pytest.raises(TypeError, match=r"Invalid value for an AE"): validate_ae_title(1234)