Commit 780e1c2365a26af9545e4f4636efc9d5bd16b231
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,7 +749,6 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 749 | 0x12, 0x34 | 749 | 0x12, 0x34 |
| 750 | }; | 750 | }; |
| 751 | /* See issue #143, test with MAX_WR_WRITE_REGISTERS */ | 751 | /* See issue #143, test with MAX_WR_WRITE_REGISTERS */ |
| 752 | - | ||
| 753 | int req_length; | 752 | int req_length; |
| 754 | uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; | 753 | uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 755 | int tab_function[] = {0x01, 0x02, 0x03, 0x04}; | 754 | int tab_function[] = {0x01, 0x02, 0x03, 0x04}; |
| @@ -759,16 +758,24 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -759,16 +758,24 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 759 | MODBUS_MAX_READ_REGISTERS + 1, | 758 | MODBUS_MAX_READ_REGISTERS + 1, |
| 760 | MODBUS_MAX_READ_REGISTERS + 1 | 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 | printf("\nTEST RAW REQUESTS:\n"); | 773 | printf("\nTEST RAW REQUESTS:\n"); |
| 764 | 774 | ||
| 765 | req_length = modbus_send_raw_request(ctx, raw_req, | 775 | req_length = modbus_send_raw_request(ctx, raw_req, |
| 766 | RAW_REQ_LENGTH * sizeof(uint8_t)); | 776 | RAW_REQ_LENGTH * sizeof(uint8_t)); |
| 767 | - | ||
| 768 | printf("* modbus_send_raw_request: "); | 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 | printf("OK\n"); | 779 | printf("OK\n"); |
| 773 | } else { | 780 | } else { |
| 774 | printf("FAILED (%d)\n", req_length); | 781 | printf("FAILED (%d)\n", req_length); |
| @@ -777,9 +784,7 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -777,9 +784,7 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 777 | 784 | ||
| 778 | printf("* modbus_receive_confirmation: "); | 785 | printf("* modbus_receive_confirmation: "); |
| 779 | rc = modbus_receive_confirmation(ctx, rsp); | 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 | printf("OK\n"); | 788 | printf("OK\n"); |
| 784 | } else { | 789 | } else { |
| 785 | printf("FAILED (%d)\n", rc); | 790 | printf("FAILED (%d)\n", rc); |
| @@ -817,9 +822,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -817,9 +822,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 817 | printf("* try an exploit with function %d: ", tab_function[i]); | 822 | printf("* try an exploit with function %d: ", tab_function[i]); |
| 818 | } | 823 | } |
| 819 | rc = modbus_receive_confirmation(ctx, rsp); | 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 | printf("OK\n"); | 828 | printf("OK\n"); |
| 824 | } else { | 829 | } else { |
| 825 | printf("FAILED\n"); | 830 | printf("FAILED\n"); |
| @@ -848,9 +853,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -848,9 +853,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 848 | printf("* try an exploit with function %d: ", tab_function[i]); | 853 | printf("* try an exploit with function %d: ", tab_function[i]); |
| 849 | } | 854 | } |
| 850 | rc = modbus_receive_confirmation(ctx, rsp); | 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 | printf("OK\n"); | 859 | printf("OK\n"); |
| 855 | } else { | 860 | } else { |
| 856 | printf("FAILED\n"); | 861 | printf("FAILED\n"); |