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 return sizeof (struct GNUNET_MessageHeader);
69 * Callback that just bounces the message back to the sender.
73 struct GNUNET_SERVER_Client *client,
74 const struct GNUNET_MessageHeader *message)
76 struct CopyContext *cc;
77 struct GNUNET_MessageHeader *cpy;
79 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
80 ntohs (message->size));
81 cc = GNUNET_malloc (sizeof (struct CopyContext));
83 cpy = GNUNET_malloc (ntohs (message->size));
84 memcpy (cpy, message, ntohs (message->size));
86 GNUNET_assert (NULL !=
87 GNUNET_SERVER_notify_transmit_ready (client,
88 ntohs (message->size),
89 GNUNET_TIME_UNIT_SECONDS,
94 static struct GNUNET_SERVER_MessageHandler handlers[] = {
95 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
101 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
104 struct GNUNET_MessageHeader msg;
106 GNUNET_assert (got != NULL); /* timeout */
107 msg.type = htons (MY_TYPE);
108 msg.size = htons (sizeof (msg));
109 GNUNET_assert (0 == memcmp (got, &msg, sizeof (msg)));
110 GNUNET_CLIENT_disconnect (client);
112 GNUNET_SERVER_destroy (server);
119 make_msg (void *cls, size_t size, void *buf)
121 struct GNUNET_MessageHeader *msg = buf;
122 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
123 msg->type = htons (MY_TYPE);
124 msg->size = htons (sizeof (msg));
125 return sizeof (struct GNUNET_MessageHeader);
130 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 struct sockaddr_in sa;
134 memset (&sa, 0, sizeof (sa));
135 sa.sin_family = AF_INET;
136 sa.sin_port = htons (PORT);
137 server = GNUNET_SERVER_create (tc->sched,
140 (const struct sockaddr *) &sa,
143 GNUNET_TIME_relative_multiply
144 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
146 GNUNET_assert (server != NULL);
147 handlers[0].callback_cls = cls;
148 handlers[1].callback_cls = cls;
149 GNUNET_SERVER_add_handlers (server, handlers);
150 client = GNUNET_CLIENT_connect (tc->sched, MYNAME, cfg);
151 GNUNET_assert (client != NULL);
152 GNUNET_assert (NULL !=
153 GNUNET_CLIENT_notify_transmit_ready (client,
155 GNUNET_MessageHeader),
156 GNUNET_TIME_UNIT_SECONDS,
158 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
159 GNUNET_TIME_relative_multiply
160 (GNUNET_TIME_UNIT_MILLISECONDS, 250));
165 * Main method, starts scheduler with task1,
166 * checks that "ok" is correct at the end.
173 cfg = GNUNET_CONFIGURATION_create ();
174 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
175 GNUNET_CONFIGURATION_set_value_string (cfg,
176 MYNAME, "HOSTNAME", "localhost");
178 GNUNET_SCHEDULER_run (&task, &ok);
179 GNUNET_CONFIGURATION_destroy (cfg);
184 main (int argc, char *argv[])
188 GNUNET_log_setup ("test_client", "WARNING", NULL);
194 /* end of test_client.c */