From: Michael Tremer Date: Tue, 28 Jan 2025 17:50:14 +0000 (+0000) Subject: tests: Add some basic tests for the log stream X-Git-Tag: 0.9.30~310 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=70ff49b448704d2f6550ee6ce33ea47da6494ede;p=pakfire.git tests: Add some basic tests for the log stream Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 701d1086..0954dac2 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ /tests/libpakfire/jail /tests/libpakfire/key /tests/libpakfire/log_buffer +/tests/libpakfire/log_stream /tests/libpakfire/main /tests/libpakfire/makefile /tests/libpakfire/os diff --git a/Makefile.am b/Makefile.am index a9b34d9e..9db5b7c5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -573,6 +573,7 @@ check_PROGRAMS += \ tests/libpakfire/jail \ tests/libpakfire/key \ tests/libpakfire/log_buffer \ + tests/libpakfire/log_stream \ tests/libpakfire/makefile \ tests/libpakfire/os \ tests/libpakfire/package \ @@ -824,6 +825,21 @@ tests_libpakfire_log_buffer_LDFLAGS = \ tests_libpakfire_log_buffer_LDADD = \ $(TESTSUITE_LDADD) +tests_libpakfire_log_stream_SOURCES = \ + tests/libpakfire/log_stream.c + +tests_libpakfire_log_stream_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_log_stream_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_log_stream_LDFLAGS = \ + $(TESTSUITE_LDFLAGS) + +tests_libpakfire_log_stream_LDADD = \ + $(TESTSUITE_LDADD) + dist_tests_libpakfire_makefile_SOURCES = \ tests/libpakfire/makefile.c diff --git a/tests/libpakfire/log_stream.c b/tests/libpakfire/log_stream.c new file mode 100644 index 00000000..38a804de --- /dev/null +++ b/tests/libpakfire/log_stream.c @@ -0,0 +1,118 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 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 "../testsuite.h" + +static int log_callback(struct pakfire_log_stream* stream, + const char* line, size_t length, void* data) { + int* lines_read = data; + + // Increment the counter + if (lines_read) + (*lines_read)++; + + printf("LINE = %.*s", (int)length, line); + + return 0; +} + +static int test_simple(const struct test* t) { + struct pakfire_log_stream* stream = NULL; + int r = EXIT_FAILURE; + + ASSERT_SUCCESS(pakfire_log_stream_create(&stream, t->ctx, NULL, NULL)); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (stream) + pakfire_log_stream_unref(stream); + + return r; +} + +static int test_fork(const struct test* t) { + struct pakfire_log_stream* stream = NULL; + sd_event* loop = NULL; + int r = EXIT_FAILURE; + + // How many lines to write + const int lines_to_write = 1000; + int lines_read = 0; + + // Fetch the default event loop + ASSERT(sd_event_default(&loop) >= 0); + + ASSERT_SUCCESS(pakfire_log_stream_create(&stream, t->ctx, log_callback, &lines_read)); + + // Fork the process + pid_t pid = fork(); + if (pid < 0) + abort(); + + // Child Code + else if (pid == 0) { + ASSERT_SUCCESS(pakfire_log_stream_in_child(stream)); + + // Write a message into the log stream + for (int i = 0; i < lines_to_write; i++) + pakfire_log_stream_printf(stream, "LINE %d\n", i); + + // End here + _exit(0); + + // Parent Code + } else { + ASSERT_SUCCESS(pakfire_log_stream_in_parent(stream, loop)); + + // Run the event loop + for (;;) { + r = sd_event_run(loop, S_TO_US(0.1)); + if (r == 0) + break; + } + } + + // Check if we have received the entire input + ASSERT_EQUALS(lines_to_write, lines_read); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (stream) + pakfire_log_stream_unref(stream); + if (loop) + sd_event_unref(loop); + + return r; +} + + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_simple, 0); + testsuite_add_test(test_fork, 0); + + return testsuite_run(argc, argv); +}