|
|
#include "unity/unity.h" |
|
|
#include <libxml/HTMLparser.h> |
|
|
#include <libxml/parser.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
static int compute_allMask(void) { |
|
|
int allMask = 0; |
|
|
allMask |= HTML_PARSE_RECOVER; |
|
|
allMask |= HTML_PARSE_HTML5; |
|
|
allMask |= HTML_PARSE_NODEFDTD; |
|
|
allMask |= HTML_PARSE_NOERROR; |
|
|
allMask |= HTML_PARSE_NOWARNING; |
|
|
allMask |= HTML_PARSE_PEDANTIC; |
|
|
allMask |= HTML_PARSE_NOBLANKS; |
|
|
allMask |= HTML_PARSE_NONET; |
|
|
allMask |= HTML_PARSE_NOIMPLIED; |
|
|
allMask |= HTML_PARSE_COMPACT; |
|
|
allMask |= HTML_PARSE_HUGE; |
|
|
allMask |= HTML_PARSE_IGNORE_ENC; |
|
|
allMask |= HTML_PARSE_BIG_LINES; |
|
|
return allMask; |
|
|
} |
|
|
|
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void test_htmlCtxtUseOptions_null_context_returns_minus1(void) { |
|
|
int ret = htmlCtxtUseOptions(NULL, 0); |
|
|
TEST_ASSERT_EQUAL_INT(-1, ret); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_noblanks_sets_fields_and_sax_callback(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->sax); |
|
|
|
|
|
int ret = htmlCtxtUseOptions(ctxt, HTML_PARSE_NOBLANKS); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NOBLANKS) != 0); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, ctxt->keepBlanks); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, ctxt->recovery); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, ctxt->dictNames); |
|
|
|
|
|
TEST_ASSERT_NOT_NULL(ctxt->sax->ignorableWhitespace); |
|
|
TEST_ASSERT(ctxt->sax->ignorableWhitespace == xmlSAX2IgnorableWhitespace); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_zero_options_sets_keepBlanks_1_and_no_noblanks_flag(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
|
|
|
int ret = htmlCtxtUseOptions(ctxt, 0); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, ctxt->options); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, ctxt->keepBlanks); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, ctxt->recovery); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, ctxt->dictNames); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_preserves_keepMask_bits_from_previous_options(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
|
|
|
|
|
|
ctxt->options = HTML_PARSE_NOERROR | |
|
|
HTML_PARSE_NOIMPLIED | |
|
|
HTML_PARSE_COMPACT | |
|
|
HTML_PARSE_HUGE | |
|
|
HTML_PARSE_IGNORE_ENC | |
|
|
HTML_PARSE_BIG_LINES; |
|
|
|
|
|
int ret = htmlCtxtUseOptions(ctxt, 0); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NOERROR) != 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NOIMPLIED) != 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_COMPACT) != 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_HUGE) != 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_IGNORE_ENC) != 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_BIG_LINES) != 0); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_clears_non_keep_bits_from_previous_options(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
|
|
|
|
|
|
ctxt->options = HTML_PARSE_RECOVER | |
|
|
HTML_PARSE_PEDANTIC | |
|
|
HTML_PARSE_NONET | |
|
|
HTML_PARSE_HTML5 | |
|
|
HTML_PARSE_NOBLANKS | |
|
|
HTML_PARSE_NODEFDTD; |
|
|
|
|
|
int ret = htmlCtxtUseOptions(ctxt, 0); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_RECOVER) == 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_PEDANTIC) == 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NONET) == 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_HTML5) == 0); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NOBLANKS) == 0); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_NODEFDTD) != 0); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_return_value_for_noent_and_unknown_bits(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
|
|
|
int allMask = compute_allMask(); |
|
|
|
|
|
|
|
|
int ret_noent_only = htmlCtxtUseOptions(ctxt, XML_PARSE_NOENT); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret_noent_only); |
|
|
|
|
|
|
|
|
int ret_all_known_plus_noent = htmlCtxtUseOptions(ctxt, allMask | XML_PARSE_NOENT); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret_all_known_plus_noent); |
|
|
|
|
|
|
|
|
unsigned int unknown = 0; |
|
|
unsigned int combined_known = (unsigned int)allMask | (unsigned int)XML_PARSE_NOENT; |
|
|
for (unsigned int i = 0; i < sizeof(unsigned int) * 8; i++) { |
|
|
unsigned int bit = 1U << i; |
|
|
if ((combined_known & bit) == 0U) { |
|
|
unknown = bit; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if (unknown != 0U) { |
|
|
int ret_unknown = htmlCtxtUseOptions(ctxt, (int)unknown); |
|
|
TEST_ASSERT_EQUAL_INT((int)unknown, ret_unknown); |
|
|
|
|
|
TEST_ASSERT_TRUE(((unsigned int)ctxt->options & unknown) == 0U); |
|
|
} else { |
|
|
|
|
|
TEST_MESSAGE("No unknown option bit available to test passthrough return; skipping that sub-check."); |
|
|
} |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
void test_htmlCtxtUseOptions_huge_with_null_dict_is_safe(void) { |
|
|
htmlParserCtxtPtr ctxt = htmlNewParserCtxt(); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
|
|
|
|
|
|
xmlDictPtr saved = ctxt->dict; |
|
|
ctxt->dict = NULL; |
|
|
|
|
|
int ret = htmlCtxtUseOptions(ctxt, HTML_PARSE_HUGE); |
|
|
TEST_ASSERT_EQUAL_INT(0, ret); |
|
|
TEST_ASSERT_TRUE((ctxt->options & HTML_PARSE_HUGE) != 0); |
|
|
|
|
|
|
|
|
ctxt->dict = saved; |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_htmlCtxtUseOptions_null_context_returns_minus1); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_noblanks_sets_fields_and_sax_callback); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_zero_options_sets_keepBlanks_1_and_no_noblanks_flag); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_preserves_keepMask_bits_from_previous_options); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_clears_non_keep_bits_from_previous_options); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_return_value_for_noent_and_unknown_bits); |
|
|
RUN_TEST(test_htmlCtxtUseOptions_huge_with_null_dict_is_safe); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |