test_gnunet_chat_handle_accounts.c (4111B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file test_gnunet_chat_handle_accounts.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_ACCOUNTS_ID "gnunet_chat_handle_accounts" 28 29 enum GNUNET_GenericReturnValue 30 on_gnunet_chat_handle_accounts_it(void *cls, 31 struct GNUNET_CHAT_Handle *handle, 32 struct GNUNET_CHAT_Account *account) 33 { 34 unsigned int *accounts_stage = cls; 35 36 ck_assert_ptr_nonnull(handle); 37 ck_assert_ptr_nonnull(account); 38 ck_assert_int_eq(*accounts_stage, 2); 39 40 const char *name = GNUNET_CHAT_account_get_name(account); 41 42 ck_assert_ptr_nonnull(name); 43 44 if (0 == strcmp(name, TEST_ACCOUNTS_ID)) 45 { 46 *accounts_stage = 3; 47 return GNUNET_NO; 48 } 49 50 return GNUNET_YES; 51 } 52 53 enum GNUNET_GenericReturnValue 54 on_gnunet_chat_handle_accounts_msg(void *cls, 55 struct GNUNET_CHAT_Context *context, 56 struct GNUNET_CHAT_Message *message) 57 { 58 static unsigned int accounts_stage = 0; 59 60 struct GNUNET_CHAT_Handle *handle = *( 61 (struct GNUNET_CHAT_Handle**) cls 62 ); 63 64 ck_assert_ptr_nonnull(handle); 65 ck_assert_ptr_null(context); 66 67 const struct GNUNET_CHAT_Account *account; 68 account = GNUNET_CHAT_message_get_account(message); 69 70 const char *name; 71 name = GNUNET_CHAT_account_get_name(account); 72 73 switch (GNUNET_CHAT_message_get_kind(message)) 74 { 75 case GNUNET_CHAT_KIND_WARNING: 76 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 77 break; 78 case GNUNET_CHAT_KIND_REFRESH: 79 if (0 == accounts_stage) 80 { 81 ck_assert_int_eq(GNUNET_CHAT_account_create( 82 handle, 83 TEST_ACCOUNTS_ID 84 ), GNUNET_OK); 85 86 accounts_stage = 1; 87 } else 88 if (2 == accounts_stage) 89 { 90 ck_assert_int_ge(GNUNET_CHAT_iterate_accounts( 91 handle, 92 on_gnunet_chat_handle_accounts_it, 93 &accounts_stage 94 ), 1); 95 } 96 97 if (3 == accounts_stage) 98 { 99 ck_assert_int_eq(GNUNET_CHAT_account_delete( 100 handle, 101 TEST_ACCOUNTS_ID 102 ), GNUNET_OK); 103 104 accounts_stage = 4; 105 } 106 107 break; 108 case GNUNET_CHAT_KIND_CREATED_ACCOUNT: 109 ck_assert_ptr_nonnull(account); 110 ck_assert_ptr_nonnull(name); 111 112 if (0 == strcmp(name, TEST_ACCOUNTS_ID)) 113 accounts_stage = 2; 114 115 break; 116 case GNUNET_CHAT_KIND_DELETED_ACCOUNT: 117 ck_assert_int_eq(accounts_stage, 4); 118 ck_assert_ptr_nonnull(name); 119 120 if (0 == strcmp(name, TEST_ACCOUNTS_ID)) 121 GNUNET_CHAT_stop(handle); 122 123 break; 124 default: 125 ck_abort(); 126 break; 127 } 128 129 return GNUNET_YES; 130 } 131 132 void 133 setup_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) 134 { 135 } 136 137 void 138 call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) 139 { 140 static struct GNUNET_CHAT_Handle *handle = NULL; 141 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_accounts_msg, &handle); 142 143 ck_assert_ptr_nonnull(handle); 144 } 145 146 void 147 cleanup_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) 148 { 149 } 150 151 CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, gnunet_chat_handle_accounts) 152 153 START_SUITE(handle_suite, "Handle") 154 ADD_TEST_TO_SUITE(test_gnunet_chat_handle_accounts, "Accounts") 155 END_SUITE 156 157 MAIN_SUITE(handle_suite, CK_NORMAL)