mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-07-31 00:03:07 +03:00
tests: Migrated torture_callbacks to cmockery.
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
project(unittests C)
|
project(unittests C)
|
||||||
|
|
||||||
add_cmockery_test(torture_buffer torture_buffer.c ${TORTURE_LIBRARY})
|
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_init torture_init.c ${TORTURE_LIBRARY})
|
||||||
#add_check_test(torture_keyfiles torture_keyfiles.c ${TORTURE_LIBRARY})
|
#add_check_test(torture_keyfiles torture_keyfiles.c ${TORTURE_LIBRARY})
|
||||||
#add_check_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY})
|
#add_check_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY})
|
||||||
|
@ -5,57 +5,62 @@
|
|||||||
#include <libssh/callbacks.h>
|
#include <libssh/callbacks.h>
|
||||||
|
|
||||||
static int myauthcallback (const char *prompt, char *buf, size_t len,
|
static int myauthcallback (const char *prompt, char *buf, size_t len,
|
||||||
int echo, int verify, void *userdata){
|
int echo, int verify, void *userdata) {
|
||||||
(void) prompt;
|
(void) prompt;
|
||||||
(void) buf;
|
(void) buf;
|
||||||
(void) len;
|
(void) len;
|
||||||
(void) echo;
|
(void) echo;
|
||||||
(void) verify;
|
(void) verify;
|
||||||
(void) userdata;
|
(void) userdata;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ssh_callbacks_struct callbacks =
|
static void setup(void **state) {
|
||||||
{
|
struct ssh_callbacks_struct *cb;
|
||||||
.userdata=(void *)0x0badc0de,
|
|
||||||
.auth_function=myauthcallback
|
|
||||||
};
|
|
||||||
|
|
||||||
static void setup(void) {
|
cb = malloc(sizeof(struct ssh_callbacks_struct));
|
||||||
ssh_callbacks_init(&callbacks);
|
assert_false(cb == NULL);
|
||||||
|
|
||||||
|
cb->userdata = (void *) 0x0badc0de;
|
||||||
|
cb->auth_function = myauthcallback;
|
||||||
|
|
||||||
|
ssh_callbacks_init(cb);
|
||||||
|
*state = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void teardown(void) {
|
static void teardown(void **state) {
|
||||||
|
free(*state);
|
||||||
}
|
}
|
||||||
|
|
||||||
START_TEST (torture_callbacks_size)
|
static void torture_callbacks_size(void **state) {
|
||||||
{
|
struct ssh_callbacks_struct *cb = *state;;
|
||||||
ck_assert_int_ne(callbacks.size,0);
|
|
||||||
}
|
|
||||||
END_TEST
|
|
||||||
|
|
||||||
START_TEST (torture_callbacks_exists)
|
assert_int_not_equal(cb->size, 0);
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
END_TEST
|
|
||||||
|
|
||||||
Suite *torture_make_suite(void) {
|
|
||||||
Suite *s = suite_create("libssh_options");
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void torture_callbacks_exists(void **state) {
|
||||||
|
struct ssh_callbacks_struct *cb = *state;
|
||||||
|
|
||||||
|
assert_int_not_equal(ssh_callbacks_exists(cb, auth_function), 0);
|
||||||
|
assert_int_equal(ssh_callbacks_exists(cb, log_function), 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user