From 7f369d3372550c9e19dd65a507d07b0e303592f0 Mon Sep 17 00:00:00 2001 From: Vipul Rahane Date: Tue, 4 Jun 2024 14:48:44 -0700 Subject: [PATCH] sys/log/full/selftest: Add unit tests for log fcb2 image hash and num_entries --- .../full/selftest/fcb2_align1_imghash/pkg.yml | 29 ++++++++++++++++ .../fcb2_align1_imghash/src/log_test_align1.c | 33 +++++++++++++++++++ .../selftest/fcb2_align1_imghash/syscfg.yml | 25 ++++++++++++++ .../fcb2_align1_imghash_num_entries/pkg.yml | 29 ++++++++++++++++ .../src/log_test_align1.c | 33 +++++++++++++++++++ .../syscfg.yml | 27 +++++++++++++++ .../full/selftest/util/src/log_test_util.c | 12 ++++++- sys/log/full/src/log.c | 7 ++-- 8 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 sys/log/full/selftest/fcb2_align1_imghash/pkg.yml create mode 100644 sys/log/full/selftest/fcb2_align1_imghash/src/log_test_align1.c create mode 100644 sys/log/full/selftest/fcb2_align1_imghash/syscfg.yml create mode 100644 sys/log/full/selftest/fcb2_align1_imghash_num_entries/pkg.yml create mode 100644 sys/log/full/selftest/fcb2_align1_imghash_num_entries/src/log_test_align1.c create mode 100644 sys/log/full/selftest/fcb2_align1_imghash_num_entries/syscfg.yml diff --git a/sys/log/full/selftest/fcb2_align1_imghash/pkg.yml b/sys/log/full/selftest/fcb2_align1_imghash/pkg.yml new file mode 100644 index 0000000000..b883f2e8de --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash/pkg.yml @@ -0,0 +1,29 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +pkg.name: sys/log/full/selftest/fcb2_align1_imghash +pkg.type: unittest +pkg.description: "Log unit tests; FCB2 flash-alignment=1." +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/sys/console/stub" + - "@apache-mynewt-core/sys/log/full" + - "@apache-mynewt-core/sys/log/full/selftest/util" + - "@apache-mynewt-core/test/testutil" diff --git a/sys/log/full/selftest/fcb2_align1_imghash/src/log_test_align1.c b/sys/log/full/selftest/fcb2_align1_imghash/src/log_test_align1.c new file mode 100644 index 0000000000..6fb73ffba7 --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash/src/log_test_align1.c @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "os/mynewt.h" +#include "log_test_util/log_test_util.h" + +int +main(int argc, char **argv) +{ + log_test_suite_cbmem_flat(); + log_test_suite_cbmem_mbuf(); + log_test_suite_fcb_flat(); + log_test_suite_fcb_mbuf(); + log_test_suite_misc(); + + return tu_any_failed; +} diff --git a/sys/log/full/selftest/fcb2_align1_imghash/syscfg.yml b/sys/log/full/selftest/fcb2_align1_imghash/syscfg.yml new file mode 100644 index 0000000000..fba07594da --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash/syscfg.yml @@ -0,0 +1,25 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +syscfg.vals: + LOG_FCB2: 1 + MCU_FLASH_MIN_WRITE_SIZE: 1 + LOG_FLAGS_IMAGE_HASH: 1 + + # The mbuf append tests allocate lots of mbufs; ensure no exhaustion. + MSYS_1_BLOCK_COUNT: 1000 diff --git a/sys/log/full/selftest/fcb2_align1_imghash_num_entries/pkg.yml b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/pkg.yml new file mode 100644 index 0000000000..1cf9408541 --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/pkg.yml @@ -0,0 +1,29 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +pkg.name: sys/log/full/selftest/fcb2_align1_imghash_num_entries +pkg.type: unittest +pkg.description: "Log unit tests; FCB2 flash-alignment=1." +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/sys/console/stub" + - "@apache-mynewt-core/sys/log/full" + - "@apache-mynewt-core/sys/log/full/selftest/util" + - "@apache-mynewt-core/test/testutil" diff --git a/sys/log/full/selftest/fcb2_align1_imghash_num_entries/src/log_test_align1.c b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/src/log_test_align1.c new file mode 100644 index 0000000000..6fb73ffba7 --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/src/log_test_align1.c @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "os/mynewt.h" +#include "log_test_util/log_test_util.h" + +int +main(int argc, char **argv) +{ + log_test_suite_cbmem_flat(); + log_test_suite_cbmem_mbuf(); + log_test_suite_fcb_flat(); + log_test_suite_fcb_mbuf(); + log_test_suite_misc(); + + return tu_any_failed; +} diff --git a/sys/log/full/selftest/fcb2_align1_imghash_num_entries/syscfg.yml b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/syscfg.yml new file mode 100644 index 0000000000..4da7c52913 --- /dev/null +++ b/sys/log/full/selftest/fcb2_align1_imghash_num_entries/syscfg.yml @@ -0,0 +1,27 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +syscfg.vals: + LOG_FCB2: 1 + MCU_FLASH_MIN_WRITE_SIZE: 1 + LOG_FLAGS_IMAGE_HASH: 1 + LOG_FLAGS_TLV_SUPPORT: 1 + LOG_TLV_NUM_ENTRIES: 1 + + # The mbuf append tests allocate lots of mbufs; ensure no exhaustion. + MSYS_1_BLOCK_COUNT: 1000 diff --git a/sys/log/full/selftest/util/src/log_test_util.c b/sys/log/full/selftest/util/src/log_test_util.c index 2447cbb616..c9ea5f5bb2 100644 --- a/sys/log/full/selftest/util/src/log_test_util.c +++ b/sys/log/full/selftest/util/src/log_test_util.c @@ -190,6 +190,7 @@ ltu_walk_verify(struct log *log, struct log_offset *log_offset, char data[128]; int dlen; uint16_t hdr_len; + uint16_t trailer_len; TEST_ASSERT(ltu_str_idx < ltu_str_max_idx); @@ -199,7 +200,8 @@ ltu_walk_verify(struct log *log, struct log_offset *log_offset, TEST_ASSERT(rc == LOG_BASE_ENTRY_HDR_SIZE); hdr_len = log_hdr_len(&ueh); - dlen = len - hdr_len; + trailer_len = log_trailer_len(&ueh); + dlen = len - hdr_len - trailer_len; TEST_ASSERT(dlen < sizeof(data)); rc = log_read(log, dptr, data, hdr_len, dlen); @@ -218,6 +220,12 @@ ltu_walk_verify(struct log *log, struct log_offset *log_offset, rc = log_read_body(log, dptr, data, 0, dlen); TEST_ASSERT(rc == dlen); +#if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_ENTRIES) + struct log_tlv tlv; + rc = log_read_trailer(log, dptr, LOG_TLV_NUM_ENTRIES, &tlv); + TEST_ASSERT(rc == 0); +#endif + TEST_ASSERT(strlen(ltu_str_logs[ltu_str_idx]) == dlen); TEST_ASSERT(!memcmp(ltu_str_logs[ltu_str_idx], data, dlen)); @@ -263,6 +271,8 @@ ltu_walk_body_verify(struct log *log, struct log_offset *log_offset, TEST_ASSERT(len < sizeof(data)); + len -= log_trailer_len(ueh); + rc = log_read_body(log, dptr, data, 0, len); TEST_ASSERT(rc == len); diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c index daa55efc95..5baa25ee44 100644 --- a/sys/log/full/src/log.c +++ b/sys/log/full/src/log.c @@ -604,12 +604,11 @@ log_trailer_len(const struct log_entry_hdr *hdr) uint16_t len = 0; if (hdr->ue_flags & LOG_FLAGS_TLV_SUPPORT) { - len += sizeof(struct log_tlv); - } - #if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_ENTRIES) - len += LOG_NUM_ENTRIES_SIZE; + len += sizeof(struct log_tlv); + len += LOG_NUM_ENTRIES_SIZE; #endif + } return len; }