unit tests: config parser 35/217035/9
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Tue, 5 Nov 2019 16:58:21 +0000 (17:58 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Wed, 4 Dec 2019 14:00:01 +0000 (14:00 +0000)
Change-Id: If103e1b6a8a9d8ae647ea081fc5a9681c5767881
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
tests/cmocka-core.c
tests/config_parser_test_1.conf [new file with mode: 0644]
tests/config_parser_test_2.conf [new file with mode: 0644]
tests/config_parser_test_3.conf [new file with mode: 0644]
tests/config_parser_test_4.conf [new file with mode: 0644]
tests/config_parser_test_5.conf [new file with mode: 0644]

index 0793cae..01750ed 100644 (file)
@@ -1,12 +1,14 @@
 #include <stdarg.h>
 #include <setjmp.h>
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <cmocka.h>
 #include <assert.h>
 #include <glib.h>
 #include <errno.h>
 #include <stdbool.h>
+#include <math.h>
 #include "notifier.h"
 #include "config-parser.h"
 
@@ -14,6 +16,8 @@ typedef int (*noti_cb)(void *data);
 
 void *__real_malloc(size_t size);
 void __real_free(void *ptr);
+char *__real_strdup(const char *s);
+char *__real_strndup(const char *s, size_t n);
 GSList *__real_g_slist_append(GSList *list, gpointer data);
 GSList *__real_g_slist_remove(GSList *list, gconstpointer data);
 
@@ -30,6 +34,45 @@ int test_notifier_cb(void *data)
        return 0;
 }
 
+int config_parser_cb(struct parse_result *result, void *user_data)
+{
+       int t = mock_type(int);
+       if (t == 1)
+               return -1;
+       check_expected_ptr(user_data);
+
+       char *s = mock_ptr_type(char *);
+       char *k = mock_ptr_type(char *);
+       char *v = mock_ptr_type(char *);
+
+       assert(!strcmp(s, result->section));
+       assert(!strcmp(k, result->name));
+       assert(!strcmp(v, result->value));
+
+       (void) s;
+       (void) k;
+       (void) v;
+       return 0;
+}
+
+char *__wrap_strdup(const char *s)
+{
+       bool fake = mock_type(bool);
+       if (fake)
+               return NULL;
+
+       return __real_strdup(s);
+}
+
+char *__wrap_strndup(const char *s, size_t n)
+{
+       bool fake = mock_type(bool);
+       if (fake)
+               return NULL;
+
+       return __real_strndup(s, n);
+}
+
 void __wrap_free(void *ptr)
 {
        bool check = mock_type(bool);
@@ -128,6 +171,112 @@ static void test_unregister_notifier(void **state)
        assert_int_equal(unregister_notifier(1, fptr), 0);
 }
 
+static void test_config_parse(void **state)
+{
+       (void) state; /* unused */
+       const char *section = "section";
+       const char *key = "key";
+       const char *value = "value";
+       void *ptr = (void*)0xD00D1E;
+
+       /* null pointer test */
+       assert_int_equal(config_parse(NULL, config_parser_cb, NULL), -EINVAL);
+
+       /* null pointer test */
+       assert_int_equal(config_parse("/dev/null", NULL, NULL), -EINVAL);
+
+       /* non-existing file test */
+       assert_int_equal(config_parse("the_x_files", config_parser_cb, NULL), -EIO);
+
+       /* invalid section header test */
+       assert_int_equal(config_parse("../../tests/config_parser_test_1.conf", config_parser_cb, NULL), -EBADMSG);
+
+       /* key with no value test */
+       assert_int_equal(config_parse("../../tests/config_parser_test_2.conf", config_parser_cb, NULL), -EBADMSG);
+
+       /* callback return value test */
+       will_return(config_parser_cb, 1);
+       assert_int_equal(config_parse("../../tests/config_parser_test_3.conf", config_parser_cb, NULL), -EBADMSG);
+
+       /* positive test */
+       will_return(config_parser_cb, 0);
+       expect_value(config_parser_cb, user_data, cast_ptr_to_largest_integral_type(ptr));
+       will_return(config_parser_cb, cast_ptr_to_largest_integral_type(section));
+       will_return(config_parser_cb, cast_ptr_to_largest_integral_type(key));
+       will_return(config_parser_cb, cast_ptr_to_largest_integral_type(value));
+       assert_int_equal(config_parse("../../tests/config_parser_test_3.conf", config_parser_cb, ptr), 0);
+}
+
+static void test_config_parse_new(void **state)
+{
+       (void) state; /* unused */
+       ConfigTableItem dummy_items[] = {
+               { NULL, NULL, NULL, 0, NULL }
+       };
+
+       char *str = NULL;
+       bool b = false;
+       int i = 0;
+       float f = 100.0;
+       int bytes = 1;
+       ConfigTableItem items[] = {
+               { "section", "key_s", config_parse_string, 0, &str },
+               { "section", "key_b", config_parse_bool, 0, &b },
+               { "section", "key_i", config_parse_int, 0, &i },
+               { "section", "key_f", config_parse_float, 0, &f },
+               { "section", "key_bytes", config_parse_bytes, 0, &bytes },
+               { NULL, NULL, NULL, 0, NULL }
+       };
+
+       will_return_maybe(__wrap_free, false);
+       /* non-existing file test */
+       assert_int_equal(config_parse_new("the_x_files", NULL), -ENOENT);
+
+       /* invalid section header test */
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_1.conf", NULL), -EBADMSG);
+
+       /* oom test 1 */
+       will_return(__wrap_strndup, true);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_2.conf", NULL), -ENOMEM);
+
+       /* oom test 2 */
+       will_return(__wrap_strndup, false);
+       will_return(__wrap_strndup, true);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_3.conf", NULL), -ENOMEM);
+
+       /* oom test 2 */
+       will_return(__wrap_strndup, false);
+       will_return(__wrap_strndup, false);
+       will_return(__wrap_strdup, true);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_3.conf", NULL), -ENOMEM);
+
+       /* too much data in a file test */
+       will_return_count(__wrap_strndup, false, 131);
+       will_return_count(__wrap_strdup, false, 65);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_4.conf", dummy_items), -EOVERFLOW);
+
+       /* lookup keys not present in config */
+       will_return_count(__wrap_strndup, false, 2);
+       will_return_count(__wrap_strdup, false, 1);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_3.conf", items), 0);
+       assert(str == NULL);
+       assert(b == false);
+       assert (i == 0);
+       assert(f == 100.0);
+       assert(bytes = 1);
+
+       /* positive test */
+       will_return_count(__wrap_strndup, false, 8);
+       will_return_count(__wrap_strdup, false, 7);
+       assert_int_equal(config_parse_new("../../tests/config_parser_test_5.conf", items), 0);
+       assert(str);
+       assert(!strcmp(str, "xD"));
+       assert(b == true);
+       assert(i == 123);
+       assert(fabsf(f - 1.23) < 1e-6);
+       assert(bytes == 10485760);
+}
+
 static int test_setup(void **state)
 {
        will_return_maybe(__wrap_malloc, false);
@@ -140,7 +289,8 @@ int main(int argc, char* argv[])
                cmocka_unit_test(test_register_notifier),
                cmocka_unit_test(test_notify),
                cmocka_unit_test(test_unregister_notifier),
-
+               cmocka_unit_test(test_config_parse),
+               cmocka_unit_test(test_config_parse_new),
        };
        return cmocka_run_group_tests(tests, test_setup, NULL);
 }
diff --git a/tests/config_parser_test_1.conf b/tests/config_parser_test_1.conf
new file mode 100644 (file)
index 0000000..70a8368
--- /dev/null
@@ -0,0 +1 @@
+[section
diff --git a/tests/config_parser_test_2.conf b/tests/config_parser_test_2.conf
new file mode 100644 (file)
index 0000000..36fad3b
--- /dev/null
@@ -0,0 +1,2 @@
+[section]
+key
diff --git a/tests/config_parser_test_3.conf b/tests/config_parser_test_3.conf
new file mode 100644 (file)
index 0000000..877008f
--- /dev/null
@@ -0,0 +1,2 @@
+[section]
+key=value
diff --git a/tests/config_parser_test_4.conf b/tests/config_parser_test_4.conf
new file mode 100644 (file)
index 0000000..d422abc
--- /dev/null
@@ -0,0 +1,140 @@
+[section0]
+key=value
+[section1]
+key=value
+[section2]
+key=value
+[section3]
+key=value
+[section4]
+key=value
+[section5]
+key=value
+[section6]
+key=value
+[section7]
+key=value
+[section8]
+key=value
+[section9]
+key=value
+[section10]
+key=value
+[section11]
+key=value
+[section12]
+key=value
+[section13]
+key=value
+[section14]
+key=value
+[section15]
+key=value
+[section16]
+key=value
+[section17]
+key=value
+[section18]
+key=value
+[section19]
+key=value
+[section20]
+key=value
+[section21]
+key=value
+[section22]
+key=value
+[section23]
+key=value
+[section24]
+key=value
+[section25]
+key=value
+[section26]
+key=value
+[section27]
+key=value
+[section28]
+key=value
+[section29]
+key=value
+[section30]
+key=value
+[section31]
+key=value
+[section32]
+key=value
+[section33]
+key=value
+[section34]
+key=value
+[section35]
+key=value
+[section36]
+key=value
+[section37]
+key=value
+[section38]
+key=value
+[section39]
+key=value
+[section30]
+key=value
+[section41]
+key=value
+[section42]
+key=value
+[section43]
+key=value
+[section44]
+key=value
+[section45]
+key=value
+[section46]
+key=value
+[section47]
+key=value
+[section48]
+key=value
+[section49]
+key=value
+[section50]
+key=value
+[section51]
+key=value
+[section52]
+key=value
+[section53]
+key=value
+[section54]
+key=value
+[section55]
+key=value
+[section56]
+key=value
+[section57]
+key=value
+[section58]
+key=value
+[section59]
+key=value
+[section60]
+key=value
+[section61]
+key=value
+[section62]
+key=value
+[section63]
+key=value
+[section64]
+key=value
+[section65]
+key=value
+[section66]
+key=value
+[section67]
+key=value
+[section68]
+key=value
+[section69]
+key=value
diff --git a/tests/config_parser_test_5.conf b/tests/config_parser_test_5.conf
new file mode 100644 (file)
index 0000000..90671bb
--- /dev/null
@@ -0,0 +1,8 @@
+[section]
+key_s=xD
+key_b=y
+key_i=123
+key_f=1.23
+key_l=1230000
+key_bytes=10M
+