diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 9 | ||||
-rw-r--r-- | test/data/normalize/INDEX | 4 | ||||
-rw-r--r-- | test/data/normalize/regression.html | 13 | ||||
-rw-r--r-- | test/normalize.c | 190 |
4 files changed, 215 insertions, 1 deletions
diff --git a/test/Makefile b/test/Makefile index 951d1bd..cafa2e6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,6 +5,9 @@ TESTCFLAGS := $(TESTCFLAGS) -I$(DIR) -I$(DIR)testutils -Ibindings/xml -Ibindings ALL_XML_TESTS := +WANT_XML_TEST := $(WANT_TEST) +# WANT_XML_TEST := no + # 1: Path to XML file # 2: Fragment C file name # 3: DTD file @@ -12,7 +15,7 @@ ALL_XML_TESTS := define do_xml_test -ifeq ($$(WANT_TEST),yes) +ifeq ($$(WANT_XML_TEST),yes) $(DIR)$2: $(DIR)testcases/tests/$1 $(DIR)transform.pl $(DIR)DOMTSHandler.pm $(VQ)$(ECHO) " XFORM: $1" @@ -48,10 +51,14 @@ endef $(DIR)INDEX: test/Makefile $(VQ)$(ECHO) " INDEX: Making test index" $(Q)$(ECHO) "#test desc dir" > $@ +ifeq ($(WANT_XML_TEST),yes) $(foreach XMLTEST,$(sort $(ALL_XML_TESTS)),$(call write_index,$(XMLTEST))) +endif + $(Q)$(ECHO) "normalize Normalize nodes normalize" > $@ TEST_PREREQS := $(TEST_PREREQS) $(DIR)INDEX +DIR_TEST_ITEMS := $(DIR_TEST_ITEMS) normalize:normalize.c;$(testutils_files) # Include the level 1 core tests $(eval $(call do_xml_suite,level1/core,dom1-interfaces.xml)) # Include level 1 html tests diff --git a/test/data/normalize/INDEX b/test/data/normalize/INDEX new file mode 100644 index 0000000..ea212ae --- /dev/null +++ b/test/data/normalize/INDEX @@ -0,0 +1,4 @@ +# Index file for generic CSS content +# +# Test Description +regression.html OOMing regression check diff --git a/test/data/normalize/regression.html b/test/data/normalize/regression.html new file mode 100644 index 0000000..084ca67 --- /dev/null +++ b/test/data/normalize/regression.html @@ -0,0 +1,13 @@ +<html> + <head> + <style> + ><<>><><<>><>><<<<>><><><<>>>><<<<>><<><<>>><<><>>><>><<><<>><<>><<<<<<>>><>><><><><<>><><<<<>><>>><><><<><>>><><><><>><<<<>>>><>><><<<<<><><<<>>>>>>>>>><>>><<<<<<<<<<>>>>><><<<>>><>>>>>><>><<<<><<><>><<>><><<<>>>>>>>><<<>>>><<><<<<<><><><>><>><<<>><<<><><<<>>>>><><<>>>><>>><<<<<<<<>>><<<>>>><>>>>>><><>><><>><><>>>>><<>><<>>>><<>><><<>><><<><<>><><<<>>><<><><<<><>><<><<>><><<<<<<<<<<>><>><><<>>><> + </style> + </head> +<body> + <script> + document.head.normalize() + </script> +</body> +</html> + diff --git a/test/normalize.c b/test/normalize.c new file mode 100644 index 0000000..1c8a30d --- /dev/null +++ b/test/normalize.c @@ -0,0 +1,190 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2024 Daniel Silverstone <dsilvers@netsurf-browser.org> + */ + +#include <stdio.h> +#include <stdbool.h> + +#include "testutils/domts.h" + +#ifdef __linux__ + +#include <sys/time.h> +#include <sys/resource.h> + +#define SOFT_LIMIT (128ul * 1024ul * 1024ul) +#define HARD_LIMIT_MUL (4ul) + +static void limit_ram(void) +{ + struct rlimit lim; + + if (getrlimit(RLIMIT_DATA, &lim) == -1) { + perror("Unable to get RLIMIT_DATA"); + } else { + printf("Initial limits were: soft=%lu hard=%lu\n", + lim.rlim_cur, + lim.rlim_max); + } + + lim.rlim_cur = SOFT_LIMIT; + lim.rlim_max = lim.rlim_cur * HARD_LIMIT_MUL; + + if (setrlimit(RLIMIT_DATA, &lim) == -1) { + perror("Unable to set RLIMIT_DATA"); + } else { + printf("Set limits to: soft=%lu hard=%lu\n", + lim.rlim_cur, + lim.rlim_max); + } +} + +#else + +static void limit_ram(void) +{ + printf("Cannot limit RAM, risky execution proceeds...\n"); +} +#endif + +static dom_exception +get_node(dom_document *doc, dom_string *tag, dom_node **out) +{ + dom_nodelist *nodes; + dom_exception err; + dom_node *docele; + dom_node *ret; + + err = dom_document_get_document_element(doc, &docele); + if (err != DOM_NO_ERR) { + printf("Could not get document node\n"); + return err; + } + err = dom_element_get_elements_by_tag_name(docele, tag, &nodes); + dom_node_unref(docele); + if (err != DOM_NO_ERR) { + printf("Could not enumerate elements for %*s\n", + dom_string_length(tag), + dom_string_data(tag)); + return err; + } + + err = dom_nodelist_item(nodes, 0, &ret); + dom_nodelist_unref(nodes); + if (err != DOM_NO_ERR) { + printf("Could not retrieve element[0] for %*s\n", + dom_string_length(tag), + dom_string_data(tag)); + return err; + } + + *out = ret; + + return DOM_NO_ERR; +} + +static bool test_normalize(const char *fname) +{ + bool outcome = true; + dom_document *doc = NULL; + dom_string *domHEAD = NULL, *domBODY = NULL; + dom_node *html = NULL, *head = NULL, *body = NULL; + dom_exception err = DOM_NO_ERR; + + limit_ram(); + + printf("Loading: %s\n", fname); + doc = load_html(fname, false); + if (doc == NULL) { + printf("Failed to load file\n"); + outcome = false; + goto cleanup; + } + + /* We have an HTML document so we normalise the head and the body */ + + err = dom_string_create((uint8_t *)"HEAD", 4, &domHEAD); + if (err != DOM_NO_ERR) { + printf("Failed to create HEAD string\n"); + outcome = false; + goto cleanup; + } + + err = dom_string_create((uint8_t *)"BODY", 4, &domBODY); + if (err != DOM_NO_ERR) { + printf("Failed to create BODY string\n"); + outcome = false; + goto cleanup; + } + + err = get_node(doc, domHEAD, &head); + if (err != DOM_NO_ERR) { + outcome = false; + goto cleanup; + } + + err = get_node(doc, domBODY, &body); + if (err != DOM_NO_ERR) { + outcome = false; + goto cleanup; + } + + printf("Normalizing head, please wait...\n"); + err = dom_node_normalize(head); + if (err != DOM_NO_ERR) { + printf("Failed to normalize head\n"); + outcome = false; + goto cleanup; + } + printf("Normalizing body, please wait...\n"); + err = dom_node_normalize(body); + if (err != DOM_NO_ERR) { + printf("Failed to normalize body\n"); + outcome = false; + goto cleanup; + } + + printf("All done\n"); + +cleanup: + if (err != DOM_NO_ERR) { + printf("DOM Exception: %d\n", err); + } + if (head != NULL) { + dom_node_unref(head); + } + if (html != NULL) { + dom_node_unref(html); + } + if (domBODY != NULL) { + dom_string_unref(domBODY); + } + if (domHEAD != NULL) { + dom_string_unref(domHEAD); + } + if (doc != NULL) { + dom_node_unref(doc); + doc = NULL; + } + return outcome; +} + +int main(int argc, char **argv) +{ + if (argc != 2) { + fprintf(stderr, "usage: %s inputfile", argv[0]); + return 1; + } + + /* This test simply loads the HTML and normalises the top node */ + if (!test_normalize(argv[1])) { + printf("\nFAILED\n"); + return 1; + } + + printf("\nPASS\n"); + return 0; +}
\ No newline at end of file |