Commit 780e1c2365a26af9545e4f4636efc9d5bd16b231

Authored by Stéphane Raimbault
1 parent 05d16704

Support RTU in new raw requests tests

Showing 1 changed file with 19 additions and 14 deletions
tests/unit-test-client.c
... ... @@ -749,7 +749,6 @@ int test_raw_request(modbus_t *ctx, int use_backend)
749 749 0x12, 0x34
750 750 };
751 751 /* See issue #143, test with MAX_WR_WRITE_REGISTERS */
752   -
753 752 int req_length;
754 753 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
755 754 int tab_function[] = {0x01, 0x02, 0x03, 0x04};
... ... @@ -759,16 +758,24 @@ int test_raw_request(modbus_t *ctx, int use_backend)
759 758 MODBUS_MAX_READ_REGISTERS + 1,
760 759 MODBUS_MAX_READ_REGISTERS + 1
761 760 };
  761 + int length;
  762 + int offset;
  763 + const int EXCEPTION_RC = 2;
  764 +
  765 + if (use_backend == RTU) {
  766 + length = 3;
  767 + offset = 1;
  768 + } else {
  769 + length = 7;
  770 + offset = 7;
  771 + }
762 772  
763 773 printf("\nTEST RAW REQUESTS:\n");
764 774  
765 775 req_length = modbus_send_raw_request(ctx, raw_req,
766 776 RAW_REQ_LENGTH * sizeof(uint8_t));
767   -
768 777 printf("* modbus_send_raw_request: ");
769   - if ((use_backend == RTU && req_length == (RAW_REQ_LENGTH + 2)) ||
770   - ((use_backend == TCP || use_backend == TCP_PI) &&
771   - req_length == (RAW_REQ_LENGTH + 6))) {
  778 + if (req_length == (length + 5)) {
772 779 printf("OK\n");
773 780 } else {
774 781 printf("FAILED (%d)\n", req_length);
... ... @@ -777,9 +784,7 @@ int test_raw_request(modbus_t *ctx, int use_backend)
777 784  
778 785 printf("* modbus_receive_confirmation: ");
779 786 rc = modbus_receive_confirmation(ctx, rsp);
780   - if ((use_backend == RTU && rc == 15) ||
781   - ((use_backend == TCP || use_backend == TCP_PI) &&
782   - rc == 19)) {
  787 + if (rc == (length + 12)) {
783 788 printf("OK\n");
784 789 } else {
785 790 printf("FAILED (%d)\n", rc);
... ... @@ -817,9 +822,9 @@ int test_raw_request(modbus_t *ctx, int use_backend)
817 822 printf("* try an exploit with function %d: ", tab_function[i]);
818 823 }
819 824 rc = modbus_receive_confirmation(ctx, rsp);
820   - if (rc == 9 &&
821   - rsp[7] == (0x80 + tab_function[i]) &&
822   - rsp[8] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
  825 + if (rc == (length + EXCEPTION_RC) &&
  826 + rsp[offset] == (0x80 + tab_function[i]) &&
  827 + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
823 828 printf("OK\n");
824 829 } else {
825 830 printf("FAILED\n");
... ... @@ -848,9 +853,9 @@ int test_raw_request(modbus_t *ctx, int use_backend)
848 853 printf("* try an exploit with function %d: ", tab_function[i]);
849 854 }
850 855 rc = modbus_receive_confirmation(ctx, rsp);
851   - if (rc == 9 &&
852   - rsp[7] == (0x80 + tab_function[i]) &&
853   - rsp[8] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
  856 + if (rc == length + EXCEPTION_RC &&
  857 + rsp[offset] == (0x80 + tab_function[i]) &&
  858 + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
854 859 printf("OK\n");
855 860 } else {
856 861 printf("FAILED\n");
... ...