[SSSD] [PATCH] TESTS: Do not rely on order of hash items

Jakub Hrozek jhrozek at redhat.com
Fri Jul 4 12:44:23 UTC 2014


Hi,

the attached patch should fix a bug Sumit was seeing in the unit tests.
-------------- next part --------------
>From 17dd2ef636f537fa67eb140717b4b4b4376346c9 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Fri, 4 Jul 2014 14:42:45 +0200
Subject: [PATCH] TESTS: Do not rely on order of hash items

The nested group test was checking returned elements in a particular
order. That's not reliable because the returned values are fetched from
a hash iterator that doesn't guarantee the same order on different
systems.
---
 src/tests/cmocka/test_nested_groups.c | 76 ++++++++++++++++++++++-------------
 1 file changed, 47 insertions(+), 29 deletions(-)

diff --git a/src/tests/cmocka/test_nested_groups.c b/src/tests/cmocka/test_nested_groups.c
index 463d66efbbde28934d54cc71365b32b0c7fbbee1..261353643c76b6c7b3985069c0732d205a4f3c30 100644
--- a/src/tests/cmocka/test_nested_groups.c
+++ b/src/tests/cmocka/test_nested_groups.c
@@ -46,6 +46,9 @@
 #define GROUP_BASE_DN "cn=groups," OBJECT_BASE_DN
 #define USER_BASE_DN "cn=users," OBJECT_BASE_DN
 
+#define N_ELEMENTS(arr) \
+    (sizeof(arr) / sizeof(arr[0]))
+
 struct nested_groups_test_ctx {
     struct sss_test_ctx *tctx;
 
@@ -59,6 +62,36 @@ struct nested_groups_test_ctx {
     unsigned long num_groups;
 };
 
+/* Both arrays must have the same length! */
+static void compare_sysdb_string_array_noorder(struct sysdb_attrs **sysdb_array,
+                                               const char **string_array,
+                                               size_t len)
+{
+    int i, ii;
+    errno_t ret;
+    const char *name;
+
+    /* Check the returned groups. The order is irrelevant. */
+    for (i = 0; i < len; i++) {
+        ret = sysdb_attrs_get_string(sysdb_array[i], SYSDB_NAME, &name);
+        assert_int_equal(ret, ERR_OK);
+
+        for (ii = 0; ii < len; ii++) {
+            if (string_array[ii] == NULL) {
+                continue;
+            }
+            if (strcmp(name, string_array[ii]) == 0) {
+                string_array[ii] = NULL;
+                break;
+            }
+        }
+    }
+
+    for (i = 0; i < len; i++) {
+        assert_null(string_array[i]);
+    }
+}
+
 static void nested_groups_test_done(struct tevent_req *req)
 {
     struct nested_groups_test_ctx *ctx = NULL;
@@ -122,12 +155,14 @@ static void nested_groups_test_one_group_unique_members(void **state)
     struct tevent_req *req = NULL;
     TALLOC_CTX *req_mem_ctx = NULL;
     errno_t ret;
-    const char *name;
     const char *users[] = { "cn=user1,"USER_BASE_DN,
                             "cn=user2,"USER_BASE_DN,
                             NULL };
     const struct sysdb_attrs *user1_reply[2] = { NULL };
     const struct sysdb_attrs *user2_reply[2] = { NULL };
+    const char * expected[] = { "user1",
+                                "user2" };
+
 
     test_ctx = talloc_get_type_abort(*state, struct nested_groups_test_ctx);
 
@@ -169,13 +204,8 @@ static void nested_groups_test_one_group_unique_members(void **state)
     assert_int_equal(test_ctx->num_users, 2);
     assert_int_equal(test_ctx->num_groups, 1);
 
-    ret = sysdb_attrs_get_string(test_ctx->users[0], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "user1");
-
-    ret = sysdb_attrs_get_string(test_ctx->users[1], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "user2");
+    compare_sysdb_string_array_noorder(test_ctx->users,
+                                       expected, N_ELEMENTS(expected));
 }
 
 static void nested_groups_test_one_group_dup_users(void **state)
@@ -244,12 +274,14 @@ static void nested_groups_test_one_group_unique_group_members(void **state)
     struct tevent_req *req = NULL;
     TALLOC_CTX *req_mem_ctx = NULL;
     errno_t ret;
-    const char *name;
     const char *groups[] = { "cn=emptygroup1,"GROUP_BASE_DN,
                              "cn=emptygroup2,"GROUP_BASE_DN,
                              NULL };
     const struct sysdb_attrs *group1_reply[2] = { NULL };
     const struct sysdb_attrs *group2_reply[2] = { NULL };
+    const char * expected[] = { "rootgroup",
+                                "emptygroup1",
+                                "emptygroup2" };
 
     test_ctx = talloc_get_type_abort(*state, struct nested_groups_test_ctx);
 
@@ -293,17 +325,8 @@ static void nested_groups_test_one_group_unique_group_members(void **state)
     assert_int_equal(test_ctx->num_users, 0);
     assert_int_equal(test_ctx->num_groups, 3);
 
-    ret = sysdb_attrs_get_string(test_ctx->groups[0], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "rootgroup");
-
-    ret = sysdb_attrs_get_string(test_ctx->groups[1], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "emptygroup2");
-
-    ret = sysdb_attrs_get_string(test_ctx->groups[2], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "emptygroup1");
+    compare_sysdb_string_array_noorder(test_ctx->groups,
+                                       expected, N_ELEMENTS(expected));
 }
 
 static void nested_groups_test_one_group_dup_group_members(void **state)
@@ -313,12 +336,13 @@ static void nested_groups_test_one_group_dup_group_members(void **state)
     struct tevent_req *req = NULL;
     TALLOC_CTX *req_mem_ctx = NULL;
     errno_t ret;
-    const char *name;
     const char *groups[] = { "cn=emptygroup1,"GROUP_BASE_DN,
                              "cn=emptygroup1,"GROUP_BASE_DN,
                              NULL };
     const struct sysdb_attrs *group1_reply[2] = { NULL };
     const struct sysdb_attrs *group2_reply[2] = { NULL };
+    const char * expected[] = { "rootgroup",
+                                "emptygroup1" };
 
     test_ctx = talloc_get_type_abort(*state, struct nested_groups_test_ctx);
 
@@ -358,17 +382,11 @@ static void nested_groups_test_one_group_dup_group_members(void **state)
     /* check return code */
     assert_int_equal(ret, ERR_OK);
 
-    /* Check the users */
     assert_int_equal(test_ctx->num_users, 0);
     assert_int_equal(test_ctx->num_groups, 2);
 
-    ret = sysdb_attrs_get_string(test_ctx->groups[0], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "rootgroup");
-
-    ret = sysdb_attrs_get_string(test_ctx->groups[1], SYSDB_NAME, &name);
-    assert_int_equal(ret, ERR_OK);
-    assert_string_equal(name, "emptygroup1");
+    compare_sysdb_string_array_noorder(test_ctx->groups,
+                                       expected, N_ELEMENTS(expected));
 }
 
 void nested_groups_test_setup(void **state)
-- 
1.9.3



More information about the sssd-devel mailing list