libgnunetchat

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

commit 638df3525e10f8e116c13ab9b078e0f25e286036
parent 49553eef2927c9e57600b38c9a55aa7c25ca1093
Author: Jacki <jacki@thejackimonster.de>
Date:   Sun,  5 May 2024 16:32:31 +0200

Adjust ticket management, complete test case changes, bump library version and fix multiple crashes

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

Diffstat:
MChangeLog | 4++++
MDoxyfile | 2+-
Minclude/gnunet/gnunet_chat_lib.h | 2+-
Msrc/gnunet_chat_context.c | 3++-
Msrc/gnunet_chat_handle.c | 11++++++++---
Msrc/gnunet_chat_handle_intern.c | 5+++--
Msrc/gnunet_chat_lib.c | 1+
Msrc/gnunet_chat_lib_intern.c | 157++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Msrc/gnunet_chat_ticket.c | 15+++++++++++----
Mtests/test_gnunet_chat_attribute.c | 344+++++++++++++++++++++++++++++++++++--------------------------------------------
Mtests/test_gnunet_chat_lobby.c | 1+
Mtests/test_gnunet_chat_message.c | 117+++++++++++++++++++++++++++++++++++++++----------------------------------------
12 files changed, 343 insertions(+), 319 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,7 @@ +## Version 0.4.0 +* This release requires the GNUnet Messenger Service 0.4! +* ... + ## Version 0.3.1 * Makes dependency to libcheck optional (only required for building test cases) * Fixes file iteration of chat context diff --git a/Doxyfile b/Doxyfile @@ -48,7 +48,7 @@ PROJECT_NAME = libgnunetchat # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 0.3.1 +PROJECT_NUMBER = 0.4.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h @@ -43,7 +43,7 @@ * the #GNUNET_MESSENGER_VERSION of the GNUnet Messenger * service while the patch version is independent. */ -#define GNUNET_CHAT_VERSION 0x000000030001L +#define GNUNET_CHAT_VERSION 0x000000040000L #define GNUNET_CHAT_VERSION_MAJOR ((GNUNET_CHAT_VERSION >> 32L) & 0xFFFFL) #define GNUNET_CHAT_VERSION_MINOR ((GNUNET_CHAT_VERSION >> 16L) & 0xFFFFL) diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c @@ -236,7 +236,8 @@ context_update_nick (struct GNUNET_CHAT_Context *context, else context->nick = NULL; - if (!(context->handle)) + if ((!(context->handle)) || + (GNUNET_YES == context->deleted)) return; handle_send_internal_message( diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -864,11 +864,16 @@ handle_request_context_by_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_CheckHandleRoomMembers check; - if ((context) && (context->type == GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN)) + if (!context) + goto new_context; + + if ((GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN == context->type) && + (GNUNET_YES != context->deleted)) goto check_type; - else if (context) - return GNUNET_OK; + + return GNUNET_OK; +new_context: context = context_create_from_room(handle, room); if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -855,8 +855,9 @@ skip_msg_handing: struct GNUNET_CHAT_InternalTickets *tickets = contact->tickets_head; while (tickets) { - if (0 == GNUNET_memcmp(&(tickets->ticket->ticket.rnd), - &(message->msg->body.ticket.identifier))) + if (0 == strncmp(tickets->ticket->ticket.gns_name, + message->msg->body.ticket.identifier, + sizeof(tickets->ticket->ticket.gns_name))) break; tickets = tickets->next; diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -1315,6 +1315,7 @@ GNUNET_CHAT_group_leave (struct GNUNET_CHAT_Group *group) if ((!group) || (group->destruction)) return GNUNET_SYSERR; + group->context->deleted = GNUNET_YES; group->destruction = GNUNET_SCHEDULER_add_now( task_group_destruction, group diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -270,27 +270,21 @@ task_group_destruction (void *cls) GNUNET_assert(cls); struct GNUNET_CHAT_Group *group = (struct GNUNET_CHAT_Group*) cls; + struct GNUNET_HashCode key; - const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key( + GNUNET_memcpy(&key, GNUNET_MESSENGER_room_get_key( group->context->room - ); + ), sizeof(key)); - GNUNET_CONTAINER_multihashmap_remove( - group->handle->groups, key, group - ); + GNUNET_MESSENGER_close_room(group->context->room); GNUNET_CONTAINER_multihashmap_remove( - group->handle->contexts, key, group->context + group->handle->groups, &key, group ); - GNUNET_MESSENGER_close_room(group->context->room); - - group->context->deleted = GNUNET_YES; context_write_records(group->context); - group->destruction = NULL; - context_destroy(group->context); group_destroy(group); } @@ -690,14 +684,35 @@ cb_issue_ticket (void *cls, attributes->op = NULL; + if ((!(attributes->contact)) || (!(attributes->contact->member))) + goto skip_sending; + struct GNUNET_CHAT_Context *context = contact_find_context( attributes->contact, GNUNET_YES ); - if ((context) && (ticket)) - GNUNET_MESSENGER_send_ticket(context->room, ticket); + if ((!context) || (!ticket)) + goto skip_sending; + + char *identifier = GNUNET_strdup(ticket->gns_name); + + if (!identifier) + goto skip_sending; + + struct GNUNET_MESSENGER_Message message; + message.header.kind = GNUNET_MESSENGER_KIND_TICKET; + message.body.ticket.identifier = identifier; + + GNUNET_MESSENGER_send_message( + context->room, + &message, + attributes->contact->member + ); + + GNUNET_free(identifier); +skip_sending: internal_attributes_destroy(attributes); } @@ -759,41 +774,52 @@ cb_share_attribute (void *cls, struct GNUNET_CHAT_Handle *handle = attributes->handle; - if (0 == strcmp(attribute->name, attributes->name)) + if (0 != strcmp(attribute->name, attributes->name)) { - internal_attributes_stop_iter(attributes); + internal_attributes_next_iter(attributes); + return; + } + + internal_attributes_stop_iter(attributes); - GNUNET_free(attributes->name); - attributes->name = NULL; + GNUNET_free(attributes->name); + attributes->name = NULL; - const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key( - handle - ); + const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key( + handle + ); - const struct GNUNET_CRYPTO_PublicKey *pubkey = contact_get_key( - attributes->contact - ); + if (!key) + return; - struct GNUNET_RECLAIM_AttributeList *attrs; - attrs = attribute_list_from_attribute(attribute); + const struct GNUNET_CRYPTO_PublicKey *pubkey = contact_get_key( + attributes->contact + ); - if ((key) && (pubkey) && (attrs)) - attributes->op = GNUNET_RECLAIM_ticket_issue( - handle->reclaim, - key, - pubkey, - attrs, - cb_issue_ticket, - attributes - ); - - if (attrs) - GNUNET_RECLAIM_attribute_list_destroy(attrs); - + if (!pubkey) return; - } - internal_attributes_next_iter(attributes); + char *rp_uri = GNUNET_CRYPTO_public_key_to_string(pubkey); + + struct GNUNET_RECLAIM_AttributeList *attrs; + attrs = attribute_list_from_attribute(attribute); + + if (!attrs) + goto cleanup; + + attributes->op = GNUNET_RECLAIM_ticket_issue( + handle->reclaim, + key, + rp_uri, + attrs, + cb_issue_ticket, + attributes + ); + + GNUNET_RECLAIM_attribute_list_destroy(attrs); + +cleanup: + GNUNET_free(rp_uri); } void @@ -925,9 +951,33 @@ cb_consume_ticket_check (void *cls, } } +static enum GNUNET_GenericReturnValue +is_contact_ticket_audience (const struct GNUNET_CHAT_Contact *contact, + const char *rp_uri) +{ + GNUNET_assert((contact) && (rp_uri)); + + const struct GNUNET_CRYPTO_PublicKey *pubkey; + pubkey = contact_get_key(contact); + + if (!pubkey) + return GNUNET_NO; + + struct GNUNET_CRYPTO_PublicKey audience; + enum GNUNET_GenericReturnValue parsing; + + parsing = GNUNET_CRYPTO_public_key_from_string(rp_uri, &audience); + + if ((GNUNET_OK != parsing) || (0 != GNUNET_memcmp(pubkey, &audience))) + return GNUNET_NO; + + return GNUNET_YES; +} + void cb_iterate_ticket_check (void *cls, - const struct GNUNET_RECLAIM_Ticket *ticket) + const struct GNUNET_RECLAIM_Ticket *ticket, + const char *rp_uri) { GNUNET_assert(cls); @@ -936,14 +986,9 @@ cb_iterate_ticket_check (void *cls, ); struct GNUNET_CHAT_Handle *handle = tickets->handle; - const struct GNUNET_CRYPTO_PublicKey *pubkey; - - if (tickets->contact) - pubkey = contact_get_key(tickets->contact); - else - pubkey = NULL; - if ((!pubkey) || (0 != GNUNET_memcmp(pubkey, &(ticket->audience)))) + if ((!rp_uri) || (!(tickets->contact)) || + (GNUNET_YES != is_contact_ticket_audience(tickets->contact, rp_uri))) { internal_tickets_next_iter(tickets); return; @@ -970,8 +1015,8 @@ cb_iterate_ticket_check (void *cls, new_tickets->op = GNUNET_RECLAIM_ticket_consume( handle->reclaim, - key, ticket, + //rp_uri, cb_consume_ticket_check, new_tickets ); @@ -1017,7 +1062,8 @@ cb_consume_ticket (void *cls, void cb_iterate_ticket (void *cls, - const struct GNUNET_RECLAIM_Ticket *ticket) + const struct GNUNET_RECLAIM_Ticket *ticket, + const char *rp_uri) { GNUNET_assert(cls); @@ -1026,14 +1072,9 @@ cb_iterate_ticket (void *cls, ); struct GNUNET_CHAT_Handle *handle = tickets->handle; - const struct GNUNET_CRYPTO_PublicKey *pubkey; - - if (tickets->contact) - pubkey = contact_get_key(tickets->contact); - else - pubkey = NULL; - if ((!pubkey) || (0 != GNUNET_memcmp(pubkey, &(ticket->audience)))) + if ((!rp_uri) || (!(tickets->contact)) || + (GNUNET_YES != is_contact_ticket_audience(tickets->contact, rp_uri))) { internal_tickets_next_iter(tickets); return; @@ -1060,8 +1101,8 @@ cb_iterate_ticket (void *cls, new_tickets->op = GNUNET_RECLAIM_ticket_consume( handle->reclaim, - key, ticket, + //rp_uri, cb_consume_ticket, new_tickets ); diff --git a/src/gnunet_chat_ticket.c b/src/gnunet_chat_ticket.c @@ -28,6 +28,7 @@ #include "gnunet_chat_ticket_intern.c" #include "gnunet_chat_handle.h" +#include <gnunet/gnunet_common.h> #include <gnunet/gnunet_messenger_service.h> #include <gnunet/gnunet_reclaim_service.h> #include <string.h> @@ -58,9 +59,8 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle, ticket->op = NULL; - GNUNET_memcpy(&(ticket->ticket.identity), identity, sizeof(ticket->ticket.identity)); - GNUNET_memcpy(&(ticket->ticket.audience), audience, sizeof(ticket->ticket.audience)); - GNUNET_memcpy(&(ticket->ticket.rnd), &(message->identifier), sizeof(ticket->ticket.rnd)); + strncpy(ticket->ticket.gns_name, message->identifier, sizeof(ticket->ticket.gns_name)); + ticket->ticket.gns_name[sizeof(ticket->ticket.gns_name) - 1] = '\0'; return ticket; } @@ -79,6 +79,11 @@ ticket_consume(struct GNUNET_CHAT_Ticket *ticket, if (!key) return; + struct GNUNET_CRYPTO_PublicKey pubkey; + GNUNET_CRYPTO_key_get_public(key, &pubkey); + + char *rp_uri = GNUNET_CRYPTO_public_key_to_string(&pubkey); + ticket->callback = callback; ticket->closure = cls; @@ -87,11 +92,13 @@ ticket_consume(struct GNUNET_CHAT_Ticket *ticket, ticket->op = GNUNET_RECLAIM_ticket_consume( ticket->handle->reclaim, - key, &(ticket->ticket), + //rp_uri, cb_ticket_consume_attribute, ticket ); + + GNUNET_free(rp_uri); } void diff --git a/tests/test_gnunet_chat_attribute.c b/tests/test_gnunet_chat_attribute.c @@ -23,35 +23,15 @@ */ #include "test_gnunet_chat.h" -#include <check.h> -#include <gnunet/gnunet_chat_lib.h> -#include <gnunet/gnunet_time_lib.h> -#include <stdio.h> -enum GNUNET_GenericReturnValue -on_gnunet_chat_attribute_check_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_attribute_check")) - { - GNUNET_CHAT_connect(chat, account); - return GNUNET_NO; - } - - return GNUNET_YES; -} +#define TEST_CHECK_ID "gnunet_chat_attribute_check" +#define TEST_CHECK_NAME "test_attribute_check_name" +#define TEST_CHECK_VALUE "test_attribute_check_value" +#define TEST_SHARE_ID_A "gnunet_chat_attribute_share_a" +#define TEST_SHARE_ID_B "gnunet_chat_attribute_share_b" +#define TEST_SHARE_GROUP "test_attribute_share_group" +#define TEST_SHARE_NAME "test_attribute_share_name" +#define TEST_SHARE_VALUE "test_attribute_share_value" enum GNUNET_GenericReturnValue on_gnunet_chat_attribute_check_attr(void *cls, @@ -59,16 +39,16 @@ on_gnunet_chat_attribute_check_attr(void *cls, const char *name, const char *value) { - ck_assert_ptr_eq(cls, NULL); - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_ne(name, NULL); + ck_assert_ptr_null(cls); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(name); - if (0 == strcmp(name, "test_attribute_name")) + if (0 == strcmp(name, TEST_CHECK_NAME)) { - ck_assert_ptr_ne(value, NULL); - ck_assert_int_eq(strcmp(value, "test_attribute_value"), 0); + ck_assert_ptr_nonnull(value); + ck_assert_str_eq(value, TEST_CHECK_VALUE); - GNUNET_CHAT_delete_attribute(handle, "test_attribute_name"); + GNUNET_CHAT_delete_attribute(handle, TEST_CHECK_NAME); return GNUNET_NO; } @@ -84,52 +64,63 @@ on_gnunet_chat_attribute_check_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - const char *text = NULL; - - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_ne(message, NULL); - - if (GNUNET_CHAT_get_connected(handle)) - goto skip_search_account; + const struct GNUNET_CHAT_Account *account; + const char *text; - GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_attribute_check_it, - handle - ); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(message); - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; + account = GNUNET_CHAT_message_get_account(message); -skip_search_account: 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_eq(context, NULL); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); GNUNET_CHAT_set_attribute( handle, - "test_attribute_name", - "test_attribute_value", + TEST_CHECK_NAME, + TEST_CHECK_VALUE, GNUNET_TIME_relative_get_forever_() ); break; case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_int_eq(GNUNET_CHAT_account_delete( handle, - "gnunet_chat_attribute_check" + TEST_CHECK_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: + ck_assert_ptr_nonnull(account); + break; case GNUNET_CHAT_KIND_ATTRIBUTES: - ck_assert_ptr_eq(context, NULL); + ck_assert_ptr_null(context); text = GNUNET_CHAT_message_get_text(message); if (text) { - ck_assert_int_eq(strcmp(text, "test_attribute_name"), 0); + ck_assert_str_eq(text, TEST_CHECK_NAME); + GNUNET_CHAT_get_attributes( handle, on_gnunet_chat_attribute_check_attr, @@ -141,6 +132,7 @@ skip_search_account: break; default: + ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); break; } @@ -153,87 +145,39 @@ call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_check_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( handle, - "gnunet_chat_attribute_check" + TEST_CHECK_ID ), 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); + ck_assert_ptr_nonnull(cls); + ck_assert_ptr_nonnull(contact); + ck_assert_ptr_nonnull(name); + ck_assert_ptr_nonnull(value); 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); + const struct GNUNET_CHAT_Account *account; + account = GNUNET_CHAT_get_connected(handle); - GNUNET_CHAT_unshare_attribute_from(handle, contact, "test_attribute_name"); - return GNUNET_NO; - } + ck_assert_ptr_nonnull(account); + ck_assert_str_eq(GNUNET_CHAT_account_get_name(account), TEST_SHARE_ID_B); + ck_assert_str_eq(GNUNET_CHAT_contact_get_name(contact), TEST_SHARE_ID_A); + ck_assert_str_eq(name, TEST_SHARE_NAME); + ck_assert_str_eq(value, TEST_SHARE_VALUE); - return GNUNET_YES; + GNUNET_CHAT_unshare_attribute_from(handle, contact, name); + return GNUNET_NO; } enum GNUNET_GenericReturnValue @@ -245,102 +189,125 @@ on_gnunet_chat_attribute_share_msg(void *cls, (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); + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Contact *sender; + const char *name; - if (GNUNET_CHAT_get_connected(handle)) - goto skip_search_account; + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(message); - GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_attribute_share_it1, - handle - ); - -skip_search_account: - account = GNUNET_CHAT_get_connected(handle); + account = GNUNET_CHAT_message_get_account(message); 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); + case GNUNET_CHAT_KIND_WARNING: + ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); break; - default: + case GNUNET_CHAT_KIND_REFRESH: break; - } + case GNUNET_CHAT_KIND_LOGIN: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); - 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); + name = GNUNET_CHAT_account_get_name(account); - GNUNET_CHAT_iterate_accounts( + ck_assert_ptr_nonnull(name); + + if (0 == strcmp(name, TEST_SHARE_ID_B)) + GNUNET_CHAT_set_attribute( handle, - on_gnunet_chat_attribute_share_it2, - handle + TEST_SHARE_NAME, + TEST_SHARE_VALUE, + GNUNET_TIME_relative_get_forever_() ); - break; - default: - break; - } - return GNUNET_YES; - } + ck_assert_ptr_nonnull(GNUNET_CHAT_group_create( + handle, TEST_SHARE_GROUP + )); + break; + case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); - switch (GNUNET_CHAT_message_get_kind(message)) - { - case GNUNET_CHAT_KIND_LOGIN: - ck_assert_ptr_eq(context, NULL); + name = GNUNET_CHAT_account_get_name(account); - GNUNET_CHAT_set_attribute( - handle, - "test_attribute_name", - "test_attribute_value", - GNUNET_TIME_relative_get_forever_() - ); + ck_assert_ptr_nonnull(name); - break; - case GNUNET_CHAT_KIND_LOGOUT: - ck_assert_ptr_eq(context, NULL); + if (0 == strcmp(name, TEST_SHARE_ID_A)) + break; 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" + handle, TEST_SHARE_ID_A ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_CREATED_ACCOUNT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); + break; + case GNUNET_CHAT_KIND_DELETED_ACCOUNT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + + name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_nonnull(name); + + if (0 == strcmp(name, TEST_SHARE_ID_A)) + ck_assert_int_eq(GNUNET_CHAT_account_delete( + handle, TEST_SHARE_ID_B + ), GNUNET_OK); + else + GNUNET_CHAT_stop(handle); - GNUNET_CHAT_stop(handle); + break; + case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: + 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_ne(context, NULL); - ck_assert_ptr_ne(sender, NULL); + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(account); + ck_assert_ptr_nonnull(sender); - if (GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) - GNUNET_CHAT_share_attribute_with(handle, sender, "test_attribute_name"); + name = GNUNET_CHAT_account_get_name(account); + + ck_assert_ptr_nonnull(name); + + if (0 == strcmp(name, TEST_SHARE_ID_A)) + ck_assert_int_eq(GNUNET_CHAT_account_create( + handle, + TEST_SHARE_ID_B + ), GNUNET_OK); + else if (GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) + GNUNET_CHAT_share_attribute_with(handle, sender, TEST_SHARE_NAME); + + break; + case GNUNET_CHAT_KIND_CONTACT: + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(sender); + break; + case GNUNET_CHAT_KIND_ATTRIBUTES: + ck_assert_ptr_null(context); break; case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: + ck_assert_ptr_nonnull(context); + ck_assert_ptr_nonnull(sender); + if (GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) { + printf("exit?\n"); + GNUNET_CHAT_disconnect(handle); break; } + printf("list attributes?\n"); + GNUNET_CHAT_get_shared_attributes( handle, GNUNET_CHAT_message_get_recipient(message), @@ -350,6 +317,7 @@ skip_search_account: break; default: + ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); break; } @@ -362,14 +330,10 @@ 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_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( handle, - "gnunet_chat_attribute_share_2" + TEST_SHARE_ID_A ), GNUNET_OK); } diff --git a/tests/test_gnunet_chat_lobby.c b/tests/test_gnunet_chat_lobby.c @@ -86,6 +86,7 @@ on_gnunet_chat_lobby_base_msg(void *cls, GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: + ck_assert_ptr_nonnull(account); break; default: ck_abort(); diff --git a/tests/test_gnunet_chat_message.c b/tests/test_gnunet_chat_message.c @@ -23,34 +23,10 @@ */ #include "test_gnunet_chat.h" -#include <check.h> -#include <gnunet/gnunet_chat_lib.h> -#include <stdio.h> -enum GNUNET_GenericReturnValue -on_gnunet_chat_message_text_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_message_text")) - { - GNUNET_CHAT_connect(chat, account); - return GNUNET_NO; - } - - return GNUNET_YES; -} +#define TEST_TEXT_ID "gnunet_chat_message_text" +#define TEST_TEXT_GROUP "gnunet_chat_message_text_group" +#define TEST_TEXT_MSG "test_text_message" enum GNUNET_GenericReturnValue on_gnunet_chat_message_text_msg(void *cls, @@ -61,59 +37,83 @@ on_gnunet_chat_message_text_msg(void *cls, (struct GNUNET_CHAT_Handle**) cls ); - struct GNUNET_CHAT_Group *group = NULL; - const char *text = NULL; - - ck_assert_ptr_ne(handle, NULL); - ck_assert_ptr_ne(message, NULL); - - if (GNUNET_CHAT_get_connected(handle)) - goto skip_search_account; + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Group *group; + const char *text; - GNUNET_CHAT_iterate_accounts( - handle, - on_gnunet_chat_message_text_it, - handle - ); + ck_assert_ptr_nonnull(handle); + ck_assert_ptr_nonnull(message); - if (!GNUNET_CHAT_get_connected(handle)) - return GNUNET_YES; + account = GNUNET_CHAT_message_get_account(message); -skip_search_account: 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_eq(context, NULL); + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); - group = GNUNET_CHAT_group_create(handle, "gnunet_chat_message"); - ck_assert_ptr_ne(group, NULL); + group = GNUNET_CHAT_group_create(handle, TEST_TEXT_GROUP); - ck_assert_int_eq(GNUNET_CHAT_context_send_text( - GNUNET_CHAT_group_get_context(group), "test_text_message" - ), GNUNET_OK); + ck_assert_ptr_nonnull(group); break; case GNUNET_CHAT_KIND_LOGOUT: + ck_assert_ptr_null(context); + ck_assert_ptr_nonnull(account); + ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - "gnunet_chat_message_text" + handle, TEST_TEXT_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: + 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_int_eq(GNUNET_CHAT_context_send_text( + context, TEST_TEXT_MSG + ), GNUNET_OK); + break; + case GNUNET_CHAT_KIND_LEAVE: + ck_assert_ptr_nonnull(context); + + GNUNET_CHAT_disconnect(handle); + break; + case GNUNET_CHAT_KIND_CONTACT: + ck_assert_ptr_nonnull(context); + break; case GNUNET_CHAT_KIND_TEXT: - ck_assert_ptr_ne(context, NULL); + ck_assert_ptr_nonnull(context); group = GNUNET_CHAT_context_get_group(context); - ck_assert_ptr_ne(group, NULL); + + ck_assert_ptr_nonnull(group); text = GNUNET_CHAT_message_get_text(message); - ck_assert_int_eq(strcmp(text, "test_text_message"), 0); + ck_assert_str_eq(text, TEST_TEXT_MSG); ck_assert_int_eq(GNUNET_CHAT_group_leave(group), GNUNET_OK); - - GNUNET_CHAT_disconnect(handle); break; default: + ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); break; } @@ -126,10 +126,9 @@ call_gnunet_chat_message_text(const struct GNUNET_CONFIGURATION_Handle *cfg) static struct GNUNET_CHAT_Handle *handle = NULL; handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_message_text_msg, &handle); - ck_assert_ptr_ne(handle, NULL); + ck_assert_ptr_nonnull(handle); ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - "gnunet_chat_message_text" + handle, TEST_TEXT_ID ), GNUNET_OK); }