URL: https://github.com/SSSD/sssd/pull/45
Author: jhrozek
 Title: #45: tests: Add unit tests for UPNs in SSS_NSS_GETSIDBYNAME and 
SSS_NSS_GETORIGBYNAME
Action: opened

PR body:
"""
This is what I used to test recent Sumit's patches for ticket #3194.
"""

To pull the PR as Git branch:
git remote add ghsssd https://github.com/SSSD/sssd
git fetch ghsssd pull/45/head:pr45
git checkout pr45
From b3f4b9b94d089ee9f05fe5045a29893aa86e644f Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhro...@redhat.com>
Date: Mon, 26 Sep 2016 10:44:16 +0200
Subject: [PATCH 1/2] tests: Add tests for sidbyname NSS operation

---
 src/tests/cmocka/test_nss_srv.c | 129 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
index 41425e7..8e9f5fe 100644
--- a/src/tests/cmocka/test_nss_srv.c
+++ b/src/tests/cmocka/test_nss_srv.c
@@ -3446,6 +3446,129 @@ void test_nss_getnamebycert_neg(void **state)
     assert_int_equal(nss_test_ctx->ncache_hits, 1);
 }
 
+struct passwd sid_user = {
+    .pw_name = discard_const("testusersid"),
+    .pw_uid = 1234,
+    .pw_gid = 5678,
+    .pw_dir = discard_const("/home/testusersid"),
+    .pw_gecos = discard_const("test user"),
+    .pw_shell = discard_const("/bin/sh"),
+    .pw_passwd = discard_const("*"),
+};
+
+static int test_nss_getsidbyname_check(uint32_t status,
+                                       uint8_t *body,
+                                       size_t blen)
+{
+    const char *name;
+    enum sss_id_type type;
+    size_t rp = 2 * sizeof(uint32_t);
+    char *expected_result = sss_mock_ptr_type(char *);
+
+    if (expected_result == NULL) {
+        assert_int_equal(status, EINVAL);
+        assert_int_equal(blen, 0);
+    } else {
+        assert_int_equal(status, EOK);
+
+        SAFEALIGN_COPY_UINT32(&type, body+rp, &rp);
+
+        name = (char *) body+rp;
+
+        assert_int_equal(type, SSS_ID_TYPE_UID);
+        assert_string_equal(name, expected_result);
+    }
+
+    return EOK;
+}
+
+void test_nss_getsidbyname(void **state)
+{
+    errno_t ret;
+    struct sysdb_attrs *attrs;
+    const char *testuser_sid = "S-1-2-3-4";
+
+    attrs = sysdb_new_attrs(nss_test_ctx);
+    assert_non_null(attrs);
+
+    ret = sysdb_attrs_add_string(attrs, SYSDB_SID_STR, testuser_sid);
+    assert_int_equal(ret, EOK);
+
+    ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
+                     &sid_user, attrs, 0);
+    assert_int_equal(ret, EOK);
+
+    mock_input_user_or_group("testusersid");
+    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETSIDBYNAME);
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
+
+    will_return(test_nss_getsidbyname_check, testuser_sid);
+
+    /* Query for that user, call a callback when command finishes */
+    set_cmd_cb(test_nss_getsidbyname_check);
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETSIDBYNAME,
+                          nss_test_ctx->nss_cmds);
+    assert_int_equal(ret, EOK);
+
+    /* Wait until the test finishes with EOK */
+    ret = test_ev_loop(nss_test_ctx->tctx);
+    assert_int_equal(ret, EOK);
+}
+
+void test_nss_getsidbyupn(void **state)
+{
+    errno_t ret;
+    struct sysdb_attrs *attrs;
+    const char *testuser_sid = "S-1-2-3-4";
+    const char *testuser_upn = "testuser...@upndomain.test";
+
+    attrs = sysdb_new_attrs(nss_test_ctx);
+    assert_non_null(attrs);
+
+    ret = sysdb_attrs_add_string(attrs, SYSDB_SID_STR, testuser_sid);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, testuser_upn);
+    assert_int_equal(ret, EOK);
+
+    ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
+                     &sid_user, attrs, 0);
+    assert_int_equal(ret, EOK);
+
+    mock_input_user_or_group(testuser_upn);
+    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETSIDBYNAME);
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
+
+    will_return(test_nss_getsidbyname_check, testuser_sid);
+
+    /* Query for that user, call a callback when command finishes */
+    set_cmd_cb(test_nss_getsidbyname_check);
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETSIDBYNAME,
+                          nss_test_ctx->nss_cmds);
+    assert_int_equal(ret, EOK);
+
+    /* Wait until the test finishes with EOK */
+    ret = test_ev_loop(nss_test_ctx->tctx);
+    assert_int_equal(ret, EOK);
+}
+
+void test_nss_getsidbyname_neg(void **state)
+{
+    errno_t ret;
+
+    mock_input_user_or_group("testnosuchsid");
+    mock_account_recv_simple();
+
+    /* Query for that user, call a callback when command finishes */
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETSIDBYNAME,
+                          nss_test_ctx->nss_cmds);
+    assert_int_equal(ret, EOK);
+
+    /* Wait until the test finishes with ENOENT (because there is no such SID */
+    ret = test_ev_loop(nss_test_ctx->tctx);
+    assert_int_equal(ret, ENOENT);
+}
+
 int main(int argc, const char *argv[])
 {
     int rv;
@@ -3563,6 +3686,12 @@ int main(int argc, const char *argv[])
                                         nss_test_setup, nss_test_teardown),
         cmocka_unit_test_setup_teardown(test_nss_getnamebycert,
                                         nss_test_setup, nss_test_teardown),
+        cmocka_unit_test_setup_teardown(test_nss_getsidbyname,
+                                        nss_test_setup, nss_test_teardown),
+        cmocka_unit_test_setup_teardown(test_nss_getsidbyupn,
+                                        nss_test_setup, nss_test_teardown),
+        cmocka_unit_test_setup_teardown(test_nss_getsidbyname_neg,
+                                        nss_test_setup, nss_test_teardown),
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */

From 78f3debc488fd63688de15cce5aff96dd3adc962 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhro...@redhat.com>
Date: Mon, 26 Sep 2016 11:03:33 +0200
Subject: [PATCH 2/2] tests: Add tests for getorig by UPN NSS op

---
 src/tests/cmocka/test_nss_srv.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
index 8e9f5fe..f41c342 100644
--- a/src/tests/cmocka/test_nss_srv.c
+++ b/src/tests/cmocka/test_nss_srv.c
@@ -2130,7 +2130,17 @@ static int test_nss_getorigbyname_check(uint32_t status, uint8_t *body,
     s = (char *) body+rp;
     assert_string_equal(s, "1234");
     rp += strlen(s) + 1;
-    assert_int_equal(rp, blen);
+    assert_true(rp < blen);
+
+    s = (char *) body+rp;
+    assert_string_equal(s, SYSDB_UPN);
+    rp += strlen(s) + 1;
+    assert_true(rp < blen);
+
+    s = (char *) body+rp;
+    assert_string_equal(s, "testusero...@upndomain.test");
+    rp += strlen(s) + 1;
+    assert_true(rp = blen);
 
     return EOK;
 }
@@ -2149,6 +2159,7 @@ void test_nss_getorigbyname(void **state)
 {
     errno_t ret;
     struct sysdb_attrs *attrs;
+    const char *test_upn = "testusero...@upndomain.test";
 
     attrs = sysdb_new_attrs(nss_test_ctx);
     assert_non_null(attrs);
@@ -2163,6 +2174,9 @@ void test_nss_getorigbyname(void **state)
     ret = sysdb_attrs_add_uint32(attrs, ORIGINALAD_PREFIX SYSDB_UIDNUM, 1234);
     assert_int_equal(ret, EOK);
 
+    ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, test_upn);
+    assert_int_equal(ret, EOK);
+
     /* Prime the cache with a valid user */
     ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
                      &orig_name, attrs, 0);
@@ -2181,6 +2195,23 @@ void test_nss_getorigbyname(void **state)
     /* Wait until the test finishes with EOK */
     ret = test_ev_loop(nss_test_ctx->tctx);
     assert_int_equal(ret, EOK);
+
+    /* Also test looking up the same stuff with UPN */
+    nss_test_ctx->tctx->done = false;
+
+    mock_input_user_or_group(test_upn);
+    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETORIGBYNAME);
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
+
+    /* Query for that user, call a callback when command finishes */
+    set_cmd_cb(test_nss_getorigbyname_check);
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETORIGBYNAME,
+                          nss_test_ctx->nss_cmds);
+    assert_int_equal(ret, EOK);
+
+    /* Wait until the test finishes with EOK */
+    ret = test_ev_loop(nss_test_ctx->tctx);
+    assert_int_equal(ret, EOK);
 }
 
 static int test_nss_getorigbyname_extra_check(uint32_t status, uint8_t *body,
@@ -3658,6 +3689,9 @@ int main(int argc, const char *argv[])
         cmocka_unit_test_setup_teardown(test_nss_getorigbyname_multi_value_attrs,
                                         nss_test_setup_extra_attr,
                                         nss_test_teardown),
+        cmocka_unit_test_setup_teardown(test_nss_getorigbyname,
+                                        nss_test_setup,
+                                        nss_test_teardown),
         cmocka_unit_test_setup_teardown(test_nss_getpwnam_upn,
                                         nss_test_setup, nss_test_teardown),
         cmocka_unit_test_setup_teardown(test_nss_getpwnam_upn_neg,
_______________________________________________
sssd-devel mailing list -- sssd-devel@lists.fedorahosted.org
To unsubscribe send an email to sssd-devel-le...@lists.fedorahosted.org

Reply via email to