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 3, 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_util_lib.h"
30 #define MYNAME "test_client"
32 static struct GNUNET_CLIENT_Connection *client;
34 static struct GNUNET_SERVER_Handle *server;
36 static struct GNUNET_CONFIGURATION_Handle *cfg;
42 struct GNUNET_SERVER_Client *client;
43 struct GNUNET_MessageHeader *cpy;
47 copy_msg (void *cls, size_t size, void *buf)
49 struct CopyContext *ctx = cls;
50 struct GNUNET_MessageHeader *cpy = ctx->cpy;
52 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
53 GNUNET_assert (size >= ntohs (cpy->size));
54 memcpy (buf, cpy, ntohs (cpy->size));
55 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
59 return sizeof (struct GNUNET_MessageHeader);
64 * Callback that just bounces the message back to the sender.
67 echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
68 const struct GNUNET_MessageHeader *message)
70 struct CopyContext *cc;
71 struct GNUNET_MessageHeader *cpy;
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74 "Receiving message from client, bouncing back\n");
75 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
76 cc = GNUNET_malloc (sizeof (struct CopyContext));
78 cpy = GNUNET_malloc (ntohs (message->size));
79 memcpy (cpy, message, ntohs (message->size));
81 GNUNET_assert (NULL !=
82 GNUNET_SERVER_notify_transmit_ready (client,
83 ntohs (message->size),
84 GNUNET_TIME_UNIT_SECONDS,
89 static struct GNUNET_SERVER_MessageHandler handlers[] = {
90 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
96 recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
99 struct GNUNET_MessageHeader msg;
101 GNUNET_assert (got != NULL); /* timeout */
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n");
103 msg.type = htons (MY_TYPE);
104 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
105 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
106 GNUNET_CLIENT_disconnect (client);
108 GNUNET_SERVER_destroy (server);
115 make_msg (void *cls, size_t size, void *buf)
117 struct GNUNET_MessageHeader *msg = buf;
119 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
120 msg->type = htons (MY_TYPE);
121 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
123 return sizeof (struct GNUNET_MessageHeader);
128 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 struct sockaddr_in sa;
131 struct sockaddr *sap[2];
134 /* test that ill-configured client fails instantly */
135 GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg));
137 /* test IPC between client and server */
138 sap[0] = (struct sockaddr *) &sa;
139 slens[0] = sizeof (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);
149 GNUNET_SERVER_create (NULL, NULL, sap, slens,
150 GNUNET_TIME_relative_multiply
151 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
152 GNUNET_assert (server != NULL);
153 handlers[0].callback_cls = cls;
154 handlers[1].callback_cls = cls;
155 GNUNET_SERVER_add_handlers (server, handlers);
156 client = GNUNET_CLIENT_connect (MYNAME, cfg);
157 GNUNET_assert (client != NULL);
158 GNUNET_assert (NULL !=
159 GNUNET_CLIENT_notify_transmit_ready (client,
161 GNUNET_MessageHeader),
162 GNUNET_TIME_UNIT_SECONDS,
163 GNUNET_NO, &make_msg,
165 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
166 GNUNET_TIME_relative_multiply
167 (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
172 main (int argc, char *argv[])
176 GNUNET_log_setup ("test_client",
179 cfg = GNUNET_CONFIGURATION_create ();
180 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
181 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
182 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
185 GNUNET_SCHEDULER_run (&task, &ok);
186 GNUNET_CONFIGURATION_destroy (cfg);
190 /* end of test_client.c */