1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include "cbor.h"
#include <inttypes.h>
#include <string.h>
#include "assertions.h"
cbor_item_t *map;
struct cbor_load_result res;
unsigned char empty_map[] = {0xA0};
static void test_empty_map(void **state)
{
map = cbor_load(empty_map, 1, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_size(map) == 0);
assert_true(res.read == 1);
assert_int_equal(cbor_map_allocated(map), 0);
cbor_decref(&map);
assert_null(map);
}
unsigned char simple_map[] = {0xA2, 0x01, 0x02, 0x03, 0x04};
/* {1: 2, 3: 4} */
static void test_simple_map(void **state)
{
map = cbor_load(simple_map, 5, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_definite(map));
assert_true(cbor_map_size(map) == 2);
assert_true(res.read == 5);
struct cbor_pair *handle = cbor_map_handle(map);
assert_uint8(handle[0].key, 1);
assert_uint8(handle[0].value, 2);
assert_uint8(handle[1].key, 3);
assert_uint8(handle[1].value, 4);
cbor_decref(&map);
assert_null(map);
}
unsigned char simple_indef_map[] = {0xBF, 0x01, 0x02, 0x03, 0x04, 0xFF};
/* {_ 1: 2, 3: 4} */
static void test_indef_simple_map(void **state)
{
map = cbor_load(simple_indef_map, 6, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_indefinite(map));
assert_true(cbor_map_size(map) == 2);
assert_true(res.read == 6);
struct cbor_pair *handle = cbor_map_handle(map);
assert_uint8(handle[0].key, 1);
assert_uint8(handle[0].value, 2);
assert_uint8(handle[1].key, 3);
assert_uint8(handle[1].value, 4);
cbor_decref(&map);
assert_null(map);
}
//{
// "glossary": {
// "title": "example glossary"
// }
//}
unsigned char def_nested_map[] = {0xA1, 0x68, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79, 0xA1, 0x65, 0x74, 0x69, 0x74, 0x6C, 0x65, 0x70, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79};
static void test_def_nested_map(void **state)
{
map = cbor_load(def_nested_map, 34, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_definite(map));
assert_true(cbor_map_size(map) == 1);
assert_true(res.read == 34);
struct cbor_pair *handle = cbor_map_handle(map);
assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_MAP);
struct cbor_pair *inner_handle = cbor_map_handle(handle[0].value);
assert_true(cbor_typeof(inner_handle[0].key) == CBOR_TYPE_STRING);
assert_true(cbor_typeof(inner_handle[0].value) == CBOR_TYPE_STRING);
assert_memory_equal(
cbor_string_handle(inner_handle[0].value),
"example glossary",
strlen("example glossary")
);
cbor_decref(&map);
assert_null(map);
}
unsigned char streamed_key_map[] = {0xA1, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF, 0xA0};
/* '{ (_"a" "b"): {}}' */
static void test_streamed_key_map(void **state)
{
map = cbor_load(streamed_key_map, 8, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_definite(map));
assert_true(cbor_map_size(map) == 1);
assert_true(res.read == 8);
struct cbor_pair *handle = cbor_map_handle(map);
assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
assert_true(cbor_string_is_indefinite(handle[0].key));
assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
assert_true(cbor_isa_map(handle[0].value));
assert_int_equal(cbor_map_size(handle[0].value), 0);
cbor_decref(&map);
assert_null(map);
}
unsigned char streamed_kv_map[] = {0xA1, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF, 0x7F, 0x61, 0x63, 0x61, 0x64, 0xFF};
/* '{ (_"a" "b"): (_"c", "d")}' */
static void test_streamed_kv_map(void **state)
{
map = cbor_load(streamed_kv_map, 13, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_definite(map));
assert_int_equal(cbor_map_size(map), 1);
assert_int_equal(res.read, 13);
struct cbor_pair *handle = cbor_map_handle(map);
assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
assert_true(cbor_string_is_indefinite(handle[0].key));
assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
assert_true(cbor_string_is_indefinite(handle[0].value));
assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
assert_memory_equal(
cbor_string_handle(
cbor_string_chunks_handle(handle[0].value)[1]
),
"d",
1
);
cbor_decref(&map);
assert_null(map);
}
unsigned char streamed_streamed_kv_map[] = {0xBF, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF, 0x7F, 0x61, 0x63, 0x61, 0x64, 0xFF, 0xFF};
/* '{_ (_"a" "b"): (_"c", "d")}' */
static void test_streamed_streamed_kv_map(void **state)
{
map = cbor_load(streamed_streamed_kv_map, 14, &res);
assert_non_null(map);
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
assert_true(cbor_isa_map(map));
assert_true(cbor_map_is_indefinite(map));
assert_int_equal(cbor_map_size(map), 1);
assert_int_equal(res.read, 14);
struct cbor_pair *handle = cbor_map_handle(map);
assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
assert_true(cbor_string_is_indefinite(handle[0].key));
assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
assert_true(cbor_string_is_indefinite(handle[0].value));
assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
assert_memory_equal(
cbor_string_handle(
cbor_string_chunks_handle(handle[0].value)[1]
),
"d",
1
);
cbor_decref(&map);
assert_null(map);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_empty_map),
cmocka_unit_test(test_simple_map),
cmocka_unit_test(test_indef_simple_map),
cmocka_unit_test(test_def_nested_map),
cmocka_unit_test(test_streamed_key_map),
cmocka_unit_test(test_streamed_kv_map),
cmocka_unit_test(test_streamed_streamed_kv_map)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|