| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/clock.h> |
| 7 |
#include <aws/common/xml_parser.h> |
| 8 |
#include <aws/testing/aws_test_harness.h> |
| 9 |
|
| 10 |
const char *root_with_text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootNode>TestBody</rootNode>"; |
| 11 |
|
| 12 |
struct root_with_text_capture { |
| 13 |
struct aws_byte_cursor capture; |
| 14 |
struct aws_byte_cursor node_name; |
| 15 |
}; |
| 16 |
|
| 17 |
int s_root_with_text_root_node(struct aws_xml_node *node, void *user_data) { |
| 18 |
struct root_with_text_capture *capture = user_data; |
| 19 |
if (aws_xml_node_as_body(node, &capture->capture)) { |
| 20 |
return AWS_OP_ERR; |
| 21 |
} |
| 22 |
capture->node_name = aws_xml_node_get_name(node); |
| 23 |
|
| 24 |
return AWS_OP_SUCCESS; |
| 25 |
} |
| 26 |
|
| 27 |
static int s_xml_parser_root_with_text_test(struct aws_allocator *allocator, void *ctx) { |
| 28 |
(void)ctx; |
| 29 |
|
| 30 |
struct root_with_text_capture capture; |
| 31 |
AWS_ZERO_STRUCT(capture); |
| 32 |
|
| 33 |
struct aws_xml_parser_options options = { |
| 34 |
.doc = aws_byte_cursor_from_c_str(root_with_text), |
| 35 |
.on_root_encountered = s_root_with_text_root_node, |
| 36 |
.user_data = &capture, |
| 37 |
}; |
| 38 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 39 |
|
| 40 |
const char expected_name[] = "rootNode"; |
| 41 |
const char expected_value[] = "TestBody"; |
| 42 |
|
| 43 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.node_name.ptr, capture.node_name.len); |
| 44 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value, sizeof(expected_value) - 1, capture.capture.ptr, capture.capture.len); |
| 45 |
|
| 46 |
return AWS_OP_SUCCESS; |
| 47 |
} |
| 48 |
|
| 49 |
AWS_TEST_CASE(xml_parser_root_with_text, s_xml_parser_root_with_text_test) |
| 50 |
|
| 51 |
const char *child_with_text = |
| 52 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootNode><child1>TestBody</child1></rootNode>"; |
| 53 |
|
| 54 |
struct child_text_capture { |
| 55 |
struct aws_byte_cursor capture; |
| 56 |
struct aws_byte_cursor node_name; |
| 57 |
}; |
| 58 |
|
| 59 |
int s_child_with_text_root_node(struct aws_xml_node *node, void *user_data) { |
| 60 |
struct child_text_capture *capture = user_data; |
| 61 |
if (aws_xml_node_as_body(node, &capture->capture)) { |
| 62 |
return AWS_OP_ERR; |
| 63 |
} |
| 64 |
capture->node_name = aws_xml_node_get_name(node); |
| 65 |
|
| 66 |
return AWS_OP_SUCCESS; |
| 67 |
} |
| 68 |
|
| 69 |
int s_root_with_child(struct aws_xml_node *node, void *user_data) { |
| 70 |
if (aws_xml_node_traverse(node, s_child_with_text_root_node, user_data)) { |
| 71 |
return AWS_OP_ERR; |
| 72 |
} |
| 73 |
return AWS_OP_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
static int s_xml_parser_child_with_text_test(struct aws_allocator *allocator, void *ctx) { |
| 77 |
(void)ctx; |
| 78 |
struct child_text_capture capture; |
| 79 |
AWS_ZERO_STRUCT(capture); |
| 80 |
|
| 81 |
struct aws_xml_parser_options options = { |
| 82 |
.doc = aws_byte_cursor_from_c_str(child_with_text), |
| 83 |
.on_root_encountered = s_root_with_child, |
| 84 |
.user_data = &capture, |
| 85 |
}; |
| 86 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 87 |
|
| 88 |
const char expected_name[] = "child1"; |
| 89 |
const char expected_value[] = "TestBody"; |
| 90 |
|
| 91 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.node_name.ptr, capture.node_name.len); |
| 92 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value, sizeof(expected_value) - 1, capture.capture.ptr, capture.capture.len); |
| 93 |
|
| 94 |
return AWS_OP_SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
AWS_TEST_CASE(xml_parser_child_with_text, s_xml_parser_child_with_text_test) |
| 98 |
|
| 99 |
const char *s_empty_element_name_doc = |
| 100 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootNode><><child1>TestBody</child1></rootNode>"; |
| 101 |
|
| 102 |
static int s_xml_parser_empty_element_name_test(struct aws_allocator *allocator, void *ctx) { |
| 103 |
(void)ctx; |
| 104 |
struct child_text_capture capture; |
| 105 |
AWS_ZERO_STRUCT(capture); |
| 106 |
|
| 107 |
struct aws_xml_parser_options options = { |
| 108 |
.doc = aws_byte_cursor_from_c_str(s_empty_element_name_doc), |
| 109 |
.on_root_encountered = s_root_with_child, |
| 110 |
.user_data = &capture, |
| 111 |
}; |
| 112 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_xml_parse(allocator, &options)); |
| 113 |
|
| 114 |
return AWS_OP_SUCCESS; |
| 115 |
} |
| 116 |
|
| 117 |
AWS_TEST_CASE(xml_parser_empty_element_name_test, s_xml_parser_empty_element_name_test) |
| 118 |
|
| 119 |
const char *siblings_with_text = |
| 120 |
"<?xml version=\"1.0\" " |
| 121 |
"encoding=\"UTF-8\"?><rootNode><child1>TestBody</child1><child2>TestBody2</child2></rootNode>"; |
| 122 |
|
| 123 |
struct sibling_text_capture { |
| 124 |
struct aws_byte_cursor capture1; |
| 125 |
struct aws_byte_cursor capture2; |
| 126 |
struct aws_byte_cursor node_name1; |
| 127 |
struct aws_byte_cursor node_name2; |
| 128 |
}; |
| 129 |
|
| 130 |
int s_sibling_with_text_root_node(struct aws_xml_node *node, void *user_data) { |
| 131 |
struct sibling_text_capture *capture = user_data; |
| 132 |
|
| 133 |
struct aws_byte_cursor child1_name = aws_byte_cursor_from_c_str("child1"); |
| 134 |
struct aws_byte_cursor child2_name = aws_byte_cursor_from_c_str("child2"); |
| 135 |
|
| 136 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 137 |
|
| 138 |
if (aws_byte_cursor_eq_ignore_case(&node_name, &child1_name)) { |
| 139 |
capture->node_name1 = node_name; |
| 140 |
if (aws_xml_node_as_body(node, &capture->capture1)) { |
| 141 |
return AWS_OP_ERR; |
| 142 |
} |
| 143 |
} else if (aws_byte_cursor_eq_ignore_case(&node_name, &child2_name)) { |
| 144 |
capture->node_name2 = node_name; |
| 145 |
if (aws_xml_node_as_body(node, &capture->capture2)) { |
| 146 |
return AWS_OP_ERR; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return AWS_OP_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
int s_root_with_child_siblings(struct aws_xml_node *node, void *user_data) { |
| 154 |
return aws_xml_node_traverse(node, s_sibling_with_text_root_node, user_data); |
| 155 |
} |
| 156 |
|
| 157 |
static int s_xml_parser_siblings_with_text_test(struct aws_allocator *allocator, void *ctx) { |
| 158 |
(void)ctx; |
| 159 |
|
| 160 |
struct sibling_text_capture capture; |
| 161 |
AWS_ZERO_STRUCT(capture); |
| 162 |
|
| 163 |
struct aws_xml_parser_options options = { |
| 164 |
.doc = aws_byte_cursor_from_c_str(siblings_with_text), |
| 165 |
.on_root_encountered = s_root_with_child_siblings, |
| 166 |
.user_data = &capture, |
| 167 |
}; |
| 168 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 169 |
|
| 170 |
const char expected_name1[] = "child1"; |
| 171 |
const char expected_value1[] = "TestBody"; |
| 172 |
|
| 173 |
const char expected_name2[] = "child2"; |
| 174 |
const char expected_value2[] = "TestBody2"; |
| 175 |
|
| 176 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 177 |
expected_name1, sizeof(expected_name1) - 1, capture.node_name1.ptr, capture.node_name1.len); |
| 178 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value1, sizeof(expected_value1) - 1, capture.capture1.ptr, capture.capture1.len); |
| 179 |
|
| 180 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 181 |
expected_name2, sizeof(expected_name2) - 1, capture.node_name2.ptr, capture.node_name2.len); |
| 182 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value2, sizeof(expected_value2) - 1, capture.capture2.ptr, capture.capture2.len); |
| 183 |
|
| 184 |
return AWS_OP_SUCCESS; |
| 185 |
} |
| 186 |
|
| 187 |
AWS_TEST_CASE(xml_parser_siblings_with_text, s_xml_parser_siblings_with_text_test) |
| 188 |
|
| 189 |
const char *siblings_with_empty_tag = "<?xml version=\"1.0\" " |
| 190 |
"encoding=\"UTF-8\"?><rootNode><child1 /><child2>TestBody2</child2></rootNode>"; |
| 191 |
|
| 192 |
static int s_xml_parser_child_empty_tag(struct aws_allocator *allocator, void *ctx) { |
| 193 |
(void)ctx; |
| 194 |
struct sibling_text_capture capture; |
| 195 |
AWS_ZERO_STRUCT(capture); |
| 196 |
|
| 197 |
capture.capture1 = aws_byte_cursor_from_c_str("random values"); |
| 198 |
|
| 199 |
struct aws_xml_parser_options options = { |
| 200 |
.doc = aws_byte_cursor_from_c_str(siblings_with_empty_tag), |
| 201 |
.on_root_encountered = s_root_with_child_siblings, |
| 202 |
.user_data = &capture, |
| 203 |
}; |
| 204 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 205 |
|
| 206 |
const char expected_name1[] = "child1"; |
| 207 |
|
| 208 |
const char expected_name2[] = "child2"; |
| 209 |
const char expected_value2[] = "TestBody2"; |
| 210 |
|
| 211 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 212 |
expected_name1, sizeof(expected_name1) - 1, capture.node_name1.ptr, capture.node_name1.len); |
| 213 |
ASSERT_PTR_EQUALS(capture.capture1.ptr, NULL); |
| 214 |
ASSERT_UINT_EQUALS(capture.capture1.len, 0); |
| 215 |
|
| 216 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 217 |
expected_name2, sizeof(expected_name2) - 1, capture.node_name2.ptr, capture.node_name2.len); |
| 218 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value2, sizeof(expected_value2) - 1, capture.capture2.ptr, capture.capture2.len); |
| 219 |
|
| 220 |
return AWS_OP_SUCCESS; |
| 221 |
} |
| 222 |
|
| 223 |
AWS_TEST_CASE(xml_parser_child_empty_tag, s_xml_parser_child_empty_tag) |
| 224 |
|
| 225 |
const char *preamble_and_attributes = |
| 226 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 227 |
"<!DOCTYPE html \n" |
| 228 |
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
| 229 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> " |
| 230 |
"<rootNode attribute1=\"abc\" attribute2=\"def\">\n" |
| 231 |
"<child1>TestBody</child1><child2 attribute3=\"childAttr\">TestBody2</child2></rootNode>"; |
| 232 |
|
| 233 |
struct preamble_and_attributes_capture { |
| 234 |
struct aws_byte_cursor capture1; |
| 235 |
struct aws_byte_cursor capture2; |
| 236 |
struct aws_xml_attribute capture2_attr; |
| 237 |
struct aws_byte_cursor node_name1; |
| 238 |
struct aws_byte_cursor node_name2; |
| 239 |
struct aws_xml_attribute root_attr1; |
| 240 |
struct aws_xml_attribute root_attr2; |
| 241 |
}; |
| 242 |
|
| 243 |
int s_preamble_and_attributes_child_node(struct aws_xml_node *node, void *user_data) { |
| 244 |
struct preamble_and_attributes_capture *capture = user_data; |
| 245 |
|
| 246 |
struct aws_byte_cursor child1_name = aws_byte_cursor_from_c_str("child1"); |
| 247 |
struct aws_byte_cursor child2_name = aws_byte_cursor_from_c_str("child2"); |
| 248 |
|
| 249 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 250 |
|
| 251 |
if (aws_byte_cursor_eq_ignore_case(&node_name, &child1_name)) { |
| 252 |
capture->node_name1 = node_name; |
| 253 |
if (aws_xml_node_as_body(node, &capture->capture1)) { |
| 254 |
return AWS_OP_ERR; |
| 255 |
} |
| 256 |
} else if (aws_byte_cursor_eq_ignore_case(&node_name, &child2_name)) { |
| 257 |
capture->node_name2 = node_name; |
| 258 |
if (aws_xml_node_as_body(node, &capture->capture2)) { |
| 259 |
return AWS_OP_ERR; |
| 260 |
} |
| 261 |
|
| 262 |
ASSERT_TRUE(aws_xml_node_get_num_attributes(node) == 1); |
| 263 |
|
| 264 |
capture->capture2_attr = aws_xml_node_get_attribute(node, 0); |
| 265 |
} |
| 266 |
|
| 267 |
return AWS_OP_SUCCESS; |
| 268 |
} |
| 269 |
|
| 270 |
int s_preamble_and_attributes(struct aws_xml_node *node, void *user_data) { |
| 271 |
struct preamble_and_attributes_capture *capture = user_data; |
| 272 |
|
| 273 |
ASSERT_TRUE(aws_xml_node_get_num_attributes(node) == 2); |
| 274 |
|
| 275 |
capture->root_attr1 = aws_xml_node_get_attribute(node, 0); |
| 276 |
capture->root_attr2 = aws_xml_node_get_attribute(node, 1); |
| 277 |
return aws_xml_node_traverse(node, s_preamble_and_attributes_child_node, user_data); |
| 278 |
} |
| 279 |
|
| 280 |
static int s_xml_parser_preamble_and_attributes_test(struct aws_allocator *allocator, void *ctx) { |
| 281 |
(void)ctx; |
| 282 |
|
| 283 |
struct preamble_and_attributes_capture capture; |
| 284 |
AWS_ZERO_STRUCT(capture); |
| 285 |
|
| 286 |
struct aws_xml_parser_options options = { |
| 287 |
.doc = aws_byte_cursor_from_c_str(preamble_and_attributes), |
| 288 |
.on_root_encountered = s_preamble_and_attributes, |
| 289 |
.user_data = &capture, |
| 290 |
}; |
| 291 |
|
| 292 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 293 |
|
| 294 |
const char expected_attr1_name[] = "attribute1"; |
| 295 |
const char expected_attr1_value1[] = "abc"; |
| 296 |
|
| 297 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 298 |
expected_attr1_name, sizeof(expected_attr1_name) - 1, capture.root_attr1.name.ptr, capture.root_attr1.name.len); |
| 299 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 300 |
expected_attr1_value1, |
| 301 |
sizeof(expected_attr1_value1) - 1, |
| 302 |
capture.root_attr1.value.ptr, |
| 303 |
capture.root_attr1.value.len); |
| 304 |
|
| 305 |
const char expected_attr2_name[] = "attribute2"; |
| 306 |
const char expected_attr2_value1[] = "def"; |
| 307 |
|
| 308 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 309 |
expected_attr2_name, sizeof(expected_attr2_name) - 1, capture.root_attr2.name.ptr, capture.root_attr2.name.len); |
| 310 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 311 |
expected_attr2_value1, |
| 312 |
sizeof(expected_attr2_value1) - 1, |
| 313 |
capture.root_attr2.value.ptr, |
| 314 |
capture.root_attr2.value.len); |
| 315 |
|
| 316 |
const char expected_name1[] = "child1"; |
| 317 |
const char expected_value1[] = "TestBody"; |
| 318 |
|
| 319 |
const char expected_name2[] = "child2"; |
| 320 |
const char expected_value2[] = "TestBody2"; |
| 321 |
|
| 322 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 323 |
expected_name1, sizeof(expected_name1) - 1, capture.node_name1.ptr, capture.node_name1.len); |
| 324 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value1, sizeof(expected_value1) - 1, capture.capture1.ptr, capture.capture1.len); |
| 325 |
|
| 326 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 327 |
expected_name2, sizeof(expected_name2) - 1, capture.node_name2.ptr, capture.node_name2.len); |
| 328 |
ASSERT_BIN_ARRAYS_EQUALS(expected_value2, sizeof(expected_value2) - 1, capture.capture2.ptr, capture.capture2.len); |
| 329 |
|
| 330 |
const char expected_attr3_name[] = "attribute3"; |
| 331 |
const char expected_attr3_value1[] = "childAttr"; |
| 332 |
|
| 333 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 334 |
expected_attr3_name, |
| 335 |
sizeof(expected_attr2_name) - 1, |
| 336 |
capture.capture2_attr.name.ptr, |
| 337 |
capture.capture2_attr.name.len); |
| 338 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 339 |
expected_attr3_value1, |
| 340 |
sizeof(expected_attr3_value1) - 1, |
| 341 |
capture.capture2_attr.value.ptr, |
| 342 |
capture.capture2_attr.value.len); |
| 343 |
|
| 344 |
return AWS_OP_SUCCESS; |
| 345 |
} |
| 346 |
|
| 347 |
AWS_TEST_CASE(xml_parser_preamble_and_attributes, s_xml_parser_preamble_and_attributes_test) |
| 348 |
|
| 349 |
const char *nested_nodes_same_name_doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 350 |
"<!DOCTYPE html \n" |
| 351 |
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
| 352 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> " |
| 353 |
"<Node>\n" |
| 354 |
" <Node>\n" |
| 355 |
" <Node>\n" |
| 356 |
" <Node>TestBody</Node>\n" |
| 357 |
" </Node>\n" |
| 358 |
" </Node>\n" |
| 359 |
" <Node>\n" |
| 360 |
" <Node>TestBody2</Node>\n" |
| 361 |
" </Node>\n" |
| 362 |
"</Node>"; |
| 363 |
|
| 364 |
struct nested_node_capture { |
| 365 |
struct aws_byte_cursor node_body; |
| 366 |
}; |
| 367 |
|
| 368 |
int s_nested_node(struct aws_xml_node *node, void *user_data) { |
| 369 |
struct nested_node_capture *capture = user_data; |
| 370 |
|
| 371 |
return aws_xml_node_as_body(node, &capture->node_body); |
| 372 |
} |
| 373 |
|
| 374 |
static int s_xml_parser_nested_node_same_name_test(struct aws_allocator *allocator, void *ctx) { |
| 375 |
(void)ctx; |
| 376 |
|
| 377 |
struct nested_node_capture capture; |
| 378 |
AWS_ZERO_STRUCT(capture); |
| 379 |
|
| 380 |
struct aws_xml_parser_options options = { |
| 381 |
.doc = aws_byte_cursor_from_c_str(nested_nodes_same_name_doc), |
| 382 |
.on_root_encountered = s_nested_node, |
| 383 |
.user_data = &capture, |
| 384 |
}; |
| 385 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 386 |
|
| 387 |
const char *expected_body = "\n <Node>\n" |
| 388 |
" <Node>\n" |
| 389 |
" <Node>TestBody</Node>\n" |
| 390 |
" </Node>\n" |
| 391 |
" </Node>\n" |
| 392 |
" <Node>\n" |
| 393 |
" <Node>TestBody2</Node>\n" |
| 394 |
" </Node>\n"; |
| 395 |
|
| 396 |
ASSERT_BIN_ARRAYS_EQUALS(expected_body, strlen(expected_body), capture.node_body.ptr, capture.node_body.len); |
| 397 |
|
| 398 |
return AWS_OP_SUCCESS; |
| 399 |
} |
| 400 |
|
| 401 |
AWS_TEST_CASE(xml_parser_nested_node_same_name_test, s_xml_parser_nested_node_same_name_test) |
| 402 |
|
| 403 |
const char *nested_nodes_deep_recursion_doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 404 |
"<!DOCTYPE html \n" |
| 405 |
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
| 406 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> " |
| 407 |
"<Node>\n" |
| 408 |
" <Node>\n" |
| 409 |
" <Node></Node>\n" |
| 410 |
" </Node>\n" |
| 411 |
"</Node>"; |
| 412 |
|
| 413 |
int s_nested_node_deep_recursion(struct aws_xml_node *node, void *user_data) { |
| 414 |
return aws_xml_node_traverse(node, s_nested_node_deep_recursion, user_data); |
| 415 |
} |
| 416 |
|
| 417 |
static int s_xml_parser_nested_node_deep_recursion_test(struct aws_allocator *allocator, void *ctx) { |
| 418 |
(void)ctx; |
| 419 |
struct aws_xml_parser_options options = { |
| 420 |
.doc = aws_byte_cursor_from_c_str(nested_nodes_deep_recursion_doc), |
| 421 |
.max_depth = 2, |
| 422 |
.on_root_encountered = s_nested_node_deep_recursion, |
| 423 |
.user_data = NULL, |
| 424 |
}; |
| 425 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_xml_parse(allocator, &options)); |
| 426 |
|
| 427 |
return AWS_OP_SUCCESS; |
| 428 |
} |
| 429 |
|
| 430 |
AWS_TEST_CASE(xml_parser_nested_node_deep_recursion_test, s_xml_parser_nested_node_deep_recursion_test) |
| 431 |
|
| 432 |
const char *too_many_attributes = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 433 |
"<!DOCTYPE html \n" |
| 434 |
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
| 435 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> " |
| 436 |
"<rootNode attribute1=\"abc\" attribute2=\"def\" attribute3=\"def\" " |
| 437 |
"attribute4=\"def\" attribute5=\"def\" attribute6=\"def\" attribute7=\"def\" " |
| 438 |
"attribute8=\"def\" attribute9=\"def\" attribute10=\"def\" attribute11=\"def\">\n" |
| 439 |
"</rootNode>"; |
| 440 |
|
| 441 |
int s_too_many_attributes(struct aws_xml_node *node, void *user_data) { |
| 442 |
(void)node; |
| 443 |
(void)user_data; |
| 444 |
return AWS_OP_SUCCESS; |
| 445 |
} |
| 446 |
|
| 447 |
static int s_xml_parser_too_many_attributes_test(struct aws_allocator *allocator, void *ctx) { |
| 448 |
(void)ctx; |
| 449 |
|
| 450 |
struct aws_xml_parser_options options = { |
| 451 |
.doc = aws_byte_cursor_from_c_str(too_many_attributes), |
| 452 |
.on_root_encountered = s_too_many_attributes, |
| 453 |
.user_data = NULL, |
| 454 |
}; |
| 455 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_xml_parse(allocator, &options)); |
| 456 |
|
| 457 |
return AWS_OP_SUCCESS; |
| 458 |
} |
| 459 |
|
| 460 |
AWS_TEST_CASE(xml_parser_too_many_attributes_test, s_xml_parser_too_many_attributes_test) |
| 461 |
|
| 462 |
const char *node_name_too_long = |
| 463 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 464 |
"<!DOCTYPE html \n" |
| 465 |
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
| 466 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> " |
| 467 |
"<abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi" |
| 468 |
"jklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs" |
| 469 |
"tuvwxyzabcdefghijklmnopqrstuvwxyz>" |
| 470 |
"</" |
| 471 |
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij" |
| 472 |
"klmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst" |
| 473 |
"uvwxyzabcdefghijklmnopqrstuvwxyz>"; |
| 474 |
|
| 475 |
int s_too_long(struct aws_xml_node *node, void *user_data) { |
| 476 |
(void)node; |
| 477 |
(void)user_data; |
| 478 |
return AWS_OP_SUCCESS; |
| 479 |
} |
| 480 |
|
| 481 |
static int s_xml_parser_name_too_long_test(struct aws_allocator *allocator, void *ctx) { |
| 482 |
(void)ctx; |
| 483 |
|
| 484 |
struct aws_xml_parser_options options = { |
| 485 |
.doc = aws_byte_cursor_from_c_str(node_name_too_long), |
| 486 |
.on_root_encountered = s_too_long, |
| 487 |
.user_data = NULL, |
| 488 |
}; |
| 489 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_xml_parse(allocator, &options)); |
| 490 |
|
| 491 |
return AWS_OP_SUCCESS; |
| 492 |
} |
| 493 |
|
| 494 |
AWS_TEST_CASE(xml_parser_name_too_long_test, s_xml_parser_name_too_long_test) |
| 495 |
|
| 496 |
static int s_xml_parser_unescape_test(struct aws_allocator *allocator, void *ctx) { |
| 497 |
(void)ctx; |
| 498 |
|
| 499 |
enum { num_cases = 5 }; |
| 500 |
|
| 501 |
struct aws_byte_cursor input[num_cases] = { |
| 502 |
aws_byte_cursor_from_c_str("Au revoir, Shoshanna!"), |
| 503 |
aws_byte_cursor_from_c_str("quote1 " quote2 " quote3 " " |
| 504 |
"gt1 > gt2 > gt3 > " |
| 505 |
"lt1 < lt2 < lt3 < " |
| 506 |
"apos1 ' apos2 ' apos3 ' " |
| 507 |
"amp1 & amp2 & amp3 &"), |
| 508 |
aws_byte_cursor_from_c_str("Say 'what' again; Say 'what' again..."), |
| 509 |
aws_byte_cursor_from_c_str("Hello 😀"), |
| 510 |
aws_byte_cursor_from_c_str("Hello 😀")}; |
| 511 |
|
| 512 |
struct aws_byte_cursor expected[num_cases] = { |
| 513 |
aws_byte_cursor_from_c_str("Au revoir, Shoshanna!"), |
| 514 |
aws_byte_cursor_from_c_str("quote1 \" quote2 \" quote3 \" " |
| 515 |
"gt1 > gt2 > gt3 > " |
| 516 |
"lt1 < lt2 < lt3 < " |
| 517 |
"apos1 ' apos2 ' apos3 ' " |
| 518 |
"amp1 & amp2 & amp3 &"), |
| 519 |
aws_byte_cursor_from_c_str("Say 'what' again; Say 'what' again..."), |
| 520 |
aws_byte_cursor_from_c_str("Hello \xF0\x9F\x98\x80"), |
| 521 |
aws_byte_cursor_from_c_str("Hello \xF0\x9F\x98\x80")}; |
| 522 |
|
| 523 |
for (size_t i = 0; i < num_cases; ++i) { |
| 524 |
struct aws_byte_buf out; |
| 525 |
aws_byte_buf_init(&out, allocator, 30); |
| 526 |
|
| 527 |
ASSERT_SUCCESS(aws_byte_buf_append_unescaped_xml(allocator, input[i], &out)); |
| 528 |
|
| 529 |
ASSERT_BIN_ARRAYS_EQUALS(out.buffer, out.len, expected[i].ptr, expected[i].len); |
| 530 |
|
| 531 |
aws_byte_buf_clean_up(&out); |
| 532 |
} |
| 533 |
|
| 534 |
return AWS_OP_SUCCESS; |
| 535 |
} |
| 536 |
AWS_TEST_CASE(xml_parser_unescape_test, s_xml_parser_unescape_test) |
| 537 |
|
| 538 |
static int s_xml_parser_unescape_error_test(struct aws_allocator *allocator, void *ctx) { |
| 539 |
(void)ctx; |
| 540 |
|
| 541 |
enum { num_cases = 7 }; |
| 542 |
|
| 543 |
struct aws_byte_cursor input[num_cases] = { |
| 544 |
aws_byte_cursor_from_c_str("aaa &bbb c"), |
| 545 |
aws_byte_cursor_from_c_str("aaa &" c"), |
| 546 |
aws_byte_cursor_from_c_str("aaa "123; c"), |
| 547 |
aws_byte_cursor_from_c_str("aaa &#; c"), |
| 548 |
aws_byte_cursor_from_c_str("aaa &#x; c"), |
| 549 |
aws_byte_cursor_from_c_str("aaa � c"), |
| 550 |
aws_byte_cursor_from_c_str("aaa � c"), |
| 551 |
}; |
| 552 |
|
| 553 |
for (size_t i = 0; i < num_cases; ++i) { |
| 554 |
struct aws_byte_buf out; |
| 555 |
aws_byte_buf_init(&out, allocator, 30); |
| 556 |
|
| 557 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_byte_buf_append_unescaped_xml(allocator, input[i], &out)); |
| 558 |
|
| 559 |
aws_byte_buf_clean_up(&out); |
| 560 |
} |
| 561 |
|
| 562 |
return AWS_OP_SUCCESS; |
| 563 |
} |
| 564 |
AWS_TEST_CASE(xml_parser_unescape_error_test, s_xml_parser_unescape_error_test) |
| 565 |
|
| 566 |
const char *child_with_text_escaped = |
| 567 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootNode><child1>"TestBody"</child1></rootNode>"; |
| 568 |
|
| 569 |
struct child_text_buf_capture { |
| 570 |
struct aws_allocator *allocator; |
| 571 |
struct aws_byte_buf capture_buf; |
| 572 |
struct aws_byte_cursor node_name; |
| 573 |
}; |
| 574 |
|
| 575 |
int s_child_with_text_root_node_escaped(struct aws_xml_node *node, void *user_data) { |
| 576 |
struct child_text_buf_capture *capture = user_data; |
| 577 |
if (aws_xml_node_as_body_unescaped(capture->allocator, node, &capture->capture_buf)) { |
| 578 |
return AWS_OP_ERR; |
| 579 |
} |
| 580 |
capture->node_name = aws_xml_node_get_name(node); |
| 581 |
|
| 582 |
return AWS_OP_SUCCESS; |
| 583 |
} |
| 584 |
|
| 585 |
int s_root_with_child_escaped(struct aws_xml_node *node, void *user_data) { |
| 586 |
if (aws_xml_node_traverse(node, s_child_with_text_root_node_escaped, user_data)) { |
| 587 |
return AWS_OP_ERR; |
| 588 |
} |
| 589 |
return AWS_OP_SUCCESS; |
| 590 |
} |
| 591 |
|
| 592 |
static int s_xml_parser_child_with_text_escaped_test(struct aws_allocator *allocator, void *ctx) { |
| 593 |
(void)ctx; |
| 594 |
struct child_text_buf_capture capture; |
| 595 |
AWS_ZERO_STRUCT(capture); |
| 596 |
capture.allocator = allocator; |
| 597 |
aws_byte_buf_init(&capture.capture_buf, allocator, 30); |
| 598 |
|
| 599 |
struct aws_xml_parser_options options = { |
| 600 |
.doc = aws_byte_cursor_from_c_str(child_with_text_escaped), |
| 601 |
.on_root_encountered = s_root_with_child_escaped, |
| 602 |
.user_data = &capture, |
| 603 |
}; |
| 604 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 605 |
|
| 606 |
const char expected_name[] = "child1"; |
| 607 |
const char expected_value[] = "\"TestBody\""; |
| 608 |
|
| 609 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.node_name.ptr, capture.node_name.len); |
| 610 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 611 |
expected_value, sizeof(expected_value) - 1, capture.capture_buf.buffer, capture.capture_buf.len); |
| 612 |
|
| 613 |
aws_byte_buf_clean_up(&capture.capture_buf); |
| 614 |
|
| 615 |
return AWS_OP_SUCCESS; |
| 616 |
} |
| 617 |
|
| 618 |
AWS_TEST_CASE(xml_parser_child_with_text_escaped, s_xml_parser_child_with_text_escaped_test) |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
const char *prefix_collision_doc = "<ListBucketResult>" |
| 625 |
"<Contents>" |
| 626 |
"<Owner><OwnerId>abc123</OwnerId><DisplayName>test</DisplayName></Owner>" |
| 627 |
"<Key>file.txt</Key>" |
| 628 |
"</Contents>" |
| 629 |
"</ListBucketResult>"; |
| 630 |
|
| 631 |
struct prefix_collision_capture { |
| 632 |
struct aws_byte_cursor owner_body; |
| 633 |
struct aws_byte_cursor key_body; |
| 634 |
}; |
| 635 |
|
| 636 |
int s_prefix_collision_contents_child(struct aws_xml_node *node, void *user_data) { |
| 637 |
struct prefix_collision_capture *capture = user_data; |
| 638 |
struct aws_byte_cursor name = aws_xml_node_get_name(node); |
| 639 |
struct aws_byte_cursor owner_name = aws_byte_cursor_from_c_str("Owner"); |
| 640 |
struct aws_byte_cursor key_name = aws_byte_cursor_from_c_str("Key"); |
| 641 |
|
| 642 |
if (aws_byte_cursor_eq(&name, &owner_name)) { |
| 643 |
return aws_xml_node_as_body(node, &capture->owner_body); |
| 644 |
} else if (aws_byte_cursor_eq(&name, &key_name)) { |
| 645 |
return aws_xml_node_as_body(node, &capture->key_body); |
| 646 |
} |
| 647 |
return AWS_OP_SUCCESS; |
| 648 |
} |
| 649 |
|
| 650 |
int s_prefix_collision_contents(struct aws_xml_node *node, void *user_data) { |
| 651 |
return aws_xml_node_traverse(node, s_prefix_collision_contents_child, user_data); |
| 652 |
} |
| 653 |
|
| 654 |
int s_prefix_collision_root(struct aws_xml_node *node, void *user_data) { |
| 655 |
return aws_xml_node_traverse(node, s_prefix_collision_contents, user_data); |
| 656 |
} |
| 657 |
|
| 658 |
static int s_xml_parser_prefix_collision_test(struct aws_allocator *allocator, void *ctx) { |
| 659 |
(void)ctx; |
| 660 |
|
| 661 |
struct prefix_collision_capture capture; |
| 662 |
AWS_ZERO_STRUCT(capture); |
| 663 |
|
| 664 |
struct aws_xml_parser_options options = { |
| 665 |
.doc = aws_byte_cursor_from_c_str(prefix_collision_doc), |
| 666 |
.on_root_encountered = s_prefix_collision_root, |
| 667 |
.user_data = &capture, |
| 668 |
}; |
| 669 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 670 |
|
| 671 |
const char expected_owner[] = "<OwnerId>abc123</OwnerId><DisplayName>test</DisplayName>"; |
| 672 |
const char expected_key[] = "file.txt"; |
| 673 |
|
| 674 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 675 |
expected_owner, sizeof(expected_owner) - 1, capture.owner_body.ptr, capture.owner_body.len); |
| 676 |
ASSERT_BIN_ARRAYS_EQUALS(expected_key, sizeof(expected_key) - 1, capture.key_body.ptr, capture.key_body.len); |
| 677 |
|
| 678 |
return AWS_OP_SUCCESS; |
| 679 |
} |
| 680 |
|
| 681 |
AWS_TEST_CASE(xml_parser_prefix_collision_test, s_xml_parser_prefix_collision_test) |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
const char *nested_same_name_with_prefix_children_doc = "<a><a>inner</a><ab>other</ab></a>"; |
| 688 |
|
| 689 |
struct nested_prefix_capture { |
| 690 |
struct aws_byte_cursor a_body; |
| 691 |
}; |
| 692 |
|
| 693 |
int s_nested_prefix_root(struct aws_xml_node *node, void *user_data) { |
| 694 |
struct nested_prefix_capture *capture = user_data; |
| 695 |
return aws_xml_node_as_body(node, &capture->a_body); |
| 696 |
} |
| 697 |
|
| 698 |
static int s_xml_parser_nested_same_name_with_prefix_children_test(struct aws_allocator *allocator, void *ctx) { |
| 699 |
(void)ctx; |
| 700 |
|
| 701 |
struct nested_prefix_capture capture; |
| 702 |
AWS_ZERO_STRUCT(capture); |
| 703 |
|
| 704 |
struct aws_xml_parser_options options = { |
| 705 |
.doc = aws_byte_cursor_from_c_str(nested_same_name_with_prefix_children_doc), |
| 706 |
.on_root_encountered = s_nested_prefix_root, |
| 707 |
.user_data = &capture, |
| 708 |
}; |
| 709 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 710 |
|
| 711 |
const char expected[] = "<a>inner</a><ab>other</ab>"; |
| 712 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected) - 1, capture.a_body.ptr, capture.a_body.len); |
| 713 |
|
| 714 |
return AWS_OP_SUCCESS; |
| 715 |
} |
| 716 |
|
| 717 |
AWS_TEST_CASE( |
| 718 |
xml_parser_nested_same_name_with_prefix_children_test, |
| 719 |
s_xml_parser_nested_same_name_with_prefix_children_test) |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
const char *comment_in_body_doc = "<root><!-- this is a comment --><child>value</child></root>"; |
| 725 |
|
| 726 |
struct comment_capture { |
| 727 |
struct aws_byte_cursor child_body; |
| 728 |
struct aws_byte_cursor child_name; |
| 729 |
}; |
| 730 |
|
| 731 |
int s_comment_child(struct aws_xml_node *node, void *user_data) { |
| 732 |
struct comment_capture *capture = user_data; |
| 733 |
capture->child_name = aws_xml_node_get_name(node); |
| 734 |
return aws_xml_node_as_body(node, &capture->child_body); |
| 735 |
} |
| 736 |
|
| 737 |
int s_comment_root(struct aws_xml_node *node, void *user_data) { |
| 738 |
return aws_xml_node_traverse(node, s_comment_child, user_data); |
| 739 |
} |
| 740 |
|
| 741 |
static int s_xml_parser_comment_skipped_test(struct aws_allocator *allocator, void *ctx) { |
| 742 |
(void)ctx; |
| 743 |
|
| 744 |
struct comment_capture capture; |
| 745 |
AWS_ZERO_STRUCT(capture); |
| 746 |
|
| 747 |
struct aws_xml_parser_options options = { |
| 748 |
.doc = aws_byte_cursor_from_c_str(comment_in_body_doc), |
| 749 |
.on_root_encountered = s_comment_root, |
| 750 |
.user_data = &capture, |
| 751 |
}; |
| 752 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 753 |
|
| 754 |
const char expected_name[] = "child"; |
| 755 |
const char expected_value[] = "value"; |
| 756 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.child_name.ptr, capture.child_name.len); |
| 757 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 758 |
expected_value, sizeof(expected_value) - 1, capture.child_body.ptr, capture.child_body.len); |
| 759 |
|
| 760 |
return AWS_OP_SUCCESS; |
| 761 |
} |
| 762 |
|
| 763 |
AWS_TEST_CASE(xml_parser_comment_skipped_test, s_xml_parser_comment_skipped_test) |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
const char *cdata_in_body_doc = "<root><![CDATA[<not>an</element>]]><child>value</child></root>"; |
| 769 |
|
| 770 |
static int s_xml_parser_cdata_skipped_test(struct aws_allocator *allocator, void *ctx) { |
| 771 |
(void)ctx; |
| 772 |
|
| 773 |
struct comment_capture capture; |
| 774 |
AWS_ZERO_STRUCT(capture); |
| 775 |
|
| 776 |
struct aws_xml_parser_options options = { |
| 777 |
.doc = aws_byte_cursor_from_c_str(cdata_in_body_doc), |
| 778 |
.on_root_encountered = s_comment_root, |
| 779 |
.user_data = &capture, |
| 780 |
}; |
| 781 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 782 |
|
| 783 |
const char expected_name[] = "child"; |
| 784 |
const char expected_value[] = "value"; |
| 785 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.child_name.ptr, capture.child_name.len); |
| 786 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 787 |
expected_value, sizeof(expected_value) - 1, capture.child_body.ptr, capture.child_body.len); |
| 788 |
|
| 789 |
return AWS_OP_SUCCESS; |
| 790 |
} |
| 791 |
|
| 792 |
AWS_TEST_CASE(xml_parser_cdata_skipped_test, s_xml_parser_cdata_skipped_test) |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
const char *pi_in_body_doc = "<root><?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?><child>value</child></root>"; |
| 798 |
|
| 799 |
static int s_xml_parser_pi_skipped_test(struct aws_allocator *allocator, void *ctx) { |
| 800 |
(void)ctx; |
| 801 |
|
| 802 |
struct comment_capture capture; |
| 803 |
AWS_ZERO_STRUCT(capture); |
| 804 |
|
| 805 |
struct aws_xml_parser_options options = { |
| 806 |
.doc = aws_byte_cursor_from_c_str(pi_in_body_doc), |
| 807 |
.on_root_encountered = s_comment_root, |
| 808 |
.user_data = &capture, |
| 809 |
}; |
| 810 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 811 |
|
| 812 |
const char expected_name[] = "child"; |
| 813 |
const char expected_value[] = "value"; |
| 814 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.child_name.ptr, capture.child_name.len); |
| 815 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 816 |
expected_value, sizeof(expected_value) - 1, capture.child_body.ptr, capture.child_body.len); |
| 817 |
|
| 818 |
return AWS_OP_SUCCESS; |
| 819 |
} |
| 820 |
|
| 821 |
AWS_TEST_CASE(xml_parser_pi_skipped_test, s_xml_parser_pi_skipped_test) |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
const char *comment_with_gt_doc = "<root><!-- contains > character --><child>safe</child></root>"; |
| 828 |
|
| 829 |
static int s_xml_parser_comment_with_gt_test(struct aws_allocator *allocator, void *ctx) { |
| 830 |
(void)ctx; |
| 831 |
|
| 832 |
struct comment_capture capture; |
| 833 |
AWS_ZERO_STRUCT(capture); |
| 834 |
|
| 835 |
struct aws_xml_parser_options options = { |
| 836 |
.doc = aws_byte_cursor_from_c_str(comment_with_gt_doc), |
| 837 |
.on_root_encountered = s_comment_root, |
| 838 |
.user_data = &capture, |
| 839 |
}; |
| 840 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 841 |
|
| 842 |
const char expected_name[] = "child"; |
| 843 |
const char expected_value[] = "safe"; |
| 844 |
ASSERT_BIN_ARRAYS_EQUALS(expected_name, sizeof(expected_name) - 1, capture.child_name.ptr, capture.child_name.len); |
| 845 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 846 |
expected_value, sizeof(expected_value) - 1, capture.child_body.ptr, capture.child_body.len); |
| 847 |
|
| 848 |
return AWS_OP_SUCCESS; |
| 849 |
} |
| 850 |
|
| 851 |
AWS_TEST_CASE(xml_parser_comment_with_gt_test, s_xml_parser_comment_with_gt_test) |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
static int s_xml_parser_nested_same_name_large_depth_test(struct aws_allocator *allocator, void *ctx) { |
| 858 |
(void)ctx; |
| 859 |
|
| 860 |
|
| 861 |
const size_t depth = 20000; |
| 862 |
struct aws_byte_buf doc_buf; |
| 863 |
aws_byte_buf_init(&doc_buf, allocator, depth * 10); |
| 864 |
|
| 865 |
struct aws_byte_cursor a_open = aws_byte_cursor_from_c_str("<a>"); |
| 866 |
struct aws_byte_cursor a_close = aws_byte_cursor_from_c_str("</a>"); |
| 867 |
|
| 868 |
for (size_t i = 0; i < depth; i++) { |
| 869 |
aws_byte_buf_append_dynamic(&doc_buf, &a_open); |
| 870 |
} |
| 871 |
for (size_t i = 0; i < depth; i++) { |
| 872 |
aws_byte_buf_append_dynamic(&doc_buf, &a_close); |
| 873 |
} |
| 874 |
|
| 875 |
struct nested_node_capture capture; |
| 876 |
AWS_ZERO_STRUCT(capture); |
| 877 |
|
| 878 |
struct aws_xml_parser_options options = { |
| 879 |
.doc = aws_byte_cursor_from_buf(&doc_buf), |
| 880 |
.on_root_encountered = s_nested_node, |
| 881 |
.user_data = &capture, |
| 882 |
}; |
| 883 |
|
| 884 |
uint64_t start_ns = 0; |
| 885 |
aws_high_res_clock_get_ticks(&start_ns); |
| 886 |
|
| 887 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 888 |
|
| 889 |
uint64_t end_ns = 0; |
| 890 |
aws_high_res_clock_get_ticks(&end_ns); |
| 891 |
|
| 892 |
uint64_t elapsed_ms = (end_ns - start_ns) / 1000000; |
| 893 |
ASSERT_TRUE( |
| 894 |
elapsed_ms < 2000, |
| 895 |
"xml parse took %llu ms — expected < 2000 ms with depth=%zu", |
| 896 |
(unsigned long long)elapsed_ms, |
| 897 |
depth); |
| 898 |
|
| 899 |
ASSERT_TRUE(capture.node_body.len > 0); |
| 900 |
|
| 901 |
aws_byte_buf_clean_up(&doc_buf); |
| 902 |
return AWS_OP_SUCCESS; |
| 903 |
} |
| 904 |
|
| 905 |
AWS_TEST_CASE(xml_parser_nested_same_name_large_depth_test, s_xml_parser_nested_same_name_large_depth_test) |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
const char *self_closing_same_name_doc = "<a><a/>inner</a>"; |
| 911 |
|
| 912 |
static int s_xml_parser_self_closing_same_name_test(struct aws_allocator *allocator, void *ctx) { |
| 913 |
(void)ctx; |
| 914 |
|
| 915 |
struct nested_node_capture capture; |
| 916 |
AWS_ZERO_STRUCT(capture); |
| 917 |
|
| 918 |
struct aws_xml_parser_options options = { |
| 919 |
.doc = aws_byte_cursor_from_c_str(self_closing_same_name_doc), |
| 920 |
.on_root_encountered = s_nested_node, |
| 921 |
.user_data = &capture, |
| 922 |
}; |
| 923 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 924 |
|
| 925 |
const char expected[] = "<a/>inner"; |
| 926 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected) - 1, capture.node_body.ptr, capture.node_body.len); |
| 927 |
|
| 928 |
return AWS_OP_SUCCESS; |
| 929 |
} |
| 930 |
|
| 931 |
AWS_TEST_CASE(xml_parser_self_closing_same_name_test, s_xml_parser_self_closing_same_name_test) |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
const char *cdata_false_close_doc = "<data><![CDATA[</data>]]>real</data>"; |
| 937 |
|
| 938 |
int s_cdata_false_close_root(struct aws_xml_node *node, void *user_data) { |
| 939 |
struct nested_node_capture *capture = user_data; |
| 940 |
return aws_xml_node_as_body(node, &capture->node_body); |
| 941 |
} |
| 942 |
|
| 943 |
static int s_xml_parser_cdata_false_close_test(struct aws_allocator *allocator, void *ctx) { |
| 944 |
(void)ctx; |
| 945 |
|
| 946 |
struct nested_node_capture capture; |
| 947 |
AWS_ZERO_STRUCT(capture); |
| 948 |
|
| 949 |
struct aws_xml_parser_options options = { |
| 950 |
.doc = aws_byte_cursor_from_c_str(cdata_false_close_doc), |
| 951 |
.on_root_encountered = s_cdata_false_close_root, |
| 952 |
.user_data = &capture, |
| 953 |
}; |
| 954 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 955 |
|
| 956 |
const char expected[] = "<![CDATA[</data>]]>real"; |
| 957 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected) - 1, capture.node_body.ptr, capture.node_body.len); |
| 958 |
|
| 959 |
return AWS_OP_SUCCESS; |
| 960 |
} |
| 961 |
|
| 962 |
AWS_TEST_CASE(xml_parser_cdata_false_close_test, s_xml_parser_cdata_false_close_test) |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
const char *comment_false_open_doc = "<tag><!-- <tag> -->body</tag>"; |
| 968 |
|
| 969 |
static int s_xml_parser_comment_false_open_test(struct aws_allocator *allocator, void *ctx) { |
| 970 |
(void)ctx; |
| 971 |
|
| 972 |
struct nested_node_capture capture; |
| 973 |
AWS_ZERO_STRUCT(capture); |
| 974 |
|
| 975 |
struct aws_xml_parser_options options = { |
| 976 |
.doc = aws_byte_cursor_from_c_str(comment_false_open_doc), |
| 977 |
.on_root_encountered = s_nested_node, |
| 978 |
.user_data = &capture, |
| 979 |
}; |
| 980 |
ASSERT_SUCCESS(aws_xml_parse(allocator, &options)); |
| 981 |
|
| 982 |
const char expected[] = "<!-- <tag> -->body"; |
| 983 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected) - 1, capture.node_body.ptr, capture.node_body.len); |
| 984 |
|
| 985 |
return AWS_OP_SUCCESS; |
| 986 |
} |
| 987 |
|
| 988 |
AWS_TEST_CASE(xml_parser_comment_false_open_test, s_xml_parser_comment_false_open_test) |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
static int s_xml_parser_trailing_open_bracket_test(struct aws_allocator *allocator, void *ctx) { |
| 994 |
(void)ctx; |
| 995 |
|
| 996 |
const char *doc = "<a><"; |
| 997 |
|
| 998 |
struct nested_node_capture capture; |
| 999 |
AWS_ZERO_STRUCT(capture); |
| 1000 |
|
| 1001 |
struct aws_xml_parser_options options = { |
| 1002 |
.doc = aws_byte_cursor_from_c_str(doc), |
| 1003 |
.on_root_encountered = s_nested_node, |
| 1004 |
.user_data = &capture, |
| 1005 |
}; |
| 1006 |
ASSERT_ERROR(AWS_ERROR_INVALID_XML, aws_xml_parse(allocator, &options)); |
| 1007 |
|
| 1008 |
return AWS_OP_SUCCESS; |
| 1009 |
} |
| 1010 |
|
| 1011 |
AWS_TEST_CASE(xml_parser_trailing_open_bracket_test, s_xml_parser_trailing_open_bracket_test) |