dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 9a4c06ddf5ec8d610f49acf5d3e231d36b37c50b Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Sun, 19 Nov 2017 17:25:02 +0100
Subject: [PATCH 77/83] TESTS: Add tests for the object-by-id cache_req
 interface
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This interface will be extended in later patches, but had no tests at
all.

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
Reviewed-by: Sumit Bose <sbose@redhat.com>
(cherry picked from commit 6cd367da68ff56eb48b8b4167dbdd5e53992d194)
---
 src/tests/cmocka/test_responder_cache_req.c | 385 ++++++++++++++++++++++++++++
 1 file changed, 385 insertions(+)

diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
index 80086232fd437876c2b190fb972c2ee3194d9efd..f075480a019e476407a3081a795c3c289455aca8 100644
--- a/src/tests/cmocka/test_responder_cache_req.c
+++ b/src/tests/cmocka/test_responder_cache_req.c
@@ -197,6 +197,18 @@ static void cache_req_object_by_sid_test_done(struct tevent_req *req)
     ctx->tctx->done = true;
 }
 
+static void cache_req_object_by_id_test_done(struct tevent_req *req)
+{
+    struct cache_req_test_ctx *ctx = NULL;
+
+    ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
+
+    ctx->tctx->error = cache_req_object_by_id_recv(ctx, req, &ctx->result);
+    talloc_zfree(req);
+
+    ctx->tctx->done = true;
+}
+
 static void prepare_user(struct sss_domain_info *domain,
                          struct test_user *user,
                          uint64_t timeout,
@@ -417,6 +429,33 @@ static void run_object_by_sid(struct cache_req_test_ctx *test_ctx,
     talloc_free(req_mem_ctx);
 }
 
+static void run_object_by_id(struct cache_req_test_ctx *test_ctx,
+                             struct sss_domain_info *domain,
+                             id_t id,
+                             const char **attrs,
+                             int cache_refresh_percent,
+                             errno_t exp_ret)
+{
+    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 = cache_req_object_by_id_send(req_mem_ctx, test_ctx->tctx->ev,
+            test_ctx->rctx, test_ctx->ncache, cache_refresh_percent,
+            (domain == NULL ? NULL : domain->name), id, attrs);
+    assert_non_null(req);
+    tevent_req_set_callback(req, cache_req_object_by_id_test_done, test_ctx);
+
+    ret = test_ev_loop(test_ctx->tctx);
+    assert_int_equal(ret, exp_ret);
+    assert_true(check_leaks_pop(req_mem_ctx));
+
+    talloc_free(req_mem_ctx);
+}
+
 struct tevent_req *
 __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
                                struct resp_ctx *rctx,
@@ -2132,6 +2171,334 @@ void test_object_by_sid_group_multiple_domains_notfound(void **state)
     assert_true(test_ctx->dp_called);
 }
 
+void test_object_by_id_user_cache_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_user(test_ctx->tctx->dom, &users[0], 1000, time(NULL));
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_user_cache_expired(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_user(test_ctx->tctx->dom, &users[0], -1000, time(NULL));
+
+    /* Mock values. */
+    /* DP should be contacted */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_user_cache_midpoint(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_user(test_ctx->tctx->dom, &users[0], 50, time(NULL) - 26);
+
+    /* Mock values. */
+    /* DP should be contacted without callback */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 50, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_user_ncache(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. We explicitly add the UID into BOTH UID and GID
+     * namespaces, because otherwise the cache_req plugin would
+     * search the Data Provider anyway, becase it can't be sure
+     * the object can be of the other type or not
+     */
+    ret = sss_ncache_set_uid(test_ctx->ncache,
+                             false,
+                             test_ctx->tctx->dom,
+                             users[0].uid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_set_gid(test_ctx->ncache,
+                             false,
+                             test_ctx->tctx->dom,
+                             users[0].uid);
+    assert_int_equal(ret, EOK);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
+    assert_false(test_ctx->dp_called);
+}
+
+void test_object_by_id_user_missing_found(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    test_ctx->create_user1 = true;
+    test_ctx->create_user2 = false;
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_user(test_ctx, &users[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_user_missing_notfound(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
+    assert_true(test_ctx->dp_called);
+}
+
+void test_object_by_id_user_multiple_domains_found(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    struct sss_domain_info *domain = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    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);
+
+    prepare_user(domain, &users[0], 1000, time(NULL));
+
+    /* Mock values. */
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
+    will_return_always(sss_dp_req_recv, 0);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_user(test_ctx, &users[0], domain);
+}
+
+void test_object_by_id_user_multiple_domains_notfound(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_PW_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
+    will_return_always(sss_dp_req_recv, 0);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, users[0].uid, attrs, 0, ENOENT);
+    assert_true(test_ctx->dp_called);
+}
+
+void test_object_by_id_group_cache_valid(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_group(test_ctx->tctx->dom, &groups[0], 1000, time(NULL));
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_group_cache_expired(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_group(test_ctx->tctx->dom, &groups[0], -1000, time(NULL));
+
+    /* Mock values. */
+    /* DP should be contacted */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_group_cache_midpoint(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup user. */
+    prepare_group(test_ctx->tctx->dom, &groups[0], 50, time(NULL) - 26);
+
+    /* Mock values. */
+    /* DP should be contacted without callback */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 50, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_group_ncache(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Setup group. We explicitly add the UID into BOTH UID and GID
+     * namespaces, because otherwise the cache_req plugin would
+     * search the Data Provider anyway, becase it can't be sure
+     * the object can be of the other type or not
+     */
+    ret = sss_ncache_set_uid(test_ctx->ncache,
+                             false,
+                             test_ctx->tctx->dom,
+                             groups[0].gid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_set_gid(test_ctx->ncache,
+                             false,
+                             test_ctx->tctx->dom,
+                             groups[0].gid);
+    assert_int_equal(ret, EOK);
+
+    assert_int_equal(ret, EOK);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
+    assert_false(test_ctx->dp_called);
+}
+
+void test_object_by_id_group_missing_found(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    test_ctx->create_group1 = true;
+    test_ctx->create_group2 = false;
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_group(test_ctx, &groups[0], test_ctx->tctx->dom);
+}
+
+void test_object_by_id_group_missing_notfound(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return(__wrap_sss_dp_get_account_send, test_ctx);
+    mock_account_recv_simple();
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
+    assert_true(test_ctx->dp_called);
+}
+
+void test_object_by_id_group_multiple_domains_found(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    struct sss_domain_info *domain = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    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);
+
+    prepare_group(domain, &groups[0], 1000, time(NULL));
+
+    /* Mock values. */
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
+    will_return_always(sss_dp_req_recv, 0);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ERR_OK);
+    assert_true(test_ctx->dp_called);
+    check_group(test_ctx, &groups[0], domain);
+}
+
+void test_object_by_id_group_multiple_domains_notfound(void **state)
+{
+    struct cache_req_test_ctx *test_ctx = NULL;
+    const char *attrs[] = SYSDB_GRSRC_ATTRS;
+
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
+
+    /* Mock values. */
+    will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
+    will_return_always(sss_dp_req_recv, 0);
+
+    /* Test. */
+    run_object_by_id(test_ctx, NULL, groups[0].gid, attrs, 0, ENOENT);
+    assert_true(test_ctx->dp_called);
+}
+
 int main(int argc, const char *argv[])
 {
     poptContext pc;
@@ -2218,6 +2585,24 @@ int main(int argc, const char *argv[])
         new_single_domain_test(object_by_sid_group_missing_notfound),
         new_multi_domain_test(object_by_sid_group_multiple_domains_found),
         new_multi_domain_test(object_by_sid_group_multiple_domains_notfound),
+
+        new_single_domain_test(object_by_id_user_cache_valid),
+        new_single_domain_test(object_by_id_user_cache_expired),
+        new_single_domain_test(object_by_id_user_cache_midpoint),
+        new_single_domain_test(object_by_id_user_ncache),
+        new_single_domain_test(object_by_id_user_missing_found),
+        new_single_domain_test(object_by_id_user_missing_notfound),
+        new_multi_domain_test(object_by_id_user_multiple_domains_found),
+        new_multi_domain_test(object_by_id_user_multiple_domains_notfound),
+
+        new_single_domain_test(object_by_id_group_cache_valid),
+        new_single_domain_test(object_by_id_group_cache_expired),
+        new_single_domain_test(object_by_id_group_cache_midpoint),
+        new_single_domain_test(object_by_id_group_ncache),
+        new_single_domain_test(object_by_id_group_missing_found),
+        new_single_domain_test(object_by_id_group_missing_notfound),
+        new_multi_domain_test(object_by_id_group_multiple_domains_found),
+        new_multi_domain_test(object_by_id_group_multiple_domains_notfound),
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
-- 
2.14.3