unit tests: fd-handler 29/217829/6
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Thu, 14 Nov 2019 16:20:55 +0000 (17:20 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Wed, 4 Dec 2019 14:03:26 +0000 (14:03 +0000)
Change-Id: Id52044e532cc1873c6838f06e24c5f72229e107f
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
tests/cmocka-core.c

index 01750ed..9cc2852 100644 (file)
@@ -11,6 +11,7 @@
 #include <math.h>
 #include "notifier.h"
 #include "config-parser.h"
+#include "fd-handler.h"
 
 typedef int (*noti_cb)(void *data);
 
@@ -283,14 +284,171 @@ static int test_setup(void **state)
        return 0;
 }
 
+struct test_fd_read_handler_ctx {
+       fd_handler_h h;
+       int fd[2];
+       int flag;
+       GMainLoop *loop;
+};
+
+bool test_fd_read_handler_cb(int fd, void *data)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) data;
+       int check = mock_type(int);
+
+       if (check == 1) {
+               check_expected(fd);
+               check_expected_ptr(data);
+       }
+       g_main_loop_quit(c->loop);
+       return TRUE;
+
+}
+
+static void test_fd_read_handler_release_cb(void *data)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) data;
+       int check = mock_type(int);
+
+       if (check == 1)
+               check_expected_ptr(data);
+       c->flag++;
+}
+
+static int test_fd_read_handler_setup(void **state)
+{
+       struct test_fd_read_handler_ctx *c = calloc(1, sizeof (struct test_fd_read_handler_ctx));
+
+       if (c == NULL)
+               return -1;
+
+       if (pipe(c->fd) < 0)
+               return -1;
+
+       c->loop = g_main_loop_new(NULL, FALSE);
+
+       will_return_maybe(__wrap_malloc, false);
+       *state = c;
+
+       return 0;
+}
+
+static int test_fd_read_handler_teardown(void **state)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) *state;
+       assert(c);
+       assert(c->fd >= 0);
+
+       close(c->fd[0]);
+       close(c->fd[1]);
+
+       if (c->loop)
+               g_main_loop_unref(c->loop);
+
+       will_return(__wrap_free, false);
+       free(c);
+       return 0;
+}
+
+static void test_add_fd_read_handler(void **state)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) *state;
+       assert(c != NULL);
+       assert(c->fd >= 0);
+
+       assert_int_equal(add_fd_read_handler(-1, NULL, NULL, NULL, &(c->h)), -EINVAL);
+       assert_int_equal(add_fd_read_handler(231, NULL, NULL, NULL, &(c->h)), -EINVAL);
+       assert_int_equal(add_fd_read_handler(c->fd[0], test_fd_read_handler_cb, c, test_fd_read_handler_release_cb, &(c->h)), 0);
+}
+
+static gboolean test_fd_read_handler_start(gpointer data)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) data;
+       int r = write(c->fd[1], "hi", 2);
+       assert(r > 0);
+
+       return FALSE;
+}
+
+static gboolean test_fd_read_handler_terminate(gpointer data)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) data;
+       c->flag = 1;
+       g_main_loop_quit(c->loop);
+       return FALSE;
+}
+
+
+static void test_fd_read_handler(void **state)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) *state;
+
+       assert(c != NULL);
+       assert(c->fd >= 0);
+
+       will_return(test_fd_read_handler_cb, 1);
+       expect_value(test_fd_read_handler_cb, fd, c->fd[0]);
+       expect_value(test_fd_read_handler_cb, data, cast_ptr_to_largest_integral_type(c));
+
+       /* defer write to a pipe and run mainloop */
+       g_timeout_add_seconds(0, test_fd_read_handler_start, c);
+       g_main_loop_run(c->loop);
+}
+
+
+static void test_remove_fd_read_handler(void **state)
+{
+       struct test_fd_read_handler_ctx *c = (struct test_fd_read_handler_ctx *) *state;
+       assert(c != NULL);
+       assert(c->fd >= 0);
+
+       c->flag = 0;
+       assert_int_equal(remove_fd_read_handler(NULL), -EINVAL);
+       will_return(__wrap_free, false);
+       will_return(test_fd_read_handler_release_cb, 1);
+       expect_value(test_fd_read_handler_release_cb, data, cast_ptr_to_largest_integral_type(c));
+       assert_int_equal(remove_fd_read_handler(&(c->h)), 0);
+       assert_int_equal(c->flag, 1);
+
+       /* defer write to a pipe and run mainloop, the callback should not be called */
+       g_timeout_add_seconds(0, test_fd_read_handler_start, c);
+
+       /* flag should be changed in test_fd_read_handler_terminate() */
+       c->flag = 0;
+       g_timeout_add_seconds(2, test_fd_read_handler_terminate, c);
+       g_main_loop_run(c->loop);
+       assert_int_equal(c->flag, 1);
+}
+
 int main(int argc, char* argv[])
 {
-       const struct CMUnitTest tests[] = {
+       int r;
+       const struct CMUnitTest notifier_tests[] = {
                cmocka_unit_test(test_register_notifier),
                cmocka_unit_test(test_notify),
-               cmocka_unit_test(test_unregister_notifier),
+               cmocka_unit_test(test_unregister_notifier)
+       };
+       const struct CMUnitTest config_parser_tests[] = {
                cmocka_unit_test(test_config_parse),
-               cmocka_unit_test(test_config_parse_new),
+               cmocka_unit_test(test_config_parse_new)
        };
-       return cmocka_run_group_tests(tests, test_setup, NULL);
+       const struct CMUnitTest fd_read_handler_tests[] = {
+               cmocka_unit_test(test_add_fd_read_handler),
+               cmocka_unit_test(test_fd_read_handler),
+               cmocka_unit_test(test_remove_fd_read_handler),
+       };
+
+       r = cmocka_run_group_tests(notifier_tests, test_setup, NULL);
+       if (r != 0)
+               return r;
+
+       r = cmocka_run_group_tests(config_parser_tests, test_setup, NULL);
+       if (r != 0)
+               return r;
+
+       r = cmocka_run_group_tests(fd_read_handler_tests, test_fd_read_handler_setup, test_fd_read_handler_teardown);
+       if (r != 0)
+               return r;
+
+       return r;
 }