test_gnunet_chat_attribute_share.c (10206B)
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_attribute_share.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_SHARE_ID_A "gnunet_chat_attribute_share_a" 28 #define TEST_SHARE_ID_B "gnunet_chat_attribute_share_b" 29 #define TEST_SHARE_GROUP "test_attribute_share_group" 30 #define TEST_SHARE_NAME "test_attribute_share_name" 31 #define TEST_SHARE_VALUE "test_attribute_share_value" 32 #define TEST_SHARE_SECRET_A "test_secret_attribute_share_a" 33 #define TEST_SHARE_SECRET_B "test_secret_attribute_share_b" 34 35 enum GNUNET_GenericReturnValue 36 on_gnunet_chat_attribute_share_attr(void *cls, 37 struct GNUNET_CHAT_Contact *contact, 38 const char *name, 39 const char *value) 40 { 41 ck_assert_ptr_nonnull(cls); 42 ck_assert_ptr_nonnull(contact); 43 ck_assert_ptr_nonnull(name); 44 ck_assert_ptr_nonnull(value); 45 46 struct GNUNET_CHAT_Handle *handle = ( 47 (struct GNUNET_CHAT_Handle*) cls 48 ); 49 50 const struct GNUNET_CHAT_Account *account; 51 account = GNUNET_CHAT_get_connected(handle); 52 53 const char *account_name = GNUNET_CHAT_account_get_name(account); 54 const char *contact_name = GNUNET_CHAT_contact_get_name(contact); 55 56 ck_assert_ptr_nonnull(account); 57 ck_assert_str_eq(account_name, TEST_SHARE_ID_B); 58 59 if (contact_name) 60 ck_assert_str_eq(contact_name, TEST_SHARE_ID_A); 61 62 ck_assert_str_eq(name, TEST_SHARE_NAME); 63 ck_assert_str_eq(value, TEST_SHARE_VALUE); 64 65 GNUNET_CHAT_unshare_attribute_from(handle, contact, name); 66 return GNUNET_NO; 67 } 68 69 enum GNUNET_GenericReturnValue 70 on_gnunet_chat_attribute_share_msg(void *cls, 71 struct GNUNET_CHAT_Context *context, 72 struct GNUNET_CHAT_Message *message) 73 { 74 static unsigned int share_stage = 0; 75 76 struct GNUNET_CHAT_Handle *handle = *( 77 (struct GNUNET_CHAT_Handle**) cls 78 ); 79 80 struct GNUNET_CHAT_Account *account; 81 struct GNUNET_CHAT_Contact *recipient; 82 struct GNUNET_CHAT_Contact *sender; 83 struct GNUNET_CHAT_Group *group; 84 const char *name; 85 86 ck_assert_ptr_nonnull(handle); 87 ck_assert_ptr_nonnull(message); 88 89 account = GNUNET_CHAT_message_get_account(message); 90 recipient = GNUNET_CHAT_message_get_recipient(message); 91 sender = GNUNET_CHAT_message_get_sender(message); 92 93 switch (GNUNET_CHAT_message_get_kind(message)) 94 { 95 case GNUNET_CHAT_KIND_WARNING: 96 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 97 break; 98 case GNUNET_CHAT_KIND_REFRESH: 99 ck_assert_ptr_null(context); 100 ck_assert_ptr_null(account); 101 102 if (share_stage == 0) 103 { 104 account = GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_A); 105 106 ck_assert_ptr_nonnull(account); 107 108 GNUNET_CHAT_connect( 109 handle, 110 account, 111 TEST_SHARE_SECRET_A, 112 strlen(TEST_SHARE_SECRET_A) 113 ); 114 115 share_stage = 1; 116 } 117 118 break; 119 case GNUNET_CHAT_KIND_LOGIN: 120 ck_assert_ptr_null(context); 121 ck_assert_ptr_nonnull(account); 122 123 name = GNUNET_CHAT_account_get_name(account); 124 125 ck_assert_ptr_nonnull(name); 126 ck_assert_uint_ge(share_stage, 1); 127 128 if (0 == strcmp(name, TEST_SHARE_ID_B)) 129 { 130 ck_assert_uint_ge(share_stage, 4); 131 132 if (share_stage > 4) 133 { 134 ck_assert_uint_eq(share_stage, 12); 135 136 share_stage = 13; 137 } 138 else 139 { 140 ck_assert_uint_eq(share_stage, 4); 141 142 GNUNET_CHAT_set_attribute( 143 handle, 144 TEST_SHARE_NAME, 145 TEST_SHARE_VALUE 146 ); 147 148 share_stage = 5; 149 } 150 } 151 else 152 { 153 if (share_stage > 1) 154 { 155 ck_assert_uint_eq(share_stage, 8); 156 157 share_stage = 9; 158 } 159 else 160 { 161 ck_assert_uint_eq(share_stage, 1); 162 163 share_stage = 2; 164 } 165 } 166 167 ck_assert_ptr_nonnull(GNUNET_CHAT_group_create( 168 handle, TEST_SHARE_GROUP 169 )); 170 break; 171 case GNUNET_CHAT_KIND_LOGOUT: 172 ck_assert_ptr_null(context); 173 ck_assert_ptr_nonnull(account); 174 ck_assert_uint_ge(share_stage, 3); 175 176 name = GNUNET_CHAT_account_get_name(account); 177 178 ck_assert_ptr_nonnull(name); 179 180 if (0 == strcmp(name, TEST_SHARE_ID_A)) 181 { 182 if (share_stage > 3) 183 { 184 ck_assert_uint_eq(share_stage, 11); 185 186 share_stage = 12; 187 } 188 else 189 { 190 ck_assert_uint_eq(share_stage, 3); 191 192 share_stage = 4; 193 } 194 } 195 else if (0 == strcmp(name, TEST_SHARE_ID_B)) 196 { 197 ck_assert_uint_ge(share_stage, 7); 198 199 if (share_stage > 7) 200 { 201 ck_assert_uint_eq(share_stage, 15); 202 203 GNUNET_CHAT_stop(handle); 204 } 205 else 206 { 207 ck_assert_uint_eq(share_stage, 7); 208 209 share_stage = 8; 210 } 211 } 212 213 break; 214 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 215 ck_assert_ptr_nonnull(account); 216 break; 217 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 218 ck_assert_ptr_nonnull(context); 219 break; 220 case GNUNET_CHAT_KIND_JOIN: 221 ck_assert_ptr_nonnull(context); 222 ck_assert_ptr_nonnull(account); 223 ck_assert_ptr_nonnull(sender); 224 225 name = GNUNET_CHAT_account_get_name(account); 226 227 ck_assert_ptr_nonnull(name); 228 229 if ((0 == strcmp(name, TEST_SHARE_ID_A)) && 230 (GNUNET_YES == GNUNET_CHAT_message_is_sent(message))) 231 { 232 ck_assert_uint_ge(share_stage, 2); 233 234 if (share_stage > 2) 235 { 236 ck_assert_uint_eq(share_stage, 9); 237 238 group = GNUNET_CHAT_context_get_group(context); 239 240 ck_assert_ptr_nonnull(group); 241 242 ck_assert_int_eq( 243 GNUNET_CHAT_group_leave(group), 244 GNUNET_OK 245 ); 246 247 share_stage = 10; 248 } 249 else 250 { 251 ck_assert_uint_eq(share_stage, 2); 252 253 GNUNET_CHAT_connect( 254 handle, 255 GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_B), 256 TEST_SHARE_SECRET_B, 257 strlen(TEST_SHARE_SECRET_B) 258 ); 259 260 share_stage = 3; 261 } 262 } 263 else if ((GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) && 264 (share_stage == 5)) 265 { 266 GNUNET_CHAT_share_attribute_with(handle, sender, TEST_SHARE_NAME); 267 268 share_stage = 6; 269 } 270 else if ((0 == strcmp(name, TEST_SHARE_ID_B)) && 271 (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)) && 272 (share_stage > 6)) 273 { 274 ck_assert_uint_eq(share_stage, 13); 275 276 group = GNUNET_CHAT_context_get_group(context); 277 278 ck_assert_ptr_nonnull(group); 279 280 ck_assert_int_eq( 281 GNUNET_CHAT_group_leave(group), 282 GNUNET_OK 283 ); 284 285 share_stage = 14; 286 } 287 288 break; 289 case GNUNET_CHAT_KIND_LEAVE: 290 ck_assert_ptr_nonnull(account); 291 292 name = GNUNET_CHAT_account_get_name(account); 293 294 ck_assert_ptr_nonnull(name); 295 296 if (0 == strcmp(name, TEST_SHARE_ID_A)) 297 { 298 ck_assert_uint_eq(share_stage, 10); 299 300 GNUNET_CHAT_connect( 301 handle, 302 GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_B), 303 TEST_SHARE_SECRET_B, 304 strlen(TEST_SHARE_SECRET_B) 305 ); 306 307 share_stage = 11; 308 } 309 else if (0 == strcmp(name, TEST_SHARE_ID_B)) 310 { 311 ck_assert_uint_eq(share_stage, 14); 312 313 GNUNET_CHAT_disconnect(handle); 314 315 share_stage = 15; 316 } 317 318 break; 319 case GNUNET_CHAT_KIND_CONTACT: 320 ck_assert_ptr_nonnull(context); 321 ck_assert_ptr_nonnull(sender); 322 break; 323 case GNUNET_CHAT_KIND_ATTRIBUTES: 324 ck_assert_ptr_null(context); 325 ck_assert_uint_ge(share_stage, 5); 326 break; 327 case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: 328 ck_assert_uint_ge(share_stage, 6); 329 330 if (!context) 331 { 332 ck_assert_ptr_null(account); 333 ck_assert_ptr_null(sender); 334 ck_assert_ptr_null(recipient); 335 336 GNUNET_CHAT_connect( 337 handle, 338 GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_A), 339 TEST_SHARE_SECRET_A, 340 strlen(TEST_SHARE_SECRET_A) 341 ); 342 343 share_stage = 7; 344 } 345 else if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)) 346 { 347 ck_assert_ptr_nonnull(account); 348 ck_assert_ptr_nonnull(sender); 349 ck_assert_ptr_nonnull(recipient); 350 ck_assert_ptr_ne(sender, recipient); 351 352 GNUNET_CHAT_get_shared_attributes( 353 handle, 354 recipient, 355 on_gnunet_chat_attribute_share_attr, 356 handle 357 ); 358 } 359 break; 360 default: 361 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 362 break; 363 } 364 365 return GNUNET_YES; 366 } 367 368 const char *TEST_SHARE_IDS [] = { 369 TEST_SHARE_ID_A, 370 TEST_SHARE_ID_B, 371 NULL 372 }; 373 374 SETUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) 375 CLEANUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) 376 377 void 378 call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg) 379 { 380 static struct GNUNET_CHAT_Handle *handle = NULL; 381 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_share_msg, &handle); 382 383 ck_assert_ptr_nonnull(handle); 384 } 385 386 CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, gnunet_chat_attribute_share) 387 388 START_SUITE(handle_suite, "Attribute") 389 ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_share, "Share") 390 END_SUITE 391 392 MAIN_SUITE(handle_suite, CK_NORMAL)