]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
tests: Add simple tests for the log buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Dec 2024 13:25:41 +0000 (13:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Dec 2024 13:25:41 +0000 (13:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/libpakfire/log_buffer.c [new file with mode: 0644]

index de7a7f265d1347d925e562d2ed4ad13ffe560251..4f3fa4b6efebab8ebbfc7e04aa5bb0face852420 100644 (file)
@@ -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
index 1ca9630d70e497d674d2f971a1eaa29e0981ab28..8b03d9ad1460dfdb9771d9f4aa6bcade190bea52 100644 (file)
@@ -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 (file)
index 0000000..834cbb8
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/log_buffer.h>
+
+#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);
+}