You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2026/10/26 14:55:49 UTC

(mynewt-core) 05/08: kernel/os: Add test for os_mbuf_copydata and os_mbuf_off

This is an automated email from the ASF dual-hosted git repository.

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 27d42fe2c5ea0554e79825dff939c4d5c4515f93
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 23:41:02 2026 +0100

    kernel/os: Add test for os_mbuf_copydata and os_mbuf_off
    
    Verify that os_mbuf_copydata() rejects negative offset and length
    without copying any data and that os_mbuf_off() returns NULL for a
    negative offset, while valid calls keep working. This covers the
    checks added in the previous commit.
    
    Assisted-by: Claude:claude-opus-5-5
---
 kernel/os/selftest/src/mbuf_test.c                 |  2 +
 .../selftest/src/testcases/os_mbuf_test_copydata.c | 53 ++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/kernel/os/selftest/src/mbuf_test.c b/kernel/os/selftest/src/mbuf_test.c
index 89cc3522f..5e4b2b798 100644
--- a/kernel/os/selftest/src/mbuf_test.c
+++ b/kernel/os/selftest/src/mbuf_test.c
@@ -94,6 +94,7 @@ TEST_CASE_DECL(os_mbuf_test_alloc)
 TEST_CASE_DECL(os_mbuf_test_dup)
 TEST_CASE_DECL(os_mbuf_test_dup_pool)
 TEST_CASE_DECL(os_mbuf_test_append)
+TEST_CASE_DECL(os_mbuf_test_copydata)
 TEST_CASE_DECL(os_mbuf_test_pullup)
 TEST_CASE_DECL(os_mbuf_test_extend)
 TEST_CASE_DECL(os_mbuf_test_adj)
@@ -107,6 +108,7 @@ TEST_SUITE(os_mbuf_test_suite)
     os_mbuf_test_dup();
     os_mbuf_test_dup_pool();
     os_mbuf_test_append();
+    os_mbuf_test_copydata();
     os_mbuf_test_pullup();
     os_mbuf_test_extend();
     os_mbuf_test_adj();
diff --git a/kernel/os/selftest/src/testcases/os_mbuf_test_copydata.c b/kernel/os/selftest/src/testcases/os_mbuf_test_copydata.c
new file mode 100644
index 000000000..ee8c874f3
--- /dev/null
+++ b/kernel/os/selftest/src/testcases/os_mbuf_test_copydata.c
@@ -0,0 +1,53 @@
+/*
+ * 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_test_priv.h"
+
+TEST_CASE_SELF(os_mbuf_test_copydata)
+{
+    struct os_mbuf *om;
+    uint16_t off;
+    int rc;
+    uint8_t databuf[] = { 0xa, 0xb, 0xc, 0xd };
+    uint8_t cmpbuf[] = { 0xff, 0xff, 0xff, 0xff };
+
+    os_mbuf_test_setup();
+
+    om = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
+
+    rc = os_mbuf_append(om, databuf, sizeof(databuf));
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = os_mbuf_copydata(om, 1, 2, cmpbuf);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(cmpbuf[0] == 0xb && cmpbuf[1] == 0xc && cmpbuf[2] == 0xff);
+
+    /* Negative offset or length are rejected */
+    memset(cmpbuf, 0xff, sizeof(cmpbuf));
+    rc = os_mbuf_copydata(om, -1, 2, cmpbuf);
+    TEST_ASSERT(rc != 0);
+    TEST_ASSERT(cmpbuf[0] == 0xff);
+    rc = os_mbuf_copydata(om, 0, -1, cmpbuf);
+    TEST_ASSERT(rc != 0);
+
+    TEST_ASSERT(os_mbuf_off(om, -1, &off) == NULL);
+    TEST_ASSERT(os_mbuf_off(om, 2, &off) == om && off == 2);
+
+    os_mbuf_free_chain(om);
+}