From: Michael Tremer Date: Tue, 24 Dec 2024 13:25:41 +0000 (+0000) Subject: tests: Add simple tests for the log buffer X-Git-Tag: 0.9.30~690 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb819acd4928db4369e07c603e14b9c473082b93;p=pakfire.git tests: Add simple tests for the log buffer Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index de7a7f265..4f3fa4b6e 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ /tests/libpakfire/httpclient /tests/libpakfire/jail /tests/libpakfire/key +/tests/libpakfire/log_buffer /tests/libpakfire/main /tests/libpakfire/makefile /tests/libpakfire/os diff --git a/Makefile.am b/Makefile.am index 1ca9630d7..8b03d9ad1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -608,6 +608,7 @@ check_PROGRAMS += \ tests/libpakfire/httpclient \ tests/libpakfire/jail \ tests/libpakfire/key \ + tests/libpakfire/log_buffer \ tests/libpakfire/makefile \ tests/libpakfire/os \ tests/libpakfire/package \ @@ -829,6 +830,21 @@ tests_libpakfire_key_LDFLAGS = \ tests_libpakfire_key_LDADD = \ $(TESTSUITE_LDADD) +tests_libpakfire_log_buffer_SOURCES = \ + tests/libpakfire/log_buffer.c + +tests_libpakfire_log_buffer_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_log_buffer_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_log_buffer_LDFLAGS = \ + $(TESTSUITE_LDFLAGS) + +tests_libpakfire_log_buffer_LDADD = \ + $(TESTSUITE_LDADD) + dist_tests_libpakfire_makefile_SOURCES = \ tests/libpakfire/makefile.c diff --git a/tests/libpakfire/log_buffer.c b/tests/libpakfire/log_buffer.c new file mode 100644 index 000000000..834cbb822 --- /dev/null +++ b/tests/libpakfire/log_buffer.c @@ -0,0 +1,84 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2024 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 "../testsuite.h" + +static int test_simple(const struct test* t) { + struct pakfire_log_buffer* buffer = NULL; + char* line = NULL; + size_t length = 0; + int priority = 0; + int r = EXIT_FAILURE; + + ASSERT_SUCCESS(pakfire_log_buffer_create(&buffer, t->ctx, 0)); + + // Enqueue three strings + ASSERT_SUCCESS(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, "A", -1)); + ASSERT_SUCCESS(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, "BB", -1)); + ASSERT_SUCCESS(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, "CCC", -1)); + + // Dequeue the first string + ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length)); + + ASSERT_EQUALS(priority, LOG_DEBUG); + ASSERT_STRING_EQUALS(line, "A"); + ASSERT_EQUALS(length, 1); + free(line); + + // Dequeue the second string + ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length)); + + ASSERT_EQUALS(priority, LOG_DEBUG); + ASSERT_STRING_EQUALS(line, "BB"); + ASSERT_EQUALS(length, 2); + free(line); + + // Dequeue the third string + ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length)); + + ASSERT_EQUALS(priority, LOG_DEBUG); + ASSERT_STRING_EQUALS(line, "CCC"); + ASSERT_EQUALS(length, 3); + free(line); + + // Dequeue more than we put in + ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length)); + + ASSERT_EQUALS(priority, -1); + ASSERT_EQUALS(line, NULL); + ASSERT_EQUALS(length, -1); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (buffer) + pakfire_log_buffer_unref(buffer); + + return r; +} + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_simple, 0); + + return testsuite_run(argc, argv); +}