[SSSD] Cmocka-unit-test-for-functions-getpwuid-added

Jakub Hrozek jhrozek at redhat.com
Tue Feb 25 14:55:38 UTC 2014


On Sat, Feb 15, 2014 at 01:04:36AM +0545, Pallavi Jha wrote:
> Hi,
> 
> Attached is a  patch with the additional test functions for getpwuid*
> functions. I think I did not get the flow of the data in program properly.
> That is why getting similar errors.
> 
> Please have a look into the patch and let me know how to improve upon it.
> 
> 
> 
> Thanks!
> Pallavi

Hi,

this test is mostly good after I did some local modifications. See a
couple of comments inline.

> From 5341e1331d984f48ee28118dba05d11dd4f068c4 Mon Sep 17 00:00:00 2001
> From: Pallavi Jha <pallavikumarijha at gmail.com>
> Date: Mon, 27 Jan 2014 19:31:06 +0545
> Subject: [PATCH] cmocka-unit-test-for-functions-getpwuid*-added

[snip]

> +static void mock_input_id(uint8_t *id)
> +{
> +    will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
> +    will_return(__wrap_sss_packet_get_body, id);
> +}
> +

I had to change this function, what the responder expects is a body and
body length which was previously only strlen. Now with this test we want
to pass an uint32_t to SSSD, so we need to have a way to set the blen to
sizeof(uint32_t). See the attached patch that applies on top of yours.

>  static void mock_fill_user(void)
>  {
>      /* One packet for the entry and one for num entries */
> @@ -587,6 +606,343 @@ void test_nss_getpwnam_fqdn_fancy(void **state)
>      assert_int_equal(ret, EOK);
>  }
>  
> +/* Check getting cached and valid id from cache. Account callback will
> + * not be called and test_nss_getpwuid_check will make sure the id is
> + * the same as the test entered before starting
> + */
> +static int test_nss_getpwuid_check(uint32_t status, uint8_t *body, size_t blen)
> +{
> +    struct passwd pwd;
> +    errno_t ret;
> +
> +    assert_int_equal(status, EOK);
> +
> +    ret = parse_user_packet(body, blen, &pwd);
> +    assert_int_equal(ret, EOK);
> +
> +    assert_int_equal(pwd.pw_uid, 101);
> +    assert_int_equal(pwd.pw_gid, 401);
> +    assert_string_equal(pwd.pw_name, "testuser1");
> +    assert_string_equal(pwd.pw_shell, "/bin/sh");
> +    assert_string_equal(pwd.pw_passwd, "*");
> +    return EOK;
> +}
> +
> +void test_nss_getpwuid(void **state)
> +{
> +    errno_t ret;
> +
> +    /* Prime the cache with a valid user */
> +    ret = sysdb_add_user(nss_test_ctx->tctx->dom,
> +                         "testuser1", 101, 401, "test user1",
> +                         "/home/testuser1", "/bin/sh", NULL,
> +                         NULL, 300, 0);
> +    assert_int_equal(ret, EOK);
> +
> +    uint8_t id = 101;

IDs in Linux are larger, typically uid_t is used, but inside SSSD we use
uint32_t.

> +    mock_input_id(&id);
> +    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
> +    mock_fill_user();
> +
> +    /* Query for that id, call a callback when command finishes */
> +    set_cmd_cb(test_nss_getpwuid_check);
> +    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETPWUID,
> +                          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);
> +}
> +

The other tests were mostly good, so I'm going to omit those.

> +static int test_nss_getpwuid_check_fqdn(uint32_t status,
> +                                        uint8_t *body, size_t blen)

Unlike get-<something>-by-name, get-by-id doesn't support fully
qualified lookups. So you can just remove the getpwuid_fqdn tests.

> +{
> +    struct passwd pwd;
> +    errno_t ret;
> +
> +    assert_int_equal(status, EOK);
> +
> +    nss_test_ctx->cctx->rctx->domains[0].fqnames = false;
> +
> +    ret = parse_user_packet(body, blen, &pwd);
> +    assert_int_equal(ret, EOK);
> +
> +    assert_int_equal(pwd.pw_uid, 301);
> +    assert_int_equal(pwd.pw_gid, 755);
> +    assert_string_equal(pwd.pw_name, "exampleuser_fqdn@"TEST_DOM_NAME);
> +    assert_string_equal(pwd.pw_shell, "/bin/sh");
> +    return EOK;
> +}
> +
> +void test_nss_getpwuid_fqdn(void **state)
> +{
> +    errno_t ret;
> +
> +    /* Prime the cache with a valid user */
> +    ret = sysdb_add_user(nss_test_ctx->tctx->dom,
> +                         "exampleuser_fqdn", 301, 755, "example user",
> +                         "/home/exampleuser", "/bin/sh", NULL,
> +                         NULL, 300, 0);
> +    assert_int_equal(ret, EOK);
> +
> +    uint8_t id = 301;

btw 301 doesn't fit uinto uint8_t. But that's not really important
anymore since we're changing the type to uint32_t.

Good work in overall! Please check out the attached patch and squash it
in if you agree. With the patch, all tests except the fqdn ones pass,
but the fqdn test should be removed anyway.
-------------- next part --------------
>From 5965ba7f8ee368cd2fe1a1aa8c9fce3014da1cf9 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Tue, 25 Feb 2014 15:54:13 +0100
Subject: [PATCH] fix the getpwuid tests

---
 src/tests/cmocka/test_nss_srv.c | 48 +++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 14 deletions(-)

diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
index 564cdde705278ad8b5ae2351aa05727dcdf54bc1..1acc5925dd57173120f1fc92de3134982f34c534 100644
--- a/src/tests/cmocka/test_nss_srv.c
+++ b/src/tests/cmocka/test_nss_srv.c
@@ -96,13 +96,18 @@ void __wrap_sss_packet_get_body(struct sss_packet *packet,
                                 uint8_t **body, size_t *blen)
 {
     enum sss_test_wrapper_call wtype = sss_mock_type(enum sss_test_wrapper_call);
+    size_t len;
 
     if (wtype == WRAP_CALL_REAL) {
         return __real_sss_packet_get_body(packet, body, blen);
     }
 
     *body = sss_mock_ptr_type(uint8_t *);
-    *blen = strlen((const char *) *body)+1;
+    len = sss_mock_type(size_t);
+    if (len == 0) {
+        len = strlen((const char *) *body)+1;
+    }
+    *blen = len;
     return;
 }
 
@@ -176,12 +181,20 @@ static void mock_input_user_or_group(const char *username)
 {
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, username);
+    will_return(__wrap_sss_packet_get_body, 0);
 }
 
-static void mock_input_id(uint8_t *id)
+static void mock_input_id(TALLOC_CTX *mem_ctx, uint32_t id)
 {
+    uint8_t *body;
+
+    body = talloc_zero_array(mem_ctx, uint8_t, 4);
+    if (body == NULL) return;
+
+    SAFEALIGN_SETMEM_UINT32(body, id, NULL);
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
-    will_return(__wrap_sss_packet_get_body, id);
+    will_return(__wrap_sss_packet_get_body, body);
+    will_return(__wrap_sss_packet_get_body, sizeof(uint32_t));
 }
 
 static void mock_fill_user(void)
@@ -639,8 +652,8 @@ void test_nss_getpwuid(void **state)
                          NULL, 300, 0);
     assert_int_equal(ret, EOK);
 
-    uint8_t id = 101;
-    mock_input_id(&id);
+    uint32_t id = 101;
+    mock_input_id(nss_test_ctx, id);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
     mock_fill_user();
 
@@ -663,11 +676,11 @@ void test_nss_getpwuid_neg(void **state)
     errno_t ret;
 
     uint8_t id = 102;
-    mock_input_id(&id);
+    mock_input_id(nss_test_ctx, id);
     mock_account_recv_simple();
 
     assert_true(nss_test_ctx->ncache_hit == false);
-    
+
     ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETPWUID,
                           nss_test_ctx->nss_cmds);
     assert_int_equal(ret, EOK);
@@ -683,7 +696,7 @@ void test_nss_getpwuid_neg(void **state)
      */
     nss_test_ctx->tctx->done = false;
 
-    mock_input_id(&id);
+    mock_input_id(nss_test_ctx, id);
     ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETPWUID,
                           nss_test_ctx->nss_cmds);
     assert_int_equal(ret, EOK);
@@ -733,7 +746,7 @@ void test_nss_getpwuid_search(void **state)
     struct ldb_result *res;
 
     uint8_t id = 107;
-    mock_input_id(&id);
+    mock_input_id(nss_test_ctx, id);
     mock_account_recv(0, 0, NULL, test_nss_getpwuid_search_acct_cb, nss_test_ctx);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
     mock_fill_user();
@@ -811,7 +824,7 @@ void test_nss_getpwuid_update(void **state)
 
     /* Mock client input */
     uint8_t id = 109;
-    mock_input_id(&id);
+    mock_input_id(nss_test_ctx, id);
     /* Mock client command */
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
     /* Call this function when id is updated by the mock DP request */
@@ -874,8 +887,8 @@ void test_nss_getpwuid_fqdn(void **state)
                          NULL, 300, 0);
     assert_int_equal(ret, EOK);
 
-    uint8_t id = 301;
-    mock_input_id(&id);
+    uint32_t id = 301;
+    mock_input_id(nss_test_ctx, id);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
     mock_fill_user();
 
@@ -926,8 +939,8 @@ void test_nss_getpwuid_fqdn_fancy(void **state)
                          NULL, 300, 0);
     assert_int_equal(ret, EOK);
 
-    uint8_t id = 424;
-    mock_input_id(&id);
+    uint32_t id = 424;
+    mock_input_id(nss_test_ctx, id);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETPWUID);
     mock_fill_user();
 
@@ -1477,6 +1490,7 @@ void test_nss_well_known_getnamebysid(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "S-1-5-32-550");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETNAMEBYSID);
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
     will_return(test_nss_well_known_sid_check, "Print Operators at BUILTIN");
@@ -1497,6 +1511,7 @@ void test_nss_well_known_getnamebysid_special(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "S-1-2-0");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETNAMEBYSID);
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
     will_return(test_nss_well_known_sid_check, "LOCAL at LOCAL AUTHORITY");
@@ -1517,6 +1532,7 @@ void test_nss_well_known_getnamebysid_non_existing(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "S-1-5-32-123");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETNAMEBYSID);
     will_return(test_nss_well_known_sid_check, NULL);
 
@@ -1536,6 +1552,7 @@ void test_nss_well_known_getidbysid_failure(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "S-1-5-32-550");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETIDBYSID);
     will_return(test_nss_well_known_sid_check, NULL);
 
@@ -1555,6 +1572,7 @@ void test_nss_well_known_getsidbyname(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "Cryptographic Operators at BUILTIN");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETSIDBYNAME);
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
     will_return(test_nss_well_known_sid_check, "S-1-5-32-569");
@@ -1575,6 +1593,7 @@ void test_nss_well_known_getsidbyname_nonexisting(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "Abc at BUILTIN");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETSIDBYNAME);
     will_return(test_nss_well_known_sid_check, NULL);
 
@@ -1594,6 +1613,7 @@ void test_nss_well_known_getsidbyname_special(void **state)
 
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_WRAPPER);
     will_return(__wrap_sss_packet_get_body, "CREATOR OWNER at CREATOR AUTHORITY");
+    will_return(__wrap_sss_packet_get_body, 0);
     will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETSIDBYNAME);
     will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
     will_return(test_nss_well_known_sid_check, "S-1-3-0");
-- 
1.8.5.3



More information about the sssd-devel mailing list