Link against socket and nsl libs when building on SunOS
[libmodbus.git] / tests / unit-test-client.c
bloba4417665484dedb86282bde4614ad773a5d0285a
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <errno.h>
8 #include <modbus.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include "unit-test.h"
16 const int EXCEPTION_RC = 2;
18 enum {
19 TCP,
20 TCP_PI,
21 RTU
24 int test_server(modbus_t *ctx, int use_backend);
25 int send_crafted_request(modbus_t *ctx,
26 int function,
27 uint8_t *req,
28 int req_size,
29 uint16_t max_value,
30 uint16_t bytes,
31 int backend_length,
32 int backend_offset);
33 int equal_dword(uint16_t *tab_reg, const uint32_t value);
34 int is_memory_equal(const void *s1, const void *s2, size_t size);
36 #define BUG_REPORT(_cond, _format, _args...) \
37 printf( \
38 "\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args)
40 #define ASSERT_TRUE(_cond, _format, __args...) \
41 { \
42 if (_cond) { \
43 printf("OK\n"); \
44 } else { \
45 BUG_REPORT(_cond, _format, ##__args); \
46 goto close; \
47 } \
50 int is_memory_equal(const void *s1, const void *s2, size_t size)
52 return (memcmp(s1, s2, size) == 0);
55 int equal_dword(uint16_t *tab_reg, const uint32_t value)
57 return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF)));
60 int main(int argc, char *argv[])
62 /* Length of report slave ID response slave ID + ON/OFF + 'LMB' + version */
63 const int NB_REPORT_SLAVE_ID = 2 + 3 + strlen(LIBMODBUS_VERSION_STRING);
64 uint8_t *tab_rp_bits = NULL;
65 uint16_t *tab_rp_registers = NULL;
66 uint16_t *tab_rp_registers_bad = NULL;
67 modbus_t *ctx = NULL;
68 int i;
69 uint8_t value;
70 int nb_points;
71 int rc;
72 float real;
73 uint32_t old_response_to_sec;
74 uint32_t old_response_to_usec;
75 uint32_t new_response_to_sec;
76 uint32_t new_response_to_usec;
77 uint32_t old_byte_to_sec;
78 uint32_t old_byte_to_usec;
79 int use_backend;
80 int success = FALSE;
81 int old_slave;
82 char *ip_or_device;
84 if (argc > 1) {
85 if (strcmp(argv[1], "tcp") == 0) {
86 use_backend = TCP;
87 } else if (strcmp(argv[1], "tcppi") == 0) {
88 use_backend = TCP_PI;
89 } else if (strcmp(argv[1], "rtu") == 0) {
90 use_backend = RTU;
91 } else {
92 printf("Modbus client for unit testing\n");
93 printf("Usage:\n %s [tcp|tcppi|rtu]\n", argv[0]);
94 printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB1\n\n");
95 exit(1);
97 } else {
98 /* By default */
99 use_backend = TCP;
102 if (argc > 2) {
103 ip_or_device = argv[2];
104 } else {
105 switch (use_backend) {
106 case TCP:
107 ip_or_device = "127.0.0.1";
108 break;
109 case TCP_PI:
110 ip_or_device = "::1";
111 break;
112 case RTU:
113 ip_or_device = "/dev/ttyUSB1";
114 break;
115 default:
116 break;
120 if (use_backend == TCP) {
121 ctx = modbus_new_tcp(ip_or_device, 1502);
122 } else if (use_backend == TCP_PI) {
123 ctx = modbus_new_tcp_pi(ip_or_device, "1502");
124 } else {
125 ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1);
127 if (ctx == NULL) {
128 fprintf(stderr, "Unable to allocate libmodbus context\n");
129 return -1;
132 modbus_set_debug(ctx, TRUE);
133 modbus_set_error_recovery(
134 ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
136 if (use_backend == RTU) {
137 modbus_set_slave(ctx, SERVER_ID);
140 modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
141 if (modbus_connect(ctx) == -1) {
142 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
143 modbus_free(ctx);
144 return -1;
147 /* Allocate and initialize the memory to store the bits */
148 nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
149 tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
150 memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
152 /* Allocate and initialize the memory to store the registers */
153 nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
154 : UT_INPUT_REGISTERS_NB;
155 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
156 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
158 printf("** UNIT TESTING **\n");
160 printf("1/1 No response timeout modification on connect: ");
161 modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
162 ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
163 old_response_to_usec == new_response_to_usec,
164 "");
166 printf("\nTEST WRITE/READ:\n");
168 /** COIL BITS **/
170 /* Single */
171 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
172 printf("1/2 modbus_write_bit: ");
173 ASSERT_TRUE(rc == 1, "");
175 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
176 printf("2/2 modbus_read_bits: ");
177 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
178 ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
180 /* End single */
182 /* Multiple bits */
184 uint8_t tab_value[UT_BITS_NB];
186 modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
187 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_value);
188 printf("1/2 modbus_write_bits: ");
189 ASSERT_TRUE(rc == UT_BITS_NB, "");
192 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
193 printf("2/2 modbus_read_bits: ");
194 ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc);
196 i = 0;
197 nb_points = UT_BITS_NB;
198 while (nb_points > 0) {
199 int nb_bits = (nb_points > 8) ? 8 : nb_points;
201 value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits);
202 ASSERT_TRUE(
203 value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
205 nb_points -= nb_bits;
206 i++;
208 printf("OK\n");
209 /* End of multiple bits */
211 /** DISCRETE INPUTS **/
212 rc =
213 modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, tab_rp_bits);
214 printf("1/1 modbus_read_input_bits: ");
215 ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
217 i = 0;
218 nb_points = 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_bits(tab_rp_bits, i * 8, nb_bits);
222 ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i],
223 "FAILED (%0X != %0X)\n",
224 value,
225 UT_INPUT_BITS_TAB[i]);
227 nb_points -= nb_bits;
228 i++;
230 printf("OK\n");
232 /** HOLDING REGISTERS **/
234 /* Single register */
235 rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
236 printf("1/2 modbus_write_register: ");
237 ASSERT_TRUE(rc == 1, "");
239 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
240 printf("2/2 modbus_read_registers: ");
241 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
242 ASSERT_TRUE(tab_rp_registers[0] == 0x1234,
243 "FAILED (%0X != %0X)\n",
244 tab_rp_registers[0],
245 0x1234);
246 /* End of single register */
248 /* Many registers */
249 rc = modbus_write_registers(
250 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, UT_REGISTERS_TAB);
251 printf("1/5 modbus_write_registers: ");
252 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
254 rc = modbus_read_registers(
255 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
256 printf("2/5 modbus_read_registers: ");
257 ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
259 for (i = 0; i < UT_REGISTERS_NB; i++) {
260 ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
261 "FAILED (%0X != %0X)\n",
262 tab_rp_registers[i],
263 UT_REGISTERS_TAB[i]);
266 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 0, tab_rp_registers);
267 printf("3/5 modbus_read_registers (0): ");
268 ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
270 nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
271 : UT_INPUT_REGISTERS_NB;
272 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
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_registers(ctx,
278 UT_REGISTERS_ADDRESS + 1,
279 UT_REGISTERS_NB - 1,
280 tab_rp_registers,
281 UT_REGISTERS_ADDRESS,
282 UT_REGISTERS_NB,
283 tab_rp_registers);
284 printf("4/5 modbus_write_and_read_registers: ");
285 ASSERT_TRUE(
286 rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
288 ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
289 "FAILED (%0X != %0X)\n",
290 tab_rp_registers[0],
291 UT_REGISTERS_TAB[0]);
293 for (i = 1; i < UT_REGISTERS_NB; i++) {
294 ASSERT_TRUE(
295 tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers[i], 0);
298 /* End of many registers */
300 /** INPUT REGISTERS **/
301 rc = modbus_read_input_registers(
302 ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB, tab_rp_registers);
303 printf("1/1 modbus_read_input_registers: ");
304 ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
306 for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
307 ASSERT_TRUE(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]);
313 /* MASKS */
314 printf("1/1 Write mask: ");
315 rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x12);
316 rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
317 ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
318 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
319 ASSERT_TRUE(
320 tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17);
322 printf("\nTEST FLOATS\n");
323 /** FLOAT **/
324 printf("1/4 Set/get float ABCD: ");
325 modbus_set_float_abcd(UT_REAL, tab_rp_registers);
326 ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD_SET, 4),
327 "FAILED Set float ABCD");
328 real = modbus_get_float_abcd(UT_IREAL_ABCD_GET);
329 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
331 printf("2/4 Set/get float DCBA: ");
332 modbus_set_float_dcba(UT_REAL, tab_rp_registers);
333 ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA_SET, 4),
334 "FAILED Set float DCBA");
335 real = modbus_get_float_dcba(UT_IREAL_DCBA_GET);
336 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
338 printf("3/4 Set/get float BADC: ");
339 modbus_set_float_badc(UT_REAL, tab_rp_registers);
340 ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC_SET, 4),
341 "FAILED Set float BADC");
342 real = modbus_get_float_badc(UT_IREAL_BADC_GET);
343 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
345 printf("4/4 Set/get float CDAB: ");
346 modbus_set_float_cdab(UT_REAL, tab_rp_registers);
347 ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB_SET, 4),
348 "FAILED Set float CDAB");
349 real = modbus_get_float_cdab(UT_IREAL_CDAB_GET);
350 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
352 printf("\nAt this point, error messages doesn't mean the test has failed\n");
354 /** ILLEGAL DATA ADDRESS **/
355 printf("\nTEST ILLEGAL DATA ADDRESS:\n");
357 /* The mapping begins at the defined addresses and ends at address +
358 * nb_points so these addresses are not valid. */
360 rc = modbus_read_bits(ctx, 0, 1, tab_rp_bits);
361 printf("* modbus_read_bits (0): ");
362 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
364 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits);
365 printf("* modbus_read_bits (max): ");
366 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
368 rc = modbus_read_input_bits(ctx, 0, 1, tab_rp_bits);
369 printf("* modbus_read_input_bits (0): ");
370 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
372 rc = modbus_read_input_bits(
373 ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB + 1, tab_rp_bits);
374 printf("* modbus_read_input_bits (max): ");
375 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
377 rc = modbus_read_registers(ctx, 0, 1, tab_rp_registers);
378 printf("* modbus_read_registers (0): ");
379 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
381 rc = modbus_read_registers(
382 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX + 1, tab_rp_registers);
383 printf("* modbus_read_registers (max): ");
384 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
386 rc = modbus_read_input_registers(ctx, 0, 1, tab_rp_registers);
387 printf("* modbus_read_input_registers (0): ");
388 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
390 rc = modbus_read_input_registers(
391 ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB + 1, tab_rp_registers);
392 printf("* modbus_read_input_registers (max): ");
393 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
395 rc = modbus_write_bit(ctx, 0, ON);
396 printf("* modbus_write_bit (0): ");
397 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
399 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
400 printf("* modbus_write_bit (max): ");
401 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
403 rc = modbus_write_bits(ctx, 0, 1, tab_rp_bits);
404 printf("* modbus_write_coils (0): ");
405 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
407 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, UT_BITS_NB, tab_rp_bits);
408 printf("* modbus_write_coils (max): ");
409 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
411 rc = modbus_write_register(ctx, 0, tab_rp_registers[0]);
412 printf("* modbus_write_register (0): ");
413 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
415 rc = modbus_write_register(
416 ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, tab_rp_registers[0]);
417 printf("* modbus_write_register (max): ");
418 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
420 rc = modbus_write_registers(ctx, 0, 1, tab_rp_registers);
421 printf("* modbus_write_registers (0): ");
422 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
424 rc = modbus_write_registers(ctx,
425 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
426 UT_REGISTERS_NB,
427 tab_rp_registers);
428 printf("* modbus_write_registers (max): ");
429 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
431 rc = modbus_mask_write_register(ctx, 0, 0xF2, 0x25);
432 printf("* modbus_mask_write_registers (0): ");
433 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
435 rc = modbus_mask_write_register(
436 ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 0xF2, 0x25);
437 printf("* modbus_mask_write_registers (max): ");
438 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
440 rc = modbus_write_and_read_registers(
441 ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers);
442 printf("* modbus_write_and_read_registers (0): ");
443 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
445 rc = modbus_write_and_read_registers(ctx,
446 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
447 UT_REGISTERS_NB,
448 tab_rp_registers,
449 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
450 UT_REGISTERS_NB,
451 tab_rp_registers);
452 printf("* modbus_write_and_read_registers (max): ");
453 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
455 /** TOO MANY DATA **/
456 printf("\nTEST TOO MANY DATA ERROR:\n");
458 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
459 printf("* modbus_read_bits: ");
460 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
462 rc = modbus_read_input_bits(
463 ctx, UT_INPUT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
464 printf("* modbus_read_input_bits: ");
465 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
467 rc = modbus_read_registers(
468 ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
469 printf("* modbus_read_registers: ");
470 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
472 rc = modbus_read_input_registers(
473 ctx, UT_INPUT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
474 printf("* modbus_read_input_registers: ");
475 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
477 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
478 printf("* modbus_write_bits: ");
479 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
481 rc = modbus_write_registers(
482 ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_WRITE_REGISTERS + 1, tab_rp_registers);
483 printf("* modbus_write_registers: ");
484 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
486 /** SLAVE ADDRESS **/
487 old_slave = modbus_get_slave(ctx);
489 printf("\nTEST SLAVE ADDRESS:\n");
491 printf("1/2 Not compliant slave address is refused: ");
492 rc = modbus_set_slave(ctx, 248);
493 ASSERT_TRUE(rc == -1, "Slave address of 248 shouldn't be allowed");
495 printf("2/2 Not compliant slave address is allowed: ");
496 modbus_enable_quirks(ctx, MODBUS_QUIRK_MAX_SLAVE);
497 rc = modbus_set_slave(ctx, 248);
498 ASSERT_TRUE(rc == 0, "Not compliant slave address should have been accepted");
500 modbus_disable_quirks(ctx, MODBUS_QUIRK_MAX_SLAVE);
501 rc = modbus_set_slave(ctx, old_slave);
502 ASSERT_TRUE(rc == 0, "Uanble to restore slave value")
504 /** SLAVE REPLY **/
506 printf("\nTEST SLAVE REPLY:\n");
507 modbus_set_slave(ctx, INVALID_SERVER_ID);
508 rc = modbus_read_registers(
509 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
510 if (use_backend == RTU) {
511 const int RAW_REQ_LENGTH = 6;
512 uint8_t raw_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01};
513 /* Too many points */
514 uint8_t raw_invalid_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF};
515 const int RAW_RSP_LENGTH = 7;
516 uint8_t raw_rsp[] = {INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0};
517 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
519 /* No response in RTU mode */
520 printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
521 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
523 /* The slave raises a timeout on a confirmation to ignore because if an
524 * indication for another slave is received, a confirmation must follow */
526 /* Send a pair of indication/confirmation to the slave with a different
527 * slave ID to simulate a communication on a RS485 bus. At first, the
528 * slave will see the indication message then the confirmation, and it must
529 * ignore both. */
530 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
531 modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t));
532 rc = modbus_receive_confirmation(ctx, rsp);
534 printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
535 INVALID_SERVER_ID);
536 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
538 /* Send an INVALID request for another slave */
539 modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
540 rc = modbus_receive_confirmation(ctx, rsp);
542 printf("1-C/3 No response from slave %d with invalid request: ",
543 INVALID_SERVER_ID);
544 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
546 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
547 ASSERT_TRUE(rc == 0, "Invalid broadcast address");
549 rc = modbus_read_registers(
550 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
551 printf("2/3 No reply after a broadcast query: ");
552 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
553 } else {
554 /* Response in TCP mode */
555 printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
556 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
558 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
559 ASSERT_TRUE(rc == 0, "Invalid broacast address");
561 rc = modbus_read_registers(
562 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
563 printf("2/3 Reply after a query with unit id == 0: ");
564 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
567 /* Restore slave */
568 modbus_set_slave(ctx, old_slave);
570 printf("3/3 Response with an invalid TID or slave: ");
571 rc = modbus_read_registers(
572 ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 1, tab_rp_registers);
573 ASSERT_TRUE(rc == -1, "");
575 printf("1/2 Report slave ID truncated: \n");
576 /* Set a marker to ensure limit is respected */
577 tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
578 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
579 /* Return the size required (response size) but respects the defined limit */
580 ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
581 "Return is rc %d (%d) and marker is %d (42)",
583 NB_REPORT_SLAVE_ID,
584 tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
586 printf("2/2 Report slave ID: \n");
587 /* tab_rp_bits is used to store bytes */
588 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
589 ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, "");
591 /* Slave ID is an arbitrary number for libmodbus */
592 ASSERT_TRUE(rc > 0, "");
594 /* Run status indicator is ON */
595 ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, "");
597 /* Print additional data as string */
598 if (rc > 2) {
599 printf("Additional data: ");
600 for (i = 2; i < rc; i++) {
601 printf("%c", tab_rp_bits[i]);
603 printf("\n");
606 /* Save original timeout */
607 modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
608 modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec);
610 rc = modbus_set_response_timeout(ctx, 0, 0);
611 printf("1/8 Invalid response timeout (zero): ");
612 ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
614 rc = modbus_set_response_timeout(ctx, 0, 1000000);
615 printf("2/8 Invalid response timeout (too large us): ");
616 ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
618 rc = modbus_set_byte_timeout(ctx, 0, 1000000);
619 printf("3/8 Invalid byte timeout (too large us): ");
620 ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
622 modbus_set_response_timeout(ctx, 0, 1);
623 rc = modbus_read_registers(
624 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
625 printf("4/8 1us response timeout: ");
626 if (rc == -1 && errno == ETIMEDOUT) {
627 printf("OK\n");
628 } else {
629 printf("FAILED (can fail on some platforms)\n");
632 /* A wait and flush operation is done by the error recovery code of
633 * libmodbus but after a sleep of current response timeout
634 * so 0 can be too short!
636 usleep(old_response_to_sec * 1000000 + old_response_to_usec);
637 modbus_flush(ctx);
639 /* Trigger a special behaviour on server to wait for 0.5 second before
640 * replying whereas allowed timeout is 0.2 second */
641 modbus_set_response_timeout(ctx, 0, 200000);
642 rc = modbus_read_registers(
643 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
644 printf("5/8 Too short response timeout (0.2s < 0.5s): ");
645 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
647 /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
648 usleep(400000);
649 modbus_flush(ctx);
651 modbus_set_response_timeout(ctx, 0, 600000);
652 rc = modbus_read_registers(
653 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
654 printf("6/8 Adequate response timeout (0.6s > 0.5s): ");
655 ASSERT_TRUE(rc == 1, "");
657 /* Disable the byte timeout.
658 The full response must be available in the 600ms interval */
659 modbus_set_byte_timeout(ctx, 0, 0);
660 rc = modbus_read_registers(
661 ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
662 printf("7/8 Disable byte timeout: ");
663 ASSERT_TRUE(rc == 1, "");
665 // Invalid in TCP or RTU mode...
666 modbus_t *invalid_ctx = modbus_new_tcp("1.2.3.4", 1502);
667 modbus_set_response_timeout(ctx, 0, 1);
668 rc = modbus_connect(invalid_ctx);
669 printf("8/8 Connection timeout: ");
670 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
671 modbus_free(invalid_ctx);
673 /* Restore original response timeout */
674 modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
676 if (use_backend == TCP) {
677 /* The test server is only able to test byte timeouts with the TCP
678 * backend */
680 /* Timeout of 3ms between bytes */
681 modbus_set_byte_timeout(ctx, 0, 3000);
682 rc = modbus_read_registers(
683 ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
684 printf("1/2 Too small byte timeout (3ms < 5ms): ");
685 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
687 /* Wait remaining bytes before flushing */
688 usleep(11 * 5000);
689 modbus_flush(ctx);
691 /* Timeout of 7ms between bytes */
692 modbus_set_byte_timeout(ctx, 0, 7000);
693 rc = modbus_read_registers(
694 ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
695 printf("2/2 Adapted byte timeout (7ms > 5ms): ");
696 ASSERT_TRUE(rc == 1, "");
699 /* Restore original byte timeout */
700 modbus_set_byte_timeout(ctx, old_byte_to_sec, old_byte_to_usec);
702 /** BAD RESPONSE **/
703 printf("\nTEST BAD RESPONSE ERROR:\n");
705 /* Allocate only the required space */
706 tab_rp_registers_bad =
707 (uint16_t *) malloc(UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
709 rc = modbus_read_registers(
710 ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
711 printf("* modbus_read_registers: ");
712 ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
713 free(tab_rp_registers_bad);
715 /** MANUAL EXCEPTION **/
716 printf("\nTEST MANUAL EXCEPTION:\n");
717 rc = modbus_read_registers(
718 ctx, UT_REGISTERS_ADDRESS_SPECIAL, UT_REGISTERS_NB, tab_rp_registers);
720 printf("* modbus_read_registers at special address: ");
721 ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
723 /** Run a few tests to challenge the server code **/
724 if (test_server(ctx, use_backend) == -1) {
725 goto close;
728 modbus_close(ctx);
729 modbus_free(ctx);
730 ctx = NULL;
732 /* Test init functions */
733 printf("\nTEST INVALID INITIALIZATION:\n");
734 ctx = modbus_new_rtu(NULL, 1, 'A', 0, 0);
735 ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
737 ctx = modbus_new_rtu("/dev/dummy", 0, 'A', 0, 0);
738 ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
740 printf("\nALL TESTS PASS WITH SUCCESS.\n");
741 success = TRUE;
743 close:
744 /* Free the memory */
745 free(tab_rp_bits);
746 free(tab_rp_registers);
748 /* Close the connection */
749 modbus_close(ctx);
750 modbus_free(ctx);
752 return (success) ? 0 : -1;
755 /* Send crafted requests to test server resilience
756 and ensure proper exceptions are returned. */
757 int test_server(modbus_t *ctx, int use_backend)
759 int rc;
760 int i;
761 /* Read requests */
762 const int READ_RAW_REQ_LEN = 6;
763 const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE;
764 uint8_t read_raw_req[] = {slave,
765 /* function, address, 5 values */
766 MODBUS_FC_READ_HOLDING_REGISTERS,
767 UT_REGISTERS_ADDRESS >> 8,
768 UT_REGISTERS_ADDRESS & 0xFF,
769 0x0,
770 0x05};
771 /* Write and read registers request */
772 const int RW_RAW_REQ_LEN = 13;
773 uint8_t rw_raw_req[] = {slave,
774 /* function, addr to read, nb to read */
775 MODBUS_FC_WRITE_AND_READ_REGISTERS,
776 /* Read */
777 UT_REGISTERS_ADDRESS >> 8,
778 UT_REGISTERS_ADDRESS & 0xFF,
779 (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
780 (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
781 /* Write */
786 /* Write byte count */
787 1 * 2,
788 /* One data to write... */
789 0x12,
790 0x34};
791 const int WRITE_RAW_REQ_LEN = 13;
792 uint8_t write_raw_req[] = {slave,
793 /* function will be set in the loop */
794 MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
795 /* Address */
796 UT_REGISTERS_ADDRESS >> 8,
797 UT_REGISTERS_ADDRESS & 0xFF,
798 /* 3 values, 6 bytes */
799 0x00,
800 0x03,
801 0x06,
802 /* Dummy data to write */
803 0x02,
804 0x2B,
805 0x00,
806 0x01,
807 0x00,
808 0x64};
809 const int INVALID_FC = 0x42;
810 const int INVALID_FC_REQ_LEN = 6;
811 uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00};
813 int req_length;
814 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
815 int tab_read_function[] = {MODBUS_FC_READ_COILS,
816 MODBUS_FC_READ_DISCRETE_INPUTS,
817 MODBUS_FC_READ_HOLDING_REGISTERS,
818 MODBUS_FC_READ_INPUT_REGISTERS};
819 int tab_read_nb_max[] = {MODBUS_MAX_READ_BITS + 1,
820 MODBUS_MAX_READ_BITS + 1,
821 MODBUS_MAX_READ_REGISTERS + 1,
822 MODBUS_MAX_READ_REGISTERS + 1};
823 int backend_length;
824 int backend_offset;
826 if (use_backend == RTU) {
827 backend_length = 3;
828 backend_offset = 1;
829 } else {
830 backend_length = 7;
831 backend_offset = 7;
834 printf("\nTEST RAW REQUESTS:\n");
836 uint32_t old_response_to_sec;
837 uint32_t old_response_to_usec;
839 /* This requests can generate flushes server side so we need a higher
840 * response timeout than the server. The server uses the defined response
841 * timeout to sleep before flushing.
842 * The old timeouts are restored at the end.
844 modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
845 modbus_set_response_timeout(ctx, 0, 600000);
847 int old_s = modbus_get_socket(ctx);
848 modbus_set_socket(ctx, -1);
849 rc = modbus_receive(ctx, rsp);
850 modbus_set_socket(ctx, old_s);
851 printf("* modbus_receive with invalid socket: ");
852 ASSERT_TRUE(rc == -1, "FAILED (%d)\n", rc);
854 req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
855 printf("* modbus_send_raw_request: ");
856 ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
858 printf("* modbus_receive_confirmation: ");
859 rc = modbus_receive_confirmation(ctx, rsp);
860 ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc);
862 /* Try to read more values than a response could hold for all data
863 types. */
864 for (i = 0; i < 4; i++) {
865 rc = send_crafted_request(ctx,
866 tab_read_function[i],
867 read_raw_req,
868 READ_RAW_REQ_LEN,
869 tab_read_nb_max[i],
871 backend_length,
872 backend_offset);
873 if (rc == -1)
874 goto close;
877 rc = send_crafted_request(ctx,
878 MODBUS_FC_WRITE_AND_READ_REGISTERS,
879 rw_raw_req,
880 RW_RAW_REQ_LEN,
881 MODBUS_MAX_WR_READ_REGISTERS + 1,
883 backend_length,
884 backend_offset);
885 if (rc == -1)
886 goto close;
888 rc = send_crafted_request(ctx,
889 MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
890 write_raw_req,
891 WRITE_RAW_REQ_LEN,
892 MODBUS_MAX_WRITE_REGISTERS + 1,
894 backend_length,
895 backend_offset);
896 if (rc == -1)
897 goto close;
899 rc = send_crafted_request(ctx,
900 MODBUS_FC_WRITE_MULTIPLE_COILS,
901 write_raw_req,
902 WRITE_RAW_REQ_LEN,
903 MODBUS_MAX_WRITE_BITS + 1,
905 backend_length,
906 backend_offset);
907 if (rc == -1)
908 goto close;
910 /* Modbus write multiple registers with large number of values but a set a
911 small number of bytes in requests (not nb * 2 as usual). */
912 rc = send_crafted_request(ctx,
913 MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
914 write_raw_req,
915 WRITE_RAW_REQ_LEN,
916 MODBUS_MAX_WRITE_REGISTERS,
918 backend_length,
919 backend_offset);
920 if (rc == -1)
921 goto close;
923 rc = send_crafted_request(ctx,
924 MODBUS_FC_WRITE_MULTIPLE_COILS,
925 write_raw_req,
926 WRITE_RAW_REQ_LEN,
927 MODBUS_MAX_WRITE_BITS,
929 backend_length,
930 backend_offset);
931 if (rc == -1)
932 goto close;
934 /* Test invalid function code */
935 modbus_send_raw_request(
936 ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
937 rc = modbus_receive_confirmation(ctx, rsp);
938 printf("Return an exception on unknown function code: ");
939 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
940 rsp[backend_offset] == (0x80 + INVALID_FC),
943 modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
944 return 0;
945 close:
946 modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
947 return -1;
950 int send_crafted_request(modbus_t *ctx,
951 int function,
952 uint8_t *req,
953 int req_len,
954 uint16_t max_value,
955 uint16_t bytes,
956 int backend_length,
957 int backend_offset)
959 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
960 int j;
962 for (j = 0; j < 2; j++) {
963 int rc;
965 req[1] = function;
966 if (j == 0) {
967 /* Try to read or write zero values on first iteration */
968 req[4] = 0x00;
969 req[5] = 0x00;
970 if (bytes) {
971 /* Write query */
972 req[6] = 0x00;
974 } else {
975 /* Try to read or write max values + 1 on second iteration */
976 req[4] = (max_value >> 8) & 0xFF;
977 req[5] = max_value & 0xFF;
978 if (bytes) {
979 /* Write query (nb values * 2 to convert in bytes for registers) */
980 req[6] = bytes;
984 modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
985 if (j == 0) {
986 printf(
987 "* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read");
988 } else {
989 printf("* try function 0x%X: %s %d values: ",
990 function,
991 bytes ? "write" : "read",
992 max_value);
994 rc = modbus_receive_confirmation(ctx, rsp);
995 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
996 rsp[backend_offset] == (0x80 + function) &&
997 rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
998 "");
1000 return 0;
1001 close:
1002 return -1;