Browse Source

support/testing: new runtime test for gumbo-parser

The new test requires a br2-external directory because we compile a
small test program on the host and install it on the target, but it's
not useful to have it in the main Buildroot package tree.

The test program loads and parses a sample HTML document. Taking
inspiration from 'examples/get_title.c' in gumbo-parser, it also
searches for the title of the document just to check that we can do
more than the parsing.

Signed-off-by: Raphaël Mélotte <raphael.melotte@mind.be>
Signed-off-by: Julien Olivain <ju.o@free.fr>
(cherry picked from commit da23be6338c1e82f39475d166407f97511e0b666)
Signed-off-by: Thomas Perale <thomas.perale@mind.be>
Raphaël Mélotte 1 month ago
parent
commit
f980395994

+ 1 - 0
support/testing/tests/package/br2-external/gumbo-parser/Config.in

@@ -0,0 +1 @@
+source "$BR2_EXTERNAL_GUMBO_PARSER_PATH/package/gumbo-parser-test/Config.in"

+ 1 - 0
support/testing/tests/package/br2-external/gumbo-parser/external.desc

@@ -0,0 +1 @@
+name: GUMBO_PARSER

+ 1 - 0
support/testing/tests/package/br2-external/gumbo-parser/external.mk

@@ -0,0 +1 @@
+include $(sort $(wildcard $(BR2_EXTERNAL_GUMBO_PARSER_PATH)/package/*/*.mk))

+ 7 - 0
support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/Config.in

@@ -0,0 +1,7 @@
+config BR2_PACKAGE_GUMBO_PARSER_TEST
+	bool "gumbo-parser-test"
+	depends on BR2_PACKAGE_GUMBO_PARSER
+	help
+	  Test program for gumbo-parser library.
+	  This package builds a simple test program that demonstrates
+	  basic HTML parsing functionality using gumbo-parser.

+ 19 - 0
support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo-parser-test.mk

@@ -0,0 +1,19 @@
+################################################################################
+#
+# gumbo-parser-test
+#
+################################################################################
+
+GUMBO_PARSER_TEST_DEPENDENCIES = gumbo-parser
+
+define GUMBO_PARSER_TEST_BUILD_CMDS
+	$(TARGET_CC) $(TARGET_CFLAGS) -o $(@D)/gumbo_test \
+		$(GUMBO_PARSER_TEST_PKGDIR)/gumbo_test.c \
+		$(TARGET_LDFLAGS) -lgumbo
+endef
+
+define GUMBO_PARSER_TEST_INSTALL_TARGET_CMDS
+	$(INSTALL) -D -m 0755 $(@D)/gumbo_test $(TARGET_DIR)/usr/bin/gumbo_test
+endef
+
+$(eval $(generic-package))

+ 40 - 0
support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo_test.c

@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <gumbo.h>
+
+static void search_for_title(GumboNode* node) {
+    if (node->type != GUMBO_NODE_ELEMENT) {
+        return;
+    }
+
+    if (node->v.element.tag == GUMBO_TAG_TITLE) {
+        GumboNode* text = node->v.element.children.data[0];
+        if (text->type == GUMBO_NODE_TEXT) {
+            printf("Found title: %s\n", text->v.text.text);
+        }
+        return;
+    }
+
+    GumboVector* children = &node->v.element.children;
+    for (unsigned int i = 0; i < children->length; ++i) {
+        search_for_title(children->data[i]);
+    }
+}
+
+int main() {
+    const char* html = "<html><head><title>Test HTML</title></head><body><p>Hello World</p></body></html>";
+
+    GumboOutput* output = gumbo_parse(html);
+
+    if (output == NULL) {
+        printf("HTML parsing failed\n");
+        return 1;
+    }
+
+    printf("HTML parsing successful\n");
+    search_for_title(output->root);
+
+    gumbo_destroy_output(&kGumboDefaultOptions, output);
+    return 0;
+}

+ 27 - 0
support/testing/tests/package/test_gumbo_parser.py

@@ -0,0 +1,27 @@
+import os
+
+import infra.basetest
+
+
+class TestGumboParser(infra.basetest.BRTest):
+    br2_external = [infra.filepath("tests/package/br2-external/gumbo-parser")]
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_PACKAGE_GUMBO_PARSER=y
+        BR2_PACKAGE_GUMBO_PARSER_TEST=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def test_run(self):
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=["-initrd", cpio_file])
+        self.emulator.login()
+
+        # Run the test program and check output
+        out, ret = self.emulator.run("/usr/bin/gumbo_test")
+        self.assertEqual(ret, 0)
+        self.assertIn("HTML parsing successful", "\n".join(out))
+        self.assertIn("Found title: Test HTML", "\n".join(out))