summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2010-06-25 16:19:19 +0200
committerAris Adamantiadis <aris@0xbadc0de.be>2010-06-25 16:19:19 +0200
commit94b00cc7625bf139a72c3c8d5f8c30243319f5b4 (patch)
treef2fb01507002a4163e0ced9fbc340faed649ca16
parente4701e7c86aecc7bd201ea2ed54282f2d4737092 (diff)
downloadlibssh-94b00cc7625bf139a72c3c8d5f8c30243319f5b4.tar.gz
libssh-94b00cc7625bf139a72c3c8d5f8c30243319f5b4.tar.xz
libssh-94b00cc7625bf139a72c3c8d5f8c30243319f5b4.zip
Add ssh_callbacks_exists internal macro + unittest
(first commit with eclipse helios, crossing fingers ...)
-rw-r--r--include/libssh/callbacks.h26
-rw-r--r--tests/unittests/CMakeLists.txt5
-rw-r--r--tests/unittests/torture_callbacks.c61
3 files changed, 84 insertions, 8 deletions
diff --git a/include/libssh/callbacks.h b/include/libssh/callbacks.h
index d5a3745..3ceb385 100644
--- a/include/libssh/callbacks.h
+++ b/include/libssh/callbacks.h
@@ -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
diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt
index c7f2238..28aa50b 100644
--- a/tests/unittests/CMakeLists.txt
+++ b/tests/unittests/CMakeLists.txt
@@ -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})
diff --git a/tests/unittests/torture_callbacks.c b/tests/unittests/torture_callbacks.c
new file mode 100644
index 0000000..3f681e0
--- /dev/null
+++ b/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;
+}
+