test_gnunet_chat_discourse_open.c (5312B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2024--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_discourse_open.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_OPEN_ID "gnunet_chat_discourse_open" 28 #define TEST_OPEN_GROUP "gnunet_chat_discourse_open_group" 29 #define TEST_OPEN_DISCOURSE "gnunet_chat_discourse_open_discourse" 30 #define TEST_OPEN_SECRET "test_secret_discourse_open" 31 32 enum GNUNET_GenericReturnValue 33 on_gnunet_chat_discourse_open_msg(void *cls, 34 struct GNUNET_CHAT_Context *context, 35 struct GNUNET_CHAT_Message *message) 36 { 37 static unsigned int discourse_stage = 0; 38 39 struct GNUNET_CHAT_Handle *handle = *( 40 (struct GNUNET_CHAT_Handle**) cls 41 ); 42 43 ck_assert_ptr_nonnull(handle); 44 ck_assert_ptr_nonnull(message); 45 46 struct GNUNET_CHAT_Account *account; 47 account = GNUNET_CHAT_message_get_account(message); 48 49 const char *name = GNUNET_CHAT_get_name(handle); 50 struct GNUNET_CHAT_DiscourseId discourse_id; 51 52 struct GNUNET_CHAT_Group *group; 53 struct GNUNET_CHAT_Discourse *discourse; 54 discourse = GNUNET_CHAT_message_get_discourse(message); 55 56 switch (GNUNET_CHAT_message_get_kind(message)) 57 { 58 case GNUNET_CHAT_KIND_WARNING: 59 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 60 break; 61 case GNUNET_CHAT_KIND_REFRESH: 62 ck_assert_ptr_null(context); 63 ck_assert_ptr_null(account); 64 65 if (discourse_stage == 0) 66 { 67 account = GNUNET_CHAT_find_account(handle, TEST_OPEN_ID); 68 69 ck_assert_ptr_nonnull(account); 70 71 GNUNET_CHAT_connect( 72 handle, 73 account, 74 TEST_OPEN_SECRET, 75 strlen(TEST_OPEN_SECRET) 76 ); 77 78 discourse_stage = 1; 79 } 80 81 break; 82 case GNUNET_CHAT_KIND_LOGIN: 83 ck_assert_ptr_null(context); 84 ck_assert_ptr_nonnull(account); 85 ck_assert_ptr_nonnull(name); 86 ck_assert_str_eq(name, TEST_OPEN_ID); 87 ck_assert_uint_eq(discourse_stage, 1); 88 89 group = GNUNET_CHAT_group_create(handle, TEST_OPEN_GROUP); 90 91 ck_assert_ptr_nonnull(group); 92 93 discourse_stage = 2; 94 break; 95 case GNUNET_CHAT_KIND_LOGOUT: 96 ck_assert_ptr_null(context); 97 ck_assert_ptr_nonnull(account); 98 ck_assert_uint_eq(discourse_stage, 6); 99 100 GNUNET_CHAT_stop(handle); 101 break; 102 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 103 break; 104 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 105 break; 106 case GNUNET_CHAT_KIND_JOIN: 107 ck_assert_ptr_nonnull(context); 108 ck_assert_ptr_null(discourse); 109 ck_assert_uint_eq(discourse_stage, 2); 110 111 GNUNET_memcpy( 112 &discourse_id, 113 TEST_OPEN_DISCOURSE, 114 sizeof(discourse_id) 115 ); 116 117 discourse = GNUNET_CHAT_context_open_discourse( 118 context, 119 &discourse_id 120 ); 121 122 ck_assert_ptr_nonnull(discourse); 123 ck_assert_int_eq(GNUNET_CHAT_discourse_is_open(discourse), GNUNET_NO); 124 125 discourse_stage = 3; 126 break; 127 case GNUNET_CHAT_KIND_LEAVE: 128 ck_assert_ptr_nonnull(context); 129 ck_assert_ptr_nonnull(account); 130 ck_assert_uint_eq(discourse_stage, 5); 131 132 GNUNET_CHAT_disconnect(handle); 133 134 discourse_stage = 6; 135 break; 136 case GNUNET_CHAT_KIND_CONTACT: 137 break; 138 case GNUNET_CHAT_KIND_DISCOURSE: 139 ck_assert_ptr_nonnull(context); 140 ck_assert_ptr_nonnull(discourse); 141 142 if (GNUNET_YES == GNUNET_CHAT_discourse_is_open(discourse)) 143 { 144 ck_assert_uint_eq(discourse_stage, 3); 145 146 GNUNET_CHAT_discourse_close(discourse); 147 discourse_stage = 4; 148 } 149 else 150 { 151 ck_assert_uint_eq(discourse_stage, 4); 152 153 group = GNUNET_CHAT_context_get_group(context); 154 155 ck_assert_ptr_nonnull(group); 156 157 ck_assert_int_eq( 158 GNUNET_CHAT_group_leave(group), 159 GNUNET_OK 160 ); 161 162 discourse_stage = 5; 163 } 164 165 break; 166 default: 167 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 168 ck_abort(); 169 break; 170 } 171 172 return GNUNET_YES; 173 } 174 175 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_open, TEST_OPEN_ID) 176 177 void 178 call_gnunet_chat_discourse_open(const struct GNUNET_CONFIGURATION_Handle *cfg) 179 { 180 static struct GNUNET_CHAT_Handle *handle = NULL; 181 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_open_msg, &handle); 182 183 ck_assert_ptr_nonnull(handle); 184 } 185 186 CREATE_GNUNET_TEST(test_gnunet_chat_discourse_open, gnunet_chat_discourse_open) 187 188 START_SUITE(handle_suite, "Handle") 189 ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_open, "Open/Close") 190 END_SUITE 191 192 MAIN_SUITE(handle_suite, CK_NORMAL)