Skip to content

Commit

Permalink
sys/log: Update selftests to account for header and trailers
Browse files Browse the repository at this point in the history
- Earlier these would only account for the entries, now they
  also account for headers and trailers
  • Loading branch information
vrahane committed Jun 10, 2024
1 parent 159a6f0 commit 8333460
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 90 deletions.
1 change: 1 addition & 0 deletions sys/log/full/selftest/fcb2_align1_imghash/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pkg.deps:
- "@apache-mynewt-core/sys/log/full"
- "@apache-mynewt-core/sys/log/full/selftest/util"
- "@apache-mynewt-core/test/testutil"
- "@apache-mynewt-core/boot/stub"
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pkg.deps:
- "@apache-mynewt-core/sys/log/full"
- "@apache-mynewt-core/sys/log/full/selftest/util"
- "@apache-mynewt-core/test/testutil"
- "@apache-mynewt-core/boot/stub"
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern struct fcb2 log_fcb;
#endif
extern struct log my_log;
extern char *ltu_str_logs[];
extern uint8_t dummy_log_arr[];

struct os_mbuf *ltu_flat_to_fragged_mbuf(const void *flat, int len,
int frag_sz);
Expand All @@ -49,6 +50,9 @@ void ltu_setup_2fcbs(struct fcb_log *fcb_log1, struct log *log1,
struct fcb_log *fcb_log2, struct log *log2);
void ltu_setup_cbmem(struct cbmem *cbmem, struct log *log);
void ltu_verify_contents(struct log *log);
uint16_t *ltu_get_ltu_off_arr(void);
uint16_t ltu_init_arr(void);
int ltu_num_strs(void);

TEST_SUITE_DECL(log_test_suite_cbmem_flat);
TEST_CASE_DECL(log_test_case_cbmem_append);
Expand Down
79 changes: 79 additions & 0 deletions sys/log/full/selftest/util/src/log_test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "log_test_util/log_test_util.h"
#include "log/log.h"

#if MYNEWT_VAL(LOG_FCB)
static struct flash_area fcb_areas[] = {
Expand Down Expand Up @@ -46,6 +47,37 @@ static struct flash_sector_range fcb_range = {
static int ltu_str_idx = 0;
static int ltu_str_max_idx = 0;

struct dummy_log {
struct log_entry_hdr hdr;
struct log_tlv tlv;
uint32_t num_entries;
};

struct dummy_log dummy_log = {
.hdr = {
.ue_ts = 1,
.ue_module = 2,
.ue_etype = 3,
.ue_flags = 0
#if MYNEWT_VAL(LOG_FLAGS_IMAGE_HASH)
| LOG_FLAGS_IMG_HASH
#endif
#if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT)
| LOG_FLAGS_TLV_SUPPORT
#endif
,
.ue_etype = 0,
.ue_imghash = {1, 2, 3, 4},
.ue_level = 3,
.ue_num_entries = 5
},
.tlv = {
.tag = LOG_TLV_NUM_ENTRIES,
.len = LOG_NUM_ENTRIES_SIZE
},
.num_entries = 0,
};

char *ltu_str_logs[] = {
"testdata",
"1testdata2",
Expand All @@ -54,6 +86,10 @@ char *ltu_str_logs[] = {
NULL
};

uint16_t ltu_off_arr[5];

uint8_t dummy_log_arr[2048];

static uint8_t ltu_cbmem_buf[2048];

int
Expand All @@ -66,6 +102,41 @@ ltu_num_strs(void)
return i;
}

uint16_t *
ltu_get_ltu_off_arr(void)
{
return ltu_off_arr;
}

uint16_t
ltu_init_arr(void)
{
int i;
uint16_t offset = 0;

for (i = 0; i < ltu_num_strs(); i++) {
TEST_ASSERT_FATAL(offset <= 2048);
ltu_off_arr[i] = offset;
memcpy(dummy_log_arr + offset, &dummy_log.hdr, LOG_BASE_ENTRY_HDR_SIZE);
offset += LOG_BASE_ENTRY_HDR_SIZE;
#if MYNEWT_VAL(LOG_FLAGS_IMAGE_HASH)
memcpy(dummy_log_arr + offset,
dummy_log.hdr.ue_imghash, LOG_IMG_HASHLEN);
offset += LOG_IMG_HASHLEN;
#endif
memcpy(dummy_log_arr + offset, ltu_str_logs[i], strlen(ltu_str_logs[i]));
offset += strlen(ltu_str_logs[i]);
#if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_ENTRIES)
memcpy(dummy_log_arr + offset, &dummy_log.tlv, sizeof(struct log_tlv));
offset += sizeof(struct log_tlv);
memcpy(dummy_log_arr + offset, &dummy_log.num_entries, LOG_NUM_ENTRIES_SIZE);
offset += LOG_NUM_ENTRIES_SIZE;
#endif
}
ltu_off_arr[i] = offset;
return offset;
}

struct os_mbuf *
ltu_flat_to_fragged_mbuf(const void *flat, int len, int frag_sz)
{
Expand Down Expand Up @@ -191,13 +262,21 @@ ltu_walk_verify(struct log *log, struct log_offset *log_offset,
int dlen;
uint16_t hdr_len;
uint16_t trailer_len;
uint16_t offset = 0;

TEST_ASSERT(ltu_str_idx < ltu_str_max_idx);

/*** Verify contents using single read. */

rc = log_read(log, dptr, &ueh, 0, LOG_BASE_ENTRY_HDR_SIZE);
TEST_ASSERT(rc == LOG_BASE_ENTRY_HDR_SIZE);
offset = LOG_BASE_ENTRY_HDR_SIZE;

if (ueh.ue_flags & LOG_FLAGS_IMG_HASH) {
rc = log_read(log, dptr, data, offset, LOG_IMG_HASHLEN);
TEST_ASSERT(rc == LOG_IMG_HASHLEN);
offset += LOG_IMG_HASHLEN;
}

hdr_len = log_hdr_len(&ueh);
trailer_len = log_trailer_len(&ueh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ TEST_CASE_SELF(log_test_case_cbmem_append)
{
struct cbmem cbmem;
struct log log;
uint8_t buf[256];
char *str;
int body_len;
uint16_t len = 0;
uint16_t *off_arr;
int i;
int rc;
int num_strs = ltu_num_strs();
struct log_entry_hdr *hdr;

ltu_setup_cbmem(&cbmem, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

body_len = strlen(str);
memcpy(buf + LOG_HDR_SIZE, str, body_len);
rc = log_append_typed(&log, 0, 0, LOG_ETYPE_STRING, buf, body_len);
for (i = 0; i < num_strs; i++) {
hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]);
len = off_arr[i+1] - off_arr[i] - log_hdr_len(hdr) - log_trailer_len(hdr);
rc = log_append_typed(&log, 2, 3, LOG_ETYPE_STRING,
dummy_log_arr + off_arr[i],
len);
TEST_ASSERT_FATAL(rc == 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,28 @@ TEST_CASE_SELF(log_test_case_cbmem_append_body)
{
struct cbmem cbmem;
struct log log;
char *str;
uint16_t len = 0;
uint16_t *off_arr;
int i;
int rc;
struct log_entry_hdr *hdr;
int num_strs = ltu_num_strs();

ltu_setup_cbmem(&cbmem, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

log_append_body(&log, 0, 0, LOG_ETYPE_STRING, str, strlen(str));
for (i = 0; i < num_strs; i++) {
hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]);
len = off_arr[i + 1] - off_arr[i] -
log_hdr_len(hdr) - log_trailer_len(hdr);
rc = log_append_body(&log, 2, 3, LOG_ETYPE_STRING,
dummy_log_arr + off_arr[i] + log_hdr_len(hdr),
len);
TEST_ASSERT_FATAL(rc == 0);
}

ltu_verify_contents(&log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@
TEST_CASE_SELF(log_test_case_cbmem_append_mbuf)
{
struct cbmem cbmem;
struct os_mbuf *om;
struct log log;
char *str;
int rc;
struct os_mbuf *om;
uint16_t len = 0;
uint16_t *off_arr;
int i;
int rc;
int num_strs = ltu_num_strs();

ltu_setup_cbmem(&cbmem, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

for (i = 0; i < num_strs; i++) {
len = off_arr[i+1] - off_arr[i];
/* Split chain into several mbufs. */
om = ltu_flat_to_fragged_mbuf(str, strlen(str), 2);

/* Prepend space for the entry header. */
om = os_mbuf_prepend(om, LOG_HDR_SIZE);
TEST_ASSERT(om != NULL);
om = ltu_flat_to_fragged_mbuf(dummy_log_arr + off_arr[i],
len, 2);

rc = log_append_mbuf_typed(&log, 0, 0, LOG_ETYPE_STRING, om);
rc = log_append_mbuf_typed(&log, 2, 3, LOG_ETYPE_STRING, om);
TEST_ASSERT_FATAL(rc == 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ TEST_CASE_SELF(log_test_case_cbmem_append_mbuf_body)
struct cbmem cbmem;
struct os_mbuf *om;
struct log log;
char *str;
int rc;
int i;
uint16_t len;
int num_strs = ltu_num_strs();
struct log_entry_hdr *hdr;
uint16_t *off_arr;

ltu_setup_cbmem(&cbmem, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

for (i = 0; i < num_strs; i++) {
hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]);
len = off_arr[i + 1] - off_arr[i] -
log_hdr_len(hdr);

/* Split chain into several mbufs. */
om = ltu_flat_to_fragged_mbuf(str, strlen(str), 2);
om = ltu_flat_to_fragged_mbuf(dummy_log_arr + off_arr[i] + log_hdr_len(hdr),
len, 2);

rc = log_append_mbuf_body(&log, 0, 0, LOG_ETYPE_STRING, om);
rc = log_append_mbuf_body(&log, 2, 3, LOG_ETYPE_STRING, om);
TEST_ASSERT_FATAL(rc == 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ TEST_CASE_SELF(log_test_case_cbmem_printf)
{
struct cbmem cbmem;
struct log log;
char *str;
int i;
uint16_t len = 0;
int num_strs = ltu_num_strs();
uint16_t *off_arr;
struct log_entry_hdr *hdr;
char data[256];

ltu_setup_cbmem(&cbmem, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

log_printf(&log, 0, 0, str, strlen(str));
for (i = 0; i < num_strs; i++) {
hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]);
len = off_arr[i+1] - off_arr[i] - log_hdr_len(hdr) - log_trailer_len(hdr);
memcpy(data, dummy_log_arr + off_arr[i] + log_hdr_len(hdr),
len);
data[len] = '\0';
log_printf(&log, 0, 0, data, len);
}

ltu_verify_contents(&log);
Expand Down
27 changes: 16 additions & 11 deletions sys/log/full/selftest/util/src/testcases/log_test_case_fcb_append.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ TEST_CASE_SELF(log_test_case_fcb_append)
{
struct fcb_log fcb_log;
struct log log;
uint8_t buf[256];
char *str;
int body_len;
uint16_t len = 0;
uint16_t *off_arr;
int i;
int rc;
int num_strs = ltu_num_strs();
struct log_entry_hdr *hdr;

ltu_setup_fcb(&fcb_log, &log);
len = ltu_init_arr();
TEST_ASSERT_FATAL(len != 0);

for (i = 0; ; i++) {
str = ltu_str_logs[i];
if (!str) {
break;
}
off_arr = ltu_get_ltu_off_arr();
TEST_ASSERT_FATAL(off_arr != NULL);

body_len = strlen(str);
memcpy(buf + LOG_HDR_SIZE, str, body_len);
log_append_typed(&log, 0, 0, LOG_ETYPE_STRING, buf, body_len);
for (i = 0;i < num_strs; i++) {
hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]);
len = off_arr[i+1] - off_arr[i] - log_hdr_len(hdr) - log_trailer_len(hdr);
rc = log_append_typed(&log, 2, 3, LOG_ETYPE_STRING,
dummy_log_arr + off_arr[i],
len);
TEST_ASSERT_FATAL(rc == 0);
}

ltu_verify_contents(&log);
Expand Down
Loading

0 comments on commit 8333460

Please sign in to comment.