test_gnunet_chat_discourse_write.c (5793B)
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_write.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_WRITE_ID "gnunet_chat_discourse_write" 28 #define TEST_WRITE_GROUP "gnunet_chat_discourse_write_group" 29 #define TEST_WRITE_DISCOURSE "gnunet_chat_discourse_write_discourse" 30 #define TEST_WRITE_SECRET "test_secret_discourse_write" 31 32 enum GNUNET_GenericReturnValue 33 on_gnunet_chat_discourse_write_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_WRITE_ID); 67 68 ck_assert_ptr_nonnull(account); 69 70 GNUNET_CHAT_connect( 71 handle, 72 account, 73 TEST_WRITE_SECRET, 74 strlen(TEST_WRITE_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_WRITE_ID); 86 ck_assert_uint_eq(discourse_stage, 1); 87 88 GNUNET_CHAT_group_create(handle, TEST_WRITE_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, 6); 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_WRITE_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 GNUNET_memcpy( 130 &discourse_id, 131 TEST_WRITE_DISCOURSE, 132 sizeof(discourse_id) 133 ); 134 135 if (GNUNET_YES == GNUNET_CHAT_discourse_is_open(discourse)) 136 { 137 ck_assert_uint_eq(discourse_stage, 3); 138 139 ck_assert_int_eq( 140 GNUNET_CHAT_discourse_write( 141 discourse, 142 (const char*) &discourse_id, 143 sizeof(discourse_id) 144 ), 145 GNUNET_OK 146 ); 147 148 discourse_stage = 4; 149 } 150 else 151 { 152 ck_assert_uint_eq(discourse_stage, 5); 153 154 GNUNET_CHAT_disconnect(handle); 155 156 discourse_stage = 6; 157 } 158 159 break; 160 case GNUNET_CHAT_KIND_DATA: 161 ck_assert_ptr_nonnull(context); 162 ck_assert_ptr_nonnull(discourse); 163 ck_assert_uint_eq(discourse_stage, 4); 164 165 ck_assert_uint_eq( 166 GNUNET_CHAT_message_available(message), 167 sizeof(discourse_id) 168 ); 169 170 ck_assert_int_eq( 171 GNUNET_CHAT_message_read( 172 message, 173 (char*) &discourse_id, 174 sizeof(discourse_id) 175 ), 176 GNUNET_OK 177 ); 178 179 ck_assert_mem_eq( 180 &discourse_id, 181 TEST_WRITE_DISCOURSE, 182 sizeof(discourse_id) 183 ); 184 185 GNUNET_CHAT_discourse_close(discourse); 186 discourse_stage = 5; 187 break; 188 default: 189 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 190 ck_abort(); 191 break; 192 } 193 194 return GNUNET_YES; 195 } 196 197 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_write, TEST_WRITE_ID) 198 199 void 200 call_gnunet_chat_discourse_write(const struct GNUNET_CONFIGURATION_Handle *cfg) 201 { 202 static struct GNUNET_CHAT_Handle *handle = NULL; 203 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_write_msg, &handle); 204 205 ck_assert_ptr_nonnull(handle); 206 } 207 208 CREATE_GNUNET_TEST(test_gnunet_chat_discourse_write, gnunet_chat_discourse_write) 209 210 START_SUITE(handle_suite, "Handle") 211 ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_write, "Talk") 212 END_SUITE 213 214 MAIN_SUITE(handle_suite, CK_NORMAL)