You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by "janc.apache.org via commits" <co...@mynewt.apache.org> on 2026/10/31 22:12:09 UTC
(mynewt-core) 02/02: net/oic: Add test for CoAP transaction handling
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 2b161f5bb6991a62fdefe930d5e22d47b19d0323
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Fri Oct 30 14:52:12 2026 +0100
net/oic: Add test for CoAP transaction handling
Verify that transaction used for response to request is freed only
once when it is followed by malformed message.
Assisted-by: Claude:claude-opus-5-5
---
net/oic/selftest/src/test_coap.c | 95 ++++++++++++++++++++++++++++++
net/oic/selftest/src/test_oic.h | 1 +
net/oic/selftest/src/testcases/oic_tests.c | 1 +
3 files changed, 97 insertions(+)
diff --git a/net/oic/selftest/src/test_coap.c b/net/oic/selftest/src/test_coap.c
new file mode 100644
index 000000000..a96bfe3ef
--- /dev/null
+++ b/net/oic/selftest/src/test_coap.c
@@ -0,0 +1,95 @@
+/*
+ * 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 <oic/oc_api.h>
+#include <oic/port/mynewt/ip.h>
+#include "test_oic.h"
+
+/* Internal API of CoAP engine */
+int coap_receive(struct os_mbuf **mp);
+
+static struct os_mbuf *
+test_coap_mbuf(const uint8_t *data, size_t len)
+{
+ struct os_mbuf *m;
+ int rc;
+
+ m = os_msys_get_pkthdr(len, sizeof(struct oc_endpoint_ip));
+ TEST_ASSERT_FATAL(m != NULL);
+ memset(OC_MBUF_ENDPOINT(m), 0, sizeof(struct oc_endpoint_ip));
+ OC_MBUF_ENDPOINT(m)->ep.oe_type = oc_ip6_transport_id;
+
+ rc = os_mbuf_append(m, data, len);
+ TEST_ASSERT_FATAL(rc == 0);
+
+ return m;
+}
+
+static void
+test_coap_rx(const uint8_t *data, size_t len)
+{
+ struct os_mbuf *m;
+
+ m = test_coap_mbuf(data, len);
+ coap_receive(&m);
+ if (m) {
+ os_mbuf_free_chain(m);
+ }
+}
+
+static int
+test_coap_tran_num_free(void)
+{
+ struct os_mempool_info omi;
+
+ TEST_ASSERT_FATAL(os_mempool_get("coap_tran", &omi) != NULL);
+ TEST_ASSERT(omi.omi_num_free <= omi.omi_num_blocks);
+
+ return omi.omi_num_free;
+}
+
+void
+test_coap_transaction(void)
+{
+ /* NON GET request for unknown resource "x" */
+ static const uint8_t non_get[] = {
+ 0x51, 0x01, 0x12, 0x34, 0xaa, 0xb1, 'x',
+ };
+ /* Message with invalid token length of 9 */
+ static const uint8_t bad_tkl[] = {
+ 0x59, 0x01, 0x12, 0x35, 0x01, 0x02, 0x03,
+ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+ };
+ int num_free;
+ int i;
+
+ num_free = test_coap_tran_num_free();
+
+ /*
+ * Transaction used for response is freed when response is sent and
+ * shall not be freed again by following requests.
+ */
+ for (i = 0; i < 2; i++) {
+ test_coap_rx(non_get, sizeof(non_get));
+ TEST_ASSERT(test_coap_tran_num_free() == num_free);
+
+ test_coap_rx(bad_tkl, sizeof(bad_tkl));
+ TEST_ASSERT(test_coap_tran_num_free() == num_free);
+ }
+}
diff --git a/net/oic/selftest/src/test_oic.h b/net/oic/selftest/src/test_oic.h
index e937a0d7b..26b66306c 100644
--- a/net/oic/selftest/src/test_oic.h
+++ b/net/oic/selftest/src/test_oic.h
@@ -42,6 +42,7 @@ void oic_test_get_endpoint(struct oc_server_handle *);
void test_uuid(void);
void test_parse(void);
+void test_coap_transaction(void);
void test_discovery(void);
void test_getset(void);
void test_observe(void);
diff --git a/net/oic/selftest/src/testcases/oic_tests.c b/net/oic/selftest/src/testcases/oic_tests.c
index 535ecc933..02c1cc2de 100644
--- a/net/oic/selftest/src/testcases/oic_tests.c
+++ b/net/oic/selftest/src/testcases/oic_tests.c
@@ -81,6 +81,7 @@ TEST_CASE_TASK(oic_tests)
oc_main_init(&test_handler);
test_uuid();
test_parse();
+ test_coap_transaction();
test_discovery();
test_getset();
test_observe();