libgnunetchat

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

commit 668788871bf903fd8d487a2f63e7adc0b16b371e
parent 6dc2e8abf1f13d83d2645920859b581641233b75
Author: Jacki <jacki@thejackimonster.de>
Date:   Fri, 12 Apr 2024 16:18:17 +0200

Implement test case for sharing attributes

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

Diffstat:
Minclude/gnunet/gnunet_chat_lib.h | 6+++---
Msrc/gnunet_chat_handle.c | 2++
Msrc/gnunet_chat_handle.h | 5+++--
Msrc/gnunet_chat_lib.c | 8++++++--
Msrc/gnunet_chat_lib_intern.c | 21++++++++++++++-------
Msrc/gnunet_chat_ticket.c | 2+-
Msrc/gnunet_chat_ticket.h | 5++---
Msrc/gnunet_chat_util.c | 2+-
Mtests/test_gnunet_chat_attribute.c | 219++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
9 files changed, 249 insertions(+), 21 deletions(-)

diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h @@ -243,14 +243,14 @@ typedef enum GNUNET_GenericReturnValue * Iterator over accessible attributes of a specific chat contact. * * @param[in,out] cls Closure - * @param[in] contact Chat contact + * @param[in,out] contact Chat contact * @param[in] name Attribute name * @param[in] value Attribute value * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef enum GNUNET_GenericReturnValue (*GNUNET_CHAT_ContactAttributeCallback) (void *cls, - const struct GNUNET_CHAT_Contact *contact, + struct GNUNET_CHAT_Contact *contact, const char *name, const char *value); @@ -589,7 +589,7 @@ GNUNET_CHAT_unshare_attribute_from (struct GNUNET_CHAT_Handle *handle, */ void GNUNET_CHAT_get_shared_attributes (struct GNUNET_CHAT_Handle *handle, - const struct GNUNET_CHAT_Contact *contact, + struct GNUNET_CHAT_Contact *contact, GNUNET_CHAT_ContactAttributeCallback callback, void *cls); diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -95,6 +95,7 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, handle->accounts_head = NULL; handle->accounts_tail = NULL; + handle->next = NULL; handle->current = NULL; handle->monitor = NULL; @@ -412,6 +413,7 @@ handle_connect (struct GNUNET_CHAT_Handle *handle, handle ); + handle->next = NULL; handle->current = account; handle_update_identity(handle); } diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -94,7 +94,7 @@ struct GNUNET_CHAT_UriLookups struct GNUNET_CHAT_AttributeProcess { struct GNUNET_CHAT_Handle *handle; - const struct GNUNET_CHAT_Contact *contact; + struct GNUNET_CHAT_Contact *contact; struct GNUNET_RECLAIM_Attribute *attribute; char *name; @@ -112,7 +112,7 @@ struct GNUNET_CHAT_AttributeProcess struct GNUNET_CHAT_TicketProcess { struct GNUNET_CHAT_Handle *handle; - const struct GNUNET_CHAT_Contact *contact; + struct GNUNET_CHAT_Contact *contact; struct GNUNET_RECLAIM_Ticket *ticket; char *name; @@ -150,6 +150,7 @@ struct GNUNET_CHAT_Handle struct GNUNET_CHAT_InternalAccounts *accounts_head; struct GNUNET_CHAT_InternalAccounts *accounts_tail; + const struct GNUNET_CHAT_Account *next; const struct GNUNET_CHAT_Account *current; struct GNUNET_NAMESTORE_ZoneMonitor *monitor; diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -168,7 +168,11 @@ GNUNET_CHAT_connect (struct GNUNET_CHAT_Handle *handle, return; if (handle->current) - handle_disconnect(handle); + { + handle->next = account; + GNUNET_CHAT_disconnect(handle); + return; + } if (!account) return; @@ -557,7 +561,7 @@ GNUNET_CHAT_unshare_attribute_from (struct GNUNET_CHAT_Handle *handle, void GNUNET_CHAT_get_shared_attributes (struct GNUNET_CHAT_Handle *handle, - const struct GNUNET_CHAT_Contact *contact, + struct GNUNET_CHAT_Contact *contact, GNUNET_CHAT_ContactAttributeCallback callback, void *cls) { diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -78,6 +78,14 @@ task_handle_disconnection (void *cls) handle_disconnect(handle); handle->disconnection = NULL; + + if (! handle->next) + return; + + const struct GNUNET_CHAT_Account *account = handle->next; + handle->next = NULL; + + handle_connect(handle, account); } void @@ -593,13 +601,6 @@ cb_issue_ticket (void *cls, if ((context) && (context->room) && (ticket)) GNUNET_MESSENGER_send_ticket(context->room, ticket); - handle_send_internal_message( - handle, - NULL, - GNUNET_CHAT_FLAG_SHARED_ATTRIBUTES, - NULL - ); - GNUNET_CONTAINER_DLL_remove( handle->attributes_head, handle->attributes_tail, @@ -990,6 +991,12 @@ cb_iterate_ticket (void *cls, tick ); + if (! tick->op) + { + GNUNET_free(tick); + return; + } + GNUNET_CONTAINER_DLL_insert_tail( handle->tickets_head, handle->tickets_tail, diff --git a/src/gnunet_chat_ticket.c b/src/gnunet_chat_ticket.c @@ -34,7 +34,7 @@ struct GNUNET_CHAT_Ticket* ticket_create_from_message (struct GNUNET_CHAT_Handle *handle, - const struct GNUNET_CHAT_Contact *issuer, + struct GNUNET_CHAT_Contact *issuer, const struct GNUNET_MESSENGER_MessageTicket *message) { GNUNET_assert((handle) && (issuer) && (message)); diff --git a/src/gnunet_chat_ticket.h b/src/gnunet_chat_ticket.h @@ -34,8 +34,7 @@ struct GNUNET_CHAT_Ticket { struct GNUNET_CHAT_Handle *handle; - - const struct GNUNET_CHAT_Contact *issuer; + struct GNUNET_CHAT_Contact *issuer; GNUNET_CHAT_ContactAttributeCallback callback; void *closure; @@ -57,7 +56,7 @@ struct GNUNET_CHAT_Ticket */ struct GNUNET_CHAT_Ticket* ticket_create_from_message (struct GNUNET_CHAT_Handle *handle, - const struct GNUNET_CHAT_Contact *issuer, + struct GNUNET_CHAT_Contact *issuer, const struct GNUNET_MESSENGER_MessageTicket *message); /** diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -420,7 +420,7 @@ util_message_kind_from_kind (enum GNUNET_MESSENGER_MessageKind kind) case GNUNET_MESSENGER_KIND_DELETE: return GNUNET_CHAT_KIND_DELETION; case GNUNET_MESSENGER_KIND_TICKET: - return GNUNET_CHAT_KIND_CONTACT; + return GNUNET_CHAT_KIND_SHARED_ATTRIBUTES; case GNUNET_MESSENGER_KIND_TAG: return GNUNET_CHAT_KIND_TAG; default: diff --git a/tests/test_gnunet_chat_attribute.c b/tests/test_gnunet_chat_attribute.c @@ -77,8 +77,8 @@ on_gnunet_chat_attribute_check_attr(void *cls, enum GNUNET_GenericReturnValue on_gnunet_chat_attribute_check_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 @@ -160,10 +160,225 @@ call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg) ), GNUNET_OK); } +enum GNUNET_GenericReturnValue +on_gnunet_chat_attribute_share_it1(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_attribute_share_1")) + { + GNUNET_CHAT_connect(chat, account); + return GNUNET_NO; + } + + return GNUNET_YES; +} + +enum GNUNET_GenericReturnValue +on_gnunet_chat_attribute_share_it2(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_ne(GNUNET_CHAT_get_connected(handle), NULL); + + if (0 == strcmp(name, "gnunet_chat_attribute_share_2")) + { + GNUNET_CHAT_connect(chat, account); + return GNUNET_NO; + } + + return GNUNET_YES; +} + +enum GNUNET_GenericReturnValue +on_gnunet_chat_attribute_share_attr(void *cls, + struct GNUNET_CHAT_Contact *contact, + const char *name, + const char *value) +{ + ck_assert_ptr_ne(cls, NULL); + ck_assert_ptr_ne(contact, NULL); + ck_assert_ptr_ne(name, NULL); + + struct GNUNET_CHAT_Handle *handle = ( + (struct GNUNET_CHAT_Handle*) cls + ); + + if (0 == strcmp(name, "test_attribute_name")) + { + ck_assert_ptr_ne(value, NULL); + ck_assert_int_eq(strcmp(value, "test_attribute_value"), 0); + + GNUNET_CHAT_unshare_attribute_from(handle, contact, "test_attribute_name"); + return GNUNET_NO; + } + + return GNUNET_YES; +} + +enum GNUNET_GenericReturnValue +on_gnunet_chat_attribute_share_msg(void *cls, + struct GNUNET_CHAT_Context *context, + const struct GNUNET_CHAT_Message *message) +{ + struct GNUNET_CHAT_Handle *handle = *( + (struct GNUNET_CHAT_Handle**) cls + ); + + const struct GNUNET_CHAT_Account *account = NULL; + struct GNUNET_CHAT_Contact *sender = NULL; + + ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_ne(message, NULL); + + if (GNUNET_CHAT_get_connected(handle)) + goto skip_search_account; + + GNUNET_CHAT_iterate_accounts( + handle, + on_gnunet_chat_attribute_share_it1, + handle + ); + +skip_search_account: + account = GNUNET_CHAT_get_connected(handle); + sender = GNUNET_CHAT_message_get_sender(message); + + if (!account) + return GNUNET_YES; + + switch (GNUNET_CHAT_message_get_kind(message)) + { + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_eq(context, NULL); + + ck_assert_ptr_ne(GNUNET_CHAT_group_create( + handle, "test_attribute_group" + ), NULL); + break; + default: + break; + } + + if (0 != strcmp(GNUNET_CHAT_account_get_name(account), + "gnunet_chat_attribute_share_2")) + { + switch (GNUNET_CHAT_message_get_kind(message)) { + case GNUNET_CHAT_KIND_JOIN: + ck_assert_ptr_ne(context, NULL); + + GNUNET_CHAT_iterate_accounts( + handle, + on_gnunet_chat_attribute_share_it2, + handle + ); + break; + default: + break; + } + + return GNUNET_YES; + } + + switch (GNUNET_CHAT_message_get_kind(message)) + { + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_eq(context, NULL); + + GNUNET_CHAT_set_attribute( + handle, + "test_attribute_name", + "test_attribute_value", + GNUNET_TIME_relative_get_forever_() + ); + + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_eq(context, NULL); + + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, + "gnunet_chat_attribute_share_1" + ), GNUNET_OK); + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, + "gnunet_chat_attribute_share_2" + ), GNUNET_OK); + + GNUNET_CHAT_stop(handle); + break; + case GNUNET_CHAT_KIND_JOIN: + ck_assert_ptr_ne(context, NULL); + ck_assert_ptr_ne(sender, NULL); + + if (GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) + GNUNET_CHAT_share_attribute_with(handle, sender, "test_attribute_name"); + break; + case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: + if (GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) + { + GNUNET_CHAT_disconnect(handle); + break; + } + + GNUNET_CHAT_get_shared_attributes( + handle, + GNUNET_CHAT_message_get_recipient(message), + on_gnunet_chat_attribute_share_attr, + handle + ); + + break; + default: + break; + } + + return GNUNET_YES; +} + +void +call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + static struct GNUNET_CHAT_Handle *handle = NULL; + handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_share_msg, &handle); + + ck_assert_ptr_ne(handle, NULL); + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + "gnunet_chat_attribute_share_1" + ), GNUNET_OK); + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + "gnunet_chat_attribute_share_2" + ), GNUNET_OK); +} + CREATE_GNUNET_TEST(test_gnunet_chat_attribute_check, call_gnunet_chat_attribute_check) +CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, call_gnunet_chat_attribute_share) START_SUITE(handle_suite, "Attribute") ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_check, "Check") +ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_share, "Share") END_SUITE MAIN_SUITE(handle_suite, CK_NORMAL)