diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-31 22:50:14 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-31 22:50:14 +0200 |
commit | e2baf6fe4dd8358095a102ca320bf4abf034716a (patch) | |
tree | 182ec3d4d2c00dcac324db1133757bd1d9993edc | |
parent | 5b266a4c6e22ba210086b6d9759fd8aae01abaaf (diff) |
Completed handle tests and added user pointer functions to header
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | include/gnunet_chat_lib.h | 22 | ||||
-rw-r--r-- | tests/test_gnunet_chat.h | 2 | ||||
-rw-r--r-- | tests/test_gnunet_chat_handle.c | 160 |
3 files changed, 166 insertions, 18 deletions
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h index 135e82e..6d3a581 100644 --- a/include/gnunet_chat_lib.h +++ b/include/gnunet_chat_lib.h @@ -522,6 +522,28 @@ void GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_CHAT_Uri *uri); + +/** + * Sets a custom <i>user pointer</i> to a given chat <i>handle</i> so it can + * be accessed in all handle related callbacks. + * + * @param[in,out] handle Chat handle + * @param[in] user_pointer Custom user pointer + */ +void +GNUNET_CHAT_set_user_pointer (struct GNUNET_CHAT_Handle *handle, + void *user_pointer); + +/** + * Returns the custom user pointer of a given chat <i>handle</i> or NULL if it + * was not set any. + * + * @param[in] handle Chat handle + * @return Custom user pointer or NULL + */ +void* +GNUNET_CHAT_get_user_pointer (const struct GNUNET_CHAT_Handle *handle); + /** * Iterates through the contacts of a given chat <i>handle</i> with a selected * callback and custom closure. diff --git a/tests/test_gnunet_chat.h b/tests/test_gnunet_chat.h index 2172611..c7e6dcb 100644 --- a/tests/test_gnunet_chat.h +++ b/tests/test_gnunet_chat.h @@ -32,8 +32,6 @@ #include <gnunet/gnunet_chat_lib.h> -#define GNUNET_CHAT_TEST_ACCOUNT "check" - #define CREATE_GNUNET_TEST(test_name, test_call) \ void \ task_##test_call (__attribute__ ((unused)) void *cls, \ diff --git a/tests/test_gnunet_chat_handle.c b/tests/test_gnunet_chat_handle.c index f8a7b60..f4d4ef8 100644 --- a/tests/test_gnunet_chat_handle.c +++ b/tests/test_gnunet_chat_handle.c @@ -49,7 +49,7 @@ on_gnunet_chat_handle_accounts_it(__attribute__ ((unused)) void *cls, ck_assert_ptr_ne(name, NULL); - if (0 == strcmp(name, GNUNET_CHAT_TEST_ACCOUNT)) + if (0 == strcmp(name, "gnunet_chat_handle_accounts")) accounts_stage |= 2; return GNUNET_YES; @@ -77,7 +77,7 @@ on_gnunet_chat_handle_accounts_msg(void *cls, if (3 == accounts_stage) ck_assert_int_eq(GNUNET_CHAT_account_delete( accounts_handle, - GNUNET_CHAT_TEST_ACCOUNT + "gnunet_chat_handle_accounts" ), GNUNET_OK); accounts_stage = 4; @@ -88,7 +88,7 @@ on_gnunet_chat_handle_accounts_msg(void *cls, { ck_assert_int_eq(GNUNET_CHAT_account_create( accounts_handle, - GNUNET_CHAT_TEST_ACCOUNT + "gnunet_chat_handle_accounts" ), GNUNET_OK); accounts_stage = 1; @@ -108,53 +108,181 @@ call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, call_gnunet_chat_handle_accounts) -struct GNUNET_CHAT_Handle *connection_handle; +int +on_gnunet_chat_handle_connection_it(void *cls, + const struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Account *account) +{ + struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; + + ck_assert_ptr_ne(chat, NULL); + ck_assert_ptr_eq(handle, chat); + ck_assert_ptr_ne(account, NULL); + + const char *name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_ne(name, NULL); + ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); + + if (0 == strcmp(name, "gnunet_chat_handle_connection")) + { + GNUNET_CHAT_connect(chat, account); + return GNUNET_NO; + } + + return GNUNET_YES; +} int on_gnunet_chat_handle_connection_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) { - enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); + struct GNUNET_CHAT_Handle *handle = *( + (struct GNUNET_CHAT_Handle**) cls + ); - ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); - ck_assert_ptr_eq(cls, NULL); + ck_assert_ptr_ne(handle, NULL); ck_assert_ptr_eq(context, NULL); + ck_assert_ptr_ne(message, NULL); - GNUNET_CHAT_stop(connection_handle); + GNUNET_CHAT_iterate_accounts( + handle, + on_gnunet_chat_handle_connection_it, + handle + ); + + if (!GNUNET_CHAT_get_connected(handle)) + return GNUNET_YES; + + GNUNET_CHAT_disconnect(handle); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, + "gnunet_chat_handle_connection" + ), GNUNET_OK); + + GNUNET_CHAT_stop(handle); return GNUNET_YES; } void call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg) { - connection_handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, NULL); - ck_assert_ptr_ne(connection_handle, NULL); + static struct GNUNET_CHAT_Handle *handle = NULL; + handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, &handle); + + ck_assert_ptr_ne(handle, NULL); + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + "gnunet_chat_handle_connection" + ), GNUNET_OK); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, call_gnunet_chat_handle_connection) -struct GNUNET_CHAT_Handle *update_handle; +int +on_gnunet_chat_handle_update_it(void *cls, + const struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Account *account) +{ + struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; + + ck_assert_ptr_ne(chat, NULL); + ck_assert_ptr_eq(handle, chat); + ck_assert_ptr_ne(account, NULL); + + const char *name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_ne(name, NULL); + ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); + + if (0 == strcmp(name, "gnunet_chat_handle_update")) + { + GNUNET_CHAT_connect(chat, account); + return GNUNET_NO; + } + + return GNUNET_YES; +} int on_gnunet_chat_handle_update_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) { - enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); + struct GNUNET_CHAT_Handle *handle = *( + (struct GNUNET_CHAT_Handle**) cls + ); - ck_assert_ptr_eq(cls, NULL); + ck_assert_ptr_ne(handle, NULL); ck_assert_ptr_eq(context, NULL); + ck_assert_ptr_ne(message, NULL); + + enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); + + if (GNUNET_CHAT_get_connected(handle)) + goto skip_search_account; + + GNUNET_CHAT_iterate_accounts( + handle, + on_gnunet_chat_handle_update_it, + handle + ); + + if (!GNUNET_CHAT_get_connected(handle)) + return GNUNET_YES; + +skip_search_account: + if (GNUNET_CHAT_KIND_LOGIN != kind) + return GNUNET_YES; + + const char *key = GNUNET_CHAT_get_key(handle); + ck_assert_ptr_ne(key, NULL); + + char *dup = (char*) GNUNET_CHAT_get_user_pointer(handle); + + if (!dup) + { + dup = GNUNET_strdup(key); + + ck_assert_ptr_ne(dup, NULL); + ck_assert_str_eq(key, dup); + + GNUNET_CHAT_set_user_pointer(handle, (void*) dup); + GNUNET_CHAT_update(handle); + } + else + { + ck_assert_ptr_ne(dup, NULL); + // ck_assert_str_ne(key, dup); // TODO: needs to be implemented in service! + + GNUNET_free(dup); + + GNUNET_CHAT_disconnect(handle); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, + "gnunet_chat_handle_update" + ), GNUNET_OK); + + GNUNET_CHAT_stop(handle); + } - GNUNET_CHAT_stop(update_handle); return GNUNET_YES; } void call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) { - update_handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, NULL); - ck_assert_ptr_ne(update_handle, NULL); + static struct GNUNET_CHAT_Handle *handle = NULL; + handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, &handle); + + ck_assert_ptr_ne(handle, NULL); + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + "gnunet_chat_handle_update" + ), GNUNET_OK); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, call_gnunet_chat_handle_update) |