cborattr; support decoding arrays of elements.
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/c61c0261 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/c61c0261 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/c61c0261 Branch: refs/heads/develop Commit: c61c026102ab8314b991e8ee6ccc1be34723cf98 Parents: ae5368a Author: Marko Kiiskila <[email protected]> Authored: Tue Jan 3 12:17:41 2017 -0800 Committer: Marko Kiiskila <[email protected]> Committed: Tue Jan 3 12:17:41 2017 -0800 ---------------------------------------------------------------------- encoding/cborattr/include/cborattr/cborattr.h | 3 +- encoding/cborattr/src/cborattr.c | 293 ++++++++++++------- encoding/cborattr/test/src/test_cborattr.c | 7 +- encoding/cborattr/test/src/test_cborattr.h | 7 +- .../test/src/testcases/cborattr_decode2.c | 47 --- .../test/src/testcases/cborattr_decode3.c | 123 -------- .../src/testcases/cborattr_decode_bool_array.c | 101 +++++++ .../src/testcases/cborattr_decode_int_array.c | 143 +++++++++ .../src/testcases/cborattr_decode_obj_array.c | 109 +++++++ .../src/testcases/cborattr_decode_partial.c | 47 +++ .../test/src/testcases/cborattr_decode_simple.c | 123 ++++++++ .../testcases/cborattr_decode_string_array.c | 135 +++++++++ 12 files changed, 851 insertions(+), 287 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/include/cborattr/cborattr.h ---------------------------------------------------------------------- diff --git a/encoding/cborattr/include/cborattr/cborattr.h b/encoding/cborattr/include/cborattr/cborattr.h index d7f0e03..ed92d35 100644 --- a/encoding/cborattr/include/cborattr/cborattr.h +++ b/encoding/cborattr/include/cborattr/cborattr.h @@ -50,6 +50,7 @@ typedef enum CborAttrType { CborAttrFloatType, CborAttrDoubleType, CborAttrArrayType, + CborAttrObjectType, CborAttrNullType, } CborAttrType; @@ -119,7 +120,7 @@ struct cbor_attr_t { }; int cbor_read_object(struct CborValue *, const struct cbor_attr_t *); -int cbor_read_array(struct CborParser *, const struct cbor_array_t *); +int cbor_read_array(struct CborValue *, const struct cbor_array_t *); int cbor_read_flat_attrs(const uint8_t *data, int len, const struct cbor_attr_t *attrs); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/src/cborattr.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c index c8ccc9d..19b0584 100644 --- a/encoding/cborattr/src/cborattr.c +++ b/encoding/cborattr/src/cborattr.c @@ -28,50 +28,50 @@ static int valid_attr_type(CborType ct, CborAttrType at) { switch (at) { - case CborAttrIntegerType: - case CborAttrUnsignedIntegerType: - if (ct == CborIntegerType) { - return 1; - } - break; - case CborAttrByteStringType: - if (ct == CborByteStringType) { - return 1; - } - break; - case CborAttrTextStringType: - if (ct == CborTextStringType) { - return 1; - } - break; - case CborAttrBooleanType: - if (ct == CborBooleanType) { - return 1; - } + case CborAttrIntegerType: + case CborAttrUnsignedIntegerType: + if (ct == CborIntegerType) { + return 1; + } + break; + case CborAttrByteStringType: + if (ct == CborByteStringType) { + return 1; + } + break; + case CborAttrTextStringType: + if (ct == CborTextStringType) { + return 1; + } + break; + case CborAttrBooleanType: + if (ct == CborBooleanType) { + return 1; + } #if FLOAT_SUPPORT - case CborAttrFloatType: - if (ct == CborFloatType) { - return 1; - } - break; - case CborAttrDoubleType: - if (ct == CborDoubleType) { - return 1; - } - break; + case CborAttrFloatType: + if (ct == CborFloatType) { + return 1; + } + break; + case CborAttrDoubleType: + if (ct == CborDoubleType) { + return 1; + } + break; #endif - case CborAttrArrayType: - if (ct == CborArrayType) { - return 1; - } - break; - case CborAttrNullType: - if (ct == CborNullType) { - return 1; - } - break; - default: - break; + case CborAttrArrayType: + if (ct == CborArrayType) { + return 1; + } + break; + case CborAttrNullType: + if (ct == CborNullType) { + return 1; + } + break; + default: + break; } return 0; } @@ -80,10 +80,11 @@ valid_attr_type(CborType ct, CborAttrType at) * write or read and attribute from the cbor_attr_r structure */ static char * cbor_target_address(const struct cbor_attr_t *cursor, - const struct cbor_array_t *parent, int offset) + const struct cbor_array_t *parent, int offset) { char *targetaddr = NULL; - if (parent == NULL || parent->element_type != CborAttrArrayType) { + + if (parent == NULL || parent->element_type != CborAttrObjectType) { /* ordinary case - use the address in the cursor structure */ switch (cursor->type) { case CborAttrNullType: @@ -133,9 +134,9 @@ cbor_internal_read_object(CborValue *root_value, { const struct cbor_attr_t *cursor; char attrbuf[CBOR_ATTR_MAX + 1]; - char *lptr; + void *lptr; CborValue cur_value; - CborError g_err = 0; + CborError err = 0; size_t len; CborType type = CborInvalidType; @@ -145,35 +146,36 @@ cbor_internal_read_object(CborValue *root_value, lptr = cbor_target_address(cursor, parent, offset); if (lptr != NULL) { switch (cursor->type) { - case CborAttrIntegerType: - memcpy(lptr, &cursor->dflt.integer, sizeof(long long int)); - break; - case CborAttrUnsignedIntegerType: - memcpy(lptr, &cursor->dflt.integer, sizeof(long long unsigned int)); - break; - case CborAttrBooleanType: - memcpy(lptr, &cursor->dflt.boolean, sizeof(bool)); - break; + case CborAttrIntegerType: + memcpy(lptr, &cursor->dflt.integer, sizeof(long long int)); + break; + case CborAttrUnsignedIntegerType: + memcpy(lptr, &cursor->dflt.integer, + sizeof(long long unsigned int)); + break; + case CborAttrBooleanType: + memcpy(lptr, &cursor->dflt.boolean, sizeof(bool)); + break; #if FLOAT_SUPPORT - case CborAttrFloatType: - memcpy(lptr, &cursor->dflt.fval, sizeof(float)); - break; - case CborAttrDoubleType: - memcpy(lptr, &cursor->dflt.real, sizeof(double)); - break; + case CborAttrFloatType: + memcpy(lptr, &cursor->dflt.fval, sizeof(float)); + break; + case CborAttrDoubleType: + memcpy(lptr, &cursor->dflt.real, sizeof(double)); + break; #endif - default: - break; + default: + break; } } } } if (cbor_value_is_map(root_value)) { - g_err |= cbor_value_enter_container(root_value, &cur_value); + err |= cbor_value_enter_container(root_value, &cur_value); } else { - g_err |= CborErrorIllegalType; - return g_err; + err |= CborErrorIllegalType; + return err; } /* contains key value pairs */ @@ -182,24 +184,25 @@ cbor_internal_read_object(CborValue *root_value, if (cbor_value_is_text_string(&cur_value)) { if (cbor_value_calculate_string_length(&cur_value, &len) == 0) { if (len > CBOR_ATTR_MAX) { - g_err |= CborErrorDataTooLarge; - goto g_err_return; + err |= CborErrorDataTooLarge; + goto err_return; } - g_err |= cbor_value_copy_text_string(&cur_value, attrbuf, &len, NULL); + err |= cbor_value_copy_text_string(&cur_value, attrbuf, &len, + NULL); } } else { - g_err |= CborErrorIllegalType; - goto g_err_return; + err |= CborErrorIllegalType; + goto err_return; } /* 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); + err |= cbor_value_advance(&cur_value); if (cbor_value_is_valid(&cur_value)) { type = cbor_value_get_type(&cur_value); } else { - g_err |= CborErrorIllegalType; - goto g_err_return; + err |= CborErrorIllegalType; + goto err_return; } /* find this attribute in our list */ @@ -212,52 +215,118 @@ cbor_internal_read_object(CborValue *root_value, /* we found a match */ if (cursor->attribute != NULL) { - lptr = cbor_target_address(cursor, parent, offset); + lptr = cbor_target_address(cursor, parent, offset); switch (cursor->type) { - case CborAttrNullType: - /* nothing to do */ - break; - case CborAttrBooleanType: - g_err |= cbor_value_get_boolean(&cur_value, (bool *) lptr); - break; - case CborAttrIntegerType: - g_err |= cbor_value_get_int64(&cur_value, (long long int *) lptr); - break; - case CborAttrUnsignedIntegerType: - g_err |= cbor_value_get_uint64(&cur_value, (long long unsigned int *) lptr); - break; + case CborAttrNullType: + /* nothing to do */ + break; + case CborAttrBooleanType: + err |= cbor_value_get_boolean(&cur_value, lptr); + break; + case CborAttrIntegerType: + err |= cbor_value_get_int64(&cur_value, lptr); + break; + case CborAttrUnsignedIntegerType: + err |= cbor_value_get_uint64(&cur_value, lptr); + break; #if FLOAT_SUPPORT - case CborAttrFloatType: - g_err |= cbor_value_get_float(&cur_value, (float *) lptr); - break; - case CborAttrDoubleType: - g_err |= cbor_value_get_double(&cur_value, (double *) lptr); - break; + case CborAttrFloatType: + err |= cbor_value_get_float(&cur_value, lptr); + break; + case CborAttrDoubleType: + err |= cbor_value_get_double(&cur_value, lptr); + break; #endif - case CborAttrByteStringType: - { - size_t len = cursor->len; - g_err |= cbor_value_copy_byte_string(&cur_value, (unsigned char *) lptr, &len, NULL); - *cursor->addr.bytestring.len = len; - break; - } - case CborAttrTextStringType: - { - size_t len = cursor->len; - g_err |= cbor_value_copy_text_string(&cur_value, (char *) lptr, &len, NULL); - break; - } - /* TODO array */ - default: - g_err |= CborErrorIllegalType; + case CborAttrByteStringType: { + size_t len = cursor->len; + err |= cbor_value_copy_byte_string(&cur_value, lptr, + &len, NULL); + *cursor->addr.bytestring.len = len; + break; + } + case CborAttrTextStringType: { + size_t len = cursor->len; + err |= cbor_value_copy_text_string(&cur_value, lptr, + &len, NULL); + break; + } + case CborAttrArrayType: + err |= cbor_read_array(&cur_value, &cursor->addr.array); + continue; + default: + err |= CborErrorIllegalType; } } cbor_value_advance(&cur_value); } -g_err_return: +err_return: /* that should be it for this container */ - g_err |= cbor_value_leave_container(root_value, &cur_value); - return g_err; + err |= cbor_value_leave_container(root_value, &cur_value); + return err; +} + +int +cbor_read_array(struct CborValue *value, const struct cbor_array_t *arr) +{ + CborError err = 0; + struct CborValue elem; + int off, arrcount; + size_t len; + void *lptr; + char *tp; + + err = cbor_value_enter_container(value, &elem); + if (err) { + return err; + } + arrcount = 0; + tp = arr->arr.strings.store; + for (off = 0; off < arr->maxlen; off++) { + switch (arr->element_type) { + case CborAttrBooleanType: + lptr = &arr->arr.booleans.store[off]; + err |= cbor_value_get_boolean(&elem, lptr); + break; + case CborAttrIntegerType: + lptr = &arr->arr.integers.store[off]; + err |= cbor_value_get_int64(&elem, lptr); + break; + case CborAttrUnsignedIntegerType: + lptr = &arr->arr.uintegers.store[off]; + err |= cbor_value_get_uint64(&elem, lptr); + break; +#if FLOAT_SUPPORT + case CborAttrFloatType: + case CborAttrDoubleType: + lptr = &arr->arr.reals.store[off]; + err |= cbor_value_get_double(&elem, lptr); + break; +#endif + case CborAttrTextStringType: + len = arr->arr.strings.storelen - (tp - arr->arr.strings.store); + err |= cbor_value_copy_text_string(&elem, tp, &len, NULL); + arr->arr.strings.ptrs[off] = tp; + tp += len + 1; + break; + default: + err |= CborErrorIllegalType; + break; + } + arrcount++; + err |= cbor_value_advance(&elem); + if (!cbor_value_is_valid(&elem)) { + break; + } + } + if (arr->count) { + *arr->count = arrcount; + } + while (!cbor_value_at_end(&elem)) { + err |= CborErrorDataTooLarge; + cbor_value_advance(&elem); + } + err |= cbor_value_leave_container(value, &elem); + return err; } int http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/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 fe1f4ec..bdcce20 100644 --- a/encoding/cborattr/test/src/test_cborattr.c +++ b/encoding/cborattr/test/src/test_cborattr.c @@ -24,8 +24,11 @@ TEST_SUITE(test_cborattr_suite) { test_cborattr_decode1(); - test_cborattr_decode2(); - test_cborattr_decode3(); + test_cborattr_decode_partial(); + test_cborattr_decode_simple(); + test_cborattr_decode_int_array(); + test_cborattr_decode_bool_array(); + test_cborattr_decode_string_array(); } #if MYNEWT_VAL(SELFTEST) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/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 34fd76d..68823b4 100644 --- a/encoding/cborattr/test/src/test_cborattr.h +++ b/encoding/cborattr/test/src/test_cborattr.h @@ -39,8 +39,11 @@ const uint8_t *test_str1(int *len); * Testcases */ TEST_CASE_DECL(test_cborattr_decode1); -TEST_CASE_DECL(test_cborattr_decode2); -TEST_CASE_DECL(test_cborattr_decode3); +TEST_CASE_DECL(test_cborattr_decode_partial); +TEST_CASE_DECL(test_cborattr_decode_simple); +TEST_CASE_DECL(test_cborattr_decode_int_array); +TEST_CASE_DECL(test_cborattr_decode_bool_array); +TEST_CASE_DECL(test_cborattr_decode_string_array); #ifdef __cplusplus http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode2.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode2.c b/encoding/cborattr/test/src/testcases/cborattr_decode2.c deleted file mode 100644 index 5504158..0000000 --- a/encoding/cborattr/test/src/testcases/cborattr_decode2.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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" - -/* - * Simple decoding. Only have key for one of the key/value pairs. - */ -TEST_CASE(test_cborattr_decode2) -{ - const uint8_t *data; - int len; - int rc; - char test_str_b[4] = { '\0' }; - struct cbor_attr_t test_attrs[] = { - [0] = { - .attribute = "b", - .type = CborAttrTextStringType, - .addr.string = test_str_b, - .len = sizeof(test_str_b), - .nodefault = true - }, - [1] = { - .attribute = NULL - } - }; - - data = test_str1(&len); - rc = cbor_read_flat_attrs(data, len, test_attrs); - TEST_ASSERT(rc == 0); - TEST_ASSERT(!strcmp(test_str_b, "B")); -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/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 deleted file mode 100644 index 3d857e1..0000000 --- a/encoding/cborattr/test/src/testcases/cborattr_decode3.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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); -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c new file mode 100644 index 0000000..71520c4 --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c @@ -0,0 +1,101 @@ +/* + * 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_bool_array(void) +{ + CborEncoder data; + CborEncoder array; + + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); + + /* + * a: [true,true,false] + */ + cbor_encode_text_stringz(&data, "a"); + + cbor_encoder_create_array(&data, &array, CborIndefiniteLength); + cbor_encode_boolean(&array, true); + cbor_encode_boolean(&array, true); + cbor_encode_boolean(&array, false); + cbor_encoder_close_container(&data, &array); + + cbor_encoder_close_container(&test_encoder, &data); +} + +/* + * array of booleans + */ +TEST_CASE(test_cborattr_decode_bool_array) +{ + int rc; + bool arr_data[5]; + int arr_cnt = 0; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "a", + .type = CborAttrArrayType, + .addr.array.element_type = CborAttrBooleanType, + .addr.array.arr.booleans.store = arr_data, + .addr.array.count = &arr_cnt, + .addr.array.maxlen = sizeof(arr_data) / sizeof(arr_data[0]), + .nodefault = true + }, + [1] = { + .attribute = NULL + } + }; + + test_encode_bool_array(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(arr_cnt == 3); + TEST_ASSERT(arr_data[0] == true); + TEST_ASSERT(arr_data[1] == true); + TEST_ASSERT(arr_data[2] == false); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c new file mode 100644 index 0000000..96bc8cc --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c @@ -0,0 +1,143 @@ +/* + * 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_int_array(void) +{ + CborEncoder data; + CborEncoder array; + + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); + + /* + * a: [1,2,33,15,-4] + */ + cbor_encode_text_stringz(&data, "a"); + + cbor_encoder_create_array(&data, &array, CborIndefiniteLength); + cbor_encode_int(&array, 1); + cbor_encode_int(&array, 2); + cbor_encode_int(&array, 33); + cbor_encode_int(&array, 15); + cbor_encode_int(&array, -4); + cbor_encoder_close_container(&data, &array); + + cbor_encoder_close_container(&test_encoder, &data); +} + +/* + * integer array + */ +TEST_CASE(test_cborattr_decode_int_array) +{ + int rc; + int64_t arr_data[5]; + int64_t b_int; + int arr_cnt = 0; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "a", + .type = CborAttrArrayType, + .addr.array.element_type = CborAttrIntegerType, + .addr.array.arr.integers.store = arr_data, + .addr.array.count = &arr_cnt, + .addr.array.maxlen = sizeof(arr_data) / sizeof(arr_data[0]), + .nodefault = true + }, + [1] = { + .attribute = "b", + .type = CborAttrIntegerType, + .addr.integer = &b_int, + .dflt.integer = 1 + }, + [2] = { + .attribute = NULL + } + }; + struct cbor_attr_t test_attrs_small[] = { + [0] = { + .attribute = "a", + .type = CborAttrArrayType, + .addr.array.element_type = CborAttrIntegerType, + .addr.array.arr.integers.store = arr_data, + .addr.array.count = &arr_cnt, + .addr.array.maxlen = 1, + .nodefault = true + }, + [1] = { + .attribute = "b", + .type = CborAttrIntegerType, + .addr.integer = &b_int, + .dflt.integer = 1 + }, + [2] = { + .attribute = NULL + } + }; + + test_encode_int_array(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(arr_cnt == 5); + TEST_ASSERT(arr_data[0] == 1); + TEST_ASSERT(arr_data[1] == 2); + TEST_ASSERT(arr_data[2] == 33); + TEST_ASSERT(arr_data[3] == 15); + TEST_ASSERT(arr_data[4] == -4); + TEST_ASSERT(b_int == 1); + + memset(arr_data, 0, sizeof(arr_data)); + b_int = 0; + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs_small); + TEST_ASSERT(rc == CborErrorDataTooLarge); + TEST_ASSERT(arr_cnt == 1); + TEST_ASSERT(arr_data[0] == 1); + TEST_ASSERT(arr_data[1] == 0); + TEST_ASSERT(b_int == 1); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c new file mode 100644 index 0000000..57c46cd --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c @@ -0,0 +1,109 @@ +/* + * 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_obj_array(void) +{ + CborEncoder data; + CborEncoder array; + CborEncoder obj; + + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); + + /* + * a: [{ n:"a", v:1}, {n:"b", v:2} ] + */ + cbor_encode_text_stringz(&data, "a"); + cbor_encoder_create_array(&data, &array, CborIndefiniteLength); + + cbor_encoder_create_map(&array, &obj, CborIndefiniteLength); + cbor_encode_text_stringz(&obj, "n"); + cbor_encode_text_stringz(&obj, "a"); + cbor_encode_text_stringz(&obj, "v"); + cbor_encode_int(&obj, 1); + cbor_encoder_close_container(&array, &obj); + + cbor_encoder_create_map(&array, &obj, CborIndefiniteLength); + cbor_encode_text_stringz(&obj, "n"); + cbor_encode_text_stringz(&obj, "b"); + cbor_encode_text_stringz(&obj, "v"); + cbor_encode_int(&obj, 2); + cbor_encoder_close_container(&array, &obj); + + cbor_encoder_close_container(&data, &array); + cbor_encoder_close_container(&test_encoder, &data); +} + +/* + * object array + */ +TEST_CASE(test_cborattr_decode_obj_array) +{ + int rc; + char arr_data[4]; + int arr_cnt; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "a", + .type = CborAttrArrayType, + .addr.array.element_type = CborAttrNullType, + .addr.array.arr.objects.base = arr_data, + .addr.array.count = &arr_cnt, + .addr.array.maxlen = 4, + .nodefault = true + }, + [1] = { + .attribute = NULL + } + }; + + test_encode_obj_array(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(arr_cnt == 2); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_partial.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_partial.c b/encoding/cborattr/test/src/testcases/cborattr_decode_partial.c new file mode 100644 index 0000000..df0b8d4 --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_partial.c @@ -0,0 +1,47 @@ +/* + * 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" + +/* + * Simple decoding. Only have key for one of the key/value pairs. + */ +TEST_CASE(test_cborattr_decode_partial) +{ + const uint8_t *data; + int len; + int rc; + char test_str_b[4] = { '\0' }; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "b", + .type = CborAttrTextStringType, + .addr.string = test_str_b, + .len = sizeof(test_str_b), + .nodefault = true + }, + [1] = { + .attribute = NULL + } + }; + + data = test_str1(&len); + rc = cbor_read_flat_attrs(data, len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(!strcmp(test_str_b, "B")); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c b/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c new file mode 100644 index 0000000..9e206e1 --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_simple.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_decode_simple) +{ + 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); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c61c0261/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c new file mode 100644 index 0000000..d68e8df --- /dev/null +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c @@ -0,0 +1,135 @@ +/* + * 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_string_array_one(void) +{ + CborEncoder data; + CborEncoder array; + + test_cbor_len = 0; + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); + + /* + * a: ["asdf"] + */ + cbor_encode_text_stringz(&data, "a"); + + cbor_encoder_create_array(&data, &array, CborIndefiniteLength); + cbor_encode_text_stringz(&array, "asdf"); + cbor_encoder_close_container(&data, &array); + + cbor_encoder_close_container(&test_encoder, &data); +} + +static void +test_encode_string_array_three(void) +{ + CborEncoder data; + CborEncoder array; + + test_cbor_len = 0; + cbor_encoder_init(&test_encoder, &test_writer, 0); + + cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); + + /* + * a: ["asdf", "k", "blurb"] + */ + cbor_encode_text_stringz(&data, "a"); + + cbor_encoder_create_array(&data, &array, CborIndefiniteLength); + cbor_encode_text_stringz(&array, "asdf"); + cbor_encode_text_stringz(&array, "k"); + cbor_encode_text_stringz(&array, "blurb"); + cbor_encoder_close_container(&data, &array); + + cbor_encoder_close_container(&test_encoder, &data); +} + +/* + * string array + */ +TEST_CASE(test_cborattr_decode_string_array) +{ + int rc; + char *str_ptrs[5]; + char arr_data[256]; + int arr_cnt = 0; + struct cbor_attr_t test_attrs[] = { + [0] = { + .attribute = "a", + .type = CborAttrArrayType, + .addr.array.element_type = CborAttrTextStringType, + .addr.array.arr.strings.ptrs = str_ptrs, + .addr.array.arr.strings.store = arr_data, + .addr.array.arr.strings.storelen = sizeof(arr_data), + .addr.array.count = &arr_cnt, + .addr.array.maxlen = sizeof(arr_data) / sizeof(arr_data[0]), + .nodefault = true + }, + [1] = { + .attribute = NULL + } + }; + + test_encode_string_array_one(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(arr_cnt == 1); + TEST_ASSERT(!strcmp(str_ptrs[0], "asdf")); + + test_encode_string_array_three(); + + rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); + TEST_ASSERT(rc == 0); + TEST_ASSERT(arr_cnt == 3); + TEST_ASSERT(!strcmp(str_ptrs[0], "asdf")); + TEST_ASSERT(!strcmp(str_ptrs[1], "k")); + TEST_ASSERT(!strcmp(str_ptrs[2], "blurb")); +}
