test_gnunet_chat_lobby_join.c (4658B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2022--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_lobby_join.c 23 */ 24 25 #include "test_gnunet_chat.h" 26 27 #define TEST_JOIN_ID "gnunet_chat_lobby_join" 28 #define TEST_JOIN_SECRET "test_secret_lobby_join" 29 30 void 31 on_gnunet_chat_lobby_join_open(void *cls, 32 const struct GNUNET_CHAT_Uri *uri) 33 { 34 struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; 35 36 ck_assert_ptr_nonnull(chat); 37 ck_assert_ptr_nonnull(uri); 38 39 GNUNET_CHAT_lobby_join(chat, uri); 40 } 41 42 enum GNUNET_GenericReturnValue 43 on_gnunet_chat_lobby_join_msg(void *cls, 44 struct GNUNET_CHAT_Context *context, 45 struct GNUNET_CHAT_Message *message) 46 { 47 static unsigned int lobby_stage = 0; 48 static struct GNUNET_CHAT_Lobby *lobby = NULL; 49 50 struct GNUNET_CHAT_Handle *handle = *( 51 (struct GNUNET_CHAT_Handle**) cls 52 ); 53 54 ck_assert_ptr_nonnull(handle); 55 ck_assert_ptr_nonnull(message); 56 57 struct GNUNET_CHAT_Account *account; 58 account = GNUNET_CHAT_message_get_account(message); 59 60 struct GNUNET_CHAT_Group *group; 61 62 switch (GNUNET_CHAT_message_get_kind(message)) 63 { 64 case GNUNET_CHAT_KIND_WARNING: 65 ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message)); 66 break; 67 case GNUNET_CHAT_KIND_REFRESH: 68 ck_assert_ptr_null(context); 69 ck_assert_ptr_null(account); 70 71 if (lobby_stage == 0) 72 { 73 account = GNUNET_CHAT_find_account(handle, TEST_JOIN_ID); 74 75 ck_assert_ptr_nonnull(account); 76 77 GNUNET_CHAT_connect( 78 handle, 79 account, 80 TEST_JOIN_SECRET, 81 strlen(TEST_JOIN_SECRET) 82 ); 83 84 lobby_stage = 1; 85 } 86 87 break; 88 case GNUNET_CHAT_KIND_LOGIN: 89 ck_assert_ptr_null(context); 90 ck_assert_ptr_nonnull(account); 91 ck_assert_ptr_null(lobby); 92 ck_assert_uint_eq(lobby_stage, 1); 93 94 lobby = GNUNET_CHAT_lobby_open( 95 handle, 96 GNUNET_CHAT_LOBBY_TYPE_GROUP, 97 1, 98 on_gnunet_chat_lobby_join_open, 99 handle 100 ); 101 102 ck_assert_ptr_nonnull(lobby); 103 lobby_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_ptr_null(lobby); 109 ck_assert_uint_eq(lobby_stage, 4); 110 111 GNUNET_CHAT_stop(handle); 112 break; 113 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 114 ck_assert_ptr_null(context); 115 ck_assert_ptr_nonnull(account); 116 break; 117 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 118 ck_assert_ptr_nonnull(context); 119 break; 120 case GNUNET_CHAT_KIND_JOIN: 121 ck_assert_ptr_nonnull(context); 122 ck_assert_ptr_nonnull(account); 123 ck_assert_ptr_nonnull(lobby); 124 ck_assert_uint_eq(lobby_stage, 2); 125 126 GNUNET_CHAT_lobby_close(lobby); 127 lobby = NULL; 128 129 group = GNUNET_CHAT_context_get_group(context); 130 131 ck_assert_ptr_nonnull(group); 132 133 ck_assert_int_eq( 134 GNUNET_CHAT_group_leave(group), 135 GNUNET_OK 136 ); 137 138 lobby_stage = 3; 139 break; 140 case GNUNET_CHAT_KIND_LEAVE: 141 ck_assert_ptr_nonnull(context); 142 ck_assert_ptr_nonnull(account); 143 ck_assert_uint_eq(lobby_stage, 3); 144 145 GNUNET_CHAT_disconnect(handle); 146 147 lobby_stage = 4; 148 break; 149 case GNUNET_CHAT_KIND_CONTACT: 150 break; 151 default: 152 ck_abort(); 153 break; 154 } 155 156 return GNUNET_YES; 157 } 158 159 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_lobby_join, TEST_JOIN_ID) 160 161 void 162 call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg) 163 { 164 static struct GNUNET_CHAT_Handle *handle = NULL; 165 handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_join_msg, &handle); 166 167 ck_assert_ptr_nonnull(handle); 168 } 169 170 CREATE_GNUNET_TEST(test_gnunet_chat_lobby_join, gnunet_chat_lobby_join) 171 172 START_SUITE(lobby_suite, "Lobby") 173 ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_join, "Join") 174 END_SUITE 175 176 MAIN_SUITE(lobby_suite, CK_NORMAL)