tests: Look for single-char match in test_abort_epilog

...instead of a full-string match.

These tests were depending on getting handler callbacks with exactly
one character of data at a time. For example, if test_abort_epilog got
"\n\r\n" in one callback, it would fail to match on the '\r', and would
not abort parsing as expected.

By searching the callback arg for the magic character rather than
expecting a full match, the test no longer depends on exact callback
timing.

`userData` is never NULL in these tests, so that check was left out of
the new version.
This commit is contained in:
Snild Dolkow 2023-08-31 14:35:48 +02:00
parent 7474fe3d3f
commit 4978d285d2
2 changed files with 16 additions and 9 deletions

View file

@ -3474,10 +3474,10 @@ END_TEST
/* Test aborting the parse in an epilog works */
START_TEST(test_abort_epilog) {
const char *text = "<doc></doc>\n\r\n";
XML_Char match[] = XCS("\r");
XML_Char trigger_char = XCS('\r');
XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
XML_SetUserData(g_parser, match);
XML_SetUserData(g_parser, &trigger_char);
g_resumable = XML_FALSE;
if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
!= XML_STATUS_ERROR)
@ -3490,10 +3490,10 @@ END_TEST
/* Test a different code path for abort in the epilog */
START_TEST(test_abort_epilog_2) {
const char *text = "<doc></doc>\n";
XML_Char match[] = XCS("\n");
XML_Char trigger_char = XCS('\n');
XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
XML_SetUserData(g_parser, match);
XML_SetUserData(g_parser, &trigger_char);
g_resumable = XML_FALSE;
expect_failure(text, XML_ERROR_ABORTED, "Abort not triggered");
}
@ -3502,10 +3502,10 @@ END_TEST
/* Test suspension from the epilog */
START_TEST(test_suspend_epilog) {
const char *text = "<doc></doc>\n";
XML_Char match[] = XCS("\n");
XML_Char trigger_char = XCS('\n');
XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
XML_SetUserData(g_parser, match);
XML_SetUserData(g_parser, &trigger_char);
g_resumable = XML_TRUE;
if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
!= XML_STATUS_SUSPENDED)

View file

@ -1793,10 +1793,17 @@ data_check_comment_handler(void *userData, const XML_Char *data) {
void XMLCALL
selective_aborting_default_handler(void *userData, const XML_Char *s, int len) {
const XML_Char *match = (const XML_Char *)userData;
const XML_Char trigger_char = *(const XML_Char *)userData;
if (match == NULL
|| (xcstrlen(match) == (unsigned)len && ! xcstrncmp(match, s, len))) {
int found = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == trigger_char) {
found = 1;
break;
}
}
if (found) {
XML_StopParser(g_parser, g_resumable);
XML_SetDefaultHandler(g_parser, NULL);
}