test_gnunet_chat_discourse_open.c (4853B)
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_Discourse *discourse; 53 discourse = GNUNET_CHAT_message_get_discourse(message); 54 55 switch (GNUNET_CHAT_message_get_kind(message)) 56 { 57 case GNUNET_CHAT_KIND_WARNING: 58 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 59 break; 60 case GNUNET_CHAT_KIND_REFRESH: 61 ck_assert_ptr_null(context); 62 ck_assert_ptr_null(account); 63 64 if (discourse_stage == 0) 65 { 66 account = GNUNET_CHAT_find_account(handle, TEST_OPEN_ID); 67 68 ck_assert_ptr_nonnull(account); 69 70 GNUNET_CHAT_connect( 71 handle, 72 account, 73 TEST_OPEN_SECRET, 74 strlen(TEST_OPEN_SECRET) 75 ); 76 77 discourse_stage = 1; 78 } 79 80 break; 81 case GNUNET_CHAT_KIND_LOGIN: 82 ck_assert_ptr_null(context); 83 ck_assert_ptr_nonnull(account); 84 ck_assert_ptr_nonnull(name); 85 ck_assert_str_eq(name, TEST_OPEN_ID); 86 ck_assert_uint_eq(discourse_stage, 1); 87 88 GNUNET_CHAT_group_create(handle, TEST_OPEN_GROUP); 89 discourse_stage = 2; 90 break; 91 case GNUNET_CHAT_KIND_LOGOUT: 92 ck_assert_ptr_null(context); 93 ck_assert_ptr_nonnull(account); 94 ck_assert_uint_eq(discourse_stage, 5); 95 96 GNUNET_CHAT_stop(handle); 97 break; 98 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 99 break; 100 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 101 break; 102 case GNUNET_CHAT_KIND_JOIN: 103 ck_assert_ptr_nonnull(context); 104 ck_assert_ptr_null(discourse); 105 ck_assert_uint_eq(discourse_stage, 2); 106 107 GNUNET_memcpy( 108 &discourse_id, 109 TEST_OPEN_DISCOURSE, 110 sizeof(discourse_id) 111 ); 112 113 discourse = GNUNET_CHAT_context_open_discourse( 114 context, 115 &discourse_id 116 ); 117 118 ck_assert_ptr_nonnull(discourse); 119 ck_assert_int_eq(GNUNET_CHAT_discourse_is_open(discourse), GNUNET_NO); 120 121 discourse_stage = 3; 122 break; 123 case GNUNET_CHAT_KIND_CONTACT: 124 break; 125 case GNUNET_CHAT_KIND_DISCOURSE: 126 ck_assert_ptr_nonnull(context); 127 ck_assert_ptr_nonnull(discourse); 128 129 if (GNUNET_YES == GNUNET_CHAT_discourse_is_open(discourse)) 130 { 131 ck_assert_uint_eq(discourse_stage, 3); 132 133 GNUNET_CHAT_discourse_close(discourse); 134 discourse_stage = 4; 135 } 136 else 137 { 138 ck_assert_uint_eq(discourse_stage, 4); 139 140 GNUNET_CHAT_disconnect(handle); 141 discourse_stage = 5; 142 } 143 144 break; 145 default: 146 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 147 ck_abort(); 148 break; 149 } 150 151 return GNUNET_YES; 152 } 153 154 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_open, TEST_OPEN_ID) 155 156 void 157 call_gnunet_chat_discourse_open(const struct GNUNET_CONFIGURATION_Handle *cfg) 158 { 159 static struct GNUNET_CHAT_Handle *handle = NULL; 160 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_open_msg, &handle); 161 162 ck_assert_ptr_nonnull(handle); 163 } 164 165 CREATE_GNUNET_TEST(test_gnunet_chat_discourse_open, gnunet_chat_discourse_open) 166 167 START_SUITE(handle_suite, "Handle") 168 ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_open, "Open/Close") 169 END_SUITE 170 171 MAIN_SUITE(handle_suite, CK_NORMAL)