From fafb8fcb735c5ad398a1bd7304faa38d2f2c0700 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Thu, 12 Jun 2025 08:54:31 +0200 Subject: [PATCH] log/full: Fix log level unit test Unit test was broken in several ways and pass verdict was not reliable. First of log_offset used for starting walk was never initialized and received random data. By accident this data had non zero ts_stamp that resulted in expected start from the beginning (it stack was different it could look for timestamp instead). log_last_walk function that is called on each enumerated entry used uninitialized lo_index value to report last entry index. The assumption that lo_index has means something in this context was also incorrect as this value never changes in walk functions. It looks like original code expected this value to have index of log entry which it never does. Code also did not expected that very first entry added to the log will have index 0 (which was never modified correctly). To read entry index log_read function must be called. So now walk callback function does read index of the log. After fixing incorrect code few more conditions are tested. - more logs that should not be stored are generated with different log levels - logs with log level above threshold are also tested - additional check if index of logs increases Signed-off-by: Jerzy Kasenberg --- .../util/src/testcases/log_test_case_level.c | 61 +++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/sys/log/full/selftest/util/src/testcases/log_test_case_level.c b/sys/log/full/selftest/util/src/testcases/log_test_case_level.c index 9957eda83c..4e357f0933 100644 --- a/sys/log/full/selftest/util/src/testcases/log_test_case_level.c +++ b/sys/log/full/selftest/util/src/testcases/log_test_case_level.c @@ -19,37 +19,43 @@ #include "log_test_util/log_test_util.h" +struct log_walk_data { + struct log_offset log_offset; + size_t walk_count; + int idx; +}; + static int log_last_walk(struct log *log, struct log_offset *log_offset, const void *dptr, uint16_t len) { - uint32_t *idx; + struct log_walk_data *wd = (struct log_walk_data *)log_offset; + struct log_entry_hdr hdr; + + log_read(log, dptr, &hdr, 0, sizeof(hdr)); - idx = log_offset->lo_arg; - *idx = log_offset->lo_index; + wd->idx = hdr.ue_index; + wd->walk_count++; return 0; } static uint32_t -log_last(struct log *log) +log_last(struct log *log, struct log_walk_data *wd) { - struct log_offset lo; - uint32_t idx; - - idx = 0; + memset(wd, 0, sizeof(*wd)); - lo.lo_arg = &idx; - log_walk(log, log_last_walk, &lo); + log_walk(log, log_last_walk, &wd->log_offset); - return idx; + return wd->walk_count; } TEST_CASE_SELF(log_test_case_level) { struct cbmem cbmem; struct log log; - uint32_t idx; + struct log_walk_data wd; + size_t log_count; int rc; int i; @@ -78,11 +84,32 @@ TEST_CASE_SELF(log_test_case_level) rc = log_level_set(100, 4); TEST_ASSERT(rc == 0); - idx = log_last(&log); - log_printf(&log, 100, 1, "hello"); - TEST_ASSERT(log_last(&log) == idx); + /* wd is used several times and it is cleared at the beginning of log_last */ + log_count = log_last(&log, &wd); + /* Log is empty */ + TEST_ASSERT(log_count == 0); + /* Write logs that should be dropped due to level */ + for (uint8_t level = 0; level < 4; ++level) { + log_printf(&log, 100, 1, "hello"); + /* Log is still empty? */ + log_count = log_last(&log, &wd); + TEST_ASSERT(log_count == 0); + } /* Ensure log write when level is equal. */ - log_printf(&log, 100, 4, "hello"); - TEST_ASSERT(log_last(&log) > idx); + log_printf(&log, 100, 4, "hello1"); + log_count = log_last(&log, &wd); + int log_idx = wd.idx; + TEST_ASSERT(log_count == 1); + /* Ensure log write when level is equal. */ + log_printf(&log, 100, 4, "hello2"); + log_count = log_last(&log, &wd); + TEST_ASSERT(log_count == 2); + TEST_ASSERT(log_idx < wd.idx); + /* Write log with higher level */ + log_idx = wd.idx; + log_printf(&log, 100, 5, "hello3"); + log_count = log_last(&log, &wd); + TEST_ASSERT(log_count == 3); + TEST_ASSERT(log_idx < wd.idx); }