diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-31 22:50:14 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-31 22:50:14 +0200 |
commit | e2baf6fe4dd8358095a102ca320bf4abf034716a (patch) | |
tree | 182ec3d4d2c00dcac324db1133757bd1d9993edc | |
parent | 5b266a4c6e22ba210086b6d9759fd8aae01abaaf (diff) | |
download | libgnunetchat-e2baf6fe4dd8358095a102ca320bf4abf034716a.tar.gz libgnunetchat-e2baf6fe4dd8358095a102ca320bf4abf034716a.zip |
Completed handle tests and added user pointer functions to header
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | include/gnunet_chat_lib.h | 22 | ||||
-rw-r--r-- | tests/test_gnunet_chat.h | 2 | ||||
-rw-r--r-- | tests/test_gnunet_chat_handle.c | 160 |
3 files changed, 166 insertions, 18 deletions
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h index 135e82e..6d3a581 100644 --- a/include/gnunet_chat_lib.h +++ b/include/gnunet_chat_lib.h | |||
@@ -522,6 +522,28 @@ void | |||
522 | GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, | 522 | GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, |
523 | const struct GNUNET_CHAT_Uri *uri); | 523 | const struct GNUNET_CHAT_Uri *uri); |
524 | 524 | ||
525 | |||
526 | /** | ||
527 | * Sets a custom <i>user pointer</i> to a given chat <i>handle</i> so it can | ||
528 | * be accessed in all handle related callbacks. | ||
529 | * | ||
530 | * @param[in,out] handle Chat handle | ||
531 | * @param[in] user_pointer Custom user pointer | ||
532 | */ | ||
533 | void | ||
534 | GNUNET_CHAT_set_user_pointer (struct GNUNET_CHAT_Handle *handle, | ||
535 | void *user_pointer); | ||
536 | |||
537 | /** | ||
538 | * Returns the custom user pointer of a given chat <i>handle</i> or NULL if it | ||
539 | * was not set any. | ||
540 | * | ||
541 | * @param[in] handle Chat handle | ||
542 | * @return Custom user pointer or NULL | ||
543 | */ | ||
544 | void* | ||
545 | GNUNET_CHAT_get_user_pointer (const struct GNUNET_CHAT_Handle *handle); | ||
546 | |||
525 | /** | 547 | /** |
526 | * Iterates through the contacts of a given chat <i>handle</i> with a selected | 548 | * Iterates through the contacts of a given chat <i>handle</i> with a selected |
527 | * callback and custom closure. | 549 | * callback and custom closure. |
diff --git a/tests/test_gnunet_chat.h b/tests/test_gnunet_chat.h index 2172611..c7e6dcb 100644 --- a/tests/test_gnunet_chat.h +++ b/tests/test_gnunet_chat.h | |||
@@ -32,8 +32,6 @@ | |||
32 | 32 | ||
33 | #include <gnunet/gnunet_chat_lib.h> | 33 | #include <gnunet/gnunet_chat_lib.h> |
34 | 34 | ||
35 | #define GNUNET_CHAT_TEST_ACCOUNT "check" | ||
36 | |||
37 | #define CREATE_GNUNET_TEST(test_name, test_call) \ | 35 | #define CREATE_GNUNET_TEST(test_name, test_call) \ |
38 | void \ | 36 | void \ |
39 | task_##test_call (__attribute__ ((unused)) void *cls, \ | 37 | task_##test_call (__attribute__ ((unused)) void *cls, \ |
diff --git a/tests/test_gnunet_chat_handle.c b/tests/test_gnunet_chat_handle.c index f8a7b60..f4d4ef8 100644 --- a/tests/test_gnunet_chat_handle.c +++ b/tests/test_gnunet_chat_handle.c | |||
@@ -49,7 +49,7 @@ on_gnunet_chat_handle_accounts_it(__attribute__ ((unused)) void *cls, | |||
49 | 49 | ||
50 | ck_assert_ptr_ne(name, NULL); | 50 | ck_assert_ptr_ne(name, NULL); |
51 | 51 | ||
52 | if (0 == strcmp(name, GNUNET_CHAT_TEST_ACCOUNT)) | 52 | if (0 == strcmp(name, "gnunet_chat_handle_accounts")) |
53 | accounts_stage |= 2; | 53 | accounts_stage |= 2; |
54 | 54 | ||
55 | return GNUNET_YES; | 55 | return GNUNET_YES; |
@@ -77,7 +77,7 @@ on_gnunet_chat_handle_accounts_msg(void *cls, | |||
77 | if (3 == accounts_stage) | 77 | if (3 == accounts_stage) |
78 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | 78 | ck_assert_int_eq(GNUNET_CHAT_account_delete( |
79 | accounts_handle, | 79 | accounts_handle, |
80 | GNUNET_CHAT_TEST_ACCOUNT | 80 | "gnunet_chat_handle_accounts" |
81 | ), GNUNET_OK); | 81 | ), GNUNET_OK); |
82 | 82 | ||
83 | accounts_stage = 4; | 83 | accounts_stage = 4; |
@@ -88,7 +88,7 @@ on_gnunet_chat_handle_accounts_msg(void *cls, | |||
88 | { | 88 | { |
89 | ck_assert_int_eq(GNUNET_CHAT_account_create( | 89 | ck_assert_int_eq(GNUNET_CHAT_account_create( |
90 | accounts_handle, | 90 | accounts_handle, |
91 | GNUNET_CHAT_TEST_ACCOUNT | 91 | "gnunet_chat_handle_accounts" |
92 | ), GNUNET_OK); | 92 | ), GNUNET_OK); |
93 | 93 | ||
94 | accounts_stage = 1; | 94 | accounts_stage = 1; |
@@ -108,53 +108,181 @@ call_gnunet_chat_handle_accounts(const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
108 | 108 | ||
109 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, call_gnunet_chat_handle_accounts) | 109 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_accounts, call_gnunet_chat_handle_accounts) |
110 | 110 | ||
111 | struct GNUNET_CHAT_Handle *connection_handle; | 111 | int |
112 | on_gnunet_chat_handle_connection_it(void *cls, | ||
113 | const struct GNUNET_CHAT_Handle *handle, | ||
114 | struct GNUNET_CHAT_Account *account) | ||
115 | { | ||
116 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
117 | |||
118 | ck_assert_ptr_ne(chat, NULL); | ||
119 | ck_assert_ptr_eq(handle, chat); | ||
120 | ck_assert_ptr_ne(account, NULL); | ||
121 | |||
122 | const char *name = GNUNET_CHAT_account_get_name(account); | ||
123 | |||
124 | ck_assert_ptr_ne(name, NULL); | ||
125 | ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); | ||
126 | |||
127 | if (0 == strcmp(name, "gnunet_chat_handle_connection")) | ||
128 | { | ||
129 | GNUNET_CHAT_connect(chat, account); | ||
130 | return GNUNET_NO; | ||
131 | } | ||
132 | |||
133 | return GNUNET_YES; | ||
134 | } | ||
112 | 135 | ||
113 | int | 136 | int |
114 | on_gnunet_chat_handle_connection_msg(void *cls, | 137 | on_gnunet_chat_handle_connection_msg(void *cls, |
115 | struct GNUNET_CHAT_Context *context, | 138 | struct GNUNET_CHAT_Context *context, |
116 | const struct GNUNET_CHAT_Message *message) | 139 | const struct GNUNET_CHAT_Message *message) |
117 | { | 140 | { |
118 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | 141 | struct GNUNET_CHAT_Handle *handle = *( |
142 | (struct GNUNET_CHAT_Handle**) cls | ||
143 | ); | ||
119 | 144 | ||
120 | ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); | 145 | ck_assert_ptr_ne(handle, NULL); |
121 | ck_assert_ptr_eq(cls, NULL); | ||
122 | ck_assert_ptr_eq(context, NULL); | 146 | ck_assert_ptr_eq(context, NULL); |
147 | ck_assert_ptr_ne(message, NULL); | ||
123 | 148 | ||
124 | GNUNET_CHAT_stop(connection_handle); | 149 | GNUNET_CHAT_iterate_accounts( |
150 | handle, | ||
151 | on_gnunet_chat_handle_connection_it, | ||
152 | handle | ||
153 | ); | ||
154 | |||
155 | if (!GNUNET_CHAT_get_connected(handle)) | ||
156 | return GNUNET_YES; | ||
157 | |||
158 | GNUNET_CHAT_disconnect(handle); | ||
159 | |||
160 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | ||
161 | handle, | ||
162 | "gnunet_chat_handle_connection" | ||
163 | ), GNUNET_OK); | ||
164 | |||
165 | GNUNET_CHAT_stop(handle); | ||
125 | return GNUNET_YES; | 166 | return GNUNET_YES; |
126 | } | 167 | } |
127 | 168 | ||
128 | void | 169 | void |
129 | call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg) | 170 | call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg) |
130 | { | 171 | { |
131 | connection_handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, NULL); | 172 | static struct GNUNET_CHAT_Handle *handle = NULL; |
132 | ck_assert_ptr_ne(connection_handle, NULL); | 173 | handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, &handle); |
174 | |||
175 | ck_assert_ptr_ne(handle, NULL); | ||
176 | ck_assert_int_eq(GNUNET_CHAT_account_create( | ||
177 | handle, | ||
178 | "gnunet_chat_handle_connection" | ||
179 | ), GNUNET_OK); | ||
133 | } | 180 | } |
134 | 181 | ||
135 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, call_gnunet_chat_handle_connection) | 182 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, call_gnunet_chat_handle_connection) |
136 | 183 | ||
137 | struct GNUNET_CHAT_Handle *update_handle; | 184 | int |
185 | on_gnunet_chat_handle_update_it(void *cls, | ||
186 | const struct GNUNET_CHAT_Handle *handle, | ||
187 | struct GNUNET_CHAT_Account *account) | ||
188 | { | ||
189 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
190 | |||
191 | ck_assert_ptr_ne(chat, NULL); | ||
192 | ck_assert_ptr_eq(handle, chat); | ||
193 | ck_assert_ptr_ne(account, NULL); | ||
194 | |||
195 | const char *name = GNUNET_CHAT_account_get_name(account); | ||
196 | |||
197 | ck_assert_ptr_ne(name, NULL); | ||
198 | ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); | ||
199 | |||
200 | if (0 == strcmp(name, "gnunet_chat_handle_update")) | ||
201 | { | ||
202 | GNUNET_CHAT_connect(chat, account); | ||
203 | return GNUNET_NO; | ||
204 | } | ||
205 | |||
206 | return GNUNET_YES; | ||
207 | } | ||
138 | 208 | ||
139 | int | 209 | int |
140 | on_gnunet_chat_handle_update_msg(void *cls, | 210 | on_gnunet_chat_handle_update_msg(void *cls, |
141 | struct GNUNET_CHAT_Context *context, | 211 | struct GNUNET_CHAT_Context *context, |
142 | const struct GNUNET_CHAT_Message *message) | 212 | const struct GNUNET_CHAT_Message *message) |
143 | { | 213 | { |
144 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | 214 | struct GNUNET_CHAT_Handle *handle = *( |
215 | (struct GNUNET_CHAT_Handle**) cls | ||
216 | ); | ||
145 | 217 | ||
146 | ck_assert_ptr_eq(cls, NULL); | 218 | ck_assert_ptr_ne(handle, NULL); |
147 | ck_assert_ptr_eq(context, NULL); | 219 | ck_assert_ptr_eq(context, NULL); |
220 | ck_assert_ptr_ne(message, NULL); | ||
221 | |||
222 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | ||
223 | |||
224 | if (GNUNET_CHAT_get_connected(handle)) | ||
225 | goto skip_search_account; | ||
226 | |||
227 | GNUNET_CHAT_iterate_accounts( | ||
228 | handle, | ||
229 | on_gnunet_chat_handle_update_it, | ||
230 | handle | ||
231 | ); | ||
232 | |||
233 | if (!GNUNET_CHAT_get_connected(handle)) | ||
234 | return GNUNET_YES; | ||
235 | |||
236 | skip_search_account: | ||
237 | if (GNUNET_CHAT_KIND_LOGIN != kind) | ||
238 | return GNUNET_YES; | ||
239 | |||
240 | const char *key = GNUNET_CHAT_get_key(handle); | ||
241 | ck_assert_ptr_ne(key, NULL); | ||
242 | |||
243 | char *dup = (char*) GNUNET_CHAT_get_user_pointer(handle); | ||
244 | |||
245 | if (!dup) | ||
246 | { | ||
247 | dup = GNUNET_strdup(key); | ||
248 | |||
249 | ck_assert_ptr_ne(dup, NULL); | ||
250 | ck_assert_str_eq(key, dup); | ||
251 | |||
252 | GNUNET_CHAT_set_user_pointer(handle, (void*) dup); | ||
253 | GNUNET_CHAT_update(handle); | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | ck_assert_ptr_ne(dup, NULL); | ||
258 | // ck_assert_str_ne(key, dup); // TODO: needs to be implemented in service! | ||
259 | |||
260 | GNUNET_free(dup); | ||
261 | |||
262 | GNUNET_CHAT_disconnect(handle); | ||
263 | |||
264 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | ||
265 | handle, | ||
266 | "gnunet_chat_handle_update" | ||
267 | ), GNUNET_OK); | ||
268 | |||
269 | GNUNET_CHAT_stop(handle); | ||
270 | } | ||
148 | 271 | ||
149 | GNUNET_CHAT_stop(update_handle); | ||
150 | return GNUNET_YES; | 272 | return GNUNET_YES; |
151 | } | 273 | } |
152 | 274 | ||
153 | void | 275 | void |
154 | call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) | 276 | call_gnunet_chat_handle_update(const struct GNUNET_CONFIGURATION_Handle *cfg) |
155 | { | 277 | { |
156 | update_handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, NULL); | 278 | static struct GNUNET_CHAT_Handle *handle = NULL; |
157 | ck_assert_ptr_ne(update_handle, NULL); | 279 | handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_update_msg, &handle); |
280 | |||
281 | ck_assert_ptr_ne(handle, NULL); | ||
282 | ck_assert_int_eq(GNUNET_CHAT_account_create( | ||
283 | handle, | ||
284 | "gnunet_chat_handle_update" | ||
285 | ), GNUNET_OK); | ||
158 | } | 286 | } |
159 | 287 | ||
160 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, call_gnunet_chat_handle_update) | 288 | CREATE_GNUNET_TEST(test_gnunet_chat_handle_update, call_gnunet_chat_handle_update) |