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