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