| | #include "unity/unity.h" |
| | #include <libxml/HTMLtree.h> |
| | #include <libxml/HTMLparser.h> |
| | #include <libxml/xmlstring.h> |
| |
|
| | #include <string.h> |
| | #include <stdlib.h> |
| |
|
| | void setUp(void) { |
| | |
| | } |
| |
|
| | void tearDown(void) { |
| | |
| | } |
| |
|
| | static xmlDocPtr make_simple_html_doc(void) { |
| | const char *html = |
| | "<!DOCTYPE html>" |
| | "<html><head><title>T</title></head>" |
| | "<body>" |
| | "<div><p>a & b</p></div>" |
| | "</body></html>"; |
| | |
| | xmlDocPtr doc = htmlReadMemory(html, (int)strlen(html), "test.html", |
| | NULL, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING); |
| | return doc; |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_null_doc_sets_outputs(void) { |
| | xmlChar *mem = (xmlChar*)1; |
| | int size = -123; |
| |
|
| | htmlDocDumpMemoryFormat(NULL, &mem, &size, 0); |
| |
|
| | TEST_ASSERT_NULL(mem); |
| | TEST_ASSERT_EQUAL_INT(0, size); |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_mem_null_does_not_touch_size(void) { |
| | xmlDocPtr doc = make_simple_html_doc(); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc"); |
| |
|
| | int size = 777; |
| | |
| | htmlDocDumpMemoryFormat(doc, NULL, &size, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(777, size); |
| |
|
| | xmlFreeDoc(doc); |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_size_null_does_not_touch_mem(void) { |
| | xmlDocPtr doc = make_simple_html_doc(); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc"); |
| |
|
| | xmlChar *mem = (xmlChar*)0xDEADBEEF; |
| | |
| | htmlDocDumpMemoryFormat(doc, &mem, NULL, 0); |
| |
|
| | TEST_ASSERT_EQUAL_PTR((void*)0xDEADBEEF, mem); |
| |
|
| | xmlFreeDoc(doc); |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_invalid_encoding_returns_null(void) { |
| | xmlDocPtr doc = make_simple_html_doc(); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc"); |
| |
|
| | |
| | if (doc->encoding != NULL) { |
| | xmlFree((void*)doc->encoding); |
| | doc->encoding = NULL; |
| | } |
| | doc->encoding = xmlCharStrdup("x-invalid-encoding-123"); |
| |
|
| | xmlChar *mem = (xmlChar*)1; |
| | int size = -42; |
| |
|
| | htmlDocDumpMemoryFormat(doc, &mem, &size, 0); |
| |
|
| | TEST_ASSERT_NULL(mem); |
| | TEST_ASSERT_EQUAL_INT(0, size); |
| |
|
| | xmlFreeDoc(doc); |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_basic_html_output_nonformatted(void) { |
| | xmlDocPtr doc = make_simple_html_doc(); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc"); |
| |
|
| | xmlChar *mem = NULL; |
| | int size = 0; |
| |
|
| | htmlDocDumpMemoryFormat(doc, &mem, &size, 0); |
| |
|
| | TEST_ASSERT_NOT_NULL(mem); |
| | TEST_ASSERT_TRUE(size > 0); |
| | |
| | TEST_ASSERT_EQUAL_HEX8(0, ((const unsigned char*)mem)[size]); |
| |
|
| | |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<html")); |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<body")); |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<p")); |
| | |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "&")); |
| |
|
| | xmlFree(mem); |
| | xmlFreeDoc(doc); |
| | } |
| |
|
| | void test_htmlDocDumpMemoryFormat_basic_html_output_formatted(void) { |
| | xmlDocPtr doc = make_simple_html_doc(); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc"); |
| |
|
| | xmlChar *mem = NULL; |
| | int size = 0; |
| |
|
| | htmlDocDumpMemoryFormat(doc, &mem, &size, 1); |
| |
|
| | TEST_ASSERT_NOT_NULL(mem); |
| | TEST_ASSERT_TRUE(size > 0); |
| | TEST_ASSERT_EQUAL_HEX8(0, ((const unsigned char*)mem)[size]); |
| |
|
| | |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<html")); |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<body")); |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<p")); |
| | TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "&")); |
| |
|
| | xmlFree(mem); |
| | xmlFreeDoc(doc); |
| | } |
| |
|
| | int main(void) { |
| | UNITY_BEGIN(); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_null_doc_sets_outputs); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_mem_null_does_not_touch_size); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_size_null_does_not_touch_mem); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_invalid_encoding_returns_null); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_basic_html_output_nonformatted); |
| | RUN_TEST(test_htmlDocDumpMemoryFormat_basic_html_output_formatted); |
| | return UNITY_END(); |
| | } |