From 7cc4b6afa4e85ce7bf8a27f9d35fc7a9a77f63f4 Mon Sep 17 00:00:00 2001 From: Helen Kwok <107752876+helenkwok@users.noreply.github.com> Date: Sat, 7 Nov 2026 16:07:41 +1030 Subject: [PATCH] tests: make the raw requests valid RTU frames The RTU unit test stopped in test_server() at the first crafted write request (unit-test-client.c, "write 0 values") and never reached the end of the suite. Two raw requests can't be framed by an RTU server, which works out the request length from the function code and byte count: - send_crafted_request() zeroes the byte count of write requests on the "0 values" pass but still sends the data bytes. The server takes the first 9 bytes as the request (bad CRC), then waits for the rest of a "request" made of the 6 leftover bytes and times out. - The unknown function code request carries 4 payload bytes. The server can't know a length for an unknown function code, reads slave, function code and CRC only, the CRC fails and nothing is replied. Send the zero-value write requests without data, and the unknown function code request as slave and function code only. TCP keeps working since the MBAP length follows the actual request length. Tested with unit-test-server/unit-test-client: - RTU, QNX 8.0 (Raspberry Pi 4) <-> Linux (Raspberry Pi 3B) over UART at 115200 8N1: all tests pass (before: stopped at the "write 0 values" check after 65 OKs). - RTU, Linux <-> Linux over a pty pair: all tests pass (before: same stop). - TCP and TCP PI (IPv6): all tests pass on Linux and QNX. --- tests/unit-test-client.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/unit-test-client.c b/tests/unit-test-client.c index c1542e971..c06ff776b 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -866,8 +866,11 @@ int test_server(modbus_t *ctx, int use_backend) 0x00, 0x64}; const int INVALID_FC = 0x42; - const int INVALID_FC_REQ_LEN = 6; - uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00}; + /* Slave and function code only: with an unknown function code an RTU + * server can't know the request length, so any payload would be read + * as part of the CRC and the request would never get a reply. */ + const int INVALID_FC_REQ_LEN = 2; + uint8_t invalid_fc_raw_req[] = {slave, 0x42}; int req_length; uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; @@ -1020,6 +1023,7 @@ int send_crafted_request(modbus_t *ctx, for (j = 0; j < 2; j++) { int rc; + int len = req_len; req[1] = function; if (j == 0) { @@ -1027,8 +1031,12 @@ int send_crafted_request(modbus_t *ctx, req[4] = 0x00; req[5] = 0x00; if (bytes) { - /* Write query */ + /* Write query: a byte count of 0 must not be followed by + * data, otherwise an RTU server, which frames the request + * from the byte count, reads the data bytes as the start + * of a new request and never replies. */ req[6] = 0x00; + len = req_len - bytes; } } else { /* Try to read or write max values + 1 on second iteration */ @@ -1040,7 +1048,7 @@ int send_crafted_request(modbus_t *ctx, } } - modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t)); + modbus_send_raw_request(ctx, req, len * sizeof(uint8_t)); if (j == 0) { printf( "* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read");