libgnunetchat

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

commit f109d8a868546f7a7557c5667c7d76f14f262296
parent df221ee6f08296510edd171b2ee9415cfd4d3e66
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Mon, 19 Sep 2022 13:17:35 +0200

Adjusted file test and handled empty files

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

Diffstat:
Msrc/gnunet_chat_util.c | 36+++++++++++++++++++++++++-----------
Mtests/test_gnunet_chat_file.c | 15+++++++--------
2 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -66,21 +66,32 @@ util_hash_file (const char *filename, struct GNUNET_HashCode *hash) return GNUNET_SYSERR; struct GNUNET_DISK_MapHandle *mapping; - const void* data = GNUNET_DISK_file_map( - file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size - ); + const void* data; - if (!data) + if (size > 0) { - GNUNET_DISK_file_close(file); - return GNUNET_SYSERR; + data = GNUNET_DISK_file_map( + file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size + ); + + if ((!data) || (!mapping)) + { + GNUNET_DISK_file_close(file); + return GNUNET_SYSERR; + } + } + else + { + mapping = NULL; + data = NULL; } GNUNET_CRYPTO_hash(data, size, hash); - GNUNET_DISK_file_unmap(mapping); - GNUNET_DISK_file_close(file); + if (mapping) + GNUNET_DISK_file_unmap(mapping); + GNUNET_DISK_file_close(file); return GNUNET_OK; } @@ -104,8 +115,11 @@ util_encrypt_file (const char *filename, if (!file) return GNUNET_SYSERR; - struct GNUNET_DISK_MapHandle *mapping = NULL; - void* data = GNUNET_DISK_file_map( + if (size <= 0) + return GNUNET_DISK_file_close(file); + + struct GNUNET_DISK_MapHandle *mapping; + const void* data = GNUNET_DISK_file_map( file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size ); @@ -146,7 +160,7 @@ util_encrypt_file (const char *filename, break; } - if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)) + if ((mapping) && (GNUNET_OK != GNUNET_DISK_file_unmap(mapping))) result = -1; if (GNUNET_OK != GNUNET_DISK_file_sync(file)) diff --git a/tests/test_gnunet_chat_file.c b/tests/test_gnunet_chat_file.c @@ -101,7 +101,7 @@ on_gnunet_chat_file_send_msg(void *cls, skip_search_account: if ((GNUNET_CHAT_KIND_LOGIN != kind) || (context)) - goto skip_first_login; + goto skip_file_upload; ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); @@ -120,22 +120,21 @@ skip_search_account: ck_assert_ptr_ne(group_context, NULL); -skip_first_login: - if (GNUNET_CHAT_KIND_JOIN != kind) - goto skip_file_upload; + char *filename = GNUNET_DISK_mktemp("gnunet_chat_file_send_name"); - ck_assert(kind == GNUNET_CHAT_KIND_JOIN); - ck_assert_ptr_ne(context, NULL); + ck_assert_ptr_ne(filename, NULL); struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file( - context, - "Makefile", + group_context, + filename, on_gnunet_chat_file_send_upload, handle ); ck_assert_ptr_ne(file, NULL); + GNUNET_free(filename); + skip_file_upload: if (GNUNET_CHAT_KIND_FILE != kind) return GNUNET_YES;