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. Renames as per tnt's request. UI changed to reflect number of usable messages, as per zecke's request. --- .gitignore | 2 + include/osmocom/core/logging.h | 5 + include/osmocom/core/loggingrb.h | 40 +++++++ include/osmocom/core/strrb.h | 58 ++++++++++ src/Makefile.am | 4 +- src/loggingrb.c | 98 +++++++++++++++++ src/strrb.c | 170 +++++++++++++++++++++++++++++ src/vty/logging_vty.c | 85 ++++++++++++++- tests/Makefile.am | 13 ++- tests/loggingrb/logging_test.err | 3 + tests/loggingrb/logging_test.ok | 0 tests/loggingrb/loggingrb_test.c | 83 +++++++++++++++ tests/strrb/strrb_test.c | 225 +++++++++++++++++++++++++++++++++++++++ tests/strrb/strrb_test.ok | 1 + tests/testsuite.at | 13 +++ 15 files changed, 792 insertions(+), 8 deletions(-) create mode 100644 include/osmocom/core/loggingrb.h create mode 100644 include/osmocom/core/strrb.h create mode 100644 src/loggingrb.c create mode 100644 src/strrb.c create mode 100644 tests/loggingrb/logging_test.err create mode 100644 tests/loggingrb/logging_test.ok create mode 100644 tests/loggingrb/loggingrb_test.c create mode 100644 tests/strrb/strrb_test.c create mode 100644 tests/strrb/strrb_test.ok
diff --git a/.gitignore b/.gitignore index 24ca677..aedd9fd 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,8 @@ tests/gb/bssgp_fc_test tests/gsm0408/gsm0408_test tests/logging/logging_test tests/fr/fr_test +tests/loggingrb/loggingrb_test +tests/ringbuf/ringbuf_test
utils/osmo-arfcn utils/osmo-auc-gen diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index 54262b7..fa3365a 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -114,6 +114,7 @@ enum log_target_type { LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */ LOG_TGT_TYPE_FILE, /*!< \brief text file logging */ LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */ + LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */ };
/*! \brief structure representing a logging target */ @@ -154,6 +155,10 @@ struct log_target { struct { void *vty; } tgt_vty; + + struct { + void *rb; + } tgt_rb; };
/*! \brief call-back function to be called when the logging framework diff --git a/include/osmocom/core/loggingrb.h b/include/osmocom/core/loggingrb.h new file mode 100644 index 0000000..a6f377b --- /dev/null +++ b/include/osmocom/core/loggingrb.h @@ -0,0 +1,40 @@ +#ifndef _LOGGINGRB_H +#define _LOGGINGRB_H + +/* (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 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. + * + */ + + +/*! \defgroup loggingrb Osmocom ringbuffer-backed logging + * @{ + */ + +/*! \file loggingrb.h + */ + +struct log_info; + +size_t log_target_rb_used_size(struct log_target const *target); +size_t log_target_rb_avail_size(struct log_target const *target); +const char *log_target_rb_get(struct log_target const *target, size_t logindex); +struct log_target *log_target_create_rb(size_t size); + +/*! @} */ + +#endif /* _LOGGINGRB_H */ diff --git a/include/osmocom/core/strrb.h b/include/osmocom/core/strrb.h new file mode 100644 index 0000000..cfc56dc --- /dev/null +++ b/include/osmocom/core/strrb.h @@ -0,0 +1,58 @@ +#ifndef _STRRB_H +#define _STRRB_H + +/* (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 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. + * + */ + +/*! \defgroup osmo_strrb Osmocom ringbuffers for log strings + * @{ + */ + +/*! \file strrb.h + * \brief Osmocom string ringbuffer handling routines + */ + +#include <unistd.h> +#include <stdbool.h> +#include <stdint.h> + +#include <osmocom/core/talloc.h> + +/*! \brief A structure representing an osmocom string ringbuffer */ + +#define RB_MAX_MESSAGE_SIZE 240 +struct osmo_strrb { + uint16_t start; /*!< \brief index of the first slot */ + uint16_t end; /*!< \brief index of the last slot */ + uint16_t size; /*!< \brief max number of messages to store */ + char **buffer; /*!< \brief storage for messages */ +}; + +struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size); +bool osmo_strrb_is_empty(const struct osmo_strrb *rb); +const char *osmo_strrb_get_nth(const struct osmo_strrb *rb, + unsigned int string_index); +bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb, + unsigned int offset); +size_t osmo_strrb_elements(const struct osmo_strrb *rb); +int osmo_strrb_add(struct osmo_strrb *rb, const char *data); + +/*! @} */ + +#endif /* _STRRB_H */ diff --git a/src/Makefile.am b/src/Makefile.am index b425ea1..081be96 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,8 +11,8 @@ libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c bits.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c rate_ctr.c \ gsmtap_util.c crc16.c panic.c backtrace.c \ - conv.c application.c rbtree.c \ - crc8gen.c crc16gen.c crc32gen.c crc64gen.c + conv.c application.c rbtree.c strrb.c \ + loggingrb.c crc8gen.c crc16gen.c crc32gen.c crc64gen.c
BUILT_SOURCES = crc8gen.c crc16gen.c crc32gen.c crc64gen.c
diff --git a/src/loggingrb.c b/src/loggingrb.c new file mode 100644 index 0000000..bb339eb --- /dev/null +++ b/src/loggingrb.c @@ -0,0 +1,98 @@ +/* Ringbuffer-backed logging support code */ + +/* (C) 2012-2013 by Katerina Barone-Adesi + * All Rights Reserved + * + * 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 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. + * + */ + +/*! \addtogroup logging + * @{ + */ + +/*! \file loggingrb.c */ + +#include <osmocom/core/strrb.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/loggingrb.h> + +static void _rb_output(struct log_target *target, + unsigned int level, const char *log) +{ + osmo_strrb_add(target->tgt_rb.rb, log); +} + +/*! \brief Return the number of log strings in the osmo_strrb-backed target. + * \param[in] target The target to search. + * + * \return The number of log strings in the osmo_strrb-backed target. + */ +size_t log_target_rb_used_size(struct log_target const *target) +{ + return osmo_strrb_elements(target->tgt_rb.rb); +} + +/*! \brief Return the capacity of the osmo_strrb-backed target. + * \param[in] target The target to search. + * + * Note that this is the capacity (aka max number of messages +1). + * It is not the number of unused message slots. + * \return The number of log strings in the osmo_strrb-backed target. + */ +size_t log_target_rb_avail_size(struct log_target const *target) +{ + struct osmo_strrb *rb = target->tgt_rb.rb; + return rb->size; +} + +/*! \brief Return the nth log entry in a target. + * \param[in] target The target to search. + * \param[in] logindex The index of the log entry/error message. + * + * \return A pointer to the nth message, or NULL if logindex is invalid. + */ +const char *log_target_rb_get(struct log_target const *target, size_t logindex) +{ + return osmo_strrb_get_nth(target->tgt_rb.rb, logindex); +} + +/*! \brief Create a new logging target for ringbuffer-backed logging. + * \param[in] size The size of the internal backing osmo_strrb (messages +1). + * \returns A log target in case of success, NULL in case of error. + */ +struct log_target *log_target_create_rb(size_t size) +{ + struct log_target *target; + struct osmo_strrb *rb; + + target = log_target_create(); + if (!target) + return NULL; + + rb = osmo_strrb_create(target, size); + if (!rb) { + log_target_destroy(target); + return NULL; + } + + target->tgt_rb.rb = rb; + target->type = LOG_TGT_TYPE_STRRB; + target->output = _rb_output; + + return target; +} + +/* @} */ diff --git a/src/strrb.c b/src/strrb.c new file mode 100644 index 0000000..580c65c --- /dev/null +++ b/src/strrb.c @@ -0,0 +1,170 @@ +/* Ringbuffer implementation, tailored for logging. + * This is a lossy ringbuffer. It keeps up to N of the newest messages, + * overwriting the oldest as newer ones come in. + * + * (C) 2012-2013, 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 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. + * + */ + +/*! \file strrb.c + * \brief Lossy string ringbuffer for logging; keeps newest messages. + */ + +#include <stdio.h> +#include <string.h> +#include <strings.h> + +#include <osmocom/core/strrb.h> + +/* Ringbuffer assumptions, invarients, and notes: + * - start is the index of the first used index slot in the ring buffer. + * - end is the index of the next index slot in the ring buffer. + * - start == end => buffer is empty + * - Consequence: the buffer can hold at most size - 1 messages + * (if this were not the case, full and empty buffers would be indistinguishable + * given the conventions in this implementation). + * - Whenever the ringbuffer is full, start is advanced. The second oldest + * message becomes unreachable by valid indexes (end is not a valid index) + * and the oldest message is overwritten (if there was a message there, which + * is the case unless this is the first time the ringbuffer becomes full). +*/ + +/*! \brief Create an empty, initialized osmo_strrb. + * \param[in] ctx The talloc memory context which should own this. + * \param[in] rb_size The number of messages the osmo_strrb can hold. + * \returns A struct osmo_strrb* on success, NULL in case of error. + * + * This function creates and initializes a ringbuffer. + */ + +struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size) +{ + struct osmo_strrb *rb = NULL; + unsigned int i; + + rb = talloc_zero(ctx, struct osmo_strrb); + if (!rb) + goto alloc_error; + + /* start and end are zero already, which is correct */ + rb->size = rb_size + 1; /* one space is overhead */ + + rb->buffer = talloc_array(rb, char *, rb->size); + if (!rb->buffer) + goto alloc_error; + for (i = 0; i < rb->size; i++) { + rb->buffer[i] = + talloc_zero_size(rb->buffer, RB_MAX_MESSAGE_SIZE); + if (!rb->buffer[i]) + goto alloc_error; + } + + return rb; + +alloc_error: /* talloc_free(NULL) is safe */ + talloc_free(rb); + return NULL; +} + +/*! \brief Check if an osmo_strrb is empty. + * \param[in] rb The osmo_strrb to check. + * \returns True if the osmo_strrb is empty, false otherwise. + */ +bool osmo_strrb_is_empty(const struct osmo_strrb *rb) +{ + return rb->end == rb->start; +} + +/*! \brief Return a pointer to the Nth string in the osmo_strrb. + * \param[in] rb The osmo_strrb to search. + * \param[in] string_index The index sought (N), zero-indexed. + * + * Return a pointer to the Nth string in the osmo_strrb. + * Return NULL if there is no Nth string. + * Note that N is zero-indexed. + * \returns A pointer to the target string on success, NULL in case of error. + */ +const char *osmo_strrb_get_nth(const struct osmo_strrb *rb, + unsigned int string_index) +{ + unsigned int offset = string_index + rb->start; + + if ((offset >= rb->size) && (rb->start > rb->end)) + offset -= rb->size; + if (_osmo_strrb_is_bufindex_valid(rb, offset)) + return rb->buffer[offset]; + + return NULL; +} + +bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb, + unsigned int bufi) +{ + if (osmo_strrb_is_empty(rb)) + return 0; + if ((bufi >= rb->size) || (bufi < 0)) + return 0; + if (rb->start < rb->end) + return (bufi >= rb->start) && (bufi < rb->end); + return (bufi < rb->end) || (bufi >= rb->start); +} + +/*! \brief Count the number of log messages in an osmo_strrb. + * \param[in] rb The osmo_strrb to count the elements of. + * + * \returns The number of log messages in the osmo_strrb. + */ +size_t osmo_strrb_elements(const struct osmo_strrb *rb) +{ + if (rb->end < rb->start) + return rb->end + (rb->size - rb->start); + + return rb->end - rb->start; +} + +/*! \brief Add a string to the osmo_strrb. + * \param[in] rb The osmo_strrb to add to. + * \param[in] data The string to add. + * + * Add a message to the osmo_strrb. + * Older messages will be overwritten as necessary. + * \returns 0 normally, 1 as a warning (ie, if data was truncated). + */ +int osmo_strrb_add(struct osmo_strrb *rb, const char *data) +{ + size_t len = strlen(data); + int ret = 0; + + if (len >= RB_MAX_MESSAGE_SIZE) { + len = RB_MAX_MESSAGE_SIZE - 1; + ret = 1; + } + + memcpy(rb->buffer[rb->end], data, len); + rb->buffer[rb->end][len] = '\0'; + + rb->end += 1; + rb->end %= rb->size; + + /* The buffer is full; oldest message is forgotten - see notes above */ + if (rb->end == rb->start) { + rb->start += 1; + rb->start %= rb->size; + } + return ret; +} diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c index d473f12..ace346a 100644 --- a/src/vty/logging_vty.c +++ b/src/vty/logging_vty.c @@ -27,8 +27,8 @@ #include <osmocom/core/talloc.h> #include <osmocom/core/logging.h> #include <osmocom/core/utils.h> - -//#include <openbsc/vty.h> +#include <osmocom/core/strrb.h> +#include <osmocom/core/loggingrb.h>
#include <osmocom/vty/command.h> #include <osmocom/vty/buffer.h> @@ -252,8 +252,8 @@ static void vty_print_logtarget(struct vty *vty, const struct log_info *info, #define SHOW_LOG_STR "Show current logging configuration\n"
DEFUN(show_logging_vty, - show_logging_vty_cmd, - "show logging vty", + show_logging_vty_cmd, + "show logging vty", SHOW_STR SHOW_LOG_STR "Show current logging configuration for this vty\n") { @@ -267,6 +267,33 @@ DEFUN(show_logging_vty, return CMD_SUCCESS; }
+DEFUN(show_alarms, + show_alarms_cmd, + "show alarms", + SHOW_STR SHOW_LOG_STR + "Show the contents of the logging ringbuffer\n") +{ + int i, num_alarms; + struct osmo_strrb *rb; + struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); + if (!tgt) { + vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s", + VTY_NEWLINE); + return CMD_WARNING; + } + + rb = tgt->tgt_rb.rb; + num_alarms = osmo_strrb_elements(rb); + + vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE); + + for (i = 0; i < num_alarms; i++) + vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i), + VTY_NEWLINE); + + return CMD_SUCCESS; +} + gDEFUN(cfg_description, cfg_description_cmd, "description .TEXT", "Save human-readable decription of the object\n" @@ -510,6 +537,49 @@ DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, return CMD_SUCCESS; }
+DEFUN(cfg_log_alarms, cfg_log_alarms_cmd, + "log alarms <2-32700>", + LOG_STR "Logging alarms to osmo_strrb\n" + "Maximum number of messages to log\n") +{ + struct log_target *tgt; + unsigned int rbsize = atoi(argv[0]); + + tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); + if (tgt) + log_target_destroy(tgt); + + tgt = log_target_create_rb(rbsize); + if (!tgt) { + vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s", + rbsize, VTY_NEWLINE); + return CMD_WARNING; + } + log_add_target(tgt); + + vty->index = tgt; + vty->node = CFG_LOG_NODE; + + return CMD_SUCCESS; +} + +DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd, + "no log alarms", + NO_STR LOG_STR "Logging alarms to osmo_strrb\n") +{ + struct log_target *tgt; + + tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); + if (!tgt) { + vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE); + return CMD_WARNING; + } + + log_target_destroy(tgt); + + return CMD_SUCCESS; +} + static int config_write_log_single(struct vty *vty, struct log_target *tgt) { int i; @@ -533,6 +603,10 @@ static int config_write_log_single(struct vty *vty, struct log_target *tgt) case LOG_TGT_TYPE_FILE: vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); break; + case LOG_TGT_TYPE_STRRB: + vty_out(vty, "log alarms %zu%s", + log_target_rb_avail_size(tgt), VTY_NEWLINE); + break; }
vty_out(vty, " logging filter all %u%s", @@ -590,6 +664,7 @@ void logging_vty_add_cmds(const struct log_info *cat) logging_level_cmd.doc = log_vty_command_description(cat); install_element_ve(&logging_level_cmd); install_element_ve(&show_logging_vty_cmd); + install_element_ve(&show_alarms_cmd);
install_node(&cfg_log_node, config_write_log); install_default(CFG_LOG_NODE); @@ -603,6 +678,8 @@ void logging_vty_add_cmds(const struct log_info *cat) install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); install_element(CONFIG_NODE, &cfg_log_file_cmd); install_element(CONFIG_NODE, &cfg_no_log_file_cmd); + install_element(CONFIG_NODE, &cfg_log_alarms_cmd); + install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd); #ifdef HAVE_SYSLOG_H install_element(CONFIG_NODE, &cfg_log_syslog_cmd); install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); diff --git a/tests/Makefile.am b/tests/Makefile.am index be0b5f4..bc9b7de 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 0000000..b59d2e8 --- /dev/null +++ b/tests/loggingrb/logging_test.err @@ -0,0 +1,3 @@ +[1;31mYou should see this +[0;m[1;32mYou should see this +[0;m \ 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 0000000..e69de29 diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c new file mode 100644 index 0000000..1ab5212 --- /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 0000000..b7c5e27 --- /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 2 + +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, 3); + 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 0000000..9ac5124 --- /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 5029b9e..21fad1d 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
On Thu, Feb 21, 2013 at 05:16:29AM +0000, Katerina Barone-Adesi wrote:
Dear Katerina,
thanks for the patch. There was one issue left, I tried to fix it myself, pushed it and only made it worse. So much for following the process. :)
Your fixed version made "log alarms 2" have be able to hold two entries but "write terminal" was emitting "log alarms 3". So I have decided to move the +1/-1 into the loggingrb.c. I have attempted to update the API docs at the places. Please have another look if I have done it correctly.
For new header files one needs to add them to the include/**Makefile.am files, otherwise a "make dist" will not package these files. This kind of error is caught on our jenkins system (which runs make distcheck).
One thing I didn't notice during the review is the usage of assert(). We are not checking if NDEBUG is in the CPPFLAGS/CFLAGS or not. This is why I normally come up with my own assertion macro to make sure that the assertion code is always enabled. It would be nice if you could come up with a patch to do that. My hint would be to take a look at cocci to do semantic patching.
The diff between your patch and my fixup attempt is attached to this emails.
thanks a lot for your contribution holger
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings. --- include/osmocom/core/utils.h | 7 +++ tests/lapd/lapd_test.c | 8 +-- tests/loggingrb/loggingrb_test.c | 3 +- tests/strrb/strrb_test.c | 126 +++++++++++++++++++-------------------- 4 files changed, 72 insertions(+), 72 deletions(-)
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 03861d7..4790386 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -51,6 +51,13 @@ do { \ rem -= ret; \ } while (0)
+#define ASSERT(exp) \ + if (!(exp)) { \ + printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ + abort(); \ + } + + /*! @} */
#endif diff --git a/tests/lapd/lapd_test.c b/tests/lapd/lapd_test.c index acd3cad..65206ed 100644 --- a/tests/lapd/lapd_test.c +++ b/tests/lapd/lapd_test.c @@ -21,6 +21,7 @@
#include <osmocom/core/application.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h> #include <osmocom/gsm/lapdm.h> #include <osmocom/gsm/rsl.h>
@@ -34,13 +35,6 @@ abort(); \ }
-#define ASSERT(exp) \ - if (!(exp)) { \ - printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ - abort(); \ - } - - static struct log_info info = {};
struct lapdm_polling_state { diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c index 1ab5212..7c08a7f 100644 --- a/tests/loggingrb/loggingrb_test.c +++ b/tests/loggingrb/loggingrb_test.c @@ -18,7 +18,6 @@ * 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> @@ -77,7 +76,7 @@ int main(int argc, char **argv) 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)); + 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 index abe649f..6dcf8e0 100644 --- a/tests/strrb/strrb_test.c +++ b/tests/strrb/strrb_test.c @@ -18,12 +18,12 @@ */
#include <stdio.h> -#include <assert.h> #include <string.h>
#include <osmocom/core/strrb.h> #include <osmocom/core/talloc.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h>
struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
@@ -77,98 +77,98 @@ void free_rbs(void)
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(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(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(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)); + 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); + 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)); + 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(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))); + 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); + 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); + 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))); + 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))); + 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))); + 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))); + 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))); + 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); } @@ -184,8 +184,8 @@ void test_long_msg(void) tests1 = malloc(test_size); tests2 = malloc(test_size); /* Be certain allocating memory worked before continuing */ - assert(tests1); - assert(tests2); + ASSERT(tests1); + ASSERT(tests2);
for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) { tests1[i] = 'a'; @@ -201,9 +201,9 @@ void test_long_msg(void) 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); + 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);
On Fri, Mar 01, 2013 at 06:43:33PM +0000, Katerina Barone-Adesi wrote:
Dear Katerina,
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings.
--- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h
+#define ASSERT(exp) \
it is good thinking to share the code between multiple users (e.g. in OpenBSC I have a bit of copy and paste for the assertion/testing macros) that we could clean up.
One thing we have to be careful with is to not pollute the global namespace, e.g. with something as generic as 'ASSERT' we either need to find another name or prefix it with OSMO_.
kind regards holger
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings. This uses the OSMO_ prefix as it's in the global namespace. --- include/osmocom/core/utils.h | 7 +++ tests/lapd/lapd_test.c | 62 +++++++++---------- tests/loggingrb/loggingrb_test.c | 3 +- tests/strrb/strrb_test.c | 126 +++++++++++++++++++-------------------- 4 files changed, 100 insertions(+), 98 deletions(-)
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 03861d7..8f7bee3 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -51,6 +51,13 @@ do { \ rem -= ret; \ } while (0)
+#define OSMO_ASSERT(exp) \ + if (!(exp)) { \ + printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ + abort(); \ + } + + /*! @} */
#endif diff --git a/tests/lapd/lapd_test.c b/tests/lapd/lapd_test.c index acd3cad..a60c45d 100644 --- a/tests/lapd/lapd_test.c +++ b/tests/lapd/lapd_test.c @@ -21,6 +21,7 @@
#include <osmocom/core/application.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h> #include <osmocom/gsm/lapdm.h> #include <osmocom/gsm/rsl.h>
@@ -34,13 +35,6 @@ abort(); \ }
-#define ASSERT(exp) \ - if (!(exp)) { \ - printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ - abort(); \ - } - - static struct log_info info = {};
struct lapdm_polling_state { @@ -101,9 +95,9 @@ static struct msgb *create_mm_id_req(void)
msg = msgb_from_array(mm, sizeof(mm)); msg->l2h = msg->data + 3; - ASSERT(msgb_l2len(msg) == 12); + OSMO_ASSERT(msgb_l2len(msg) == 12); msg->l3h = msg->l2h + 6; - ASSERT(msgb_l3len(msg) == 6); + OSMO_ASSERT(msgb_l3len(msg) == 6);
return msg; } @@ -113,7 +107,7 @@ static struct msgb *create_empty_msg(void) struct msgb *msg;
msg = msgb_from_array(NULL, 0); - ASSERT(msgb_l3len(msg) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 0); rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1); return msg; } @@ -155,7 +149,7 @@ static int send(struct msgb *in_msg, struct lapdm_channel *chan) pp.u.data.link_id = 0; /* feed into the LAPDm code of libosmogsm */ rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch); - ASSERT(rc == 0 || rc == -EBUSY); + OSMO_ASSERT(rc == 0 || rc == -EBUSY); return 0; }
@@ -172,12 +166,14 @@ static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_
if (state->bts_read == 0) { printf("BTS: Verifying CM request.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); - ASSERT(memcmp(in_msg->l3h, cm_padded, ARRAY_SIZE(cm_padded)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); + OSMO_ASSERT(memcmp(in_msg->l3h, cm_padded, + ARRAY_SIZE(cm_padded)) == 0); } else if (state->bts_read == 1) { printf("BTS: Verifying dummy message.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); - ASSERT(memcmp(in_msg->l3h, dummy1, ARRAY_SIZE(dummy1)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); + OSMO_ASSERT(memcmp(in_msg->l3h, dummy1, + ARRAY_SIZE(dummy1)) == 0); } else { printf("BTS: Do not know to verify: %d\n", state->bts_read); } @@ -210,23 +206,23 @@ static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx struct abis_rsl_rll_hdr hdr;
printf("MS: Verifying incoming primitive.\n"); - ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3); + OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
/* verify the header */ memset(&hdr, 0, sizeof(hdr)); rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF); hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP; - ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0); + OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
/* Verify the added RSL_IE_L3_INFO but we have a bug here */ - ASSERT(msg->data[6] == RSL_IE_L3_INFO); + OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO); #warning "RSL_IE_L3_INFO 16 bit length is wrong" /* ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x9c); */ /* this should be 0x0 and 0x0... but we have a bug */ } else if (state->ms_read == 1) { printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg)); - ASSERT(msgb_l3len(msg) == 3); - ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 3); + OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); } else { printf("MS: Do not know to verify: %d\n", state->ms_read); } @@ -274,13 +270,13 @@ static void test_lapdm_polling()
/* 2. Poll on the BTS for sending out a confirmation */ printf("\nConfirming\n"); - ASSERT(test_state.bts_read == 1) + OSMO_ASSERT(test_state.bts_read == 1); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); - ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); + OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 1); + OSMO_ASSERT(test_state.ms_read == 1);
/* 3. Send some data to the MS */ printf("\nSending back to MS\n"); @@ -289,35 +285,35 @@ static void test_lapdm_polling() CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2);
/* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0);
/* 3. And back to the BTS */ printf("\nSending back to BTS\n"); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
/* 4. And back to the MS, but let's move data/l2h apart */ - ASSERT(test_state.bts_read == 2) - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.bts_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); msgb_free(pp.oph.msg);
/* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0);
/* check sending an empty L3 message fails */ rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel); - ASSERT(rc == -1); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(rc == -1); + OSMO_ASSERT(test_state.ms_read == 2);
/* clean up */ lapdm_channel_exit(&bts_to_ms_channel); @@ -346,7 +342,7 @@ static void test_lapdm_early_release()
/* Send the release request */ rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel); - ASSERT(rc == -EINVAL); + OSMO_ASSERT(rc == -EINVAL);
/* clean up */ lapdm_channel_exit(&bts_to_ms_channel); diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c index 1ab5212..9957b53 100644 --- a/tests/loggingrb/loggingrb_test.c +++ b/tests/loggingrb/loggingrb_test.c @@ -18,7 +18,6 @@ * 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> @@ -77,7 +76,7 @@ int main(int argc, char **argv) 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)); + OSMO_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 index abe649f..6140ac9 100644 --- a/tests/strrb/strrb_test.c +++ b/tests/strrb/strrb_test.c @@ -18,12 +18,12 @@ */
#include <stdio.h> -#include <assert.h> #include <string.h>
#include <osmocom/core/strrb.h> #include <osmocom/core/talloc.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h>
struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
@@ -77,98 +77,98 @@ void free_rbs(void)
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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb1, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 1)); + OSMO_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)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb3, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 1)); + OSMO_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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb4, 1)); + OSMO_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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 1)); + OSMO_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); + OSMO_ASSERT(osmo_strrb_elements(rb0) == 0); + OSMO_ASSERT(osmo_strrb_elements(rb1) == 1); + OSMO_ASSERT(osmo_strrb_elements(rb2) == 2); + OSMO_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)); + OSMO_ASSERT(!osmo_strrb_get_nth(rb0, 0)); + OSMO_ASSERT(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1))); + OSMO_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))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0))); + OSMO_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))); + OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0))); + OSMO_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_ASSERT(rb->start == 0); + OSMO_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_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_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_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(osmo_strrb_elements(rb) == 3); + OSMO_ASSERT(!strcmp("b", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 2); + OSMO_ASSERT(rb->end == 1); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 3); + OSMO_ASSERT(rb->end == 2); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 1))); + OSMO_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))); + OSMO_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
talloc_free(rb); } @@ -184,8 +184,8 @@ void test_long_msg(void) tests1 = malloc(test_size); tests2 = malloc(test_size); /* Be certain allocating memory worked before continuing */ - assert(tests1); - assert(tests2); + OSMO_ASSERT(tests1); + OSMO_ASSERT(tests2);
for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) { tests1[i] = 'a'; @@ -201,9 +201,9 @@ void test_long_msg(void) 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); + OSMO_ASSERT(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1)); + OSMO_ASSERT(!rb_content[RB_MAX_MESSAGE_SIZE - 1]); + OSMO_ASSERT(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
free(tests2); talloc_free(rb);
Hi Katerina,
On Sun, Mar 03, 2013 at 10:36:52AM +0000, Katerina Barone-Adesi wrote:
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings. This uses the OSMO_ prefix as it's in the global namespace.
Two more ideas:
* since it is an error condition, fprintf(stderr). * it might make sense to also write it to the osmocom logging, using LOGL_FATAL / DLGLOBAL * while at it, we might also print a backtrace by means of osmo_backtrace().
What do you think?
On Sun, Mar 03, 2013 at 03:05:02PM +0000, Harald Welte wrote:
Hi,
Two more ideas:
- since it is an error condition, fprintf(stderr).
 - it might make sense to also write it to the osmocom logging, using LOGL_FATAL / DLGLOBAL
 - while at it, we might also print a backtrace by means of osmo_backtrace().
 What do you think?
the current usage will be inside the regression/unit testing, e.g. not having a dependency on the logging framework is quite nice to have.
For osmo_backtrace and frpintf would be nice. I think the value of osmo_backtrace will be a bit limited as we will have no symbol name for the test function (but this could come from the fprintf).
cheers holger
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings. This uses the OSMO_ prefix as it's in the global namespace. It shows a backtrace, and uses fprintf(stderr, ...) --- include/osmocom/core/utils.h | 8 +++ tests/lapd/lapd_test.c | 62 +++++++++---------- tests/loggingrb/loggingrb_test.c | 3 +- tests/strrb/strrb_test.c | 126 +++++++++++++++++++-------------------- 4 files changed, 101 insertions(+), 98 deletions(-)
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 03861d7..f8e6fc9 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -51,6 +51,14 @@ do { \ rem -= ret; \ } while (0)
+#define OSMO_ASSERT(exp) \ + if (!(exp)) { \ + fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ + osmo_generate_backtrace(); \ + abort(); \ + } + + /*! @} */
#endif diff --git a/tests/lapd/lapd_test.c b/tests/lapd/lapd_test.c index acd3cad..a60c45d 100644 --- a/tests/lapd/lapd_test.c +++ b/tests/lapd/lapd_test.c @@ -21,6 +21,7 @@
#include <osmocom/core/application.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h> #include <osmocom/gsm/lapdm.h> #include <osmocom/gsm/rsl.h>
@@ -34,13 +35,6 @@ abort(); \ }
-#define ASSERT(exp) \ - if (!(exp)) { \ - printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ - abort(); \ - } - - static struct log_info info = {};
struct lapdm_polling_state { @@ -101,9 +95,9 @@ static struct msgb *create_mm_id_req(void)
msg = msgb_from_array(mm, sizeof(mm)); msg->l2h = msg->data + 3; - ASSERT(msgb_l2len(msg) == 12); + OSMO_ASSERT(msgb_l2len(msg) == 12); msg->l3h = msg->l2h + 6; - ASSERT(msgb_l3len(msg) == 6); + OSMO_ASSERT(msgb_l3len(msg) == 6);
return msg; } @@ -113,7 +107,7 @@ static struct msgb *create_empty_msg(void) struct msgb *msg;
msg = msgb_from_array(NULL, 0); - ASSERT(msgb_l3len(msg) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 0); rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1); return msg; } @@ -155,7 +149,7 @@ static int send(struct msgb *in_msg, struct lapdm_channel *chan) pp.u.data.link_id = 0; /* feed into the LAPDm code of libosmogsm */ rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch); - ASSERT(rc == 0 || rc == -EBUSY); + OSMO_ASSERT(rc == 0 || rc == -EBUSY); return 0; }
@@ -172,12 +166,14 @@ static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_
if (state->bts_read == 0) { printf("BTS: Verifying CM request.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); - ASSERT(memcmp(in_msg->l3h, cm_padded, ARRAY_SIZE(cm_padded)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); + OSMO_ASSERT(memcmp(in_msg->l3h, cm_padded, + ARRAY_SIZE(cm_padded)) == 0); } else if (state->bts_read == 1) { printf("BTS: Verifying dummy message.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); - ASSERT(memcmp(in_msg->l3h, dummy1, ARRAY_SIZE(dummy1)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); + OSMO_ASSERT(memcmp(in_msg->l3h, dummy1, + ARRAY_SIZE(dummy1)) == 0); } else { printf("BTS: Do not know to verify: %d\n", state->bts_read); } @@ -210,23 +206,23 @@ static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx struct abis_rsl_rll_hdr hdr;
printf("MS: Verifying incoming primitive.\n"); - ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3); + OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
/* verify the header */ memset(&hdr, 0, sizeof(hdr)); rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF); hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP; - ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0); + OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
/* Verify the added RSL_IE_L3_INFO but we have a bug here */ - ASSERT(msg->data[6] == RSL_IE_L3_INFO); + OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO); #warning "RSL_IE_L3_INFO 16 bit length is wrong" /* ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x9c); */ /* this should be 0x0 and 0x0... but we have a bug */ } else if (state->ms_read == 1) { printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg)); - ASSERT(msgb_l3len(msg) == 3); - ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 3); + OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); } else { printf("MS: Do not know to verify: %d\n", state->ms_read); } @@ -274,13 +270,13 @@ static void test_lapdm_polling()
/* 2. Poll on the BTS for sending out a confirmation */ printf("\nConfirming\n"); - ASSERT(test_state.bts_read == 1) + OSMO_ASSERT(test_state.bts_read == 1); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); - ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); + OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 1); + OSMO_ASSERT(test_state.ms_read == 1);
/* 3. Send some data to the MS */ printf("\nSending back to MS\n"); @@ -289,35 +285,35 @@ static void test_lapdm_polling() CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2);
/* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0);
/* 3. And back to the BTS */ printf("\nSending back to BTS\n"); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
/* 4. And back to the MS, but let's move data/l2h apart */ - ASSERT(test_state.bts_read == 2) - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.bts_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); msgb_free(pp.oph.msg);
/* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0);
/* check sending an empty L3 message fails */ rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel); - ASSERT(rc == -1); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(rc == -1); + OSMO_ASSERT(test_state.ms_read == 2);
/* clean up */ lapdm_channel_exit(&bts_to_ms_channel); @@ -346,7 +342,7 @@ static void test_lapdm_early_release()
/* Send the release request */ rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel); - ASSERT(rc == -EINVAL); + OSMO_ASSERT(rc == -EINVAL);
/* clean up */ lapdm_channel_exit(&bts_to_ms_channel); diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c index 1ab5212..9957b53 100644 --- a/tests/loggingrb/loggingrb_test.c +++ b/tests/loggingrb/loggingrb_test.c @@ -18,7 +18,6 @@ * 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> @@ -77,7 +76,7 @@ int main(int argc, char **argv) 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)); + OSMO_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 index abe649f..6140ac9 100644 --- a/tests/strrb/strrb_test.c +++ b/tests/strrb/strrb_test.c @@ -18,12 +18,12 @@ */
#include <stdio.h> -#include <assert.h> #include <string.h>
#include <osmocom/core/strrb.h> #include <osmocom/core/talloc.h> #include <osmocom/core/logging.h> +#include <osmocom/core/utils.h>
struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
@@ -77,98 +77,98 @@ void free_rbs(void)
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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb1, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 1)); + OSMO_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)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb3, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 1)); + OSMO_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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb4, 1)); + OSMO_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)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 1)); + OSMO_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); + OSMO_ASSERT(osmo_strrb_elements(rb0) == 0); + OSMO_ASSERT(osmo_strrb_elements(rb1) == 1); + OSMO_ASSERT(osmo_strrb_elements(rb2) == 2); + OSMO_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)); + OSMO_ASSERT(!osmo_strrb_get_nth(rb0, 0)); + OSMO_ASSERT(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1))); + OSMO_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))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0))); + OSMO_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))); + OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0))); + OSMO_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_ASSERT(rb->start == 0); + OSMO_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_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_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_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(osmo_strrb_elements(rb) == 3); + OSMO_ASSERT(!strcmp("b", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 2); + OSMO_ASSERT(rb->end == 1); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 3); + OSMO_ASSERT(rb->end == 2); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 1))); + OSMO_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_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 1))); + OSMO_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))); + OSMO_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
talloc_free(rb); } @@ -184,8 +184,8 @@ void test_long_msg(void) tests1 = malloc(test_size); tests2 = malloc(test_size); /* Be certain allocating memory worked before continuing */ - assert(tests1); - assert(tests2); + OSMO_ASSERT(tests1); + OSMO_ASSERT(tests2);
for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) { tests1[i] = 'a'; @@ -201,9 +201,9 @@ void test_long_msg(void) 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); + OSMO_ASSERT(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1)); + OSMO_ASSERT(!rb_content[RB_MAX_MESSAGE_SIZE - 1]); + OSMO_ASSERT(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
free(tests2); talloc_free(rb);
Hi Katerina,
On Mon, Mar 11, 2013 at 09:36:32AM +0000, Katerina Barone-Adesi wrote:
Rationale: zecke pointed out that the tests should unconditionally assert, regardless of debug settings.
this looks fine now to me. Holger: Are you going to merge it?