commit bfea5275133bf085e0ee74a0e09f87e94aafa245 parent 6e32f4c763daa53b8961abcbbc4f1d08f923f843 Author: Jacki <jacki@thejackimonster.de> Date: Mon, 26 Aug 2024 02:41:13 +0200 Update tests with setup and cleanup per test case Signed-off-by: Jacki <jacki@thejackimonster.de> Diffstat:
14 files changed, 397 insertions(+), 272 deletions(-)
diff --git a/tests/attribute/test_gnunet_chat_attribute_check.c b/tests/attribute/test_gnunet_chat_attribute_check.c @@ -73,6 +73,14 @@ on_gnunet_chat_attribute_check_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_CHECK_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -89,19 +97,6 @@ on_gnunet_chat_attribute_check_msg(void *cls, ck_assert_ptr_null(context); ck_assert_ptr_nonnull(account); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - 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: @@ -134,6 +129,8 @@ on_gnunet_chat_attribute_check_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_attribute_check, TEST_CHECK_ID) + void call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -141,13 +138,9 @@ call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_check_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_CHECK_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_attribute_check, call_gnunet_chat_attribute_check) +CREATE_GNUNET_TEST(test_gnunet_chat_attribute_check, gnunet_chat_attribute_check) START_SUITE(handle_suite, "Attribute") ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_check, "Check") diff --git a/tests/attribute/test_gnunet_chat_attribute_share.c b/tests/attribute/test_gnunet_chat_attribute_share.c @@ -66,6 +66,8 @@ on_gnunet_chat_attribute_share_msg(void *cls, struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message) { + static unsigned int share_stage = 0; + struct GNUNET_CHAT_Handle *handle = *( (struct GNUNET_CHAT_Handle**) cls ); @@ -88,6 +90,20 @@ on_gnunet_chat_attribute_share_msg(void *cls, 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); + + if (GNUNET_CHAT_get_connected(handle)) + break; + + ck_assert_uint_eq(share_stage, 0); + + account = GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_A); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); + share_stage = 1; break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -96,8 +112,12 @@ on_gnunet_chat_attribute_share_msg(void *cls, name = GNUNET_CHAT_account_get_name(account); ck_assert_ptr_nonnull(name); + ck_assert_uint_ge(share_stage, 1); if (0 == strcmp(name, TEST_SHARE_ID_B)) + { + ck_assert_uint_eq(share_stage, 4); + GNUNET_CHAT_set_attribute( handle, TEST_SHARE_NAME, @@ -105,46 +125,34 @@ on_gnunet_chat_attribute_share_msg(void *cls, GNUNET_TIME_relative_get_forever_() ); + share_stage = 5; + } + ck_assert_ptr_nonnull(GNUNET_CHAT_group_create( handle, TEST_SHARE_GROUP )); + + if (share_stage < 2) + share_stage = 2; break; case GNUNET_CHAT_KIND_LOGOUT: ck_assert_ptr_null(context); ck_assert_ptr_nonnull(account); + ck_assert_uint_ge(share_stage, 3); name = GNUNET_CHAT_account_get_name(account); ck_assert_ptr_nonnull(name); + if (share_stage < 4) + share_stage = 4; + if (0 == strcmp(name, TEST_SHARE_ID_A)) break; - ck_assert_int_eq(GNUNET_CHAT_account_delete( - 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); + ck_assert_uint_eq(share_stage, 7); + GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: ck_assert_ptr_nonnull(account); @@ -161,13 +169,25 @@ on_gnunet_chat_attribute_share_msg(void *cls, 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)) + if ((0 == strcmp(name, TEST_SHARE_ID_A)) && + (GNUNET_YES == GNUNET_CHAT_message_is_sent(message))) + { + ck_assert_uint_eq(share_stage, 2); + + GNUNET_CHAT_connect( + handle, + GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_B) + ); + + share_stage = 3; + } + else if ((GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) && + (share_stage == 5)) + { GNUNET_CHAT_share_attribute_with(handle, sender, TEST_SHARE_NAME); + share_stage = 6; + } + break; case GNUNET_CHAT_KIND_CONTACT: ck_assert_ptr_nonnull(context); @@ -175,8 +195,11 @@ on_gnunet_chat_attribute_share_msg(void *cls, break; case GNUNET_CHAT_KIND_ATTRIBUTES: ck_assert_ptr_null(context); + ck_assert_uint_ge(share_stage, 5); break; case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: + ck_assert_uint_ge(share_stage, 6); + if (!context) { ck_assert_ptr_null(account); @@ -184,6 +207,7 @@ on_gnunet_chat_attribute_share_msg(void *cls, ck_assert_ptr_null(recipient); GNUNET_CHAT_disconnect(handle); + share_stage = 7; } else if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)) { @@ -208,6 +232,15 @@ on_gnunet_chat_attribute_share_msg(void *cls, return GNUNET_YES; } +const char *TEST_SHARE_IDS [] = { + TEST_SHARE_ID_A, + TEST_SHARE_ID_B, + NULL +}; + +SETUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) +CLEANUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) + void call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -215,13 +248,9 @@ call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_share_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_SHARE_ID_A - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, call_gnunet_chat_attribute_share) +CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, gnunet_chat_attribute_share) START_SUITE(handle_suite, "Attribute") ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_share, "Share") diff --git a/tests/discourse/test_gnunet_chat_discourse_open.c b/tests/discourse/test_gnunet_chat_discourse_open.c @@ -55,6 +55,14 @@ on_gnunet_chat_discourse_open_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_OPEN_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -67,21 +75,7 @@ on_gnunet_chat_discourse_open_msg(void *cls, case GNUNET_CHAT_KIND_LOGOUT: ck_assert_ptr_null(context); ck_assert_ptr_nonnull(account); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - TEST_OPEN_ID - ), 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); - + GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: @@ -126,6 +120,8 @@ on_gnunet_chat_discourse_open_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_open, TEST_OPEN_ID) + void call_gnunet_chat_discourse_open(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -133,12 +129,9 @@ call_gnunet_chat_discourse_open(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_open_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_OPEN_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_discourse_open, call_gnunet_chat_discourse_open) +CREATE_GNUNET_TEST(test_gnunet_chat_discourse_open, gnunet_chat_discourse_open) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_open, "Open/Close") diff --git a/tests/discourse/test_gnunet_chat_discourse_write.c b/tests/discourse/test_gnunet_chat_discourse_write.c @@ -55,6 +55,14 @@ on_gnunet_chat_discourse_write_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_WRITE_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -67,21 +75,7 @@ on_gnunet_chat_discourse_write_msg(void *cls, case GNUNET_CHAT_KIND_LOGOUT: ck_assert_ptr_null(context); ck_assert_ptr_nonnull(account); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, - TEST_WRITE_ID - ), 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); - + GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: @@ -165,6 +159,8 @@ on_gnunet_chat_discourse_write_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_write, TEST_WRITE_ID) + void call_gnunet_chat_discourse_write(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -172,12 +168,9 @@ call_gnunet_chat_discourse_write(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_write_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_WRITE_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_discourse_write, call_gnunet_chat_discourse_write) +CREATE_GNUNET_TEST(test_gnunet_chat_discourse_write, gnunet_chat_discourse_write) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_write, "Talk") diff --git a/tests/file/test_gnunet_chat_file_send.c b/tests/file/test_gnunet_chat_file_send.c @@ -89,7 +89,6 @@ on_gnunet_chat_file_send_msg(void *cls, struct GNUNET_CHAT_Group *group; struct GNUNET_CHAT_File *file; - const char *name; const char *text; switch (GNUNET_CHAT_message_get_kind(message)) @@ -99,6 +98,13 @@ on_gnunet_chat_file_send_msg(void *cls, break; case GNUNET_CHAT_KIND_REFRESH: ck_assert_ptr_null(context); + ck_assert_ptr_null(account); + + account = GNUNET_CHAT_find_account(handle, TEST_SEND_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -131,34 +137,6 @@ on_gnunet_chat_file_send_msg(void *cls, 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; @@ -180,9 +158,8 @@ on_gnunet_chat_file_send_msg(void *cls, 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); + + GNUNET_CHAT_stop(handle); break; case GNUNET_CHAT_KIND_FILE: ck_assert_ptr_nonnull(context); @@ -204,6 +181,8 @@ on_gnunet_chat_file_send_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_file_send, TEST_SEND_ID) + void call_gnunet_chat_file_send(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -211,13 +190,9 @@ call_gnunet_chat_file_send(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_file_send_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, - TEST_SEND_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_file_send, call_gnunet_chat_file_send) +CREATE_GNUNET_TEST(test_gnunet_chat_file_send, gnunet_chat_file_send) START_SUITE(handle_suite, "File") ADD_TEST_TO_SUITE(test_gnunet_chat_file_send, "Send") diff --git a/tests/handle/test_gnunet_chat_handle_accounts.c b/tests/handle/test_gnunet_chat_handle_accounts.c @@ -130,6 +130,11 @@ on_gnunet_chat_handle_accounts_msg(void *cls, } void +setup_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +void call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) { static struct GNUNET_CHAT_Handle *handle = NULL; @@ -138,7 +143,12 @@ call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) ck_assert_ptr_nonnull(handle); } -CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, call_gnunet_chat_handle_accounts) +void +cleanup_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, gnunet_chat_handle_accounts) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_handle_accounts, "Accounts") diff --git a/tests/handle/test_gnunet_chat_handle_connection.c b/tests/handle/test_gnunet_chat_handle_connection.c @@ -52,6 +52,14 @@ on_gnunet_chat_handle_connection_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_CONNECTION_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_nonnull(connected); @@ -75,30 +83,6 @@ on_gnunet_chat_handle_connection_msg(void *cls, 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_nonnull(name); - ck_assert_str_eq(name, 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); - - ck_assert_ptr_nonnull(name); - ck_assert_str_eq(name, TEST_CONNECTION_ID); - GNUNET_CHAT_stop(handle); break; default: @@ -109,6 +93,8 @@ on_gnunet_chat_handle_connection_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_handle_connection, TEST_CONNECTION_ID) + void call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -116,12 +102,9 @@ call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_CONNECTION_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, call_gnunet_chat_handle_connection) +CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, gnunet_chat_handle_connection) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_handle_connection, "Connect/Disconnect") diff --git a/tests/handle/test_gnunet_chat_handle_init.c b/tests/handle/test_gnunet_chat_handle_init.c @@ -25,6 +25,11 @@ #include "test_gnunet_chat.h" void +setup_gnunet_chat_handle_init(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +void call_gnunet_chat_handle_init(const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_CHAT_Handle *handle; @@ -36,7 +41,12 @@ call_gnunet_chat_handle_init(const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_CHAT_stop(handle); } -CREATE_GNUNET_TEST(test_gnunet_chat_handle_init, call_gnunet_chat_handle_init) +void +cleanup_gnunet_chat_handle_init(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +CREATE_GNUNET_TEST(test_gnunet_chat_handle_init, gnunet_chat_handle_init) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_handle_init, "Start/Stop") diff --git a/tests/handle/test_gnunet_chat_handle_rename.c b/tests/handle/test_gnunet_chat_handle_rename.c @@ -109,6 +109,11 @@ on_gnunet_chat_handle_rename_msg(void *cls, } void +setup_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +void call_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) { static struct GNUNET_CHAT_Handle *handle = NULL; @@ -120,7 +125,12 @@ call_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_handle_rename, call_gnunet_chat_handle_rename) +void +cleanup_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) +{ +} + +CREATE_GNUNET_TEST(test_gnunet_chat_handle_rename, gnunet_chat_handle_rename) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_handle_rename, "Rename") diff --git a/tests/handle/test_gnunet_chat_handle_update.c b/tests/handle/test_gnunet_chat_handle_update.c @@ -53,6 +53,19 @@ on_gnunet_chat_handle_update_msg(void *cls, 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); + + if (update_stage == 0) + { + account = GNUNET_CHAT_find_account(handle, TEST_UPDATE_ID); + + ck_assert_ptr_nonnull(account); + + update_stage = 1; + GNUNET_CHAT_connect(handle, account); + } + break; case GNUNET_CHAT_KIND_LOGIN: account = GNUNET_CHAT_get_connected(handle); @@ -68,6 +81,7 @@ on_gnunet_chat_handle_update_msg(void *cls, dup = (char*) GNUNET_CHAT_get_user_pointer(handle); + ck_assert_ptr_null(dup); ck_assert_int_eq(update_stage, 1); dup = GNUNET_strdup(key); @@ -94,37 +108,11 @@ on_gnunet_chat_handle_update_msg(void *cls, if (update_stage == 3) { - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, TEST_UPDATE_ID - ), GNUNET_OK); - update_stage = 4; + GNUNET_CHAT_stop(handle); } break; - case GNUNET_CHAT_KIND_CREATED_ACCOUNT: - ck_assert_ptr_nonnull(account); - - if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), - TEST_UPDATE_ID))) - break; - - ck_assert_int_eq(update_stage, 0); - - update_stage = 1; - GNUNET_CHAT_connect(handle, account); - break; - case GNUNET_CHAT_KIND_DELETED_ACCOUNT: - ck_assert_ptr_nonnull(account); - - if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), - TEST_UPDATE_ID))) - break; - - ck_assert_int_eq(update_stage, 4); - - GNUNET_CHAT_stop(handle); - break; case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: ck_assert_ptr_nonnull(account); @@ -139,6 +127,10 @@ on_gnunet_chat_handle_update_msg(void *cls, ck_assert_int_eq(update_stage, 2); ck_assert_ptr_nonnull(dup); + + if (0 == strcmp(key, dup)) + break; + ck_assert_str_ne(key, dup); GNUNET_free(dup); @@ -155,6 +147,8 @@ on_gnunet_chat_handle_update_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_handle_update, TEST_UPDATE_ID) + void call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -162,12 +156,9 @@ call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_UPDATE_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, call_gnunet_chat_handle_update) +CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, gnunet_chat_handle_update) START_SUITE(handle_suite, "Handle") ADD_TEST_TO_SUITE(test_gnunet_chat_handle_update, "Update") diff --git a/tests/lobby/test_gnunet_chat_lobby_join.c b/tests/lobby/test_gnunet_chat_lobby_join.c @@ -64,6 +64,12 @@ on_gnunet_chat_lobby_join_msg(void *cls, case GNUNET_CHAT_KIND_REFRESH: ck_assert_ptr_null(context); ck_assert_ptr_null(account); + + account = GNUNET_CHAT_find_account(handle, TEST_JOIN_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -84,22 +90,6 @@ on_gnunet_chat_lobby_join_msg(void *cls, 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: @@ -127,6 +117,8 @@ on_gnunet_chat_lobby_join_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_lobby_join, TEST_JOIN_ID) + void call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -134,12 +126,9 @@ call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_join_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_JOIN_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_lobby_join, call_gnunet_chat_lobby_join) +CREATE_GNUNET_TEST(test_gnunet_chat_lobby_join, gnunet_chat_lobby_join) START_SUITE(lobby_suite, "Lobby") ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_join, "Join") diff --git a/tests/lobby/test_gnunet_chat_lobby_open.c b/tests/lobby/test_gnunet_chat_lobby_open.c @@ -50,7 +50,14 @@ on_gnunet_chat_lobby_open_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_OPEN_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_nonnull(account); @@ -70,18 +77,6 @@ on_gnunet_chat_lobby_open_msg(void *cls, case GNUNET_CHAT_KIND_LOGOUT: ck_assert_ptr_nonnull(account); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - handle, TEST_OPEN_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: @@ -95,6 +90,8 @@ on_gnunet_chat_lobby_open_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_lobby_open, TEST_OPEN_ID) + void call_gnunet_chat_lobby_open(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -102,12 +99,9 @@ call_gnunet_chat_lobby_open(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_open_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_OPEN_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_lobby_open, call_gnunet_chat_lobby_open) +CREATE_GNUNET_TEST(test_gnunet_chat_lobby_open, gnunet_chat_lobby_open) START_SUITE(lobby_suite, "Lobby") ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_open, "Open/Close") diff --git a/tests/message/test_gnunet_chat_message_text.c b/tests/message/test_gnunet_chat_message_text.c @@ -52,6 +52,14 @@ on_gnunet_chat_message_text_msg(void *cls, 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); + + account = GNUNET_CHAT_find_account(handle, TEST_TEXT_ID); + + ck_assert_ptr_nonnull(account); + + GNUNET_CHAT_connect(handle, account); break; case GNUNET_CHAT_KIND_LOGIN: ck_assert_ptr_null(context); @@ -65,18 +73,6 @@ on_gnunet_chat_message_text_msg(void *cls, ck_assert_ptr_null(context); ck_assert_ptr_nonnull(account); - ck_assert_int_eq(GNUNET_CHAT_account_delete( - 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: @@ -120,6 +116,8 @@ on_gnunet_chat_message_text_msg(void *cls, return GNUNET_YES; } +REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_message_text, TEST_TEXT_ID) + void call_gnunet_chat_message_text(const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -127,12 +125,9 @@ call_gnunet_chat_message_text(const struct GNUNET_CONFIGURATION_Handle *cfg) handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_message_text_msg, &handle); ck_assert_ptr_nonnull(handle); - ck_assert_int_eq(GNUNET_CHAT_account_create( - handle, TEST_TEXT_ID - ), GNUNET_OK); } -CREATE_GNUNET_TEST(test_gnunet_chat_message_text, call_gnunet_chat_message_text) +CREATE_GNUNET_TEST(test_gnunet_chat_message_text, gnunet_chat_message_text) START_SUITE(handle_suite, "Message") ADD_TEST_TO_SUITE(test_gnunet_chat_message_text, "Text") diff --git a/tests/test_gnunet_chat.h b/tests/test_gnunet_chat.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021--2022 GNUnet e.V. + Copyright (C) 2021--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 @@ -32,39 +32,192 @@ #include <gnunet/gnunet_chat_lib.h> -#define CREATE_GNUNET_TEST(test_name, test_call) \ +#define SETUP_GNUNET_CHAT_ACCOUNTS(test_call, test_accounts) \ +enum GNUNET_GenericReturnValue \ +on_setup_##test_call (void *cls, \ + struct GNUNET_CHAT_Context *context, \ + const struct GNUNET_CHAT_Message *message) \ +{ \ + static enum GNUNET_GenericReturnValue done = GNUNET_NO; \ + static size_t counter = 0; \ + \ + struct GNUNET_CHAT_Handle *handle = *( \ + (struct GNUNET_CHAT_Handle**) cls \ + ); \ + \ + enum GNUNET_CHAT_MessageKind kind; \ + kind = GNUNET_CHAT_message_get_kind(message); \ + \ + if (GNUNET_CHAT_KIND_CREATED_ACCOUNT == kind) \ + { \ + const struct GNUNET_CHAT_Account *account; \ + account = GNUNET_CHAT_message_get_account(message); \ + ck_assert_ptr_nonnull(account); \ + \ + const char **accounts; \ + for (accounts = test_accounts; *accounts; accounts++) \ + if (0 == strcmp(GNUNET_CHAT_account_get_name(account), \ + *accounts)) \ + break; \ + \ + if (*accounts) \ + counter = (counter? counter - 1 : counter); \ + } \ + \ + \ + if ((GNUNET_YES == done) && (0 == counter)) \ + GNUNET_CHAT_stop(handle); \ + \ + if ((GNUNET_CHAT_KIND_REFRESH != kind) || (GNUNET_YES == done)) \ + return GNUNET_YES; \ + \ + for (; test_accounts[counter]; counter++) \ + ck_assert(GNUNET_OK == GNUNET_CHAT_account_create( \ + handle, test_accounts[counter] \ + )); \ + \ + done = GNUNET_YES; \ + return GNUNET_YES; \ +} \ + \ +void \ +setup_##test_call (const struct GNUNET_CONFIGURATION_Handle *cfg) \ +{ \ + static struct GNUNET_CHAT_Handle *handle = NULL; \ + handle = GNUNET_CHAT_start( \ + cfg, \ + on_setup_##test_call, \ + &handle \ + ); \ + \ + ck_assert_ptr_nonnull(handle); \ +} + +#define CLEANUP_GNUNET_CHAT_ACCOUNTS(test_call, test_accounts) \ +enum GNUNET_GenericReturnValue \ +on_cleanup_##test_call (void *cls, \ + struct GNUNET_CHAT_Context *context, \ + const struct GNUNET_CHAT_Message *message) \ +{ \ + static enum GNUNET_GenericReturnValue done = GNUNET_NO; \ + static size_t counter = 0; \ + \ + struct GNUNET_CHAT_Handle *handle = *( \ + (struct GNUNET_CHAT_Handle**) cls \ + ); \ + \ + enum GNUNET_CHAT_MessageKind kind; \ + kind = GNUNET_CHAT_message_get_kind(message); \ + \ + if (GNUNET_CHAT_KIND_DELETED_ACCOUNT == kind) \ + { \ + const struct GNUNET_CHAT_Account *account; \ + account = GNUNET_CHAT_message_get_account(message); \ + ck_assert_ptr_nonnull(account); \ + \ + const char **accounts; \ + for (accounts = test_accounts; *accounts; accounts++) \ + if (0 == strcmp(GNUNET_CHAT_account_get_name(account), \ + *accounts)) \ + break; \ + \ + if (*accounts) \ + counter = (counter? counter - 1 : counter); \ + } \ + \ + if ((GNUNET_YES == done) && (0 == counter)) \ + GNUNET_CHAT_stop(handle); \ + \ + if ((GNUNET_CHAT_KIND_REFRESH != kind) || (GNUNET_YES == done)) \ + return GNUNET_YES; \ + \ + for (; test_accounts[counter]; counter++) \ + ck_assert(GNUNET_OK == GNUNET_CHAT_account_delete( \ + handle, test_accounts[counter] \ + )); \ + \ + done = GNUNET_YES; \ + return GNUNET_YES; \ +} \ + \ +void \ +cleanup_##test_call (const struct GNUNET_CONFIGURATION_Handle *cfg) \ +{ \ + static struct GNUNET_CHAT_Handle *handle = NULL; \ + handle = GNUNET_CHAT_start( \ + cfg, \ + on_cleanup_##test_call, \ + &handle \ + ); \ + \ + ck_assert_ptr_nonnull(handle); \ +} + +#define REQUIRE_GNUNET_CHAT_ACCOUNT(test_call, test_account) \ +const char *accounts_##test_call [] = { \ + test_account, NULL \ +}; \ + \ +SETUP_GNUNET_CHAT_ACCOUNTS(test_call, accounts_##test_call) \ +CLEANUP_GNUNET_CHAT_ACCOUNTS(test_call, accounts_##test_call) + +#define __CREATE_GNUNET_TEST_TASK(test_call) \ void \ -task_##test_call (__attribute__ ((unused)) void *cls, \ +task_##test_call (void *cls, \ __attribute__ ((unused)) char *const *args, \ - __attribute__ ((unused)) const char *cfgfile, \ - const struct GNUNET_CONFIGURATION_Handle *cfg) \ -{ \ - test_call (cfg); \ -} \ - \ -START_TEST(test_name) \ + __attribute__ ((unused)) const char *cfgfile, \ + const struct GNUNET_CONFIGURATION_Handle *cfg) \ { \ - enum GNUNET_GenericReturnValue result; \ - struct GNUNET_GETOPT_CommandLineOption options[] = { \ - GNUNET_GETOPT_OPTION_END \ - }; \ - \ - char *binary = #test_call; \ - char *const args [] = { binary }; \ - \ - result = GNUNET_PROGRAM_run( \ - 1, \ - args, \ - binary, \ - "", \ - options, \ - task_##test_call, \ - NULL \ + ck_assert_ptr_nonnull(cls); \ + ck_assert_ptr_nonnull(cfg); \ + GNUNET_log( \ + GNUNET_ERROR_TYPE_INFO, \ + "Stage: %s\n", \ + (const char*) cls \ ); \ - \ - ck_assert(result == GNUNET_OK); \ -} \ -END_TEST + test_call (cfg); \ +} + +#define __CALL_GNUNET_TEST_TASK(test_call) \ +{ \ + enum GNUNET_GenericReturnValue result; \ + struct GNUNET_GETOPT_CommandLineOption options[] = { \ + GNUNET_GETOPT_OPTION_END \ + }; \ + \ + char *binary = #test_call; \ + char *const args [] = { binary }; \ + \ + result = GNUNET_PROGRAM_run( \ + 1, \ + args, \ + binary, \ + "", \ + options, \ + task_##test_call, \ + binary \ + ); \ + \ + ck_assert(result == GNUNET_OK); \ +} + +#define CREATE_GNUNET_TEST(test_name, test_call) \ +__CREATE_GNUNET_TEST_TASK(call_##test_call) \ + \ +START_TEST(test_name) \ +__CALL_GNUNET_TEST_TASK(call_##test_call) \ +END_TEST \ + \ +__CREATE_GNUNET_TEST_TASK(setup_##test_call) \ +__CREATE_GNUNET_TEST_TASK(cleanup_##test_call) \ + \ +void \ +setup_##test_name () \ +__CALL_GNUNET_TEST_TASK(setup_##test_call) \ + \ +void \ +cleanup_##test_name () \ +__CALL_GNUNET_TEST_TASK(cleanup_##test_call) #define START_SUITE(suite_name, suite_title) \ Suite* suite_name (void) \ @@ -77,6 +230,13 @@ Suite* suite_name (void) \ #define ADD_TEST_TO_SUITE(test_name, test_title) \ tcase = tcase_create(test_title); \ tcase_add_test(tcase, test_name); \ + \ + tcase_add_checked_fixture( \ + tcase, \ + setup_##test_name, \ + cleanup_##test_name \ + ); \ + \ suite_add_tcase(suite, tcase); #define END_SUITE \