diff options
Diffstat (limited to 'src/service/messenger/test_messenger.c')
-rw-r--r-- | src/service/messenger/test_messenger.c | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/src/service/messenger/test_messenger.c b/src/service/messenger/test_messenger.c new file mode 100644 index 000000000..4f7a51e34 --- /dev/null +++ b/src/service/messenger/test_messenger.c | |||
@@ -0,0 +1,218 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020--2023 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 | * @file messenger/test_messenger.c | ||
22 | * @author Tobias Frisch | ||
23 | * @brief Test for the messenger service using cadet API. | ||
24 | */ | ||
25 | |||
26 | #include <stdio.h> | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_lib.h" | ||
29 | #include "gnunet_messenger_service.h" | ||
30 | |||
31 | /** | ||
32 | * How long until we really give up on a particular testcase portion? | ||
33 | */ | ||
34 | #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \ | ||
35 | 60) | ||
36 | |||
37 | /** | ||
38 | * How long until we give up on any particular operation (and retry)? | ||
39 | */ | ||
40 | #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
41 | |||
42 | #define TESTER_NAME "tester" | ||
43 | |||
44 | static int status = 1; | ||
45 | |||
46 | static struct GNUNET_SCHEDULER_Task *die_task = NULL; | ||
47 | static struct GNUNET_SCHEDULER_Task *op_task = NULL; | ||
48 | static struct GNUNET_SCHEDULER_Task *it_task = NULL; | ||
49 | |||
50 | struct GNUNET_MESSENGER_Handle *messenger = NULL; | ||
51 | |||
52 | static struct GNUNET_CRYPTO_PrivateKey identity; | ||
53 | |||
54 | static void | ||
55 | end (void *cls) | ||
56 | { | ||
57 | die_task = NULL; | ||
58 | |||
59 | if (it_task) | ||
60 | { | ||
61 | GNUNET_SCHEDULER_cancel (it_task); | ||
62 | it_task = NULL; | ||
63 | } | ||
64 | |||
65 | if (op_task) | ||
66 | { | ||
67 | GNUNET_SCHEDULER_cancel (op_task); | ||
68 | op_task = NULL; | ||
69 | } | ||
70 | |||
71 | if (messenger) | ||
72 | { | ||
73 | GNUNET_MESSENGER_disconnect (messenger); | ||
74 | messenger = NULL; | ||
75 | } | ||
76 | |||
77 | GNUNET_CRYPTO_private_key_clear (&identity); | ||
78 | status = 0; | ||
79 | } | ||
80 | |||
81 | |||
82 | static void | ||
83 | end_badly (void *cls) | ||
84 | { | ||
85 | fprintf (stderr, "Testcase failed (timeout).\n"); | ||
86 | |||
87 | end (NULL); | ||
88 | status = 1; | ||
89 | } | ||
90 | |||
91 | |||
92 | static void | ||
93 | end_operation (void *cls) | ||
94 | { | ||
95 | op_task = NULL; | ||
96 | |||
97 | fprintf (stderr, "Testcase failed (operation: '%s').\n", cls ? (const | ||
98 | char*) cls : | ||
99 | "unknown"); | ||
100 | |||
101 | if (die_task) | ||
102 | GNUNET_SCHEDULER_cancel (die_task); | ||
103 | |||
104 | end (NULL); | ||
105 | status = 1; | ||
106 | } | ||
107 | |||
108 | |||
109 | static int identity_counter = 0; | ||
110 | |||
111 | /** | ||
112 | * Function called when an identity is retrieved. | ||
113 | * | ||
114 | * @param cls Closure | ||
115 | * @param handle Handle of messenger service | ||
116 | */ | ||
117 | static void | ||
118 | on_iteration (void *cls) | ||
119 | { | ||
120 | struct GNUNET_MESSENGER_Handle *handle = cls; | ||
121 | it_task = NULL; | ||
122 | |||
123 | if (op_task) | ||
124 | { | ||
125 | GNUNET_SCHEDULER_cancel (op_task); | ||
126 | op_task = NULL; | ||
127 | } | ||
128 | |||
129 | const char *name = GNUNET_MESSENGER_get_name (handle); | ||
130 | |||
131 | if ((! name) || (0 != strcmp (name, TESTER_NAME))) | ||
132 | { | ||
133 | op_task = GNUNET_SCHEDULER_add_now (&end_operation, "name"); | ||
134 | return; | ||
135 | } | ||
136 | |||
137 | const struct GNUNET_CRYPTO_PublicKey *key = GNUNET_MESSENGER_get_key ( | ||
138 | handle); | ||
139 | |||
140 | struct GNUNET_CRYPTO_PublicKey pubkey; | ||
141 | GNUNET_CRYPTO_key_get_public (&identity, &pubkey); | ||
142 | |||
143 | if (((! identity_counter) && (key)) || ((identity_counter) && ((! key) || | ||
144 | (0 != | ||
145 | GNUNET_memcmp ( | ||
146 | key, | ||
147 | &pubkey))))) | ||
148 | { | ||
149 | op_task = GNUNET_SCHEDULER_add_now (&end_operation, "key"); | ||
150 | return; | ||
151 | } | ||
152 | |||
153 | if (identity_counter) | ||
154 | { | ||
155 | GNUNET_MESSENGER_disconnect (handle); | ||
156 | |||
157 | op_task = NULL; | ||
158 | messenger = NULL; | ||
159 | |||
160 | if (die_task) | ||
161 | GNUNET_SCHEDULER_cancel (die_task); | ||
162 | |||
163 | die_task = GNUNET_SCHEDULER_add_now (&end, NULL); | ||
164 | return; | ||
165 | } | ||
166 | |||
167 | GNUNET_MESSENGER_set_key (handle, &identity); | ||
168 | identity_counter++; | ||
169 | |||
170 | it_task = GNUNET_SCHEDULER_add_now (&on_iteration, handle); | ||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * Main function for testcase. | ||
176 | * | ||
177 | * @param cls Closure | ||
178 | * @param cfg Configuration | ||
179 | * @param peer Peer for testing | ||
180 | */ | ||
181 | static void | ||
182 | run (void *cls, | ||
183 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
184 | struct GNUNET_TESTING_Peer *peer) | ||
185 | { | ||
186 | die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, &end_badly, NULL); | ||
187 | |||
188 | identity_counter = 0; | ||
189 | |||
190 | op_task = GNUNET_SCHEDULER_add_delayed (BASE_TIMEOUT, &end_operation, | ||
191 | "connect"); | ||
192 | messenger = GNUNET_MESSENGER_connect (cfg, TESTER_NAME, NULL, NULL, NULL); | ||
193 | |||
194 | identity.type = htonl (GNUNET_PUBLIC_KEY_TYPE_ECDSA); | ||
195 | GNUNET_CRYPTO_ecdsa_key_create (&(identity.ecdsa_key)); | ||
196 | |||
197 | if (messenger) | ||
198 | it_task = GNUNET_SCHEDULER_add_now (&on_iteration, messenger); | ||
199 | } | ||
200 | |||
201 | |||
202 | /** | ||
203 | * The main function. | ||
204 | * | ||
205 | * @param argc number of arguments from the command line | ||
206 | * @param argv command line arguments | ||
207 | * @return 0 ok, 1 on error | ||
208 | */ | ||
209 | int | ||
210 | main (int argc, | ||
211 | char **argv) | ||
212 | { | ||
213 | if (0 != GNUNET_TESTING_peer_run ("test-messenger", "test_messenger_api.conf", | ||
214 | &run, NULL)) | ||
215 | return 1; | ||
216 | |||
217 | return status; | ||
218 | } | ||