From 6d959e66fb88e918945a9c6a7da72be5861ebe6d Mon Sep 17 00:00:00 2001
From: ikerexxe <ipedrosa@redhat.com>
Date: Mon, 6 Jul 2020 14:39:42 +0200
Subject: [PATCH 1/2] Test: Add users_by_filter_multiple_domains_valid

Test users_by_filter_multiple_domains_valid was removed in [1] because
it was failing. Apparently, the failure was related with a filter that caused
that only users added after the request was started to be returned. When adding
back the test I haven't found that problem, but another one related with memory
handling in the test itself.

The failure was related with a filter, added when
calling cache_req_group_fy_filter_send(), that causes that only users
added after the request started are returned.

This commit adds back the test after fixing several problems related
with memory handling in the test itself.

Explanation of the test:
Given two users are present
When the users are searched by filtering domains
Then the two users are returned correctly.

Resolves:
https://github.com/SSSD/sssd/issues/3920

Links:
[1] https://github.com/SSSD/sssd/commit/bdf422fde0fd6b40b3412bad3b200f8fd7ea8693
---
 src/tests/cmocka/test_responder_cache_req.c | 91 +++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 68a6512409..cb306a2c49 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -2451,6 +2451,96 @@ void test_users_by_filter_notfound(void **state)
     assert_true(check_leaks_pop(req_mem_ctx));
 }
 
+/*
+ * Given two users are present
+ * When the users are searched by filtering domains
+ * Then the two users are returned correctly.
+ */
+static void test_users_by_filter_multiple_domains_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    struct sss_domain_info *domain = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    size_t num_users = 2;
+    const char **input_dns = NULL;
+    const char **user_names = NULL;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = true;
+
+    domain = find_domain_by_name(test_ctx->tctx->dom,
+                                 "responder_cache_req_test_d", true);
+    assert_non_null(domain);
+
+    /* Generate DN for user1 */
+    input_dns = talloc_zero_array(test_ctx, const char *, num_users);
+    assert_non_null(input_dns);
+    input_dns[0] = talloc_asprintf(input_dns, "cn=%s,dc=test",
+                                    users[0].short_name);
+    assert_non_null(input_dns[0]);
+
+    /* Generate internal FQDN for user1 */
+    user_names = talloc_zero_array(test_ctx, const char *, num_users);
+    assert_non_null(user_names);
+    user_names[0] = sss_create_internal_fqname(user_names, users[0].short_name,
+                                                domain->name);
+    assert_non_null(user_names[0]);
+
+    ret = sysdb_store_user(domain, user_names[0], "pwd", 1000, 1000,
+                           NULL, NULL, NULL, input_dns[0], NULL,
+                           NULL, 1000, time(NULL));
+    assert_int_equal(ret, EOK);
+
+    /* Generate DN for user2 */
+    input_dns[1] = talloc_asprintf(input_dns, "cn=%s,dc=test",
+                                    users[1].short_name);
+    assert_non_null(input_dns[1]);
+
+    /* Generate internal FQDN for user2 */
+    user_names[1] = sss_create_internal_fqname(user_names, users[1].short_name,
+                                                domain->name);
+    assert_non_null(user_names[1]);
+
+    ret = sysdb_store_user(domain, user_names[1], "pwd", 1001, 1001,
+                           NULL, NULL, NULL, input_dns[1], NULL,
+                           NULL, 1000, time(NULL));
+    assert_int_equal(ret, EOK);
+
+    req_mem_ctx = talloc_new(global_talloc_context);
+    check_leaks_push(req_mem_ctx);
+
+    /* Filters always go to DP */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                        test_ctx->rctx,
+                                        CACHE_REQ_POSIX_DOM,
+                                        test_ctx->tctx->dom->name,
+                                        TEST_USER_PREFIX);
+    assert_non_null(req);
+    tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
+
+    ret = test_ev_loop(test_ctx->tctx);
+    assert_int_equal(ret, ERR_OK);
+    assert_true(check_leaks_pop(req_mem_ctx));
+
+    assert_non_null(test_ctx->result);
+    assert_int_equal(test_ctx->result->count, num_users);
+
+    for (int i = 0; i < num_users; ++i) {
+        assert_msg_has_shortname(test_ctx,
+                                 test_ctx->result->msgs[i],
+                                 users[i].short_name);
+    }
+
+    talloc_free(user_names);
+    talloc_free(input_dns);
+}
+
 void test_users_by_filter_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -4027,6 +4117,7 @@ int main(int argc, const char *argv[])
 
         new_single_domain_test(users_by_filter_filter_old),
         new_single_domain_test(users_by_filter_notfound),
+        new_multi_domain_test(users_by_filter_multiple_domains_valid),
         new_multi_domain_test(users_by_filter_multiple_domains_notfound),
         new_single_domain_test(groups_by_filter_notfound),
         new_multi_domain_test(groups_by_filter_multiple_domains_notfound),

From f5236eab06d3709bab3d6480d52a6b2203f53a7c Mon Sep 17 00:00:00 2001
From: ikerexxe <ipedrosa@redhat.com>
Date: Mon, 6 Jul 2020 16:14:43 +0200
Subject: [PATCH 2/2] Test: Add groups_by_filter_multiple_domains_valid

Test groups_by_filter_multiple_domains_valid was removed in [1] because
it was failing. Apparently, the failure was related with a filter that caused
that only groups added after the request was started to be returned. When adding
back the test I haven't found that problem, but another one related with memory
handling in the test itself.

This commit adds back the test after fixing several problems related
with memory handling in the test itself.

Explanation of the test:
Given two groups are present
When the groups are searched by filtering domains
Then the two groups are returned correctly.

Resolves:
https://github.com/SSSD/sssd/issues/3920

Links:
[1] https://github.com/SSSD/sssd/commit/bdf422fde0fd6b40b3412bad3b200f8fd7ea8693
---
 src/tests/cmocka/test_responder_cache_req.c | 79 +++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index cb306a2c49..37638137df 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -2724,6 +2724,84 @@ void test_groups_by_filter_notfound(void **state)
     assert_true(check_leaks_pop(req_mem_ctx));
 }
 
+/*
+ * Given two groups are present
+ * When the groups are searched by filtering domains
+ * Then the two groups are returned correctly.
+ */
+void test_groups_by_filter_multiple_domains_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    struct sss_domain_info *domain = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    size_t num_groups = 2;
+    const char **group_names = NULL;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+    test_ctx->create_group1 = true;
+    test_ctx->create_group2 = true;
+
+    domain = find_domain_by_name(test_ctx->tctx->dom,
+                                 "responder_cache_req_test_d", true);
+    assert_non_null(domain);
+
+    /* Generate internal FQDN for group1 */
+    group_names = talloc_zero_array(test_ctx, const char *, num_groups);
+    assert_non_null(group_names);
+    group_names[0] = sss_create_internal_fqname(group_names,
+                                                groups[0].short_name,
+                                                domain->name);
+    assert_non_null(group_names[0]);
+
+    ret = sysdb_store_group(domain, group_names[0],
+                            1000, NULL, 1000, time(NULL));
+    assert_int_equal(ret, EOK);
+
+    /* Generate internal FQDN for group2 */
+    group_names[1] = sss_create_internal_fqname(group_names,
+                                                groups[1].short_name,
+                                                domain->name);
+    assert_non_null(group_names[1]);
+
+    ret = sysdb_store_group(domain, group_names[1],
+                            1001, NULL, 1001, time(NULL));
+    assert_int_equal(ret, EOK);
+
+    req_mem_ctx = talloc_new(global_talloc_context);
+    check_leaks_push(req_mem_ctx);
+
+    /* Filters always go to DP */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    req = cache_req_group_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                        test_ctx->rctx,
+                                        CACHE_REQ_POSIX_DOM,
+                                        test_ctx->tctx->dom->name,
+                                        TEST_USER_PREFIX);
+    assert_non_null(req);
+    tevent_req_set_callback(req, cache_req_group_by_filter_test_done, test_ctx);
+
+    ret = test_ev_loop(test_ctx->tctx);
+    assert_int_equal(ret, ERR_OK);
+    assert_true(check_leaks_pop(req_mem_ctx));
+
+    assert_non_null(test_ctx->result);
+    assert_int_equal(test_ctx->result->count, num_groups);
+
+    assert_msg_has_shortname(test_ctx,
+                             test_ctx->result->msgs[0],
+                             groups[1].short_name);
+
+    assert_msg_has_shortname(test_ctx,
+                             test_ctx->result->msgs[1],
+                             groups[0].short_name);
+
+    talloc_free(group_names);
+}
+
 void test_groups_by_filter_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -4120,6 +4198,7 @@ int main(int argc, const char *argv[])
         new_multi_domain_test(users_by_filter_multiple_domains_valid),
         new_multi_domain_test(users_by_filter_multiple_domains_notfound),
         new_single_domain_test(groups_by_filter_notfound),
+        new_multi_domain_test(groups_by_filter_multiple_domains_valid),
         new_multi_domain_test(groups_by_filter_multiple_domains_notfound),
 
         new_single_domain_test(object_by_sid_user_cache_valid),
