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 12:40:44 UTC

(mynewt-core) 02/07: encoding/cborattr: Add test for array attribute default count

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 6688941cf7c80842679110c112bae4b3964137f9
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 19:14:59 2026 +0100

    encoding/cborattr: Add test for array attribute default count
    
    Verify that the array count is set to 0 when the array attribute is
    missing or is not an array, and left untouched when nodefault is set.
    
    Assisted-by: Claude:claude-opus-5-5
---
 encoding/cborattr/test/src/test_cborattr.c         |  1 +
 encoding/cborattr/test/src/test_cborattr.h         |  1 +
 .../src/testcases/cborattr_decode_array_default.c  | 90 ++++++++++++++++++++++
 3 files changed, 92 insertions(+)

diff --git a/encoding/cborattr/test/src/test_cborattr.c b/encoding/cborattr/test/src/test_cborattr.c
index f349a6378..03ef2b577 100644
--- a/encoding/cborattr/test/src/test_cborattr.c
+++ b/encoding/cborattr/test/src/test_cborattr.c
@@ -38,6 +38,7 @@ TEST_SUITE(test_cborattr_suite)
     test_cborattr_decode_string_bounds();
     test_cborattr_decode_string_array_bounds();
     test_cborattr_decode_mbuf();
+    test_cborattr_decode_array_default();
 }
 
 #if MYNEWT_VAL(SELFTEST)
diff --git a/encoding/cborattr/test/src/test_cborattr.h b/encoding/cborattr/test/src/test_cborattr.h
index a71b513df..49dc480f6 100644
--- a/encoding/cborattr/test/src/test_cborattr.h
+++ b/encoding/cborattr/test/src/test_cborattr.h
@@ -54,6 +54,7 @@ TEST_CASE_DECL(test_cborattr_decode_nesting);
 TEST_CASE_DECL(test_cborattr_decode_string_bounds);
 TEST_CASE_DECL(test_cborattr_decode_string_array_bounds);
 TEST_CASE_DECL(test_cborattr_decode_mbuf);
+TEST_CASE_DECL(test_cborattr_decode_array_default);
 
 #ifdef __cplusplus
 }
diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_array_default.c b/encoding/cborattr/test/src/testcases/cborattr_decode_array_default.c
new file mode 100644
index 000000000..1d5176aac
--- /dev/null
+++ b/encoding/cborattr/test/src/testcases/cborattr_decode_array_default.c
@@ -0,0 +1,90 @@
+/*
+ * 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 "test_cborattr.h"
+
+static uint8_t test_dflt_buf[32];
+static int test_dflt_len;
+
+static CborError
+test_dflt_wr(void *token, const void *data, size_t len, CborEncoderAppendType append)
+{
+    memcpy(test_dflt_buf + test_dflt_len, data, len);
+    test_dflt_len += len;
+    assert(test_dflt_len < sizeof(test_dflt_buf));
+    return CborNoError;
+}
+
+/* Encodes {key: 1} */
+static void
+test_dflt_encode(const char *key)
+{
+    CborEncoder enc;
+    CborEncoder map;
+
+    test_dflt_len = 0;
+    cbor_encoder_init_writer(&enc, test_dflt_wr, NULL);
+    cbor_encoder_create_map(&enc, &map, CborIndefiniteLength);
+    cbor_encode_text_stringz(&map, key);
+    cbor_encode_int(&map, 1);
+    cbor_encoder_close_container(&enc, &map);
+}
+
+TEST_CASE_SELF(test_cborattr_decode_array_default)
+{
+    char *ptrs[2];
+    char store[16];
+    int cnt;
+    int rc;
+
+    struct cbor_attr_t attrs[] = {
+        {
+         .attribute = "a",
+         .type = CborAttrArrayType,
+         .addr.array.element_type = CborAttrTextStringType,
+         .addr.array.arr.strings.ptrs = ptrs,
+         .addr.array.arr.strings.store = store,
+         .addr.array.arr.strings.storelen = sizeof(store),
+         .addr.array.count = &cnt,
+         .addr.array.maxlen = 2,
+         },
+        { 0 },
+    };
+
+    /* Array attribute missing */
+    test_dflt_encode("b");
+    cnt = 1234;
+    rc = cbor_read_flat_attrs(test_dflt_buf, test_dflt_len, attrs);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(cnt == 0);
+
+    /* Array attribute present, but not an array */
+    test_dflt_encode("a");
+    cnt = 1234;
+    rc = cbor_read_flat_attrs(test_dflt_buf, test_dflt_len, attrs);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(cnt == 0);
+
+    /* No default requested, count left untouched */
+    attrs[0].nodefault = true;
+    test_dflt_encode("b");
+    cnt = 1234;
+    rc = cbor_read_flat_attrs(test_dflt_buf, test_dflt_len, attrs);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(cnt == 1234);
+}