Commit 52a5f1afe0d722a9d9db07e4339d5fa1d84ec620

Authored by Stéphane Raimbault
1 parent c870bc16

Rewrite new unit test for invalid function code

Showing 1 changed file with 38 additions and 37 deletions
tests/unit-test-client.c
@@ -13,6 +13,8 @@ @@ -13,6 +13,8 @@
13 13
14 #include "unit-test.h" 14 #include "unit-test.h"
15 15
  16 +const int EXCEPTION_RC = 2;
  17 +
16 enum { 18 enum {
17 TCP, 19 TCP,
18 TCP_PI, 20 TCP_PI,
@@ -457,7 +459,7 @@ int main(int argc, char *argv[]) @@ -457,7 +459,7 @@ int main(int argc, char *argv[])
457 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH]; 459 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
458 460
459 /* No response in RTU mode */ 461 /* No response in RTU mode */
460 - printf("1-A/4 No response from slave %d: ", INVALID_SERVER_ID); 462 + printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
461 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 463 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
462 464
463 /* The slave raises a timeout on a confirmation to ignore because if an 465 /* The slave raises a timeout on a confirmation to ignore because if an
@@ -472,7 +474,7 @@ int main(int argc, char *argv[]) @@ -472,7 +474,7 @@ int main(int argc, char *argv[])
472 modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t)); 474 modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t));
473 rc = modbus_receive_confirmation(ctx, rsp); 475 rc = modbus_receive_confirmation(ctx, rsp);
474 476
475 - printf("1-B/4 No response from slave %d on indication/confirmation messages: ", 477 + printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
476 INVALID_SERVER_ID); 478 INVALID_SERVER_ID);
477 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 479 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
478 480
@@ -480,12 +482,12 @@ int main(int argc, char *argv[]) @@ -480,12 +482,12 @@ int main(int argc, char *argv[])
480 modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t)); 482 modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
481 rc = modbus_receive_confirmation(ctx, rsp); 483 rc = modbus_receive_confirmation(ctx, rsp);
482 484
483 - printf("1-C/4 No response from slave %d with invalid request: ", 485 + printf("1-C/3 No response from slave %d with invalid request: ",
484 INVALID_SERVER_ID); 486 INVALID_SERVER_ID);
485 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 487 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
486 } else { 488 } else {
487 /* Response in TCP mode */ 489 /* Response in TCP mode */
488 - printf("1/4 Response from slave %d: ", INVALID_SERVER_ID); 490 + printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
489 ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); 491 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
490 } 492 }
491 493
@@ -494,25 +496,13 @@ int main(int argc, char *argv[]) @@ -494,25 +496,13 @@ int main(int argc, char *argv[])
494 496
495 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 497 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
496 UT_REGISTERS_NB, tab_rp_registers); 498 UT_REGISTERS_NB, tab_rp_registers);
497 - printf("2/4 No reply after a broadcast query: "); 499 + printf("2/3 No reply after a broadcast query: ");
498 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 500 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
499 501
500 /* Restore slave */ 502 /* Restore slave */
501 modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE); 503 modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE);
502 504
503 - {  
504 - const int RAW_REQ_LENGTH = 6;  
505 - uint8_t raw_req[] = { use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE, 0x42, 0x00, 0x00, 0x00, 0x00 };  
506 - uint8_t rsp[MODBUS_MAX_ADU_LENGTH];  
507 -  
508 - rc = modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));  
509 - ASSERT_TRUE(rc != -1, "Unable to send raw request with invalid function code");  
510 - rc = modbus_receive_confirmation(ctx, rsp);  
511 - printf("3/4 Raise an exception on unknown function code: ");  
512 - ASSERT_TRUE(rc == -1, "");  
513 - }  
514 -  
515 - printf("4/4 Response with an invalid TID or slave: "); 505 + printf("3/3 Response with an invalid TID or slave: ");
516 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 506 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
517 1, tab_rp_registers); 507 1, tab_rp_registers);
518 ASSERT_TRUE(rc == -1, ""); 508 ASSERT_TRUE(rc == -1, "");
@@ -700,9 +690,9 @@ int test_server(modbus_t *ctx, int use_backend) @@ -700,9 +690,9 @@ int test_server(modbus_t *ctx, int use_backend)
700 int i; 690 int i;
701 /* Read requests */ 691 /* Read requests */
702 const int READ_RAW_REQ_LEN = 6; 692 const int READ_RAW_REQ_LEN = 6;
  693 + const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE;
703 uint8_t read_raw_req[] = { 694 uint8_t read_raw_req[] = {
704 - /* slave */  
705 - (use_backend == RTU) ? SERVER_ID : 0xFF, 695 + slave,
706 /* function, address, 5 values */ 696 /* function, address, 5 values */
707 MODBUS_FC_READ_HOLDING_REGISTERS, 697 MODBUS_FC_READ_HOLDING_REGISTERS,
708 UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, 698 UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
@@ -711,8 +701,7 @@ int test_server(modbus_t *ctx, int use_backend) @@ -711,8 +701,7 @@ int test_server(modbus_t *ctx, int use_backend)
711 /* Write and read registers request */ 701 /* Write and read registers request */
712 const int RW_RAW_REQ_LEN = 13; 702 const int RW_RAW_REQ_LEN = 13;
713 uint8_t rw_raw_req[] = { 703 uint8_t rw_raw_req[] = {
714 - /* slave */  
715 - (use_backend == RTU) ? SERVER_ID : 0xFF, 704 + slave,
716 /* function, addr to read, nb to read */ 705 /* function, addr to read, nb to read */
717 MODBUS_FC_WRITE_AND_READ_REGISTERS, 706 MODBUS_FC_WRITE_AND_READ_REGISTERS,
718 /* Read */ 707 /* Read */
@@ -729,8 +718,7 @@ int test_server(modbus_t *ctx, int use_backend) @@ -729,8 +718,7 @@ int test_server(modbus_t *ctx, int use_backend)
729 }; 718 };
730 const int WRITE_RAW_REQ_LEN = 13; 719 const int WRITE_RAW_REQ_LEN = 13;
731 uint8_t write_raw_req[] = { 720 uint8_t write_raw_req[] = {
732 - /* slave */  
733 - (use_backend == RTU) ? SERVER_ID : 0xFF, 721 + slave,
734 /* function will be set in the loop */ 722 /* function will be set in the loop */
735 MODBUS_FC_WRITE_MULTIPLE_REGISTERS, 723 MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
736 /* Address */ 724 /* Address */
@@ -740,6 +728,12 @@ int test_server(modbus_t *ctx, int use_backend) @@ -740,6 +728,12 @@ int test_server(modbus_t *ctx, int use_backend)
740 /* Dummy data to write */ 728 /* Dummy data to write */
741 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64 729 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
742 }; 730 };
  731 + const int INVALID_FC = 0x42;
  732 + const int INVALID_FC_REQ_LEN = 6;
  733 + uint8_t invalid_fc_raw_req[] = {
  734 + slave, 0x42, 0x00, 0x00, 0x00, 0x00
  735 + };
  736 +
743 int req_length; 737 int req_length;
744 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; 738 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
745 int tab_read_function[] = { 739 int tab_read_function[] = {
@@ -767,6 +761,17 @@ int test_server(modbus_t *ctx, int use_backend) @@ -767,6 +761,17 @@ int test_server(modbus_t *ctx, int use_backend)
767 761
768 printf("\nTEST RAW REQUESTS:\n"); 762 printf("\nTEST RAW REQUESTS:\n");
769 763
  764 + uint32_t old_response_to_sec;
  765 + uint32_t old_response_to_usec;
  766 +
  767 + /* This requests can generate flushes server side so we need a higher
  768 + * response timeout than the server. The server uses the defined response
  769 + * timeout to sleep before flushing.
  770 + * The old timeouts are restored at the end.
  771 + */
  772 + modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  773 + modbus_set_response_timeout(ctx, 0, 600000);
  774 +
770 req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN); 775 req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
771 printf("* modbus_send_raw_request: "); 776 printf("* modbus_send_raw_request: ");
772 ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length); 777 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) @@ -810,8 +815,17 @@ int test_server(modbus_t *ctx, int use_backend)
810 if (rc == -1) 815 if (rc == -1)
811 goto close; 816 goto close;
812 817
  818 + /* Test invalid function code */
  819 + modbus_send_raw_request(ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
  820 + rc = modbus_receive_confirmation(ctx, rsp);
  821 + printf("Return an exception on unknown function code: ");
  822 + ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
  823 + rsp[backend_offset] == (0x80 + INVALID_FC), "")
  824 +
  825 + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
813 return 0; 826 return 0;
814 close: 827 close:
  828 + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
815 return -1; 829 return -1;
816 } 830 }
817 831
@@ -821,19 +835,8 @@ int send_crafted_request(modbus_t *ctx, int function, @@ -821,19 +835,8 @@ int send_crafted_request(modbus_t *ctx, int function,
821 uint16_t max_value, uint16_t bytes, 835 uint16_t max_value, uint16_t bytes,
822 int backend_length, int backend_offset) 836 int backend_length, int backend_offset)
823 { 837 {
824 - const int EXCEPTION_RC = 2;  
825 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; 838 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
826 int j; 839 int j;
827 - uint32_t old_response_to_sec;  
828 - uint32_t old_response_to_usec;  
829 -  
830 - /* This requests can generate flushes server side so we need a higher  
831 - * response timeout than the server. The server uses the defined response  
832 - * timeout to sleep before flushing.  
833 - * The old timeouts are restored at the end.  
834 - */  
835 - modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);  
836 - modbus_set_response_timeout(ctx, 0, 600000);  
837 840
838 for (j=0; j<2; j++) { 841 for (j=0; j<2; j++) {
839 int rc; 842 int rc;
@@ -869,9 +872,7 @@ int send_crafted_request(modbus_t *ctx, int function, @@ -869,9 +872,7 @@ int send_crafted_request(modbus_t *ctx, int function,
869 rsp[backend_offset] == (0x80 + function) && 872 rsp[backend_offset] == (0x80 + function) &&
870 rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, ""); 873 rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
871 } 874 }
872 - modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);  
873 return 0; 875 return 0;
874 close: 876 close:
875 - modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);  
876 return -1; 877 return -1;
877 } 878 }