From aa78994e2499dcfea1bc5104d6a2a81087a19259 Mon Sep 17 00:00:00 2001
From: Petr Cech <pcech@redhat.com>
Date: Fri, 27 Nov 2015 10:07:55 -0500
Subject: [PATCH 3/4] TEST_TOOLS_COLONDB: Test for sss_colondb_writeline

There are three functions at API of colondb wrapper:
 * sss_colondb_open()
 * sss_colondb_readline()
 * sss_colondb_writeline()
This patch adds tests for sss_colondb_writeline() function.

We test those cases:
 * write to empty file
 * write to file with existing records

And tests on sss_colondb_open() with existing file were replaced
with tests in cases of  existig empty file and existing file
with records.

Resolves:
https://fedorahosted.org/sssd/ticket/2764
---
 src/tests/cmocka/test_tools_colondb.c | 158 ++++++++++++++++++++++++++++++----
 1 file changed, 143 insertions(+), 15 deletions(-)

diff --git a/src/tests/cmocka/test_tools_colondb.c b/src/tests/cmocka/test_tools_colondb.c
index a3f66119e7ab887138a1d03811dfd15f19b89b12..51690a3e7bb365f8a224f385d5e114b8a42419e2 100644
--- a/src/tests/cmocka/test_tools_colondb.c
+++ b/src/tests/cmocka/test_tools_colondb.c
@@ -28,14 +28,26 @@
 #define TESTS_PATH "tp_" BASE_FILE_STEM
 #define TESTS_FILE "test_colondb.ldb"
 
-#define new_test_with_file(test)                                               \
-    cmocka_unit_test_setup_teardown(test_##test, test_colondb_with_file_setup, \
-                                    test_colondb_teardown)
+#define TEST_STRING1 "white"
+#define TEST_INT1 12
+
+#define TEST_STRING2 "black"
+#define TEST_INT2 34
 
 #define new_test_without_file(test)                                            \
     cmocka_unit_test_setup_teardown(                                           \
         test_##test, test_colondb_without_file_setup, test_colondb_teardown)
 
+#define new_test_with_empty_file(test)                                         \
+    cmocka_unit_test_setup_teardown(test_##test,                               \
+                                    test_colondb_with_empty_file_setup,        \
+                                    test_colondb_teardown)
+
+#define new_test_with_nonempty_file(test)                                      \
+    cmocka_unit_test_setup_teardown(test_##test,                               \
+                                    test_colondb_with_nonempty_file_setup,     \
+                                    test_colondb_teardown)
+
 struct test_colondb_ctx
 {
     bool is_file_created;
@@ -54,7 +66,7 @@ static int _create_dir(const char *path)
     return 0;
 }
 
-static int _create_file(const char *path, const char *name)
+static int _create_empty_file(const char *path, const char *name)
 {
     TALLOC_CTX *tmp_ctx = NULL;
     char *file_name = NULL;
@@ -93,7 +105,39 @@ done:
     return ret;
 }
 
-static int test_colondb_setup(void **state, bool create_file)
+static int _create_nonempty_file(const char *path, const char *name)
+{
+    TALLOC_CTX *tmp_ctx = NULL;
+    struct sss_colondb *db = NULL;
+    struct sss_colondb_write_field table[] = {
+        {SSS_COLONDB_STRING, {.str = TEST_STRING2}},
+        {SSS_COLONDB_UINT32, {.uint32 = TEST_INT2}},
+        {SSS_COLONDB_SENTINEL, {0}}};
+    errno_t ret;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
+        return 1;
+    }
+
+    ret = _create_empty_file(TESTS_PATH, TESTS_FILE);
+    if (ret != 0) {
+        ret = 1;
+        goto done;
+    }
+
+    db =
+        sss_colondb_open(tmp_ctx, SSS_COLONDB_WRITE, TESTS_PATH "/" TESTS_FILE);
+
+    ret = sss_colondb_writeline(db, table);
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
+static int test_colondb_setup(void **state, int file_state)
 {
     struct test_colondb_ctx *test_ctx = NULL;
 
@@ -102,11 +146,21 @@ static int test_colondb_setup(void **state, bool create_file)
     test_ctx = talloc_zero(global_talloc_context, struct test_colondb_ctx);
     assert_non_null(test_ctx);
 
-    if (create_file) {
-        _create_file(TESTS_PATH, TESTS_FILE);
+    switch (file_state) {
+    case 0:
+        test_ctx->is_file_created = false;
+        break;
+    case 1:
+        _create_empty_file(TESTS_PATH, TESTS_FILE);
+        test_ctx->is_file_created = true;
+        break;
+    case 2:
+        _create_nonempty_file(TESTS_PATH, TESTS_FILE);
         test_ctx->is_file_created = true;
-    } else {
+        break;
+    default:
         test_ctx->is_file_created = false;
+        break;
     }
 
     *state = test_ctx;
@@ -116,12 +170,17 @@ static int test_colondb_setup(void **state, bool create_file)
 
 static int test_colondb_without_file_setup(void **state)
 {
-    return test_colondb_setup(state, false);
+    return test_colondb_setup(state, 0);
 }
 
-static int test_colondb_with_file_setup(void **state)
+static int test_colondb_with_empty_file_setup(void **state)
 {
-    return test_colondb_setup(state, true);
+    return test_colondb_setup(state, 1);
+}
+
+static int test_colondb_with_nonempty_file_setup(void **state)
+{
+    return test_colondb_setup(state, 2);
 }
 
 static int test_colondb_teardown(void **state)
@@ -151,7 +210,6 @@ static int test_colondb_teardown(void **state)
 
 void test_open_nonexist_for_read(void **state)
 {
-
     struct test_colondb_ctx *test_ctx = NULL;
     struct sss_colondb *db = NULL;
 
@@ -166,7 +224,6 @@ void test_open_nonexist_for_read(void **state)
 
 void test_open_nonexist_for_write(void **state)
 {
-
     struct test_colondb_ctx *test_ctx = NULL;
     struct sss_colondb *db = NULL;
 
@@ -206,6 +263,72 @@ void test_open_exist_for_write(void **state)
     assert_non_null(db);
 }
 
+void test_open_nonempty_for_read(void **state)
+{
+
+    struct test_colondb_ctx *test_ctx = NULL;
+    struct sss_colondb *db = NULL;
+
+    test_ctx = talloc_get_type_abort(*state, struct test_colondb_ctx);
+
+    db =
+        sss_colondb_open(test_ctx, SSS_COLONDB_READ, TESTS_PATH "/" TESTS_FILE);
+    assert_non_null(db);
+}
+
+void test_open_nonempty_for_write(void **state)
+{
+
+    struct test_colondb_ctx *test_ctx = NULL;
+    struct sss_colondb *db = NULL;
+
+    test_ctx = talloc_get_type_abort(*state, struct test_colondb_ctx);
+
+    db = sss_colondb_open(test_ctx, SSS_COLONDB_WRITE,
+                          TESTS_PATH "/" TESTS_FILE);
+    assert_non_null(db);
+}
+
+void test_write_to_empty(void **state)
+{
+    struct test_colondb_ctx *test_ctx = NULL;
+    struct sss_colondb *db = NULL;
+    struct sss_colondb_write_field table[] = {
+        {SSS_COLONDB_STRING, {.str = TEST_STRING1}},
+        {SSS_COLONDB_UINT32, {.uint32 = TEST_INT1}},
+        {SSS_COLONDB_SENTINEL, {0}}};
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct test_colondb_ctx);
+
+    db = sss_colondb_open(test_ctx, SSS_COLONDB_WRITE,
+                          TESTS_PATH "/" TESTS_FILE);
+    assert_non_null(db);
+
+    ret = sss_colondb_writeline(db, table);
+    assert_true(ret == 0);
+}
+
+void test_write_to_nonempty(void **state)
+{
+    struct test_colondb_ctx *test_ctx = NULL;
+    struct sss_colondb *db = NULL;
+    struct sss_colondb_write_field table[] = {
+        {SSS_COLONDB_STRING, {.str = TEST_STRING1}},
+        {SSS_COLONDB_UINT32, {.uint32 = TEST_INT1}},
+        {SSS_COLONDB_SENTINEL, {0}}};
+    errno_t ret;
+
+    test_ctx = talloc_get_type_abort(*state, struct test_colondb_ctx);
+
+    db = sss_colondb_open(test_ctx, SSS_COLONDB_WRITE,
+                          TESTS_PATH "/" TESTS_FILE);
+    assert_non_null(db);
+
+    ret = sss_colondb_writeline(db, table);
+    assert_true(ret == 0);
+}
+
 int main(int argc, const char *argv[])
 {
     poptContext pc;
@@ -216,8 +339,13 @@ int main(int argc, const char *argv[])
     const struct CMUnitTest tests[] = {
         new_test_without_file(open_nonexist_for_read),
         new_test_without_file(open_nonexist_for_write),
-        new_test_with_file(open_exist_for_read),
-        new_test_with_file(open_exist_for_write),
+        new_test_with_empty_file(open_exist_for_read),
+        new_test_with_empty_file(open_exist_for_write),
+        new_test_with_nonempty_file(open_nonempty_for_read),
+        new_test_with_nonempty_file(open_nonempty_for_write),
+
+        new_test_with_empty_file(write_to_empty),
+        new_test_with_nonempty_file(write_to_nonempty),
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
-- 
2.4.3

