aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_client.c')
-rw-r--r--src/util/test_client.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/src/util/test_client.c b/src/util/test_client.c
new file mode 100644
index 000000000..5c2e552e9
--- /dev/null
+++ b/src/util/test_client.c
@@ -0,0 +1,195 @@
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 2, 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#define VERBOSE GNUNET_NO
33
34#define PORT 14325
35
36#define MYNAME "test_client"
37
38static struct GNUNET_CLIENT_Connection *client;
39
40static struct GNUNET_SERVER_Handle *server;
41
42static struct GNUNET_CONFIGURATION_Handle *cfg;
43
44#define MY_TYPE 130
45
46struct CopyContext
47{
48 struct GNUNET_SERVER_Client *client;
49 struct GNUNET_MessageHeader *cpy;
50};
51
52static size_t
53copy_msg (void *cls, size_t size, void *buf)
54{
55 struct CopyContext *ctx = cls;
56 struct GNUNET_MessageHeader *cpy = ctx->cpy;
57
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);
62 GNUNET_free (cpy);
63 GNUNET_free (ctx);
64 return sizeof (struct GNUNET_MessageHeader);
65}
66
67
68/**
69 * Callback that just bounces the message back to the sender.
70 */
71static void
72echo_cb (void *cls,
73 struct GNUNET_SERVER_Handle *server,
74 struct GNUNET_SERVER_Client *client,
75 const struct GNUNET_MessageHeader *message)
76{
77 struct CopyContext *cc;
78 struct GNUNET_MessageHeader *cpy;
79
80 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
81 ntohs (message->size));
82 cc = GNUNET_malloc (sizeof (struct CopyContext));
83 cc->client = client;
84 cpy = GNUNET_malloc (ntohs (message->size));
85 memcpy (cpy, message, ntohs (message->size));
86 cc->cpy = cpy;
87 GNUNET_assert (NULL !=
88 GNUNET_SERVER_notify_transmit_ready (client,
89 ntohs (message->size),
90 GNUNET_TIME_UNIT_SECONDS,
91 &copy_msg, cc));
92}
93
94
95static struct GNUNET_SERVER_MessageHandler handlers[] = {
96 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
97 {NULL, NULL, 0, 0}
98};
99
100
101static void
102recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
103{
104 int *ok = cls;
105 struct GNUNET_MessageHeader msg;
106
107 GNUNET_assert (got != NULL); /* timeout */
108 msg.type = htons (MY_TYPE);
109 msg.size = htons (sizeof (msg));
110 GNUNET_assert (0 == memcmp (got, &msg, sizeof (msg)));
111 GNUNET_CLIENT_disconnect (client);
112 client = NULL;
113 GNUNET_SERVER_destroy (server);
114 server = NULL;
115 *ok = 0;
116}
117
118
119static size_t
120make_msg (void *cls, size_t size, void *buf)
121{
122 struct GNUNET_MessageHeader *msg = buf;
123 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
124 msg->type = htons (MY_TYPE);
125 msg->size = htons (sizeof (msg));
126 return sizeof (struct GNUNET_MessageHeader);
127}
128
129
130static void
131task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132{
133 struct sockaddr_in sa;
134
135 memset (&sa, 0, sizeof (sa));
136 sa.sin_family = AF_INET;
137 sa.sin_port = htons (PORT);
138 server = GNUNET_SERVER_create (tc->sched,
139 NULL,
140 NULL,
141 (const struct sockaddr *) &sa,
142 sizeof (sa),
143 1024,
144 GNUNET_TIME_relative_multiply
145 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
146 GNUNET_NO);
147 GNUNET_assert (server != NULL);
148 handlers[0].callback_cls = cls;
149 handlers[1].callback_cls = cls;
150 GNUNET_SERVER_add_handlers (server, handlers);
151 client = GNUNET_CLIENT_connect (tc->sched, MYNAME, cfg);
152 GNUNET_assert (client != NULL);
153 GNUNET_assert (NULL !=
154 GNUNET_CLIENT_notify_transmit_ready (client,
155 sizeof (struct
156 GNUNET_MessageHeader),
157 GNUNET_TIME_UNIT_SECONDS,
158 &make_msg, NULL));
159 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
160 GNUNET_TIME_relative_multiply
161 (GNUNET_TIME_UNIT_MILLISECONDS, 250));
162}
163
164
165/**
166 * Main method, starts scheduler with task1,
167 * checks that "ok" is correct at the end.
168 */
169static int
170check ()
171{
172 int ok;
173
174 cfg = GNUNET_CONFIGURATION_create ();
175 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
176 GNUNET_CONFIGURATION_set_value_string (cfg,
177 MYNAME, "HOSTNAME", "localhost");
178 ok = 1;
179 GNUNET_SCHEDULER_run (&task, &ok);
180 GNUNET_CONFIGURATION_destroy (cfg);
181 return ok;
182}
183
184int
185main (int argc, char *argv[])
186{
187 int ret = 0;
188
189 GNUNET_log_setup ("test_client", "WARNING", NULL);
190 ret += check ();
191
192 return ret;
193}
194
195/* end of test_client.c */