1
1
"""Test transaction."""
2
+ from unittest .mock import patch
2
3
4
+ import pytest
5
+
6
+ from pymodbus .exceptions import ModbusIOException
3
7
from pymodbus .framer import (
4
8
FramerAscii ,
5
9
FramerRTU ,
12
16
TEST_MESSAGE = b"\x7b \x01 \x03 \x00 \x00 \x00 \x05 \x85 \xC9 \x7d "
13
17
14
18
15
- class TestExtas :
19
+ class TestExtras :
16
20
"""Test for the framer module."""
17
21
18
22
client = None
@@ -73,6 +77,26 @@ def test_tcp_framer_transaction_short(self):
73
77
assert used_len == len (msg1 ) + len (msg2 )
74
78
assert pdu .function_code .to_bytes (1 ,'big' ) + pdu .encode () == msg2 [7 :]
75
79
80
+ def test_tcp_framer_transaction_wrong_id (self ):
81
+ """Test a half completed tcp frame transaction."""
82
+ msg = b"\x00 \x01 \x12 \x34 \x00 \x06 \xff \x02 \x01 \x02 \x00 \x08 "
83
+ used_len , pdu = self ._tcp .handleFrame (msg , 1 , 0 )
84
+ assert not pdu
85
+ assert used_len == len (msg )
86
+
87
+ def test_tcp_framer_transaction_wrong_tid (self ):
88
+ """Test a half completed tcp frame transaction."""
89
+ msg = b"\x00 \x01 \x12 \x34 \x00 \x06 \xff \x02 \x01 \x02 \x00 \x08 "
90
+ used_len , pdu = self ._tcp .handleFrame (msg , 0 , 10 )
91
+ assert not pdu
92
+ assert used_len == len (msg )
93
+
94
+ def test_tcp_framer_transaction_wrong_fc (self ):
95
+ """Test a half completed tcp frame transaction."""
96
+ msg = b"\x00 \x01 \x12 \x34 \x00 \x06 \xff \x70 \x01 \x02 \x00 \x08 "
97
+ with pytest .raises (ModbusIOException ):
98
+ self ._tcp .handleFrame (msg , 0 , 0 )
99
+
76
100
def test_tls_incoming_packet (self ):
77
101
"""Framer tls incoming packet."""
78
102
msg = b"\x00 \x01 \x12 \x34 \x00 \x06 \xff \x02 \x01 \x02 \x00 \x08 "
@@ -85,6 +109,32 @@ def test_rtu_process_incoming_packets(self):
85
109
_ , pdu = self ._rtu .handleFrame (msg , 0 , 0 )
86
110
assert pdu
87
111
112
+ def test_rtu_short_packets (self ):
113
+ """Test rtu process incoming packets."""
114
+ msg1 = b"\x00 \x01 "
115
+ msg2 = b"\x00 \x00 \x00 \x01 \xfc \x1b "
116
+ used_len , pdu = self ._rtu .handleFrame (msg1 , 0 , 0 )
117
+ assert not used_len
118
+ assert not pdu
119
+ used_len , pdu = self ._rtu .handleFrame (msg1 + msg2 , 0 , 0 )
120
+ assert used_len == len (msg1 ) + len (msg2 )
121
+ assert pdu
122
+
123
+ def test_rtu_calculate (self ):
124
+ """Test rtu process incoming packets."""
125
+ msg = b"\x00 \x01 \x00 \x00 \x00 \x01 \xfc \x1b "
126
+ with patch ("pymodbus.pdu.ReadCoilsRequest.calculateRtuFrameSize" , return_value = 0 ):
127
+ used_len , pdu = self ._rtu .handleFrame (msg , 0 , 0 )
128
+ assert not used_len
129
+ assert not pdu
130
+
131
+ def test_rtu_wrong_fc (self ):
132
+ """Test rtu process incoming packets."""
133
+ msg = b"\x00 \x70 \x00 \x00 \x00 \x71 \xfc \x1b "
134
+ used_len , pdu = self ._rtu .handleFrame (msg , 0 , 0 )
135
+ assert not pdu
136
+ assert not used_len
137
+
88
138
def test_ascii_process_incoming_packets (self ):
89
139
"""Test ascii process incoming packet."""
90
140
msg = b":F7031389000A60\r \n "
0 commit comments