Debian Codegraph @

tests/unit-test-client.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <errno.h>
8 #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "unit-test.h"cxx . . $ `<file>/tests/unit-test.h`/
15
16 const int EXCEPTION_RCcxx . . $ EXCEPTION_RC. = 2;
17
18 enumcxx . . $ $anonymous_type_b3ed69e3c7e582a2_0# {
19 TCPcxx . . $ TCP.,
20 TCP_PIcxx . . $ TCP_PI.,
21 RTUcxx . . $ RTU.
22 };
23
24 int test_servercxx . . $ test_server(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int use_backend);
25 int send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(modbus_tcxx . . $ modbus_t# *ctx,
26 int function,
27 uint8_tcxx . . $ uint8_t# *req,
28 int req_size,
29 uint16_tcxx . . $ uint16_t# max_value,
30 uint16_tcxx . . $ uint16_t# bytes,
31 int backend_length,
32 int backend_offset);
33 int equal_dwordcxx . . $ equal_dword(90556b1a121344a2).(uint16_tcxx . . $ uint16_t# *tab_reg, const uint32_tcxx . . $ uint32_t# value);
34 int is_memory_equalcxx . . $ is_memory_equal(36f949851efa3055).(const void *s1, const void *s2, size_t size);
35
36 #define BUG_REPORTcxx . . $ `tests/unit-test-client.c:36:9`!(_cond, _format, _args...) \
37 printf( \
38 "\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args)
39
40 #define ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(_cond, _format, __args...) \
41 { \
42 if (_cond) { \
43 printf("OK\n"); \
44 } else { \
45 BUG_REPORTcxx . . $ `tests/unit-test-client.c:36:9`!(_cond, _format, ##__args); \
46 goto close; \
47 } \
48 };
49
50 int is_memory_equalcxx . . $ is_memory_equal(36f949851efa3055).(const void *s1, const void *s2, size_t size)
51 {
52 return (memcmpcxx . . $ memcmp(36f949851efa3055).(s1, s2, size) == 0);
53 }
54
55 int equal_dwordcxx . . $ equal_dword(90556b1a121344a2).(uint16_tcxx . . $ uint16_t# *tab_reg, const uint32_tcxx . . $ uint32_t# value)
56 {
57 return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF)));
58 }
59
60 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
61 {
62 /* Length of report slave ID response slave ID + ON/OFF + 'LMB' + version */
63 const int NB_REPORT_SLAVE_ID = 2 + 3 + strlencxx . . $ strlen(751346f8406fd082).(LIBMODBUS_VERSION_STRINGcxx . . $ `src/modbus-version.h:36:9`!);
64 uint8_tcxx . . $ uint8_t# *tab_rp_bits = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
65 uint16_tcxx . . $ uint16_t# *tab_rp_registers = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
66 uint16_tcxx . . $ uint16_t# *tab_rp_registers_bad = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
67 modbus_tcxx . . $ modbus_t# *ctx = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
68 int i;
69 uint8_tcxx . . $ uint8_t# value;
70 int nb_points;
71 int rc;
72 float real;
73 uint32_tcxx . . $ uint32_t# old_response_to_sec;
74 uint32_tcxx . . $ uint32_t# old_response_to_usec;
75 uint32_tcxx . . $ uint32_t# new_response_to_sec;
76 uint32_tcxx . . $ uint32_t# new_response_to_usec;
77 uint32_tcxx . . $ uint32_t# old_byte_to_sec;
78 uint32_tcxx . . $ uint32_t# old_byte_to_usec;
79 int use_backend;
80 int success = FALSEcxx . . $ `src/modbus.h:47:9`!;
81 int old_slave;
82 char *ip_or_device;
83
84 if (argc > 1) {
85 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcp") == 0) {
86 use_backend = TCPcxx . . $ TCP.;
87 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcppi") == 0) {
88 use_backend = TCP_PIcxx . . $ TCP_PI.;
89 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "rtu") == 0) {
90 use_backend = RTUcxx . . $ RTU.;
91 } else {
92 printfcxx . . $ printf(581fddd72a8db8fc).("Modbus client for unit testing\n");
93 printfcxx . . $ printf(581fddd72a8db8fc).("Usage:\n %s [tcp|tcppi|rtu]\n", argv[0]);
94 printfcxx . . $ printf(581fddd72a8db8fc).("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB1\n\n");
95 exitcxx . . $ exit(3ca292c338df9bb3).(1);
96 }
97 } else {
98 /* By default */
99 use_backend = TCPcxx . . $ TCP.;
100 }
101
102 if (argc > 2) {
103 ip_or_device = argv[2];
104 } else {
105 switch (use_backend) {
106 case TCPcxx . . $ TCP.:
107 ip_or_device = "127.0.0.1";
108 break;
109 case TCP_PIcxx . . $ TCP_PI.:
110 ip_or_device = "::1";
111 break;
112 case RTUcxx . . $ RTU.:
113 ip_or_device = "/dev/ttyUSB1";
114 break;
115 default:
116 break;
117 }
118 }
119
120 if (use_backend == TCPcxx . . $ TCP.) {
121 ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).(ip_or_device, 1502);
122 } else if (use_backend == TCP_PIcxx . . $ TCP_PI.) {
123 ctx = modbus_new_tcp_picxx . . $ modbus_new_tcp_pi(5f5df5bc2aa40d50).(ip_or_device, "1502");
124 } else {
125 ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).(ip_or_device, 115200, 'N', 8, 1);
126 }
127 if (ctx == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
128 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Unable to allocate libmodbus context\n");
129 return -1;
130 }
131
132 modbus_set_debugcxx . . $ modbus_set_debug(2a7e0e283938d456).(ctx, TRUEcxx . . $ `src/modbus.h:51:9`!);
133 modbus_set_error_recoverycxx . . $ modbus_set_error_recovery(882859f4d5bb8f75).(
134 ctx, MODBUS_ERROR_RECOVERY_LINKcxx . . $ MODBUS_ERROR_RECOVERY_LINK. | MODBUS_ERROR_RECOVERY_PROTOCOLcxx . . $ MODBUS_ERROR_RECOVERY_PROTOCOL.);
135
136 if (use_backend == RTUcxx . . $ RTU.) {
137 modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, SERVER_IDcxx . . $ `tests/unit-test.h:28:9`!);
138 }
139
140 modbus_get_response_timeoutcxx . . $ modbus_get_response_timeout(536e4a54166c71db).(ctx, &old_response_to_sec, &old_response_to_usec);
141 if (modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(ctx) == -1) {
142 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Connection failed: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
143 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
144 return -1;
145 }
146
147 /* Allocate and initialize the memory to store the bits */
148 nb_points = (UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`! > UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB.) ? UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`! : UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB.;
149 tab_rp_bits = (uint8_tcxx . . $ uint8_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(nb_points * sizeof(uint8_tcxx . . $ uint8_t#));
150 memsetcxx . . $ memset(d1de1e4fb215f1c6).(tab_rp_bits, 0, nb_points * sizeof(uint8_tcxx . . $ uint8_t#));
151
152 /* Allocate and initialize the memory to store the registers */
153 nb_points = (UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB. > UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.) ? UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.
154 : UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.;
155 tab_rp_registers = (uint16_tcxx . . $ uint16_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(nb_points * sizeof(uint16_tcxx . . $ uint16_t#));
156 memsetcxx . . $ memset(d1de1e4fb215f1c6).(tab_rp_registers, 0, nb_points * sizeof(uint16_tcxx . . $ uint16_t#));
157
158 printfcxx . . $ printf(581fddd72a8db8fc).("** UNIT TESTING **\n");
159
160 printfcxx . . $ printf(581fddd72a8db8fc).("1/1 No response timeout modification on connect: ");
161 modbus_get_response_timeoutcxx . . $ modbus_get_response_timeout(536e4a54166c71db).(ctx, &new_response_to_sec, &new_response_to_usec);
162 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(old_response_to_sec == new_response_to_sec &&
163 old_response_to_usec == new_response_to_usec,
164 "");
165
166 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST WRITE/READ:\n");
167
168 /** COIL BITS **/
169
170 /* Single */
171 rc = modbus_write_bitcxx . . $ modbus_write_bit(16de6ae3c67ca311).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., ONcxx . . $ `src/modbus.h:59:9`!);
172 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 modbus_write_bit: ");
173 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "");
174
175 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., 1, tab_rp_bits);
176 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 modbus_read_bits: ");
177 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "FAILED (nb points %d)\n", rc);
178 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(tab_rp_bits[0] == ONcxx . . $ `src/modbus.h:59:9`!, "FAILED (%0X != %0X)\n", tab_rp_bits[0], ONcxx . . $ `src/modbus.h:59:9`!);
179
180 /* End single */
181
182 /* Multiple bits */
183 {
184 uint8_tcxx . . $ uint8_t# tab_value[UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!];
185
186 modbus_set_bits_from_bytescxx . . $ modbus_set_bits_from_bytes(a1587c06cb9b3a17).(tab_value, 0, UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, UT_BITS_TABcxx . . $ UT_BITS_TAB.);
187 rc = modbus_write_bitscxx . . $ modbus_write_bits(90e367cb830b4f97).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, tab_value);
188 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 modbus_write_bits: ");
189 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, "");
190 }
191
192 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, tab_rp_bits);
193 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 modbus_read_bits: ");
194 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, "FAILED (nb points %d)\n", rc);
195
196 i = 0;
197 nb_points = UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!;
198 while (nb_points > 0) {
199 int nb_bits = (nb_points > 8) ? 8 : nb_points;
200
201 value = modbus_get_byte_from_bitscxx . . $ modbus_get_byte_from_bits(452d793f89d8163c).(tab_rp_bits, i * 8, nb_bits);
202 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(
203 value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
204
205 nb_points -= nb_bits;
206 i++;
207 }
208 printfcxx . . $ printf(581fddd72a8db8fc).("OK\n");
209 /* End of multiple bits */
210
211 /** DISCRETE INPUTS **/
212 rc =
213 modbus_read_input_bitscxx . . $ modbus_read_input_bits(11f4da49ebe31404).(ctx, UT_INPUT_BITS_ADDRESScxx . . $ UT_INPUT_BITS_ADDRESS., UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB., tab_rp_bits);
214 printfcxx . . $ printf(581fddd72a8db8fc).("1/1 modbus_read_input_bits: ");
215 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
216
217 i = 0;
218 nb_points = UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB.;
219 while (nb_points > 0) {
220 int nb_bits = (nb_points > 8) ? 8 : nb_points;
221 value = modbus_get_byte_from_bitscxx . . $ modbus_get_byte_from_bits(452d793f89d8163c).(tab_rp_bits, i * 8, nb_bits);
222 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(value == UT_INPUT_BITS_TAB[i],
223 "FAILED (%0X != %0X)\n",
224 value,
225 UT_INPUT_BITS_TAB[i]);
226
227 nb_points -= nb_bits;
228 i++;
229 }
230 printfcxx . . $ printf(581fddd72a8db8fc).("OK\n");
231
232 /** HOLDING REGISTERS **/
233
234 /* Single register */
235 rc = modbus_write_registercxx . . $ modbus_write_register(146febe49439e9d3).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 0x1234);
236 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 modbus_write_register: ");
237 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "");
238
239 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 1, tab_rp_registers);
240 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 modbus_read_registers: ");
241 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "FAILED (nb points %d)\n", rc);
242 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(tab_rp_registers[0] == 0x1234,
243 "FAILED (%0X != %0X)\n",
244 tab_rp_registers[0],
245 0x1234);
246 /* End of single register */
247
248 /* Many registers */
249 rc = modbus_write_registerscxx . . $ modbus_write_registers(1ccebf9f3d033562).(
250 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., UT_REGISTERS_TABcxx . . $ UT_REGISTERS_TAB.);
251 printfcxx . . $ printf(581fddd72a8db8fc).("1/5 modbus_write_registers: ");
252 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_REGISTERS_NB, "");
253
254 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
255 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
256 printfcxx . . $ printf(581fddd72a8db8fc).("2/5 modbus_read_registers: ");
257 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
258
259 for (i = 0; i < UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.; i++) {
260 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
261 "FAILED (%0X != %0X)\n",
262 tab_rp_registers[i],
263 UT_REGISTERS_TAB[i]);
264 }
265
266 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 0, tab_rp_registers);
267 printfcxx . . $ printf(581fddd72a8db8fc).("3/5 modbus_read_registers (0): ");
268 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1, "FAILED (nb_points %d)\n", rc);
269
270 nb_points = (UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB. > UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.) ? UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.
271 : UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.;
272 memsetcxx . . $ memset(d1de1e4fb215f1c6).(tab_rp_registers, 0, nb_points * sizeof(uint16_tcxx . . $ uint16_t#));
273
274 /* Write registers to zero from tab_rp_registers and store read registers
275 into tab_rp_registers. So the read registers must set to 0, except the
276 first one because there is an offset of 1 register on write. */
277 rc = modbus_write_and_read_registerscxx . . $ modbus_write_and_read_registers(c6a0b9119112ae8e).(ctx,
278 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + 1,
279 UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB. - 1,
280 tab_rp_registers,
281 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS.,
282 UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.,
283 tab_rp_registers);
284 printfcxx . . $ printf(581fddd72a8db8fc).("4/5 modbus_write_and_read_registers: ");
285 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(
286 rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
287
288 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
289 "FAILED (%0X != %0X)\n",
290 tab_rp_registers[0],
291 UT_REGISTERS_TAB[0]);
292
293 for (i = 1; i < UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.; i++) {
294 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(
295 tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers[i], 0);
296 }
297
298 /* End of many registers */
299
300 /** INPUT REGISTERS **/
301 rc = modbus_read_input_registerscxx . . $ modbus_read_input_registers(e941881bef6bc78c).(
302 ctx, UT_INPUT_REGISTERS_ADDRESScxx . . $ UT_INPUT_REGISTERS_ADDRESS., UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB., tab_rp_registers);
303 printfcxx . . $ printf(581fddd72a8db8fc).("1/1 modbus_read_input_registers: ");
304 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
305
306 for (i = 0; i < UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.; i++) {
307 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
308 "FAILED (%0X != %0X)\n",
309 tab_rp_registers[i],
310 UT_INPUT_REGISTERS_TAB[i]);
311 }
312
313 /* MASKS */
314 printfcxx . . $ printf(581fddd72a8db8fc).("1/1 Write mask: ");
315 rc = modbus_write_registercxx . . $ modbus_write_register(146febe49439e9d3).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 0x12);
316 rc = modbus_mask_write_registercxx . . $ modbus_mask_write_register(ba42a0470b5e623e).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 0xF2, 0x25);
317 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc != -1, "FAILED (%x == -1)\n", rc);
318 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., 1, tab_rp_registers);
319 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(
320 tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17);
321
322 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST FLOATS\n");
323 /** FLOAT **/
324 printfcxx . . $ printf(581fddd72a8db8fc).("1/4 Set/get float ABCD: ");
325 modbus_set_float_abcdcxx . . $ modbus_set_float_abcd(39eb9394749c9c9b).(UT_REALcxx . . $ UT_REAL., tab_rp_registers);
326 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD, 4),
327 "FAILED Set float ABCD");
328 real = modbus_get_float_abcdcxx . . $ modbus_get_float_abcd(b07654c42935ee93).(UT_IREAL_ABCDcxx . . $ UT_IREAL_ABCD.);
329 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
330
331 printfcxx . . $ printf(581fddd72a8db8fc).("2/4 Set/get float DCBA: ");
332 modbus_set_float_dcbacxx . . $ modbus_set_float_dcba(39eb9394749c9c9b).(UT_REALcxx . . $ UT_REAL., tab_rp_registers);
333 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA, 4),
334 "FAILED Set float DCBA");
335 real = modbus_get_float_dcbacxx . . $ modbus_get_float_dcba(b07654c42935ee93).(UT_IREAL_DCBAcxx . . $ UT_IREAL_DCBA.);
336 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
337
338 printfcxx . . $ printf(581fddd72a8db8fc).("3/4 Set/get float BADC: ");
339 modbus_set_float_badccxx . . $ modbus_set_float_badc(39eb9394749c9c9b).(UT_REALcxx . . $ UT_REAL., tab_rp_registers);
340 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(tab_rp_registers, UT_IREAL_BADC, 4),
341 "FAILED Set float BADC");
342 real = modbus_get_float_badccxx . . $ modbus_get_float_badc(b07654c42935ee93).(UT_IREAL_BADCcxx . . $ UT_IREAL_BADC.);
343 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
344
345 printfcxx . . $ printf(581fddd72a8db8fc).("4/4 Set/get float CDAB: ");
346 modbus_set_float_cdabcxx . . $ modbus_set_float_cdab(39eb9394749c9c9b).(UT_REALcxx . . $ UT_REAL., tab_rp_registers);
347 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB, 4),
348 "FAILED Set float CDAB");
349 real = modbus_get_float_cdabcxx . . $ modbus_get_float_cdab(b07654c42935ee93).(UT_IREAL_CDABcxx . . $ UT_IREAL_CDAB.);
350 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
351
352 /* Regression: a negative float has its most significant byte >= 0x80.
353 On the get side the "a << 24" byte assembly overflows a signed int
354 (undefined behavior) unless done in unsigned width; on the set side the
355 float must be serialized with memcpy, not a float->uint32_t type-pun
356 (strict-aliasing UB). UT_REAL is positive (0x47F12000) and misses both
357 paths; -UT_REAL (0xC7F12000) exercises them. Run under
358 -fsanitize=undefined to catch the get-side UB. */
359 printfcxx . . $ printf(581fddd72a8db8fc).("Set/get negative float ABCD/DCBA/BADC/CDAB: ");
360 {
361 const uint16_tcxx . . $ uint16_t# neg_abcd[] = {0xC7F1, 0x2000};
362 const uint16_tcxx . . $ uint16_t# neg_dcba[] = {0x0020, 0xF1C7};
363 const uint16_tcxx . . $ uint16_t# neg_badc[] = {0xF1C7, 0x0020};
364 const uint16_tcxx . . $ uint16_t# neg_cdab[] = {0x2000, 0xC7F1};
365 uint16_tcxx . . $ uint16_t# out[2];
366
367 modbus_set_float_abcdcxx . . $ modbus_set_float_abcd(39eb9394749c9c9b).(-UT_REALcxx . . $ UT_REAL., out);
368 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(out, neg_abcd, 4) &&
369 modbus_get_float_abcd(neg_abcd) == -UT_REAL,
370 "FAILED negative float ABCD\n");
371 modbus_set_float_dcbacxx . . $ modbus_set_float_dcba(39eb9394749c9c9b).(-UT_REALcxx . . $ UT_REAL., out);
372 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(out, neg_dcba, 4) &&
373 modbus_get_float_dcba(neg_dcba) == -UT_REAL,
374 "FAILED negative float DCBA\n");
375 modbus_set_float_badccxx . . $ modbus_set_float_badc(39eb9394749c9c9b).(-UT_REALcxx . . $ UT_REAL., out);
376 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(out, neg_badc, 4) &&
377 modbus_get_float_badc(neg_badc) == -UT_REAL,
378 "FAILED negative float BADC\n");
379 modbus_set_float_cdabcxx . . $ modbus_set_float_cdab(39eb9394749c9c9b).(-UT_REALcxx . . $ UT_REAL., out);
380 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(is_memory_equal(out, neg_cdab, 4) &&
381 modbus_get_float_cdab(neg_cdab) == -UT_REAL,
382 "FAILED negative float CDAB\n");
383 }
384
385 /* Regression: the MODBUS_GET_INT*_FROM_INT* macros must shift in unsigned
386 width. A high word/byte with the top bit set makes the old signed shift
387 overflow (undefined behavior) and, for INT16, produce a sign-incorrect
388 result. */
389 printfcxx . . $ printf(581fddd72a8db8fc).("Get signed int from registers INT16/INT32/INT64: ");
390 {
391 const uint8_tcxx . . $ uint8_t# r8[] = {0xC7, 0xF1};
392 const uint16_tcxx . . $ uint16_t# r16[] = {0xC7F1, 0x2000};
393 const uint16_tcxx . . $ uint16_t# r64[] = {0xD200, 0x1234, 0x5678, 0x9ABC};
394
395 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(MODBUS_GET_INT16_FROM_INT8cxx . . $ `src/modbus.h:304:9`!(r8, 0) == (int16_t) 0xC7F1 &&
396 MODBUS_GET_INT32_FROM_INT16cxx . . $ `src/modbus.h:301:9`!(r16, 0) == (int32_t) 0xC7F12000 &&
397 MODBUS_GET_INT64_FROM_INT16cxx . . $ `src/modbus.h:296:9`!(r64, 0) ==
398 (int64_t) 0xD200123456789ABCULL,
399 "FAILED signed int conversion\n");
400 }
401
402 printfcxx . . $ printf(581fddd72a8db8fc).("\nAt this point, error messages doesn't mean the test has failed\n");
403
404 /** ILLEGAL DATA ADDRESS **/
405 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST ILLEGAL DATA ADDRESS:\n");
406
407 /* The mapping begins at the defined addresses and ends at address +
408 * nb_points so these addresses are not valid. */
409
410 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, 0, 1, tab_rp_bits);
411 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_bits (0): ");
412 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
413
414 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`! + 1, tab_rp_bits);
415 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_bits (max): ");
416 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
417
418 rc = modbus_read_input_bitscxx . . $ modbus_read_input_bits(11f4da49ebe31404).(ctx, 0, 1, tab_rp_bits);
419 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_bits (0): ");
420 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
421
422 rc = modbus_read_input_bitscxx . . $ modbus_read_input_bits(11f4da49ebe31404).(
423 ctx, UT_INPUT_BITS_ADDRESScxx . . $ UT_INPUT_BITS_ADDRESS., UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB. + 1, tab_rp_bits);
424 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_bits (max): ");
425 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
426
427 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(ctx, 0, 1, tab_rp_registers);
428 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_registers (0): ");
429 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
430
431 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
432 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX. + 1, tab_rp_registers);
433 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_registers (max): ");
434 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
435
436 rc = modbus_read_input_registerscxx . . $ modbus_read_input_registers(e941881bef6bc78c).(ctx, 0, 1, tab_rp_registers);
437 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_registers (0): ");
438 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
439
440 rc = modbus_read_input_registerscxx . . $ modbus_read_input_registers(e941881bef6bc78c).(
441 ctx, UT_INPUT_REGISTERS_ADDRESScxx . . $ UT_INPUT_REGISTERS_ADDRESS., UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB. + 1, tab_rp_registers);
442 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_registers (max): ");
443 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
444
445 rc = modbus_write_bitcxx . . $ modbus_write_bit(16de6ae3c67ca311).(ctx, 0, ONcxx . . $ `src/modbus.h:59:9`!);
446 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bit (0): ");
447 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
448
449 rc = modbus_write_bitcxx . . $ modbus_write_bit(16de6ae3c67ca311).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS. + UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, ONcxx . . $ `src/modbus.h:59:9`!);
450 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bit (max): ");
451 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
452
453 rc = modbus_write_bitscxx . . $ modbus_write_bits(90e367cb830b4f97).(ctx, 0, 1, tab_rp_bits);
454 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bits (0): ");
455 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
456
457 rc = modbus_write_bitscxx . . $ modbus_write_bits(90e367cb830b4f97).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS. + UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!, tab_rp_bits);
458 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bits (max): ");
459 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
460
461 rc = modbus_write_registercxx . . $ modbus_write_register(146febe49439e9d3).(ctx, 0, tab_rp_registers[0]);
462 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_register (0): ");
463 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
464
465 rc = modbus_write_registercxx . . $ modbus_write_register(146febe49439e9d3).(
466 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX., tab_rp_registers[0]);
467 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_register (max): ");
468 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
469
470 rc = modbus_write_registerscxx . . $ modbus_write_registers(1ccebf9f3d033562).(ctx, 0, 1, tab_rp_registers);
471 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_registers (0): ");
472 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
473
474 rc = modbus_write_registerscxx . . $ modbus_write_registers(1ccebf9f3d033562).(ctx,
475 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX.,
476 UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.,
477 tab_rp_registers);
478 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_registers (max): ");
479 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
480
481 rc = modbus_mask_write_registercxx . . $ modbus_mask_write_register(ba42a0470b5e623e).(ctx, 0, 0xF2, 0x25);
482 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_mask_write_registers (0): ");
483 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
484
485 rc = modbus_mask_write_registercxx . . $ modbus_mask_write_register(ba42a0470b5e623e).(
486 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX., 0xF2, 0x25);
487 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_mask_write_registers (max): ");
488 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
489
490 rc = modbus_write_and_read_registerscxx . . $ modbus_write_and_read_registers(c6a0b9119112ae8e).(
491 ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers);
492 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_and_read_registers (0): ");
493 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
494
495 rc = modbus_write_and_read_registerscxx . . $ modbus_write_and_read_registers(c6a0b9119112ae8e).(ctx,
496 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX.,
497 UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.,
498 tab_rp_registers,
499 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. + UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX.,
500 UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB.,
501 tab_rp_registers);
502 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_and_read_registers (max): ");
503 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILADDcxx . . $ `src/modbus.h:135:9`!, "");
504
505 /** TOO MANY DATA **/
506 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST TOO MANY DATA ERROR:\n");
507
508 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! + 1, tab_rp_bits);
509 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_bits: ");
510 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
511
512 rc = modbus_read_input_bitscxx . . $ modbus_read_input_bits(11f4da49ebe31404).(
513 ctx, UT_INPUT_BITS_ADDRESScxx . . $ UT_INPUT_BITS_ADDRESS., MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! + 1, tab_rp_bits);
514 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_bits: ");
515 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
516
517 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
518 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! + 1, tab_rp_registers);
519 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_registers: ");
520 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
521
522 rc = modbus_read_input_registerscxx . . $ modbus_read_input_registers(e941881bef6bc78c).(
523 ctx, UT_INPUT_REGISTERS_ADDRESScxx . . $ UT_INPUT_REGISTERS_ADDRESS., MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! + 1, tab_rp_registers);
524 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_input_registers: ");
525 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
526
527 rc = modbus_write_bitscxx . . $ modbus_write_bits(90e367cb830b4f97).(ctx, UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS., MODBUS_MAX_WRITE_BITScxx . . $ `src/modbus.h:84:9`! + 1, tab_rp_bits);
528 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bits: ");
529 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
530
531 rc = modbus_write_registerscxx . . $ modbus_write_registers(1ccebf9f3d033562).(
532 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., MODBUS_MAX_WRITE_REGISTERScxx . . $ `src/modbus.h:94:9`! + 1, tab_rp_registers);
533 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_registers: ");
534 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
535
536 /** SLAVE ADDRESS **/
537 old_slave = modbus_get_slavecxx . . $ modbus_get_slave(c57adc5f48380e39).(ctx);
538
539 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST SLAVE ADDRESS:\n");
540
541 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 Not compliant slave address is refused: ");
542 rc = modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, 248);
543 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1, "Slave address of 248 shouldn't be allowed");
544
545 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 Not compliant slave address is allowed: ");
546 modbus_enable_quirkscxx . . $ modbus_enable_quirks(6e391a97c669bd3b).(ctx, MODBUS_QUIRK_MAX_SLAVEcxx . . $ MODBUS_QUIRK_MAX_SLAVE.);
547 rc = modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, 248);
548 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 0, "Not compliant slave address should have been accepted");
549
550 modbus_disable_quirkscxx . . $ modbus_disable_quirks(6e391a97c669bd3b).(ctx, MODBUS_QUIRK_MAX_SLAVEcxx . . $ MODBUS_QUIRK_MAX_SLAVE.);
551 rc = modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, old_slave);
552 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 0, "Unable to restore slave value")
553
554 /** BAD USE OF REPLY FUNCTION **/
555 rc = modbus_write_bitcxx . . $ modbus_write_bit(16de6ae3c67ca311).(ctx, UT_BITS_ADDRESS_INVALID_REQUEST_LENGTHcxx . . $ UT_BITS_ADDRESS_INVALID_REQUEST_LENGTH., ONcxx . . $ `src/modbus.h:59:9`!);
556 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_bit (triggers invalid reply): ");
557 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
558
559 rc = modbus_write_registercxx . . $ modbus_write_register(146febe49439e9d3).(ctx, UT_REGISTERS_ADDRESS_SPECIALcxx . . $ UT_REGISTERS_ADDRESS_SPECIAL., 0x42);
560 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_write_register (triggers invalid reply): ");
561 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXILVALcxx . . $ `src/modbus.h:136:9`!, "");
562
563 /** SLAVE REPLY **/
564
565 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST SLAVE REPLY:\n");
566 modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!);
567 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
568 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
569 if (use_backend == RTUcxx . . $ RTU.) {
570 const int RAW_REQ_LENGTH = 6;
571 uint8_tcxx . . $ uint8_t# raw_req[] = {INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!, 0x03, 0x00, 0x01, 0x01, 0x01};
572 /* Too many points */
573 uint8_tcxx . . $ uint8_t# raw_invalid_req[] = {INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!, 0x03, 0x00, 0x01, 0xFF, 0xFF};
574 const int RAW_RSP_LENGTH = 7;
575 uint8_tcxx . . $ uint8_t# raw_rsp[] = {INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!, 0x03, 0x04, 0, 0, 0, 0};
576 uint8_tcxx . . $ uint8_t# rsp[MODBUS_RTU_MAX_ADU_LENGTHcxx . . $ `src/modbus-rtu.h:17:9`!];
577
578 /* No response in RTU mode */
579 printfcxx . . $ printf(581fddd72a8db8fc).("1-A/3 No response from slave %d: ", INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!);
580 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
581
582 /* The slave raises a timeout on a confirmation to ignore because if an
583 * indication for another slave is received, a confirmation must follow */
584
585 /* Send a pair of indication/confirmation to the slave with a different
586 * slave ID to simulate a communication on a RS485 bus. At first, the
587 * slave will see the indication message then the confirmation, and it must
588 * ignore both. */
589 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_tcxx . . $ uint8_t#));
590 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_tcxx . . $ uint8_t#));
591 rc = modbus_receive_confirmationcxx . . $ modbus_receive_confirmation(744a031d86f39170).(ctx, rsp);
592
593 printfcxx . . $ printf(581fddd72a8db8fc).("1-B/3 No response from slave %d on indication/confirmation messages: ",
594 INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!);
595 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
596
597 /* Send an INVALID request for another slave */
598 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_tcxx . . $ uint8_t#));
599 rc = modbus_receive_confirmationcxx . . $ modbus_receive_confirmation(744a031d86f39170).(ctx, rsp);
600
601 printfcxx . . $ printf(581fddd72a8db8fc).("1-C/3 No response from slave %d with invalid request: ",
602 INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!);
603 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
604
605 rc = modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, MODBUS_BROADCAST_ADDRESScxx . . $ `src/modbus.h:76:9`!);
606 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 0, "Invalid broadcast address");
607
608 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
609 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
610 printfcxx . . $ printf(581fddd72a8db8fc).("2/3 No reply after a broadcast query: ");
611 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
612 } else {
613 /* Response in TCP mode */
614 printfcxx . . $ printf(581fddd72a8db8fc).("1/3 Response from slave %d: ", INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`!);
615 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_REGISTERS_NB, "");
616
617 rc = modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, MODBUS_BROADCAST_ADDRESScxx . . $ `src/modbus.h:76:9`!);
618 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 0, "Invalid broacast address");
619
620 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
621 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
622 printfcxx . . $ printf(581fddd72a8db8fc).("2/3 Reply after a query with unit id == 0: ");
623 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == UT_REGISTERS_NB, "");
624 }
625
626 /* Restore slave */
627 modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, old_slave);
628
629 printfcxx . . $ printf(581fddd72a8db8fc).("3/3 Response with an invalid TID or slave: ");
630 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
631 ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVEcxx . . $ UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE., 1, tab_rp_registers);
632 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1, "");
633
634 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 Report slave ID truncated: \n");
635 /* Set a marker to ensure limit is respected */
636 tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
637 rc = modbus_report_slave_idcxx . . $ modbus_report_slave_id(a57d72a08e870a11).(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
638 /* Return the size required (response size) but respects the defined limit */
639 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
640 "Return is rc %d (%d) and marker is %d (42)",
641 rc,
642 NB_REPORT_SLAVE_ID,
643 tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
644
645 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 Report slave ID: \n");
646 /* tab_rp_bits is used to store bytes */
647 rc = modbus_report_slave_idcxx . . $ modbus_report_slave_id(a57d72a08e870a11).(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
648 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == NB_REPORT_SLAVE_ID, "");
649
650 /* Slave ID is an arbitrary number for libmodbus */
651 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc > 0, "");
652
653 /* Run status indicator is ON */
654 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc > 1 && tab_rp_bits[1] == 0xFF, "");
655
656 /* Print additional data as string */
657 if (rc > 2) {
658 printfcxx . . $ printf(581fddd72a8db8fc).("Additional data: ");
659 for (i = 2; i < rc; i++) {
660 printfcxx . . $ printf(581fddd72a8db8fc).("%c", tab_rp_bits[i]);
661 }
662 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
663 }
664
665 /* Save original timeout */
666 modbus_get_response_timeoutcxx . . $ modbus_get_response_timeout(536e4a54166c71db).(ctx, &old_response_to_sec, &old_response_to_usec);
667 modbus_get_byte_timeoutcxx . . $ modbus_get_byte_timeout(536e4a54166c71db).(ctx, &old_byte_to_sec, &old_byte_to_usec);
668
669 rc = modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 0);
670 printfcxx . . $ printf(581fddd72a8db8fc).("1/8 Invalid response timeout (zero): ");
671 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, "");
672
673 rc = modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 1000000);
674 printfcxx . . $ printf(581fddd72a8db8fc).("2/8 Invalid response timeout (too large us): ");
675 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, "");
676
677 rc = modbus_set_byte_timeoutcxx . . $ modbus_set_byte_timeout(a50f7c93d24cb0e).(ctx, 0, 1000000);
678 printfcxx . . $ printf(581fddd72a8db8fc).("3/8 Invalid byte timeout (too large us): ");
679 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, "");
680
681 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 1);
682 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
683 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
684 printfcxx . . $ printf(581fddd72a8db8fc).("4/8 1us response timeout: ");
685 if (rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!) {
686 printfcxx . . $ printf(581fddd72a8db8fc).("OK\n");
687 } else {
688 printfcxx . . $ printf(581fddd72a8db8fc).("FAILED (can fail on some platforms)\n");
689 }
690
691 /* A wait and flush operation is done by the error recovery code of
692 * libmodbus but after a sleep of current response timeout
693 * so 0 can be too short!
694 */
695 usleepcxx . . $ usleep(4c679297bf94b9eb).(old_response_to_sec * 1000000 + old_response_to_usec);
696 modbus_flushcxx . . $ modbus_flush(c57adc5f48380e39).(ctx);
697
698 /* Trigger a special behaviour on server to wait for 0.5 second before
699 * replying whereas allowed timeout is 0.2 second */
700 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 200000);
701 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
702 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MScxx . . $ UT_REGISTERS_ADDRESS_SLEEP_500_MS., 1, tab_rp_registers);
703 printfcxx . . $ printf(581fddd72a8db8fc).("5/8 Too short response timeout (0.2s < 0.5s): ");
704 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
705
706 /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
707 usleepcxx . . $ usleep(4c679297bf94b9eb).(400000);
708 modbus_flushcxx . . $ modbus_flush(c57adc5f48380e39).(ctx);
709
710 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 600000);
711 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
712 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MScxx . . $ UT_REGISTERS_ADDRESS_SLEEP_500_MS., 1, tab_rp_registers);
713 printfcxx . . $ printf(581fddd72a8db8fc).("6/8 Adequate response timeout (0.6s > 0.5s): ");
714 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "");
715
716 /* Disable the byte timeout.
717 The full response must be available in the 600ms interval */
718 modbus_set_byte_timeoutcxx . . $ modbus_set_byte_timeout(a50f7c93d24cb0e).(ctx, 0, 0);
719 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
720 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MScxx . . $ UT_REGISTERS_ADDRESS_SLEEP_500_MS., 1, tab_rp_registers);
721 printfcxx . . $ printf(581fddd72a8db8fc).("7/8 Disable byte timeout: ");
722 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "");
723
724 // Invalid in TCP or RTU mode...
725 modbus_tcxx . . $ modbus_t# *invalid_ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).("1.2.3.4", 1502);
726 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(invalid_ctx, 0, 1);
727 rc = modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(invalid_ctx);
728 printfcxx . . $ printf(581fddd72a8db8fc).("8/8 Connection timeout: ");
•729 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENETUNREACHcxx . . $ `<external>/17922322188510340118/errno.h:85:9`!), "");
730 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(invalid_ctx);
731
732 /* Restore original response timeout */
733 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, old_response_to_sec, old_response_to_usec);
734
735 if (use_backend == TCPcxx . . $ TCP.) {
736 /* The test server is only able to test byte timeouts with the TCP
737 * backend */
738
739 /* Timeout of 3ms between bytes */
740 modbus_set_byte_timeoutcxx . . $ modbus_set_byte_timeout(a50f7c93d24cb0e).(ctx, 0, 3000);
741 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
742 ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MScxx . . $ UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS., 1, tab_rp_registers);
743 printfcxx . . $ printf(581fddd72a8db8fc).("1/2 Too small byte timeout (3ms < 5ms): ");
744 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!, "");
745
746 /* Wait remaining bytes before flushing */
747 usleepcxx . . $ usleep(4c679297bf94b9eb).(11 * 5000);
748 modbus_flushcxx . . $ modbus_flush(c57adc5f48380e39).(ctx);
749
750 /* Timeout of 7ms between bytes */
751 modbus_set_byte_timeoutcxx . . $ modbus_set_byte_timeout(a50f7c93d24cb0e).(ctx, 0, 7000);
752 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
753 ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MScxx . . $ UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS., 1, tab_rp_registers);
754 printfcxx . . $ printf(581fddd72a8db8fc).("2/2 Adapted byte timeout (7ms > 5ms): ");
755 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == 1, "");
756 }
757
758 /* Restore original byte timeout */
759 modbus_set_byte_timeoutcxx . . $ modbus_set_byte_timeout(a50f7c93d24cb0e).(ctx, old_byte_to_sec, old_byte_to_usec);
760
761 /** BAD RESPONSE **/
762 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST BAD RESPONSE ERROR:\n");
763
764 /* Allocate only the required space */
765 tab_rp_registers_bad =
766 (uint16_tcxx . . $ uint16_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(UT_REGISTERS_NB_SPECIALcxx . . $ UT_REGISTERS_NB_SPECIAL. * sizeof(uint16_tcxx . . $ uint16_t#));
767
768 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
769 ctx, UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS., UT_REGISTERS_NB_SPECIALcxx . . $ UT_REGISTERS_NB_SPECIAL., tab_rp_registers_bad);
770 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_registers: ");
771 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBBADDATAcxx . . $ `src/modbus.h:147:9`!, "");
772 freecxx . . $ free(5d22bdacc48458e8).(tab_rp_registers_bad);
773
774 /** MANUAL EXCEPTION **/
775 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST MANUAL EXCEPTION:\n");
776 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(
777 ctx, UT_REGISTERS_ADDRESS_SPECIALcxx . . $ UT_REGISTERS_ADDRESS_SPECIAL., UT_REGISTERS_NBcxx . . $ UT_REGISTERS_NB., tab_rp_registers);
778
779 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_read_registers at special address: ");
780 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBXSBUSYcxx . . $ `src/modbus.h:139:9`!, "");
781
782 /** Run a few tests to challenge the server code **/
783 if (test_servercxx . . $ test_server(2a7e0e283938d456).(ctx, use_backend) == -1) {
784 goto close;
785 }
786
787 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx);
788 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
789 ctx = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
790
791 /* Test init functions */
792 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST INVALID INITIALIZATION:\n");
793 ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 1, 'A', 0, 0);
794 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(ctx == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, "");
795
796 ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).("/dev/dummy", 0, 'A', 0, 0);
797 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(ctx == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, "");
798
799 printfcxx . . $ printf(581fddd72a8db8fc).("\nALL TESTS PASS WITH SUCCESS.\n");
800 success = TRUEcxx . . $ `src/modbus.h:51:9`!;
801
802 close:
803 /* Free the memory */
804 freecxx . . $ free(5d22bdacc48458e8).(tab_rp_bits);
805 freecxx . . $ free(5d22bdacc48458e8).(tab_rp_registers);
806
807 /* Close the connection */
808 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx);
809 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
810
811 return (success) ? 0 : -1;
812 }
813
814 /* Send crafted requests to test server resilience
815 and ensure proper exceptions are returned. */
816 int test_servercxx . . $ test_server(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int use_backend)
817 {
818 int rc;
819 int i;
820 /* Read requests */
821 const int READ_RAW_REQ_LEN = 6;
822 const int slave = (use_backend == RTUcxx . . $ RTU.) ? SERVER_IDcxx . . $ `tests/unit-test.h:28:9`! : MODBUS_TCP_SLAVEcxx . . $ `src/modbus-tcp.h:35:9`!;
823 uint8_tcxx . . $ uint8_t# read_raw_req[] = {slave,
824 /* function, address, 5 values */
825 MODBUS_FC_READ_HOLDING_REGISTERScxx . . $ `src/modbus.h:65:9`!,
826 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. >> 8,
827 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. & 0xFF,
828 0x0,
829 0x05};
830 /* Write and read registers request */
831 const int RW_RAW_REQ_LEN = 13;
832 uint8_tcxx . . $ uint8_t# rw_raw_req[] = {slave,
833 /* function, addr to read, nb to read */
834 MODBUS_FC_WRITE_AND_READ_REGISTERScxx . . $ `src/modbus.h:74:9`!,
835 /* Read */
836 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. >> 8,
837 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. & 0xFF,
838 (MODBUS_MAX_WR_READ_REGISTERScxx . . $ `src/modbus.h:96:9`! + 1) >> 8,
839 (MODBUS_MAX_WR_READ_REGISTERScxx . . $ `src/modbus.h:96:9`! + 1) & 0xFF,
840 /* Write */
841 0,
842 0,
843 0,
844 1,
845 /* Write byte count */
846 1 * 2,
847 /* One data to write... */
848 0x12,
849 0x34};
850 const int WRITE_RAW_REQ_LEN = 13;
851 uint8_tcxx . . $ uint8_t# write_raw_req[] = {slave,
852 /* function will be set in the loop */
853 MODBUS_FC_WRITE_MULTIPLE_REGISTERScxx . . $ `src/modbus.h:71:9`!,
854 /* Address */
855 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. >> 8,
856 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS. & 0xFF,
857 /* 3 values, 6 bytes */
858 0x00,
859 0x03,
860 0x06,
861 /* Dummy data to write */
862 0x02,
863 0x2B,
864 0x00,
865 0x01,
866 0x00,
867 0x64};
868 const int INVALID_FC = 0x42;
869 const int INVALID_FC_REQ_LEN = 6;
870 uint8_tcxx . . $ uint8_t# invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00};
871
872 int req_length;
873 uint8_tcxx . . $ uint8_t# rsp[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!];
874 int tab_read_function[] = {MODBUS_FC_READ_COILScxx . . $ `src/modbus.h:63:9`!,
875 MODBUS_FC_READ_DISCRETE_INPUTScxx . . $ `src/modbus.h:64:9`!,
876 MODBUS_FC_READ_HOLDING_REGISTERScxx . . $ `src/modbus.h:65:9`!,
877 MODBUS_FC_READ_INPUT_REGISTERScxx . . $ `src/modbus.h:66:9`!};
878 int tab_read_nb_max[] = {MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! + 1,
879 MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! + 1,
880 MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! + 1,
881 MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! + 1};
882 int backend_length;
883 int backend_offset;
884
885 if (use_backend == RTUcxx . . $ RTU.) {
886 backend_length = 3;
887 backend_offset = 1;
888 } else {
889 backend_length = 7;
890 backend_offset = 7;
891 }
892
893 printfcxx . . $ printf(581fddd72a8db8fc).("\nTEST RAW REQUESTS:\n");
894
895 uint32_tcxx . . $ uint32_t# old_response_to_sec;
896 uint32_tcxx . . $ uint32_t# old_response_to_usec;
897
898 /* This requests can generate flushes server side so we need a higher
899 * response timeout than the server. The server uses the defined response
900 * timeout to sleep before flushing.
901 * The old timeouts are restored at the end.
902 */
903 modbus_get_response_timeoutcxx . . $ modbus_get_response_timeout(536e4a54166c71db).(ctx, &old_response_to_sec, &old_response_to_usec);
904 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, 0, 600000);
905
906 int old_s = modbus_get_socketcxx . . $ modbus_get_socket(c57adc5f48380e39).(ctx);
907 modbus_set_socketcxx . . $ modbus_set_socket(2a7e0e283938d456).(ctx, -1);
908 rc = modbus_receivecxx . . $ modbus_receive(744a031d86f39170).(ctx, rsp);
909 modbus_set_socketcxx . . $ modbus_set_socket(2a7e0e283938d456).(ctx, old_s);
910 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_receive with invalid socket: ");
911 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == -1, "FAILED (%d)\n", rc);
912
913 req_length = modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, read_raw_req, READ_RAW_REQ_LEN);
914 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_send_raw_request: ");
915 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
916
917 printfcxx . . $ printf(581fddd72a8db8fc).("* modbus_receive_confirmation: ");
918 rc = modbus_receive_confirmationcxx . . $ modbus_receive_confirmation(744a031d86f39170).(ctx, rsp);
919 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == (backend_length + 12), "FAILED (%d)\n", rc);
920
921 /* Try to read more values than a response could hold for all data
922 types. */
923 for (i = 0; i < 4; i++) {
924 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
925 tab_read_function[i],
926 read_raw_req,
927 READ_RAW_REQ_LEN,
928 tab_read_nb_max[i],
929 0,
930 backend_length,
931 backend_offset);
932 if (rc == -1)
933 goto close;
934 }
935
936 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
937 MODBUS_FC_WRITE_AND_READ_REGISTERScxx . . $ `src/modbus.h:74:9`!,
938 rw_raw_req,
939 RW_RAW_REQ_LEN,
940 MODBUS_MAX_WR_READ_REGISTERScxx . . $ `src/modbus.h:96:9`! + 1,
941 0,
942 backend_length,
943 backend_offset);
944 if (rc == -1)
945 goto close;
946
947 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
948 MODBUS_FC_WRITE_MULTIPLE_REGISTERScxx . . $ `src/modbus.h:71:9`!,
949 write_raw_req,
950 WRITE_RAW_REQ_LEN,
951 MODBUS_MAX_WRITE_REGISTERScxx . . $ `src/modbus.h:94:9`! + 1,
952 6,
953 backend_length,
954 backend_offset);
955 if (rc == -1)
956 goto close;
957
958 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
959 MODBUS_FC_WRITE_MULTIPLE_COILScxx . . $ `src/modbus.h:70:9`!,
960 write_raw_req,
961 WRITE_RAW_REQ_LEN,
962 MODBUS_MAX_WRITE_BITScxx . . $ `src/modbus.h:84:9`! + 1,
963 6,
964 backend_length,
965 backend_offset);
966 if (rc == -1)
967 goto close;
968
969 /* Modbus write multiple registers with large number of values but a set a
970 small number of bytes in requests (not nb * 2 as usual). */
971 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
972 MODBUS_FC_WRITE_MULTIPLE_REGISTERScxx . . $ `src/modbus.h:71:9`!,
973 write_raw_req,
974 WRITE_RAW_REQ_LEN,
975 MODBUS_MAX_WRITE_REGISTERScxx . . $ `src/modbus.h:94:9`!,
976 6,
977 backend_length,
978 backend_offset);
979 if (rc == -1)
980 goto close;
981
982 rc = send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(ctx,
983 MODBUS_FC_WRITE_MULTIPLE_COILScxx . . $ `src/modbus.h:70:9`!,
984 write_raw_req,
985 WRITE_RAW_REQ_LEN,
986 MODBUS_MAX_WRITE_BITScxx . . $ `src/modbus.h:84:9`!,
987 6,
988 backend_length,
989 backend_offset);
990 if (rc == -1)
991 goto close;
992
993 /* Test invalid function code */
994 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(
995 ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_tcxx . . $ uint8_t#));
996 rc = modbus_receive_confirmationcxx . . $ modbus_receive_confirmation(744a031d86f39170).(ctx, rsp);
997 printfcxx . . $ printf(581fddd72a8db8fc).("Return an exception on unknown function code: ");
998 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == (backend_length + EXCEPTION_RC) &&
999 rsp[backend_offset] == (0x80 + INVALID_FC),
1000 "")
1001
1002 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, old_response_to_sec, old_response_to_usec);
1003 return 0;
1004 close:
1005 modbus_set_response_timeoutcxx . . $ modbus_set_response_timeout(a50f7c93d24cb0e).(ctx, old_response_to_sec, old_response_to_usec);
1006 return -1;
1007 }
1008
1009 int send_crafted_requestcxx . . $ send_crafted_request(2434da53dbbcfcc0).(modbus_tcxx . . $ modbus_t# *ctx,
1010 int function,
1011 uint8_tcxx . . $ uint8_t# *req,
1012 int req_len,
1013 uint16_tcxx . . $ uint16_t# max_value,
1014 uint16_tcxx . . $ uint16_t# bytes,
1015 int backend_length,
1016 int backend_offset)
1017 {
1018 uint8_tcxx . . $ uint8_t# rsp[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!];
1019 int j;
1020
1021 for (j = 0; j < 2; j++) {
1022 int rc;
1023
1024 req[1] = function;
1025 if (j == 0) {
1026 /* Try to read or write zero values on first iteration */
1027 req[4] = 0x00;
1028 req[5] = 0x00;
1029 if (bytes) {
1030 /* Write query */
1031 req[6] = 0x00;
1032 }
1033 } else {
1034 /* Try to read or write max values + 1 on second iteration */
1035 req[4] = (max_value >> 8) & 0xFF;
1036 req[5] = max_value & 0xFF;
1037 if (bytes) {
1038 /* Write query (nb values * 2 to convert in bytes for registers) */
1039 req[6] = bytes;
1040 }
1041 }
1042
1043 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, req, req_len * sizeof(uint8_tcxx . . $ uint8_t#));
1044 if (j == 0) {
1045 printfcxx . . $ printf(581fddd72a8db8fc).(
1046 "* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read");
1047 } else {
1048 printfcxx . . $ printf(581fddd72a8db8fc).("* try function 0x%X: %s %d values: ",
1049 function,
1050 bytes ? "write" : "read",
1051 max_value);
1052 }
1053 rc = modbus_receive_confirmationcxx . . $ modbus_receive_confirmation(744a031d86f39170).(ctx, rsp);
1054 ASSERT_TRUEcxx . . $ `tests/unit-test-client.c:40:9`!(rc == (backend_length + EXCEPTION_RC) &&
1055 rsp[backend_offset] == (0x80 + function) &&
1056 rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
1057 "");
1058 }
1059 return 0;
1060 close:
1061 return -1;
1062 }