diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-18 21:28:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-18 21:28:49 +0000 |
commit | 9f649cbc9454317228c4a4d3580de3a8f8f008a8 (patch) | |
tree | 4b756b9524ab5e735029e84c65a708c7a040daf3 /src/util/test_service.c | |
parent | af6f9ae173e641a15639b59238bd5e86113a9113 (diff) | |
download | gnunet-9f649cbc9454317228c4a4d3580de3a8f8f008a8.tar.gz gnunet-9f649cbc9454317228c4a4d3580de3a8f8f008a8.zip |
adapting test_service to new service API, fixing misc issues found by test
Diffstat (limited to 'src/util/test_service.c')
-rw-r--r-- | src/util/test_service.c | 271 |
1 files changed, 97 insertions, 174 deletions
diff --git a/src/util/test_service.c b/src/util/test_service.c index 707f8658b..dc9d16ed9 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c | |||
@@ -25,134 +25,90 @@ | |||
25 | #include "platform.h" | 25 | #include "platform.h" |
26 | #include "gnunet_util_lib.h" | 26 | #include "gnunet_util_lib.h" |
27 | 27 | ||
28 | |||
29 | #define PORT 12435 | ||
30 | |||
31 | /** | 28 | /** |
32 | * Message type we use for testing. | 29 | * Message type we use for testing. |
33 | */ | 30 | */ |
34 | #define MY_TYPE 256 | 31 | #define MY_TYPE 256 |
35 | 32 | ||
36 | static struct GNUNET_SERVICE_Context *sctx; | 33 | static int global_ret = 1; |
37 | |||
38 | static int ok = 1; | ||
39 | 34 | ||
40 | static struct GNUNET_MQ_Handle *mq; | 35 | static struct GNUNET_MQ_Handle *mq; |
41 | 36 | ||
42 | 37 | ||
43 | static void | 38 | static void |
44 | do_stop (void *cls) | 39 | handle_recv (void *cls, |
45 | { | 40 | const struct GNUNET_MessageHeader *message) |
46 | if (NULL != mq) | ||
47 | { | ||
48 | GNUNET_MQ_destroy (mq); | ||
49 | mq = NULL; | ||
50 | } | ||
51 | if (NULL != sctx) | ||
52 | { | ||
53 | GNUNET_SERVICE_stop (sctx); | ||
54 | sctx = NULL; | ||
55 | } | ||
56 | else | ||
57 | { | ||
58 | GNUNET_SCHEDULER_shutdown (); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | |||
63 | static void | ||
64 | ready (void *cls, | ||
65 | int result) | ||
66 | { | 41 | { |
67 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 42 | struct GNUNET_SERVICE_Client *client = cls; |
68 | struct GNUNET_MQ_Envelope *env; | 43 | |
69 | struct GNUNET_MessageHeader *msg; | ||
70 | |||
71 | GNUNET_assert (GNUNET_YES == result); | ||
72 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 44 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
73 | "Service confirmed running\n"); | 45 | "Received client message...\n"); |
74 | mq = GNUNET_CLIENT_connecT (cfg, | 46 | GNUNET_SERVICE_client_continue (client); |
75 | "test_service", | 47 | global_ret = 2; |
76 | NULL, | 48 | GNUNET_MQ_destroy (mq); |
77 | NULL, | 49 | mq = NULL; |
78 | NULL); | ||
79 | GNUNET_assert (NULL != mq); | ||
80 | env = GNUNET_MQ_msg (msg, | ||
81 | MY_TYPE); | ||
82 | GNUNET_MQ_send (mq, | ||
83 | env); | ||
84 | } | 50 | } |
85 | 51 | ||
86 | 52 | ||
87 | static void | 53 | /** |
88 | recv_cb (void *cls, | 54 | * Function called when the client connects to the service. |
89 | struct GNUNET_SERVER_Client *sc, | 55 | * |
90 | const struct GNUNET_MessageHeader *message) | 56 | * @param cls the name of the service |
57 | * @param c connecting client | ||
58 | * @param mq message queue to talk to the client | ||
59 | * @return @a c so we have the client handle in the future | ||
60 | */ | ||
61 | static void * | ||
62 | connect_cb (void *cls, | ||
63 | struct GNUNET_SERVICE_Client *c, | ||
64 | struct GNUNET_MQ_Handle *mq) | ||
91 | { | 65 | { |
92 | GNUNET_assert (NULL != message); | 66 | /* FIXME: in the future, do something with mq |
93 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n"); | 67 | to test sending messages to the client! */ |
94 | GNUNET_SERVER_receive_done (sc, GNUNET_OK); | 68 | return c; |
95 | GNUNET_SCHEDULER_add_now (&do_stop, NULL); | ||
96 | ok = 0; | ||
97 | } | 69 | } |
98 | 70 | ||
99 | 71 | ||
100 | static struct GNUNET_SERVER_MessageHandler myhandlers[] = { | 72 | /** |
101 | {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)}, | 73 | * Function called when the client disconnects. |
102 | {NULL, NULL, 0, 0} | 74 | * |
103 | }; | 75 | * @param cls our service name |
104 | 76 | * @param c disconnecting client | |
105 | 77 | * @param internal_cls must match @a c | |
78 | */ | ||
106 | static void | 79 | static void |
107 | runner (void *cls, | 80 | disconnect_cb (void *cls, |
108 | struct GNUNET_SERVER_Handle *server, | 81 | struct GNUNET_SERVICE_Client *c, |
109 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 82 | void *internal_cls) |
110 | { | 83 | { |
111 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 84 | GNUNET_assert (c == internal_cls); |
112 | "Service initializing\n"); | 85 | if (2 == global_ret) |
113 | GNUNET_SERVER_add_handlers (server, | 86 | { |
114 | myhandlers); | 87 | GNUNET_SCHEDULER_shutdown (); |
115 | GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS, | 88 | global_ret = 0; |
116 | &ready, (void *) cfg); | 89 | } |
117 | } | 90 | } |
118 | 91 | ||
119 | 92 | ||
120 | /** | 93 | /** |
121 | * Main method, starts scheduler with task1, | 94 | * Initialization function of the service. Starts |
122 | * checks that "ok" is correct at the end. | 95 | * a client to connect to the service. |
96 | * | ||
97 | * @param cls the name of the service (const char *) | ||
98 | * @param cfg the configuration we use | ||
99 | * @param sh handle to the service | ||
123 | */ | 100 | */ |
124 | static int | ||
125 | check () | ||
126 | { | ||
127 | ok = 1; | ||
128 | char *const argv[] = { | ||
129 | "test_service", | ||
130 | "-c", | ||
131 | "test_service_data.conf", | ||
132 | NULL | ||
133 | }; | ||
134 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n"); | ||
135 | GNUNET_assert (GNUNET_OK == | ||
136 | GNUNET_SERVICE_run (3, argv, "test_service", | ||
137 | GNUNET_SERVICE_OPTION_NONE, &runner, &ok)); | ||
138 | GNUNET_assert (0 == ok); | ||
139 | return ok; | ||
140 | } | ||
141 | |||
142 | |||
143 | static void | 101 | static void |
144 | ready6 (void *cls, | 102 | service_init (void *cls, |
145 | int result) | 103 | const struct GNUNET_CONFIGURATION_Handle *cfg, |
104 | struct GNUNET_SERVICE_Handle *sh) | ||
146 | { | 105 | { |
147 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 106 | const char *service_name = cls; |
148 | struct GNUNET_MQ_Envelope *env; | 107 | struct GNUNET_MQ_Envelope *env; |
149 | struct GNUNET_MessageHeader *msg; | 108 | struct GNUNET_MessageHeader *msg; |
150 | 109 | ||
151 | GNUNET_assert (GNUNET_YES == result); | ||
152 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
153 | "V6 ready\n"); | ||
154 | mq = GNUNET_CLIENT_connecT (cfg, | 110 | mq = GNUNET_CLIENT_connecT (cfg, |
155 | "test_service6", | 111 | service_name, |
156 | NULL, | 112 | NULL, |
157 | NULL, | 113 | NULL, |
158 | NULL); | 114 | NULL); |
@@ -164,87 +120,50 @@ ready6 (void *cls, | |||
164 | } | 120 | } |
165 | 121 | ||
166 | 122 | ||
167 | static void | ||
168 | runner6 (void *cls, | ||
169 | struct GNUNET_SERVER_Handle *server, | ||
170 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
171 | { | ||
172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
173 | "Initializing v6 service\n"); | ||
174 | GNUNET_SERVER_add_handlers (server, | ||
175 | myhandlers); | ||
176 | GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS, | ||
177 | &ready6, (void *) cfg); | ||
178 | } | ||
179 | |||
180 | |||
181 | /** | 123 | /** |
182 | * Main method, starts scheduler with task1, | 124 | * Main method, starts the service and initiates |
183 | * checks that "ok" is correct at the end. | 125 | * the running of the test. |
126 | * | ||
127 | * @param sname name of the service to run | ||
184 | */ | 128 | */ |
185 | static int | 129 | static int |
186 | check6 () | 130 | check (const char *sname) |
187 | { | 131 | { |
188 | char *const argv[] = { | 132 | struct GNUNET_MQ_MessageHandler myhandlers[] = { |
189 | "test_service6", | 133 | GNUNET_MQ_hd_fixed_size (recv, |
190 | "-c", | 134 | MY_TYPE, |
191 | "test_service_data.conf", | 135 | struct GNUNET_MessageHeader, |
192 | NULL | 136 | NULL), |
137 | GNUNET_MQ_handler_end () | ||
193 | }; | 138 | }; |
194 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n"); | ||
195 | GNUNET_assert (GNUNET_OK == | ||
196 | GNUNET_SERVICE_run (3, argv, "test_service6", | ||
197 | GNUNET_SERVICE_OPTION_NONE, &runner6, | ||
198 | &ok)); | ||
199 | GNUNET_assert (0 == ok); | ||
200 | return ok; | ||
201 | } | ||
202 | |||
203 | |||
204 | static void | ||
205 | start_stop_main (void *cls, | ||
206 | char *const *args, | ||
207 | const char *cfgfile, | ||
208 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
209 | { | ||
210 | int *ret = cls; | ||
211 | |||
212 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
213 | "Starting service using start method\n"); | ||
214 | sctx = GNUNET_SERVICE_start ("test_service", | ||
215 | cfg, | ||
216 | GNUNET_SERVICE_OPTION_NONE); | ||
217 | GNUNET_assert (NULL != sctx); | ||
218 | runner (cls, GNUNET_SERVICE_get_server (sctx), cfg); | ||
219 | *ret = 0; | ||
220 | } | ||
221 | |||
222 | |||
223 | static int | ||
224 | check_start_stop () | ||
225 | { | ||
226 | char *const argv[] = { | 139 | char *const argv[] = { |
227 | "test-service-program", | 140 | (char *) sname, |
228 | "-c", | 141 | "-c", |
229 | "test_service_data.conf", | 142 | "test_service_data.conf", |
230 | NULL | 143 | NULL |
231 | }; | 144 | }; |
232 | const struct GNUNET_GETOPT_CommandLineOption options[] = { | 145 | |
233 | GNUNET_GETOPT_OPTION_END | 146 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
234 | }; | 147 | "Starting `%s' service\n", |
235 | int ret = 1; | 148 | sname); |
236 | 149 | global_ret = 1; | |
237 | GNUNET_assert (GNUNET_OK == | 150 | GNUNET_assert (0 == |
238 | GNUNET_PROGRAM_run (3, argv, "test-service-program", "no help", | 151 | GNUNET_SERVICE_ruN_ (3, |
239 | options, &start_stop_main, &ret)); | 152 | argv, |
240 | 153 | sname, | |
241 | GNUNET_break (0 == ret); | 154 | GNUNET_SERVICE_OPTION_NONE, |
242 | return ret; | 155 | &service_init, |
156 | &connect_cb, | ||
157 | &disconnect_cb, | ||
158 | (void *) sname, | ||
159 | myhandlers)); | ||
160 | return global_ret; | ||
243 | } | 161 | } |
244 | 162 | ||
245 | 163 | ||
246 | int | 164 | int |
247 | main (int argc, char *argv[]) | 165 | main (int argc, |
166 | char *argv[]) | ||
248 | { | 167 | { |
249 | int ret = 0; | 168 | int ret = 0; |
250 | struct GNUNET_NETWORK_Handle *s = NULL; | 169 | struct GNUNET_NETWORK_Handle *s = NULL; |
@@ -252,18 +171,22 @@ main (int argc, char *argv[]) | |||
252 | GNUNET_log_setup ("test-service", | 171 | GNUNET_log_setup ("test-service", |
253 | "WARNING", | 172 | "WARNING", |
254 | NULL); | 173 | NULL); |
255 | ret += check (); | 174 | ret += check ("test_service"); |
256 | ret += check (); | 175 | ret += check ("test_service"); |
257 | // FIXME | ||
258 | #ifndef MINGW | 176 | #ifndef MINGW |
259 | s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); | 177 | s = GNUNET_NETWORK_socket_create (PF_INET6, |
178 | SOCK_STREAM, | ||
179 | 0); | ||
260 | #endif | 180 | #endif |
261 | if (NULL == s) | 181 | if (NULL == s) |
262 | { | 182 | { |
263 | if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) || | 183 | if ( (errno == ENOBUFS) || |
264 | (errno == EACCES)) | 184 | (errno == ENOMEM) || |
185 | (errno == ENFILE) || | ||
186 | (errno == EACCES) ) | ||
265 | { | 187 | { |
266 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | 188 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, |
189 | "socket"); | ||
267 | return 1; | 190 | return 1; |
268 | } | 191 | } |
269 | FPRINTF (stderr, | 192 | FPRINTF (stderr, |
@@ -272,10 +195,10 @@ main (int argc, char *argv[]) | |||
272 | } | 195 | } |
273 | else | 196 | else |
274 | { | 197 | { |
275 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); | 198 | GNUNET_break (GNUNET_OK == |
276 | ret += check6 (); | 199 | GNUNET_NETWORK_socket_close (s)); |
200 | ret += check ("test_service6"); | ||
277 | } | 201 | } |
278 | ret += check_start_stop (); | ||
279 | return ret; | 202 | return ret; |
280 | } | 203 | } |
281 | 204 | ||