--- /dev/null
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <glib.h>
+
+#include "libcommon/file.h"
+#include "libcommon/list.h"
+#include "libcommon/ini-parser.h"
+#include "../test-main.h"
+#include "../test-mock.h"
+
+#define MOCK_FILE_PATH "testfile"
+#define MOCK_FILE_LENGTH 100
+
+static int setup(void **state)
+{
+ FILE *fp;
+ char cwd[128] = {0, };
+ char path[256] = {0, };
+ char str[MOCK_FILE_LENGTH];
+
+ memset(str, 't', MOCK_FILE_LENGTH);
+
+ snprintf(path, sizeof(path), "%s/%s", getcwd(cwd, sizeof(cwd)), MOCK_FILE_PATH);
+
+ fp = fopen(path, "w+");
+
+ if (!fp) {
+ printf("Failed to setup mockfile, %m\n");
+ return -1;
+ }
+
+ fwrite(str, 1, MOCK_FILE_LENGTH, fp);
+ fclose(fp);
+
+ return 0;
+}
+
+static int teardown(void **state)
+{
+ char cwd[128] = {0, };
+ char path[256] = {0, };
+
+ snprintf(path, sizeof(path), "%s/%s", getcwd(cwd, sizeof(cwd)), MOCK_FILE_PATH);
+
+ unlink(path);
+
+ return 0;
+}
+
+static void test_sys_get_str_p1(void **state)
+{
+ char buffer[MOCK_FILE_LENGTH * 2];
+ int retval;
+
+ /* remove possible null character */
+ for (int i = 0; i < sizeof(buffer); ++i)
+ buffer[i] = 'x';
+
+ /* mocking open() syscall */
+ mock_open(MOCK_FILE_PATH, O_RDONLY);
+
+ /* this is example of how mock works */
+ retval = sys_get_str("the pathname will be replaced to mocked one",
+ buffer, sizeof(buffer));
+
+ assert_int_equal(retval, 0);
+}
+
+/* test small buffer */
+static void test_sys_get_str_n1(void **state)
+{
+ char buffer[MOCK_FILE_LENGTH / 2];
+ int retval;
+
+ /* remove possible null character */
+ for (int i = 0; i < sizeof(buffer); ++i)
+ buffer[i] = 'x';
+
+ /* mocking open() syscall */
+ mock_open(MOCK_FILE_PATH, O_RDONLY);
+
+ /* this is example of how mock works */
+ retval = sys_get_str("the pathname will be replaced to mocked one",
+ buffer, sizeof(buffer));
+
+ assert_int_equal(retval, -EIO);
+ assert_int_equal(buffer[0], 0);
+}
+
+/* test no-exist file */
+static void test_sys_get_str_n2(void **state)
+{
+ char buffer[MOCK_FILE_LENGTH * 2];
+ int retval;
+
+ mock_open(MOCK_FILE_PATH, O_RDONLY);
+
+ retval = sys_get_str("the pathname will be replaced to mocked one",
+ buffer, sizeof(buffer));
+
+ assert_int_equal(retval, -ENOENT);
+}
+
+static void test_list_append_p(void **state)
+{
+ GList *head = NULL;
+ GList *elem, *elem2;
+ int i;
+ int value;
+ int length;
+
+ for ( i = 0; i < 10; ++i)
+ SYS_G_LIST_APPEND(head, (intptr_t) i);
+
+ length = SYS_G_LIST_LENGTH(head);
+ assert_int_equal(length, 10);
+
+ i = 0;
+ SYS_G_LIST_FOREACH(head, elem, elem2) {
+ value = (int) elem2;
+ assert_int_equal(value, i++);
+ }
+}
+
+static void test_list_prepend_p(void **state)
+{
+ GList *head = NULL;
+ GList *elem, *elem2;
+ int i;
+ int value;
+ int length;
+
+ for ( i = 0; i < 10; ++i)
+ SYS_G_LIST_PREPEND(head, (intptr_t) i);
+
+ length = SYS_G_LIST_LENGTH(head);
+ assert_int_equal(length, 10);
+
+ i = 10;
+ SYS_G_LIST_FOREACH(head, elem, elem2) {
+ value = (int) elem2;
+ assert_int_equal(value, --i);
+ }
+}
+
+static int check_user_data = 0x443;
+static bool checked1;
+static bool checked2;
+static bool checked3;
+static bool garbage;
+static int test_parser(struct parse_result *result, void *data)
+{
+ assert_int_equal(data, check_user_data);
+
+ if (MATCH(result->section, "Section1") &&
+ MATCH(result->name, "Key1") &&
+ MATCH(result->value, "Value1"))
+ checked1 = true;
+ else if (MATCH(result->section, "Section2") &&
+ MATCH(result->name, "Key2") &&
+ MATCH(result->value, "Value2"))
+ checked2 = true;
+ else if (MATCH(result->section, "Section2") &&
+ MATCH(result->name, "Key3") &&
+ MATCH(result->value, "Value3"))
+ checked3 = true;
+ else
+ garbage = true;
+
+ return 0;
+}
+
+static void test_config_parse_p(void **state)
+{
+ char cwd[128];
+ char path[256];
+ int retval;
+
+ /* load predefined test.conf file */
+ retval = snprintf(path, sizeof(path), "%s/libcommon/test.conf", getcwd(cwd, sizeof(cwd)));
+ if (retval >= sizeof(path))
+ path[sizeof(path) - 1] = '\0';
+
+ config_parse(path, test_parser, (void *)(intptr_t) check_user_data);
+
+ assert_true(checked1);
+ assert_true(checked2);
+ assert_true(checked3);
+ assert_false(garbage);
+}
+
+int run_test_suite(void)
+{
+ const struct CMUnitTest testsuite[] = {
+ cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown),
+ cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown),
+ cmocka_unit_test(test_sys_get_str_n2),
+ cmocka_unit_test(test_list_append_p),
+ cmocka_unit_test(test_list_prepend_p),
+ cmocka_unit_test(test_config_parse_p),
+ };
+
+ return cmocka_run_group_tests(testsuite, NULL, NULL);
+}
--- /dev/null
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "test-main.h"
+#include "test-mock.h"
+
+//static FILE *__fp = NULL;
+
+int __wrap_open(const char *pathname, int flags)
+{
+ const char *mock_pathname;
+ int mock_flags;
+
+ mock_pathname = mock_ptr_type(char *);
+ if (mock_pathname == NULL) {
+ errno = -ENOENT;
+ return -1;
+ }
+
+ mock_flags = mock_type(int);
+
+ return open(mock_pathname, mock_flags);
+}
+
+int __wrap_open64(const char *pathname, int flags)
+{
+ return __wrap_open(pathname, flags);
+}
+
+
+void mock_open(const char *pathname, int flags)
+{
+ will_return(__wrap_open, pathname);
+ will_return(__wrap_open, flags);
+}
+
+/*
+FILE *__wrap_fopen(const char *pathname, const char *mode)
+{
+ const char *mock_pathname = mock_ptr_type(char *);
+ const char *mock_mode = mock_ptr_type(char *);
+
+ __fp = fopen(mock_pathname, mock_mode);
+
+ return __fp;
+}
+
+FILE *__wrap_fopen64(const char *pathname, const char *mode)
+{
+ return __wrap_fopen(pathname, mode);
+
+}
+
+int __wrap_fclose(FILE *stream)
+{
+ return 0;
+}
+
+int __wrap_fclose64(FILE *stream)
+{
+ return __wrap_fclose(stream);
+}
+*/