Hello
*/ static xmlDocPtr build_simple_html_doc(void) { xmlDocPtr doc = htmlNewDocNoDtD(NULL, NULL); if (doc == NULL) return NULL; xmlNodePtr root = htmlNewDocNode(doc, NULL, BAD_CAST "html", NULL); if (root == NULL) { xmlFreeDoc(doc); return NULL; } xmlDocSetRootElement(doc, root); xmlNodePtr body = htmlNewDocNode(doc, NULL, BAD_CAST "body", NULL); if (body == NULL) { xmlFreeDoc(doc); return NULL; } xmlAddChild(root, body); xmlNodePtr p = htmlNewDocNode(doc, NULL, BAD_CAST "p", NULL); if (p == NULL) { xmlFreeDoc(doc); return NULL; } xmlAddChild(body, p); xmlNodePtr text = xmlNewText(BAD_CAST "Hello"); if (text == NULL) { xmlFreeDoc(doc); return NULL; } xmlAddChild(p, text); return doc; } /* Test: Passing NULL document should return -1 and not close the file. */ static void test_htmlDocDump_null_doc_returns_minus1(void) { char path[256]; FILE *f = NULL; TEST_ASSERT_TRUE_MESSAGE(create_temp_file(path, sizeof(path), &f), "Failed to create temp file"); int ret = htmlDocDump(f, NULL); TEST_ASSERT_EQUAL_INT_MESSAGE(-1, ret, "htmlDocDump should return -1 for NULL doc"); /* Since the function returned early, it should not have closed the FILE*. We close it now. */ TEST_ASSERT_EQUAL_INT_MESSAGE(0, fflush(f), "Flushing file failed"); TEST_ASSERT_EQUAL_INT_MESSAGE(0, fclose(f), "Closing file failed"); /* File should be empty (no output) */ long size = 0; char *contents = read_file_to_string(path, &size); TEST_ASSERT_NOT_NULL_MESSAGE(contents, "Failed to read temp file"); TEST_ASSERT_EQUAL_INT_MESSAGE(0, (int)size, "File should be empty when doc is NULL"); free(contents); remove(path); } /* Test: Passing NULL file should return -1. */ static void test_htmlDocDump_null_file_returns_minus1(void) { xmlDocPtr doc = build_simple_html_doc(); TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to build HTML doc"); int ret = htmlDocDump(NULL, doc); TEST_ASSERT_EQUAL_INT_MESSAGE(-1, ret, "htmlDocDump should return -1 for NULL file"); xmlFreeDoc(doc); } /* Test: Unsupported encoding should cause htmlDocDump to return -1 and not write anything. */ static void test_htmlDocDump_invalid_encoding_returns_minus1_and_no_write(void) { xmlDocPtr doc = build_simple_html_doc(); TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to build HTML doc"); /* Set an encoding that is very likely unsupported */ doc->encoding = xmlStrdup(BAD_CAST "x-unknown-encoding-xyz-1234"); char path[256]; FILE *f = NULL; TEST_ASSERT_TRUE_MESSAGE(create_temp_file(path, sizeof(path), &f), "Failed to create temp file"); int ret = htmlDocDump(f, doc); TEST_ASSERT_EQUAL_INT_MESSAGE(-1, ret, "htmlDocDump should return -1 for unsupported encoding"); /* The function should have returned before creating/using the output buffer, so FILE* is still open */ TEST_ASSERT_EQUAL_INT_MESSAGE(0, fflush(f), "Flushing file failed"); TEST_ASSERT_EQUAL_INT_MESSAGE(0, fclose(f), "Closing file failed"); long size = -1; char *contents = read_file_to_string(path, &size); TEST_ASSERT_NOT_NULL_MESSAGE(contents, "Failed to read temp file"); TEST_ASSERT_EQUAL_INT_MESSAGE(0, (int)size, "File should be empty on encoder failure"); free(contents); remove(path); xmlFreeDoc(doc); /* Frees doc->encoding as well */ } /* Test: Successful dump with default (NULL) encoding should write content and return >= 0. */ static void test_htmlDocDump_writes_content_with_default_encoding(void) { xmlDocPtr doc = build_simple_html_doc(); TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to build HTML doc"); /* Ensure default (NULL) encoding to trigger fallback to "HTML" */ doc->encoding = NULL; char path[256]; FILE *f = NULL; TEST_ASSERT_TRUE_MESSAGE(create_temp_file(path, sizeof(path), &f), "Failed to create temp file"); int ret = htmlDocDump(f, doc); TEST_ASSERT_MESSAGE(ret >= 0, "htmlDocDump should succeed (ret >= 0) with default encoding"); /* On success, htmlDocDump likely closed the FILE*, so don't fclose(f) here. */ long size = -1; char *contents = read_file_to_string(path, &size); TEST_ASSERT_NOT_NULL_MESSAGE(contents, "Failed to read dumped file"); TEST_ASSERT_MESSAGE(size > 0, "Dumped file should contain data"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(contents, "Hello"), "Dumped HTML should contain text 'Hello'"); /* Also check that tag appears */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(contents, " element"); free(contents); remove(path); xmlFreeDoc(doc); } /* Test: Successful dump with explicit UTF-8 encoding should write content and return >= 0. */ static void test_htmlDocDump_writes_content_with_utf8_encoding(void) { xmlDocPtr doc = build_simple_html_doc(); TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to build HTML doc"); /* Set UTF-8 encoding explicitly */ doc->encoding = xmlStrdup(BAD_CAST "UTF-8"); char path[256]; FILE *f = NULL; TEST_ASSERT_TRUE_MESSAGE(create_temp_file(path, sizeof(path), &f), "Failed to create temp file"); int ret = htmlDocDump(f, doc); TEST_ASSERT_MESSAGE(ret >= 0, "htmlDocDump should succeed (ret >= 0) with UTF-8 encoding"); long size = -1; char *contents = read_file_to_string(path, &size); TEST_ASSERT_NOT_NULL_MESSAGE(contents, "Failed to read dumped file"); TEST_ASSERT_MESSAGE(size > 0, "Dumped file should contain data"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(contents, "Hello"), "Dumped HTML should contain text 'Hello'"); free(contents); remove(path); xmlFreeDoc(doc); } /* Main runner */ int main(void) { UNITY_BEGIN(); RUN_TEST(test_htmlDocDump_null_doc_returns_minus1); RUN_TEST(test_htmlDocDump_null_file_returns_minus1); RUN_TEST(test_htmlDocDump_invalid_encoding_returns_minus1_and_no_write); RUN_TEST(test_htmlDocDump_writes_content_with_default_encoding); RUN_TEST(test_htmlDocDump_writes_content_with_utf8_encoding); return UNITY_END(); }