reject '@' as a label in domain names, this is usually a mistake
[oweals/gnunet.git] / src / util / test_service.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2013, 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_service.c
22  * @brief tests for service.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 /**
29  * Message type we use for testing.
30  */
31 #define MY_TYPE 256
32
33 #define TIMEOUT GNUNET_TIME_UNIT_SECONDS
34
35 static int global_ret = 1;
36
37 static struct GNUNET_MQ_Handle *mq;
38
39 /**
40  * Timeout task.
41  */
42 static struct GNUNET_SCHEDULER_Task *tt;
43
44
45 static void
46 handle_recv (void *cls,
47              const struct GNUNET_MessageHeader *message)
48 {
49   struct GNUNET_SERVICE_Client *client = cls;
50   
51   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
52               "Received client message...\n");
53   GNUNET_SERVICE_client_continue (client);
54   global_ret = 2;
55   if (NULL != mq)
56   {
57     GNUNET_MQ_destroy (mq);
58     mq = NULL;
59   }
60 }
61
62
63 /**
64  * Function called when the client connects to the service.
65  *
66  * @param cls the name of the service
67  * @param c connecting client
68  * @param mq message queue to talk to the client
69  * @return @a c so we have the client handle in the future
70  */
71 static void *
72 connect_cb (void *cls,
73             struct GNUNET_SERVICE_Client *c,
74             struct GNUNET_MQ_Handle *mq)
75 {
76   /* FIXME: in the future, do something with mq
77      to test sending messages to the client! */
78   return c;
79 }
80
81
82 /**
83  * Function called when the client disconnects.
84  *
85  * @param cls our service name
86  * @param c disconnecting client
87  * @param internal_cls must match @a c
88  */ 
89 static void
90 disconnect_cb (void *cls,
91                struct GNUNET_SERVICE_Client *c,
92                void *internal_cls)
93 {
94   GNUNET_assert (c == internal_cls);
95   if (2 == global_ret)
96   {
97     GNUNET_SCHEDULER_shutdown ();
98     global_ret = 0;
99     if (NULL != tt)
100     {
101       GNUNET_SCHEDULER_cancel (tt);
102       tt = NULL;
103     }
104   }
105 }
106
107
108 static void
109 timeout_task (void *cls)
110 {
111   tt = NULL;
112   if (NULL != mq)
113   {
114     GNUNET_MQ_destroy (mq);
115     mq = NULL;
116   }
117   global_ret = 33;
118   GNUNET_SCHEDULER_shutdown ();
119 }
120
121
122 /**
123  * Initialization function of the service.  Starts
124  * a client to connect to the service.
125  *
126  * @param cls the name of the service (const char *)
127  * @param cfg the configuration we use
128  * @param sh handle to the service
129  */
130 static void
131 service_init (void *cls,
132               const struct GNUNET_CONFIGURATION_Handle *cfg,
133               struct GNUNET_SERVICE_Handle *sh)
134 {
135   const char *service_name = cls;
136   struct GNUNET_MQ_Envelope *env;
137   struct GNUNET_MessageHeader *msg;
138
139   GNUNET_assert (NULL == tt);
140   tt = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
141                                      &timeout_task,
142                                      NULL);
143   mq = GNUNET_CLIENT_connect (cfg,
144                               service_name,
145                               NULL,
146                               NULL,
147                               NULL);
148   GNUNET_assert (NULL != mq);
149   env = GNUNET_MQ_msg (msg,
150                        MY_TYPE);
151   GNUNET_MQ_send (mq,
152                   env);
153 }
154
155
156 /**
157  * Main method, starts the service and initiates
158  * the running of the test.
159  *
160  * @param sname name of the service to run
161  */
162 static int
163 check (const char *sname)
164 {
165   struct GNUNET_MQ_MessageHandler myhandlers[] = {
166     GNUNET_MQ_hd_fixed_size (recv,
167                              MY_TYPE,
168                              struct GNUNET_MessageHeader,
169                              NULL),
170     GNUNET_MQ_handler_end ()
171   };
172   char *const argv[] = {
173     (char *) sname,
174     "-c",
175     "test_service_data.conf",
176     NULL
177   };
178   
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180               "Starting `%s' service\n",
181               sname);
182   global_ret = 1;
183   GNUNET_assert (0 ==
184                  GNUNET_SERVICE_run_ (3,
185                                       argv,
186                                       sname,
187                                       GNUNET_SERVICE_OPTION_NONE,
188                                       &service_init,
189                                       &connect_cb,
190                                       &disconnect_cb,
191                                       (void *) sname,
192                                       myhandlers));
193   return global_ret;
194 }
195
196
197 int
198 main (int argc,
199       char *argv[])
200 {
201   int ret = 0;
202   struct GNUNET_NETWORK_Handle *s = NULL;
203
204   GNUNET_log_setup ("test-service",
205                     "WARNING",
206                     NULL);
207   ret += check ("test_service");
208   ret += check ("test_service");
209 #ifndef MINGW
210   s = GNUNET_NETWORK_socket_create (PF_INET6,
211                                     SOCK_STREAM,
212                                     0);
213 #endif
214   if (NULL == s)
215   {
216     if ( (errno == ENOBUFS) ||
217          (errno == ENOMEM) ||
218          (errno == ENFILE) ||
219          (errno == EACCES) )
220     {
221       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
222                            "socket");
223       return 1;
224     }
225     FPRINTF (stderr,
226              "IPv6 support seems to not be available (%s), not testing it!\n",
227              strerror (errno));
228   }
229   else
230   {
231     GNUNET_break (GNUNET_OK ==
232                   GNUNET_NETWORK_socket_close (s));
233     ret += check ("test_service6");
234   }
235   return ret;
236 }
237
238 /* end of test_service.c */