da14531: add unit tests for da14531_protocol_format
What changed, and why it matters
This commit adds unit tests for a Bluetooth/serial framing function and slightly relaxes four buffer-size checks from '<' to '<='. The change is described as 'off by one in the safe conservative direction', meaning the old checks rejected a perfectly-sized buffer as too small. The patch does not fix a memory corruption bug; it removes a false-positive assertion that could have caused a harmless device reset when a caller supplied an exactly-sized buffer. No security vulnerability is disclosed or demonstrated.
No immediate action required. Treat as routine hardening/test-coverage commit. Reviewers may verify that the new '<=' bounds exactly match the bytes written in each code path and that the added unit tests exercise the corrected boundary cases.
Security signals we found
Boundary-condition correction in buffer-length assertions
Addition of unit-test coverage for serial-link framing and escaping
No overflow or out-of-bounds write introduced by the diff
ASSERT-based hardening remains in place; only false-positive threshold reduced
Evidence from the diff
In src/da14531/da14531_protocol.c, four ASSERTs in da14531_protocol_format and _serial_link_format_byte are changed from strict less-than to less-than-or-equal. The function writes one or two bytes at idx and then increments, so the correct pre-write check is idx < buf_len (one byte) or idx + 1 < buf_len (two bytes), i.e. idx + 2 <= buf_len. The old checks were conservative by one byte and would assert on a buffer that was actually large enough. The commit also adds 500+ lines of cmocka unit tests covering SOF/escape framing, CRC escaping, zero-length payloads, and boundary payload lengths. No overflow is introduced; the change only permits the exact-minimum-size buffer that was previously rejected.
Changed components
src/da14531/da14531_protocol.ctest/unit-test/test_da14531.ctest/unit-test/CMakeLists.txtInspect captured patch +513 / −4
diff --git a/src/da14531/da14531_protocol.c b/src/da14531/da14531_protocol.c
index 74c8258..f4d269b 100644
--- a/src/da14531/da14531_protocol.c
+++ b/src/da14531/da14531_protocol.c
@@ -328,7 +328,7 @@ static struct da14531_protocol_frame* _serial_link_in_poll(
static void _serial_link_format_byte(uint8_t data, uint8_t* buf, uint16_t buf_len, uint16_t* idx)
{
- ASSERT(*idx + 2 < buf_len);
+ ASSERT(*idx + 2 <= buf_len);
(void)buf_len;
switch (data) {
case SL_SOF:
@@ -352,7 +352,7 @@ uint16_t da14531_protocol_format(
uint16_t idx = 0;
crc_t crc = crc_init();
- ASSERT(idx + 1 < buf_len);
+ ASSERT(idx < buf_len);
buf[idx++] = SL_SOF;
crc = crc_update(crc, &type, 1);
@@ -378,7 +378,7 @@ uint16_t da14531_protocol_format(
_serial_link_format_byte(crc & 0xff, buf, buf_len, &idx);
crc >>= 8;
}
- ASSERT(idx + 1 < buf_len);
+ ASSERT(idx < buf_len);
buf[idx++] = SL_SOF;
return idx;
}
@@ -393,7 +393,7 @@ struct da14531_protocol_frame* da14531_protocol_poll(
uint8_t tmp[12 + 64 * 2];
int len = da14531_protocol_format(
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA, *hww_data, 64);
- ASSERT(len < (int)sizeof(tmp));
+ ASSERT(len <= (int)sizeof(tmp));
// If it won't fit, try again later
if (ringbuffer_num(out_queue) + len > out_queue->size) {
util_log("ringbuffer full");
diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt
index 7fb3999..00d42b1 100644
--- a/test/unit-test/CMakeLists.txt
+++ b/test/unit-test/CMakeLists.txt
@@ -57,6 +57,8 @@ else()
""
memory
"-Wl,--wrap=memory_read_chunk_fake,--wrap=memory_write_chunk_fake,--wrap=rust_noise_generate_static_private_key,--wrap=memory_read_shared_bootdata_fake,--wrap=memory_write_to_address_fake,--wrap=random_32_bytes_mcu"
+ da14531
+ ""
util
""
ugui
@@ -85,6 +87,7 @@ else()
)
target_include_directories(${EXE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
+ ${CMAKE_SOURCE_DIR}/external/asf4-drivers/hal/utils/include
)
if(TEST_NAME STREQUAL "optiga")
target_sources(${EXE} PRIVATE
diff --git a/test/unit-test/test_da14531.c b/test/unit-test/test_da14531.c
new file mode 100644
index 0000000..5fea5f0
--- /dev/null
+++ b/test/unit-test/test_da14531.c
@@ -0,0 +1,506 @@
+// Copyright 2025 Shift Crypto AG
+//
+// Licensed 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 "da14531/da14531_protocol.h"
+
+#include <limits.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmocka.h>
+
+#define SL_SOF 0x7E
+#define SL_ESCAPE 0x7D
+
+static void _assert_format_case_fixture(
+ enum da14531_protocol_packet_type type,
+ const uint8_t* payload,
+ uint16_t payload_len,
+ const uint8_t* expected,
+ size_t expected_len)
+{
+ size_t buf_len = expected_len;
+ assert_true(buf_len <= UINT16_MAX);
+
+ uint8_t* actual = malloc(buf_len);
+ assert_non_null(actual);
+ memset(actual, 0xaa, buf_len);
+
+ uint16_t actual_len =
+ da14531_protocol_format(actual, (uint16_t)buf_len, type, payload, payload_len);
+
+ assert_int_equal(actual_len, expected_len);
+ assert_memory_equal(actual, expected, expected_len);
+
+ free(actual);
+}
+
+// Verifies a small payload that requires no escaping is formatted correctly.
+static void _test_format_basic_no_escaping(void** state)
+{
+ (void)state;
+
+ const uint8_t payload[] = {0x01, 0x02, 0x03};
+ const uint8_t expected[] = {0x7e, 0xb4, 0x03, 0x00, 0x01, 0x02, 0x03, 0x4f, 0x15, 0x7e};
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA,
+ payload,
+ (uint16_t)sizeof(payload),
+ expected,
+ sizeof(expected));
+}
+
+// Verifies payload bytes requiring escaping are encoded correctly.
+static void _test_format_payload_bytes_escaped(void** state)
+{
+ (void)state;
+
+ const uint8_t payload[] = {SL_SOF, SL_ESCAPE, 0x55};
+ const uint8_t expected[] = {
+ 0x7e, 0xb4, 0x03, 0x00, 0x7d, 0x5e, 0x7d, 0x5d, 0x55, 0xde, 0xc3, 0x7e};
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA,
+ payload,
+ (uint16_t)sizeof(payload),
+ expected,
+ sizeof(expected));
+}
+
+// Verifies framing/CRC for a zero-length payload.
+static void _test_format_zero_length_payload(void** state)
+{
+ (void)state;
+
+ uint8_t payload_dummy = 0xaa;
+ const uint8_t expected[] = {0x7e, 0xb4, 0x00, 0x00, 0x40, 0x26, 0x7e};
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, &payload_dummy, 0, expected, sizeof(expected));
+}
+
+// Verifies every single-byte payload value formats correctly.
+static void _test_format_payload_all_bytes(void** state)
+{
+ (void)state;
+ const uint8_t fixture_payload_all_bytes[256][9] = {
+ {0x7e, 0x3c, 0x01, 0x00, 0x00, 0x5d, 0x90, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x01, 0x9c, 0x50, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x02, 0xdc, 0x51, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x03, 0x1d, 0x91, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x04, 0x5c, 0x53, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x05, 0x9d, 0x93, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x06, 0xdd, 0x92, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x07, 0x1c, 0x52, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x08, 0x5c, 0x56, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x09, 0x9d, 0x96, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x0a, 0xdd, 0x97, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x0b, 0x1c, 0x57, 0x7e, 0x00},
+ {0x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x45, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x33, 0x1d, 0x85, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x34, 0x5c, 0x47, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x35, 0x9d, 0x87, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x36, 0xdd, 0x86, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x37, 0x1c, 0x46, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x38, 0x5c, 0x42, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x39, 0x9d, 0x82, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3a, 0xdd, 0x83, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3b, 0x1c, 0x43, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3c, 0x5d, 0x81, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3d, 0x9c, 0x41, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3e, 0xdc, 0x40, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x3f, 0x1d, 0x80, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x40, 0x5c, 0x60, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x41, 0x9d, 0xa0, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x42, 0xdd, 0xa1, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x43, 0x1c, 0x61, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x44, 0x5d, 0xa3, 0x7e, 0x00},
+ {0x7e, 0x3c, 0x01, 0x00, 0x45, 0x9c, 0x63, 0x7e, 0x00},
+ {0x7e, 0x3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, 8, 8, 8, 9, 8, 8, 8, 8, 9, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ };
+ uint8_t payload = 0;
+ for (int i = 0; i <= 0xff; i++) {
+ payload = (uint8_t)i;
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA,
+ &payload,
+ 1,
+ fixture_payload_all_bytes[i],
+ fixture_payload_all_bytes_len[i]);
+ }
+}
+
+// Verifies escaping when the low payload length byte matches SOF/ESCAPE.
+static void _test_format_len_low_escaping(void** state)
+{
+ (void)state;
+ uint8_t* payload = malloc(0x7E);
+ assert_non_null(payload);
+ memset(payload, 0x11, 0x7E);
+ const uint8_t expected_7d[] = {
+ 0x7e, 0xb4, 0x7d, 0x5d, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x08, 0xa1, 0x7e};
+ const uint8_t expected_7e[] = {
+ 0x7e, 0xb4, 0x7d, 0x5e, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x60, 0x2e, 0x7e};
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, payload, 0x7D, expected_7d, sizeof(expected_7d));
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA, payload, 0x7E, expected_7e, sizeof(expected_7e));
+ free(payload);
+}
+
+// Verifies escaping when the high payload length byte matches SOF/ESCAPE.
+static void _test_format_len_high_escaping(void** state)
+{
+ (void)state;
+ const uint16_t payload_len_7d00 = 0x7D00;
+ const uint16_t payload_len_7e00 = 0x7E00;
+ uint8_t* payload = malloc(payload_len_7e00);
+ assert_non_null(payload);
+ memset(payload, 0x22, payload_len_7e00);
+
+ const size_t expected_len_7d00 = 32008;
+ const size_t expected_len_7e00 = 32264;
+ uint8_t* expected_7d00 = malloc(expected_len_7d00);
+ uint8_t* expected_7e00 = malloc(expected_len_7e00);
+ assert_non_null(expected_7d00);
+ assert_non_null(expected_7e00);
+
+ size_t idx = 0;
+ expected_7d00[idx++] = SL_SOF;
+ expected_7d00[idx++] = 0xb4;
+ expected_7d00[idx++] = 0x00;
+ expected_7d00[idx++] = SL_ESCAPE;
+ expected_7d00[idx++] = 0x5d;
+ memset(&expected_7d00[idx], 0x22, payload_len_7d00);
+ idx += payload_len_7d00;
+ expected_7d00[idx++] = 0xc2;
+ expected_7d00[idx++] = 0xf8;
+ expected_7d00[idx++] = SL_SOF;
+ assert_int_equal(idx, expected_len_7d00);
+
+ idx = 0;
+ expected_7e00[idx++] = SL_SOF;
+ expected_7e00[idx++] = 0xb4;
+ expected_7e00[idx++] = 0x00;
+ expected_7e00[idx++] = SL_ESCAPE;
+ expected_7e00[idx++] = 0x5e;
+ memset(&expected_7e00[idx], 0x22, payload_len_7e00);
+ idx += payload_len_7e00;
+ expected_7e00[idx++] = 0x7c;
+ expected_7e00[idx++] = 0xe9;
+ expected_7e00[idx++] = SL_SOF;
+ assert_int_equal(idx, expected_len_7e00);
+
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA,
+ payload,
+ payload_len_7d00,
+ expected_7d00,
+ expected_len_7d00);
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_CTRL_DATA,
+ payload,
+ payload_len_7e00,
+ expected_7e00,
+ expected_len_7e00);
+
+ free(expected_7d00);
+ free(expected_7e00);
+ free(payload);
+}
+
+// Verifies CRC bytes requiring escaping are encoded correctly.
+static void _test_format_crc_escaping(void** state)
+{
+ (void)state;
+ const uint8_t payload_low[2] = {0x68, 0x00};
+ const uint8_t payload_high[2] = {0x00, 0x01};
+ const uint8_t expected_low[] = {
+ 0x7e, 0x3c, 0x02, 0x00, 0x68, 0x00, 0x7d, 0x5e, 0x7d, 0x5d, 0x7e};
+ const uint8_t expected_high[] = {0x7e, 0x3c, 0x02, 0x00, 0x00, 0x01, 0x90, 0x7d, 0x5d, 0x7e};
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA, payload_low, 2, expected_low, sizeof(expected_low));
+ _assert_format_case_fixture(
+ DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA,
+ payload_high,
+ 2,
+ expected_high,
+ sizeof(expected_high));
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(_test_format_basic_no_escaping),
+ cmocka_unit_test(_test_format_payload_bytes_escaped),
+ cmocka_unit_test(_test_format_zero_length_payload),
+ cmocka_unit_test(_test_format_payload_all_bytes),
+ cmocka_unit_test(_test_format_len_low_escaping),
+ cmocka_unit_test(_test_format_len_high_escaping),
+ cmocka_unit_test(_test_format_crc_escaping),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.