On 11/04/2015 11:11 AM, Jakub Hrozek wrote:
Hi,

Sorry it took so long to get back to the review.  I only have some minor
comments, see inline..

Because the group patches are more or less equivalent, I'll just comment
here. If you agree with the comments, please also change the group tests
and resend in a single set.

Thanks for the tests!

> From e3dd543eec09f6e4386bfe6f1505538575fe5356 Mon Sep 17 00:00:00 2001
>From: Petr Cech<pc...@redhat.com>
>Date: Fri, 2 Oct 2015 07:34:08 -0400
>Subject: [PATCH 1/3] TEST: Add test_user_by_recent_filter_valid
>
>Test users_by_filter_valid() was removed in past. We will add two new
>tests instead of it. Logic of those tests is connected to RECENT
>filter. It returns only records which have been wrote or updated after
>filter was created (or another given time).
>
>users_by_filter_valid() --> user_by_recent_filter_valid()
>                             users_by_recent_filter_valid()
>
>The first of new tests, user_by_recent_filter_valid(), counts with two
>users. One is stored before filter request creation and the second user
>is stored after filter request creation. So filter returns only one
>user.
>
>The second of new tests, users_by_recent_filter_valid(), counts with
>three users. One is stored before filter request creation and two users
>are stored after filter request creation. So filter returns two users.
>
>This patch adds user_by_recent_filter_valid().
>
>Resolves:
>https://fedorahosted.org/sssd/ticket/2730
>---
>  src/tests/cmocka/test_responder_cache_req.c | 50 
+++++++++++++++++++++++++++++
>  1 file changed, 50 insertions(+)
>
>diff --git a/src/tests/cmocka/test_responder_cache_req.c 
b/src/tests/cmocka/test_responder_cache_req.c
>index 
744c8f4a8f7aa4e08f82aca5aea003438b5b59da..3379b17f7feea521966d6c8646afd9859a3c5255 
100644
>--- a/src/tests/cmocka/test_responder_cache_req.c
>+++ b/src/tests/cmocka/test_responder_cache_req.c
>@@ -1239,6 +1239,53 @@ static void cache_req_user_by_filter_test_done(struct 
tevent_req *req)
>      ctx->tctx->done = true;
>  }
>
>+void test_user_by_recent_filter_valid(void **state)
>+{
>+    struct cache_req_test_ctx *test_ctx = NULL;
>+    TALLOC_CTX *req_mem_ctx = NULL;
>+    struct tevent_req *req = NULL;
>+    const char *ldbname = NULL;
>+    errno_t ret;
>+
>+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
>+    test_ctx->create_user = true;
>+
>+    ret = sysdb_store_user(test_ctx->tctx->dom, TEST_USER_NAME2, "pwd", 1001, 
1001,
>+                           NULL, NULL, NULL, "cn="TEST_USER_NAME2",dc=test", 
NULL,
>+                           NULL, 1000, time(NULL));
>+    assert_int_equal(ret, EOK);
>+
>+    sleep(1);
The purpose of the sleep() here is just to make sure the entry was
created in the past, right? Would it be equally safe to create the user
with timestamp time(NULL)-1 to make the test faster?

>+
>+    req_mem_ctx = talloc_new(test_ctx->tctx);
>+    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();
Can you add a comment that the TEST_USER is created with a DP callback
here?

>+
>+    req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
>+                                        test_ctx->rctx,
>+                                        test_ctx->tctx->dom->name,
>+                                        "test*");
It would read nicer if we had a constant TEST_USER_PREFIX "test_user" #defined,
or even TEST_USER_FILTER with the asterist.

>+    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, 1);
>+
>+    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
>+                                          SYSDB_NAME, NULL);
>+    assert_non_null(ldbname);
>+    assert_string_equal(ldbname, TEST_USER_NAME);
>+}
> From c2e87544dfbc0667e1b935394d697322b34dddeb Mon Sep 17 00:00:00 2001
>From: Petr Cech<pc...@redhat.com>
>Date: Tue, 27 Oct 2015 03:53:18 -0400
>Subject: [PATCH 2/3] TEST: Refactor of test_responder_cache_req.c
>
>We need little more in background of responder_cache_req tests. There
>will be tests which will use three test users. This patch add support
>for it.
>
>Resolves:
>https://fedorahosted.org/sssd/ticket/2730
>---
>  src/tests/cmocka/test_responder_cache_req.c | 59 
+++++++++++++++++++++--------
>  1 file changed, 44 insertions(+), 15 deletions(-)
>
>diff --git a/src/tests/cmocka/test_responder_cache_req.c 
b/src/tests/cmocka/test_responder_cache_req.c
>index 
3379b17f7feea521966d6c8646afd9859a3c5255..a457d5f277314b75cd73b1306995665456df7f9d 
100644
>--- a/src/tests/cmocka/test_responder_cache_req.c
>+++ b/src/tests/cmocka/test_responder_cache_req.c
>@@ -39,8 +39,15 @@
>  #define TEST_GROUP_NAME "test-group"
>  #define TEST_GROUP_ID 1000
>
>-#define TEST_USER_NAME2 "test-user2"
>-#define TEST_GROUP_NAME2 "test-group2"
>+#define TEST_USER_ID2 1001
>+#define TEST_USER_NAME2 "test_user2"
>+#define TEST_GROUP_NAME2 "test_group2"
>+#define TEST_GROUP_ID2 1001
>+
>+#define TEST_USER_ID3 1002
>+#define TEST_USER_NAME3 "test_user3"
>+#define TEST_GROUP_NAME3 "test_group3"
>+#define TEST_GROUP_ID3 1002
>
>  #define new_single_domain_test(test) \
>      cmocka_unit_test_setup_teardown(test_ ## test, \
>@@ -82,7 +89,8 @@ struct cache_req_test_ctx {
>      struct sss_domain_info *domain;
>      char *name;
>      bool dp_called;
>-    bool create_user;
>+    bool create_user1;
>+    bool create_user2;
With just two booleans this might be OK, but if we end up adding more
users, then additional booleans might get clunky and maybe something
like a bitshift where the DP callback would test for a particular bit
set might be more generic? Can you add a comment so that the
next person touching the code won't add another boolean?

> From 99e1932b876186e702fcb20b6d0bdd5c7e07e1ba Mon Sep 17 00:00:00 2001
>From: Petr Cech<pc...@redhat.com>
>Date: Tue, 27 Oct 2015 04:02:46 -0400
>Subject: [PATCH 3/3] TEST: Add test_users_by_recent_filter_valid
[...]

>+    assert_non_null(test_ctx->result);
>+    assert_int_equal(test_ctx->result->count, 2);
>+
>+    ldbname1 = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
>+                                           SYSDB_NAME, NULL);
>+    assert_non_null(ldbname1);
>+
>+    ldbname2 = ldb_msg_find_attr_as_string(test_ctx->result->msgs[1],
>+                                           SYSDB_NAME, NULL);
>+    assert_non_null(ldbname2);
>+
>+    assert_string_not_equal(ldbname1, ldbname2);
>+    comparison1 = strcmp(ldbname1, TEST_USER_NAME) == 0 ? true : false;
>+    comparison2 = strcmp(ldbname1, TEST_USER_NAME2) == 0 ? true : false;
>+    comparison3 = strcmp(ldbname2, TEST_USER_NAME) == 0 ? true : false;
>+    comparison4 = strcmp(ldbname2, TEST_USER_NAME2) == 0 ? true : false;
>+    assert_true((comparison1 || comparison2) && (comparison3 || comparison4));
>+}
Can you extract this part into a common function that would accept a
ldb_result pointer and two usernames and would make sure they are in the
ldb_messages? The same pattern is used in the group tests.

(I wonder if that could also be written in a nice and generic way for N
usernames in a string array, but it's not important at the moment)

Hello Jakub,

I addresed all hints. There are fixed patch set attached.

Regards,

Petr

PS: I am still not sure what was the purpose of
  * users_by_filter_multiple_domains_valid
  * groups_by_filter_multiple_domains_valid
tests. How I wrote earlier: "I am afraid I misunderstood their purpose. Because users/groups are set with the same domains." Please, do you remember their purpose?
>From c380f1c1df0e891e89bded8e68eefb53a05131c7 Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Fri, 2 Oct 2015 07:34:08 -0400
Subject: [PATCH 1/7] TEST: Add test_user_by_recent_filter_valid

Test users_by_filter_valid() was removed in past. We will add two new
tests instead of it. Logic of those tests is connected to RECENT
filter. It returns only records which have been wrote or updated after
filter was created (or another given time).

users_by_filter_valid() --> user_by_recent_filter_valid()
                            users_by_recent_filter_valid()

The first of new tests, user_by_recent_filter_valid(), counts with two
users. One is stored before filter request creation and the second user
is stored after filter request creation. So filter returns only one
user.

The second of new tests, users_by_recent_filter_valid(), counts with
three users. One is stored before filter request creation and two users
are stored after filter request creation. So filter returns two users.

This patch adds user_by_recent_filter_valid().

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 49 +++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 744c8f4a8f7aa4e08f82aca5aea003438b5b59da..255f728ed82583a5f0c5405953eaf8c2665eab0c 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -1239,6 +1239,52 @@ static void cache_req_user_by_filter_test_done(struct tevent_req *req)
     ctx->tctx->done = true;
 }
 
+void test_user_by_recent_filter_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    const char *ldbname = NULL;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+    test_ctx->create_user = true;
+
+    ret = sysdb_store_user(test_ctx->tctx->dom, TEST_USER_NAME2, "pwd", 1001, 1001,
+                           NULL, NULL, NULL, "cn="TEST_USER_NAME2",dc=test", NULL,
+                           NULL, 1000, time(NULL)-1);
+    assert_int_equal(ret, EOK);
+
+    req_mem_ctx = talloc_new(test_ctx->tctx);
+    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();
+
+    /* User TEST_USER is created with a DP callback. */
+    req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                        test_ctx->rctx,
+                                        test_ctx->tctx->dom->name,
+                                        "test*");
+    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, 1);
+
+    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
+                                          SYSDB_NAME, NULL);
+    assert_non_null(ldbname);
+    assert_string_equal(ldbname, TEST_USER_NAME);
+}
+
+
 void test_users_by_filter_filter_old(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -1476,11 +1522,14 @@ int main(int argc, const char *argv[])
         new_multi_domain_test(group_by_id_multiple_domains_found),
         new_multi_domain_test(group_by_id_multiple_domains_notfound),
 
+        new_single_domain_test(user_by_recent_filter_valid),
+
         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_notfound),
         new_single_domain_test(groups_by_filter_notfound),
         new_multi_domain_test(groups_by_filter_multiple_domains_notfound),
+
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
-- 
2.4.3

>From ab97caaf39b3b65416fa8a4a72a104a9a0391a48 Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Wed, 4 Nov 2015 06:50:33 -0500
Subject: [PATCH 2/7] TEST: Refactor of test_responder_cache_req.c

This patch only defines constant TEST_USER_FILTER. So code will be more
redeable.

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 255f728ed82583a5f0c5405953eaf8c2665eab0c..14231b3dcdf7c6da5e475f36d3314f11be2f42aa 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -42,6 +42,8 @@
 #define TEST_USER_NAME2 "test-user2"
 #define TEST_GROUP_NAME2 "test-group2"
 
+#define TEST_USER_PREFIX "test*"
+
 #define new_single_domain_test(test) \
     cmocka_unit_test_setup_teardown(test_ ## test, \
                                     test_single_domain_setup, \
@@ -1266,7 +1268,7 @@ void test_user_by_recent_filter_valid(void **state)
     req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
                                         test_ctx->rctx,
                                         test_ctx->tctx->dom->name,
-                                        "test*");
+                                        TEST_USER_PREFIX);
     assert_non_null(req);
 
     tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
@@ -1313,7 +1315,7 @@ void test_users_by_filter_filter_old(void **state)
     req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
                                         test_ctx->rctx,
                                         test_ctx->tctx->dom->name,
-                                        "test*");
+                                        TEST_USER_PREFIX);
     assert_non_null(req);
     tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
 
-- 
2.4.3

>From 21f44a019f2e4fcd97d3f3d25f4ab2ddedd1b93e Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Tue, 27 Oct 2015 03:53:18 -0400
Subject: [PATCH 3/7] TEST: Refactor of test_responder_cache_req.c

We need little more in background of responder_cache_req tests. There
will be tests which will use three test users. This patch add support
for it.

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 61 ++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 14231b3dcdf7c6da5e475f36d3314f11be2f42aa..e5f92329fd06019eaccc18aa57756e59b72e815c 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -39,8 +39,15 @@
 #define TEST_GROUP_NAME "test-group"
 #define TEST_GROUP_ID 1000
 
-#define TEST_USER_NAME2 "test-user2"
-#define TEST_GROUP_NAME2 "test-group2"
+#define TEST_USER_ID2 1001
+#define TEST_USER_NAME2 "test_user2"
+#define TEST_GROUP_NAME2 "test_group2"
+#define TEST_GROUP_ID2 1001
+
+#define TEST_USER_ID3 1002
+#define TEST_USER_NAME3 "test_user3"
+#define TEST_GROUP_NAME3 "test_group3"
+#define TEST_GROUP_ID3 1002
 
 #define TEST_USER_PREFIX "test*"
 
@@ -84,7 +91,10 @@ struct cache_req_test_ctx {
     struct sss_domain_info *domain;
     char *name;
     bool dp_called;
-    bool create_user;
+
+    /* NOTE: Please, instead of adding new create_user bool, use bitshift. */
+    bool create_user1;
+    bool create_user2;
     bool create_group;
 };
 
@@ -159,10 +169,13 @@ static void cache_req_group_by_id_test_done(struct tevent_req *req)
     ctx->tctx->done = true;
 }
 
-static void prepare_user(TALLOC_CTX *mem_ctx,
-                         struct sss_domain_info *domain,
-                         uint64_t timeout,
-                         time_t time)
+static void prepare_concrete_user(TALLOC_CTX *mem_ctx,
+                                  struct sss_domain_info *domain,
+                                  const char* user_name,
+                                  int user_id,
+                                  int group_id,
+                                  uint64_t timeout,
+                                  time_t time)
 {
     struct sysdb_attrs *attrs;
     errno_t ret;
@@ -173,13 +186,22 @@ static void prepare_user(TALLOC_CTX *mem_ctx,
     ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, TEST_UPN);
     assert_int_equal(ret, EOK);
 
-    ret = sysdb_store_user(domain, TEST_USER_NAME, "pwd",
-                           TEST_USER_ID, TEST_GROUP_ID, NULL, NULL, NULL,
+    ret = sysdb_store_user(domain, user_name, "pwd",
+                           user_id, group_id, NULL, NULL, NULL,
                            "cn=test-user,dc=test", attrs, NULL,
                            timeout, time);
     assert_int_equal(ret, EOK);
 }
 
+static void prepare_user(TALLOC_CTX *mem_ctx,
+                         struct sss_domain_info *domain,
+                         uint64_t timeout,
+                         time_t time)
+{
+    prepare_concrete_user(mem_ctx, domain, TEST_USER_NAME, TEST_USER_ID,
+                          TEST_GROUP_ID, timeout, time);
+}
+
 static void run_user_by_name(struct cache_req_test_ctx *test_ctx,
                              struct sss_domain_info *domain,
                              int cache_refresh_percent,
@@ -313,9 +335,13 @@ __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
     ctx = sss_mock_ptr_type(struct cache_req_test_ctx*);
     ctx->dp_called = true;
 
-    if (ctx->create_user) {
+    if (ctx->create_user1) {
         prepare_user(ctx, ctx->tctx->dom, 1000, time(NULL));
     }
+    if (ctx->create_user2) {
+        prepare_concrete_user(mem_ctx, ctx->tctx->dom, TEST_USER_NAME2,
+                              TEST_USER_ID2, TEST_GROUP_ID2, 1000, time(NULL));
+    }
 
     if (ctx->create_group) {
         ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME,
@@ -570,7 +596,8 @@ void test_user_by_name_missing_found(void **state)
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    test_ctx->create_user = true;
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
 
     /* Test. */
     run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
@@ -723,7 +750,8 @@ void test_user_by_upn_missing_found(void **state)
     mock_account_recv_simple();
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    test_ctx->create_user = true;
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
 
     /* Test. */
     run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
@@ -865,7 +893,8 @@ void test_user_by_id_missing_found(void **state)
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    test_ctx->create_user = true;
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
 
     /* Test. */
     run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
@@ -1250,7 +1279,8 @@ void test_user_by_recent_filter_valid(void **state)
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
-    test_ctx->create_user = true;
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
 
     ret = sysdb_store_user(test_ctx->tctx->dom, TEST_USER_NAME2, "pwd", 1001, 1001,
                            NULL, NULL, NULL, "cn="TEST_USER_NAME2",dc=test", NULL,
@@ -1296,7 +1326,8 @@ void test_users_by_filter_filter_old(void **state)
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
-    test_ctx->create_user = true;
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
 
     /* This user was updated in distant past, so it wont't be reported by
      * the filter search */
-- 
2.4.3

>From beb13c2bd236355666127f8476966b29e6f33295 Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Tue, 27 Oct 2015 04:02:46 -0400
Subject: [PATCH 4/7] TEST: Add test_users_by_recent_filter_valid

Test users_by_filter_valid() was removed in past. We will add two new
tests instead of it. Logic of those tests is connected to RECENT
filter. It returns only records which have been wrote or updated
after filter was created (or another given time).

users_by_filter_valid() --> user_by_recent_filter_valid()
                            users_by_recent_filter_valid()

The first of new tests, user_by_recent_filter_valid(), counts with
two users. One is stored before filter request creation and the second
user is stored after filter request creation. So filter returns only one
user.

The second of new tests, users_by_recent_filter_valid(), counts with
three users. One is stored before filter request creation and two
users are stored after filter request creation. So filter returns two
users.

This patch adds users_by_recent_filter_valid().

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 88 ++++++++++++++++++++++++++++-
 1 file changed, 87 insertions(+), 1 deletion(-)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index e5f92329fd06019eaccc18aa57756e59b72e815c..c25256a9d4685d66a818512e9d46edcacbdc65bd 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -113,6 +113,27 @@ struct cli_protocol_version *register_cli_protocol_version(void)
     return version;
 }
 
+/* Return true if all values are in ldb_results */
+static bool are_values_in_ldb_result(const char **ldb_results,
+                                     const char **values)
+{
+    bool is_value_in_result = false;
+    bool is_value_in_results = false;
+    bool result = true;
+
+    for (unsigned int i = 0; i < talloc_array_length(values); ++i) {
+        is_value_in_results = false;
+        for (size_t j = 0; j < talloc_array_length(ldb_results); ++j) {
+            is_value_in_result = strcmp(ldb_results[i], values[j]) == 0 ? \
+                                 true : false;
+            is_value_in_results = is_value_in_results || is_value_in_result;
+        }
+        result = result && is_value_in_results;
+    }
+
+    return result;
+}
+
 static void cache_req_user_by_name_test_done(struct tevent_req *req)
 {
     struct cache_req_test_ctx *ctx = NULL;
@@ -318,7 +339,6 @@ static void check_group(struct cache_req_test_ctx *test_ctx,
     assert_string_equal(exp_dom->name, test_ctx->domain->name);
 }
 
-
 struct tevent_req *
 __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
                                struct resp_ctx *rctx,
@@ -1316,6 +1336,71 @@ void test_user_by_recent_filter_valid(void **state)
     assert_string_equal(ldbname, TEST_USER_NAME);
 }
 
+void test_users_by_recent_filter_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    const char **user_names = NULL;
+    const char **ldb_results = NULL;
+    const char *ldbname = NULL;
+    void *tmp_ctx = 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;
+
+    ret = sysdb_store_user(test_ctx->tctx->dom, TEST_USER_NAME3, "pwd", 1002, 1002,
+                           NULL, NULL, NULL, "cn="TEST_USER_NAME3",dc=test", NULL,
+                           NULL, 1000, time(NULL)-1);
+    assert_int_equal(ret, EOK);
+
+    req_mem_ctx = talloc_new(test_ctx->tctx);
+    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();
+
+    /* User TEST_USER1 and TEST_USER2 are created with a DP callback. */
+    req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                        test_ctx->rctx,
+                                        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, 2);
+
+    tmp_ctx = talloc_zero(NULL, void *);
+
+    user_names = talloc_array(tmp_ctx, const char *, 2);
+    assert_non_null(user_names);
+    user_names[0] = TEST_USER_NAME;
+    user_names[1] = TEST_USER_NAME2;
+
+    ldb_results = talloc_array(tmp_ctx, const char *, 2);
+    assert_non_null(ldb_results);
+    for (int i = 0; i < 2; ++i) {
+        ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[i],
+                                              SYSDB_NAME, NULL);
+        assert_non_null(ldbname);
+        ldb_results[i] = ldbname;
+    }
+
+    assert_string_not_equal(ldb_results[0], ldb_results[1]);
+
+    assert_true(are_values_in_ldb_result(ldb_results, user_names));
+
+    talloc_zfree(tmp_ctx);
+}
 
 void test_users_by_filter_filter_old(void **state)
 {
@@ -1556,6 +1641,7 @@ int main(int argc, const char *argv[])
         new_multi_domain_test(group_by_id_multiple_domains_notfound),
 
         new_single_domain_test(user_by_recent_filter_valid),
+        new_single_domain_test(users_by_recent_filter_valid),
 
         new_single_domain_test(users_by_filter_filter_old),
         new_single_domain_test(users_by_filter_notfound),
-- 
2.4.3

>From c464211d04e0dd5faff3a69e21808ca23d091309 Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Sun, 1 Nov 2015 07:09:28 -0500
Subject: [PATCH 5/7] TEST: Add test_group_by_recent_filter_valid

Test groups_by_filter_valid() was removed in past. We will add two new
tests instead of it. Logic of those tests is connected to RECENT
filter. It returns only records which have been wrote or updated after
filter was created (or another given time).

groups_by_filter_valid() --> group_by_recent_filter_valid()
                             grous_by_recent_filter_valid()

The first of new tests, group_by_recent_filter_valid(), counts with two
groups. One is stored before filter request creation and the second
group is stored after filter request creation. So filter returns only
one group.

The second of new tests, groups_by_recent_filter_valid(), counts with
three users. One is stored before filter request creation and two
groups are stored after filter request creation. So filter returns two
groups.

This patch adds group_by_recent_filter_valid().

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 44 +++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index c25256a9d4685d66a818512e9d46edcacbdc65bd..d1eaefe406563d3d1e383e7235483576683827a9 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -1522,6 +1522,49 @@ static void cache_req_group_by_filter_test_done(struct tevent_req *req)
     ctx->tctx->done = true;
 }
 
+void test_group_by_recent_filter_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    const char *ldbname = NULL;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+    test_ctx->create_group = true;
+
+    ret = sysdb_store_group(test_ctx->tctx->dom, TEST_GROUP_NAME2,
+                            1001, NULL, 1001, time(NULL)-1);
+    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();
+
+    /* Group TEST_GROUP is created with a DP callback. */
+    req = cache_req_group_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                         test_ctx->rctx,
+                                         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, 1);
+
+    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
+                                          SYSDB_NAME, NULL);
+    assert_non_null(ldbname);
+    assert_string_equal(ldbname, TEST_GROUP_NAME);
+}
+
 void test_groups_by_filter_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -1642,6 +1685,7 @@ int main(int argc, const char *argv[])
 
         new_single_domain_test(user_by_recent_filter_valid),
         new_single_domain_test(users_by_recent_filter_valid),
+        new_single_domain_test(group_by_recent_filter_valid),
 
         new_single_domain_test(users_by_filter_filter_old),
         new_single_domain_test(users_by_filter_notfound),
-- 
2.4.3

>From 60883b72a6f2d46ca1a20b335ce5658afca327c2 Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Sun, 1 Nov 2015 07:21:18 -0500
Subject: [PATCH 6/7] TEST: Refactor of test_responder_cache_req.c

We need little more in backroung of responder_cache_req tests. There
will be tests which will use three test groups. This patch add support
for it.

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index d1eaefe406563d3d1e383e7235483576683827a9..ea1ef0b325aea863a23a4ab19b70f7abe46ae3ba 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -92,10 +92,12 @@ struct cache_req_test_ctx {
     char *name;
     bool dp_called;
 
-    /* NOTE: Please, instead of adding new create_user bool, use bitshift. */
+    /* NOTE: Please, instead of adding new create_[user|group] bool,
+     * use bitshift. */
     bool create_user1;
     bool create_user2;
-    bool create_group;
+    bool create_group1;
+    bool create_group2;
 };
 
 const char *domains[] = {"responder_cache_req_test_a",
@@ -363,11 +365,17 @@ __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
                               TEST_USER_ID2, TEST_GROUP_ID2, 1000, time(NULL));
     }
 
-    if (ctx->create_group) {
+    if (ctx->create_group1) {
         ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME,
                                 TEST_GROUP_ID, NULL, 1000, time(NULL));
         assert_int_equal(ret, EOK);
     }
+    if (ctx->create_group2) {
+        ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME2,
+                                TEST_GROUP_ID2, NULL, 1000, time(NULL));
+        assert_int_equal(ret, EOK);
+    }
+
 
     return test_req_succeed_send(mem_ctx, rctx->ev);
 }
@@ -1114,7 +1122,8 @@ void test_group_by_name_missing_found(void **state)
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    test_ctx->create_group = true;
+    test_ctx->create_group1 = true;
+    test_ctx->create_group2 = false;
 
     /* Test. */
     run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
@@ -1254,7 +1263,8 @@ void test_group_by_id_missing_found(void **state)
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    test_ctx->create_group = true;
+    test_ctx->create_group1 = true;
+    test_ctx->create_group2 = false;
 
     /* Test. */
     run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
@@ -1531,7 +1541,8 @@ void test_group_by_recent_filter_valid(void **state)
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
-    test_ctx->create_group = true;
+    test_ctx->create_group1 = true;
+    test_ctx->create_group2 = false;
 
     ret = sysdb_store_group(test_ctx->tctx->dom, TEST_GROUP_NAME2,
                             1001, NULL, 1001, time(NULL)-1);
-- 
2.4.3

>From b84ebcdfbe5786b5ac1e641cefcd3a20cc3c509a Mon Sep 17 00:00:00 2001
From: Petr Cech <pc...@redhat.com>
Date: Sun, 1 Nov 2015 07:45:56 -0500
Subject: [PATCH 7/7] TEST: Add test_groups_by_recent_filter_valid

Test groups_by_filter_valid() was removed in past. We will add two new
tests instead of it. Logic of those tests is connected to RECENT
filter. It returns only records which have been wrote or updated after
filter was created (or another given time).

groups_by_filter_valid() --> group_by_recent_filter_valid()
                             grous_by_recent_filter_valid()

The first of new tests, group_by_recent_filter_valid(), counts with two
groups. One is stored before filter request creation and the second
group is stored after filter request creation. So filter returns only
one group.

The second of new tests, groups_by_recent_filter_valid(), counts with
three users. One is stored before filter request creation and two
groups are stored after filter request creation. So filter returns two
groups.

This patch adds groups_by_recent_filter_valid().

Resolves:
https://fedorahosted.org/sssd/ticket/2730
---
 src/tests/cmocka/test_responder_cache_req.c | 68 +++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index ea1ef0b325aea863a23a4ab19b70f7abe46ae3ba..a1ea4e26b743479b3e26e578c3f97f07a2a1778f 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -1576,6 +1576,73 @@ void test_group_by_recent_filter_valid(void **state)
     assert_string_equal(ldbname, TEST_GROUP_NAME);
 }
 
+void test_groups_by_recent_filter_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    TALLOC_CTX *req_mem_ctx = NULL;
+    struct tevent_req *req = NULL;
+    const char **group_names = NULL;
+    const char **ldb_results = NULL;
+    const char *ldbname = NULL;
+    void *tmp_ctx = 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;
+
+    ret = sysdb_store_group(test_ctx->tctx->dom, TEST_GROUP_NAME2,
+                            1001, NULL, 1001, time(NULL));
+    assert_int_equal(ret, EOK);
+
+    sleep(1);
+
+    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();
+
+    /* Group TEST_GROUP1 and TEST_GROUP2 are created with a DP callback. */
+    req = cache_req_group_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
+                                         test_ctx->rctx,
+                                         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, 2);
+
+    tmp_ctx = talloc_zero(NULL, void *);
+
+    group_names = talloc_array(tmp_ctx, const char *, 2);
+    assert_non_null(group_names);
+    group_names[0] = TEST_GROUP_NAME;
+    group_names[1] = TEST_GROUP_NAME2;
+
+    ldb_results = talloc_array(tmp_ctx, const char *, 2);
+    assert_non_null(ldb_results);
+    for (int i = 0; i < 2; ++i) {
+        ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[i],
+                                              SYSDB_NAME, NULL);
+        assert_non_null(ldbname);
+        ldb_results[i] = ldbname;
+    }
+
+    assert_string_not_equal(ldb_results[0], ldb_results[1]);
+
+    assert_true(are_values_in_ldb_result(ldb_results, group_names));
+
+    talloc_zfree(tmp_ctx);
+}
+
 void test_groups_by_filter_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -1697,6 +1764,7 @@ int main(int argc, const char *argv[])
         new_single_domain_test(user_by_recent_filter_valid),
         new_single_domain_test(users_by_recent_filter_valid),
         new_single_domain_test(group_by_recent_filter_valid),
+        new_single_domain_test(groups_by_recent_filter_valid),
 
         new_single_domain_test(users_by_filter_filter_old),
         new_single_domain_test(users_by_filter_notfound),
-- 
2.4.3

_______________________________________________
sssd-devel mailing list
sssd-devel@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/sssd-devel

Reply via email to