From e41f96a47f2b0f8d3e07e34af83e9a516d29df34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 14 Sep 2015 11:06:45 +0200
Subject: [PATCH 3/3] cache_req tests: reduce code duplication

---
 src/tests/cmocka/test_responder_cache_req.c | 1624 +++++++--------------------
 1 file changed, 394 insertions(+), 1230 deletions(-)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 7db87ccc816ea0e30e707ec8c2fa4666441892a8..2af481494319d0d01d29d0c243020c5adcb06d3a 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -52,6 +52,27 @@
                                     test_multi_domain_setup, \
                                     test_multi_domain_teardown)
 
+#define run_cache_req(ctx, send_fn, done_fn, dom, crp, lookup, expret) do { \
+    TALLOC_CTX *req_mem_ctx;                                                \
+    struct tevent_req *req;                                                 \
+    errno_t ret;                                                            \
+                                                                            \
+    req_mem_ctx = talloc_new(global_talloc_context);                        \
+    check_leaks_push(req_mem_ctx);                                          \
+                                                                            \
+    req = send_fn(req_mem_ctx, ctx->tctx->ev, ctx->rctx,                    \
+                  ctx->ncache, 10, crp,                                     \
+                  (dom == NULL ? NULL : dom->name), lookup);                \
+    assert_non_null(req);                                                   \
+    tevent_req_set_callback(req, done_fn, ctx);                             \
+                                                                            \
+    ret = test_ev_loop(ctx->tctx);                                          \
+    assert_int_equal(ret, expret);                                          \
+    assert_true(check_leaks_pop(req_mem_ctx));                              \
+                                                                            \
+    talloc_free(req_mem_ctx);                                               \
+} while (0)
+
 struct cache_req_test_ctx {
     struct sss_test_ctx *tctx;
     struct resp_ctx *rctx;
@@ -80,46 +101,6 @@ struct cli_protocol_version *register_cli_protocol_version(void)
     return version;
 }
 
-struct tevent_req *
-__wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
-                               struct resp_ctx *rctx,
-                               struct sss_domain_info *dom,
-                               bool fast_reply,
-                               enum sss_dp_acct_type type,
-                               const char *opt_name,
-                               uint32_t opt_id,
-                               const char *extra)
-{
-    struct sysdb_attrs *attrs = NULL;
-    struct cache_req_test_ctx *ctx = NULL;
-    errno_t ret;
-
-    ctx = sss_mock_ptr_type(struct cache_req_test_ctx*);
-    ctx->dp_called = true;
-
-    if (ctx->create_user) {
-        attrs = sysdb_new_attrs(ctx);
-        assert_non_null(attrs);
-
-        ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, TEST_UPN);
-        assert_int_equal(ret, EOK);
-
-        ret = sysdb_store_user(ctx->tctx->dom, TEST_USER_NAME, "pwd",
-                               TEST_USER_ID, 1000, NULL, NULL, NULL,
-                               "cn=test-user,dc=test", attrs, NULL,
-                               1000, time(NULL));
-        assert_int_equal(ret, EOK);
-    }
-
-    if (ctx->create_group) {
-        ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME,
-                                TEST_GROUP_ID, NULL, 1000, time(NULL));
-        assert_int_equal(ret, EOK);
-    }
-
-    return test_req_succeed_send(mem_ctx, rctx->ev);
-}
-
 static void cache_req_user_by_name_test_done(struct tevent_req *req)
 {
     struct cache_req_test_ctx *ctx = NULL;
@@ -176,6 +157,173 @@ 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)
+{
+    struct sysdb_attrs *attrs;
+    errno_t ret;
+
+    attrs = sysdb_new_attrs(mem_ctx);
+    assert_non_null(attrs);
+
+    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,
+                           "cn=test-user,dc=test", attrs, NULL,
+                           timeout, time);
+    assert_int_equal(ret, EOK);
+}
+
+static void run_user_by_name(struct cache_req_test_ctx *test_ctx,
+                             struct sss_domain_info *domain,
+                             int cache_refresh_percent,
+                             errno_t exp_ret)
+{
+    run_cache_req(test_ctx, cache_req_user_by_name_send,
+                  cache_req_user_by_name_test_done, domain,
+                  cache_refresh_percent, TEST_USER_NAME, exp_ret);
+}
+
+static errno_t run_user_by_upn(struct cache_req_test_ctx *test_ctx,
+                               struct sss_domain_info *domain,
+                               int cache_refresh_percent,
+                               errno_t exp_ret)
+{
+    run_cache_req(test_ctx, cache_req_user_by_name_send,
+                  cache_req_user_by_name_test_done, domain,
+                  cache_refresh_percent, TEST_UPN, exp_ret);
+}
+
+static errno_t run_user_by_id(struct cache_req_test_ctx *test_ctx,
+                              struct sss_domain_info *domain,
+                              int cache_refresh_percent,
+                              errno_t exp_ret)
+{
+    run_cache_req(test_ctx, cache_req_user_by_id_send,
+                  cache_req_user_by_id_test_done, domain,
+                  cache_refresh_percent, TEST_USER_ID, exp_ret);
+}
+
+static void check_user(struct cache_req_test_ctx *test_ctx,
+                       struct sss_domain_info *exp_dom)
+{
+    const char *ldbname;
+    const char *ldbupn;
+    uid_t ldbuid;
+
+    assert_non_null(test_ctx->result);
+    assert_int_equal(test_ctx->result->count, 1);
+    assert_non_null(test_ctx->result->msgs);
+    assert_non_null(test_ctx->result->msgs[0]);
+
+    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);
+
+    ldbupn = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
+                                         SYSDB_UPN, NULL);
+    assert_non_null(ldbupn);
+    assert_string_equal(ldbupn, TEST_UPN);
+
+    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
+                                       SYSDB_UIDNUM, 0);
+    assert_int_equal(ldbuid, TEST_USER_ID);
+
+    assert_non_null(test_ctx->domain);
+    assert_string_equal(exp_dom->name, test_ctx->domain->name);
+}
+
+static void prepare_group(TALLOC_CTX *mem_ctx,
+                         struct sss_domain_info *domain,
+                         uint64_t timeout,
+                         time_t time)
+{
+    errno_t ret;
+
+    ret = sysdb_store_group(domain, TEST_GROUP_NAME, TEST_GROUP_ID, NULL,
+                            timeout, time);
+    assert_int_equal(ret, EOK);
+}
+
+static errno_t run_group_by_name(struct cache_req_test_ctx *test_ctx,
+                                 struct sss_domain_info *domain,
+                                 int cache_refresh_percent,
+                                 errno_t exp_ret)
+{
+    run_cache_req(test_ctx, cache_req_group_by_name_send,
+                  cache_req_group_by_name_test_done, domain,
+                  cache_refresh_percent, TEST_GROUP_NAME, exp_ret);
+}
+
+static errno_t run_group_by_id(struct cache_req_test_ctx *test_ctx,
+                               struct sss_domain_info *domain,
+                               int cache_refresh_percent,
+                               errno_t exp_ret)
+{
+    run_cache_req(test_ctx, cache_req_group_by_id_send,
+                  cache_req_group_by_id_test_done, domain,
+                  cache_refresh_percent, TEST_GROUP_ID, exp_ret);
+}
+
+static void check_group(struct cache_req_test_ctx *test_ctx,
+                        struct sss_domain_info *exp_dom)
+{
+    const char *ldbname;
+    gid_t ldbgid;
+
+    assert_non_null(test_ctx->result);
+    assert_int_equal(test_ctx->result->count, 1);
+    assert_non_null(test_ctx->result->msgs);
+    assert_non_null(test_ctx->result->msgs[0]);
+
+    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);
+
+    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
+                                       SYSDB_GIDNUM, 0);
+    assert_int_equal(ldbgid, TEST_USER_ID);
+
+    assert_non_null(test_ctx->domain);
+    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,
+                               struct sss_domain_info *dom,
+                               bool fast_reply,
+                               enum sss_dp_acct_type type,
+                               const char *opt_name,
+                               uint32_t opt_id,
+                               const char *extra)
+{
+    struct cache_req_test_ctx *ctx = NULL;
+    errno_t ret;
+
+    ctx = sss_mock_ptr_type(struct cache_req_test_ctx*);
+    ctx->dp_called = true;
+
+    if (ctx->create_user) {
+        prepare_user(ctx, ctx->tctx->dom, 1000, time(NULL));
+    }
+
+    if (ctx->create_group) {
+        ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME,
+                                TEST_GROUP_ID, NULL, 1000, time(NULL));
+        assert_int_equal(ret, EOK);
+    }
+
+    return test_req_succeed_send(mem_ctx, rctx->ev);
+}
+
 static int test_single_domain_setup(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
@@ -243,83 +391,40 @@ void test_user_by_name_multiple_domains_found(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;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup user. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_user(domain, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    prepare_user(test_ctx, domain, 1000, time(NULL));
 
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
-    mock_parse_inp(name, NULL, ERR_OK);
-
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
+    mock_parse_inp(TEST_USER_NAME, NULL, ERR_OK);
 
+    /* Test. */
+    run_user_by_name(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_user(test_ctx, domain);
 }
 
 void test_user_by_name_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
-    mock_parse_inp(name, NULL, ERR_OK);
-
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
+    mock_parse_inp(TEST_USER_NAME, NULL, ERR_OK);
 
+    /* Test. */
+    run_user_by_name(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
@@ -331,45 +436,41 @@ void test_user_by_name_multiple_domains_parse(void **state)
     struct tevent_req *req = NULL;
     const char *name = TEST_USER_NAME;
     const char *fqn = NULL;
-    const char *ldbname = NULL;
-    uid_t uid = 2000;
-    uid_t ldbuid;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    /* Add user to the first domain. */
+    /* Add user to the first domain with different uid then test user. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_a", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_user(domain, name, "pwd", 1000, 1000,
+    ret = sysdb_store_user(domain, name, "pwd", 2000, 1000,
                            NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
                            NULL, 1000, time(NULL));
     assert_int_equal(ret, EOK);
 
-    /* Add user to the last domain, with different uid. */
+    /* Add test user to the last domain. */
 
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_user(domain, name, "pwd", uid, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    prepare_user(test_ctx, domain, 1000, time(NULL));
 
     /* Append domain name to the username. */
     fqn = talloc_asprintf(test_ctx, "%s@%s", name,
                           "responder_cache_req_test_d");
     assert_non_null(fqn);
 
+    /* Mock values. */
+    mock_parse_inp(name, "responder_cache_req_test_d", ERR_OK);
+
+
     /* Test. */
     req_mem_ctx = talloc_new(global_talloc_context);
     check_leaks_push(req_mem_ctx);
 
-    mock_parse_inp(name, "responder_cache_req_test_d", ERR_OK);
-
     req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
                                       test_ctx->rctx, test_ctx->ncache, 10, 0,
                                       NULL, fqn);
@@ -381,22 +482,7 @@ void test_user_by_name_multiple_domains_parse(void **state)
     assert_true(check_leaks_pop(req_mem_ctx));
     assert_false(test_ctx->dp_called);
 
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_user(test_ctx, domain);
 
     assert_non_null(test_ctx->name);
     assert_string_equal(name, test_ctx->name);
@@ -405,589 +491,257 @@ void test_user_by_name_multiple_domains_parse(void **state)
 void test_user_by_name_cache_valid(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_name_cache_expired(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, -1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
 
+    /* Mock values. */
     /* DP should be contacted */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_name_cache_midpoint(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 50, time(NULL) - 26);
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
 
+    /* Mock values. */
     /* DP should be contacted without callback */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 50,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_name_ncache(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup user. */
     ret = sss_ncache_set_user(test_ctx->ncache, false,
-                              test_ctx->tctx->dom, name);
+                              test_ctx->tctx->dom, TEST_USER_NAME);
     assert_int_equal(ret, EOK);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_false(test_ctx->dp_called);
 }
 
 void test_user_by_name_missing_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
     test_ctx->create_user = true;
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_name_missing_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
 void test_user_by_upn_multiple_domains_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    struct sysdb_attrs *attrs = NULL;
     struct sss_domain_info *domain = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *upn = TEST_UPN;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup user. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
 
-    attrs = sysdb_new_attrs(test_ctx);
-    assert_non_null(attrs);
-
-    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, upn);
-    assert_int_equal(ret, EOK);
-
-    ret = sysdb_store_user(domain, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", attrs,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    prepare_user(test_ctx, domain, 1000, time(NULL));
 
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_user(test_ctx, domain);
 }
 
 void test_user_by_upn_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *upn = TEST_UPN;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
 void test_user_by_upn_cache_valid(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    struct sysdb_attrs *attrs = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *upn = TEST_UPN;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    attrs = sysdb_new_attrs(test_ctx);
-    assert_non_null(attrs);
-
-    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, upn);
-    assert_int_equal(ret, EOK);
-
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", attrs,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
 
+    /* Mock values. */
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_upn_cache_expired(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    struct sysdb_attrs *attrs = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *upn = TEST_UPN;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    attrs = sysdb_new_attrs(test_ctx);
-    assert_non_null(attrs);
-
-    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, upn);
-    assert_int_equal(ret, EOK);
-
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", attrs,
-                           NULL, -1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
 
+    /* Mock values. */
     /* DP should be contacted */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_upn_cache_midpoint(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    struct sysdb_attrs *attrs = NULL;
-    const char *upn = TEST_UPN;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    attrs = sysdb_new_attrs(test_ctx);
-    assert_non_null(attrs);
-
-    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, upn);
-    assert_int_equal(ret, EOK);
-
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", 1000, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", attrs,
-                           NULL, 50, time(NULL) - 26);
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
 
+    /* Mock values. */
     /* DP should be contacted without callback */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 10, 50,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 50, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_upn_ncache(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *upn = TEST_UPN;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup user. */
     ret = sss_ncache_set_user(test_ctx->ncache, false,
-                              test_ctx->tctx->dom, upn);
+                              test_ctx->tctx->dom, TEST_UPN);
     assert_int_equal(ret, EOK);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ENOENT);
     assert_false(test_ctx->dp_called);
 }
 
 void test_user_by_upn_missing_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *upn = TEST_UPN;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
     test_ctx->create_user = true;
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_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));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_upn_missing_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *upn = TEST_UPN;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
     mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
 
-    req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                      test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                      NULL, upn);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_upn(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
@@ -995,342 +749,140 @@ void test_user_by_id_multiple_domains_found(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;
-    const char *name = TEST_USER_NAME;
-    uid_t uid = TEST_USER_ID;
-    const char *ldbname = NULL;
-    uid_t ldbuid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup user. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_user(domain, name, "pwd", uid, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    prepare_user(test_ctx, domain, 1000, time(NULL));
 
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                    NULL, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_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));
-
+    /* Test. */
+    run_user_by_id(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_user(test_ctx, domain);
 }
 
 void test_user_by_id_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    uid_t uid = TEST_USER_ID;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                    NULL, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_id(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
 void test_user_by_id_cache_valid(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    uid_t uid = TEST_USER_ID;
-    uid_t ldbuid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", uid, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_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);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_id_cache_expired(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    uid_t uid = TEST_USER_ID;
-    uid_t ldbuid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", uid, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, -1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    /* DP should be contacted */
+    /* Mock values. */
+    /* DP should be contacted. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_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));
-
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_id_cache_midpoint(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    uid_t uid = TEST_USER_ID;
-    uid_t ldbuid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_user(test_ctx->tctx->dom, name, "pwd", uid, 1000,
-                           NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
-                           NULL, 50, time(NULL) - 26);
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup user. */
+    prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
 
+    /* Mock values. */
     /* DP should be contacted without callback */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 10, 50,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_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));
-
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_id_ncache(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    uid_t uid = TEST_USER_ID;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sss_ncache_set_uid(test_ctx->ncache, false, NULL, uid);
+    /* Setup user. */
+    ret = sss_ncache_set_uid(test_ctx->ncache, false, NULL, TEST_USER_ID);
     assert_int_equal(ret, EOK);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_false(test_ctx->dp_called);
 }
 
 void test_user_by_id_missing_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_USER_NAME;
-    const char *ldbname = NULL;
-    uid_t uid = TEST_USER_ID;
-    uid_t ldbuid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
     test_ctx->create_user = true;
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_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));
-
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_UIDNUM, 0);
-    assert_int_equal(ldbuid, uid);
+    check_user(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_user_by_id_missing_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    uid_t uid = TEST_USER_ID;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_user_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                    test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                    test_ctx->tctx->dom->name, uid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_user_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
@@ -1338,82 +890,39 @@ void test_group_by_name_multiple_domains_found(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;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup group. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
+    prepare_group(test_ctx, domain, 1000, time(NULL));
 
-    ret = sysdb_store_group(domain, name, TEST_GROUP_ID, NULL,
-                            1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
-    mock_parse_inp(name, NULL, ERR_OK);
-
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                       NULL, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_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));
+    mock_parse_inp(TEST_GROUP_NAME, NULL, ERR_OK);
 
+    /* Test. */
+    run_group_by_name(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_group(test_ctx, domain);
 }
 
 void test_group_by_name_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
-    mock_parse_inp(name, NULL, ERR_OK);
-
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                       NULL, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
+    mock_parse_inp(TEST_GROUP_NAME, NULL, ERR_OK);
 
+    /* Test. */
+    run_group_by_name(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
@@ -1425,31 +934,26 @@ void test_group_by_name_multiple_domains_parse(void **state)
     struct tevent_req *req = NULL;
     const char *name = TEST_GROUP_NAME;
     const char *fqn = NULL;
-    const char *ldbname = NULL;
-    uid_t gid = 2000;
-    uid_t ldbgid;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    /* Add user to the first domain. */
+    /* Add group to the first domain. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_a", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_group(domain, name, 1000, NULL,
+    ret = sysdb_store_group(domain, name, 2000, NULL,
                             1000, time(NULL));
     assert_int_equal(ret, EOK);
 
-    /* Add user to the last domain, with different uid. */
+    /* Add group to the last domain, with different gid. */
 
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
 
-    ret = sysdb_store_group(domain, name, gid, NULL,
-                            1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    prepare_group(test_ctx, domain, 1000, time(NULL));
 
     /* Append domain name to the username. */
     fqn = talloc_asprintf(test_ctx, "%s@%s", name,
@@ -1473,22 +977,7 @@ void test_group_by_name_multiple_domains_parse(void **state)
     assert_true(check_leaks_pop(req_mem_ctx));
     assert_false(test_ctx->dp_called);
 
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_group(test_ctx, domain);
 
     assert_non_null(test_ctx->name);
     assert_string_equal(name, test_ctx->name);
@@ -1497,229 +986,103 @@ void test_group_by_name_multiple_domains_parse(void **state)
 void test_group_by_name_cache_valid(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, TEST_GROUP_ID, NULL,
-                            1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_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);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_name_cache_expired(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, TEST_GROUP_ID, NULL,
-                            -1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
 
+    /* Mock values. */
     /* DP should be contacted */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_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));
-
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_name_cache_midpoint(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, TEST_GROUP_ID, NULL,
-                            50, time(NULL) - 26);
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
 
+    /* Mock values. */
     /* DP should be contacted without callback */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
 
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 10, 50,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_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));
-
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_name_ncache(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup group. */
     ret = sss_ncache_set_group(test_ctx->ncache, false,
-                               test_ctx->tctx->dom, name);
+                               test_ctx->tctx->dom, TEST_GROUP_NAME);
     assert_int_equal(ret, EOK);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_false(test_ctx->dp_called);
 }
 
 void test_group_by_name_missing_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
     test_ctx->create_group = true;
 
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_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));
-
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_name_missing_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
-                                       test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                       test_ctx->tctx->dom->name, name);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_name_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
@@ -1727,338 +1090,139 @@ void test_group_by_id_multiple_domains_found(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;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    gid_t ldbgid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
+    /* Setup group. */
     domain = find_domain_by_name(test_ctx->tctx->dom,
                                  "responder_cache_req_test_d", true);
     assert_non_null(domain);
+    prepare_group(test_ctx, domain, 1000, time(NULL));
 
-    ret = sysdb_store_group(domain, name, gid, NULL,
-                            1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                     NULL, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_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));
-
+    /* Test. */
+    run_group_by_id(test_ctx, NULL, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
-
-    assert_non_null(test_ctx->domain);
-    assert_string_equal(domain->name, test_ctx->domain->name);
+    check_group(test_ctx, domain);
 }
 
 void test_group_by_id_multiple_domains_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
     will_return_always(sss_dp_get_account_recv, 0);
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                     NULL, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_group_by_id(test_ctx, NULL, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
 void test_group_by_id_cache_valid(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    gid_t ldbgid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, gid, NULL,
-                            1000, time(NULL));
-    assert_int_equal(ret, EOK);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_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);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_id_cache_expired(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    gid_t ldbgid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, gid, NULL,
-                            -1000, time(NULL));
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
 
+    /* Mock values. */
     /* DP should be contacted */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 10, 0,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_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));
-
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_id_cache_midpoint(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    gid_t ldbgid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sysdb_store_group(test_ctx->tctx->dom, name, gid, NULL,
-                            50, time(NULL) - 26);
-    assert_int_equal(ret, EOK);
-
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
+    /* Setup group. */
+    prepare_group(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
 
+    /* Mock values. */
     /* DP should be contacted without callback */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 10, 50,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_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));
-
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_id_ncache(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    gid_t gid = TEST_GROUP_ID;
     errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    ret = sss_ncache_set_gid(test_ctx->ncache, false, NULL, gid);
+    /* Setup group. */
+    ret = sss_ncache_set_gid(test_ctx->ncache, false, NULL, TEST_GROUP_ID);
     assert_int_equal(ret, EOK);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_false(test_ctx->dp_called);
 }
 
 void test_group_by_id_missing_found(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    const char *name = TEST_GROUP_NAME;
-    const char *ldbname = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    gid_t ldbgid;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
     test_ctx->create_group = true;
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_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));
-
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
     assert_true(test_ctx->dp_called);
-
-    assert_non_null(test_ctx->result);
-    assert_int_equal(test_ctx->result->count, 1);
-    assert_non_null(test_ctx->result->msgs);
-    assert_non_null(test_ctx->result->msgs[0]);
-
-    ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
-                                          SYSDB_NAME, NULL);
-    assert_non_null(ldbname);
-    assert_string_equal(ldbname, name);
-
-    ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
-                                       SYSDB_GIDNUM, 0);
-    assert_int_equal(ldbgid, gid);
+    check_group(test_ctx, test_ctx->tctx->dom);
 }
 
 void test_group_by_id_missing_notfound(void **state)
 {
     struct cache_req_test_ctx *test_ctx = NULL;
-    TALLOC_CTX *req_mem_ctx = NULL;
-    struct tevent_req *req = NULL;
-    gid_t gid = TEST_GROUP_ID;
-    errno_t ret;
 
     test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
 
-    req_mem_ctx = talloc_new(global_talloc_context);
-    check_leaks_push(req_mem_ctx);
-
+    /* Mock values. */
     will_return(__wrap_sss_dp_get_account_send, test_ctx);
     mock_account_recv_simple();
 
-    req = cache_req_group_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
-                                     test_ctx->rctx, test_ctx->ncache, 100, 0,
-                                     test_ctx->tctx->dom->name, gid);
-    assert_non_null(req);
-    tevent_req_set_callback(req, cache_req_group_by_id_test_done, test_ctx);
-
-    ret = test_ev_loop(test_ctx->tctx);
-    assert_int_equal(ret, ENOENT);
-    assert_true(check_leaks_pop(req_mem_ctx));
-
+    /* Test. */
+    run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
     assert_true(test_ctx->dp_called);
 }
 
-- 
2.1.0

