test_gnunet_chat_attribute_share.c (7437B)
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_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 33 enum GNUNET_GenericReturnValue 34 on_gnunet_chat_attribute_share_attr(void *cls, 35 struct GNUNET_CHAT_Contact *contact, 36 const char *name, 37 const char *value) 38 { 39 ck_assert_ptr_nonnull(cls); 40 ck_assert_ptr_nonnull(contact); 41 ck_assert_ptr_nonnull(name); 42 ck_assert_ptr_nonnull(value); 43 44 struct GNUNET_CHAT_Handle *handle = ( 45 (struct GNUNET_CHAT_Handle*) cls 46 ); 47 48 const struct GNUNET_CHAT_Account *account; 49 account = GNUNET_CHAT_get_connected(handle); 50 51 const char *account_name = GNUNET_CHAT_account_get_name(account); 52 const char *contact_name = GNUNET_CHAT_contact_get_name(contact); 53 54 ck_assert_ptr_nonnull(account); 55 ck_assert_str_eq(account_name, TEST_SHARE_ID_B); 56 57 if (contact_name) 58 ck_assert_str_eq(contact_name, TEST_SHARE_ID_A); 59 60 ck_assert_str_eq(name, TEST_SHARE_NAME); 61 ck_assert_str_eq(value, TEST_SHARE_VALUE); 62 63 GNUNET_CHAT_unshare_attribute_from(handle, contact, name); 64 return GNUNET_NO; 65 } 66 67 enum GNUNET_GenericReturnValue 68 on_gnunet_chat_attribute_share_msg(void *cls, 69 struct GNUNET_CHAT_Context *context, 70 struct GNUNET_CHAT_Message *message) 71 { 72 static unsigned int share_stage = 0; 73 74 struct GNUNET_CHAT_Handle *handle = *( 75 (struct GNUNET_CHAT_Handle**) cls 76 ); 77 78 struct GNUNET_CHAT_Account *account; 79 struct GNUNET_CHAT_Contact *recipient; 80 struct GNUNET_CHAT_Contact *sender; 81 const char *name; 82 83 ck_assert_ptr_nonnull(handle); 84 ck_assert_ptr_nonnull(message); 85 86 account = GNUNET_CHAT_message_get_account(message); 87 recipient = GNUNET_CHAT_message_get_recipient(message); 88 sender = GNUNET_CHAT_message_get_sender(message); 89 90 switch (GNUNET_CHAT_message_get_kind(message)) 91 { 92 case GNUNET_CHAT_KIND_WARNING: 93 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 94 break; 95 case GNUNET_CHAT_KIND_REFRESH: 96 ck_assert_ptr_null(context); 97 ck_assert_ptr_null(account); 98 99 if (share_stage == 0) 100 { 101 account = GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_A); 102 103 ck_assert_ptr_nonnull(account); 104 105 GNUNET_CHAT_connect(handle, account); 106 share_stage = 1; 107 } 108 109 break; 110 case GNUNET_CHAT_KIND_LOGIN: 111 ck_assert_ptr_null(context); 112 ck_assert_ptr_nonnull(account); 113 114 name = GNUNET_CHAT_account_get_name(account); 115 116 ck_assert_ptr_nonnull(name); 117 ck_assert_uint_ge(share_stage, 1); 118 119 if (0 == strcmp(name, TEST_SHARE_ID_B)) 120 { 121 ck_assert_uint_eq(share_stage, 4); 122 123 GNUNET_CHAT_set_attribute( 124 handle, 125 TEST_SHARE_NAME, 126 TEST_SHARE_VALUE 127 ); 128 129 share_stage = 5; 130 } 131 else 132 { 133 ck_assert_uint_eq(share_stage, 1); 134 135 share_stage = 2; 136 } 137 138 ck_assert_ptr_nonnull(GNUNET_CHAT_group_create( 139 handle, TEST_SHARE_GROUP 140 )); 141 break; 142 case GNUNET_CHAT_KIND_LOGOUT: 143 ck_assert_ptr_null(context); 144 ck_assert_ptr_nonnull(account); 145 ck_assert_uint_ge(share_stage, 3); 146 147 name = GNUNET_CHAT_account_get_name(account); 148 149 ck_assert_ptr_nonnull(name); 150 151 if (0 == strcmp(name, TEST_SHARE_ID_A)) 152 { 153 ck_assert_uint_eq(share_stage, 3); 154 155 share_stage = 4; 156 } 157 else if (0 == strcmp(name, TEST_SHARE_ID_B)) 158 { 159 ck_assert_uint_eq(share_stage, 7); 160 161 GNUNET_CHAT_stop(handle); 162 } 163 164 break; 165 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 166 ck_assert_ptr_nonnull(account); 167 break; 168 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 169 ck_assert_ptr_nonnull(context); 170 break; 171 case GNUNET_CHAT_KIND_JOIN: 172 ck_assert_ptr_nonnull(context); 173 ck_assert_ptr_nonnull(account); 174 ck_assert_ptr_nonnull(sender); 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 (GNUNET_YES == GNUNET_CHAT_message_is_sent(message))) 182 { 183 ck_assert_uint_eq(share_stage, 2); 184 185 GNUNET_CHAT_connect( 186 handle, 187 GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_B) 188 ); 189 190 share_stage = 3; 191 } 192 else if ((GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) && 193 (share_stage == 5)) 194 { 195 GNUNET_CHAT_share_attribute_with(handle, sender, TEST_SHARE_NAME); 196 share_stage = 6; 197 } 198 199 break; 200 case GNUNET_CHAT_KIND_CONTACT: 201 ck_assert_ptr_nonnull(context); 202 ck_assert_ptr_nonnull(sender); 203 break; 204 case GNUNET_CHAT_KIND_ATTRIBUTES: 205 ck_assert_ptr_null(context); 206 ck_assert_uint_ge(share_stage, 5); 207 break; 208 case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: 209 ck_assert_uint_ge(share_stage, 6); 210 211 if (!context) 212 { 213 ck_assert_ptr_null(account); 214 ck_assert_ptr_null(sender); 215 ck_assert_ptr_null(recipient); 216 217 GNUNET_CHAT_disconnect(handle); 218 share_stage = 7; 219 } 220 else if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)) 221 { 222 ck_assert_ptr_nonnull(account); 223 ck_assert_ptr_nonnull(sender); 224 ck_assert_ptr_nonnull(recipient); 225 ck_assert_ptr_ne(sender, recipient); 226 227 GNUNET_CHAT_get_shared_attributes( 228 handle, 229 recipient, 230 on_gnunet_chat_attribute_share_attr, 231 handle 232 ); 233 } 234 break; 235 default: 236 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 237 break; 238 } 239 240 return GNUNET_YES; 241 } 242 243 const char *TEST_SHARE_IDS [] = { 244 TEST_SHARE_ID_A, 245 TEST_SHARE_ID_B, 246 NULL 247 }; 248 249 SETUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) 250 CLEANUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS) 251 252 void 253 call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg) 254 { 255 static struct GNUNET_CHAT_Handle *handle = NULL; 256 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_share_msg, &handle); 257 258 ck_assert_ptr_nonnull(handle); 259 } 260 261 CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, gnunet_chat_attribute_share) 262 263 START_SUITE(handle_suite, "Attribute") 264 ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_share, "Share") 265 END_SUITE 266 267 MAIN_SUITE(handle_suite, CK_NORMAL)