Commit e0839095edbd51cf2d1891772bc003f74e6412dd
1 parent
89d0dc01
New unit test with invalid slave and invalid request
Showing
3 changed files
with
32 additions
and
6 deletions
src/modbus.c
| @@ -250,7 +250,8 @@ static uint8_t compute_meta_length_after_function(int function, | @@ -250,7 +250,8 @@ static uint8_t compute_meta_length_after_function(int function, | ||
| 250 | } | 250 | } |
| 251 | 251 | ||
| 252 | /* Computes the length to read after the meta information (address, count, etc) */ | 252 | /* Computes the length to read after the meta information (address, count, etc) */ |
| 253 | -static int compute_data_length_after_meta(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) | 253 | +static int compute_data_length_after_meta(modbus_t *ctx, uint8_t *msg, |
| 254 | + msg_type_t msg_type) | ||
| 254 | { | 255 | { |
| 255 | int function = msg[ctx->backend->header_length]; | 256 | int function = msg[ctx->backend->header_length]; |
| 256 | int length; | 257 | int length; |
tests/unit-test-client.c
| @@ -300,7 +300,8 @@ int main(int argc, char *argv[]) | @@ -300,7 +300,8 @@ int main(int argc, char *argv[]) | ||
| 300 | 300 | ||
| 301 | /** INPUT REGISTERS **/ | 301 | /** INPUT REGISTERS **/ |
| 302 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, | 302 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, |
| 303 | - UT_INPUT_REGISTERS_NB, tab_rp_registers); | 303 | + UT_INPUT_REGISTERS_NB, |
| 304 | + tab_rp_registers); | ||
| 304 | printf("1/1 modbus_read_input_registers: "); | 305 | printf("1/1 modbus_read_input_registers: "); |
| 305 | if (rc != UT_INPUT_REGISTERS_NB) { | 306 | if (rc != UT_INPUT_REGISTERS_NB) { |
| 306 | printf("FAILED (nb points %d)\n", rc); | 307 | printf("FAILED (nb points %d)\n", rc); |
| @@ -377,7 +378,8 @@ int main(int argc, char *argv[]) | @@ -377,7 +378,8 @@ int main(int argc, char *argv[]) | ||
| 377 | } | 378 | } |
| 378 | 379 | ||
| 379 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, | 380 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, |
| 380 | - UT_INPUT_REGISTERS_NB + 1, tab_rp_registers); | 381 | + UT_INPUT_REGISTERS_NB + 1, |
| 382 | + tab_rp_registers); | ||
| 381 | printf("* modbus_read_input_registers: "); | 383 | printf("* modbus_read_input_registers: "); |
| 382 | if (rc == -1 && errno == EMBXILADD) | 384 | if (rc == -1 && errno == EMBXILADD) |
| 383 | printf("OK\n"); | 385 | printf("OK\n"); |
| @@ -484,12 +486,17 @@ int main(int argc, char *argv[]) | @@ -484,12 +486,17 @@ int main(int argc, char *argv[]) | ||
| 484 | 486 | ||
| 485 | /** SLAVE REPLY **/ | 487 | /** SLAVE REPLY **/ |
| 486 | printf("\nTEST SLAVE REPLY:\n"); | 488 | printf("\nTEST SLAVE REPLY:\n"); |
| 487 | - modbus_set_slave(ctx, 18); | 489 | + modbus_set_slave(ctx, INVALID_SERVER_ID); |
| 488 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 490 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 489 | UT_REGISTERS_NB, tab_rp_registers); | 491 | UT_REGISTERS_NB, tab_rp_registers); |
| 490 | if (use_backend == RTU) { | 492 | if (use_backend == RTU) { |
| 493 | + const int RAW_REQ_LENGTH = 6; | ||
| 494 | + uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF }; | ||
| 495 | + int req_length; | ||
| 496 | + uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; | ||
| 497 | + | ||
| 491 | /* No response in RTU mode */ | 498 | /* No response in RTU mode */ |
| 492 | - printf("1/4 No response from slave %d: ", 18); | 499 | + printf("1/4-A No response from slave %d: ", INVALID_SERVER_ID); |
| 493 | 500 | ||
| 494 | if (rc == -1 && errno == ETIMEDOUT) { | 501 | if (rc == -1 && errno == ETIMEDOUT) { |
| 495 | printf("OK\n"); | 502 | printf("OK\n"); |
| @@ -497,6 +504,23 @@ int main(int argc, char *argv[]) | @@ -497,6 +504,23 @@ int main(int argc, char *argv[]) | ||
| 497 | printf("FAILED\n"); | 504 | printf("FAILED\n"); |
| 498 | goto close; | 505 | goto close; |
| 499 | } | 506 | } |
| 507 | + | ||
| 508 | + /* Send an invalid query with a wrong slave ID */ | ||
| 509 | + req_length = modbus_send_raw_request( | ||
| 510 | + ctx, raw_req, | ||
| 511 | + RAW_REQ_LENGTH * sizeof(uint8_t)); | ||
| 512 | + rc = modbus_receive_confirmation(ctx, rsp); | ||
| 513 | + | ||
| 514 | + printf("1/4-B No response from slave %d with invalid request: ", | ||
| 515 | + INVALID_SERVER_ID); | ||
| 516 | + | ||
| 517 | + if (rc == -1 && errno == ETIMEDOUT) { | ||
| 518 | + printf("OK\n"); | ||
| 519 | + } else { | ||
| 520 | + printf("FAILED (%d)\n", rc); | ||
| 521 | + goto close; | ||
| 522 | + } | ||
| 523 | + | ||
| 500 | } else { | 524 | } else { |
| 501 | /* Response in TCP mode */ | 525 | /* Response in TCP mode */ |
| 502 | printf("1/4 Response from slave %d: ", 18); | 526 | printf("1/4 Response from slave %d: ", 18); |
tests/unit-test.h
| @@ -31,7 +31,8 @@ | @@ -31,7 +31,8 @@ | ||
| 31 | # endif | 31 | # endif |
| 32 | #endif | 32 | #endif |
| 33 | 33 | ||
| 34 | -#define SERVER_ID 17 | 34 | +#define SERVER_ID 17 |
| 35 | +#define INVALID_SERVER_ID 18 | ||
| 35 | 36 | ||
| 36 | const uint16_t UT_BITS_ADDRESS = 0x13; | 37 | const uint16_t UT_BITS_ADDRESS = 0x13; |
| 37 | const uint16_t UT_BITS_NB = 0x25; | 38 | const uint16_t UT_BITS_NB = 0x25; |