2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
21 * @file util/test_client.c
22 * @brief tests for client.c
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"
32 #define VERBOSE GNUNET_NO
36 #define MYNAME "test_client"
38 static struct GNUNET_CLIENT_Connection *client;
40 static struct GNUNET_SERVER_Handle *server;
42 static struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_SERVER_Client *client;
49 struct GNUNET_MessageHeader *cpy;
53 copy_msg (void *cls, size_t size, void *buf)
55 struct CopyContext *ctx = cls;
56 struct GNUNET_MessageHeader *cpy = ctx->cpy;
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);
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "Message bounced back to client\n");
66 return sizeof (struct GNUNET_MessageHeader);
71 * Callback that just bounces the message back to the sender.
75 struct GNUNET_SERVER_Client *client,
76 const struct GNUNET_MessageHeader *message)
78 struct CopyContext *cc;
79 struct GNUNET_MessageHeader *cpy;
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82 "Receiving message from client, bouncing back\n");
83 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
84 ntohs (message->size));
85 cc = GNUNET_malloc (sizeof (struct CopyContext));
87 cpy = GNUNET_malloc (ntohs (message->size));
88 memcpy (cpy, message, ntohs (message->size));
90 GNUNET_assert (NULL !=
91 GNUNET_SERVER_notify_transmit_ready (client,
92 ntohs (message->size),
93 GNUNET_TIME_UNIT_SECONDS,
98 static struct GNUNET_SERVER_MessageHandler handlers[] = {
99 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
105 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
108 struct GNUNET_MessageHeader msg;
110 GNUNET_assert (got != NULL); /* timeout */
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Receiving bounce, checking content\n");
113 msg.type = htons (MY_TYPE);
114 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
115 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
116 GNUNET_CLIENT_disconnect (client);
118 GNUNET_SERVER_destroy (server);
125 make_msg (void *cls, size_t size, void *buf)
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,
132 "Creating message for transmission\n");
133 return sizeof (struct GNUNET_MessageHeader);
138 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 struct sockaddr_in sa;
142 memset (&sa, 0, sizeof (sa));
143 #if HAVE_SOCKADDR_IN_SIN_LEN
144 sa.sin_len = sizeof (sa);
146 sa.sin_family = AF_INET;
147 sa.sin_port = htons (PORT);
148 server = GNUNET_SERVER_create (tc->sched,
151 (const struct sockaddr *) &sa,
154 GNUNET_TIME_relative_multiply
155 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
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 (tc->sched, MYNAME, cfg);
162 GNUNET_assert (client != NULL);
163 GNUNET_assert (NULL !=
164 GNUNET_CLIENT_notify_transmit_ready (client,
166 GNUNET_MessageHeader),
167 GNUNET_TIME_UNIT_SECONDS,
169 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
170 GNUNET_TIME_relative_multiply
171 (GNUNET_TIME_UNIT_MILLISECONDS, 250));
176 * Main method, starts scheduler with task1,
177 * checks that "ok" is correct at the end.
184 cfg = GNUNET_CONFIGURATION_create ();
185 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
186 GNUNET_CONFIGURATION_set_value_string (cfg,
187 MYNAME, "HOSTNAME", "localhost");
188 GNUNET_CONFIGURATION_set_value_string (cfg,
189 "resolver", "HOSTNAME", "localhost");
191 GNUNET_SCHEDULER_run (&task, &ok);
192 GNUNET_CONFIGURATION_destroy (cfg);
197 main (int argc, char *argv[])
201 GNUNET_log_setup ("test_client",
213 /* end of test_client.c */