summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2018-02-26 19:42:22 +0100
committerHarald Welte <laforge@gnumonks.org>2018-03-01 12:33:02 +0000
commit87fade88bd8471d0459a306255403e854122120e (patch)
tree1f090b3419983d8d08c72bfd67895f7fec679dd3 /tests
parent721aa6ded9c736e3cc5b20824dd58b1af4f4a907 (diff)
timer: Introduce osmo_clock_gettime to override clock_gettime
Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am7
-rw-r--r--tests/testsuite.at6
-rw-r--r--tests/timer/clk_override_test.c89
-rw-r--r--tests/timer/clk_override_test.ok8
4 files changed, 108 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5dd8e22b..cca128dd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -23,7 +23,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
coding/coding_test conv/conv_gsm0503_test \
abis/abis_test endian/endian_test sercomm/sercomm_test \
prbs/prbs_test gsm23003/gsm23003_test \
- codec/codec_ecu_fr_test
+ codec/codec_ecu_fr_test timer/clk_override_test
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
@@ -122,6 +122,8 @@ sms_sms_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
timer_timer_test_SOURCES = timer/timer_test.c
+timer_clk_override_test_SOURCES = timer/clk_override_test.c
+
ussd_ussd_test_SOURCES = ussd/ussd_test.c
ussd_ussd_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
@@ -250,7 +252,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
osmo-auc-gen/osmo-auc-gen_test.err \
conv/conv_gsm0503_test.ok endian/endian_test.ok \
sercomm/sercomm_test.ok prbs/prbs_test.ok \
- gsm23003/gsm23003_test.ok
+ gsm23003/gsm23003_test.ok \
+ timer/clk_override_test.ok
DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c
BUILT_SOURCES = conv/gsm0503_test_vectors.c
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 0ec852cc..15a89b6d 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -240,6 +240,12 @@ cat $abs_srcdir/timer/timer_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/timer/timer_test], [0], [expout], [ignore])
AT_CLEANUP
+AT_SETUP([clk_override])
+AT_KEYWORDS([clk_override])
+cat $abs_srcdir/timer/clk_override_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/timer/clk_override_test], [0], [expout], [ignore])
+AT_CLEANUP
+
AT_SETUP([tlv])
AT_KEYWORDS([tlv])
cat $abs_srcdir/tlv/tlv_test.ok > expout
diff --git a/tests/timer/clk_override_test.c b/tests/timer/clk_override_test.c
new file mode 100644
index 00000000..308e8212
--- /dev/null
+++ b/tests/timer/clk_override_test.c
@@ -0,0 +1,89 @@
+/*
+ * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2011 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved
+ *
+ * Authors: Holger Hans Peter Freyther <zecke@selfish.org>
+ * Pablo Neira Ayuso <pablo@gnumonks.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/timer_compat.h>
+
+int main(int argc, char *argv[])
+{
+
+ struct timespec ts1 = { 123, 456 }, ts2 = {1, 200};
+ struct timespec read1, read2, res;
+ struct timespec *mono;
+
+ osmo_clock_gettime(CLOCK_BOOTTIME, &read1);
+ usleep(500);
+ osmo_clock_gettime(CLOCK_BOOTTIME, &read2);
+ if (!timespeccmp(&read2, &read1, >))
+ return EXIT_FAILURE;
+ printf("Non implemented clocks work fine\n");
+
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
+ usleep(500);
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
+ if (!timespeccmp(&read2, &read1, >))
+ return EXIT_FAILURE;
+ printf("Monotonic clock is working fine by default\n");
+
+ osmo_clock_override_enable(CLOCK_MONOTONIC, true);
+ printf("Monotonic clock override enabled\n");
+
+ mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
+ if (timespecisset(mono))
+ return EXIT_FAILURE;
+ printf("Monotonic override is cleared by default\n");
+
+ memcpy(mono, &ts1, sizeof(struct timespec));
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
+ if (!timespeccmp(&ts1, &read1, ==))
+ return EXIT_FAILURE;
+ printf("Monotonic clock can be overriden\n");
+
+ osmo_clock_override_add(CLOCK_MONOTONIC, ts2.tv_sec, ts2.tv_nsec);
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
+ timespecadd(&ts2, &ts1, &res);
+ if (!timespeccmp(&res, &read1, ==))
+ return EXIT_FAILURE;
+ printf("osmo_clock_override_add works fine.\n");
+
+ osmo_clock_override_enable(CLOCK_MONOTONIC, false);
+ printf("Monotonic clock override disabled\n");
+
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read1);
+ usleep(500);
+ osmo_clock_gettime(CLOCK_MONOTONIC, &read2);
+ if (!timespeccmp(&read2, &read1, >))
+ return EXIT_FAILURE;
+ printf("Monotonic clock is working fine after enable+disable.\n");
+
+ return 0;
+}
diff --git a/tests/timer/clk_override_test.ok b/tests/timer/clk_override_test.ok
new file mode 100644
index 00000000..1fb519c9
--- /dev/null
+++ b/tests/timer/clk_override_test.ok
@@ -0,0 +1,8 @@
+Non implemented clocks work fine
+Monotonic clock is working fine by default
+Monotonic clock override enabled
+Monotonic override is cleared by default
+Monotonic clock can be overriden
+osmo_clock_override_add works fine.
+Monotonic clock override disabled
+Monotonic clock is working fine after enable+disable.