def test_from_primitive(self): """ Check converting PDU to primitive """ orig_pdu = P_DATA_TF() orig_pdu.Decode(p_data_tf) primitive = orig_pdu.ToParams() new_pdu = P_DATA_TF() new_pdu.FromParams(primitive) self.assertEqual(new_pdu, orig_pdu)
def test_from_primitive(self): """ Check converting PDU to primitive """ orig_pdu = P_DATA_TF() orig_pdu.decode(p_data_tf) primitive = orig_pdu.to_primitive() new_pdu = P_DATA_TF() new_pdu.from_primitive(primitive) pdv = new_pdu.presentation_data_value_items[0] assert new_pdu == orig_pdu
def test_init(self): """Test a new P_DATA_TF""" pdu = P_DATA_TF() assert pdu.presentation_data_value_items == [] assert pdu.pdu_type == 0x04 assert pdu.pdu_length == 0 assert len(pdu) == 6
def test_new_encode(self): """ Check encoding using new generic method """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) s = pdu.encode() self.assertEqual(s, p_data_tf)
def test_stream_encode(self): """ Check encoding an p_data produces the correct output """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) s = pdu.Encode() self.assertEqual(s, p_data_tf)
def AR_7(dul): """Association release AR-7. On receiving P-DATA request during attempted association release request send P-DATA-TF State-event triggers: Sta8 + Evt9 .. [1] DICOM Standard 2015b, PS3.8, Table 9-8, "Associate Release Related Actions" Parameters ---------- dul : pynetdicom3.dul.DULServiceProvider The DICOM Upper Layer Service instance for the local AE Returns ------- str Sta8, the next state of the state machine """ # Issue P-DATA-TF PDU dul.pdu = P_DATA_TF() dul.pdu.FromParams(dul.primitive) # Callback dul.assoc.acse.debug_send_data_tf(dul.pdu) bytestream = dul.pdu.Encode() dul.scu_socket.send(bytestream) return 'Sta8'
def DT_1(dul): """Data transfer DT-1. On receiving a P-DATA request, send P-DATA-TF State-event triggers: Sta6 + Evt9 .. [1] DICOM Standard 2015b, PS3.8, Table 9-7, "Data Transfer 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 P-DATA-TF PDU dul.pdu = P_DATA_TF() dul.pdu.FromParams(dul.primitive) dul.primitive = None # Why this? # Callback dul.assoc.acse.debug_send_data_tf(dul.pdu) bytestream = dul.pdu.Encode() dul.scu_socket.send(bytestream) return 'Sta6'
def test_generic_encode(self): """ Check using the new pdu.encode produces the correct output """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) s = pdu.Encode() t = pdu.encode() self.assertEqual(s, t)
def test_decode_properties(self): """ Check decoding the p_data stream produces the correct properties """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) # Check PDVs self.assertTrue(isinstance(pdu.PDVs, list)) self.assertEqual(pdu.get_length(), 90)
def test_to_primitive(self): """ Check converting PDU to primitive """ pdu = P_DATA_TF() pdu.decode(p_data_tf) primitive = pdu.to_primitive() assert primitive.presentation_data_value_list == [[1, p_data_tf[11:]]] assert isinstance(primitive.presentation_data_value_list, list)
def test_generate_items_raises(self): """Test failure modes of ._generate_items method.""" pdu = P_DATA_TF() # Short data data = b'\x00\x00\x00\x04\x01\x01\x02' gen = pdu._generate_items(data) with pytest.raises(AssertionError): next(gen)
def test_stream_decode_values_types(self): """ Check decoding the p_data stream produces the correct objects """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) self.assertEqual(pdu.pdu_type, 0x04) self.assertEqual(pdu.pdu_length, 84) self.assertTrue(isinstance(pdu.pdu_type, int)) self.assertTrue(isinstance(pdu.pdu_length, int))
def test_to_primitive(self): """ Check converting PDU to primitive """ pdu = P_DATA_TF() pdu.Decode(p_data_tf) primitive = pdu.ToParams() self.assertEqual(primitive.presentation_data_value_list, [[1, p_data_tf[11:]]]) self.assertTrue( isinstance(primitive.presentation_data_value_list, list))
def test_conversion(self): """ Check conversion to a PDU produces the correct output """ primitive = P_DATA() pdv = b"\x03\x00\x00\x00\x00" \ b"\x04\x00\x00\x00\x42\x00\x00\x00\x00\x00\x02\x00\x12\x00\x00" \ b"\x00\x31\x2e\x32\x2e\x38\x34\x30\x2e\x31\x30\x30\x30\x38\x2e" \ b"\x31\x2e\x31\x00\x00\x00\x00\x01\x02\x00\x00\x00\x30\x80\x00" \ b"\x00\x20\x01\x02\x00\x00\x00\x01\x00\x00\x00\x00\x08\x02\x00" \ b"\x00\x00\x01\x01\x00\x00\x00\x09\x02\x00\x00\x00\x00\x00" primitive.presentation_data_value_list = [[1, pdv]] pdu = P_DATA_TF() pdu.from_primitive(primitive) data = pdu.encode() self.assertEqual(data, b"\x04\x00\x00\x00\x00\x54\x00\x00\x00\x50\x01" + pdv)
def test_generate_items(self): """Test ._generate_items""" pdu = P_DATA_TF() gen = pdu._generate_items(b'') with pytest.raises(StopIteration): next(gen) data = b'\x00\x00\x00\x04\x01\x01\x02\x03' gen = pdu._generate_items(data) assert next(gen) == (1, b'\x01\x02\x03') with pytest.raises(StopIteration): next(gen) data += b'\x00\x00\x00\x05\x02\x03\x01\x02\x03' gen = pdu._generate_items(data) assert next(gen) == (1, b'\x01\x02\x03') assert next(gen) == (2, b'\x03\x01\x02\x03') with pytest.raises(StopIteration): next(gen)
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 test_decode(self): """ Check decoding the p_data stream produces the correct objects """ pdu = P_DATA_TF() pdu.decode(p_data_tf) assert pdu.pdu_type == 0x04 assert pdu.pdu_length == 84 assert len(pdu) == 90 assert len(pdu.presentation_data_value_items) == 1 assert isinstance(pdu.presentation_data_value_items[0], PresentationDataValueItem) pdv = pdu.presentation_data_value_items[0] assert pdv.presentation_context_id == 1 assert pdv.presentation_data_value == ( b'\x03\x00\x00\x00\x00\x04\x00\x00\x00\x42\x00\x00\x00\x00\x00\x02\x00' b'\x12\x00\x00\x00\x31\x2e\x32\x2e\x38\x34\x30\x2e\x31\x30\x30\x30\x38' b'\x2e\x31\x2e\x31\x00\x00\x00\x00\x01\x02\x00\x00\x00\x30\x80\x00\x00' b'\x20\x01\x02\x00\x00\x00\x01\x00\x00\x00\x00\x08\x02\x00\x00\x00\x01' b'\x01\x00\x00\x00\x09\x02\x00\x00\x00\x00\x00')
def test_wrap_generate_items(self): """Test ._wrap_generate_items""" pdu = P_DATA_TF() out = pdu._wrap_generate_items(b'') assert out == [] data = b'\x00\x00\x00\x04\x01\x01\x02\x03' out = pdu._wrap_generate_items(data) assert len(out) == 1 assert isinstance(out[0], PresentationDataValueItem) assert out[0].context_id == 1 assert out[0].presentation_data_value == b'\x01\x02\x03' data += b'\x00\x00\x00\x05\x02\x03\x01\x02\x03' out = pdu._wrap_generate_items(data) assert len(out) == 2 assert isinstance(out[0], PresentationDataValueItem) assert isinstance(out[1], PresentationDataValueItem) assert out[0].context_id == 1 assert out[1].context_id == 2 assert out[0].presentation_data_value == b'\x01\x02\x03' assert out[1].presentation_data_value == b'\x03\x01\x02\x03'
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_encode(self): """ Check encoding an p_data produces the correct output """ pdu = P_DATA_TF() pdu.decode(p_data_tf) assert pdu.encode() == p_data_tf
def test_string_output(self): """Test the string output""" pdu = P_DATA_TF() pdu.decode(p_data_tf) assert "80 bytes" in pdu.__str__() assert "0x03 0x00" in pdu.__str__()
def test_string_output(self): """Test the string output""" pdu = P_DATA_TF() pdu.Decode(p_data_tf) self.assertTrue("80 bytes" in pdu.__str__()) self.assertTrue("0x03 0x00" in pdu.__str__())