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