]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
acl: Add unit test for acl_rights_sort()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 12 Aug 2020 12:48:14 +0000 (15:48 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 11 Sep 2020 07:40:35 +0000 (07:40 +0000)
src/plugins/acl/Makefile.am
src/plugins/acl/test-acl.c [new file with mode: 0644]

index 9ac5b3cebad14bfdf6854452d81dee582a1064a9..8a5323e35de5db16a6d6ee9cf5f94a2f2515de6d 100644 (file)
@@ -2,6 +2,7 @@ doveadm_moduledir = $(moduledir)/doveadm
 
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-test \
        -I$(top_srcdir)/src/lib-dict \
        -I$(top_srcdir)/src/lib-mail \
        -I$(top_srcdir)/src/lib-imap \
@@ -52,3 +53,26 @@ doveadm_module_LTLIBRARIES = \
 
 lib10_doveadm_acl_plugin_la_SOURCES = \
        doveadm-acl.c
+
+test_programs = \
+       test-acl
+
+test_libs = \
+       $(module_LTLIBRARIES) \
+       $(LIBDOVECOT_STORAGE) \
+       $(LIBDOVECOT)
+test_deps = \
+       $(module_LTLIBRARIES) \
+       $(LIBDOVECOT_STORAGE_DEPS) \
+       $(LIBDOVECOT_DEPS)
+
+test_acl_SOURCES = test-acl.c
+test_acl_LDADD = $(test_libs)
+test_acl_DEPENDENCIES = $(test_deps)
+
+check-local:
+       for bin in $(test_programs); do \
+         if ! env $(test_options) $(RUN_TEST) ./$$bin; then exit 1; fi; \
+       done
+
+noinst_PROGRAMS = $(test_programs)
diff --git a/src/plugins/acl/test-acl.c b/src/plugins/acl/test-acl.c
new file mode 100644 (file)
index 0000000..1fafd7a
--- /dev/null
@@ -0,0 +1,66 @@
+/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "test-common.h"
+#include "acl-api-private.h"
+
+static void test_acl_rights_sort(void)
+{
+       struct acl_rights rights1 = {
+               .rights = t_strsplit("a b a c d b", " "),
+               .neg_rights = t_strsplit("e d c a a d b e", " "),
+       };
+       struct acl_rights rights2 = {
+               .rights = t_strsplit("a c x", " "),
+               .neg_rights = t_strsplit("b c y", " "),
+       };
+       struct acl_object obj = {
+               .rights_pool = pool_alloconly_create("acl rights", 256)
+       };
+       const struct acl_rights *rights;
+
+       test_begin("acl_rights_sort");
+       t_array_init(&obj.rights, 8);
+
+       /* try with zero rights */
+       acl_rights_sort(&obj);
+       test_assert(array_count(&obj.rights) == 0);
+
+       /* try with just one right */
+       array_push_back(&obj.rights, &rights1);
+       acl_rights_sort(&obj);
+       test_assert(array_count(&obj.rights) == 1);
+       rights = array_idx(&obj.rights, 0);
+       test_assert(acl_rights_cmp(rights, &rights1) == 0);
+
+       /* try with two rights that don't have equal ID */
+       struct acl_rights rights1_id2 = rights1;
+       rights1_id2.identifier = "id2";
+       array_push_back(&obj.rights, &rights1_id2);
+       acl_rights_sort(&obj);
+       test_assert(array_count(&obj.rights) == 2);
+       rights = array_idx(&obj.rights, 0);
+       test_assert(acl_rights_cmp(&rights[0], &rights1) == 0);
+       test_assert(acl_rights_cmp(&rights[1], &rights1_id2) == 0);
+
+       /* try with 3 rights where first has equal ID */
+       array_push_back(&obj.rights, &rights2);
+       acl_rights_sort(&obj);
+       test_assert(array_count(&obj.rights) == 2);
+       rights = array_idx(&obj.rights, 0);
+       test_assert_strcmp(t_strarray_join(rights[0].rights, " "), "a b c d x");
+       test_assert_strcmp(t_strarray_join(rights[0].neg_rights, " "), "a b c d e y");
+
+       pool_unref(&obj.rights_pool);
+       test_end();
+}
+
+int main(void)
+{
+       static void (*const test_functions[])(void) = {
+               test_acl_rights_sort,
+               NULL
+       };
+       return test_run(test_functions);
+}