1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 #include "test_cborattr.h"
20 
21 /*
22  * Where we collect cbor data.
23  */
24 static uint8_t test_cbor_buf[1024];
25 static int test_cbor_len;
26 
27 /*
28  * CBOR encoder data structures.
29  */
30 static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int);
31 static CborEncoder test_encoder;
32 static struct cbor_encoder_writer test_writer = {
33     .write = test_cbor_wr
34 };
35 
36 static int
test_cbor_wr(struct cbor_encoder_writer * cew,const char * data,int len)37 test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len)
38 {
39     memcpy(test_cbor_buf + test_cbor_len, data, len);
40     test_cbor_len += len;
41 
42     assert(test_cbor_len < sizeof(test_cbor_buf));
43     return 0;
44 }
45 
46 static void
test_encode_string_array_one(void)47 test_encode_string_array_one(void)
48 {
49     CborEncoder data;
50     CborEncoder array;
51 
52     test_cbor_len = 0;
53     cbor_encoder_init(&test_encoder, &test_writer, 0);
54 
55     cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength);
56 
57     /*
58      * a: ["asdf"]
59      */
60     cbor_encode_text_stringz(&data, "a");
61 
62     cbor_encoder_create_array(&data, &array, CborIndefiniteLength);
63     cbor_encode_text_stringz(&array, "asdf");
64     cbor_encoder_close_container(&data, &array);
65 
66     cbor_encoder_close_container(&test_encoder, &data);
67 }
68 
69 static void
test_encode_string_array_three(void)70 test_encode_string_array_three(void)
71 {
72     CborEncoder data;
73     CborEncoder array;
74 
75     test_cbor_len = 0;
76     cbor_encoder_init(&test_encoder, &test_writer, 0);
77 
78     cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength);
79 
80     /*
81      * a: ["asdf", "k", "blurb"]
82      */
83     cbor_encode_text_stringz(&data, "a");
84 
85     cbor_encoder_create_array(&data, &array, CborIndefiniteLength);
86     cbor_encode_text_stringz(&array, "asdf");
87     cbor_encode_text_stringz(&array, "k");
88     cbor_encode_text_stringz(&array, "blurb");
89     cbor_encoder_close_container(&data, &array);
90 
91     cbor_encoder_close_container(&test_encoder, &data);
92 }
93 
94 /*
95  * string array
96  */
TEST_CASE(test_cborattr_decode_string_array)97 TEST_CASE(test_cborattr_decode_string_array)
98 {
99     int rc;
100     char *str_ptrs[5];
101     char arr_data[256];
102     int arr_cnt = 0;
103     struct cbor_attr_t test_attrs[] = {
104         [0] = {
105             .attribute = "a",
106             .type = CborAttrArrayType,
107             .addr.array.element_type = CborAttrTextStringType,
108             .addr.array.arr.strings.ptrs = str_ptrs,
109             .addr.array.arr.strings.store = arr_data,
110             .addr.array.arr.strings.storelen = sizeof(arr_data),
111             .addr.array.count = &arr_cnt,
112             .addr.array.maxlen = sizeof(arr_data) / sizeof(arr_data[0]),
113             .nodefault = true
114         },
115         [1] = {
116             .attribute = NULL
117         }
118     };
119 
120     test_encode_string_array_one();
121 
122     rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs);
123     TEST_ASSERT(rc == 0);
124     TEST_ASSERT(arr_cnt == 1);
125     TEST_ASSERT(!strcmp(str_ptrs[0], "asdf"));
126 
127     test_encode_string_array_three();
128 
129     rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs);
130     TEST_ASSERT(rc == 0);
131     TEST_ASSERT(arr_cnt == 3);
132     TEST_ASSERT(!strcmp(str_ptrs[0], "asdf"));
133     TEST_ASSERT(!strcmp(str_ptrs[1], "k"));
134     TEST_ASSERT(!strcmp(str_ptrs[2], "blurb"));
135 }
136