libgnunetchat

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

commit df221ee6f08296510edd171b2ee9415cfd4d3e66
parent c5cc59052212c61d9cf613c95811141c7bbcfb7d
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Mon, 19 Sep 2022 12:37:34 +0200

Add file test and fixed memory leaks

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>

Diffstat:
MMakefile | 3++-
Msrc/gnunet_chat_handle.c | 22++++++++++++++++------
Msrc/gnunet_chat_lib.c | 2+-
Msrc/gnunet_chat_util.c | 2+-
Atests/test_gnunet_chat_file.c | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 196 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,7 +25,8 @@ SOURCES = gnunet_chat_lib.c\ HEADERS = gnunet_chat_lib.h TESTS = test_gnunet_chat_handle.c\ - test_gnunet_chat_lobby.c + test_gnunet_chat_lobby.c\ + test_gnunet_chat_file.c LIBRARIES = gnunetarm\ gnunetfs\ diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -297,7 +297,10 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle) { internal = handle->internal_head; - if ((internal->msg) && (internal->msg->context)) + if (!(internal->msg->context)) + break; + + if (internal->msg) message_destroy(internal->msg); GNUNET_CONTAINER_DLL_remove( @@ -546,11 +549,18 @@ handle_send_internal_message (struct GNUNET_CHAT_Handle *handle, handle->msg_cb(handle->msg_cls, context, internal->msg); - GNUNET_CONTAINER_DLL_insert( - handle->internal_head, - handle->internal_tail, - internal - ); + if (context) + GNUNET_CONTAINER_DLL_insert( + handle->internal_head, + handle->internal_tail, + internal + ); + else + GNUNET_CONTAINER_DLL_insert_tail( + handle->internal_head, + handle->internal_tail, + internal + ); } void diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -73,7 +73,7 @@ GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle) handle->destruction = GNUNET_SCHEDULER_add_at_with_priority( GNUNET_TIME_absolute_get(), - GNUNET_SCHEDULER_PRIORITY_IDLE, + GNUNET_SCHEDULER_PRIORITY_URGENT, task_handle_destruction, handle ); diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -292,7 +292,7 @@ util_get_lower(const char *name) { GNUNET_assert(name); - char *lower = GNUNET_malloc(strlen(name)); + char *lower = GNUNET_malloc(strlen(name) + 1); if (GNUNET_OK == GNUNET_STRINGS_utf8_tolower(name, lower)) return lower; diff --git a/tests/test_gnunet_chat_file.c b/tests/test_gnunet_chat_file.c @@ -0,0 +1,176 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 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 + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file test_gnunet_chat_file.c + */ + +#include "test_gnunet_chat.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_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_file_send")) + { + GNUNET_CHAT_connect(chat, account); + return GNUNET_NO; + } + + return GNUNET_YES; +} + +void +on_gnunet_chat_file_send_upload(void *cls, + const struct GNUNET_CHAT_File *file, + uint64_t completed, + uint64_t size) +{ + struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; + + ck_assert_ptr_ne(chat, NULL); + ck_assert_ptr_ne(file, NULL); + ck_assert_uint_le(completed, size); + + uint64_t check_size = GNUNET_CHAT_file_get_size(file); + + ck_assert_uint_eq(size, check_size); + + // TODO +} + +int +on_gnunet_chat_file_send_msg(void *cls, + 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_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_file_send_it, + handle + ); + + if (!GNUNET_CHAT_get_connected(handle)) + return GNUNET_YES; + +skip_search_account: + if ((GNUNET_CHAT_KIND_LOGIN != kind) || + (context)) + goto skip_first_login; + + + ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); + ck_assert_ptr_eq(context, NULL); + + struct GNUNET_CHAT_Group *group = GNUNET_CHAT_group_create( + handle, + "gnunet_chat_file_send_group" + ); + + ck_assert_ptr_ne(group, NULL); + + struct GNUNET_CHAT_Context *group_context = GNUNET_CHAT_group_get_context( + group + ); + + ck_assert_ptr_ne(group_context, NULL); + +skip_first_login: + if (GNUNET_CHAT_KIND_JOIN != kind) + goto skip_file_upload; + + ck_assert(kind == GNUNET_CHAT_KIND_JOIN); + ck_assert_ptr_ne(context, NULL); + + struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file( + context, + "Makefile", + on_gnunet_chat_file_send_upload, + handle + ); + + ck_assert_ptr_ne(file, NULL); + +skip_file_upload: + if (GNUNET_CHAT_KIND_FILE != kind) + return GNUNET_YES; + + ck_assert(kind == GNUNET_CHAT_KIND_FILE); + ck_assert_ptr_ne(context, NULL); + + GNUNET_CHAT_disconnect(handle); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, + "gnunet_chat_file_send" + ), GNUNET_OK); + + GNUNET_CHAT_stop(handle); + return GNUNET_YES; +} + +void +call_gnunet_chat_file_send(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + static struct GNUNET_CHAT_Handle *handle = NULL; + handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_file_send_msg, &handle); + + ck_assert_ptr_ne(handle, NULL); + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + "gnunet_chat_file_send" + ), GNUNET_OK); +} + +CREATE_GNUNET_TEST(test_gnunet_chat_file_send, call_gnunet_chat_file_send) + +START_SUITE(handle_suite, "File") +ADD_TEST_TO_SUITE(test_gnunet_chat_file_send, "Send") +END_SUITE + +MAIN_SUITE(handle_suite, CK_NORMAL)