diff options
-rw-r--r-- | tests/unittests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/unittests/torture_callbacks.c | 85 |
2 files changed, 46 insertions, 41 deletions
diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 12d7d933..5a114b8d 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -1,7 +1,7 @@ project(unittests C) add_cmockery_test(torture_buffer torture_buffer.c ${TORTURE_LIBRARY}) -#add_check_test(torture_callbacks torture_callbacks.c ${TORTURE_LIBRARY}) +add_cmockery_test(torture_callbacks torture_callbacks.c ${TORTURE_LIBRARY}) #add_check_test(torture_init torture_init.c ${TORTURE_LIBRARY}) #add_check_test(torture_keyfiles torture_keyfiles.c ${TORTURE_LIBRARY}) #add_check_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY}) diff --git a/tests/unittests/torture_callbacks.c b/tests/unittests/torture_callbacks.c index 3f681e08..2c984ea7 100644 --- a/tests/unittests/torture_callbacks.c +++ b/tests/unittests/torture_callbacks.c @@ -5,57 +5,62 @@ #include <libssh/callbacks.h> static int myauthcallback (const char *prompt, char *buf, size_t len, - int echo, int verify, void *userdata){ - (void) prompt; - (void) buf; - (void) len; - (void) echo; - (void) verify; - (void) userdata; - return 0; + int echo, int verify, void *userdata) { + (void) prompt; + (void) buf; + (void) len; + (void) echo; + (void) verify; + (void) userdata; + return 0; } -struct ssh_callbacks_struct callbacks = -{ - .userdata=(void *)0x0badc0de, - .auth_function=myauthcallback -}; +static void setup(void **state) { + struct ssh_callbacks_struct *cb; -static void setup(void) { - ssh_callbacks_init(&callbacks); -} + cb = malloc(sizeof(struct ssh_callbacks_struct)); + assert_false(cb == NULL); -static void teardown(void) { + cb->userdata = (void *) 0x0badc0de; + cb->auth_function = myauthcallback; + ssh_callbacks_init(cb); + *state = cb; } -START_TEST (torture_callbacks_size) -{ - ck_assert_int_ne(callbacks.size,0); +static void teardown(void **state) { + free(*state); } -END_TEST - -START_TEST (torture_callbacks_exists) -{ - ck_assert_int_ne(ssh_callbacks_exists(&callbacks,auth_function),0); - ck_assert_int_eq(ssh_callbacks_exists(&callbacks,log_function),0); - /* we redefine size so auth_function is outside the range of callbacks->size */ - callbacks.size=(unsigned char *)&(callbacks.auth_function) - (unsigned char *)&callbacks; - ck_assert_int_eq(ssh_callbacks_exists(&callbacks,auth_function),0); - /* now make it one pointer bigger so we spill over the auth_function slot */ - callbacks.size += sizeof(void *); - ck_assert_int_ne(ssh_callbacks_exists(&callbacks,auth_function),0); + +static void torture_callbacks_size(void **state) { + struct ssh_callbacks_struct *cb = *state;; + + assert_int_not_equal(cb->size, 0); } -END_TEST -Suite *torture_make_suite(void) { - Suite *s = suite_create("libssh_options"); +static void torture_callbacks_exists(void **state) { + struct ssh_callbacks_struct *cb = *state; - torture_create_case_fixture(s, "torture_callbacks_size", - torture_callbacks_size, setup, teardown); - torture_create_case_fixture(s, "torture_callbacks_exists", - torture_callbacks_exists, setup, teardown); + assert_int_not_equal(ssh_callbacks_exists(cb, auth_function), 0); + assert_int_equal(ssh_callbacks_exists(cb, log_function), 0); - return s; + /* + * We redefine size so auth_function is outside the range of + * callbacks->size. + */ + cb->size = (unsigned char *) &cb->auth_function - (unsigned char *) cb; + assert_int_equal(ssh_callbacks_exists(cb, auth_function), 0); + + /* Now make it one pointer bigger so we spill over the auth_function slot */ + cb->size += sizeof(void *); + assert_int_not_equal(ssh_callbacks_exists(cb, auth_function), 0); } +int torture_run_tests(void) { + const UnitTest tests[] = { + unit_test_setup_teardown(torture_callbacks_size, setup, teardown), + unit_test_setup_teardown(torture_callbacks_exists, setup, teardown), + }; + + return run_tests(tests); +} |