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