test_gnunet_chat_handle_update.c (4345B)
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_update.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_UPDATE_ID "gnunet_chat_handle_update" 28 29 enum GNUNET_GenericReturnValue 30 on_gnunet_chat_handle_update_msg(void *cls, 31 struct GNUNET_CHAT_Context *context, 32 struct GNUNET_CHAT_Message *message) 33 { 34 static unsigned int update_stage = 0; 35 36 struct GNUNET_CHAT_Handle *handle = *( 37 (struct GNUNET_CHAT_Handle**) cls 38 ); 39 40 ck_assert_ptr_nonnull(handle); 41 ck_assert_ptr_null(context); 42 ck_assert_ptr_nonnull(message); 43 44 struct GNUNET_CHAT_Account *account; 45 account = GNUNET_CHAT_message_get_account(message); 46 47 const char *key; 48 char *dup; 49 50 switch (GNUNET_CHAT_message_get_kind(message)) 51 { 52 case GNUNET_CHAT_KIND_WARNING: 53 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 54 break; 55 case GNUNET_CHAT_KIND_REFRESH: 56 ck_assert_ptr_null(context); 57 ck_assert_ptr_null(account); 58 59 if (update_stage == 0) 60 { 61 account = GNUNET_CHAT_find_account(handle, TEST_UPDATE_ID); 62 63 ck_assert_ptr_nonnull(account); 64 65 update_stage = 1; 66 GNUNET_CHAT_connect(handle, account); 67 } 68 69 break; 70 case GNUNET_CHAT_KIND_LOGIN: 71 account = GNUNET_CHAT_get_connected(handle); 72 73 ck_assert_ptr_nonnull(account); 74 ck_assert_str_eq( 75 GNUNET_CHAT_account_get_name(account), 76 TEST_UPDATE_ID 77 ); 78 79 key = GNUNET_CHAT_get_key(handle); 80 ck_assert_ptr_nonnull(key); 81 82 dup = (char*) GNUNET_CHAT_get_user_pointer(handle); 83 84 ck_assert_ptr_null(dup); 85 ck_assert_int_eq(update_stage, 1); 86 87 dup = GNUNET_strdup(key); 88 89 ck_assert_ptr_nonnull(dup); 90 ck_assert_str_eq(key, dup); 91 92 GNUNET_CHAT_set_user_pointer(handle, (void*) dup); 93 GNUNET_CHAT_update(handle); 94 95 update_stage = 2; 96 break; 97 case GNUNET_CHAT_KIND_LOGOUT: 98 account = GNUNET_CHAT_get_connected(handle); 99 100 ck_assert_int_ge(update_stage, 2); 101 ck_assert_int_le(update_stage, 3); 102 103 ck_assert_ptr_nonnull(account); 104 ck_assert_str_eq( 105 GNUNET_CHAT_account_get_name(account), 106 TEST_UPDATE_ID 107 ); 108 109 if (update_stage == 3) 110 { 111 update_stage = 4; 112 GNUNET_CHAT_stop(handle); 113 } 114 115 break; 116 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 117 ck_assert_ptr_nonnull(account); 118 119 if ((0 != strcmp(GNUNET_CHAT_account_get_name(account), 120 TEST_UPDATE_ID))) 121 break; 122 123 key = GNUNET_CHAT_get_key(handle); 124 ck_assert_ptr_nonnull(key); 125 126 dup = (char*) GNUNET_CHAT_get_user_pointer(handle); 127 128 ck_assert_int_eq(update_stage, 2); 129 ck_assert_ptr_nonnull(dup); 130 131 if (0 == strcmp(key, dup)) 132 break; 133 134 ck_assert_str_ne(key, dup); 135 136 GNUNET_free(dup); 137 138 GNUNET_CHAT_disconnect(handle); 139 140 update_stage = 3; 141 break; 142 default: 143 ck_abort(); 144 break; 145 } 146 147 return GNUNET_YES; 148 } 149 150 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_handle_update, TEST_UPDATE_ID) 151 152 void 153 call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) 154 { 155 static struct GNUNET_CHAT_Handle *handle = NULL; 156 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, &handle); 157 158 ck_assert_ptr_nonnull(handle); 159 } 160 161 CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, gnunet_chat_handle_update) 162 163 START_SUITE(handle_suite, "Handle") 164 ADD_TEST_TO_SUITE(test_gnunet_chat_handle_update, "Update") 165 END_SUITE 166 167 MAIN_SUITE(handle_suite, CK_NORMAL)