From f82a3a70222a38a5b6125ed2ddf3988761a1312d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 30 Nov 2022 13:55:14 +0000 Subject: [PATCH] test: Add a simple test program for the parser Signed-off-by: Michael Tremer --- .gitignore | 1 + Makefile.am | 25 +++++++++++ tests/parser/test.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 tests/parser/test.c diff --git a/.gitignore b/.gitignore index 2444915b1..19423e405 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ /tests/libpakfire/snapshot /tests/libpakfire/string /tests/libpakfire/util +/tests/parser/test /tests/stub/root /tmp *.py[co] diff --git a/Makefile.am b/Makefile.am index d672c652d..af977f8cf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -693,6 +693,31 @@ TEST_STUB_ROOT = $(abs_top_builddir)/tests/stub/root # ------------------------------------------------------------------------------ +tests_parserdir = $(testsdir)/parser + +tests_parser_PROGRAMS = \ + tests/parser/test + +dist_tests_parser_test_SOURCES = \ + tests/parser/test.c + +tests_parser_test_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + $(PAKFIRE_CPPFLAGS) \ + -DPAKFIRE_PRIVATE + +tests_parser_test_CFLAGS = \ + $(JSON_C_CFLAGS) + +tests_parser_test_LDFLAGS = \ + $(AM_LDFLAGS) + +tests_parser_test_LDADD = \ + libpakfire.la \ + libpakfire-internal.la + +# ------------------------------------------------------------------------------ + dist_scripts_SCRIPTS = \ src/scripts/check-buildroot \ src/scripts/check-fhs \ diff --git a/tests/parser/test.c b/tests/parser/test.c new file mode 100644 index 000000000..9e83f30c3 --- /dev/null +++ b/tests/parser/test.c @@ -0,0 +1,103 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2022 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include +#include +#include +#include + +int main(int argc, const char* argv[]) { + struct pakfire* pakfire = NULL; + struct pakfire_parser* parser = NULL; + struct pakfire_parser_error* error = NULL; + const char* message = NULL; + FILE* f = NULL; + int r = 1; + + char root[PATH_MAX] = "/tmp/pakfire-parser-test.XXXXXX"; + + // Create test root directory + char* tmp = pakfire_mkdtemp(root); + if (!tmp) { + fprintf(stderr, "Could not create temporary directory %s: %m\n", root); + goto ERROR; + } + + // Create a pakfire instance + r = pakfire_create(&pakfire, root, NULL, NULL, + 0, LOG_DEBUG, pakfire_log_stderr, NULL); + if (r) { + fprintf(stderr, "Could not create Pakfire: %m\n"); + goto ERROR; + } + + // Create a new parser + parser = pakfire_parser_create(pakfire, NULL, NULL, 0); + if (!parser) { + fprintf(stderr, "Could not create a parser: %m\n"); + goto ERROR; + } + + // Try parsing all files that have been passed on the command line + for (unsigned int i = 1; i < argc; i++) { + printf("Parsing %s...\n", argv[i]); + + // Try opening the file + f = fopen(argv[i], "r"); + if (!f) { + fprintf(stderr, "Could not open %s: %m\n", argv[i]); + r = 1; + goto ERROR; + } + + // Try parsing the file + r = pakfire_parser_read(parser, f, &error); + if (r) { + if (error) { + message = pakfire_parser_error_get_message(error); + fprintf(stderr, "PARSER ERROR: %s\n", message); + + fclose(f); + goto ERROR; + } + } + + fclose(f); + } + + // Dump the entire parser + char* dump = pakfire_parser_dump(parser); + if (dump) { + printf("%s\n", dump); + free(dump); + } + +ERROR: + if (error) + pakfire_parser_error_unref(error); + if (parser) + pakfire_parser_unref(parser); + if (pakfire) + pakfire_unref(pakfire); + + return r; +} -- 2.39.5