diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-09-19 12:37:34 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-09-19 12:37:34 +0200 |
commit | df221ee6f08296510edd171b2ee9415cfd4d3e66 (patch) | |
tree | 192cd2dbc9a093daee4f65a21a2d331c44d2099a | |
parent | c5cc59052212c61d9cf613c95811141c7bbcfb7d (diff) | |
download | libgnunetchat-df221ee6f08296510edd171b2ee9415cfd4d3e66.tar.gz libgnunetchat-df221ee6f08296510edd171b2ee9415cfd4d3e66.zip |
Add file test and fixed memory leaks
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | src/gnunet_chat_handle.c | 22 | ||||
-rw-r--r-- | src/gnunet_chat_lib.c | 2 | ||||
-rw-r--r-- | src/gnunet_chat_util.c | 2 | ||||
-rw-r--r-- | tests/test_gnunet_chat_file.c | 176 |
5 files changed, 196 insertions, 9 deletions
@@ -25,7 +25,8 @@ SOURCES = gnunet_chat_lib.c\ | |||
25 | HEADERS = gnunet_chat_lib.h | 25 | HEADERS = gnunet_chat_lib.h |
26 | 26 | ||
27 | TESTS = test_gnunet_chat_handle.c\ | 27 | TESTS = test_gnunet_chat_handle.c\ |
28 | test_gnunet_chat_lobby.c | 28 | test_gnunet_chat_lobby.c\ |
29 | test_gnunet_chat_file.c | ||
29 | 30 | ||
30 | LIBRARIES = gnunetarm\ | 31 | LIBRARIES = gnunetarm\ |
31 | gnunetfs\ | 32 | gnunetfs\ |
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c index 80ade04..7edc209 100644 --- a/src/gnunet_chat_handle.c +++ b/src/gnunet_chat_handle.c | |||
@@ -297,7 +297,10 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle) | |||
297 | { | 297 | { |
298 | internal = handle->internal_head; | 298 | internal = handle->internal_head; |
299 | 299 | ||
300 | if ((internal->msg) && (internal->msg->context)) | 300 | if (!(internal->msg->context)) |
301 | break; | ||
302 | |||
303 | if (internal->msg) | ||
301 | message_destroy(internal->msg); | 304 | message_destroy(internal->msg); |
302 | 305 | ||
303 | GNUNET_CONTAINER_DLL_remove( | 306 | GNUNET_CONTAINER_DLL_remove( |
@@ -546,11 +549,18 @@ handle_send_internal_message (struct GNUNET_CHAT_Handle *handle, | |||
546 | 549 | ||
547 | handle->msg_cb(handle->msg_cls, context, internal->msg); | 550 | handle->msg_cb(handle->msg_cls, context, internal->msg); |
548 | 551 | ||
549 | GNUNET_CONTAINER_DLL_insert( | 552 | if (context) |
550 | handle->internal_head, | 553 | GNUNET_CONTAINER_DLL_insert( |
551 | handle->internal_tail, | 554 | handle->internal_head, |
552 | internal | 555 | handle->internal_tail, |
553 | ); | 556 | internal |
557 | ); | ||
558 | else | ||
559 | GNUNET_CONTAINER_DLL_insert_tail( | ||
560 | handle->internal_head, | ||
561 | handle->internal_tail, | ||
562 | internal | ||
563 | ); | ||
554 | } | 564 | } |
555 | 565 | ||
556 | void | 566 | void |
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index b6911e7..640f70e 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c | |||
@@ -73,7 +73,7 @@ GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle) | |||
73 | 73 | ||
74 | handle->destruction = GNUNET_SCHEDULER_add_at_with_priority( | 74 | handle->destruction = GNUNET_SCHEDULER_add_at_with_priority( |
75 | GNUNET_TIME_absolute_get(), | 75 | GNUNET_TIME_absolute_get(), |
76 | GNUNET_SCHEDULER_PRIORITY_IDLE, | 76 | GNUNET_SCHEDULER_PRIORITY_URGENT, |
77 | task_handle_destruction, | 77 | task_handle_destruction, |
78 | handle | 78 | handle |
79 | ); | 79 | ); |
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c index 8a0879b..b97e0f1 100644 --- a/src/gnunet_chat_util.c +++ b/src/gnunet_chat_util.c | |||
@@ -292,7 +292,7 @@ util_get_lower(const char *name) | |||
292 | { | 292 | { |
293 | GNUNET_assert(name); | 293 | GNUNET_assert(name); |
294 | 294 | ||
295 | char *lower = GNUNET_malloc(strlen(name)); | 295 | char *lower = GNUNET_malloc(strlen(name) + 1); |
296 | if (GNUNET_OK == GNUNET_STRINGS_utf8_tolower(name, lower)) | 296 | if (GNUNET_OK == GNUNET_STRINGS_utf8_tolower(name, lower)) |
297 | return lower; | 297 | return lower; |
298 | 298 | ||
diff --git a/tests/test_gnunet_chat_file.c b/tests/test_gnunet_chat_file.c new file mode 100644 index 0000000..784fe04 --- /dev/null +++ b/tests/test_gnunet_chat_file.c | |||
@@ -0,0 +1,176 @@ | |||
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_file.c | ||
23 | */ | ||
24 | |||
25 | #include "test_gnunet_chat.h" | ||
26 | |||
27 | int | ||
28 | on_gnunet_chat_file_send_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_file_send")) | ||
44 | { | ||
45 | GNUNET_CHAT_connect(chat, account); | ||
46 | return GNUNET_NO; | ||
47 | } | ||
48 | |||
49 | return GNUNET_YES; | ||
50 | } | ||
51 | |||
52 | void | ||
53 | on_gnunet_chat_file_send_upload(void *cls, | ||
54 | const struct GNUNET_CHAT_File *file, | ||
55 | uint64_t completed, | ||
56 | uint64_t size) | ||
57 | { | ||
58 | struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls; | ||
59 | |||
60 | ck_assert_ptr_ne(chat, NULL); | ||
61 | ck_assert_ptr_ne(file, NULL); | ||
62 | ck_assert_uint_le(completed, size); | ||
63 | |||
64 | uint64_t check_size = GNUNET_CHAT_file_get_size(file); | ||
65 | |||
66 | ck_assert_uint_eq(size, check_size); | ||
67 | |||
68 | // TODO | ||
69 | } | ||
70 | |||
71 | int | ||
72 | on_gnunet_chat_file_send_msg(void *cls, | ||
73 | struct GNUNET_CHAT_Context *context, | ||
74 | const struct GNUNET_CHAT_Message *message) | ||
75 | { | ||
76 | struct GNUNET_CHAT_Handle *handle = *( | ||
77 | (struct GNUNET_CHAT_Handle**) cls | ||
78 | ); | ||
79 | |||
80 | ck_assert_ptr_ne(handle, NULL); | ||
81 | ck_assert_ptr_ne(message, NULL); | ||
82 | |||
83 | enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); | ||
84 | |||
85 | if ((kind != GNUNET_CHAT_KIND_REFRESH) || | ||
86 | (GNUNET_CHAT_get_connected(handle))) | ||
87 | goto skip_search_account; | ||
88 | |||
89 | ck_assert(kind == GNUNET_CHAT_KIND_REFRESH); | ||
90 | ck_assert_ptr_eq(context, NULL); | ||
91 | |||
92 | GNUNET_CHAT_iterate_accounts( | ||
93 | handle, | ||
94 | on_gnunet_chat_file_send_it, | ||
95 | handle | ||
96 | ); | ||
97 | |||
98 | if (!GNUNET_CHAT_get_connected(handle)) | ||
99 | return GNUNET_YES; | ||
100 | |||
101 | skip_search_account: | ||
102 | if ((GNUNET_CHAT_KIND_LOGIN != kind) || | ||
103 | (context)) | ||
104 | goto skip_first_login; | ||
105 | |||
106 | |||
107 | ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); | ||
108 | ck_assert_ptr_eq(context, NULL); | ||
109 | |||
110 | struct GNUNET_CHAT_Group *group = GNUNET_CHAT_group_create( | ||
111 | handle, | ||
112 | "gnunet_chat_file_send_group" | ||
113 | ); | ||
114 | |||
115 | ck_assert_ptr_ne(group, NULL); | ||
116 | |||
117 | struct GNUNET_CHAT_Context *group_context = GNUNET_CHAT_group_get_context( | ||
118 | group | ||
119 | ); | ||
120 | |||
121 | ck_assert_ptr_ne(group_context, NULL); | ||
122 | |||
123 | skip_first_login: | ||
124 | if (GNUNET_CHAT_KIND_JOIN != kind) | ||
125 | goto skip_file_upload; | ||
126 | |||
127 | ck_assert(kind == GNUNET_CHAT_KIND_JOIN); | ||
128 | ck_assert_ptr_ne(context, NULL); | ||
129 | |||
130 | struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file( | ||
131 | context, | ||
132 | "Makefile", | ||
133 | on_gnunet_chat_file_send_upload, | ||
134 | handle | ||
135 | ); | ||
136 | |||
137 | ck_assert_ptr_ne(file, NULL); | ||
138 | |||
139 | skip_file_upload: | ||
140 | if (GNUNET_CHAT_KIND_FILE != kind) | ||
141 | return GNUNET_YES; | ||
142 | |||
143 | ck_assert(kind == GNUNET_CHAT_KIND_FILE); | ||
144 | ck_assert_ptr_ne(context, NULL); | ||
145 | |||
146 | GNUNET_CHAT_disconnect(handle); | ||
147 | |||
148 | ck_assert_int_eq(GNUNET_CHAT_account_delete( | ||
149 | handle, | ||
150 | "gnunet_chat_file_send" | ||
151 | ), GNUNET_OK); | ||
152 | |||
153 | GNUNET_CHAT_stop(handle); | ||
154 | return GNUNET_YES; | ||
155 | } | ||
156 | |||
157 | void | ||
158 | call_gnunet_chat_file_send(const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
159 | { | ||
160 | static struct GNUNET_CHAT_Handle *handle = NULL; | ||
161 | handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_file_send_msg, &handle); | ||
162 | |||
163 | ck_assert_ptr_ne(handle, NULL); | ||
164 | ck_assert_int_eq(GNUNET_CHAT_account_create( | ||
165 | handle, | ||
166 | "gnunet_chat_file_send" | ||
167 | ), GNUNET_OK); | ||
168 | } | ||
169 | |||
170 | CREATE_GNUNET_TEST(test_gnunet_chat_file_send, call_gnunet_chat_file_send) | ||
171 | |||
172 | START_SUITE(handle_suite, "File") | ||
173 | ADD_TEST_TO_SUITE(test_gnunet_chat_file_send, "Send") | ||
174 | END_SUITE | ||
175 | |||
176 | MAIN_SUITE(handle_suite, CK_NORMAL) | ||