tests: Introduce running tests with GNU autotest in OpenBSC

The quality of the tests is of different value but it is good to
get started and improve from here.
diff --git a/openbsc/.gitignore b/openbsc/.gitignore
index c19c16a..37495cc 100644
--- a/openbsc/.gitignore
+++ b/openbsc/.gitignore
@@ -52,4 +52,10 @@
 tests/sms/sms_test
 tests/timer/timer_test
 
+tests/atconfig
+tests/package.m4
+tests/testsuite
+tests/testsuite.log
+
+
 src/openbsc.cfg*
diff --git a/openbsc/configure.ac b/openbsc/configure.ac
index f330c99..90e3654 100644
--- a/openbsc/configure.ac
+++ b/openbsc/configure.ac
@@ -4,6 +4,7 @@
 	[openbsc-devel@lists.openbsc.org])
 
 AM_INIT_AUTOMAKE([dist-bzip2])
+AC_CONFIG_TESTDIR(tests)
 
 dnl kernel style compile messages
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
diff --git a/openbsc/tests/Makefile.am b/openbsc/tests/Makefile.am
index 1968119..64667c9 100644
--- a/openbsc/tests/Makefile.am
+++ b/openbsc/tests/Makefile.am
@@ -2,4 +2,48 @@
 
 if BUILD_NAT
 SUBDIRS += bsc-nat
+ENABLE_NAT_TEST = echo 'm4_define([ENABLE_NAT_TEST], [1])';
+else
+ENABLE_NAT_TEST = echo '';
 endif
+
+
+# The `:;' works around a Bash 3.2 bug when the output is not writeable.
+$(srcdir)/package.m4: $(top_srcdir)/configure.ac $(top_builddir)/config.log
+	:;{ \
+               echo '# Signature of the current package.' && \
+               echo 'm4_define([AT_PACKAGE_NAME],' && \
+               echo '  [$(PACKAGE_NAME)])' && \
+               echo 'm4_define([AT_PACKAGE_TARNAME],' && \
+               echo '  [$(PACKAGE_TARNAME)])' && \
+               echo 'm4_define([AT_PACKAGE_VERSION],' && \
+               echo '  [$(PACKAGE_VERSION)])' && \
+               echo 'm4_define([AT_PACKAGE_STRING],' && \
+               echo '  [$(PACKAGE_STRING)])' && \
+               echo 'm4_define([AT_PACKAGE_BUGREPORT],' && \
+               echo '  [$(PACKAGE_BUGREPORT)])'; \
+               echo 'm4_define([AT_PACKAGE_URL],' && \
+               echo '  [$(PACKAGE_URL)])'; \
+               $(ENABLE_NAT_TEST) \
+             } >'$(srcdir)/package.m4'
+
+EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE)
+TESTSUITE = $(srcdir)/testsuite
+
+check-local: atconfig $(TESTSUITE)
+	$(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
+
+installcheck-local: atconfig $(TESTSUITE)
+	$(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
+		$(TESTSUITEFLAGS)
+
+clean-local:
+	test ! -f '$(TESTSUITE)' || \
+		$(SHELL) '$(TESTSUITE)' --clean
+	$(RM) -f atconfig
+
+AUTOM4TE = $(SHELL) $(top_srcdir)/missing --run autom4te
+AUTOTEST = $(AUTOM4TE) --language=autotest
+$(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4
+	$(AUTOTEST) -I '$(srcdir)' -o $@.tmp $@.at
+	mv $@.tmp $@
diff --git a/openbsc/tests/bsc-nat/Makefile.am b/openbsc/tests/bsc-nat/Makefile.am
index 5a68984..db3cbe5 100644
--- a/openbsc/tests/bsc-nat/Makefile.am
+++ b/openbsc/tests/bsc-nat/Makefile.am
@@ -2,7 +2,7 @@
 AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS)
 AM_LDFLAGS = $(COVERAGE_LDFLAGS)
 
-EXTRA_DIST = bsc_data.c
+EXTRA_DIST = bsc_nat_test.ok
 
 noinst_PROGRAMS = bsc_nat_test
 
diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.ok b/openbsc/tests/bsc-nat/bsc_nat_test.ok
new file mode 100644
index 0000000..2aff594
--- /dev/null
+++ b/openbsc/tests/bsc-nat/bsc_nat_test.ok
@@ -0,0 +1,21 @@
+Testing BSS Filtering.
+Going to test item: 0
+Going to test item: 1
+Going to test item: 2
+Going to test item: 3
+Going to test item: 4
+Going to test item: 5
+Going to test item: 6
+Going to test item: 7
+Going to test item: 8
+Going to test item: 9
+Going to test item: 10
+Going to test item: 11
+Going to test item: 12
+Testing connection tracking.
+Testing paging by lac.
+Testing MGCP.
+Testing finding of a BSC Connection
+Testing rewriting MGCP messages.
+Testing MGCP response parsing.
+Testing execution completed.
diff --git a/openbsc/tests/channel/Makefile.am b/openbsc/tests/channel/Makefile.am
index 404ab32..4f50dab 100644
--- a/openbsc/tests/channel/Makefile.am
+++ b/openbsc/tests/channel/Makefile.am
@@ -1,6 +1,8 @@
 INCLUDES = $(all_includes) -I$(top_srcdir)/include
 AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS)
 
+EXTRA_DIST = channel_test.ok
+
 noinst_PROGRAMS = channel_test
 
 channel_test_SOURCES = channel_test.c
diff --git a/openbsc/tests/channel/channel_test.ok b/openbsc/tests/channel/channel_test.ok
new file mode 100644
index 0000000..7976aee
--- /dev/null
+++ b/openbsc/tests/channel/channel_test.ok
@@ -0,0 +1,2 @@
+Testing the gsm_subscriber chan logic
+Reached, didn't crash, test passed
diff --git a/openbsc/tests/db/Makefile.am b/openbsc/tests/db/Makefile.am
index aab2cd6..f0d65f8 100644
--- a/openbsc/tests/db/Makefile.am
+++ b/openbsc/tests/db/Makefile.am
@@ -2,6 +2,8 @@
 AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS)
 AM_LDFLAGS = $(COVERAGE_LDFLAGS)
 
+EXTRA_DIST = db_test.ok
+
 noinst_PROGRAMS = db_test
 
 db_test_SOURCES = db_test.c
diff --git a/openbsc/tests/db/db_test.ok b/openbsc/tests/db/db_test.ok
new file mode 100644
index 0000000..2632a8c
--- /dev/null
+++ b/openbsc/tests/db/db_test.ok
@@ -0,0 +1,4 @@
+Testing subscriber database code.
+DB: Database initialized.
+DB: Database prepared.
+Done
diff --git a/openbsc/tests/gsm0408/Makefile.am b/openbsc/tests/gsm0408/Makefile.am
index 90ca05b..6ecdc07 100644
--- a/openbsc/tests/gsm0408/Makefile.am
+++ b/openbsc/tests/gsm0408/Makefile.am
@@ -2,6 +2,8 @@
 AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS)
 noinst_PROGRAMS = gsm0408_test
 
+EXTRA_DIST = gsm0408_test.ok
+
 gsm0408_test_SOURCES = gsm0408_test.c
 gsm0408_test_LDADD =	$(top_builddir)/src/libbsc/libbsc.a \
 			$(top_builddir)/src/libmsc/libmsc.a \
diff --git a/openbsc/tests/gsm0408/gsm0408_test.ok b/openbsc/tests/gsm0408/gsm0408_test.ok
new file mode 100644
index 0000000..52c601e
--- /dev/null
+++ b/openbsc/tests/gsm0408/gsm0408_test.ok
@@ -0,0 +1,5 @@
+Testing test location area identifier
+Testing parsing and generating TMSI/IMSI
+hex: 17 08 99 78 56 34 12 90 78 36 
+hex: 17 09 91 78 56 34 12 90 78 56 f4 
+Done.
diff --git a/openbsc/tests/mgcp/Makefile.am b/openbsc/tests/mgcp/Makefile.am
index 472368c..ff67cf8 100644
--- a/openbsc/tests/mgcp/Makefile.am
+++ b/openbsc/tests/mgcp/Makefile.am
@@ -2,6 +2,8 @@
 AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(COVERAGE_CFLAGS)
 AM_LDFLAGS = $(COVERAGE_LDFLAGS)
 
+EXTRA_DIST = mgcp_test.ok
+
 noinst_PROGRAMS = mgcp_test
 
 mgcp_test_SOURCES = mgcp_test.c
diff --git a/openbsc/tests/mgcp/mgcp_test.ok b/openbsc/tests/mgcp/mgcp_test.ok
new file mode 100644
index 0000000..1a01c28
--- /dev/null
+++ b/openbsc/tests/mgcp/mgcp_test.ok
@@ -0,0 +1,2 @@
+Testing AUEP
+Done
diff --git a/openbsc/tests/testsuite.at b/openbsc/tests/testsuite.at
new file mode 100644
index 0000000..fb57d07
--- /dev/null
+++ b/openbsc/tests/testsuite.at
@@ -0,0 +1,39 @@
+AT_INIT
+AT_BANNER([Regression tests.])
+
+AT_SETUP([gsm0408])
+AT_KEYWORDS([gsm0408])
+cat $abs_srcdir/gsm0408/gsm0408_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/gsm0408/gsm0408_test], [], [expout], [ignore])
+AT_CLEANUP
+
+AT_SETUP([db])
+AT_KEYWORDS([db])
+cat $abs_srcdir/db/db_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/db/db_test], [], [expout], [ignore])
+AT_CLEANUP
+
+AT_SETUP([channel])
+AT_KEYWORDS([channel])
+cat $abs_srcdir/channel/channel_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/channel/channel_test], [], [expout], [ignore])
+AT_CLEANUP
+
+AT_SETUP([mgcp])
+AT_KEYWORDS([mgcp])
+cat $abs_srcdir/mgcp/mgcp_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/mgcp/mgcp_test], [], [expout], [ignore])
+AT_CLEANUP
+
+AT_SETUP([gprs])
+AT_KEYWORDS([gprs])
+cat $abs_srcdir/gprs/gprs_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/gprs/gprs_test], [], [expout], [ignore])
+AT_CLEANUP
+
+m4_ifdef([ENABLE_NAT_TEST],[
+AT_SETUP([bsc-nat])
+AT_KEYWORDS([bsc-nat])
+cat $abs_srcdir/bsc-nat/bsc_nat_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/bsc-nat/bsc_nat_test], [], [expout], [ignore])
+AT_CLEANUP])