indentation
[oweals/gnunet.git] / src / util / test_server_with_client_unix.c
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 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_with_client_unix.c
22  * @brief tests for server.c and client.c,
23  *       specifically disconnect_notify,
24  *       client_get_address and receive_done (resume processing)
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_time_lib.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define MY_TYPE 128
36
37
38 static struct GNUNET_SERVER_Handle *server;
39
40 static struct GNUNET_CLIENT_Connection *client;
41
42 static struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 static int ok;
45
46 static void
47 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
48 {
49   struct GNUNET_SERVER_Client *argclient = cls;
50
51   GNUNET_assert (ok == 3);
52   ok++;
53   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
54 }
55
56
57 static void
58 recv_cb (void *cls,
59          struct GNUNET_SERVER_Client *argclient,
60          const struct GNUNET_MessageHeader *message)
61 {
62   switch (ok)
63   {
64   case 2:
65     ok++;
66     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
67                                   (GNUNET_TIME_UNIT_MILLISECONDS, 50),
68                                   &send_done, argclient);
69     break;
70   case 4:
71     ok++;
72     GNUNET_CLIENT_disconnect (client, GNUNET_YES);
73     GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
74     break;
75   default:
76     GNUNET_assert (0);
77   }
78
79 }
80
81
82 static void
83 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   GNUNET_SERVER_destroy (server);
86   server = NULL;
87   GNUNET_CONFIGURATION_destroy (cfg);
88   cfg = NULL;
89 }
90
91
92 /**
93  * Functions with this signature are called whenever a client
94  * is disconnected on the network level.
95  *
96  * @param cls closure
97  * @param client identification of the client
98  */
99 static void
100 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
101 {
102   if (client == NULL)
103     return;
104   GNUNET_assert (ok == 5);
105   ok = 0;
106   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
107 }
108
109
110 static size_t
111 notify_ready (void *cls, size_t size, void *buf)
112 {
113   struct GNUNET_MessageHeader *msg;
114
115   GNUNET_assert (size >= 256);
116   GNUNET_assert (1 == ok);
117   ok++;
118   msg = buf;
119   msg->type = htons (MY_TYPE);
120   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
121   msg++;
122   msg->type = htons (MY_TYPE);
123   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
124   return 2 * sizeof (struct GNUNET_MessageHeader);
125 }
126
127
128 static struct GNUNET_SERVER_MessageHandler handlers[] = {
129   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
130   {NULL, NULL, 0, 0}
131 };
132
133
134 static void
135 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct sockaddr_un un;
138   const char *unixpath = "/tmp/testsock";
139   size_t slen = strlen (unixpath);
140   struct sockaddr *sap[2];
141   socklen_t slens[2];
142
143   memset (&un, 0, sizeof (un));
144   un.sun_family = AF_UNIX;
145   memcpy (un.sun_path, unixpath, slen);
146   un.sun_path[slen] = '\0';
147 #if HAVE_SOCKADDR_IN_SIN_LEN
148   un.sun_len = (u_char) sizeof (un);
149 #endif
150 #if LINUX
151   un.sun_path[0] = '\0';
152 #endif
153
154
155   sap[0] = (struct sockaddr *) &un;
156   slens[0] = sizeof (un);
157   sap[1] = NULL;
158   slens[1] = 0;
159   server = GNUNET_SERVER_create (NULL,
160                                  NULL,
161                                  sap,
162                                  slens,
163                                  GNUNET_TIME_relative_multiply
164                                  (GNUNET_TIME_UNIT_MILLISECONDS, 250),
165                                  GNUNET_NO);
166   GNUNET_assert (server != NULL);
167   handlers[0].callback_cls = cls;
168   GNUNET_SERVER_add_handlers (server, handlers);
169   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
170   cfg = GNUNET_CONFIGURATION_create ();
171
172   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
173   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
174                                          "localhost");
175
176   client = GNUNET_CLIENT_connect ("test", cfg);
177   GNUNET_assert (client != NULL);
178   GNUNET_CLIENT_notify_transmit_ready (client,
179                                        256,
180                                        GNUNET_TIME_relative_multiply
181                                        (GNUNET_TIME_UNIT_MILLISECONDS, 250),
182                                        GNUNET_NO, &notify_ready, NULL);
183 }
184
185
186 /**
187  * Main method, starts scheduler with task1,
188  * checks that "ok" is correct at the end.
189  */
190 static int
191 check ()
192 {
193
194   ok = 1;
195   GNUNET_SCHEDULER_run (&task, NULL);
196   return ok;
197 }
198
199
200 int
201 main (int argc, char *argv[])
202 {
203   int ret = 0;
204
205   GNUNET_log_setup ("test_server_with_client_unix",
206 #if VERBOSE
207                     "DEBUG",
208 #else
209                     "WARNING",
210 #endif
211                     NULL);
212   ret += check ();
213
214   return ret;
215 }
216
217 /* end of test_server_with_client_unix.c */