summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorKaterina Barone-Adesi <kat.obsc@gmail.com>2013-02-21 05:16:29 +0000
committerHolger Hans Peter Freyther <zecke@selfish.org>2013-02-27 15:13:45 +0100
commit3309a43ef56366926589fdb3f7e4ee8af41fe1c5 (patch)
treebcef95b0f1491394a1a62bd1ce77f27655449259 /tests
parentefd2bd691fd948df00c1eebcd8599b871f5c34f5 (diff)
Added a ring buffer log target to store the last N log messages.
The log target can be used via log alarms and show alarms. Why? This feature was proposed/requested at http://openbsc.osmocom.org/trac/wiki/Tasks/ErrorLogTarget All messages use the same amount of space, prioritizing simplicity.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am13
-rw-r--r--tests/loggingrb/logging_test.err3
-rw-r--r--tests/loggingrb/logging_test.ok0
-rw-r--r--tests/loggingrb/loggingrb_test.c83
-rw-r--r--tests/strrb/strrb_test.c225
-rw-r--r--tests/strrb/strrb_test.ok1
-rw-r--r--tests/testsuite.at13
7 files changed, 336 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index be0b5f4c..bc9b7de6 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,7 +4,9 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
smscb/smscb_test bits/bitrev_test a5/a5_test \
conv/conv_test auth/milenage_test lapd/lapd_test \
gsm0808/gsm0808_test gsm0408/gsm0408_test \
- gb/bssgp_fc_test logging/logging_test fr/fr_test
+ gb/bssgp_fc_test logging/logging_test fr/fr_test \
+ loggingrb/loggingrb_test strrb/strrb_test
+
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
endif
@@ -54,6 +56,12 @@ logging_logging_test_LDADD = $(top_builddir)/src/libosmocore.la
fr_fr_test_SOURCES = fr/fr_test.c
fr_fr_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gb/libosmogb.la
+loggingrb_loggingrb_test_SOURCES = logging/logging_test.c
+loggingrb_loggingrb_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/vty/libosmovty.la
+
+strrb_strrb_test_SOURCES = strrb/strrb_test.c
+strrb_strrb_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
@@ -82,7 +90,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
gb/bssgp_fc_tests.ok gb/bssgp_fc_tests.sh \
msgfile/msgfile_test.ok msgfile/msgconfig.cfg \
logging/logging_test.ok logging/logging_test.err \
- fr/fr_test.ok
+ fr/fr_test.ok loggingrb/logging_test.ok \
+ loggingrb/logging_test.err strrb/strrb_test.ok
DISTCLEANFILES = atconfig
diff --git a/tests/loggingrb/logging_test.err b/tests/loggingrb/logging_test.err
new file mode 100644
index 00000000..b59d2e83
--- /dev/null
+++ b/tests/loggingrb/logging_test.err
@@ -0,0 +1,3 @@
+You should see this
+You should see this
+ \ No newline at end of file
diff --git a/tests/loggingrb/logging_test.ok b/tests/loggingrb/logging_test.ok
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/loggingrb/logging_test.ok
diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c
new file mode 100644
index 00000000..1ab5212d
--- /dev/null
+++ b/tests/loggingrb/loggingrb_test.c
@@ -0,0 +1,83 @@
+/* simple test for the debug interface */
+/*
+ * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+#include <assert.h>
+
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/ringb.h>
+#include <osmocom/vty/logging_rbvty.h>
+
+enum {
+ DRLL,
+ DCC,
+ DMM,
+};
+
+static const struct log_info_cat default_categories[] = {
+ [DRLL] = {
+ .name = "DRLL",
+ .description = "A-bis Radio Link Layer (RLL)",
+ .color = "\033[1;31m",
+ .enabled = 1, .loglevel = LOGL_NOTICE,
+ },
+ [DCC] = {
+ .name = "DCC",
+ .description = "Layer3 Call Control (CC)",
+ .color = "\033[1;32m",
+ .enabled = 1, .loglevel = LOGL_NOTICE,
+ },
+ [DMM] = {
+ .name = NULL,
+ .description = "Layer3 Mobility Management (MM)",
+ .color = "\033[1;33m",
+ .enabled = 1, .loglevel = LOGL_NOTICE,
+ },
+};
+
+const struct log_info log_info = {
+ .cat = default_categories,
+ .num_cat = ARRAY_SIZE(default_categories),
+};
+
+int main(int argc, char **argv)
+{
+ struct log_target *ringbuf_target;
+
+ log_init(&log_info, NULL);
+ ringbuf_target = log_target_create_rbvty(NULL, 0x1000);
+ log_add_target(ringbuf_target);
+ log_set_all_filter(ringbuf_target, 1);
+ log_set_print_filename(ringbuf_target, 0);
+
+ log_parse_category_mask(ringbuf_target, "DRLL:DCC");
+ log_parse_category_mask(ringbuf_target, "DRLL");
+ DEBUGP(DCC, "You should not see this\n");
+
+ log_parse_category_mask(ringbuf_target, "DRLL:DCC");
+ DEBUGP(DRLL, "You should see this\n");
+ DEBUGP(DCC, "You should see this\n");
+ DEBUGP(DMM, "You should not see this\n");
+ fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 0));
+ fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 1));
+ assert(!ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 2));
+
+ return 0;
+}
diff --git a/tests/strrb/strrb_test.c b/tests/strrb/strrb_test.c
new file mode 100644
index 00000000..abe649f5
--- /dev/null
+++ b/tests/strrb/strrb_test.c
@@ -0,0 +1,225 @@
+/* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
+ * All Rights Reserved
+ *
+ * This program is iree 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 3 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 <assert.h>
+#include <string.h>
+
+#include <osmocom/core/strrb.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+
+struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
+
+#define STR0 "hello"
+#define STR1 "a"
+#define STR2 "world"
+#define STR3 "sky"
+#define STR4 "moon"
+
+#define TESTSIZE 3
+
+void init_rbs(void)
+{
+ rb0 = osmo_strrb_create(NULL, TESTSIZE);
+
+ rb1 = osmo_strrb_create(NULL, TESTSIZE);
+ osmo_strrb_add(rb1, STR0);
+
+ rb2 = osmo_strrb_create(NULL, TESTSIZE);
+ osmo_strrb_add(rb2, STR0);
+ osmo_strrb_add(rb2, STR1);
+
+ rb3 = osmo_strrb_create(NULL, TESTSIZE);
+ osmo_strrb_add(rb3, STR0);
+ osmo_strrb_add(rb3, STR1);
+ osmo_strrb_add(rb3, STR2);
+
+ rb4 = osmo_strrb_create(NULL, TESTSIZE);
+ osmo_strrb_add(rb4, STR0);
+ osmo_strrb_add(rb4, STR1);
+ osmo_strrb_add(rb4, STR2);
+ osmo_strrb_add(rb4, STR3);
+
+ rb5 = osmo_strrb_create(NULL, TESTSIZE);
+ osmo_strrb_add(rb5, STR0);
+ osmo_strrb_add(rb5, STR1);
+ osmo_strrb_add(rb5, STR2);
+ osmo_strrb_add(rb5, STR3);
+ osmo_strrb_add(rb5, STR4);
+}
+
+void free_rbs(void)
+{
+ talloc_free(rb0);
+ talloc_free(rb1);
+ talloc_free(rb2);
+ talloc_free(rb3);
+ talloc_free(rb4);
+ talloc_free(rb5);
+}
+
+void test_offset_valid(void)
+{
+ assert(_osmo_strrb_is_bufindex_valid(rb1, 0));
+ assert(!_osmo_strrb_is_bufindex_valid(rb1, 1));
+ assert(!_osmo_strrb_is_bufindex_valid(rb1, 2));
+
+ assert(!_osmo_strrb_is_bufindex_valid(rb3, 0));
+ assert(_osmo_strrb_is_bufindex_valid(rb3, 1));
+ assert(_osmo_strrb_is_bufindex_valid(rb3, 2));
+
+ assert(_osmo_strrb_is_bufindex_valid(rb4, 0));
+ assert(!_osmo_strrb_is_bufindex_valid(rb4, 1));
+ assert(_osmo_strrb_is_bufindex_valid(rb4, 2));
+
+ assert(_osmo_strrb_is_bufindex_valid(rb5, 0));
+ assert(_osmo_strrb_is_bufindex_valid(rb5, 1));
+ assert(!_osmo_strrb_is_bufindex_valid(rb5, 2));
+}
+
+void test_elems(void)
+{
+ assert(osmo_strrb_elements(rb0) == 0);
+ assert(osmo_strrb_elements(rb1) == 1);
+ assert(osmo_strrb_elements(rb2) == 2);
+ assert(osmo_strrb_elements(rb3) == 2);
+}
+
+void test_getn(void)
+{
+ assert(!osmo_strrb_get_nth(rb0, 0));
+ assert(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0)));
+ assert(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1)));
+ assert(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0)));
+ assert(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1)));
+ assert(!osmo_strrb_get_nth(rb3, 2));
+}
+
+void test_getn_wrap(void)
+{
+ assert(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0)));
+ assert(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1)));
+
+ assert(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0)));
+ assert(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1)));
+}
+
+void test_add(void)
+{
+ struct osmo_strrb *rb = osmo_strrb_create(NULL, 4);
+ assert(rb->start == 0);
+ assert(rb->end == 0);
+
+ osmo_strrb_add(rb, "a");
+ osmo_strrb_add(rb, "b");
+ osmo_strrb_add(rb, "c");
+ assert(rb->start == 0);
+ assert(rb->end == 3);
+ assert(osmo_strrb_elements(rb) == 3);
+
+ osmo_strrb_add(rb, "d");
+ assert(rb->start == 1);
+ assert(rb->end == 0);
+ assert(osmo_strrb_elements(rb) == 3);
+ assert(!strcmp("b", osmo_strrb_get_nth(rb, 0)));
+ assert(!strcmp("c", osmo_strrb_get_nth(rb, 1)));
+ assert(!strcmp("d", osmo_strrb_get_nth(rb, 2)));
+
+ osmo_strrb_add(rb, "e");
+ assert(rb->start == 2);
+ assert(rb->end == 1);
+ assert(!strcmp("c", osmo_strrb_get_nth(rb, 0)));
+ assert(!strcmp("d", osmo_strrb_get_nth(rb, 1)));
+ assert(!strcmp("e", osmo_strrb_get_nth(rb, 2)));
+
+ osmo_strrb_add(rb, "f");
+ assert(rb->start == 3);
+ assert(rb->end == 2);
+ assert(!strcmp("d", osmo_strrb_get_nth(rb, 0)));
+ assert(!strcmp("e", osmo_strrb_get_nth(rb, 1)));
+ assert(!strcmp("f", osmo_strrb_get_nth(rb, 2)));
+
+ osmo_strrb_add(rb, "g");
+ assert(rb->start == 0);
+ assert(rb->end == 3);
+ assert(!strcmp("e", osmo_strrb_get_nth(rb, 0)));
+ assert(!strcmp("f", osmo_strrb_get_nth(rb, 1)));
+ assert(!strcmp("g", osmo_strrb_get_nth(rb, 2)));
+
+ osmo_strrb_add(rb, "h");
+ assert(rb->start == 1);
+ assert(rb->end == 0);
+ assert(!strcmp("f", osmo_strrb_get_nth(rb, 0)));
+ assert(!strcmp("g", osmo_strrb_get_nth(rb, 1)));
+ assert(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
+
+ talloc_free(rb);
+}
+
+void test_long_msg(void)
+{
+ struct osmo_strrb *rb = osmo_strrb_create(NULL, 2);
+ int test_size = RB_MAX_MESSAGE_SIZE + 7;
+ char *tests1, *tests2;
+ const char *rb_content;
+ int i;
+
+ tests1 = malloc(test_size);
+ tests2 = malloc(test_size);
+ /* Be certain allocating memory worked before continuing */
+ assert(tests1);
+ assert(tests2);
+
+ for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) {
+ tests1[i] = 'a';
+ tests1[i + 1] = 'b';
+ }
+ tests1[i] = '\0';
+
+ osmo_strrb_add(rb, tests1);
+ strcpy(tests2, tests1);
+
+ /* Verify that no stale data from test1 is lingering... */
+ bzero(tests1, test_size);
+ free(tests1);
+
+ rb_content = osmo_strrb_get_nth(rb, 0);
+ assert(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1));
+ assert(!rb_content[RB_MAX_MESSAGE_SIZE - 1]);
+ assert(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
+
+ free(tests2);
+ talloc_free(rb);
+}
+
+int main(int argc, char **argv)
+{
+ init_rbs();
+ test_offset_valid();
+ test_elems();
+ test_getn();
+ test_getn_wrap();
+ test_add();
+ test_long_msg();
+ printf("All tests passed\n");
+
+ free_rbs();
+ return 0;
+}
diff --git a/tests/strrb/strrb_test.ok b/tests/strrb/strrb_test.ok
new file mode 100644
index 00000000..9ac5124e
--- /dev/null
+++ b/tests/strrb/strrb_test.ok
@@ -0,0 +1 @@
+All tests passed
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 5029b9e9..21fad1d6 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -99,3 +99,16 @@ cat $abs_srcdir/fr/fr_test.err > experr
AT_CHECK([$abs_top_builddir/tests/fr/fr_test], [], [expout], [experr])
AT_CLEANUP
+
+AT_SETUP([loggingrb])
+AT_KEYWORDS([loggingrb])
+cat $abs_srcdir/loggingrb/logging_test.ok > expout
+cat $abs_srcdir/loggingrb/logging_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/loggingrb/loggingrb_test], [], [expout], [experr])
+AT_CLEANUP
+
+AT_SETUP([strrb])
+AT_KEYWORDS([strrb])
+cat $abs_srcdir/strrb/strrb_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/strrb/strrb_test], [], [expout], [ignore])
+AT_CLEANUP