diff --git a/tests/unit-test-client.c b/tests/unit-test-client.c index 727cc19..3e315f4 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -13,6 +13,8 @@ #include "unit-test.h" +const int EXCEPTION_RC = 2; + enum { TCP, TCP_PI, @@ -457,7 +459,7 @@ int main(int argc, char *argv[]) uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH]; /* No response in RTU mode */ - printf("1-A/4 No response from slave %d: ", INVALID_SERVER_ID); + printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID); ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); /* The slave raises a timeout on a confirmation to ignore because if an @@ -472,7 +474,7 @@ int main(int argc, char *argv[]) modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t)); rc = modbus_receive_confirmation(ctx, rsp); - printf("1-B/4 No response from slave %d on indication/confirmation messages: ", + printf("1-B/3 No response from slave %d on indication/confirmation messages: ", INVALID_SERVER_ID); ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); @@ -480,12 +482,12 @@ int main(int argc, char *argv[]) modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t)); rc = modbus_receive_confirmation(ctx, rsp); - printf("1-C/4 No response from slave %d with invalid request: ", + printf("1-C/3 No response from slave %d with invalid request: ", INVALID_SERVER_ID); ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); } else { /* Response in TCP mode */ - printf("1/4 Response from slave %d: ", INVALID_SERVER_ID); + printf("1/3 Response from slave %d: ", INVALID_SERVER_ID); ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); } @@ -494,25 +496,13 @@ int main(int argc, char *argv[]) rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); - printf("2/4 No reply after a broadcast query: "); + printf("2/3 No reply after a broadcast query: "); ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); /* Restore slave */ modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE); - { - const int RAW_REQ_LENGTH = 6; - uint8_t raw_req[] = { use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE, 0x42, 0x00, 0x00, 0x00, 0x00 }; - uint8_t rsp[MODBUS_MAX_ADU_LENGTH]; - - rc = modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t)); - ASSERT_TRUE(rc != -1, "Unable to send raw request with invalid function code"); - rc = modbus_receive_confirmation(ctx, rsp); - printf("3/4 Raise an exception on unknown function code: "); - ASSERT_TRUE(rc == -1, ""); - } - - printf("4/4 Response with an invalid TID or slave: "); + printf("3/3 Response with an invalid TID or slave: "); rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 1, tab_rp_registers); ASSERT_TRUE(rc == -1, ""); @@ -700,9 +690,9 @@ int test_server(modbus_t *ctx, int use_backend) int i; /* Read requests */ const int READ_RAW_REQ_LEN = 6; + const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE; uint8_t read_raw_req[] = { - /* slave */ - (use_backend == RTU) ? SERVER_ID : 0xFF, + slave, /* function, address, 5 values */ MODBUS_FC_READ_HOLDING_REGISTERS, UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, @@ -711,8 +701,7 @@ int test_server(modbus_t *ctx, int use_backend) /* Write and read registers request */ const int RW_RAW_REQ_LEN = 13; uint8_t rw_raw_req[] = { - /* slave */ - (use_backend == RTU) ? SERVER_ID : 0xFF, + slave, /* function, addr to read, nb to read */ MODBUS_FC_WRITE_AND_READ_REGISTERS, /* Read */ @@ -729,8 +718,7 @@ int test_server(modbus_t *ctx, int use_backend) }; const int WRITE_RAW_REQ_LEN = 13; uint8_t write_raw_req[] = { - /* slave */ - (use_backend == RTU) ? SERVER_ID : 0xFF, + slave, /* function will be set in the loop */ MODBUS_FC_WRITE_MULTIPLE_REGISTERS, /* Address */ @@ -740,6 +728,12 @@ int test_server(modbus_t *ctx, int use_backend) /* Dummy data to write */ 0x02, 0x2B, 0x00, 0x01, 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 + }; + int req_length; uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; int tab_read_function[] = { @@ -767,6 +761,17 @@ int test_server(modbus_t *ctx, int use_backend) printf("\nTEST RAW REQUESTS:\n"); + uint32_t old_response_to_sec; + uint32_t old_response_to_usec; + + /* This requests can generate flushes server side so we need a higher + * response timeout than the server. The server uses the defined response + * timeout to sleep before flushing. + * The old timeouts are restored at the end. + */ + modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); + modbus_set_response_timeout(ctx, 0, 600000); + req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN); printf("* modbus_send_raw_request: "); ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length); @@ -810,8 +815,17 @@ int test_server(modbus_t *ctx, int use_backend) if (rc == -1) goto close; + /* Test invalid function code */ + modbus_send_raw_request(ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t)); + rc = modbus_receive_confirmation(ctx, rsp); + printf("Return an exception on unknown function code: "); + ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && + rsp[backend_offset] == (0x80 + INVALID_FC), "") + + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); return 0; close: + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); return -1; } @@ -821,19 +835,8 @@ int send_crafted_request(modbus_t *ctx, int function, uint16_t max_value, uint16_t bytes, int backend_length, int backend_offset) { - const int EXCEPTION_RC = 2; uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; int j; - uint32_t old_response_to_sec; - uint32_t old_response_to_usec; - - /* This requests can generate flushes server side so we need a higher - * response timeout than the server. The server uses the defined response - * timeout to sleep before flushing. - * The old timeouts are restored at the end. - */ - modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); - modbus_set_response_timeout(ctx, 0, 600000); for (j=0; j<2; j++) { int rc; @@ -869,9 +872,7 @@ int send_crafted_request(modbus_t *ctx, int function, rsp[backend_offset] == (0x80 + function) && rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, ""); } - modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); return 0; close: - modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); return -1; }