getting service_new.c to compile, albeit it is not yet complete
[oweals/gnunet.git] / src / util / test_client.c
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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file util/test_client.c
22  * @brief tests for client.c
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27
28 #define PORT 14325
29
30 #define MYNAME "test_client"
31
32 static struct GNUNET_MQ_Handle *mq;
33
34 static struct GNUNET_SERVER_Handle *server;
35
36 static struct GNUNET_CONFIGURATION_Handle *cfg;
37
38 #define MY_TYPE 130
39
40 struct CopyContext
41 {
42   struct GNUNET_SERVER_Client *client;
43   struct GNUNET_MessageHeader *cpy;
44 };
45
46
47 static size_t
48 copy_msg (void *cls, size_t size, void *buf)
49 {
50   struct CopyContext *ctx = cls;
51   struct GNUNET_MessageHeader *cpy = ctx->cpy;
52
53   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
54   GNUNET_assert (size >= ntohs (cpy->size));
55   GNUNET_memcpy (buf, cpy, ntohs (cpy->size));
56   GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
57   GNUNET_free (cpy);
58   GNUNET_free (ctx);
59   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60               "Message bounced back to client\n");
61   return sizeof (struct GNUNET_MessageHeader);
62 }
63
64
65 /**
66  * Callback that just bounces the message back to the sender.
67  */
68 static void
69 echo_cb (void *cls,
70          struct GNUNET_SERVER_Client *client,
71          const struct GNUNET_MessageHeader *message)
72 {
73   struct CopyContext *cc;
74   struct GNUNET_MessageHeader *cpy;
75
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77               "Receiving message from client, bouncing back\n");
78   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
79   cc = GNUNET_new (struct CopyContext);
80   cc->client = client;
81   cpy = GNUNET_malloc (ntohs (message->size));
82   GNUNET_memcpy (cpy, message, ntohs (message->size));
83   cc->cpy = cpy;
84   GNUNET_assert (NULL !=
85                  GNUNET_SERVER_notify_transmit_ready (client,
86                                                       ntohs (message->size),
87                                                       GNUNET_TIME_UNIT_SECONDS,
88                                                       &copy_msg, cc));
89 }
90
91
92 static struct GNUNET_SERVER_MessageHandler handlers[] = {
93   {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
94   {NULL, NULL, 0, 0}
95 };
96
97
98 static void
99 handle_bounce (void *cls,
100                const struct GNUNET_MessageHeader *got)
101 {
102   int *ok = cls;
103
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105               "Receiving bounce, checking content\n");
106   GNUNET_assert (NULL != got);
107   GNUNET_MQ_destroy (mq);
108   mq = NULL;
109   GNUNET_SERVER_destroy (server);
110   server = NULL;
111   *ok = 0;
112 }
113
114
115 /**
116  * Generic error handler, called with the appropriate error code and
117  * the same closure specified at the creation of the message queue.
118  * Not every message queue implementation supports an error handler.
119  *
120  * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
121  * @param error error code
122  */
123 static void
124 mq_error_handler (void *cls,
125                   enum GNUNET_MQ_Error error)
126 {
127   GNUNET_assert (0); /* should never happen */
128 }
129
130
131 static void
132 task (void *cls)
133 {
134   struct sockaddr_in sa;
135   struct sockaddr *sap[2];
136   socklen_t slens[2];
137   struct GNUNET_MQ_Envelope *env;
138   struct GNUNET_MessageHeader *msg;
139   struct GNUNET_MQ_MessageHandler chandlers[] = {
140     GNUNET_MQ_hd_fixed_size (bounce,
141                              MY_TYPE,
142                              struct GNUNET_MessageHeader,
143                              cls),
144     GNUNET_MQ_handler_end ()
145   };
146
147   /* test that ill-configured client fails instantly */
148   GNUNET_assert (NULL == GNUNET_CLIENT_connecT (cfg,
149                                                 "invalid-service",
150                                                 NULL,
151                                                 &mq_error_handler,
152                                                 NULL));
153
154   /* test IPC between client and server */
155   sap[0] = (struct sockaddr *) &sa;
156   slens[0] = sizeof (sa);
157   sap[1] = NULL;
158   slens[1] = 0;
159   memset (&sa, 0, sizeof (sa));
160 #if HAVE_SOCKADDR_IN_SIN_LEN
161   sa.sin_len = sizeof (sa);
162 #endif
163   sa.sin_family = AF_INET;
164   sa.sin_port = htons (PORT);
165   server =
166       GNUNET_SERVER_create (NULL, NULL, sap, slens,
167                             GNUNET_TIME_relative_multiply
168                             (GNUNET_TIME_UNIT_SECONDS, 10), GNUNET_NO);
169   GNUNET_assert (server != NULL);
170   handlers[0].callback_cls = cls;
171   handlers[1].callback_cls = cls;
172   GNUNET_SERVER_add_handlers (server, handlers);
173   mq = GNUNET_CLIENT_connecT (cfg,
174                               MYNAME,
175                               chandlers,
176                               &mq_error_handler,
177                               NULL);
178   GNUNET_assert (NULL != mq);
179   env = GNUNET_MQ_msg (msg,
180                        MY_TYPE);
181   GNUNET_MQ_send (mq,
182                   env);
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   int ok;
190
191   GNUNET_log_setup ("test_client",
192                     "WARNING",
193                     NULL);
194   cfg = GNUNET_CONFIGURATION_create ();
195   GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
196   GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
197   ok = 1;
198   GNUNET_SCHEDULER_run (&task, &ok);
199   GNUNET_CONFIGURATION_destroy (cfg);
200   return ok;
201 }
202
203 /* end of test_client.c */