-add adv port
[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
33 #define PORT 14325
34
35 #define MYNAME "test_client"
36
37 static struct GNUNET_CLIENT_Connection *client;
38
39 static struct GNUNET_SERVER_Handle *server;
40
41 static struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 #define MY_TYPE 130
44
45 struct CopyContext
46 {
47   struct GNUNET_SERVER_Client *client;
48   struct GNUNET_MessageHeader *cpy;
49 };
50
51 static size_t
52 copy_msg (void *cls, size_t size, void *buf)
53 {
54   struct CopyContext *ctx = cls;
55   struct GNUNET_MessageHeader *cpy = ctx->cpy;
56
57   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
58   GNUNET_assert (size >= ntohs (cpy->size));
59   memcpy (buf, cpy, ntohs (cpy->size));
60   GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
61   GNUNET_free (cpy);
62   GNUNET_free (ctx);
63   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
64   return sizeof (struct GNUNET_MessageHeader);
65 }
66
67
68 /**
69  * Callback that just bounces the message back to the sender.
70  */
71 static void
72 echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
73          const struct GNUNET_MessageHeader *message)
74 {
75   struct CopyContext *cc;
76   struct GNUNET_MessageHeader *cpy;
77
78   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79               "Receiving message from client, bouncing back\n");
80   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
81   cc = GNUNET_malloc (sizeof (struct CopyContext));
82   cc->client = client;
83   cpy = GNUNET_malloc (ntohs (message->size));
84   memcpy (cpy, message, ntohs (message->size));
85   cc->cpy = cpy;
86   GNUNET_assert (NULL !=
87                  GNUNET_SERVER_notify_transmit_ready (client,
88                                                       ntohs (message->size),
89                                                       GNUNET_TIME_UNIT_SECONDS,
90                                                       &copy_msg, cc));
91 }
92
93
94 static struct GNUNET_SERVER_MessageHandler handlers[] = {
95   {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
96   {NULL, NULL, 0, 0}
97 };
98
99
100 static void
101 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
102 {
103   int *ok = cls;
104   struct GNUNET_MessageHeader msg;
105
106   GNUNET_assert (got != NULL);  /* timeout */
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n");
108   msg.type = htons (MY_TYPE);
109   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
110   GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
111   GNUNET_CLIENT_disconnect (client);
112   client = NULL;
113   GNUNET_SERVER_destroy (server);
114   server = NULL;
115   *ok = 0;
116 }
117
118
119 static size_t
120 make_msg (void *cls, size_t size, void *buf)
121 {
122   struct GNUNET_MessageHeader *msg = buf;
123
124   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
125   msg->type = htons (MY_TYPE);
126   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
128   return sizeof (struct GNUNET_MessageHeader);
129 }
130
131
132 static void
133 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   struct sockaddr_in sa;
136   struct sockaddr *sap[2];
137   socklen_t slens[2];
138
139   /* test that ill-configured client fails instantly */
140   GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg));
141
142   /* test IPC between client and server */
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 =
154       GNUNET_SERVER_create (NULL, NULL, sap, slens,
155                             GNUNET_TIME_relative_multiply
156                             (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
157   GNUNET_assert (server != NULL);
158   handlers[0].callback_cls = cls;
159   handlers[1].callback_cls = cls;
160   GNUNET_SERVER_add_handlers (server, handlers);
161   client = GNUNET_CLIENT_connect (MYNAME, cfg);
162   GNUNET_assert (client != NULL);
163   GNUNET_assert (NULL !=
164                  GNUNET_CLIENT_notify_transmit_ready (client,
165                                                       sizeof (struct
166                                                               GNUNET_MessageHeader),
167                                                       GNUNET_TIME_UNIT_SECONDS,
168                                                       GNUNET_NO, &make_msg,
169                                                       NULL));
170   GNUNET_CLIENT_receive (client, &recv_bounce, cls,
171                          GNUNET_TIME_relative_multiply
172                          (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
173 }
174
175
176 int
177 main (int argc, char *argv[])
178 {
179   int ok;
180
181   GNUNET_log_setup ("test_client",
182                     "WARNING",
183                     NULL);
184   cfg = GNUNET_CONFIGURATION_create ();
185   GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
186   GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
187   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
188                                          "localhost");
189   ok = 1;
190   GNUNET_SCHEDULER_run (&task, &ok);
191   GNUNET_CONFIGURATION_destroy (cfg);
192   return ok;
193 }
194
195 /* end of test_client.c */