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 | 250 | } |
| 251 | 251 | |
| 252 | 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 | 256 | int function = msg[ctx->backend->header_length]; |
| 256 | 257 | int length; | ... | ... |
tests/unit-test-client.c
| ... | ... | @@ -300,7 +300,8 @@ int main(int argc, char *argv[]) |
| 300 | 300 | |
| 301 | 301 | /** INPUT REGISTERS **/ |
| 302 | 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 | 305 | printf("1/1 modbus_read_input_registers: "); |
| 305 | 306 | if (rc != UT_INPUT_REGISTERS_NB) { |
| 306 | 307 | printf("FAILED (nb points %d)\n", rc); |
| ... | ... | @@ -377,7 +378,8 @@ int main(int argc, char *argv[]) |
| 377 | 378 | } |
| 378 | 379 | |
| 379 | 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 | 383 | printf("* modbus_read_input_registers: "); |
| 382 | 384 | if (rc == -1 && errno == EMBXILADD) |
| 383 | 385 | printf("OK\n"); |
| ... | ... | @@ -484,12 +486,17 @@ int main(int argc, char *argv[]) |
| 484 | 486 | |
| 485 | 487 | /** SLAVE REPLY **/ |
| 486 | 488 | printf("\nTEST SLAVE REPLY:\n"); |
| 487 | - modbus_set_slave(ctx, 18); | |
| 489 | + modbus_set_slave(ctx, INVALID_SERVER_ID); | |
| 488 | 490 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 489 | 491 | UT_REGISTERS_NB, tab_rp_registers); |
| 490 | 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 | 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 | 501 | if (rc == -1 && errno == ETIMEDOUT) { |
| 495 | 502 | printf("OK\n"); |
| ... | ... | @@ -497,6 +504,23 @@ int main(int argc, char *argv[]) |
| 497 | 504 | printf("FAILED\n"); |
| 498 | 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 | 524 | } else { |
| 501 | 525 | /* Response in TCP mode */ |
| 502 | 526 | printf("1/4 Response from slave %d: ", 18); | ... | ... |