#include "unity/unity.h" #include #include #include #include /* The module provides a global test wrapper for the static function. */ extern htmlParserCtxtPtr test_htmlCreateMemoryParserCtxtInternal(const char *url, const char *buffer, size_t size, const char *encoding); void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } static void free_ctxt_if_not_null(htmlParserCtxtPtr ctxt) { if (ctxt != NULL) { htmlFreeParserCtxt(ctxt); } } void test_htmlCreateMemoryParserCtxtInternal_returns_NULL_on_null_buffer(void) { htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, NULL, 0, NULL); TEST_ASSERT_NULL(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_creates_context_with_basic_html(void) { const char *buf = "T" "Hello"; size_t size = strlen(buf); htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, buf, size, NULL); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_accepts_zero_length_buffer(void) { /* Non-NULL pointer but zero size should still create a context for empty input. */ const char dummy = 0; /* Any addressable byte is fine; size is 0. */ htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, &dummy, 0, NULL); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_accepts_non_null_url(void) { const char *url = "http://example.com/index.html"; const char *buf = "URL Test"; size_t size = strlen(buf); htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(url, buf, size, NULL); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_accepts_explicit_utf8_encoding(void) { const char *buf = "UTF-8 ✓"; size_t size = strlen(buf); const char *enc = "UTF-8"; htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, buf, size, enc); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_accepts_iso_8859_1_encoding(void) { /* Buffer contains 0xE9 (é in ISO-8859-1). */ const char latin1_html[] = "caf\xE9"; size_t size = sizeof(latin1_html) - 1; /* exclude terminating NUL */ const char *enc = "ISO-8859-1"; htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, latin1_html, size, enc); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } void test_htmlCreateMemoryParserCtxtInternal_handles_embedded_nul_bytes(void) { /* Explicit size includes an embedded NUL in the buffer. */ const char html_with_nul[] = "abc\0def"; size_t size = sizeof(html_with_nul) - 1; /* include the embedded NUL, exclude trailing terminator */ htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, html_with_nul, size, NULL); TEST_ASSERT_NOT_NULL(ctxt); free_ctxt_if_not_null(ctxt); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_returns_NULL_on_null_buffer); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_creates_context_with_basic_html); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_zero_length_buffer); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_non_null_url); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_explicit_utf8_encoding); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_iso_8859_1_encoding); RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_handles_embedded_nul_bytes); return UNITY_END(); }