2 This file is part of GNUnet.
3 Copyright (C) 2009, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_MQ_Handle *mq;
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;
48 copy_msg (void *cls, size_t size, void *buf)
50 struct CopyContext *ctx = cls;
51 struct GNUNET_MessageHeader *cpy = ctx->cpy;
53 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
54 GNUNET_assert (size >= ntohs (cpy->size));
55 GNUNET_memcpy (buf, cpy, ntohs (cpy->size));
56 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60 "Message bounced back to client\n");
61 return sizeof (struct GNUNET_MessageHeader);
66 * Callback that just bounces the message back to the sender.
70 struct GNUNET_SERVER_Client *client,
71 const struct GNUNET_MessageHeader *message)
73 struct CopyContext *cc;
74 struct GNUNET_MessageHeader *cpy;
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 "Receiving message from client, bouncing back\n");
78 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
79 cc = GNUNET_new (struct CopyContext);
81 cpy = GNUNET_malloc (ntohs (message->size));
82 GNUNET_memcpy (cpy, message, ntohs (message->size));
84 GNUNET_assert (NULL !=
85 GNUNET_SERVER_notify_transmit_ready (client,
86 ntohs (message->size),
87 GNUNET_TIME_UNIT_SECONDS,
92 static struct GNUNET_SERVER_MessageHandler handlers[] = {
93 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
99 handle_bounce (void *cls,
100 const struct GNUNET_MessageHeader *got)
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Receiving bounce, checking content\n");
106 GNUNET_assert (NULL != got);
107 GNUNET_MQ_destroy (mq);
109 GNUNET_SERVER_destroy (server);
116 * Generic error handler, called with the appropriate error code and
117 * the same closure specified at the creation of the message queue.
118 * Not every message queue implementation supports an error handler.
120 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
121 * @param error error code
124 mq_error_handler (void *cls,
125 enum GNUNET_MQ_Error error)
127 GNUNET_assert (0); /* should never happen */
134 struct sockaddr_in sa;
135 struct sockaddr *sap[2];
137 struct GNUNET_MQ_Envelope *env;
138 struct GNUNET_MessageHeader *msg;
139 GNUNET_MQ_hd_fixed_size (bounce,
141 struct GNUNET_MessageHeader);
142 struct GNUNET_MQ_MessageHandler chandlers[] = {
143 make_bounce_handler (cls),
144 GNUNET_MQ_handler_end ()
147 /* test that ill-configured client fails instantly */
148 GNUNET_assert (NULL == GNUNET_CLIENT_connecT (cfg,
154 /* test IPC between client and server */
155 sap[0] = (struct sockaddr *) &sa;
156 slens[0] = sizeof (sa);
159 memset (&sa, 0, sizeof (sa));
160 #if HAVE_SOCKADDR_IN_SIN_LEN
161 sa.sin_len = sizeof (sa);
163 sa.sin_family = AF_INET;
164 sa.sin_port = htons (PORT);
166 GNUNET_SERVER_create (NULL, NULL, sap, slens,
167 GNUNET_TIME_relative_multiply
168 (GNUNET_TIME_UNIT_SECONDS, 10), GNUNET_NO);
169 GNUNET_assert (server != NULL);
170 handlers[0].callback_cls = cls;
171 handlers[1].callback_cls = cls;
172 GNUNET_SERVER_add_handlers (server, handlers);
173 mq = GNUNET_CLIENT_connecT (cfg,
178 GNUNET_assert (NULL != mq);
179 env = GNUNET_MQ_msg (msg,
187 main (int argc, char *argv[])
191 GNUNET_log_setup ("test_client",
194 cfg = GNUNET_CONFIGURATION_create ();
195 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
196 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "localhost");
198 GNUNET_SCHEDULER_run (&task, &ok);
199 GNUNET_CONFIGURATION_destroy (cfg);
203 /* end of test_client.c */