diff options
Diffstat (limited to 'tests/test_gnunet_chat_lobby.c')
-rw-r--r-- | tests/test_gnunet_chat_lobby.c | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/tests/test_gnunet_chat_lobby.c b/tests/test_gnunet_chat_lobby.c new file mode 100644 index 0000000..8f079ed --- /dev/null +++ b/tests/test_gnunet_chat_lobby.c | |||
@@ -0,0 +1,252 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2022 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.c | ||
23 | */ | ||
24 | |||
25 | #include "test_gnunet_chat.h" | ||
26 | |||
27 | int | ||
28 | on_gnunet_chat_lobby_base_it(void *cls, | ||
29 | const struct GNUNET_CHAT_Handle *handle, | ||
30 | struct GNUNET_CHAT_Account *account) | ||
31 | { | ||
32 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
33 | |||
34 | ck_assert_ptr_ne(chat, NULL); | ||
35 | ck_assert_ptr_eq(handle, chat); | ||
36 | ck_assert_ptr_ne(account, NULL); | ||
37 | |||
38 | const char *name = GNUNET_CHAT_account_get_name(account); | ||
39 | |||
40 | ck_assert_ptr_ne(name, NULL); | ||
41 | ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); | ||
42 | |||
43 | if (0 == strcmp(name, "gnunet_chat_lobby_base")) | ||
44 | { | ||
45 | GNUNET_CHAT_connect(chat, account); | ||
46 | return GNUNET_NO; | ||
47 | } | ||
48 | |||
49 | return GNUNET_YES; | ||
50 | } | ||
51 | |||
52 | int | ||
53 | on_gnunet_chat_lobby_base_msg(void *cls, | ||
54 | struct GNUNET_CHAT_Context *context, | ||
55 | const struct GNUNET_CHAT_Message *message) | ||
56 | { | ||
57 | struct GNUNET_CHAT_Handle *handle = *( | ||
58 | (struct GNUNET_CHAT_Handle**) cls | ||
59 | ); | ||
60 | |||
61 | ck_assert_ptr_ne(handle, NULL); | ||
62 | ck_assert_ptr_eq(context, NULL); | ||
63 | ck_assert_ptr_ne(message, NULL); | ||
64 | |||
65 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | ||
66 | |||
67 | if ((kind != GNUNET_CHAT_KIND_REFRESH) || | ||
68 | (GNUNET_CHAT_get_connected(handle))) | ||
69 | goto skip_search_account; | ||
70 | |||
71 | ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); | ||
72 | |||
73 | GNUNET_CHAT_iterate_accounts( | ||
74 | handle, | ||
75 | on_gnunet_chat_lobby_base_it, | ||
76 | handle | ||
77 | ); | ||
78 | |||
79 | if (!GNUNET_CHAT_get_connected(handle)) | ||
80 | return GNUNET_YES; | ||
81 | |||
82 | skip_search_account: | ||
83 | if (GNUNET_CHAT_KIND_LOGIN != kind) | ||
84 | return GNUNET_YES; | ||
85 | |||
86 | ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); | ||
87 | |||
88 | struct GNUNET_CHAT_Lobby *lobby = GNUNET_CHAT_lobby_open( | ||
89 | handle, | ||
90 | GNUNET_TIME_relative_get_second_(), | ||
91 | NULL, | ||
92 | NULL | ||
93 | ); | ||
94 | |||
95 | ck_assert_ptr_ne(lobby, NULL); | ||
96 | |||
97 | GNUNET_CHAT_lobby_close(lobby); | ||
98 | GNUNET_CHAT_disconnect(handle); | ||
99 | |||
100 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | ||
101 | handle, | ||
102 | "gnunet_chat_lobby_base" | ||
103 | ), GNUNET_OK); | ||
104 | |||
105 | GNUNET_CHAT_stop(handle); | ||
106 | |||
107 | return GNUNET_YES; | ||
108 | } | ||
109 | |||
110 | void | ||
111 | call_gnunet_chat_lobby_base(const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
112 | { | ||
113 | static struct GNUNET_CHAT_Handle *handle = NULL; | ||
114 | handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_base_msg, &handle); | ||
115 | |||
116 | ck_assert_ptr_ne(handle, NULL); | ||
117 | ck_assert_int_eq(GNUNET_CHAT_account_create( | ||
118 | handle, | ||
119 | "gnunet_chat_lobby_base" | ||
120 | ), GNUNET_OK); | ||
121 | } | ||
122 | |||
123 | CREATE_GNUNET_TEST(test_gnunet_chat_lobby_base, call_gnunet_chat_lobby_base) | ||
124 | |||
125 | int | ||
126 | on_gnunet_chat_lobby_join_it(void *cls, | ||
127 | const struct GNUNET_CHAT_Handle *handle, | ||
128 | struct GNUNET_CHAT_Account *account) | ||
129 | { | ||
130 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
131 | |||
132 | ck_assert_ptr_ne(chat, NULL); | ||
133 | ck_assert_ptr_eq(handle, chat); | ||
134 | ck_assert_ptr_ne(account, NULL); | ||
135 | |||
136 | const char *name = GNUNET_CHAT_account_get_name(account); | ||
137 | |||
138 | ck_assert_ptr_ne(name, NULL); | ||
139 | ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); | ||
140 | |||
141 | if (0 == strcmp(name, "gnunet_chat_lobby_join")) | ||
142 | { | ||
143 | GNUNET_CHAT_connect(chat, account); | ||
144 | return GNUNET_NO; | ||
145 | } | ||
146 | |||
147 | return GNUNET_YES; | ||
148 | } | ||
149 | |||
150 | void | ||
151 | on_gnunet_chat_lobby_join_task(void *cls) | ||
152 | { | ||
153 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
154 | |||
155 | ck_assert_ptr_ne(chat, NULL); | ||
156 | |||
157 | GNUNET_CHAT_disconnect(chat); | ||
158 | |||
159 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | ||
160 | chat, | ||
161 | "gnunet_chat_lobby_join" | ||
162 | ), GNUNET_OK); | ||
163 | |||
164 | GNUNET_CHAT_stop(chat); | ||
165 | } | ||
166 | |||
167 | void | ||
168 | on_gnunet_chat_lobby_join_open(void *cls, | ||
169 | const struct GNUNET_CHAT_Uri *uri) | ||
170 | { | ||
171 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
172 | |||
173 | ck_assert_ptr_ne(chat, NULL); | ||
174 | ck_assert_ptr_ne(uri, NULL); | ||
175 | |||
176 | GNUNET_CHAT_lobby_join(chat, uri); | ||
177 | |||
178 | GNUNET_SCHEDULER_add_now( | ||
179 | on_gnunet_chat_lobby_join_task, | ||
180 | chat | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | int | ||
185 | on_gnunet_chat_lobby_join_msg(void *cls, | ||
186 | struct GNUNET_CHAT_Context *context, | ||
187 | const struct GNUNET_CHAT_Message *message) | ||
188 | { | ||
189 | struct GNUNET_CHAT_Handle *handle = *( | ||
190 | (struct GNUNET_CHAT_Handle**) cls | ||
191 | ); | ||
192 | |||
193 | ck_assert_ptr_ne(handle, NULL); | ||
194 | ck_assert_ptr_ne(message, NULL); | ||
195 | |||
196 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | ||
197 | |||
198 | if ((kind != GNUNET_CHAT_KIND_REFRESH) || | ||
199 | (GNUNET_CHAT_get_connected(handle))) | ||
200 | goto skip_search_account; | ||
201 | |||
202 | ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); | ||
203 | ck_assert_ptr_eq(context, NULL); | ||
204 | |||
205 | GNUNET_CHAT_iterate_accounts( | ||
206 | handle, | ||
207 | on_gnunet_chat_lobby_join_it, | ||
208 | handle | ||
209 | ); | ||
210 | |||
211 | if (!GNUNET_CHAT_get_connected(handle)) | ||
212 | return GNUNET_YES; | ||
213 | |||
214 | skip_search_account: | ||
215 | if (GNUNET_CHAT_KIND_LOGIN != kind) | ||
216 | return GNUNET_YES; | ||
217 | |||
218 | ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); | ||
219 | ck_assert_ptr_eq(context, NULL); | ||
220 | |||
221 | struct GNUNET_CHAT_Lobby *lobby = GNUNET_CHAT_lobby_open( | ||
222 | handle, | ||
223 | GNUNET_TIME_relative_get_second_(), | ||
224 | on_gnunet_chat_lobby_join_open, | ||
225 | handle | ||
226 | ); | ||
227 | |||
228 | ck_assert_ptr_ne(lobby, NULL); | ||
229 | return GNUNET_YES; | ||
230 | } | ||
231 | |||
232 | void | ||
233 | call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
234 | { | ||
235 | static struct GNUNET_CHAT_Handle *handle = NULL; | ||
236 | handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_join_msg, &handle); | ||
237 | |||
238 | ck_assert_ptr_ne(handle, NULL); | ||
239 | ck_assert_int_eq(GNUNET_CHAT_account_create( | ||
240 | handle, | ||
241 | "gnunet_chat_lobby_join" | ||
242 | ), GNUNET_OK); | ||
243 | } | ||
244 | |||
245 | CREATE_GNUNET_TEST(test_gnunet_chat_lobby_join, call_gnunet_chat_lobby_join) | ||
246 | |||
247 | START_SUITE(handle_suite, "Lobby") | ||
248 | ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_base, "Open/Close") | ||
249 | ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_join, "Join") | ||
250 | END_SUITE | ||
251 | |||
252 | MAIN_SUITE(handle_suite, CK_NORMAL) | ||