| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/common/byte_buf.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
|
| 11 |
#include <aws/common/json.h> |
| 12 |
|
| 13 |
static char *s_test_json = "{\"array\":[1,2,3],\"boolean\":true,\"color\":\"gold\",\"null\":null,\"number\":123," |
| 14 |
"\"object\":{\"a\":\"b\",\"c\":\"d\"}}"; |
| 15 |
|
| 16 |
struct json_parse_test_data { |
| 17 |
int elements_encountered; |
| 18 |
bool all_elements_are_strings; |
| 19 |
bool all_elements_are_numbers; |
| 20 |
}; |
| 21 |
|
| 22 |
static int s_on_obj_member( |
| 23 |
const struct aws_byte_cursor *key, |
| 24 |
const struct aws_json_value *value, |
| 25 |
bool *out_should_continue, |
| 26 |
void *user_data) { |
| 27 |
(void)key; |
| 28 |
(void)out_should_continue; |
| 29 |
struct json_parse_test_data *data = user_data; |
| 30 |
++(data->elements_encountered); |
| 31 |
data->all_elements_are_strings &= aws_json_value_is_string(value); |
| 32 |
data->all_elements_are_numbers &= aws_json_value_is_number(value); |
| 33 |
return AWS_OP_SUCCESS; |
| 34 |
} |
| 35 |
|
| 36 |
static int s_on_array_value( |
| 37 |
size_t index, |
| 38 |
const struct aws_json_value *value, |
| 39 |
bool *out_should_continue, |
| 40 |
void *user_data) { |
| 41 |
(void)index; |
| 42 |
(void)out_should_continue; |
| 43 |
struct json_parse_test_data *data = user_data; |
| 44 |
++(data->elements_encountered); |
| 45 |
data->all_elements_are_strings &= aws_json_value_is_string(value); |
| 46 |
data->all_elements_are_numbers &= aws_json_value_is_number(value); |
| 47 |
return AWS_OP_SUCCESS; |
| 48 |
} |
| 49 |
|
| 50 |
static int s_on_obj_member_error( |
| 51 |
const struct aws_byte_cursor *key, |
| 52 |
const struct aws_json_value *value, |
| 53 |
bool *out_should_continue, |
| 54 |
void *user_data) { |
| 55 |
(void)key; |
| 56 |
(void)value; |
| 57 |
(void)out_should_continue; |
| 58 |
(void)user_data; |
| 59 |
return AWS_OP_ERR; |
| 60 |
} |
| 61 |
|
| 62 |
static int s_on_array_value_error( |
| 63 |
size_t index, |
| 64 |
const struct aws_json_value *value, |
| 65 |
bool *out_should_continue, |
| 66 |
void *user_data) { |
| 67 |
(void)index; |
| 68 |
(void)value; |
| 69 |
(void)out_should_continue; |
| 70 |
(void)user_data; |
| 71 |
return AWS_OP_ERR; |
| 72 |
} |
| 73 |
|
| 74 |
static int s_on_obj_member_early( |
| 75 |
const struct aws_byte_cursor *key, |
| 76 |
const struct aws_json_value *value, |
| 77 |
bool *out_should_continue, |
| 78 |
void *user_data) { |
| 79 |
(void)key; |
| 80 |
(void)value; |
| 81 |
(void)user_data; |
| 82 |
*out_should_continue = false; |
| 83 |
return AWS_OP_SUCCESS; |
| 84 |
} |
| 85 |
|
| 86 |
static int s_on_array_value_early( |
| 87 |
size_t index, |
| 88 |
const struct aws_json_value *value, |
| 89 |
bool *out_should_continue, |
| 90 |
void *user_data) { |
| 91 |
(void)index; |
| 92 |
(void)value; |
| 93 |
(void)user_data; |
| 94 |
*out_should_continue = false; |
| 95 |
return AWS_OP_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
AWS_TEST_CASE(test_json_parse_from_string, s_test_json_parse_from_string) |
| 99 |
static int s_test_json_parse_from_string(struct aws_allocator *allocator, void *ctx) { |
| 100 |
(void)ctx; |
| 101 |
|
| 102 |
aws_common_library_init(allocator); |
| 103 |
struct aws_json_value *root = aws_json_value_new_from_string(allocator, aws_byte_cursor_from_c_str(s_test_json)); |
| 104 |
|
| 105 |
ASSERT_NOT_NULL(root); |
| 106 |
ASSERT_TRUE(aws_json_value_is_object(root)); |
| 107 |
|
| 108 |
ASSERT_TRUE(aws_json_value_compare(root, root, true)); |
| 109 |
ASSERT_TRUE(aws_json_value_compare(root, root, false)); |
| 110 |
|
| 111 |
struct aws_json_value *temp = aws_json_value_new_null(allocator); |
| 112 |
ASSERT_FALSE(aws_json_value_compare(root, temp, true)); |
| 113 |
aws_json_value_destroy(temp); |
| 114 |
|
| 115 |
|
| 116 |
struct aws_json_value *array_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("array")); |
| 117 |
ASSERT_PTR_EQUALS(array_node, aws_json_value_get_from_object_c_str(root, "array")); |
| 118 |
ASSERT_NOT_NULL(array_node); |
| 119 |
ASSERT_TRUE(aws_json_value_is_array(array_node)); |
| 120 |
ASSERT_TRUE(aws_json_get_array_size(array_node) == 3); |
| 121 |
struct aws_json_value *array_node_one = aws_json_get_array_element(array_node, 0); |
| 122 |
ASSERT_NOT_NULL(array_node_one); |
| 123 |
ASSERT_TRUE(aws_json_value_is_number(array_node_one)); |
| 124 |
double double_check_value = 0; |
| 125 |
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_json_value_get_number(array_node_one, &double_check_value)); |
| 126 |
ASSERT_NOT_NULL(double_check_value); |
| 127 |
ASSERT_TRUE(double_check_value == (double)1); |
| 128 |
|
| 129 |
struct json_parse_test_data array_test_data; |
| 130 |
array_test_data.elements_encountered = 0; |
| 131 |
array_test_data.all_elements_are_strings = true; |
| 132 |
array_test_data.all_elements_are_numbers = true; |
| 133 |
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_json_const_iterate_array(array_node, s_on_array_value, &array_test_data)); |
| 134 |
ASSERT_INT_EQUALS(array_test_data.elements_encountered, 3); |
| 135 |
ASSERT_FALSE(array_test_data.all_elements_are_strings); |
| 136 |
ASSERT_TRUE(array_test_data.all_elements_are_numbers); |
| 137 |
|
| 138 |
ASSERT_INT_EQUALS(AWS_OP_ERR, aws_json_const_iterate_array(array_node, s_on_array_value_error, &array_test_data)); |
| 139 |
|
| 140 |
array_test_data.elements_encountered = 0; |
| 141 |
ASSERT_INT_EQUALS( |
| 142 |
AWS_OP_SUCCESS, aws_json_const_iterate_array(array_node, s_on_array_value_early, &array_test_data)); |
| 143 |
ASSERT_INT_EQUALS(array_test_data.elements_encountered, 0); |
| 144 |
|
| 145 |
|
| 146 |
struct aws_json_value *boolean_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("boolean")); |
| 147 |
ASSERT_NOT_NULL(boolean_node); |
| 148 |
ASSERT_TRUE(aws_json_value_is_boolean(boolean_node)); |
| 149 |
bool bool_check_value = false; |
| 150 |
aws_json_value_get_boolean(boolean_node, &bool_check_value); |
| 151 |
ASSERT_TRUE(bool_check_value); |
| 152 |
|
| 153 |
ASSERT_INT_EQUALS(AWS_OP_ERR, aws_json_const_iterate_object(boolean_node, s_on_obj_member, NULL)); |
| 154 |
ASSERT_INT_EQUALS(AWS_OP_ERR, aws_json_const_iterate_array(boolean_node, s_on_array_value, NULL)); |
| 155 |
|
| 156 |
|
| 157 |
struct aws_json_value *string_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("color")); |
| 158 |
ASSERT_NOT_NULL(string_node); |
| 159 |
ASSERT_TRUE(aws_json_value_is_string(string_node)); |
| 160 |
struct aws_byte_cursor str_string_check_value; |
| 161 |
aws_json_value_get_string(string_node, &str_string_check_value); |
| 162 |
struct aws_string *tmp_str = aws_string_new_from_cursor(allocator, &str_string_check_value); |
| 163 |
ASSERT_TRUE(strcmp(aws_string_c_str(tmp_str), "gold") == 0); |
| 164 |
aws_string_destroy_secure(tmp_str); |
| 165 |
|
| 166 |
|
| 167 |
struct aws_json_value *number_node = aws_json_value_get_from_object_c_str(root, "number"); |
| 168 |
ASSERT_NOT_NULL(number_node); |
| 169 |
ASSERT_TRUE(aws_json_value_is_number(number_node)); |
| 170 |
double double_test_two = 0; |
| 171 |
aws_json_value_get_number(number_node, &double_test_two); |
| 172 |
ASSERT_TRUE(double_test_two == (double)123); |
| 173 |
ASSERT_TRUE(aws_json_value_has_key_c_str(root, "number")); |
| 174 |
ASSERT_TRUE(aws_json_value_has_key(root, aws_byte_cursor_from_c_str("number"))); |
| 175 |
|
| 176 |
|
| 177 |
struct aws_json_value *object_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("object")); |
| 178 |
ASSERT_NOT_NULL(object_node); |
| 179 |
ASSERT_TRUE(aws_json_value_is_object(object_node)); |
| 180 |
struct aws_json_value *sub_object_node = |
| 181 |
aws_json_value_get_from_object(object_node, aws_byte_cursor_from_c_str("a")); |
| 182 |
ASSERT_NOT_NULL(sub_object_node); |
| 183 |
ASSERT_TRUE(aws_json_value_is_string(sub_object_node)); |
| 184 |
struct aws_byte_cursor str_a_value_cursor; |
| 185 |
aws_json_value_get_string(sub_object_node, &str_a_value_cursor); |
| 186 |
struct aws_string *sub_a_string = aws_string_new_from_cursor(allocator, &str_a_value_cursor); |
| 187 |
ASSERT_TRUE(strcmp(aws_string_c_str(sub_a_string), "b") == 0); |
| 188 |
aws_string_destroy_secure(sub_a_string); |
| 189 |
|
| 190 |
struct aws_json_value *duplicate = aws_json_value_duplicate(object_node); |
| 191 |
ASSERT_TRUE(aws_json_value_compare(object_node, duplicate, true)); |
| 192 |
aws_json_value_destroy(duplicate); |
| 193 |
|
| 194 |
struct json_parse_test_data test_data; |
| 195 |
test_data.elements_encountered = 0; |
| 196 |
test_data.all_elements_are_strings = true; |
| 197 |
test_data.all_elements_are_numbers = true; |
| 198 |
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_json_const_iterate_object(object_node, s_on_obj_member, &test_data)); |
| 199 |
ASSERT_INT_EQUALS(test_data.elements_encountered, 2); |
| 200 |
ASSERT_TRUE(test_data.all_elements_are_strings); |
| 201 |
ASSERT_FALSE(test_data.all_elements_are_numbers); |
| 202 |
|
| 203 |
ASSERT_INT_EQUALS(AWS_OP_ERR, aws_json_const_iterate_object(object_node, s_on_obj_member_error, &test_data)); |
| 204 |
|
| 205 |
test_data.elements_encountered = 0; |
| 206 |
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_json_const_iterate_object(object_node, s_on_obj_member_early, &test_data)); |
| 207 |
ASSERT_INT_EQUALS(test_data.elements_encountered, 0); |
| 208 |
|
| 209 |
|
| 210 |
struct aws_json_value *invalid_object = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("invalid")); |
| 211 |
ASSERT_NULL(invalid_object); |
| 212 |
ASSERT_INT_EQUALS(aws_json_value_get_number(invalid_object, NULL), AWS_OP_ERR); |
| 213 |
|
| 214 |
ASSERT_INT_EQUALS(aws_json_value_get_number(string_node, NULL), AWS_OP_ERR); |
| 215 |
|
| 216 |
ASSERT_SUCCESS(aws_json_value_remove_from_object(root, aws_byte_cursor_from_c_str("number"))); |
| 217 |
ASSERT_FALSE(aws_json_value_has_key_c_str(root, "number")); |
| 218 |
|
| 219 |
ASSERT_SUCCESS(aws_json_value_remove_from_object_c_str(root, "object")); |
| 220 |
ASSERT_FALSE(aws_json_value_has_key_c_str(root, "object")); |
| 221 |
|
| 222 |
aws_json_value_destroy(root); |
| 223 |
|
| 224 |
|
| 225 |
aws_json_value_destroy(NULL); |
| 226 |
|
| 227 |
aws_common_library_clean_up(); |
| 228 |
|
| 229 |
return AWS_OP_SUCCESS; |
| 230 |
} |
| 231 |
|
| 232 |
AWS_TEST_CASE(test_json_parse_to_string, s_test_json_parse_to_string) |
| 233 |
static int s_test_json_parse_to_string(struct aws_allocator *allocator, void *ctx) { |
| 234 |
(void)ctx; |
| 235 |
|
| 236 |
aws_common_library_init(allocator); |
| 237 |
struct aws_json_value *root = aws_json_value_new_object(allocator); |
| 238 |
|
| 239 |
struct aws_json_value *array = aws_json_value_new_array(allocator); |
| 240 |
aws_json_value_add_array_element(array, aws_json_value_new_number(allocator, 1)); |
| 241 |
aws_json_value_add_array_element(array, aws_json_value_new_number(allocator, 2)); |
| 242 |
aws_json_value_add_array_element(array, aws_json_value_new_number(allocator, 3)); |
| 243 |
aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("array"), array); |
| 244 |
|
| 245 |
aws_json_value_add_to_object_c_str(root, "boolean", aws_json_value_new_boolean(allocator, true)); |
| 246 |
aws_json_value_add_to_object_c_str(root, "color", aws_json_value_new_string_from_c_str(allocator, "gold")); |
| 247 |
aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("null"), aws_json_value_new_null(allocator)); |
| 248 |
aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("number"), aws_json_value_new_number(allocator, 123)); |
| 249 |
|
| 250 |
struct aws_json_value *object = aws_json_value_new_object(allocator); |
| 251 |
aws_json_value_add_to_object( |
| 252 |
object, aws_byte_cursor_from_c_str("a"), aws_json_value_new_string(allocator, aws_byte_cursor_from_c_str("b"))); |
| 253 |
aws_json_value_add_to_object( |
| 254 |
object, aws_byte_cursor_from_c_str("c"), aws_json_value_new_string(allocator, aws_byte_cursor_from_c_str("d"))); |
| 255 |
aws_json_value_add_to_object(root, aws_byte_cursor_from_c_str("object"), object); |
| 256 |
|
| 257 |
struct aws_byte_buf result_string_buf; |
| 258 |
aws_byte_buf_init(&result_string_buf, allocator, 0); |
| 259 |
|
| 260 |
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, aws_byte_buf_append_json_string(root, &result_string_buf)); |
| 261 |
struct aws_string *result_string = aws_string_new_from_buf(allocator, &result_string_buf); |
| 262 |
ASSERT_STR_EQUALS(s_test_json, aws_string_c_str(result_string)); |
| 263 |
aws_byte_buf_clean_up_secure(&result_string_buf); |
| 264 |
aws_string_destroy_secure(result_string); |
| 265 |
|
| 266 |
aws_json_value_destroy(root); |
| 267 |
aws_common_library_clean_up(); |
| 268 |
|
| 269 |
return AWS_OP_SUCCESS; |
| 270 |
} |