Commit bb6be03c110cba2cabf0d0087a34b1aed3c1a1a4

Authored by Stéphane Raimbault
1 parent 55bd5054

Convenient assert macro for unit testing

It's never too late to remove 261 lines of code ;)
Showing 1 changed file with 105 additions and 368 deletions
tests/unit-test-client.c
... ... @@ -32,6 +32,18 @@ enum {
32 32  
33 33 int test_raw_request(modbus_t *, int);
34 34  
  35 +#define BUG_REPORT(_cond, _format, _args ...) \
  36 + printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args)
  37 +
  38 +#define ASSERT_TRUE(_cond, _format, __args...) { \
  39 + if (_cond) { \
  40 + printf("OK\n"); \
  41 + } else { \
  42 + BUG_REPORT(_cond, _format, ## __args); \
  43 + goto close; \
  44 + } \
  45 +};
  46 +
35 47 int main(int argc, char *argv[])
36 48 {
37 49 const int NB_REPORT_SLAVE_ID = 10;
... ... @@ -100,13 +112,8 @@ int main(int argc, char *argv[])
100 112 printf("** UNIT TESTING **\n");
101 113  
102 114 printf("1/1 No response timeout modification on connect: ");
103   - if (old_response_to_sec == new_response_to_sec &&
104   - old_response_to_usec == new_response_to_usec) {
105   - printf("OK\n");
106   - } else {
107   - printf("FAILED\n");
108   - goto close;
109   - }
  115 + ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
  116 + old_response_to_usec == new_response_to_usec, "");
110 117  
111 118 /* Allocate and initialize the memory to store the bits */
112 119 nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
... ... @@ -126,25 +133,14 @@ int main(int argc, char *argv[])
126 133 /* Single */
127 134 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
128 135 printf("1/2 modbus_write_bit: ");
129   - if (rc == 1) {
130   - printf("OK\n");
131   - } else {
132   - printf("FAILED\n");
133   - goto close;
134   - }
  136 + ASSERT_TRUE(rc == 1, "");
135 137  
136 138 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
137 139 printf("2/2 modbus_read_bits: ");
138   - if (rc != 1) {
139   - printf("FAILED (nb points %d)\n", rc);
140   - goto close;
141   - }
  140 + ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  141 + ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n",
  142 + tab_rp_bits[0], ON);
142 143  
143   - if (tab_rp_bits[0] != ON) {
144   - printf("FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
145   - goto close;
146   - }
147   - printf("OK\n");
148 144 /* End single */
149 145  
150 146 /* Multiple bits */
... ... @@ -155,20 +151,12 @@ int main(int argc, char *argv[])
155 151 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
156 152 UT_BITS_NB, tab_value);
157 153 printf("1/2 modbus_write_bits: ");
158   - if (rc == UT_BITS_NB) {
159   - printf("OK\n");
160   - } else {
161   - printf("FAILED\n");
162   - goto close;
163   - }
  154 + ASSERT_TRUE(rc == UT_BITS_NB, "");
164 155 }
165 156  
166 157 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
167 158 printf("2/2 modbus_read_bits: ");
168   - if (rc != UT_BITS_NB) {
169   - printf("FAILED (nb points %d)\n", rc);
170   - goto close;
171   - }
  159 + ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc);
172 160  
173 161 i = 0;
174 162 nb_points = UT_BITS_NB;
... ... @@ -176,10 +164,8 @@ int main(int argc, char *argv[])
176 164 int nb_bits = (nb_points > 8) ? 8 : nb_points;
177 165  
178 166 value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
179   - if (value != UT_BITS_TAB[i]) {
180   - printf("FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
181   - goto close;
182   - }
  167 + ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  168 + value, UT_BITS_TAB[i]);
183 169  
184 170 nb_points -= nb_bits;
185 171 i++;
... ... @@ -191,22 +177,15 @@ int main(int argc, char *argv[])
191 177 rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
192 178 UT_INPUT_BITS_NB, tab_rp_bits);
193 179 printf("1/1 modbus_read_input_bits: ");
194   -
195   - if (rc != UT_INPUT_BITS_NB) {
196   - printf("FAILED (nb points %d)\n", rc);
197   - goto close;
198   - }
  180 + ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
199 181  
200 182 i = 0;
201 183 nb_points = UT_INPUT_BITS_NB;
202 184 while (nb_points > 0) {
203 185 int nb_bits = (nb_points > 8) ? 8 : nb_points;
204   -
205 186 value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
206   - if (value != UT_INPUT_BITS_TAB[i]) {
207   - printf("FAILED (%0X != %0X)\n", value, UT_INPUT_BITS_TAB[i]);
208   - goto close;
209   - }
  187 + ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  188 + value, UT_INPUT_BITS_TAB[i]);
210 189  
211 190 nb_points -= nb_bits;
212 191 i++;
... ... @@ -218,66 +197,38 @@ int main(int argc, char *argv[])
218 197 /* Single register */
219 198 rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
220 199 printf("1/2 modbus_write_register: ");
221   - if (rc == 1) {
222   - printf("OK\n");
223   - } else {
224   - printf("FAILED\n");
225   - goto close;
226   - }
  200 + ASSERT_TRUE(rc == 1, "");
227 201  
228 202 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
229 203 1, tab_rp_registers);
230 204 printf("2/2 modbus_read_registers: ");
231   - if (rc != 1) {
232   - printf("FAILED (nb points %d)\n", rc);
233   - goto close;
234   - }
  205 + ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  206 + ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n",
  207 + tab_rp_registers[0], 0x1234);
235 208  
236   - if (tab_rp_registers[0] != 0x1234) {
237   - printf("FAILED (%0X != %0X)\n",
238   - tab_rp_registers[0], 0x1234);
239   - goto close;
240   - }
241   - printf("OK\n");
242 209 /* End of single register */
243 210  
244 211 /* Many registers */
245 212 rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
246 213 UT_REGISTERS_NB, UT_REGISTERS_TAB);
247 214 printf("1/5 modbus_write_registers: ");
248   - if (rc == UT_REGISTERS_NB) {
249   - printf("OK\n");
250   - } else {
251   - printf("FAILED\n");
252   - goto close;
253   - }
  215 + ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
254 216  
255 217 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
256 218 UT_REGISTERS_NB, tab_rp_registers);
257 219 printf("2/5 modbus_read_registers: ");
258   - if (rc != UT_REGISTERS_NB) {
259   - printf("FAILED (nb points %d)\n", rc);
260   - goto close;
261   - }
  220 + ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
262 221  
263 222 for (i=0; i < UT_REGISTERS_NB; i++) {
264   - if (tab_rp_registers[i] != UT_REGISTERS_TAB[i]) {
265   - printf("FAILED (%0X != %0X)\n",
266   - tab_rp_registers[i],
267   - UT_REGISTERS_TAB[i]);
268   - goto close;
269   - }
  223 + ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
  224 + "FAILED (%0X != %0X)\n",
  225 + tab_rp_registers[i], UT_REGISTERS_TAB[i]);
270 226 }
271   - printf("OK\n");
272 227  
273 228 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
274 229 0, tab_rp_registers);
275 230 printf("3/5 modbus_read_registers (0): ");
276   - if (rc != -1) {
277   - printf("FAILED (nb_points %d)\n", rc);
278   - goto close;
279   - }
280   - printf("OK\n");
  231 + ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
281 232  
282 233 nb_points = (UT_REGISTERS_NB >
283 234 UT_INPUT_REGISTERS_NB) ?
... ... @@ -295,24 +246,17 @@ int main(int argc, char *argv[])
295 246 UT_REGISTERS_NB,
296 247 tab_rp_registers);
297 248 printf("4/5 modbus_write_and_read_registers: ");
298   - if (rc != UT_REGISTERS_NB) {
299   - printf("FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
300   - goto close;
301   - }
  249 + ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n",
  250 + rc, UT_REGISTERS_NB);
302 251  
303   - if (tab_rp_registers[0] != UT_REGISTERS_TAB[0]) {
304   - printf("FAILED (%0X != %0X)\n",
305   - tab_rp_registers[0], UT_REGISTERS_TAB[0]);
306   - }
  252 + ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
  253 + "FAILED (%0X != %0X)\n",
  254 + tab_rp_registers[0], UT_REGISTERS_TAB[0]);
307 255  
308 256 for (i=1; i < UT_REGISTERS_NB; i++) {
309   - if (tab_rp_registers[i] != 0) {
310   - printf("FAILED (%0X != %0X)\n",
311   - tab_rp_registers[i], 0);
312   - goto close;
313   - }
  257 + ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n",
  258 + tab_rp_registers[i], 0);
314 259 }
315   - printf("OK\n");
316 260  
317 261 /* End of many registers */
318 262  
... ... @@ -322,19 +266,13 @@ int main(int argc, char *argv[])
322 266 UT_INPUT_REGISTERS_NB,
323 267 tab_rp_registers);
324 268 printf("1/1 modbus_read_input_registers: ");
325   - if (rc != UT_INPUT_REGISTERS_NB) {
326   - printf("FAILED (nb points %d)\n", rc);
327   - goto close;
328   - }
  269 + ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
329 270  
330 271 for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
331   - if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
332   - printf("FAILED (%0X != %0X)\n",
333   - tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
334   - goto close;
335   - }
  272 + ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
  273 + "FAILED (%0X != %0X)\n",
  274 + tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
336 275 }
337   - printf("OK\n");
338 276  
339 277 printf("\nTEST FLOATS\n");
340 278 /** FLOAT **/
... ... @@ -354,33 +292,20 @@ int main(int argc, char *argv[])
354 292  
355 293 printf("2/4 Get float: ");
356 294 real = modbus_get_float(tab_rp_registers);
357   - if (real == UT_REAL) {
358   - printf("OK\n");
359   - } else {
360   - printf("FAILED (%f != %f)\n", real, UT_REAL);
361   - goto close;
362   - }
  295 + ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
363 296  
364 297 printf("3/4 Set float in DBCA order: ");
365 298 modbus_set_float_dcba(UT_REAL, tab_rp_registers);
366   - if (tab_rp_registers[1] == (UT_IREAL_DCBA >> 16) &&
367   - tab_rp_registers[0] == (UT_IREAL_DCBA & 0xFFFF)) {
368   - printf("OK\n");
369   - } else {
370   - ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF;
371   - ireal |= (uint32_t) tab_rp_registers[1] << 16;
372   - printf("FAILED (%x != %x)\n", ireal, UT_IREAL_DCBA);
373   - goto close;
374   - }
  299 + ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF;
  300 + ireal |= (uint32_t) tab_rp_registers[1] << 16;
  301 + ASSERT_TRUE(tab_rp_registers[1] == (UT_IREAL_DCBA >> 16) &&
  302 + tab_rp_registers[0] == (UT_IREAL_DCBA & 0xFFFF),
  303 + "FAILED (%x != %x)\n", ireal, UT_IREAL_DCBA);
375 304  
376 305 printf("4/4 Get float in DCBA order: ");
377 306 real = modbus_get_float_dcba(tab_rp_registers);
378   - if (real == UT_REAL) {
379   - printf("OK\n");
380   - } else {
381   - printf("FAILED (%f != %f)\n", real, UT_REAL);
382   - goto close;
383   - }
  307 + ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  308 +
384 309 printf("\nAt this point, error messages doesn't mean the test has failed\n");
385 310  
386 311 /** ILLEGAL DATA ADDRESS **/
... ... @@ -389,75 +314,39 @@ int main(int argc, char *argv[])
389 314 /* The mapping begins at 0 and ends at address + nb_points so
390 315 * the addresses are not valid. */
391 316  
392   - rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
393   - UT_BITS_NB + 1, tab_rp_bits);
  317 + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits);
394 318 printf("* modbus_read_bits: ");
395   - if (rc == -1 && errno == EMBXILADD) {
396   - printf("OK\n");
397   - } else {
398   - printf("FAILED\n");
399   - goto close;
400   - }
  319 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
401 320  
402 321 rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
403 322 UT_INPUT_BITS_NB + 1, tab_rp_bits);
404 323 printf("* modbus_read_input_bits: ");
405   - if (rc == -1 && errno == EMBXILADD)
406   - printf("OK\n");
407   - else {
408   - printf("FAILED\n");
409   - goto close;
410   - }
  324 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
411 325  
412 326 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
413 327 UT_REGISTERS_NB + 1, tab_rp_registers);
414 328 printf("* modbus_read_registers: ");
415   - if (rc == -1 && errno == EMBXILADD)
416   - printf("OK\n");
417   - else {
418   - printf("FAILED\n");
419   - goto close;
420   - }
  329 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
421 330  
422 331 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
423 332 UT_INPUT_REGISTERS_NB + 1,
424 333 tab_rp_registers);
425 334 printf("* modbus_read_input_registers: ");
426   - if (rc == -1 && errno == EMBXILADD)
427   - printf("OK\n");
428   - else {
429   - printf("FAILED\n");
430   - goto close;
431   - }
  335 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
432 336  
433 337 rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
434 338 printf("* modbus_write_bit: ");
435   - if (rc == -1 && errno == EMBXILADD) {
436   - printf("OK\n");
437   - } else {
438   - printf("FAILED\n");
439   - goto close;
440   - }
  339 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
441 340  
442 341 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
443 342 UT_BITS_NB, tab_rp_bits);
444 343 printf("* modbus_write_coils: ");
445   - if (rc == -1 && errno == EMBXILADD) {
446   - printf("OK\n");
447   - } else {
448   - printf("FAILED\n");
449   - goto close;
450   - }
  344 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
451 345  
452 346 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
453 347 UT_REGISTERS_NB, tab_rp_registers);
454 348 printf("* modbus_write_registers: ");
455   - if (rc == -1 && errno == EMBXILADD) {
456   - printf("OK\n");
457   - } else {
458   - printf("FAILED\n");
459   - goto close;
460   - }
  349 + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
461 350  
462 351 /** TOO MANY DATA **/
463 352 printf("\nTEST TOO MANY DATA ERROR:\n");
... ... @@ -465,65 +354,35 @@ int main(int argc, char *argv[])
465 354 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
466 355 MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
467 356 printf("* modbus_read_bits: ");
468   - if (rc == -1 && errno == EMBMDATA) {
469   - printf("OK\n");
470   - } else {
471   - printf("FAILED\n");
472   - goto close;
473   - }
  357 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
474 358  
475 359 rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
476 360 MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
477 361 printf("* modbus_read_input_bits: ");
478   - if (rc == -1 && errno == EMBMDATA) {
479   - printf("OK\n");
480   - } else {
481   - printf("FAILED\n");
482   - goto close;
483   - }
  362 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
484 363  
485 364 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
486 365 MODBUS_MAX_READ_REGISTERS + 1,
487 366 tab_rp_registers);
488 367 printf("* modbus_read_registers: ");
489   - if (rc == -1 && errno == EMBMDATA) {
490   - printf("OK\n");
491   - } else {
492   - printf("FAILED\n");
493   - goto close;
494   - }
  368 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
495 369  
496 370 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
497 371 MODBUS_MAX_READ_REGISTERS + 1,
498 372 tab_rp_registers);
499 373 printf("* modbus_read_input_registers: ");
500   - if (rc == -1 && errno == EMBMDATA) {
501   - printf("OK\n");
502   - } else {
503   - printf("FAILED\n");
504   - goto close;
505   - }
  374 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
506 375  
507 376 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
508 377 MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
509 378 printf("* modbus_write_bits: ");
510   - if (rc == -1 && errno == EMBMDATA) {
511   - printf("OK\n");
512   - } else {
513   - goto close;
514   - printf("FAILED\n");
515   - }
  379 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
516 380  
517 381 rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
518 382 MODBUS_MAX_WRITE_REGISTERS + 1,
519 383 tab_rp_registers);
520 384 printf("* modbus_write_registers: ");
521   - if (rc == -1 && errno == EMBMDATA) {
522   - printf("OK\n");
523   - } else {
524   - printf("FAILED\n");
525   - goto close;
526   - }
  385 + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
527 386  
528 387 /** SLAVE REPLY **/
529 388 printf("\nTEST SLAVE REPLY:\n");
... ... @@ -541,13 +400,7 @@ int main(int argc, char *argv[])
541 400  
542 401 /* No response in RTU mode */
543 402 printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
544   -
545   - if (rc == -1 && errno == ETIMEDOUT) {
546   - printf("OK\n");
547   - } else {
548   - printf("FAILED\n");
549   - goto close;
550   - }
  403 + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
551 404  
552 405 /* The slave raises a timeout on a confirmation to ignore because if an
553 406 * indication for another slave is received, a confirmation must follow */
... ... @@ -563,13 +416,7 @@ int main(int argc, char *argv[])
563 416  
564 417 printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
565 418 INVALID_SERVER_ID);
566   -
567   - if (rc == -1 && errno == ETIMEDOUT) {
568   - printf("OK\n");
569   - } else {
570   - printf("FAILED (%d)\n", rc);
571   - goto close;
572   - }
  419 + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
573 420  
574 421 /* Send an INVALID request for another slave */
575 422 modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
... ... @@ -577,40 +424,20 @@ int main(int argc, char *argv[])
577 424  
578 425 printf("1-C/3 No response from slave %d with invalid request: ",
579 426 INVALID_SERVER_ID);
580   -
581   - if (rc == -1 && errno == ETIMEDOUT) {
582   - printf("OK\n");
583   - } else {
584   - printf("FAILED (%d)\n", rc);
585   - goto close;
586   - }
  427 + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
587 428 } else {
588 429 /* Response in TCP mode */
589 430 printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
590   -
591   - if (rc == UT_REGISTERS_NB) {
592   - printf("OK\n");
593   - } else {
594   - printf("FAILED\n");
595   - goto close;
596   - }
  431 + ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
597 432 }
598 433  
599 434 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
600   - if (rc == -1) {
601   - printf("Invalid broacast address\n");
602   - goto close;
603   - }
  435 + ASSERT_TRUE(rc != -1, "Invalid broacast address");
604 436  
605 437 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
606 438 UT_REGISTERS_NB, tab_rp_registers);
607 439 printf("2/3 Reply after a broadcast query: ");
608   - if (rc == UT_REGISTERS_NB) {
609   - printf("OK\n");
610   - } else {
611   - printf("FAILED\n");
612   - goto close;
613   - }
  440 + ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
614 441  
615 442 /* Restore slave */
616 443 if (use_backend == RTU) {
... ... @@ -622,45 +449,28 @@ int main(int argc, char *argv[])
622 449 printf("3/3 Response with an invalid TID or slave: ");
623 450 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
624 451 1, tab_rp_registers);
625   - if (rc == -1) {
626   - printf("OK\n");
627   - } else {
628   - printf("FAILED\n");
629   - goto close;
630   - }
  452 + ASSERT_TRUE(rc == -1, "");
631 453  
632 454 printf("1/2 Report slave ID truncated: \n");
633 455 /* Set a marker to ensure limit is respected */
634 456 tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
635 457 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
636   - if (rc != NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] != 42) {
637   - printf("FAILED\n");
638   - goto close;
639   - }
  458 + /* Return the size required (response size) but respects the defined limit */
  459 + ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&
  460 + tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
  461 + "Return is rc %d (%d) and marker is %d (42)",
  462 + rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
640 463  
641 464 printf("2/2 Report slave ID: \n");
642 465 /* tab_rp_bits is used to store bytes */
643 466 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
644   - if (rc != NB_REPORT_SLAVE_ID) {
645   - printf("FAILED\n");
646   - goto close;
647   - }
  467 + ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, "");
648 468  
649 469 /* Slave ID is an arbitraty number for libmodbus */
650   - if (rc > 0) {
651   - printf("OK Slave ID is %d\n", tab_rp_bits[0]);
652   - } else {
653   - printf("FAILED\n");
654   - goto close;
655   - }
  470 + ASSERT_TRUE(rc > 0, "");
656 471  
657   - /* Run status indicator */
658   - if (rc > 1 && tab_rp_bits[1] == 0xFF) {
659   - printf("OK Run Status Indicator is %s\n", tab_rp_bits[1] ? "ON" : "OFF");
660   - } else {
661   - printf("FAILED\n");
662   - goto close;
663   - }
  472 + /* Run status indicator is ON */
  473 + ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, "");
664 474  
665 475 /* Print additional data as string */
666 476 if (rc > 2) {
... ... @@ -677,30 +487,15 @@ int main(int argc, char *argv[])
677 487  
678 488 rc = modbus_set_response_timeout(ctx, 0, 0);
679 489 printf("1/6 Invalid response timeout (zero): ");
680   - if (rc == -1 && errno == EINVAL) {
681   - printf("OK\n");
682   - } else {
683   - printf("FAILED\n");
684   - goto close;
685   - }
  490 + ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
686 491  
687 492 rc = modbus_set_response_timeout(ctx, 0, 1000000);
688 493 printf("2/6 Invalid response timeout (too large us): ");
689   - if (rc == -1 && errno == EINVAL) {
690   - printf("OK\n");
691   - } else {
692   - printf("FAILED\n");
693   - goto close;
694   - }
  494 + ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
695 495  
696 496 rc = modbus_set_byte_timeout(ctx, 0, 1000000);
697 497 printf("3/6 Invalid byte timeout (too large us): ");
698   - if (rc == -1 && errno == EINVAL) {
699   - printf("OK\n");
700   - } else {
701   - printf("FAILED\n");
702   - goto close;
703   - }
  498 + ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
704 499  
705 500 modbus_set_response_timeout(ctx, 0, 1);
706 501 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
... ... @@ -725,12 +520,7 @@ int main(int argc, char *argv[])
725 520 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
726 521 1, tab_rp_registers);
727 522 printf("5/6 Too short response timeout (0.2s < 0.5s): ");
728   - if (rc == -1 && errno == ETIMEDOUT) {
729   - printf("OK\n");
730   - } else {
731   - printf("FAILED\n");
732   - goto close;
733   - }
  523 + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
734 524  
735 525 /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
736 526 usleep(400000);
... ... @@ -740,12 +530,7 @@ int main(int argc, char *argv[])
740 530 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
741 531 1, tab_rp_registers);
742 532 printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
743   - if (rc == 1) {
744   - printf("OK\n");
745   - } else {
746   - printf("FAILED\n");
747   - goto close;
748   - }
  533 + ASSERT_TRUE(rc == 1, "");
749 534  
750 535 /* Disable the byte timeout.
751 536 The full response must be available in the 600ms interval */
... ... @@ -753,12 +538,7 @@ int main(int argc, char *argv[])
753 538 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
754 539 1, tab_rp_registers);
755 540 printf("7/7 Disable byte timeout: ");
756   - if (rc == 1) {
757   - printf("OK\n");
758   - } else {
759   - printf("FAILED\n");
760   - goto close;
761   - }
  541 + ASSERT_TRUE(rc == 1, "");
762 542  
763 543 /* Restore original response timeout */
764 544 modbus_set_response_timeout(ctx, old_response_to_sec,
... ... @@ -772,12 +552,7 @@ int main(int argc, char *argv[])
772 552 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
773 553 1, tab_rp_registers);
774 554 printf("1/2 Too small byte timeout (3ms < 5ms): ");
775   - if (rc == -1 && errno == ETIMEDOUT) {
776   - printf("OK\n");
777   - } else {
778   - printf("FAILED\n");
779   - goto close;
780   - }
  555 + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
781 556  
782 557 /* Wait remaing bytes before flushing */
783 558 usleep(11 * 5000);
... ... @@ -788,12 +563,7 @@ int main(int argc, char *argv[])
788 563 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
789 564 1, tab_rp_registers);
790 565 printf("2/2 Adapted byte timeout (7ms > 5ms): ");
791   - if (rc == 1) {
792   - printf("OK\n");
793   - } else {
794   - printf("FAILED\n");
795   - goto close;
796   - }
  566 + ASSERT_TRUE(rc == 1, "");
797 567 }
798 568  
799 569 /* Restore original byte timeout */
... ... @@ -809,12 +579,7 @@ int main(int argc, char *argv[])
809 579 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
810 580 UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
811 581 printf("* modbus_read_registers: ");
812   - if (rc == -1 && errno == EMBBADDATA) {
813   - printf("OK\n");
814   - } else {
815   - printf("FAILED\n");
816   - goto close;
817   - }
  582 + ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
818 583 free(tab_rp_registers_bad);
819 584  
820 585 /** MANUAL EXCEPTION **/
... ... @@ -823,12 +588,7 @@ int main(int argc, char *argv[])
823 588 UT_REGISTERS_NB, tab_rp_registers);
824 589  
825 590 printf("* modbus_read_registers at special address: ");
826   - if (rc == -1 && errno == EMBXSBUSY) {
827   - printf("OK\n");
828   - } else {
829   - printf("FAILED\n");
830   - goto close;
831   - }
  591 + ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
832 592  
833 593 /** RAW REQUEST */
834 594 if (test_raw_request(ctx, use_backend) == -1) {
... ... @@ -838,12 +598,7 @@ int main(int argc, char *argv[])
838 598 /* Test init functions */
839 599 printf("\nTEST INVALID INITIALIZATION:\n");
840 600 ctx = modbus_new_rtu(NULL, 0, 'A', 0, 0);
841   - if (ctx == NULL && errno == EINVAL) {
842   - printf("OK\n");
843   - } else {
844   - printf("FAILED\n");
845   - goto close;
846   - }
  601 + ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
847 602  
848 603 printf("\nALL TESTS PASS WITH SUCCESS.\n");
849 604  
... ... @@ -920,21 +675,11 @@ int test_raw_request(modbus_t *ctx, int use_backend)
920 675 req_length = modbus_send_raw_request(ctx, raw_req,
921 676 RAW_REQ_LENGTH * sizeof(uint8_t));
922 677 printf("* modbus_send_raw_request: ");
923   - if (req_length == (length + 5)) {
924   - printf("OK\n");
925   - } else {
926   - printf("FAILED (%d)\n", req_length);
927   - return -1;
928   - }
  678 + ASSERT_TRUE(req_length == (length + 5), "FAILED (%d)\n", req_length);
929 679  
930 680 printf("* modbus_receive_confirmation: ");
931 681 rc = modbus_receive_confirmation(ctx, rsp);
932   - if (rc == (length + 12)) {
933   - printf("OK\n");
934   - } else {
935   - printf("FAILED (%d)\n", rc);
936   - return -1;
937   - }
  682 + ASSERT_TRUE(rc == (length + 12), "FAILED (%d)\n", rc);
938 683  
939 684 /* Try to crash server with raw requests to bypass checks of client. */
940 685  
... ... @@ -967,14 +712,9 @@ int test_raw_request(modbus_t *ctx, int use_backend)
967 712 printf("* try an exploit with function %d: ", tab_function[i]);
968 713 }
969 714 rc = modbus_receive_confirmation(ctx, rsp);
970   - if (rc == (length + EXCEPTION_RC) &&
971   - rsp[offset] == (0x80 + tab_function[i]) &&
972   - rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
973   - printf("OK\n");
974   - } else {
975   - printf("FAILED\n");
976   - return -1;
977   - }
  715 + ASSERT_TRUE(rc == (length + EXCEPTION_RC) &&
  716 + rsp[offset] == (0x80 + tab_function[i]) &&
  717 + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
978 718 }
979 719 }
980 720  
... ... @@ -998,15 +738,12 @@ int test_raw_request(modbus_t *ctx, int use_backend)
998 738 printf("* try an exploit with function %d: ", tab_function[i]);
999 739 }
1000 740 rc = modbus_receive_confirmation(ctx, rsp);
1001   - if (rc == length + EXCEPTION_RC &&
1002   - rsp[offset] == (0x80 + tab_function[i]) &&
1003   - rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) {
1004   - printf("OK\n");
1005   - } else {
1006   - printf("FAILED\n");
1007   - return -1;
1008   - }
  741 + ASSERT_TRUE(rc == length + EXCEPTION_RC &&
  742 + rsp[offset] == (0x80 + tab_function[i]) &&
  743 + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
1009 744 }
1010 745  
1011 746 return 0;
  747 +close:
  748 + return -1;
1012 749 }
... ...