Add ssh_callbacks_exists internal macro + unittest
(first commit with eclipse helios, crossing fingers ...)
Этот коммит содержится в:
родитель
e4701e7c86
Коммит
94b00cc762
@ -156,7 +156,8 @@ typedef struct ssh_socket_callbacks_struct *ssh_socket_callbacks;
|
||||
#define SSH_SOCKET_CONNECTED_ERROR 2
|
||||
#define SSH_SOCKET_CONNECTED_TIMEOUT 3
|
||||
|
||||
/** Initializes an ssh_callbacks_struct
|
||||
/**
|
||||
* @brief Initializes an ssh_callbacks_struct
|
||||
* A call to this macro is mandatory when you have set a new
|
||||
* ssh_callback_struct structure. Its goal is to maintain the binary
|
||||
* compatibility with future versions of libssh as the structure
|
||||
@ -166,6 +167,20 @@ typedef struct ssh_socket_callbacks_struct *ssh_socket_callbacks;
|
||||
(p)->size=sizeof(*(p)); \
|
||||
} while(0);
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @brief tests if a callback can be called without crash
|
||||
* verifies that the struct size if big enough
|
||||
* verifies that the callback pointer exists
|
||||
* @param p callback pointer
|
||||
* @param c callback name
|
||||
* @returns nonzero if callback can be called
|
||||
*/
|
||||
#define ssh_callbacks_exists(p,c) (\
|
||||
( (char *)&((p)-> c) < (char *)(p) + (p)->size ) && \
|
||||
((p)-> c != NULL) \
|
||||
)
|
||||
|
||||
/** @brief Prototype for a packet callback, to be called when a new packet arrives
|
||||
* @param session The current session of the packet
|
||||
* @param type packet type (see ssh2.h)
|
||||
@ -217,11 +232,10 @@ typedef struct ssh_packet_callbacks_struct *ssh_packet_callbacks;
|
||||
* functions for auth, logging and status.
|
||||
*
|
||||
* @code
|
||||
* struct ssh_callbacks_struct cb;
|
||||
* memset(&cb, 0, sizeof(struct ssh_callbacks_struct));
|
||||
* cb.userdata = data;
|
||||
* cb.auth_function = my_auth_function;
|
||||
*
|
||||
* struct ssh_callbacks_struct cb = {
|
||||
* .userdata = data,
|
||||
* .auth_function = my_auth_function
|
||||
* };
|
||||
* ssh_callbacks_init(&cb);
|
||||
* ssh_set_callbacks(session, &cb);
|
||||
* @endcode
|
||||
|
@ -1,8 +1,9 @@
|
||||
project(unittests C)
|
||||
|
||||
add_check_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})
|
||||
add_check_test(torture_list torture_list.c ${TORTURE_LIBRARY})
|
||||
add_check_test(torture_misc torture_misc.c ${TORTURE_LIBRARY})
|
||||
add_check_test(torture_keyfiles torture_keyfiles.c ${TORTURE_LIBRARY})
|
||||
add_check_test(torture_options torture_options.c ${TORTURE_LIBRARY})
|
||||
add_check_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY})
|
||||
|
61
tests/unittests/torture_callbacks.c
Обычный файл
61
tests/unittests/torture_callbacks.c
Обычный файл
@ -0,0 +1,61 @@
|
||||
#define LIBSSH_STATIC
|
||||
|
||||
#include "torture.h"
|
||||
#include <libssh/priv.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
struct ssh_callbacks_struct callbacks =
|
||||
{
|
||||
.userdata=(void *)0x0badc0de,
|
||||
.auth_function=myauthcallback
|
||||
};
|
||||
|
||||
static void setup(void) {
|
||||
ssh_callbacks_init(&callbacks);
|
||||
}
|
||||
|
||||
static void teardown(void) {
|
||||
|
||||
}
|
||||
|
||||
START_TEST (torture_callbacks_size)
|
||||
{
|
||||
ck_assert_int_ne(callbacks.size,0);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user