-more libexec fixes for OpenSUSE
[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 MY_TYPE 128
34
35
36 static struct GNUNET_SERVER_Handle *server;
37
38 static struct GNUNET_CLIENT_Connection *client;
39
40 static struct GNUNET_CONFIGURATION_Handle *cfg;
41
42 static int ok;
43
44
45 static void
46 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 {
48   struct GNUNET_SERVER_Client *argclient = cls;
49
50   GNUNET_assert (ok == 3);
51   ok++;
52   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
53 }
54
55
56 static void
57 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
58          const struct GNUNET_MessageHeader *message)
59 {
60   switch (ok)
61   {
62   case 2:
63     ok++;
64     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
65                                   (GNUNET_TIME_UNIT_MILLISECONDS, 50),
66                                   &send_done, argclient);
67     break;
68   case 4:
69     ok++;
70     GNUNET_CLIENT_disconnect (client);
71     GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
72     break;
73   default:
74     GNUNET_assert (0);
75   }
76
77 }
78
79
80 static void
81 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_SERVER_destroy (server);
84   server = NULL;
85   GNUNET_CONFIGURATION_destroy (cfg);
86   cfg = NULL;
87 }
88
89
90 /**
91  * Functions with this signature are called whenever a client
92  * is disconnected on the network level.
93  *
94  * @param cls closure
95  * @param client identification of the client
96  */
97 static void
98 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
99 {
100   if (client == NULL)
101     return;
102   GNUNET_assert (ok == 5);
103   ok = 0;
104   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
105 }
106
107
108 static size_t
109 notify_ready (void *cls, size_t size, void *buf)
110 {
111   struct GNUNET_MessageHeader *msg;
112
113   GNUNET_assert (size >= 256);
114   GNUNET_assert (1 == ok);
115   ok++;
116   msg = buf;
117   msg->type = htons (MY_TYPE);
118   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
119   msg++;
120   msg->type = htons (MY_TYPE);
121   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
122   return 2 * sizeof (struct GNUNET_MessageHeader);
123 }
124
125
126 static struct GNUNET_SERVER_MessageHandler handlers[] = {
127   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
128   {NULL, NULL, 0, 0}
129 };
130
131
132 static void
133 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   struct sockaddr_un un;
136   const char *unixpath = "/tmp/testsock";
137   size_t slen = strlen (unixpath);
138   struct sockaddr *sap[2];
139   socklen_t slens[2];
140
141   memset (&un, 0, sizeof (un));
142   un.sun_family = AF_UNIX;
143   memcpy (un.sun_path, unixpath, slen);
144   un.sun_path[slen] = '\0';
145 #if HAVE_SOCKADDR_IN_SIN_LEN
146   un.sun_len = (u_char) sizeof (un);
147 #endif
148 #if LINUX
149   un.sun_path[0] = '\0';
150 #endif
151
152   sap[0] = (struct sockaddr *) &un;
153   slens[0] = sizeof (un);
154   sap[1] = NULL;
155   slens[1] = 0;
156   server =
157       GNUNET_SERVER_create (NULL, NULL, sap, slens,
158                             GNUNET_TIME_relative_multiply
159                             (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
160   GNUNET_assert (server != NULL);
161   handlers[0].callback_cls = cls;
162   GNUNET_SERVER_add_handlers (server, handlers);
163   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
164   cfg = GNUNET_CONFIGURATION_create ();
165
166   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
167   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
168                                          "localhost");
169
170   client = GNUNET_CLIENT_connect ("test", cfg);
171   GNUNET_assert (client != NULL);
172   GNUNET_CLIENT_notify_transmit_ready (client, 256,
173                                        GNUNET_TIME_relative_multiply
174                                        (GNUNET_TIME_UNIT_MILLISECONDS, 250),
175                                        GNUNET_NO, &notify_ready, NULL);
176 }
177
178
179 int
180 main (int argc, char *argv[])
181 {
182   GNUNET_log_setup ("test_server_with_client_unix",
183                     "WARNING",
184                     NULL);
185   ok = 1;
186   GNUNET_SCHEDULER_run (&task, NULL);
187   return ok;
188 }
189
190 /* end of test_server_with_client_unix.c */