libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit 49553eef2927c9e57600b38c9a55aa7c25ca1093
parent b6a01c094dd3053b106afe1e680d1141ff3ecd43
Author: Jacki <jacki@thejackimonster.de>
Date:   Tue, 30 Apr 2024 00:06:13 +0200

Normalize multiple test cases

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Msrc/gnunet_chat_handle.c | 27++++++++++++++++-----------
Msrc/gnunet_chat_handle.h | 10+++++-----
Msrc/gnunet_chat_util.c | 2+-
Mtests/test_gnunet_chat_file.c | 239+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mtests/test_gnunet_chat_handle.c | 189++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mtests/test_gnunet_chat_lobby.c | 304+++++++++++++++++++++++++++++++++++++------------------------------------------
6 files changed, 397 insertions(+), 374 deletions(-)

diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -511,7 +511,7 @@ find_accounts_by_name (struct GNUNET_CHAT_Handle *handle, return accounts; } -static int +static enum GNUNET_GenericReturnValue update_accounts_operation (struct GNUNET_CHAT_InternalAccounts **out_accounts, struct GNUNET_CHAT_Handle *handle, const char *name, @@ -538,7 +538,7 @@ update_accounts_operation (struct GNUNET_CHAT_InternalAccounts **out_accounts, return GNUNET_OK; } -int +enum GNUNET_GenericReturnValue handle_create_account (struct GNUNET_CHAT_Handle *handle, const char *name) { @@ -550,7 +550,8 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, if (accounts) return GNUNET_SYSERR; - int result = update_accounts_operation( + enum GNUNET_GenericReturnValue result; + result = update_accounts_operation( &accounts, handle, name, @@ -575,7 +576,7 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, return result; } -int +enum GNUNET_GenericReturnValue handle_delete_account (struct GNUNET_CHAT_Handle *handle, const char *name) { @@ -587,7 +588,8 @@ handle_delete_account (struct GNUNET_CHAT_Handle *handle, if (!accounts) return GNUNET_SYSERR; - int result = update_accounts_operation( + enum GNUNET_GenericReturnValue result; + result = update_accounts_operation( &accounts, handle, NULL, @@ -610,7 +612,7 @@ handle_delete_account (struct GNUNET_CHAT_Handle *handle, return result; } -int +enum GNUNET_GenericReturnValue handle_rename_account (struct GNUNET_CHAT_Handle *handle, const char *old_name, const char *new_name) @@ -623,7 +625,8 @@ handle_rename_account (struct GNUNET_CHAT_Handle *handle, if (!accounts) return GNUNET_SYSERR; - int result = update_accounts_operation( + enum GNUNET_GenericReturnValue result; + result = update_accounts_operation( &accounts, handle, NULL, @@ -664,7 +667,8 @@ handle_delete_lobby (struct GNUNET_CHAT_Handle *handle, return GNUNET_SYSERR; struct GNUNET_CHAT_InternalAccounts *accounts = NULL; - enum GNUNET_GenericReturnValue result = update_accounts_operation( + enum GNUNET_GenericReturnValue result; + result = update_accounts_operation( &accounts, handle, NULL, @@ -725,7 +729,7 @@ handle_create_file_path (const struct GNUNET_CHAT_Handle *handle, return filename; } -int +enum GNUNET_GenericReturnValue handle_update (struct GNUNET_CHAT_Handle *handle) { GNUNET_assert((handle) && (handle->current)); @@ -741,7 +745,8 @@ handle_update (struct GNUNET_CHAT_Handle *handle) if (!accounts) return GNUNET_SYSERR; - int result = update_accounts_operation( + enum GNUNET_GenericReturnValue result; + result = update_accounts_operation( &accounts, handle, name, @@ -841,7 +846,7 @@ handle_send_room_name (struct GNUNET_CHAT_Handle *handle, GNUNET_free(msg.body.name.name); } -int +enum GNUNET_GenericReturnValue handle_request_context_by_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room) { diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -197,7 +197,7 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle); * @param[in] name Chat account name * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ -int +enum GNUNET_GenericReturnValue handle_create_account (struct GNUNET_CHAT_Handle *handle, const char *name); @@ -209,7 +209,7 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, * @param[in] name Chat account name * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ -int +enum GNUNET_GenericReturnValue handle_delete_account (struct GNUNET_CHAT_Handle *handle, const char *name); @@ -223,7 +223,7 @@ handle_delete_account (struct GNUNET_CHAT_Handle *handle, * @param[in] new_name New chat account name * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ -int +enum GNUNET_GenericReturnValue handle_rename_account (struct GNUNET_CHAT_Handle *handle, const char *old_name, const char *new_name); @@ -271,7 +271,7 @@ handle_create_file_path (const struct GNUNET_CHAT_Handle *handle, * @param[in] handle Chat handle * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ -int +enum GNUNET_GenericReturnValue handle_update (struct GNUNET_CHAT_Handle *handle); /** @@ -323,7 +323,7 @@ handle_send_room_name (struct GNUNET_CHAT_Handle *handle, * @param[in,out] room Messenger room * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ -int +enum GNUNET_GenericReturnValue handle_request_context_by_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room); diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -30,7 +30,7 @@ static const char label_prefix_of_contact [] = "contact"; static const char label_prefix_of_group [] = "group"; -static const char identity_prefix_of_lobby [] = "gnunet_chat_lobby"; +static const char identity_prefix_of_lobby [] = "_gnunet_chat_lobby"; void util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, diff --git a/tests/test_gnunet_chat_file.c b/tests/test_gnunet_chat_file.c @@ -22,35 +22,12 @@ * @file test_gnunet_chat_file.c */ -#include "gnunet/gnunet_chat_lib.h" #include "test_gnunet_chat.h" -#include <check.h> -#include <gnunet/gnunet_common.h> -int -on_gnunet_chat_file_send_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_nonnull(chat); - ck_assert_ptr_eq(handle, chat); - ck_assert_ptr_nonnull(account); - - const char *name = GNUNET_CHAT_account_get_name(account); - - ck_assert_ptr_nonnull(name); - ck_assert_ptr_null(GNUNET_CHAT_get_connected(handle)); - - if (0 == strcmp(name, "gnunet_chat_file_send")) - { - GNUNET_CHAT_connect(chat, account); - return GNUNET_NO; - } - - return GNUNET_YES; -} +#define TEST_SEND_ID "gnunet_chat_file_send" +#define TEST_SEND_TEXT "gnunet_chat_file_deleted" +#define TEST_SEND_FILENAME "gnunet_chat_file_send_name" +#define TEST_SEND_GROUP "gnunet_chat_file_send_group" void on_gnunet_chat_file_send_upload(void *cls, @@ -91,10 +68,10 @@ on_gnunet_chat_file_send_unindex(void *cls, ck_assert_uint_eq(completed, size); - GNUNET_CHAT_context_send_text(context, "gnunet_chat_file_deleted"); + GNUNET_CHAT_context_send_text(context, TEST_SEND_TEXT); } -int +enum GNUNET_GenericReturnValue on_gnunet_chat_file_send_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) @@ -108,100 +85,122 @@ on_gnunet_chat_file_send_msg(void *cls, ck_assert_ptr_nonnull(handle); ck_assert_ptr_nonnull(message); - enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Group *group; struct GNUNET_CHAT_File *file; - if (kind == GNUNET_CHAT_KIND_TEXT) - goto exit_file_test; - - if ((kind != GNUNET_CHAT_KIND_REFRESH) || - (GNUNET_CHAT_get_connected(handle))) - goto skip_search_account; - - ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); - ck_assert_ptr_null(context); - - ck_assert_int_ne(GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_file_send_it, - handle - ), GNUNET_SYSERR); - - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; - -skip_search_account: - if ((GNUNET_CHAT_KIND_LOGIN != kind) || - (context)) - goto skip_file_upload; - - ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); - ck_assert_ptr_null(context); - - struct GNUNET_CHAT_Group *group = GNUNET_CHAT_group_create( - handle, - "gnunet_chat_file_send_group" - ); - - ck_assert_ptr_nonnull(group); - - struct GNUNET_CHAT_Context *group_context = GNUNET_CHAT_group_get_context( - group - ); - - ck_assert_ptr_nonnull(group_context); - ck_assert_ptr_null(filename); + const char *name; + const char *text; - filename = GNUNET_DISK_mktemp("gnunet_chat_file_send_name"); - - ck_assert_ptr_nonnull(filename); - - file = GNUNET_CHAT_context_send_file( - group_context, - filename, - on_gnunet_chat_file_send_upload, - handle - ); - - ck_assert_ptr_nonnull(file); - -skip_file_upload: - if (GNUNET_CHAT_KIND_FILE != kind) - return GNUNET_YES; - - ck_assert(kind == GNUNET_CHAT_KIND_FILE); - ck_assert_ptr_nonnull(context); - - file = GNUNET_CHAT_message_get_file(message); - - ck_assert_ptr_nonnull(file); - - ck_assert_int_eq(GNUNET_CHAT_file_unindex( - file, - on_gnunet_chat_file_send_unindex, - context - ), GNUNET_OK); - - return GNUNET_YES; - -exit_file_test: - ck_assert(kind == GNUNET_CHAT_KIND_TEXT); - ck_assert_ptr_nonnull(context); - ck_assert_ptr_nonnull(filename); - - remove(filename); - GNUNET_free(filename); - filename = NULL; - - const char* text = GNUNET_CHAT_message_get_text(message); - - ck_assert_str_eq(text, "gnunet_chat_file_deleted"); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - "gnunet_chat_file_send" - ), GNUNET_OK); + switch (GNUNET_CHAT_message_get_kind(message)) + { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; + case GNUNET_CHAT_KIND_REFRESH: + ck_assert_ptr_null(context); + break; + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_null(context); + + group = GNUNET_CHAT_group_create( + handle, TEST_SEND_GROUP + ); + + ck_assert_ptr_nonnull(group); + + context = GNUNET_CHAT_group_get_context(group); + + ck_assert_ptr_nonnull(context); + ck_assert_ptr_null(filename); + + filename = GNUNET_DISK_mktemp(TEST_SEND_FILENAME); + + ck_assert_ptr_nonnull(filename); + + file = GNUNET_CHAT_context_send_file( + context, + filename, + on_gnunet_chat_file_send_upload, + handle + ); + + ck_assert_ptr_nonnull(file); + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_null(context); + ck_assert_ptr_null(filename); + break; + case GNUNET_CHAT_KIND_CREATED_ACCOUNT: + ck_assert_ptr_null(context); + + account = GNUNET_CHAT_message_get_account(message); + + ck_assert_ptr_nonnull(account); + + name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_SEND_ID); + + GNUNET_CHAT_connect(handle, account); + break; + case GNUNET_CHAT_KIND_DELETED_ACCOUNT: + ck_assert_ptr_null(context); + + account = GNUNET_CHAT_message_get_account(message); + + ck_assert_ptr_nonnull(account); + + name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_SEND_ID); + + GNUNET_CHAT_stop(handle); + break; + case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: + ck_assert_ptr_null(context); + break; + case GNUNET_CHAT_KIND_UPDATE_CONTEXT: + case GNUNET_CHAT_KIND_JOIN: + case GNUNET_CHAT_KIND_CONTACT: + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(filename); + break; + case GNUNET_CHAT_KIND_TEXT: + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(filename); + + remove(filename); + GNUNET_free(filename); + filename = NULL; + + text = GNUNET_CHAT_message_get_text(message); + + ck_assert_ptr_nonnull(text); + ck_assert_str_eq(text, TEST_SEND_TEXT); + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, TEST_SEND_ID + ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_FILE: + ck_assert_ptr_nonnull(context); + + file = GNUNET_CHAT_message_get_file(message); + + ck_assert_ptr_nonnull(file); + ck_assert_int_eq(GNUNET_CHAT_file_unindex( + file, + on_gnunet_chat_file_send_unindex, + context + ), GNUNET_OK); + break; + default: + ck_abort(); + break; + } - GNUNET_CHAT_stop(handle); return GNUNET_YES; } @@ -214,7 +213,7 @@ call_gnunet_chat_file_send(const struct GNUNET_CONFIGURATION_Handle *cfg) ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( handle, - "gnunet_chat_file_send" + TEST_SEND_ID ), GNUNET_OK); } diff --git a/tests/test_gnunet_chat_handle.c b/tests/test_gnunet_chat_handle.c @@ -23,9 +23,6 @@ */ #include "test_gnunet_chat.h" -#include <check.h> -#include <gnunet/gnunet_chat_lib.h> -#include <string.h> #define TEST_ACCOUNTS_ID "gnunet_chat_handle_accounts" #define TEST_CONNECTION_ID "gnunet_chat_handle_connection" @@ -39,27 +36,28 @@ call_gnunet_chat_handle_init(const struct GNUNET_CONFIGURATION_Handle *cfg) struct GNUNET_CHAT_Handle *handle; handle = GNUNET_CHAT_start(cfg, NULL, NULL); - ck_assert_ptr_ne(handle, NULL); + + ck_assert_ptr_nonnull(handle); GNUNET_CHAT_stop(handle); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_init, call_gnunet_chat_handle_init) -int +enum GNUNET_GenericReturnValue on_gnunet_chat_handle_accounts_it(void *cls, const struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Account *account) { unsigned int *accounts_stage = cls; - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(account); ck_assert_int_eq(*accounts_stage, 2); const char *name = GNUNET_CHAT_account_get_name(account); - ck_assert_ptr_ne(name, NULL); + ck_assert_ptr_nonnull(name); if (0 == strcmp(name, TEST_ACCOUNTS_ID)) { @@ -81,14 +79,17 @@ on_gnunet_chat_handle_accounts_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_eq(context, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_null(context); const struct GNUNET_CHAT_Account *account; account = GNUNET_CHAT_message_get_account(message); switch (GNUNET_CHAT_message_get_kind(message)) { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; case GNUNET_CHAT_KIND_REFRESH: if (0 == accounts_stage) { @@ -120,7 +121,7 @@ on_gnunet_chat_handle_accounts_msg(void *cls, break; case GNUNET_CHAT_KIND_CREATED_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); if (0 == strcmp(GNUNET_CHAT_account_get_name(account), TEST_ACCOUNTS_ID)) @@ -149,12 +150,12 @@ call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_accounts_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, call_gnunet_chat_handle_accounts) -int +enum GNUNET_GenericReturnValue on_gnunet_chat_handle_connection_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) @@ -163,39 +164,77 @@ on_gnunet_chat_handle_connection_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_eq(context, NULL); - ck_assert_ptr_ne(message, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(message); - enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); + const struct GNUNET_CHAT_Account *connected; + const struct GNUNET_CHAT_Account *account; + const char *name; - if (GNUNET_CHAT_KIND_LOGOUT == kind) + connected = GNUNET_CHAT_get_connected(handle); + account = GNUNET_CHAT_message_get_account(message); + + switch (GNUNET_CHAT_message_get_kind(message)) { - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - TEST_CONNECTION_ID - ), GNUNET_OK); + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; + case GNUNET_CHAT_KIND_REFRESH: + break; + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_nonnull(connected); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_eq(connected, account); - GNUNET_CHAT_stop(handle); - return GNUNET_YES; - } + name = GNUNET_CHAT_account_get_name(account); - if (GNUNET_CHAT_KIND_CREATED_ACCOUNT == kind) - { - const struct GNUNET_CHAT_Account *account; - account = GNUNET_CHAT_message_get_account(message); + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_CONNECTION_ID); + + GNUNET_CHAT_disconnect(handle); + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_nonnull(connected); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_eq(connected, account); + + name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_CONNECTION_ID); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, TEST_CONNECTION_ID + ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_CREATED_ACCOUNT: + ck_assert_ptr_null(connected); + ck_assert_ptr_nonnull(account); + + name = GNUNET_CHAT_account_get_name(account); - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_CONNECTION_ID); - if (0 == strcmp(GNUNET_CHAT_account_get_name(account), - TEST_CONNECTION_ID)) GNUNET_CHAT_connect(handle, account); - } + break; + case GNUNET_CHAT_KIND_DELETED_ACCOUNT: + ck_assert_ptr_null(connected); + ck_assert_ptr_nonnull(account); + + name = GNUNET_CHAT_account_get_name(account); - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; + ck_assert_ptr_nonnull(name); + ck_assert_str_eq(name, TEST_CONNECTION_ID); + + GNUNET_CHAT_stop(handle); + break; + default: + ck_abort(); + break; + } - GNUNET_CHAT_disconnect(handle); return GNUNET_YES; } @@ -205,16 +244,15 @@ call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg 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_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_CONNECTION_ID + handle, TEST_CONNECTION_ID ), GNUNET_OK); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, call_gnunet_chat_handle_connection) -int +enum GNUNET_GenericReturnValue on_gnunet_chat_handle_update_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) @@ -225,9 +263,9 @@ on_gnunet_chat_handle_update_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_eq(context, NULL); - ck_assert_ptr_ne(message, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(message); const struct GNUNET_CHAT_Account *account; account = GNUNET_CHAT_message_get_account(message); @@ -237,19 +275,22 @@ on_gnunet_chat_handle_update_msg(void *cls, switch (GNUNET_CHAT_message_get_kind(message)) { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; case GNUNET_CHAT_KIND_REFRESH: break; case GNUNET_CHAT_KIND_LOGIN: account = GNUNET_CHAT_get_connected(handle); - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); ck_assert_str_eq( GNUNET_CHAT_account_get_name(account), TEST_UPDATE_ID ); key = GNUNET_CHAT_get_key(handle); - ck_assert_ptr_ne(key, NULL); + ck_assert_ptr_nonnull(key); dup = (char*) GNUNET_CHAT_get_user_pointer(handle); @@ -257,7 +298,7 @@ on_gnunet_chat_handle_update_msg(void *cls, dup = GNUNET_strdup(key); - ck_assert_ptr_ne(dup, NULL); + ck_assert_ptr_nonnull(dup); ck_assert_str_eq(key, dup); GNUNET_CHAT_set_user_pointer(handle, (void*) dup); @@ -271,7 +312,7 @@ on_gnunet_chat_handle_update_msg(void *cls, ck_assert_int_ge(update_stage, 2); ck_assert_int_le(update_stage, 3); - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); ck_assert_str_eq( GNUNET_CHAT_account_get_name(account), TEST_UPDATE_ID @@ -280,8 +321,7 @@ on_gnunet_chat_handle_update_msg(void *cls, if (update_stage == 3) { ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - TEST_UPDATE_ID + handle, TEST_UPDATE_ID ), GNUNET_OK); update_stage = 4; @@ -289,7 +329,7 @@ on_gnunet_chat_handle_update_msg(void *cls, break; case GNUNET_CHAT_KIND_CREATED_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), TEST_UPDATE_ID))) @@ -301,7 +341,7 @@ on_gnunet_chat_handle_update_msg(void *cls, GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_DELETED_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), TEST_UPDATE_ID))) @@ -312,19 +352,19 @@ on_gnunet_chat_handle_update_msg(void *cls, GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), TEST_UPDATE_ID))) break; key = GNUNET_CHAT_get_key(handle); - ck_assert_ptr_ne(key, NULL); + ck_assert_ptr_nonnull(key); dup = (char*) GNUNET_CHAT_get_user_pointer(handle); ck_assert_int_eq(update_stage, 2); - ck_assert_ptr_ne(dup, NULL); + ck_assert_ptr_nonnull(dup); ck_assert_str_ne(key, dup); GNUNET_free(dup); @@ -347,16 +387,15 @@ call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) 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_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_UPDATE_ID + handle, TEST_UPDATE_ID ), GNUNET_OK); } CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, call_gnunet_chat_handle_update) -int +enum GNUNET_GenericReturnValue on_gnunet_chat_handle_rename_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) @@ -365,9 +404,9 @@ on_gnunet_chat_handle_rename_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_eq(context, NULL); - ck_assert_ptr_ne(message, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(message); const struct GNUNET_CHAT_Account *account; account = GNUNET_CHAT_message_get_account(message); @@ -377,17 +416,20 @@ on_gnunet_chat_handle_rename_msg(void *cls, switch (GNUNET_CHAT_message_get_kind(message)) { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; case GNUNET_CHAT_KIND_REFRESH: break; case GNUNET_CHAT_KIND_LOGIN: - ck_assert_ptr_ne(account, NULL); - ck_assert_ptr_ne(name, NULL); - ck_assert_ptr_eq(dup, NULL); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_nonnull(name); + ck_assert_ptr_null(dup); ck_assert_str_eq(name, TEST_RENAME_ID_A); dup = GNUNET_strdup(name); - ck_assert_ptr_ne(dup, NULL); + ck_assert_ptr_nonnull(dup); ck_assert_str_eq(name, dup); GNUNET_CHAT_set_user_pointer(handle, (void*) dup); @@ -398,26 +440,26 @@ on_gnunet_chat_handle_rename_msg(void *cls, ), GNUNET_YES); break; case GNUNET_CHAT_KIND_LOGOUT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); ck_assert_int_eq(GNUNET_CHAT_account_delete( handle, TEST_RENAME_ID_B ), GNUNET_OK); break; case GNUNET_CHAT_KIND_CREATED_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_DELETED_ACCOUNT: - ck_assert_ptr_ne(account, NULL); + ck_assert_ptr_nonnull(account); GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: - ck_assert_ptr_ne(account, NULL); - ck_assert_ptr_ne(name, NULL); - ck_assert_ptr_ne(dup, NULL); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_nonnull(name); + ck_assert_ptr_nonnull(dup); ck_assert_str_ne(name, dup); ck_assert_str_eq(name, TEST_RENAME_ID_B); ck_assert_str_eq(dup, TEST_RENAME_ID_A); @@ -440,10 +482,9 @@ call_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_rename_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_RENAME_ID_A + handle, TEST_RENAME_ID_A ), GNUNET_OK); } diff --git a/tests/test_gnunet_chat_lobby.c b/tests/test_gnunet_chat_lobby.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2022 GNUnet e.V. + Copyright (C) 2022--2024 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -24,85 +24,73 @@ #include "test_gnunet_chat.h" -int -on_gnunet_chat_lobby_base_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_lobby_base")) - { - GNUNET_CHAT_connect(chat, account); - return GNUNET_NO; - } +#define TEST_BASE_ID "gnunet_chat_lobby_base" +#define TEST_JOIN_ID "gnunet_chat_lobby_join" - return GNUNET_YES; -} - -int +enum GNUNET_GenericReturnValue on_gnunet_chat_lobby_base_msg(void *cls, - struct GNUNET_CHAT_Context *context, - const struct GNUNET_CHAT_Message *message) + struct GNUNET_CHAT_Context *context, + const struct GNUNET_CHAT_Message *message) { struct GNUNET_CHAT_Handle *handle = *( (struct GNUNET_CHAT_Handle**) cls ); - 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 ((kind != GNUNET_CHAT_KIND_REFRESH) || - (GNUNET_CHAT_get_connected(handle))) - goto skip_search_account; - - ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); - - GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_lobby_base_it, - handle - ); - - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; - -skip_search_account: - if (GNUNET_CHAT_KIND_LOGIN != kind) - return GNUNET_YES; - - ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); - - struct GNUNET_CHAT_Lobby *lobby = GNUNET_CHAT_lobby_open( - handle, - GNUNET_TIME_relative_get_second_(), - NULL, - NULL - ); - - ck_assert_ptr_ne(lobby, NULL); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(message); - GNUNET_CHAT_lobby_close(lobby); - GNUNET_CHAT_disconnect(handle); + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Lobby *lobby; - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - "gnunet_chat_lobby_base" - ), GNUNET_OK); + account = GNUNET_CHAT_message_get_account(message); - GNUNET_CHAT_stop(handle); + switch (GNUNET_CHAT_message_get_kind(message)) + { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; + case GNUNET_CHAT_KIND_REFRESH: + ck_assert_ptr_null(account); + break; + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_nonnull(account); + + lobby = GNUNET_CHAT_lobby_open( + handle, + GNUNET_TIME_relative_get_second_(), + NULL, + NULL + ); + + ck_assert_ptr_nonnull(lobby); + + GNUNET_CHAT_lobby_close(lobby); + GNUNET_CHAT_disconnect(handle); + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_nonnull(account); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, TEST_BASE_ID + ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_CREATED_ACCOUNT: + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); + break; + case GNUNET_CHAT_KIND_DELETED_ACCOUNT: + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_stop(handle); + break; + case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: + break; + default: + ck_abort(); + break; + } return GNUNET_YES; } @@ -113,121 +101,112 @@ call_gnunet_chat_lobby_base(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_base_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - "gnunet_chat_lobby_base" + handle, TEST_BASE_ID ), GNUNET_OK); } CREATE_GNUNET_TEST(test_gnunet_chat_lobby_base, call_gnunet_chat_lobby_base) -int -on_gnunet_chat_lobby_join_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_lobby_join")) - { - GNUNET_CHAT_connect(chat, account); - return GNUNET_NO; - } - - return GNUNET_YES; -} - -void -on_gnunet_chat_lobby_join_task(void *cls) -{ - struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; - - ck_assert_ptr_ne(chat, NULL); - - GNUNET_CHAT_disconnect(chat); - - ck_assert_int_eq(GNUNET_CHAT_account_delete( - chat, - "gnunet_chat_lobby_join" - ), GNUNET_OK); - - GNUNET_CHAT_stop(chat); -} - void on_gnunet_chat_lobby_join_open(void *cls, - const struct GNUNET_CHAT_Uri *uri) + const struct GNUNET_CHAT_Uri *uri) { struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; - ck_assert_ptr_ne(chat, NULL); - ck_assert_ptr_ne(uri, NULL); + ck_assert_ptr_nonnull(chat); + ck_assert_ptr_nonnull(uri); GNUNET_CHAT_lobby_join(chat, uri); - - GNUNET_SCHEDULER_add_at_with_priority( - GNUNET_TIME_absolute_get(), - GNUNET_SCHEDULER_PRIORITY_IDLE, - on_gnunet_chat_lobby_join_task, - chat - ); } -int +enum GNUNET_GenericReturnValue on_gnunet_chat_lobby_join_msg(void *cls, - struct GNUNET_CHAT_Context *context, - const struct GNUNET_CHAT_Message *message) + struct GNUNET_CHAT_Context *context, + const struct GNUNET_CHAT_Message *message) { + static struct GNUNET_CHAT_Lobby *lobby = NULL; + struct GNUNET_CHAT_Handle *handle = *( (struct GNUNET_CHAT_Handle**) cls ); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_ne(message, NULL); - - enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); - - if ((kind != GNUNET_CHAT_KIND_REFRESH) || - (GNUNET_CHAT_get_connected(handle))) - goto skip_search_account; - - ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); - ck_assert_ptr_eq(context, NULL); - - GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_lobby_join_it, - handle - ); - - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(message); -skip_search_account: - if (GNUNET_CHAT_KIND_LOGIN != kind) - return GNUNET_YES; + const struct GNUNET_CHAT_Account *account; - ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); - ck_assert_ptr_eq(context, NULL); + account = GNUNET_CHAT_message_get_account(message); - struct GNUNET_CHAT_Lobby *lobby = GNUNET_CHAT_lobby_open( - handle, - GNUNET_TIME_relative_get_second_(), - on_gnunet_chat_lobby_join_open, - handle - ); + switch (GNUNET_CHAT_message_get_kind(message)) + { + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); + break; + case GNUNET_CHAT_KIND_REFRESH: + ck_assert_ptr_null(context); + ck_assert_ptr_null(account); + break; + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_null(lobby); + + lobby = GNUNET_CHAT_lobby_open( + handle, + GNUNET_TIME_relative_get_second_(), + on_gnunet_chat_lobby_join_open, + handle + ); + + ck_assert_ptr_nonnull(lobby); + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_null(lobby); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, TEST_JOIN_ID + ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_CREATED_ACCOUNT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_null(lobby); + + GNUNET_CHAT_connect(handle, account); + break; + case GNUNET_CHAT_KIND_DELETED_ACCOUNT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_null(lobby); + + GNUNET_CHAT_stop(handle); + break; + case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + break; + case GNUNET_CHAT_KIND_UPDATE_CONTEXT: + ck_assert_ptr_nonnull(context); + break; + case GNUNET_CHAT_KIND_JOIN: + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_nonnull(lobby); + + GNUNET_CHAT_lobby_close(lobby); + lobby = NULL; + + GNUNET_CHAT_disconnect(handle); + break; + default: + ck_abort(); + break; + } - ck_assert_ptr_ne(lobby, NULL); return GNUNET_YES; } @@ -237,10 +216,9 @@ call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_join_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - "gnunet_chat_lobby_join" + handle, TEST_JOIN_ID ), GNUNET_OK); }