diff options
Diffstat (limited to 'src/lib/util/test_client.c')
-rw-r--r-- | src/lib/util/test_client.c | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/src/lib/util/test_client.c b/src/lib/util/test_client.c new file mode 100644 index 000000000..64c32f646 --- /dev/null +++ b/src/lib/util/test_client.c | |||
@@ -0,0 +1,197 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009, 2016 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 util/test_client.c | ||
22 | * @brief tests for client.c | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | |||
29 | static int global_ret; | ||
30 | |||
31 | static struct GNUNET_MQ_Handle *client_mq; | ||
32 | |||
33 | #define MY_TYPE 130 | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Callback that just bounces the message back to the sender. | ||
38 | */ | ||
39 | static void | ||
40 | handle_echo (void *cls, | ||
41 | const struct GNUNET_MessageHeader *message) | ||
42 | { | ||
43 | struct GNUNET_SERVICE_Client *c = cls; | ||
44 | struct GNUNET_MQ_Handle *mq = GNUNET_SERVICE_client_get_mq (c); | ||
45 | struct GNUNET_MQ_Envelope *env; | ||
46 | |||
47 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
48 | "Receiving message from client, bouncing back\n"); | ||
49 | env = GNUNET_MQ_msg_copy (message); | ||
50 | GNUNET_MQ_send (mq, | ||
51 | env); | ||
52 | GNUNET_SERVICE_client_continue (c); | ||
53 | } | ||
54 | |||
55 | |||
56 | static void | ||
57 | handle_bounce (void *cls, | ||
58 | const struct GNUNET_MessageHeader *got) | ||
59 | { | ||
60 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
61 | "Receiving bounce, checking content\n"); | ||
62 | GNUNET_assert (NULL != got); | ||
63 | global_ret = 2; | ||
64 | GNUNET_MQ_destroy (client_mq); | ||
65 | client_mq = NULL; | ||
66 | } | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Generic error handler, called with the appropriate error code and | ||
71 | * the same closure specified at the creation of the message queue. | ||
72 | * Not every message queue implementation supports an error handler. | ||
73 | * | ||
74 | * @param cls closure with the `struct GNUNET_STATISTICS_Handle *` | ||
75 | * @param error error code | ||
76 | */ | ||
77 | static void | ||
78 | mq_error_handler (void *cls, | ||
79 | enum GNUNET_MQ_Error error) | ||
80 | { | ||
81 | GNUNET_assert (0); /* should never happen */ | ||
82 | } | ||
83 | |||
84 | |||
85 | static void | ||
86 | task (void *cls, | ||
87 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
88 | struct GNUNET_SERVICE_Handle *sh) | ||
89 | { | ||
90 | struct GNUNET_MQ_MessageHandler chandlers[] = { | ||
91 | GNUNET_MQ_hd_fixed_size (bounce, | ||
92 | MY_TYPE, | ||
93 | struct GNUNET_MessageHeader, | ||
94 | cls), | ||
95 | GNUNET_MQ_handler_end () | ||
96 | }; | ||
97 | struct GNUNET_MQ_Envelope *env; | ||
98 | struct GNUNET_MessageHeader *msg; | ||
99 | |||
100 | /* test that ill-configured client fails instantly */ | ||
101 | GNUNET_assert (NULL == | ||
102 | GNUNET_CLIENT_connect (cfg, | ||
103 | "invalid-service", | ||
104 | NULL, | ||
105 | &mq_error_handler, | ||
106 | NULL)); | ||
107 | client_mq = GNUNET_CLIENT_connect (cfg, | ||
108 | "test_client", | ||
109 | chandlers, | ||
110 | &mq_error_handler, | ||
111 | NULL); | ||
112 | GNUNET_assert (NULL != client_mq); | ||
113 | env = GNUNET_MQ_msg (msg, | ||
114 | MY_TYPE); | ||
115 | GNUNET_MQ_send (client_mq, | ||
116 | env); | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Function called when the client connects to the service. | ||
122 | * | ||
123 | * @param cls the name of the service | ||
124 | * @param c connecting client | ||
125 | * @param mq message queue to talk to the client | ||
126 | * @return @a c | ||
127 | */ | ||
128 | static void * | ||
129 | connect_cb (void *cls, | ||
130 | struct GNUNET_SERVICE_Client *c, | ||
131 | struct GNUNET_MQ_Handle *mq) | ||
132 | { | ||
133 | return c; | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Function called when the client disconnects. | ||
139 | * | ||
140 | * @param cls our service name | ||
141 | * @param c disconnecting client | ||
142 | * @param internal_cls must match @a c | ||
143 | */ | ||
144 | static void | ||
145 | disconnect_cb (void *cls, | ||
146 | struct GNUNET_SERVICE_Client *c, | ||
147 | void *internal_cls) | ||
148 | { | ||
149 | if (2 == global_ret) | ||
150 | { | ||
151 | GNUNET_SCHEDULER_shutdown (); | ||
152 | global_ret = 0; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | |||
157 | int | ||
158 | main (int argc, | ||
159 | char *argv[]) | ||
160 | { | ||
161 | struct GNUNET_MQ_MessageHandler shandlers[] = { | ||
162 | GNUNET_MQ_hd_fixed_size (echo, | ||
163 | MY_TYPE, | ||
164 | struct GNUNET_MessageHeader, | ||
165 | NULL), | ||
166 | GNUNET_MQ_handler_end () | ||
167 | }; | ||
168 | char *test_argv[] = { | ||
169 | (char *) "test_client", | ||
170 | "-c", | ||
171 | "test_client_data.conf", | ||
172 | NULL | ||
173 | }; | ||
174 | |||
175 | GNUNET_log_setup ("test_client", | ||
176 | "WARNING", | ||
177 | NULL); | ||
178 | if (0 != strstr (argv[0], | ||
179 | "unix")) | ||
180 | test_argv[2] = "test_client_unix.conf"; | ||
181 | global_ret = 1; | ||
182 | if (0 != | ||
183 | GNUNET_SERVICE_run_ (3, | ||
184 | test_argv, | ||
185 | "test_client", | ||
186 | GNUNET_SERVICE_OPTION_NONE, | ||
187 | &task, | ||
188 | &connect_cb, | ||
189 | &disconnect_cb, | ||
190 | NULL, | ||
191 | shandlers)) | ||
192 | global_ret = 3; | ||
193 | return global_ret; | ||
194 | } | ||
195 | |||
196 | |||
197 | /* end of test_client.c */ | ||