-dce
[oweals/gnunet.git] / src / util / test_server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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_server.c
22  * @brief tests for server.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_server_lib.h"
29 #include "gnunet_time_lib.h"
30
31 #define PORT 12435
32
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
34
35 #define MY_TYPE 128
36 #define MY_TYPE2 129
37
38 static struct GNUNET_SERVER_Handle *server;
39
40 static struct GNUNET_CLIENT_Connection *cc;
41
42 static struct GNUNET_SERVER_Client *argclient;
43
44 static struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 static int ok;
47
48
49 static void
50 finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   GNUNET_assert (ok == 6);
53   ok = 0;
54   GNUNET_SERVER_destroy (server);
55   GNUNET_CLIENT_disconnect (cc);
56   GNUNET_CONFIGURATION_destroy (cfg);
57 }
58
59
60 static void
61 recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client,
62              const struct GNUNET_MessageHeader *message)
63 {
64   GNUNET_assert (ok == 5);
65   ok = 6;
66   GNUNET_SERVER_receive_done (client, GNUNET_OK);
67   GNUNET_SCHEDULER_add_now (&finish_up, NULL);
68 }
69
70
71 static void
72 first_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
73 {
74   GNUNET_assert (ok == 4);
75   ok = 5;
76   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
77   GNUNET_SERVER_client_drop (argclient);
78   argclient = NULL;
79 }
80
81
82 static size_t
83 reply_msg (void *cls, size_t size, void *buf)
84 {
85   struct GNUNET_MessageHeader msg;
86
87   GNUNET_assert (ok == 3);
88   ok = 4;
89   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
90   msg.type = htons (MY_TYPE);
91   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
92   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
93   return sizeof (struct GNUNET_MessageHeader);
94 }
95
96
97 static void
98 recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
99          const struct GNUNET_MessageHeader *message)
100 {
101   GNUNET_assert (ok == 2);
102   ok = 3;
103   argclient = client;
104   GNUNET_SERVER_client_keep (argclient);
105   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
106   GNUNET_assert (MY_TYPE == ntohs (message->type));
107   GNUNET_assert (NULL !=
108                  GNUNET_SERVER_notify_transmit_ready (client,
109                                                       ntohs (message->size),
110                                                       TIMEOUT, &reply_msg,
111                                                       NULL));
112 }
113
114
115 static struct GNUNET_SERVER_MessageHandler handlers[] = {
116   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
117   {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
118   {NULL, NULL, 0, 0}
119 };
120
121
122 static size_t
123 transmit_second_message (void *cls, size_t size, void *buf)
124 {
125   struct GNUNET_MessageHeader msg;
126
127   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
128   msg.type = htons (MY_TYPE2);
129   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
130   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
131   return sizeof (struct GNUNET_MessageHeader);
132 }
133
134
135 static size_t
136 transmit_initial_message (void *cls, size_t size, void *buf)
137 {
138   struct GNUNET_MessageHeader msg;
139
140   GNUNET_assert (ok == 1);
141   ok = 2;
142   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
143   msg.type = htons (MY_TYPE);
144   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
145   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
146   GNUNET_assert (NULL !=
147                  GNUNET_CLIENT_notify_transmit_ready (cc,
148                                                       sizeof (struct
149                                                               GNUNET_MessageHeader),
150                                                       TIMEOUT, GNUNET_YES,
151                                                       &transmit_second_message,
152                                                       NULL));
153   GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
154   return sizeof (struct GNUNET_MessageHeader);
155 }
156
157
158 static void
159 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   struct sockaddr_in sa;
162   struct sockaddr *sap[2];
163   socklen_t slens[2];
164
165   sap[0] = (struct sockaddr *) &sa;
166   slens[0] = sizeof (sa);
167   sap[1] = NULL;
168   slens[1] = 0;
169   memset (&sa, 0, sizeof (sa));
170 #if HAVE_SOCKADDR_IN_SIN_LEN
171   sa.sin_len = sizeof (sa);
172 #endif
173   sa.sin_family = AF_INET;
174   sa.sin_port = htons (PORT);
175   server = GNUNET_SERVER_create (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
176   GNUNET_assert (server != NULL);
177   GNUNET_SERVER_add_handlers (server, handlers);
178   cfg = GNUNET_CONFIGURATION_create ();
179   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
180   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
181                                          "localhost");
182   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
183                                          "localhost");
184   cc = GNUNET_CLIENT_connect ("test-server", cfg);
185   GNUNET_assert (cc != NULL);
186   GNUNET_assert (NULL !=
187                  GNUNET_CLIENT_notify_transmit_ready (cc,
188                                                       sizeof (struct
189                                                               GNUNET_MessageHeader),
190                                                       TIMEOUT, GNUNET_YES,
191                                                       &transmit_initial_message,
192                                                       NULL));
193 }
194
195
196 /**
197  * Main method, starts scheduler with task1,
198  * checks that "ok" is correct at the end.
199  */
200 static int
201 check ()
202 {
203   ok = 1;
204   GNUNET_SCHEDULER_run (&task, &ok);
205   return ok;
206 }
207
208
209 int
210 main (int argc, char *argv[])
211 {
212   int ret = 0;
213
214   GNUNET_log_setup ("test_server", "WARNING", NULL);
215   ret += check ();
216
217   return ret;
218 }
219
220 /* end of test_server.c */