>From 17f043d52a8e072a88665bda5c5fe16977858c4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
Date: Fri, 2 Oct 2015 14:26:02 +0200
Subject: [PATCH 2/4] tests: Add get_next_domain_flags test

Ticket:
https://fedorahosted.org/sssd/ticket/2673
---
 src/tests/cmocka/test_utils.c | 135 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)

diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index 27c16bb..fecc557 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -569,6 +569,139 @@ static void test_get_next_domain_disabled(void **state)
     assert_null(dom);
 }
 
+static void test_get_next_domain_flags(void **state)
+{
+    struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
+                                                      struct dom_list_test_ctx);
+    struct sss_domain_info *dom = NULL;
+    uint32_t gnd_flags;
+
+    /* No flags; all doms enabled */
+    gnd_flags = 0;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Descend flag onlyl; all doms enabled  */
+    gnd_flags = SSS_GND_DESCEND;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub1a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2b");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Incl. disabled flag only; all doms enabled */
+    gnd_flags = SSS_GND_INCLUDE_DISABLED;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Descend and inculude disabled; all doms enabled */
+    gnd_flags = SSS_GND_DESCEND | SSS_GND_INCLUDE_DISABLED;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub1a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2b");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Now disable dom2 and sub2a */
+    dom = find_domain_by_name(test_ctx->dom_list, "dom2", false);
+    assert_non_null(dom);
+    sss_domain_set_state(dom, DOM_DISABLED);
+
+    dom = find_domain_by_name(test_ctx->dom_list, "sub2a", false);
+    assert_non_null(dom);
+    sss_domain_set_state(dom, DOM_DISABLED);
+
+    /* No flags; dom2 and sub2a disabled */
+    gnd_flags = 0;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_null(dom);
+
+    /* Descend flag onlyl; dom2 and sub2a disabled  */
+    gnd_flags = SSS_GND_DESCEND;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub1a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2b");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Incl. disabled flag only; dom2 and sub2a disabled */
+    gnd_flags = SSS_GND_INCLUDE_DISABLED;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+
+    /* Descend and inculude disabled; dom2 and sub2a disabled */
+    gnd_flags = SSS_GND_DESCEND | SSS_GND_INCLUDE_DISABLED;
+
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub1a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "dom2");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2a");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_non_null(dom);
+    assert_string_equal(dom->name, "sub2b");
+
+    dom = get_next_domain(dom, gnd_flags);
+    assert_null(dom);
+}
+
 struct name_init_test_ctx {
     struct confdb_ctx *confdb;
 };
@@ -1397,6 +1530,8 @@ int main(int argc, const char *argv[])
                                         setup_dom_tree, teardown_dom_tree),
         cmocka_unit_test_setup_teardown(test_get_next_domain_disabled,
                                         setup_dom_tree, teardown_dom_tree),
+        cmocka_unit_test_setup_teardown(test_get_next_domain_flags,
+                                        setup_dom_tree, teardown_dom_tree),
 
         cmocka_unit_test(test_well_known_sid_to_name),
         cmocka_unit_test(test_name_to_well_known_sid),
-- 
2.1.0

