test_gnunet_chat_attribute_check.c (4531B)
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_check.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_CHECK_ID "gnunet_chat_attribute_check" 28 #define TEST_CHECK_NAME "test_attribute_check_name" 29 #define TEST_CHECK_VALUE "test_attribute_check_value" 30 31 enum GNUNET_GenericReturnValue 32 on_gnunet_chat_attribute_check_attr(void *cls, 33 struct GNUNET_CHAT_Handle *handle, 34 const char *name, 35 const char *value) 36 { 37 ck_assert_ptr_null(cls); 38 ck_assert_ptr_nonnull(handle); 39 ck_assert_ptr_nonnull(name); 40 41 if (0 == strcmp(name, TEST_CHECK_NAME)) 42 { 43 ck_assert_ptr_nonnull(value); 44 ck_assert_str_eq(value, TEST_CHECK_VALUE); 45 46 GNUNET_CHAT_delete_attribute(handle, TEST_CHECK_NAME); 47 return GNUNET_NO; 48 } 49 50 return GNUNET_YES; 51 } 52 53 enum GNUNET_GenericReturnValue 54 on_gnunet_chat_attribute_check_msg(void *cls, 55 struct GNUNET_CHAT_Context *context, 56 struct GNUNET_CHAT_Message *message) 57 { 58 static unsigned int attribute_stage = 0; 59 60 struct GNUNET_CHAT_Handle *handle = *( 61 (struct GNUNET_CHAT_Handle**) cls 62 ); 63 64 struct GNUNET_CHAT_Account *account; 65 const char *text; 66 67 ck_assert_ptr_nonnull(handle); 68 ck_assert_ptr_nonnull(message); 69 70 account = GNUNET_CHAT_message_get_account(message); 71 72 switch (GNUNET_CHAT_message_get_kind(message)) 73 { 74 case GNUNET_CHAT_KIND_WARNING: 75 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 76 break; 77 case GNUNET_CHAT_KIND_REFRESH: 78 ck_assert_ptr_null(context); 79 ck_assert_ptr_null(account); 80 81 if (attribute_stage == 0) 82 { 83 account = GNUNET_CHAT_find_account(handle, TEST_CHECK_ID); 84 85 ck_assert_ptr_nonnull(account); 86 87 GNUNET_CHAT_connect(handle, account); 88 attribute_stage = 1; 89 } 90 91 break; 92 case GNUNET_CHAT_KIND_LOGIN: 93 ck_assert_ptr_null(context); 94 ck_assert_ptr_nonnull(account); 95 ck_assert_uint_eq(attribute_stage, 1); 96 97 GNUNET_CHAT_set_attribute( 98 handle, 99 TEST_CHECK_NAME, 100 TEST_CHECK_VALUE 101 ); 102 103 attribute_stage = 2; 104 break; 105 case GNUNET_CHAT_KIND_LOGOUT: 106 ck_assert_ptr_null(context); 107 ck_assert_ptr_nonnull(account); 108 ck_assert_uint_eq(attribute_stage, 4); 109 110 GNUNET_CHAT_stop(handle); 111 break; 112 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 113 ck_assert_ptr_nonnull(account); 114 break; 115 case GNUNET_CHAT_KIND_ATTRIBUTES: 116 ck_assert_ptr_null(context); 117 118 text = GNUNET_CHAT_message_get_text(message); 119 120 if (text) 121 { 122 ck_assert_str_eq(text, TEST_CHECK_NAME); 123 ck_assert_uint_eq(attribute_stage, 2); 124 125 GNUNET_CHAT_get_attributes( 126 handle, 127 on_gnunet_chat_attribute_check_attr, 128 NULL 129 ); 130 131 attribute_stage = 3; 132 } 133 else 134 { 135 ck_assert_uint_eq(attribute_stage, 3); 136 137 GNUNET_CHAT_disconnect(handle); 138 139 attribute_stage = 4; 140 } 141 142 break; 143 default: 144 ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message)); 145 break; 146 } 147 148 return GNUNET_YES; 149 } 150 151 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_attribute_check, TEST_CHECK_ID) 152 153 void 154 call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg) 155 { 156 static struct GNUNET_CHAT_Handle *handle = NULL; 157 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_check_msg, &handle); 158 159 ck_assert_ptr_nonnull(handle); 160 } 161 162 CREATE_GNUNET_TEST(test_gnunet_chat_attribute_check, gnunet_chat_attribute_check) 163 164 START_SUITE(handle_suite, "Attribute") 165 ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_check, "Check") 166 END_SUITE 167 168 MAIN_SUITE(handle_suite, CK_NORMAL)