Blame SOURCES/0077-TESTS-Add-tests-for-the-object-by-id-cache_req-inter.patch

ced1f5
From 9a4c06ddf5ec8d610f49acf5d3e231d36b37c50b Mon Sep 17 00:00:00 2001
ced1f5
From: Jakub Hrozek <jhrozek@redhat.com>
ced1f5
Date: Sun, 19 Nov 2017 17:25:02 +0100
ced1f5
Subject: [PATCH 77/83] TESTS: Add tests for the object-by-id cache_req
ced1f5
 interface
ced1f5
MIME-Version: 1.0
ced1f5
Content-Type: text/plain; charset=UTF-8
ced1f5
Content-Transfer-Encoding: 8bit
ced1f5
ced1f5
This interface will be extended in later patches, but had no tests at
ced1f5
all.
ced1f5
ced1f5
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ced1f5
Reviewed-by: Sumit Bose <sbose@redhat.com>
ced1f5
(cherry picked from commit 6cd367da68ff56eb48b8b4167dbdd5e53992d194)
ced1f5
---
ced1f5
 src/tests/cmocka/test_responder_cache_req.c | 385 ++++++++++++++++++++++++++++
ced1f5
 1 file changed, 385 insertions(+)
ced1f5
ced1f5
diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
ced1f5
index 80086232fd437876c2b190fb972c2ee3194d9efd..f075480a019e476407a3081a795c3c289455aca8 100644
ced1f5
--- a/src/tests/cmocka/test_responder_cache_req.c
ced1f5
+++ b/src/tests/cmocka/test_responder_cache_req.c
ced1f5
@@ -197,6 +197,18 @@ static void cache_req_object_by_sid_test_done(struct tevent_req *req)
ced1f5
     ctx->tctx->done = true;
ced1f5
 }
ced1f5
 
ced1f5
+static void cache_req_object_by_id_test_done(struct tevent_req *req)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *ctx = NULL;
ced1f5
+
ced1f5
+    ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    ctx->tctx->error = cache_req_object_by_id_recv(ctx, req, &ctx->result);
ced1f5
+    talloc_zfree(req);
ced1f5
+
ced1f5
+    ctx->tctx->done = true;
ced1f5
+}
ced1f5
+
ced1f5
 static void prepare_user(struct sss_domain_info *domain,
ced1f5
                          struct test_user *user,
ced1f5
                          uint64_t timeout,
ced1f5
@@ -417,6 +429,33 @@ static void run_object_by_sid(struct cache_req_test_ctx *test_ctx,
ced1f5
     talloc_free(req_mem_ctx);
ced1f5
 }
ced1f5
 
ced1f5
+static void run_object_by_id(struct cache_req_test_ctx *test_ctx,
ced1f5
+                             struct sss_domain_info *domain,
ced1f5
+                             id_t id,
ced1f5
+                             const char **attrs,
ced1f5
+                             int cache_refresh_percent,
ced1f5
+                             errno_t exp_ret)
ced1f5
+{
ced1f5
+    TALLOC_CTX *req_mem_ctx;
ced1f5
+    struct tevent_req *req;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    req_mem_ctx = talloc_new(global_talloc_context);
ced1f5
+    check_leaks_push(req_mem_ctx);
ced1f5
+
ced1f5
+    req = cache_req_object_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
ced1f5
+            test_ctx->rctx, test_ctx->ncache, cache_refresh_percent,
ced1f5
+            (domain == NULL ? NULL : domain->name), id, attrs);
ced1f5
+    assert_non_null(req);
ced1f5
+    tevent_req_set_callback(req, cache_req_object_by_id_test_done, test_ctx);
ced1f5
+
ced1f5
+    ret = test_ev_loop(test_ctx->tctx);
ced1f5
+    assert_int_equal(ret, exp_ret);
ced1f5
+    assert_true(check_leaks_pop(req_mem_ctx));
ced1f5
+
ced1f5
+    talloc_free(req_mem_ctx);
ced1f5
+}
ced1f5
+
ced1f5
 struct tevent_req *
ced1f5
 __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
ced1f5
                                struct resp_ctx *rctx,
ced1f5
@@ -2132,6 +2171,334 @@ void test_object_by_sid_group_multiple_domains_notfound(void **state)
ced1f5
     assert_true(test_ctx->dp_called);
ced1f5
 }
ced1f5
 
ced1f5
+void test_object_by_id_user_cache_valid(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_user(test_ctx->tctx->dom, &users[0], 1000, time(NULL));
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
ced1f5
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_cache_expired(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_user(test_ctx->tctx->dom, &users[0], -1000, time(NULL));
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    /* DP should be contacted */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_cache_midpoint(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_user(test_ctx->tctx->dom, &users[0], 50, time(NULL) - 26);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    /* DP should be contacted without callback */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 50, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_ncache(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. We explicitly add the UID into BOTH UID and GID
ced1f5
+     * namespaces, because otherwise the cache_req plugin would
ced1f5
+     * search the Data Provider anyway, becase it can't be sure
ced1f5
+     * the object can be of the other type or not
ced1f5
+     */
ced1f5
+    ret = sss_ncache_set_uid(test_ctx->ncache,
ced1f5
+                             false,
ced1f5
+                             test_ctx->tctx->dom,
ced1f5
+                             users[0].uid);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    ret = sss_ncache_set_gid(test_ctx->ncache,
ced1f5
+                             false,
ced1f5
+                             test_ctx->tctx->dom,
ced1f5
+                             users[0].uid);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
ced1f5
+    assert_false(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_missing_found(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    test_ctx->create_user1 = true;
ced1f5
+    test_ctx->create_user2 = false;
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_missing_notfound(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_multiple_domains_found(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    struct sss_domain_info *domain = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    domain = find_domain_by_name(test_ctx->tctx->dom,
ced1f5
+                                 "responder_cache_req_test_d", true);
ced1f5
+    assert_non_null(domain);
ced1f5
+
ced1f5
+    prepare_user(domain, &users[0], 1000, time(NULL));
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    will_return_always(sss_dp_req_recv, 0);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_user(test_ctx, &users[0], domain);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_user_multiple_domains_notfound(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_PW_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    will_return_always(sss_dp_req_recv, 0);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_cache_valid(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_group(test_ctx->tctx->dom, &groups[0], 1000, time(NULL));
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
ced1f5
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_cache_expired(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_group(test_ctx->tctx->dom, &groups[0], -1000, time(NULL));
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    /* DP should be contacted */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_cache_midpoint(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    prepare_group(test_ctx->tctx->dom, &groups[0], 50, time(NULL) - 26);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    /* DP should be contacted without callback */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 50, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_ncache(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup group. We explicitly add the UID into BOTH UID and GID
ced1f5
+     * namespaces, because otherwise the cache_req plugin would
ced1f5
+     * search the Data Provider anyway, becase it can't be sure
ced1f5
+     * the object can be of the other type or not
ced1f5
+     */
ced1f5
+    ret = sss_ncache_set_uid(test_ctx->ncache,
ced1f5
+                             false,
ced1f5
+                             test_ctx->tctx->dom,
ced1f5
+                             groups[0].gid);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    ret = sss_ncache_set_gid(test_ctx->ncache,
ced1f5
+                             false,
ced1f5
+                             test_ctx->tctx->dom,
ced1f5
+                             groups[0].gid);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
ced1f5
+    assert_false(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_missing_found(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    test_ctx->create_group1 = true;
ced1f5
+    test_ctx->create_group2 = false;
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_missing_notfound(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    mock_account_recv_simple();
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_multiple_domains_found(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    struct sss_domain_info *domain = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Setup user. */
ced1f5
+    domain = find_domain_by_name(test_ctx->tctx->dom,
ced1f5
+                                 "responder_cache_req_test_d", true);
ced1f5
+    assert_non_null(domain);
ced1f5
+
ced1f5
+    prepare_group(domain, &groups[0], 1000, time(NULL));
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    will_return_always(sss_dp_req_recv, 0);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+    check_group(test_ctx, &groups[0], domain);
ced1f5
+}
ced1f5
+
ced1f5
+void test_object_by_id_group_multiple_domains_notfound(void **state)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
ced1f5
+
ced1f5
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
ced1f5
+
ced1f5
+    /* Mock values. */
ced1f5
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
ced1f5
+    will_return_always(sss_dp_req_recv, 0);
ced1f5
+
ced1f5
+    /* Test. */
ced1f5
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
ced1f5
+    assert_true(test_ctx->dp_called);
ced1f5
+}
ced1f5
+
ced1f5
 int main(int argc, const char *argv[])
ced1f5
 {
ced1f5
     poptContext pc;
ced1f5
@@ -2218,6 +2585,24 @@ int main(int argc, const char *argv[])
ced1f5
         new_single_domain_test(object_by_sid_group_missing_notfound),
ced1f5
         new_multi_domain_test(object_by_sid_group_multiple_domains_found),
ced1f5
         new_multi_domain_test(object_by_sid_group_multiple_domains_notfound),
ced1f5
+
ced1f5
+        new_single_domain_test(object_by_id_user_cache_valid),
ced1f5
+        new_single_domain_test(object_by_id_user_cache_expired),
ced1f5
+        new_single_domain_test(object_by_id_user_cache_midpoint),
ced1f5
+        new_single_domain_test(object_by_id_user_ncache),
ced1f5
+        new_single_domain_test(object_by_id_user_missing_found),
ced1f5
+        new_single_domain_test(object_by_id_user_missing_notfound),
ced1f5
+        new_multi_domain_test(object_by_id_user_multiple_domains_found),
ced1f5
+        new_multi_domain_test(object_by_id_user_multiple_domains_notfound),
ced1f5
+
ced1f5
+        new_single_domain_test(object_by_id_group_cache_valid),
ced1f5
+        new_single_domain_test(object_by_id_group_cache_expired),
ced1f5
+        new_single_domain_test(object_by_id_group_cache_midpoint),
ced1f5
+        new_single_domain_test(object_by_id_group_ncache),
ced1f5
+        new_single_domain_test(object_by_id_group_missing_found),
ced1f5
+        new_single_domain_test(object_by_id_group_missing_notfound),
ced1f5
+        new_multi_domain_test(object_by_id_group_multiple_domains_found),
ced1f5
+        new_multi_domain_test(object_by_id_group_multiple_domains_notfound),
ced1f5
     };
ced1f5
 
ced1f5
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
ced1f5
-- 
ced1f5
2.14.3
ced1f5