|
|
#include "unity/unity.h" |
|
|
#include <libxml/HTMLparser.h> |
|
|
|
|
|
#include <stddef.h> |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
extern htmlParserCtxtPtr test_htmlCreateMemoryParserCtxtInternal(const char *url, |
|
|
const char *buffer, size_t size, |
|
|
const char *encoding); |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
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) { |
|
|
|
|
|
const char dummy = 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) { |
|
|
|
|
|
const char latin1_html[] = "<html><body>caf\xE9</body></html>"; |
|
|
size_t size = sizeof(latin1_html) - 1; |
|
|
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) { |
|
|
|
|
|
const char html_with_nul[] = "<html><body>abc\0def</body></html>"; |
|
|
size_t size = sizeof(html_with_nul) - 1; |
|
|
|
|
|
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(); |
|
|
} |