Commit 3f96e18c09be604e992b640e61e4d8b9ec24e646

Authored by Andrey Skvortsov
Committed by Stéphane Raimbault
1 parent e98fd68c

Run unit tests with standard: make check (closes #205, closes #238)

This patch has been developed by Andrey Skvortsov, Michael Heimpold
and Stéphane Raimbault.

- avoid bash'isms and use of GNU find
- terminate server after test run (ignored in TCP mode)
- add *.log, *.trs to .gitignore
- unit-test-client returns 0 on success
- save exit code of unit-test-client for make check status
- replace kill by killall
- add entry in README
.gitignore
@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 *.o 3 *.o
4 *.la 4 *.la
5 *.lo 5 *.lo
  6 +*.log
  7 +*.trs
6 .deps 8 .deps
7 .libs 9 .libs
8 GPATH 10 GPATH
README.md
@@ -77,6 +77,8 @@ For a quick test of libmodbus, you can run the following programs in two shells: @@ -77,6 +77,8 @@ For a quick test of libmodbus, you can run the following programs in two shells:
77 77
78 By default, all TCP unit tests will be executed (see --help for options). 78 By default, all TCP unit tests will be executed (see --help for options).
79 79
  80 +It's also possible to run the unit tests with `make check`.
  81 +
80 Report a Bug 82 Report a Bug
81 ------------ 83 ------------
82 84
tests/Makefile.am
@@ -46,4 +46,7 @@ AM_CPPFLAGS = \ @@ -46,4 +46,7 @@ AM_CPPFLAGS = \
46 46
47 AM_CFLAGS = ${my_CFLAGS} 47 AM_CFLAGS = ${my_CFLAGS}
48 48
49 -CLEANFILES = *~ 49 +CLEANFILES = *~ *.log
  50 +
  51 +noinst_SCRIPTS=unit-tests.sh
  52 +TESTS=./unit-tests.sh
tests/unit-test-client.c
@@ -57,6 +57,7 @@ int main(int argc, char *argv[]) @@ -57,6 +57,7 @@ int main(int argc, char *argv[])
57 uint32_t old_byte_to_sec; 57 uint32_t old_byte_to_sec;
58 uint32_t old_byte_to_usec; 58 uint32_t old_byte_to_usec;
59 int use_backend; 59 int use_backend;
  60 + int success = FALSE;
60 61
61 if (argc > 1) { 62 if (argc > 1) {
62 if (strcmp(argv[1], "tcp") == 0) { 63 if (strcmp(argv[1], "tcp") == 0) {
@@ -600,6 +601,7 @@ int main(int argc, char *argv[]) @@ -600,6 +601,7 @@ int main(int argc, char *argv[])
600 ASSERT_TRUE(ctx == NULL && errno == EINVAL, ""); 601 ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
601 602
602 printf("\nALL TESTS PASS WITH SUCCESS.\n"); 603 printf("\nALL TESTS PASS WITH SUCCESS.\n");
  604 + success = TRUE;
603 605
604 close: 606 close:
605 /* Free the memory */ 607 /* Free the memory */
@@ -610,7 +612,7 @@ close: @@ -610,7 +612,7 @@ close:
610 modbus_close(ctx); 612 modbus_close(ctx);
611 modbus_free(ctx); 613 modbus_free(ctx);
612 614
613 - return 0; 615 + return (success) ? 0 : -1;
614 } 616 }
615 617
616 /* Send crafted requests to test server resilience 618 /* Send crafted requests to test server resilience
tests/unit-tests.sh 0 → 100755
  1 +#!/bin/sh
  2 +
  3 +client_log=unit-test-client.log
  4 +server_log=unit-test-server.log
  5 +
  6 +rm -f $client_log $server_log
  7 +
  8 +echo "Starting server"
  9 +./unit-test-server > $server_log 2>&1 &
  10 +
  11 +echo "Starting client"
  12 +./unit-test-client > $client_log 2>&1
  13 +rc=$?
  14 +
  15 +killall unit-test-server
  16 +exit $rc
  17 +