summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am8
-rw-r--r--tests/fsm/fsm_test.c155
-rw-r--r--tests/fsm/fsm_test.err11
-rw-r--r--tests/fsm/fsm_test.ok0
-rw-r--r--tests/testsuite.at7
5 files changed, 179 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 55aaa078..73b2d6a8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -13,7 +13,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
vty/vty_test comp128/comp128_test utils/utils_test \
smscb/gsm0341_test stats/stats_test \
bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test \
- sim/sim_test tlv/tlv_test gsup/gsup_test
+ sim/sim_test tlv/tlv_test gsup/gsup_test fsm/fsm_test
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
@@ -118,6 +118,9 @@ tlv_tlv_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/l
gsup_gsup_test_SOURCES = gsup/gsup_test.c
gsup_gsup_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/libosmocore.la
+fsm_fsm_test_SOURCES = fsm/fsm_test.c
+fsm_fsm_test_LDADD = $(top_builddir)/src/libosmocore.la
+
# The `:;' works around a Bash 3.2 bug when the output is not writeable.
$(srcdir)/package.m4: $(top_srcdir)/configure.ac
:;{ \
@@ -152,7 +155,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
vty/vty_test.ok comp128/comp128_test.ok \
utils/utils_test.ok stats/stats_test.ok \
bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \
- sim/sim_test.ok tlv/tlv_test.ok gsup/gsup_test.ok
+ sim/sim_test.ok tlv/tlv_test.ok gsup/gsup_test.ok \
+ fsm/fsm_test.ok fsm/fsm_test.err
DISTCLEANFILES = atconfig
diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c
new file mode 100644
index 00000000..84c2ae4d
--- /dev/null
+++ b/tests/fsm/fsm_test.c
@@ -0,0 +1,155 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/fsm.h>
+
+enum {
+ DMAIN,
+};
+
+static void *g_ctx;
+
+
+enum test_fsm_states {
+ ST_NULL = 0,
+ ST_ONE,
+ ST_TWO,
+};
+
+enum test_fsm_evt {
+ EV_A,
+ EV_B,
+};
+
+static void test_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+ case EV_A:
+ OSMO_ASSERT(data == (void *) 23);
+ osmo_fsm_inst_state_chg(fi, ST_ONE, 0, 0);
+ break;
+ default:
+ OSMO_ASSERT(0);
+ break;
+ }
+}
+
+static void test_fsm_one(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+ switch (event) {
+ case EV_B:
+ OSMO_ASSERT(data == (void *) 42);
+ osmo_fsm_inst_state_chg(fi,ST_TWO, 1, 2342);
+ break;
+ default:
+ OSMO_ASSERT(0);
+ break;
+ }
+}
+
+static void test_fsm_tmr_cb(struct osmo_fsm_inst *fi)
+{
+ OSMO_ASSERT(fi->T == 2342);
+ OSMO_ASSERT(fi->state == ST_TWO);
+ LOGP(DMAIN, LOGL_INFO, "Timer\n");
+
+ exit(0);
+}
+
+static struct osmo_fsm_state test_fsm_states[] = {
+ [ST_NULL] = {
+ .in_event_mask = (1 << EV_A),
+ .out_state_mask = (1 << ST_ONE),
+ .name = "NULL",
+ .action = test_fsm_null,
+ },
+ [ST_ONE]= {
+ .in_event_mask = (1 << EV_B),
+ .out_state_mask = (1 << ST_TWO),
+ .name = "ONE",
+ .action= test_fsm_one,
+ },
+ [ST_TWO]= {
+ .in_event_mask = 0,
+ .name = "TWO",
+ .action = NULL,
+ },
+};
+
+static struct osmo_fsm fsm = {
+ .name = "Test FSM",
+ .states = test_fsm_states,
+ .num_states = ARRAY_SIZE(test_fsm_states),
+ .log_subsys = DMAIN,
+};
+
+static int foo(void)
+{
+ struct osmo_fsm_inst *fi;
+
+ LOGP(DMAIN, LOGL_INFO, "Checking FSM allocation\n");
+ fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
+ OSMO_ASSERT(fi);
+ OSMO_ASSERT(fi->fsm == &fsm);
+ OSMO_ASSERT(!strncmp(osmo_fsm_inst_name(fi), fsm.name, strlen(fsm.name)));
+ OSMO_ASSERT(fi->state == ST_NULL);
+ OSMO_ASSERT(fi->log_level == LOGL_DEBUG);
+
+ /* Try invalid state transition */
+ osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
+ OSMO_ASSERT(fi->state == ST_NULL);
+
+ /* Legitimate state transition */
+ osmo_fsm_inst_dispatch(fi, EV_A, (void *) 23);
+ OSMO_ASSERT(fi->state == ST_ONE);
+
+ /* Legitimate transition with timer */
+ fsm.timer_cb = test_fsm_tmr_cb;
+ osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
+ OSMO_ASSERT(fi->state == ST_TWO);
+
+
+ return 0;
+}
+
+static const struct log_info_cat default_categories[] = {
+ [DMAIN] = {
+ .name = "DMAIN",
+ .description = "Main",
+ .enabled = 1, .loglevel = LOGL_DEBUG,
+ },
+};
+
+static const struct log_info log_info = {
+ .cat = default_categories,
+ .num_cat = ARRAY_SIZE(default_categories),
+};
+
+int main(int argc, char **argv)
+{
+ struct log_target *stderr_target;
+
+ osmo_fsm_log_addr(false);
+
+ log_init(&log_info, NULL);
+ stderr_target = log_target_create_stderr();
+ log_add_target(stderr_target);
+ log_set_print_filename(stderr_target, 0);
+
+ g_ctx = NULL;
+ osmo_fsm_register(&fsm);
+
+ foo();
+
+ while (1) {
+ osmo_select_main(0);
+ }
+
+ exit(0);
+}
diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err
new file mode 100644
index 00000000..b9c738c6
--- /dev/null
+++ b/tests/fsm/fsm_test.err
@@ -0,0 +1,11 @@
+Checking FSM allocation
+Test FSM{NULL}: Allocated
+Test FSM{NULL}: Received Event 1
+Test FSM(NULL): Event 1 not permitted
+Test FSM{NULL}: Received Event 0
+Test FSM{NULL}: state_chg to ONE
+Test FSM{ONE}: Received Event 1
+Test FSM{ONE}: state_chg to TWO
+Test FSM{TWO}: Timeout of T2342
+Timer
+ \ No newline at end of file
diff --git a/tests/fsm/fsm_test.ok b/tests/fsm/fsm_test.ok
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/fsm/fsm_test.ok
diff --git a/tests/testsuite.at b/tests/testsuite.at
index d49f7ff1..93373c19 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -201,3 +201,10 @@ AT_KEYWORDS([gsup])
cat $abs_srcdir/gsup/gsup_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/gsup/gsup_test], [0], [expout], [ignore])
AT_CLEANUP
+
+AT_SETUP([fsm])
+AT_KEYWORDS([fsm])
+cat $abs_srcdir/fsm/fsm_test.ok > expout
+cat $abs_srcdir/fsm/fsm_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/fsm/fsm_test], [0], [expout], [experr])
+AT_CLEANUP