From: Michael Tremer Date: Sun, 29 Dec 2024 15:42:11 +0000 (+0000) Subject: tests: Add tests for env X-Git-Tag: 0.9.30~676 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=267717d19956f69f516832c24e99880e1c69d9cb;p=pakfire.git tests: Add tests for env Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 4f3fa4b6e..701d10868 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ /tests/libpakfire/db /tests/libpakfire/deps /tests/libpakfire/digest +/tests/libpakfire/env /tests/libpakfire/file /tests/libpakfire/httpclient /tests/libpakfire/jail diff --git a/Makefile.am b/Makefile.am index 78f100844..7bc64b650 100644 --- a/Makefile.am +++ b/Makefile.am @@ -608,6 +608,7 @@ check_PROGRAMS += \ tests/libpakfire/db \ tests/libpakfire/deps \ tests/libpakfire/digest \ + tests/libpakfire/env \ tests/libpakfire/file \ tests/libpakfire/httpclient \ tests/libpakfire/jail \ @@ -774,6 +775,21 @@ tests_libpakfire_digest_LDFLAGS = \ tests_libpakfire_digest_LDADD = \ $(TESTSUITE_LDADD) +dist_tests_libpakfire_env_SOURCES = \ + tests/libpakfire/env.c + +tests_libpakfire_env_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_env_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_env_LDFLAGS = \ + $(TESTSUITE_LDFLAGS) + +tests_libpakfire_env_LDADD = \ + $(TESTSUITE_LDADD) + dist_tests_libpakfire_file_SOURCES = \ tests/libpakfire/file.c diff --git a/tests/libpakfire/env.c b/tests/libpakfire/env.c new file mode 100644 index 000000000..9544ec8ef --- /dev/null +++ b/tests/libpakfire/env.c @@ -0,0 +1,97 @@ +/*############################################################################# +# # +# 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_set_get(const struct test* t) { + struct pakfire_env* env = NULL; + int r = EXIT_FAILURE; + + // Create the environment + ASSERT_SUCCESS(pakfire_env_create(&env, t->ctx)); + + // Add something + ASSERT_SUCCESS(pakfire_env_set(env, "A", "1")); + ASSERT_SUCCESS(pakfire_env_set(env, "B", "2")); + + // Fetch the values + ASSERT_STRING_EQUALS(pakfire_env_get(env, "A"), "1"); + ASSERT_STRING_EQUALS(pakfire_env_get(env, "B"), "2"); + ASSERT_NULL(pakfire_env_get(env, "C")); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (env) + pakfire_env_unref(env); + + return r; +} + +static int test_merge(const struct test* t) { + struct pakfire_env* env1 = NULL; + struct pakfire_env* env2 = NULL; + int r = EXIT_FAILURE; + + // Create the environments + ASSERT_SUCCESS(pakfire_env_create(&env1, t->ctx)); + ASSERT_SUCCESS(pakfire_env_create(&env2, t->ctx)); + + // Add something to the first environment + ASSERT_SUCCESS(pakfire_env_set(env1, "A", "1")); + + // Add something to the second environment + ASSERT_SUCCESS(pakfire_env_set(env2, "B", "2")); + + // Check that we have set everything correctly + ASSERT_STRING_EQUALS(pakfire_env_get(env1, "A"), "1"); + ASSERT_NULL(pakfire_env_get(env1, "B")); + + ASSERT_NULL(pakfire_env_get(env2, "A")); + ASSERT_STRING_EQUALS(pakfire_env_get(env2, "B"), "2"); + + // Merge + ASSERT_SUCCESS(pakfire_env_merge(env1, env2)); + + // env1 should now have B set, too + ASSERT_STRING_EQUALS(pakfire_env_get(env1, "A"), "1"); + ASSERT_STRING_EQUALS(pakfire_env_get(env1, "B"), "2"); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (env1) + pakfire_env_unref(env1); + if (env2) + pakfire_env_unref(env2); + + return r; +} + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_set_get, 0); + testsuite_add_test(test_merge, 0); + + return testsuite_run(argc, argv); +}