add keywords from metadata for files
[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_NO
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,
74          struct GNUNET_SERVER_Client *client,
75          const struct GNUNET_MessageHeader *message)
76 {
77   struct CopyContext *cc;
78   struct GNUNET_MessageHeader *cpy;
79
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81               "Receiving message from client, bouncing back\n");
82   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
83                  ntohs (message->size));
84   cc = GNUNET_malloc (sizeof (struct CopyContext));
85   cc->client = client;
86   cpy = GNUNET_malloc (ntohs (message->size));
87   memcpy (cpy, message, ntohs (message->size));
88   cc->cpy = cpy;
89   GNUNET_assert (NULL !=
90                  GNUNET_SERVER_notify_transmit_ready (client,
91                                                       ntohs (message->size),
92                                                       GNUNET_TIME_UNIT_SECONDS,
93                                                       &copy_msg, cc));
94 }
95
96
97 static struct GNUNET_SERVER_MessageHandler handlers[] = {
98   {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
99   {NULL, NULL, 0, 0}
100 };
101
102
103 static void
104 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
105 {
106   int *ok = cls;
107   struct GNUNET_MessageHeader msg;
108
109   GNUNET_assert (got != NULL);  /* timeout */
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111               "Receiving bounce, checking content\n");
112   msg.type = htons (MY_TYPE);
113   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
114   GNUNET_assert (0 ==
115                  memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
116   GNUNET_CLIENT_disconnect (client, GNUNET_YES);
117   client = NULL;
118   GNUNET_SERVER_destroy (server);
119   server = NULL;
120   *ok = 0;
121 }
122
123
124 static size_t
125 make_msg (void *cls, size_t size, void *buf)
126 {
127   struct GNUNET_MessageHeader *msg = buf;
128   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
129   msg->type = htons (MY_TYPE);
130   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
132   return sizeof (struct GNUNET_MessageHeader);
133 }
134
135
136 static void
137 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   struct sockaddr_in sa;
140   struct sockaddr * sap[2];
141   socklen_t slens[2];
142
143   sap[0] = (struct sockaddr*) &sa;
144   slens[0] = sizeof (sa);
145   sap[1] = NULL;
146   slens[1] = 0;
147   memset (&sa, 0, sizeof (sa));
148 #if HAVE_SOCKADDR_IN_SIN_LEN
149   sa.sin_len = sizeof (sa);
150 #endif
151   sa.sin_family = AF_INET;
152   sa.sin_port = htons (PORT);
153   server = GNUNET_SERVER_create (NULL,
154                                  NULL,
155                                  sap,
156                                  slens,
157                                  GNUNET_TIME_relative_multiply
158                                  (GNUNET_TIME_UNIT_MILLISECONDS, 10000),
159                                  GNUNET_NO);
160   GNUNET_assert (server != NULL);
161   handlers[0].callback_cls = cls;
162   handlers[1].callback_cls = cls;
163   GNUNET_SERVER_add_handlers (server, handlers);
164   client = GNUNET_CLIENT_connect (MYNAME, cfg);
165   GNUNET_assert (client != NULL);
166   GNUNET_assert (NULL !=
167                  GNUNET_CLIENT_notify_transmit_ready (client,
168                                                       sizeof (struct
169                                                               GNUNET_MessageHeader),
170                                                       GNUNET_TIME_UNIT_SECONDS,
171                                                       GNUNET_NO,
172                                                       &make_msg, NULL));
173   GNUNET_CLIENT_receive (client, &recv_bounce, cls,
174                          GNUNET_TIME_relative_multiply
175                          (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
176 }
177
178
179 /**
180  * Main method, starts scheduler with task1,
181  * checks that "ok" is correct at the end.
182  */
183 static int
184 check ()
185 {
186   int ok;
187
188   cfg = GNUNET_CONFIGURATION_create ();
189   GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
190   GNUNET_CONFIGURATION_set_value_string (cfg,
191                                          MYNAME, "HOSTNAME", "localhost");
192   GNUNET_CONFIGURATION_set_value_string (cfg,
193                                          "resolver", "HOSTNAME", "localhost");
194   ok = 1;
195   GNUNET_SCHEDULER_run (&task, &ok);
196   GNUNET_CONFIGURATION_destroy (cfg);
197   return ok;
198 }
199
200 int
201 main (int argc, char *argv[])
202 {
203   int ret = 0;
204
205   GNUNET_log_setup ("test_client",
206 #if VERBOSE
207                     "DEBUG",
208 #else
209                     "WARNING",
210 #endif
211                     NULL);
212   ret += check ();
213
214   return ret;
215 }
216
217 /* end of test_client.c */