libxml / tests /tests_HTMLparser_htmlCreateMemoryParserCtxtInternal.c
AryaWu's picture
Upload folder using huggingface_hub
6baed57 verified
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
/* 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 = "<!DOCTYPE html><html><head><title>T</title></head>"
"<body>Hello</body></html>";
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 = "<html><body>URL Test</body></html>";
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 = "<html><body>UTF-8 ✓</body></html>";
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[] = "<html><body>caf\xE9</body></html>";
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[] = "<html><body>abc\0def</body></html>";
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();
}