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