diff options
Diffstat (limited to 'src/messenger/test_messenger_anonymous.c')
-rw-r--r-- | src/messenger/test_messenger_anonymous.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/messenger/test_messenger_anonymous.c b/src/messenger/test_messenger_anonymous.c new file mode 100644 index 000000000..e2057acc4 --- /dev/null +++ b/src/messenger/test_messenger_anonymous.c | |||
@@ -0,0 +1,179 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020 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_anonymous.c | ||
22 | * @author Tobias Frisch | ||
23 | * @brief Test for the messenger service using cadet API. | ||
24 | */ | ||
25 | #include <stdio.h> | ||
26 | #include "platform.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 | static int status = 1; | ||
43 | |||
44 | static struct GNUNET_SCHEDULER_Task *die_task = NULL; | ||
45 | static struct GNUNET_SCHEDULER_Task *op_task = NULL; | ||
46 | |||
47 | struct GNUNET_MESSENGER_Handle *messenger = NULL; | ||
48 | |||
49 | static void | ||
50 | end (void *cls) | ||
51 | { | ||
52 | die_task = NULL; | ||
53 | |||
54 | if (op_task) | ||
55 | { | ||
56 | GNUNET_SCHEDULER_cancel (op_task); | ||
57 | op_task = NULL; | ||
58 | } | ||
59 | |||
60 | if (messenger) | ||
61 | { | ||
62 | GNUNET_MESSENGER_disconnect(messenger); | ||
63 | messenger = NULL; | ||
64 | } | ||
65 | |||
66 | status = 0; | ||
67 | } | ||
68 | |||
69 | |||
70 | static void | ||
71 | end_badly (void *cls) | ||
72 | { | ||
73 | fprintf (stderr, "Testcase failed (timeout).\n"); | ||
74 | |||
75 | end (NULL); | ||
76 | status = 1; | ||
77 | } | ||
78 | |||
79 | static void | ||
80 | end_operation (void *cls) | ||
81 | { | ||
82 | op_task = NULL; | ||
83 | |||
84 | fprintf (stderr, "Testcase failed (operation: '%s').\n", cls? (const char*) cls : "unknown"); | ||
85 | |||
86 | if (die_task) | ||
87 | GNUNET_SCHEDULER_cancel (die_task); | ||
88 | |||
89 | end (NULL); | ||
90 | status = 1; | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Function called when an identity is retrieved. | ||
95 | * | ||
96 | * @param cls Closure | ||
97 | * @param handle Handle of messenger service | ||
98 | */ | ||
99 | static void | ||
100 | on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle) | ||
101 | { | ||
102 | if (op_task) | ||
103 | { | ||
104 | GNUNET_SCHEDULER_cancel (op_task); | ||
105 | op_task = NULL; | ||
106 | } | ||
107 | |||
108 | const char* name = GNUNET_MESSENGER_get_name(handle); | ||
109 | |||
110 | if (NULL != name) | ||
111 | { | ||
112 | op_task = GNUNET_SCHEDULER_add_now (&end_operation, "name-anonymous"); | ||
113 | return; | ||
114 | } | ||
115 | |||
116 | if (GNUNET_SYSERR != GNUNET_MESSENGER_update(handle)) | ||
117 | { | ||
118 | op_task = GNUNET_SCHEDULER_add_now (&end_operation, "update-fail"); | ||
119 | return; | ||
120 | } | ||
121 | |||
122 | struct GNUNET_IDENTITY_Ego* ego = GNUNET_IDENTITY_ego_get_anonymous(); | ||
123 | struct GNUNET_IDENTITY_PublicKey anonymous_key; | ||
124 | |||
125 | GNUNET_IDENTITY_ego_get_public_key(ego, &anonymous_key); | ||
126 | |||
127 | const struct GNUNET_IDENTITY_PublicKey* key = GNUNET_MESSENGER_get_key(handle); | ||
128 | |||
129 | if (0 != GNUNET_memcmp(key, (&anonymous_key))) | ||
130 | { | ||
131 | op_task = GNUNET_SCHEDULER_add_now (&end_operation, "key-anonymous"); | ||
132 | return; | ||
133 | } | ||
134 | |||
135 | GNUNET_MESSENGER_disconnect(handle); | ||
136 | |||
137 | messenger = NULL; | ||
138 | |||
139 | if (die_task) | ||
140 | GNUNET_SCHEDULER_cancel (die_task); | ||
141 | |||
142 | die_task = GNUNET_SCHEDULER_add_now (&end, NULL); | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * Main function for testcase. | ||
147 | * | ||
148 | * @param cls Closure | ||
149 | * @param cfg Configuration | ||
150 | * @param peer Peer for testing | ||
151 | */ | ||
152 | static void | ||
153 | run (void *cls, | ||
154 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
155 | struct GNUNET_TESTING_Peer *peer) | ||
156 | { | ||
157 | die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, &end_badly, NULL); | ||
158 | |||
159 | op_task = GNUNET_SCHEDULER_add_delayed (BASE_TIMEOUT, &end_operation, "connect"); | ||
160 | messenger = GNUNET_MESSENGER_connect(cfg, NULL, &on_identity, NULL, NULL, NULL); | ||
161 | } | ||
162 | |||
163 | /** | ||
164 | * The main function. | ||
165 | * | ||
166 | * @param argc number of arguments from the command line | ||
167 | * @param argv command line arguments | ||
168 | * @return 0 ok, 1 on error | ||
169 | */ | ||
170 | int | ||
171 | main(int argc, char **argv) | ||
172 | { | ||
173 | if (0 != GNUNET_TESTING_peer_run("test-messenger", | ||
174 | "test_messenger_api.conf", | ||
175 | &run, NULL)) | ||
176 | return 1; | ||
177 | |||
178 | return status; | ||
179 | } | ||