Repository: incubator-mynewt-core Updated Branches: refs/heads/develop 68d2e555d -> c61c02610
cborattr; start a test for decoding different data types. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/ae5368a6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ae5368a6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ae5368a6 Branch: refs/heads/develop Commit: ae5368a6da16a7ad3d7b44ba8589097847d7fd2b Parents: 68d2e55 Author: Marko Kiiskila <[email protected]> Authored: Mon Jan 2 13:54:35 2017 -0800 Committer: Marko Kiiskila <[email protected]> Committed: Mon Jan 2 14:18:20 2017 -0800 ---------------------------------------------------------------------- encoding/cborattr/src/cborattr.c | 16 +-- encoding/cborattr/test/src/test_cborattr.c | 1 + encoding/cborattr/test/src/test_cborattr.h | 1 + .../test/src/testcases/cborattr_decode3.c | 123 +++++++++++++++++++ 4 files changed, 134 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ae5368a6/encoding/cborattr/src/cborattr.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c index 4101646..c8ccc9d 100644 --- a/encoding/cborattr/src/cborattr.c +++ b/encoding/cborattr/src/cborattr.c @@ -25,11 +25,12 @@ * one-to-one because of signedness of integers * and therefore we need a function to do this trickery */ static int -valid_attr_type(CborType ct, CborAttrType at) { +valid_attr_type(CborType ct, CborAttrType at) +{ switch (at) { case CborAttrIntegerType: case CborAttrUnsignedIntegerType: - if(ct == CborIntegerType) { + if (ct == CborIntegerType) { return 1; } break; @@ -128,7 +129,8 @@ static int cbor_internal_read_object(CborValue *root_value, const struct cbor_attr_t *attrs, const struct cbor_array_t *parent, - int offset) { + int offset) +{ const struct cbor_attr_t *cursor; char attrbuf[CBOR_ATTR_MAX + 1]; char *lptr; @@ -193,7 +195,7 @@ cbor_internal_read_object(CborValue *root_value, /* at least get the type of the next value so we can match the * attribute name and type for a perfect match */ g_err |= cbor_value_advance(&cur_value); - if(cbor_value_is_valid(&cur_value)) { + if (cbor_value_is_valid(&cur_value)) { type = cbor_value_get_type(&cur_value); } else { g_err |= CborErrorIllegalType; @@ -202,14 +204,14 @@ cbor_internal_read_object(CborValue *root_value, /* find this attribute in our list */ for (cursor = attrs; cursor->attribute != NULL; cursor++) { - if ( valid_attr_type(type, cursor->type) && - (memcmp(cursor->attribute, attrbuf, len) == 0 )) { + if (valid_attr_type(type, cursor->type) && + (memcmp(cursor->attribute, attrbuf, len) == 0)) { break; } } /* we found a match */ - if ( cursor->attribute != NULL ) { + if (cursor->attribute != NULL) { lptr = cbor_target_address(cursor, parent, offset); switch (cursor->type) { case CborAttrNullType: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ae5368a6/encoding/cborattr/test/src/test_cborattr.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/test_cborattr.c b/encoding/cborattr/test/src/test_cborattr.c index 410b87e..fe1f4ec 100644 --- a/encoding/cborattr/test/src/test_cborattr.c +++ b/encoding/cborattr/test/src/test_cborattr.c @@ -25,6 +25,7 @@ TEST_SUITE(test_cborattr_suite) { test_cborattr_decode1(); test_cborattr_decode2(); + test_cborattr_decode3(); } #if MYNEWT_VAL(SELFTEST) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ae5368a6/encoding/cborattr/test/src/test_cborattr.h ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/test_cborattr.h b/encoding/cborattr/test/src/test_cborattr.h index c2c0ba3..34fd76d 100644 --- a/encoding/cborattr/test/src/test_cborattr.h +++ b/encoding/cborattr/test/src/test_cborattr.h @@ -40,6 +40,7 @@ const uint8_t *test_str1(int *len); */ TEST_CASE_DECL(test_cborattr_decode1); TEST_CASE_DECL(test_cborattr_decode2); +TEST_CASE_DECL(test_cborattr_decode3); #ifdef __cplusplus http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ae5368a6/encoding/cborattr/test/src/testcases/cborattr_decode3.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode3.c b/encoding/cborattr/test/src/testcases/cborattr_decode3.c new file mode 100644 index 0000000..3d857e1 --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode3.c @@ -0,0 +1,123 @@ +/* + * 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" + +/* + * Where we collect cbor data. + */ +static uint8_t test_cbor_buf[1024]; +static int test_cbor_len; + +/* + * CBOR encoder data structures. + */ +static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborEncoder test_encoder; +static struct cbor_encoder_writer test_writer = { + .write = test_cbor_wr +}; + +static int +test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +{ + memcpy(test_cbor_buf + test_cbor_len, data, len); + test_cbor_len += len; + + assert(test_cbor_len < sizeof(test_cbor_buf)); + return 0; +} + +static void +test_encode_data(void) +{ + CborEncoder test_data; + uint8_t data[4] = { 0, 1, 2 }; + + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &test_data, CborIndefiniteLength); + /* + * a:22 + */ + cbor_encode_text_stringz(&test_data, "a"); + cbor_encode_uint(&test_data, 22); + + /* + * b:-13 + */ + cbor_encode_text_stringz(&test_data, "b"); + cbor_encode_int(&test_data, -13); + + /* + * c:0x000102 + */ + cbor_encode_text_stringz(&test_data, "c"); + cbor_encode_byte_string(&test_data, data, 3); + cbor_encoder_close_container(&test_encoder, &test_data); + + /* + * XXX add other data types to encode here. + */ + +} + +/* + * Simple decoding. + */ +TEST_CASE(test_cborattr_decode3) +{ + int rc; + uint64_t a_val = 0; + int64_t b_val = 0; + uint8_t c_data[4]; + size_t c_len; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "a", + .type = CborAttrIntegerType, + .addr.uinteger = &a_val, + .nodefault = true + }, + [1] = { + .attribute = "b", + .type = CborAttrIntegerType, + .addr.integer = &b_val, + .nodefault = true + }, + [2] = { + .attribute = "c", + .type = CborAttrByteStringType, + .addr.bytestring.data = c_data, + .addr.bytestring.len = &c_len, + .len = sizeof(c_data) + }, + [3] = { + .attribute = NULL + } + }; + + test_encode_data(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(a_val == 22); + TEST_ASSERT(b_val == -13); + TEST_ASSERT(c_len == 3); + TEST_ASSERT(c_data[0] == 0 && c_data[1] == 1 && c_data[2] == 2); +}
