curly wars / auto-indentation
[oweals/gnunet.git] / src / util / test_client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/test_client.c
22  * @brief tests for client.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_configuration_lib.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_time_lib.h"
31
32 #define VERBOSE GNUNET_EXTRA_LOGGING
33
34 #define PORT 14325
35
36 #define MYNAME "test_client"
37
38 static struct GNUNET_CLIENT_Connection *client;
39
40 static struct GNUNET_SERVER_Handle *server;
41
42 static struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 #define MY_TYPE 130
45
46 struct CopyContext
47 {
48   struct GNUNET_SERVER_Client *client;
49   struct GNUNET_MessageHeader *cpy;
50 };
51
52 static size_t
53 copy_msg (void *cls, size_t size, void *buf)
54 {
55   struct CopyContext *ctx = cls;
56   struct GNUNET_MessageHeader *cpy = ctx->cpy;
57
58   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
59   GNUNET_assert (size >= ntohs (cpy->size));
60   memcpy (buf, cpy, ntohs (cpy->size));
61   GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
62   GNUNET_free (cpy);
63   GNUNET_free (ctx);
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
65   return sizeof (struct GNUNET_MessageHeader);
66 }
67
68
69 /**
70  * Callback that just bounces the message back to the sender.
71  */
72 static void
73 echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
74          const struct GNUNET_MessageHeader *message)
75 {
76   struct CopyContext *cc;
77   struct GNUNET_MessageHeader *cpy;
78
79   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80               "Receiving message from client, bouncing back\n");
81   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
82   cc = GNUNET_malloc (sizeof (struct CopyContext));
83   cc->client = client;
84   cpy = GNUNET_malloc (ntohs (message->size));
85   memcpy (cpy, message, ntohs (message->size));
86   cc->cpy = cpy;
87   GNUNET_assert (NULL !=
88                  GNUNET_SERVER_notify_transmit_ready (client,
89                                                       ntohs (message->size),
90                                                       GNUNET_TIME_UNIT_SECONDS,
91                                                       &copy_msg, cc));
92 }
93
94
95 static struct GNUNET_SERVER_MessageHandler handlers[] = {
96   {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
97   {NULL, NULL, 0, 0}
98 };
99
100
101 static void
102 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
103 {
104   int *ok = cls;
105   struct GNUNET_MessageHeader msg;
106
107   GNUNET_assert (got != NULL);  /* timeout */
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n");
109   msg.type = htons (MY_TYPE);
110   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
111   GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
112   GNUNET_CLIENT_disconnect (client, GNUNET_YES);
113   client = NULL;
114   GNUNET_SERVER_destroy (server);
115   server = NULL;
116   *ok = 0;
117 }
118
119
120 static size_t
121 make_msg (void *cls, size_t size, void *buf)
122 {
123   struct GNUNET_MessageHeader *msg = buf;
124
125   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
126   msg->type = htons (MY_TYPE);
127   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
129   return sizeof (struct GNUNET_MessageHeader);
130 }
131
132
133 static void
134 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   struct sockaddr_in sa;
137   struct sockaddr *sap[2];
138   socklen_t slens[2];
139
140   sap[0] = (struct sockaddr *) &sa;
141   slens[0] = sizeof (sa);
142   sap[1] = NULL;
143   slens[1] = 0;
144   memset (&sa, 0, sizeof (sa));
145 #if HAVE_SOCKADDR_IN_SIN_LEN
146   sa.sin_len = sizeof (sa);
147 #endif
148   sa.sin_family = AF_INET;
149   sa.sin_port = htons (PORT);
150   server =
151       GNUNET_SERVER_create (NULL, NULL, sap, slens,
152                             GNUNET_TIME_relative_multiply
153                             (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
154   GNUNET_assert (server != NULL);
155   handlers[0].callback_cls = cls;
156   handlers[1].callback_cls = cls;
157   GNUNET_SERVER_add_handlers (server, handlers);
158   client = GNUNET_CLIENT_connect (MYNAME, cfg);
159   GNUNET_assert (client != NULL);
160   GNUNET_assert (NULL !=
161                  GNUNET_CLIENT_notify_transmit_ready (client,
162                                                       sizeof (struct
163                                                               GNUNET_MessageHeader),
164                                                       GNUNET_TIME_UNIT_SECONDS,
165                                                       GNUNET_NO, &make_msg,
166                                                       NULL));
167   GNUNET_CLIENT_receive (client, &recv_bounce, cls,
168                          GNUNET_TIME_relative_multiply
169                          (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
170 }
171
172
173 /**
174  * Main method, starts scheduler with task1,
175  * checks that "ok" is correct at the end.
176  */
177 static int
178 check ()
179 {
180   int ok;
181
182   cfg = GNUNET_CONFIGURATION_create ();
183   GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
184   GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
185   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
186                                          "localhost");
187   ok = 1;
188   GNUNET_SCHEDULER_run (&task, &ok);
189   GNUNET_CONFIGURATION_destroy (cfg);
190   return ok;
191 }
192
193 int
194 main (int argc, char *argv[])
195 {
196   int ret = 0;
197
198   GNUNET_log_setup ("test_client",
199 #if VERBOSE
200                     "DEBUG",
201 #else
202                     "WARNING",
203 #endif
204                     NULL);
205   ret += check ();
206
207   return ret;
208 }
209
210 /* end of test_client.c */