diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:53:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:53:20 +0000 |
commit | e5a812082ae033afb1eed82c0f2df3d0f6bdc93f (patch) | |
tree | a6716c9275b4b413f6c9194798b34b91affb3cc7 /lib/common/tests/acl | |
parent | Initial commit. (diff) | |
download | pacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.tar.xz pacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.zip |
Adding upstream version 2.1.6.upstream/2.1.6
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/common/tests/acl')
-rw-r--r-- | lib/common/tests/acl/Makefile.am | 21 | ||||
-rw-r--r-- | lib/common/tests/acl/pcmk__is_user_in_group_test.c | 38 | ||||
-rw-r--r-- | lib/common/tests/acl/pcmk_acl_required_test.c | 26 | ||||
-rw-r--r-- | lib/common/tests/acl/xml_acl_denied_test.c | 61 | ||||
-rw-r--r-- | lib/common/tests/acl/xml_acl_enabled_test.c | 61 |
5 files changed, 207 insertions, 0 deletions
diff --git a/lib/common/tests/acl/Makefile.am b/lib/common/tests/acl/Makefile.am new file mode 100644 index 0000000..50408f9 --- /dev/null +++ b/lib/common/tests/acl/Makefile.am @@ -0,0 +1,21 @@ +# +# Copyright 2021-2022 the Pacemaker project contributors +# +# The version control history for this file may have further details. +# +# This source code is licensed under the GNU General Public License version 2 +# or later (GPLv2+) WITHOUT ANY WARRANTY. +# + +include $(top_srcdir)/mk/tap.mk +include $(top_srcdir)/mk/unittest.mk + +# Add "_test" to the end of all test program names to simplify .gitignore. + +check_PROGRAMS = \ + pcmk__is_user_in_group_test \ + pcmk_acl_required_test \ + xml_acl_denied_test \ + xml_acl_enabled_test + +TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/acl/pcmk__is_user_in_group_test.c b/lib/common/tests/acl/pcmk__is_user_in_group_test.c new file mode 100644 index 0000000..917d92e --- /dev/null +++ b/lib/common/tests/acl/pcmk__is_user_in_group_test.c @@ -0,0 +1,38 @@ +/* + * Copyright 2020-2022 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/acl.h> + +#include "../../crmcommon_private.h" +#include "mock_private.h" + +static void +is_pcmk__is_user_in_group(void **state) +{ + pcmk__mock_grent = true; + + // null user + assert_false(pcmk__is_user_in_group(NULL, "grp0")); + // null group + assert_false(pcmk__is_user_in_group("user0", NULL)); + // nonexistent group + assert_false(pcmk__is_user_in_group("user0", "nonexistent_group")); + // user is in group + assert_true(pcmk__is_user_in_group("user0", "grp0")); + // user is not in group + assert_false(pcmk__is_user_in_group("user2", "grp0")); + + pcmk__mock_grent = false; +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(is_pcmk__is_user_in_group)) diff --git a/lib/common/tests/acl/pcmk_acl_required_test.c b/lib/common/tests/acl/pcmk_acl_required_test.c new file mode 100644 index 0000000..bd5b922 --- /dev/null +++ b/lib/common/tests/acl/pcmk_acl_required_test.c @@ -0,0 +1,26 @@ +/* + * Copyright 2020-2021 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/acl.h> + +static void +is_pcmk_acl_required(void **state) +{ + assert_false(pcmk_acl_required(NULL)); + assert_false(pcmk_acl_required("")); + assert_true(pcmk_acl_required("123")); + assert_false(pcmk_acl_required(CRM_DAEMON_USER)); + assert_false(pcmk_acl_required("root")); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(is_pcmk_acl_required)) diff --git a/lib/common/tests/acl/xml_acl_denied_test.c b/lib/common/tests/acl/xml_acl_denied_test.c new file mode 100644 index 0000000..faf2a39 --- /dev/null +++ b/lib/common/tests/acl/xml_acl_denied_test.c @@ -0,0 +1,61 @@ +/* + * Copyright 2020-2021 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/acl.h> + +#include "../../crmcommon_private.h" + +static void +is_xml_acl_denied_without_node(void **state) +{ + xmlNode *test_xml = create_xml_node(NULL, "test_xml"); + assert_false(xml_acl_denied(test_xml)); + + test_xml->doc->_private = NULL; + assert_false(xml_acl_denied(test_xml)); + + test_xml->doc = NULL; + assert_false(xml_acl_denied(test_xml)); + + test_xml = NULL; + assert_false(xml_acl_denied(test_xml)); +} + +static void +is_xml_acl_denied_with_node(void **state) +{ + xml_doc_private_t *docpriv; + + xmlNode *test_xml = create_xml_node(NULL, "test_xml"); + + // allocate memory for _private, which is NULL by default + test_xml->doc->_private = calloc(1, sizeof(xml_doc_private_t)); + + assert_false(xml_acl_denied(test_xml)); + + // cast _private from void* to xml_doc_private_t* + docpriv = test_xml->doc->_private; + + // enable an irrelevant flag + docpriv->flags |= pcmk__xf_acl_enabled; + + assert_false(xml_acl_denied(test_xml)); + + // enable pcmk__xf_acl_denied + docpriv->flags |= pcmk__xf_acl_denied; + + assert_true(xml_acl_denied(test_xml)); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(is_xml_acl_denied_without_node), + cmocka_unit_test(is_xml_acl_denied_with_node)) diff --git a/lib/common/tests/acl/xml_acl_enabled_test.c b/lib/common/tests/acl/xml_acl_enabled_test.c new file mode 100644 index 0000000..28665f4 --- /dev/null +++ b/lib/common/tests/acl/xml_acl_enabled_test.c @@ -0,0 +1,61 @@ +/* + * Copyright 2020-2021 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include <crm_internal.h> + +#include <crm/common/unittest_internal.h> +#include <crm/common/acl.h> + +#include "../../crmcommon_private.h" + +static void +is_xml_acl_enabled_without_node(void **state) +{ + xmlNode *test_xml = create_xml_node(NULL, "test_xml"); + assert_false(xml_acl_enabled(test_xml)); + + test_xml->doc->_private = NULL; + assert_false(xml_acl_enabled(test_xml)); + + test_xml->doc = NULL; + assert_false(xml_acl_enabled(test_xml)); + + test_xml = NULL; + assert_false(xml_acl_enabled(test_xml)); +} + +static void +is_xml_acl_enabled_with_node(void **state) +{ + xml_doc_private_t *docpriv; + + xmlNode *test_xml = create_xml_node(NULL, "test_xml"); + + // allocate memory for _private, which is NULL by default + test_xml->doc->_private = calloc(1, sizeof(xml_doc_private_t)); + + assert_false(xml_acl_enabled(test_xml)); + + // cast _private from void* to xml_doc_private_t* + docpriv = test_xml->doc->_private; + + // enable an irrelevant flag + docpriv->flags |= pcmk__xf_acl_denied; + + assert_false(xml_acl_enabled(test_xml)); + + // enable pcmk__xf_acl_enabled + docpriv->flags |= pcmk__xf_acl_enabled; + + assert_true(xml_acl_enabled(test_xml)); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(is_xml_acl_enabled_without_node), + cmocka_unit_test(is_xml_acl_enabled_with_node)) |